-
Notifications
You must be signed in to change notification settings - Fork 3
/
memcached-itool.php
executable file
·396 lines (316 loc) · 9.92 KB
/
memcached-itool.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
<?php
/**
* @author: Andrey Niakhaichyk [email protected]
* @version: 1.0
*/
const DEFAULT_TCP_TIMEOUT = 30;
const DEFAULT_UNIXSOCKET_TIMEOUT = 5;
const DUMPMODE_ONLYKEYS = 0;
const DUMPMODE_KEYVALUES = 1;
const REMOVEMODE_EXPIRED = 2;
$params = $_SERVER['argv'];
// Parse command line
$host = 'localhost';
$port = 11211;
$socketpath = NULL;
$mode = 'display';
if(!empty($params[1]))
{
if(strpos($params[1], '/') === 0)
{
$socketpath = $params[1];
$host = NULL;
$port = NULL;
}
elseif(strpos($params[1], ':') > 0)
list($host, $port) = explode(':', $params[1]);
else
$host = $params[1];
}
if(!empty($params[2]))
{
$mode = $params[2];
}
// Check params
if(count($params) < 2 || !in_array($mode, array('display', 'dumpkeys', 'dump', 'removeexp', 'settings', 'stats', 'sizes')))
{
help();
exit;
}
// Connect to memcached
$errno = NULL;
$errstr = NULL;
if($host && $port)
$fp = stream_socket_client("tcp://{$host}:{$port}", $errno, $errstr, DEFAULT_TCP_TIMEOUT);
else
$fp = stream_socket_client("unix://{$socketpath}", $errno, $errstr, DEFAULT_UNIXSOCKET_TIMEOUT);
if (!$fp)
{
echo "$errstr ($errno)".PHP_EOL;
exit -1;
}
// Run logic
switch($mode)
{
case 'settings':
show_stats($fp, 'stats settings');
break;
case 'stats':
show_stats($fp, 'stats');
break;
case 'sizes':
sizes($fp);
break;
case 'dumpkeys':
iterkeys($fp, DUMPMODE_ONLYKEYS);
break;
case 'removeexp':
iterkeys($fp, REMOVEMODE_EXPIRED);
break;
case 'dump':
iterkeys($fp, DUMPMODE_KEYVALUES);
break;
case 'display':
default:
display($fp);
break;
}
exit;
// # -------------------- functions
function help()
{
echo <<<HELP
Usage: memcached-itool <host[:port] | /path/to/socket> [mode]
memcached-itool localhost:11211 display # shows slabs information (display is default mode)
memcached-itool localhost:11211 dumpkeys # dumps only keys names
memcached-itool localhost:11211 dump # dumps keys and values, values only for non expired keys
memcached-itool localhost:11211 removeexp # remove expired keys (you may need run several times)
memcached-itool localhost:11211 settings # shows memcached settings
memcached-itool localhost:11211 sizes # group keys by sizes and show how many we waste memory
memcached-itool localhost:11211 stats # shows general stats
Warning! dumpkeys, dump, removeexp and sizes modes *will* lock up your cache! It iterates over *every item* and examines the size.
While the operation is fast, if you have many items you could prevent memcached from serving requests for several seconds.
Warning! dump and removeexp modes influence on memcached internal statistic like *expired_unfetched* and *get_misses*. So we recommend only use it for debugging purposes.
HELP;
}
function send_and_receive($fp, $command)
{
fwrite($fp, $command."\r\n");
$result = '';
while (!feof($fp))
{
$result .= fgets($fp);
if(strpos($result, 'END'."\r\n") !== FALSE)
break;
}
$lines = explode("\r\n", $result);
foreach($lines as $key=>$line)
{
if(strlen($line) == 0 || trim($line) == 'END')
unset($lines[$key]);
}
return $lines;
}
function slabs_stats($fp)
{
$slabs = array();
$lines = send_and_receive($fp, 'stats slabs');
foreach($lines as $line)
{
$m = array();
if(preg_match('/^STAT (\d+):(\w+) (\d+)/', $line, $m))
{
$slab_num = $m[1];
$slab_property = $m[2];
$slab_value = $m[3];
$slabs[$slab_num][$slab_property] = $slab_value;
}
if(preg_match('/^STAT (\w+) (\d+)/', $line, $m))
{
$slab_property = $m[1];
$slab_value = $m[2];
$slabs['total'][$slab_property] = $slab_value;
}
}
$lines = send_and_receive($fp, 'stats items');
foreach($lines as $line)
{
$m = array();
if(preg_match('/^STAT items:(\d+):(\w+) (\d+)/', $line, $m))
{
$slab_num = $m[1];
$slab_property = $m[2];
$slab_value = $m[3];
$slabs[$slab_num][$slab_property] = $slab_value;
}
}
foreach($slabs as $num => $slab)
{
if($num == 'total')
continue;
$slab['age'] = !empty($slab['age']) ? $slab['age'] : 0;
$slab['number'] = !empty($slab['number']) ? $slab['number'] : 0;
$slab['evicted'] = !empty($slab['evicted']) ? $slab['evicted'] : 0;
$slab['evicted_time'] = !empty($slab['evicted_time']) ? $slab['evicted_time'] : 0;
$slab['outofmemory'] = !empty($slab['outofmemory']) ? $slab['outofmemory'] : 0;
$slabs[$num] = $slab;
}
ksort($slabs);
return $slabs;
}
function get_stats($fp, $command = 'stats')
{
$stats = array();
$lines = send_and_receive($fp, $command);
foreach($lines as $line)
{
$m = array();
if(preg_match('/^STAT ([^\s]+) ([^\s]+)/', $line, $m))
{
$property = $m[1];
$value = $m[2];
$stats[$property] = $value;
}
}
ksort($stats);
return $stats;
}
function settings_stats($fp)
{
$stats = array();
$lines = send_and_receive($fp, 'stats settings');
foreach($lines as $line)
{
$m = array();
if(preg_match('/^STAT ([^\s]+) ([^\s]+)/', $line, $m))
{
$property = $m[1];
$value = $m[2];
$stats[$property] = $value;
}
}
return $stats;
}
function display($fp)
{
$slabs = slabs_stats($fp);
print " # Chunk_Size Max_age Pages Count Full? Evicted Evict_Time OOM Used Wasted".PHP_EOL;
foreach($slabs as $num => $slab)
{
if($num == 'total')
continue;
$is_slab_full = $slab['free_chunks_end'] == 0 ? "yes" : "no";
$wasted = $slab['number'] ? (1.0 - (float)$slab['mem_requested'] / ($slab['chunk_size'] * $slab['number'])) * 100 : 0.0;
printf("%3d %10s %7ds %7d %7d %7s %8d %10d %3d %8s %7d%%".PHP_EOL, $num, descriptive_size($slab['chunk_size']), $slab['age'], $slab['total_pages'], $slab['number'], $is_slab_full,
$slab['evicted'], $slab['evicted_time'], $slab['outofmemory'], descriptive_size($slab['mem_requested']), $wasted);
}
print PHP_EOL."Total:".PHP_EOL;
foreach($slabs['total'] as $property=>$value)
{
if($property == 'total_malloced')
printf("%-15s %12s".PHP_EOL, $property, descriptive_size($value));
else
printf("%-15s %12s".PHP_EOL, $property, $value);
}
$stats = settings_stats($fp);
$pages = 1;
for($chunk_size = 96; $chunk_size * $stats['growth_factor'] < $stats['item_size_max']; $chunk_size *= $stats['growth_factor'])
$pages++;
printf("%-15s %12s (real %s - %s)".PHP_EOL, 'maxbytes', descriptive_size($stats['maxbytes']), descriptive_size(max($stats['item_size_max'] * $pages, $stats['maxbytes'])), descriptive_size($stats['item_size_max'] * ($pages + $stats['maxbytes'] / $stats['item_size_max'] - 1)));
printf("%-15s %12s".PHP_EOL, 'item_size_max', descriptive_size($stats['item_size_max']));
printf("%-15s %12s".PHP_EOL, 'evictions', $stats['evictions']);
printf("%-15s %12s".PHP_EOL, 'growth_factor', $stats['growth_factor']);
}
function show_stats($fp, $command = 'stats')
{
$stats = get_stats($fp, $command);
ksort($stats);
printf ("%24s %15s".PHP_EOL, "Field", "Value");
foreach($stats as $property => $value)
printf ("%24s %15s".PHP_EOL, $property, $value);
}
function iterkeys($fp, $dumpmode = DUMPMODE_ONLYKEYS)
{
$slabs = slabs_stats($fp);
$stats = get_stats($fp, 'stats');
ksort($slabs);
printf(" %-40s %20s %10s %8s".PHP_EOL, 'Key', 'Expire status', 'Size', 'Waste');
foreach($slabs as $num => $slab)
{
if($num == 'total')
continue;
if($slab['number'])
{
$lines = send_and_receive($fp, "stats cachedump {$num} {$slab['number']}");
foreach($lines as $line)
{
$m = array();
if(preg_match('/^ITEM ([^\s]+) \[(\d+) b; (\d+) s\]/', $line, $m))
{
$key = $m[1];
$size = $m[2];
$now = time();
$waste = (1.0 - (float)$size / $slab['chunk_size']) * 100;
$expiration_time = $m[3];
if($expiration_time == $stats['time'] - $stats['uptime'])
$status = '[never expire]';
elseif($now > $expiration_time)
$status = '[expired]';
else
$status = ($expiration_time - $now).'s left';
printf("ITEM %-40s %20s %10s %7.0f%%".PHP_EOL, $key, $status, $size, $waste);
// Get value
if($dumpmode == DUMPMODE_KEYVALUES && $status != '[expired]')
{
$lines = send_and_receive($fp, "get {$key}");
if(count($lines))
{
$info = $lines[0];
$data = $lines[1];
preg_match('/^VALUE ([^\s]+) (\d+) (\d+)/', $info, $m);
$flags = $m[2];
printf("VALUE %-40s flags=%X".PHP_EOL, $key, $flags);
printf("%s".PHP_EOL, $data);
}
}
// Get value
if($dumpmode == REMOVEMODE_EXPIRED && $status == '[expired]')
{
$lines = send_and_receive($fp, "get {$key}");
}
}
}
}
}
}
function sizes($fp)
{
$sizes = array();
$stats = settings_stats($fp);
printf("%-10s %10s %10s %10s".PHP_EOL, 'Size', 'Items', 'Chunk_Size', 'Wasted');
$lines = send_and_receive($fp, 'stats sizes');
foreach($lines as $line)
{
$m = array();
if(preg_match('/^STAT ([^\s]+) ([^\s]+)/', $line, $m))
{
$size = $m[1];
$values = $m[2];
for($chunk_size = 96; $chunk_size * $stats['growth_factor'] < $size; $chunk_size *= $stats['growth_factor']);
$chunk_size *= $stats['growth_factor'];
if($chunk_size * $stats['growth_factor'] > $stats['item_size_max'])
$chunk_size = $stats['item_size_max'];
$wasted = (1.0 - $size / $chunk_size) * 100;
printf("%-10s %10d %10s %9.0f%%".PHP_EOL, descriptive_size($size), $values, descriptive_size((int)$chunk_size), $wasted);
$sizes[$size] = $values;
}
}
}
function descriptive_size($size)
{
if($size >= 1024*1024)
return sprintf('%.1fM', (float)$size/(1024*1024));
if($size >= 1024)
return sprintf('%.1fK', (float)$size/(1024));
return $size.'B';
}