1#!/usr/bin/env perl
2# Test the 'stats items' evictions counters.
3
4use strict;
5use Test::More tests => 309;
6use FindBin qw($Bin);
7use lib "$Bin/lib";
8use MemcachedTest;
9
10my $server = new_memcached("-m 3 -o modern,slab_automove_window=3");
11my $sock = $server->sock;
12my $value = "B"x66560;
13my $key = 0;
14
15# These aren't set to expire.
16for ($key = 0; $key < 40; $key++) {
17    print $sock "set key$key 0 0 66560\r\n$value\r\n";
18    is(scalar <$sock>, "STORED\r\n", "stored key$key");
19}
20
21my $stats  = mem_stats($sock);
22my $evicted = $stats->{evictions};
23isnt($evicted, "0", "check evicted");
24
25# We're past the memory limit. Try adjusting maxbytes upward.
26$stats = mem_stats($sock, "settings");
27my $pre_maxbytes = $stats->{"maxbytes"};
28print $sock "cache_memlimit 8\r\n";
29is(scalar <$sock>, "OK\r\n", "bumped maxbytes from 3m to 8m");
30
31# Confirm maxbytes updated.
32$stats = mem_stats($sock, "settings");
33isnt($stats->{"maxbytes"}, $pre_maxbytes, "stats settings maxbytes updated");
34
35# Check for total_malloced increasing as new memory is added
36$stats = mem_stats($sock, "slabs");
37my $t_malloc = $stats->{"total_malloced"};
38
39print $sock "set toast 0 0 66560\r\n$value\r\n";
40is(scalar <$sock>, "STORED\r\n", "stored toast");
41$stats = mem_stats($sock, "slabs");
42cmp_ok($stats->{"total_malloced"}, '>', $t_malloc, "stats slabs total_malloced increased");
43
44$stats = mem_stats($sock);
45my $new_evicted = $stats->{evictions};
46cmp_ok($new_evicted, '==', $evicted, "no new evictions");
47
48# Bump up to 16, fill a bit more, then delete everything.
49print $sock "cache_memlimit 16\r\n";
50is(scalar <$sock>, "OK\r\n", "bumped maxbytes from 8m to 16m");
51for (;$key < 150; $key++) {
52    print $sock "set key$key 0 0 66560\r\n$value\r\n";
53    is(scalar <$sock>, "STORED\r\n", "stored key$key");
54}
55
56# Grab total_malloced after filling everything up.
57$stats = mem_stats($sock, "slabs");
58$t_malloc = $stats->{"total_malloced"};
59print $sock "cache_memlimit 8\r\n";
60is(scalar <$sock>, "OK\r\n", "bumped maxbytes from 16m to 8m");
61
62# Remove all of the keys, allowing the slab rebalancer to push pages toward
63# the global page pool.
64for ($key = 0; $key < 150; $key++) {
65    print $sock "delete key$key\r\n";
66    like(scalar <$sock>, qr/(DELETED|NOT_FOUND)\r\n/, "deleted key$key");
67}
68
69# If memory limit is lower, it should free those pages back to the OS.
70my $reduced = 0;
71for (my $tries = 0; $tries < 6; $tries++) {
72    sleep 1;
73    $stats = mem_stats($sock, "slabs");
74    $reduced = $stats->{"total_malloced"} if ($t_malloc > $stats->{"total_malloced"});
75    last if $reduced;
76}
77
78isnt($reduced, 0, "total_malloced reduced to $reduced");
79