-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmemcached-slabs.php
executable file
·1300 lines (1086 loc) · 44.7 KB
/
memcached-slabs.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env php
<?php
srand(1);
function starMatch($pattern, $string) {
$pattern = preg_quote($pattern);
$pattern = '/^' . str_replace(array('%', '\\?'), array('.*', '.'), $pattern) . '$/';
return preg_match($pattern, $string);
}
class MemcachedTool {
public $host;
public $port;
private $reportName;
public function __construct($host, $port) {
$this->host = $host;
$this->port = $port;
$this->reportName = $host . '_' . $port . '_' . date('YmdH');
$this->topEntitiesPath = './' . $this->reportName . '.top.entities/';
}
public $keyStats = array();
public $keySamples = array();
public $keySize = array();
public $keySlabs = array();
public $emptyKeysBySlab = array();
public $saveKeys;
public $saveTopEntitiesCount = 10;
public $topEntities = array();
const MIN_ADDED_PAGES = 2;
const BASIC_TOTAL_PERCENT = 0.2;
const EV_SET_TOTAL_PERCENT_MUL = 1;
private $evictionsReportCommand;
private $evictionsReportTotalPages;
private $evictionsReportTotalMemory;
public function evictionsReport()
{
$preallocateCommand = '';
$totalPages = 0;
$totalPreallocatedMemory = 0;
$slabs = $this->slabs();
if (!$slabs) {
return;
}
foreach ($slabs as $slab) {
if (empty($slab['max'])) {
continue;
}
$evSet = $slab['evicted'] / $slab['cmd_set'];
if ($evSet >= 0.01) {
$pageSize = $slab['chunk_size'] * $slab['chunks_per_page'];
$total_number = $slab['total_chunks'];
$percent = self::BASIC_TOTAL_PERCENT + self::EV_SET_TOTAL_PERCENT_MUL * $evSet * 100;
$preallocateNumber = round($total_number * (1 + $percent / 100));
$preallocatePages = ceil($preallocateNumber / $slab['chunks_per_page']);
if ($preallocatePages < self::MIN_ADDED_PAGES + $slab['total_pages']) {
$preallocatePages = self::MIN_ADDED_PAGES + 1 * $slab['total_pages'];
$preallocateNumber = ceil($preallocatePages * $slab['chunks_per_page']);
}
$preallocateCommand .= ' -p ' . $slab['max'] . ':' . $preallocateNumber;
$preallocatePages = ceil($preallocateNumber / $slab['chunks_per_page']);
$totalPages += $preallocatePages;
$totalPreallocatedMemory += $preallocatePages * $pageSize;
}
$this->evictionsReportCommand = 'php ' . basename(__FILE__) . $preallocateCommand;
$this->evictionsReportTotalPages = $totalPages;
$this->evictionsReportTotalMemory = $totalPreallocatedMemory;
}
}
public function formatKeys(&$keysText, $prefix, &$slab) {
$keysText = explode("\r\n", $keysText);
$now = time();
$slab['min'] = 10000000;
$slab['max'] = 0;
$slab['avg'] = 0;
$count = 0;
foreach ($keysText as $line) {
$line = trim(substr($line, 5));
if (!$line) {
continue;
}
list($key, $params) = explode(' ', $line, 2);
list($length, $timestamp) = explode(' b; ', substr($params, 1, -3));
$length = (int)$length;
$expired = $timestamp < $now;
++$count;
$slab['min'] = min($slab['min'], $length);
$slab['max'] = max($slab['max'], $length);
$slab['avg'] = ($slab['avg'] * ($count - 1) / $count) + ($length / $count);
$mask = null;
//var_dump($this->groupPatterns);
//die();
foreach ($this->groupPatterns as $pattern) {
$match = starMatch($pattern, $key);
if ($match) {
$mask = $pattern;
break;
}
}
if (!$mask) {
$path = explode('_', $key);
foreach ($path as &$item) {
if (preg_match('/[0-9\.]+/', $item)) {
$item = '%';
}
}
$mask = implode('_', $path);
}
$total = &$this->keyStats[$mask];
++$total;
$this->keySamples[$mask] = $key;
$size = &$this->keySize[$mask];
if (empty($size)) {
$size = array('c' => 0, 't' => 0, 'm' => 100000000, 'M' => -1, 'Mk' => array(), 'e' => 0);
}
$size['c']++;
$size['t'] += $length;
$size['m'] = min($size['m'], $length);
if ($size['M'] < $length) {
$size['M'] = $length;
}
if (!$expired) {
if (count($size['Mk']) < $this->saveTopEntitiesCount) {
$size['Mk'][$key] = $length;
arsort($size['Mk']);
}
else {
$last = end($size['Mk']);
if ($last < $length) {
$size['Mk'][$key] = $length;
arsort($size['Mk']);
array_pop($size['Mk']);
}
}
}
else {
++$size['e'];
}
$total = &$this->keySlabs[$mask][$slab['id']];
++$total;
}
arsort($this->keyStats);
}
public $analyzeKeys = true;
private $usedSlabs = array();
public function getKeys() {
$slabs = $this->getSlabs();
if (!$slabs) {
return array();
}
foreach ($slabs as $id => $slab) {
$prefix = $id . ':' . $slab['chunk_size'];
echo "\n", $this->reportName;
echo "\n$prefix\n";
//print_r($slab);
$start = microtime(1);
if ($this->analyzeKeys && !empty($slab['number'])) {
echo "Getting keys...\n";
$keys = $this->mcQuery('stats cachedump ' . $id . ' 100000000');
if ($this->saveKeys) {
file_put_contents($this->reportName . '_' . $id . '.keys', $keys);
}
}
$slab['prefix'] = $prefix;
$slab['cachedump_time'] = 'skipped';
if ($this->analyzeKeys && !empty($slab['number'])) {
$slab['cachedump_time'] = round(microtime(1) - $start, 4);
$this->formatKeys($keys, $prefix, $slab);
}
$this->usedSlabs []= $slab;
}
if ($this->saveTopEntitiesCount) {
if (!file_exists($this->topEntitiesPath)) {
mkdir($this->topEntitiesPath);
}
foreach ($this->keySize as $mask => $size) {
if ($this->saveTopEntitiesCount) {
foreach ($size['Mk'] as $key => $length) {
//$val = $this->getValue($key);
$val = print_r($this->getPhpValue($key), 1);
//echo $key, "\n", $val;
$filename = $this->topEntitiesPath . '/' . str_replace(array(':','/','\\'), '', $key);
file_put_contents($filename, $val);
$this->topEntities[$key] = $filename;
}
}
}
}
return $this->keyStats;
}
public $groupPatterns = array();
public $evictedOnly = false;
public function getSlabs() {
if ($this->evictedOnly) {
$evicted = array();
$slabs = $this->slabs();
foreach ($slabs as $id => $slab) {
if (!empty($slab['evicted'])) {
$evicted [$id]= $slab;
}
}
return $evicted;
}
else {
return $this->slabs();
}
}
private $slabs;
public function slabs() {
if (null !== $this->slabs) {
return $this->slabs;
}
echo "Getting slabs stats...\n";
$this->slabs = array();
$slabParamNames = array();
$res = $this->mcQuery("stats slabs");
//echo $res;
foreach (explode("\r\n", $res) as $line) {
$line = substr($line, 5);
$data = explode(':', $line);
if (count($data) == 2) {
$slabId = $data[0];
$slabParam = explode(' ', $data[1], 2);
$this->slabs[$slabId]['id'] = $slabId;
$this->slabs[$slabId][$slabParam[0]] = $slabParam[1];
$slabParamNames[$slabParam[0]] = $slabParam[0];
}
}
$res = $this->mcQuery('stats items');
foreach (explode("\r\n", $res) as $line) {
$line = substr($line, 11);
$data = explode(':', $line);
if (count($data) == 2) {
$slabId = $data[0];
$slabParam = explode(' ', $data[1], 2);
$this->slabs[$slabId]['id'] = $slabId;
$this->slabs[$slabId][$slabParam[0]] = $slabParam[1];
$slabParamNames[$slabParam[0]] = $slabParam[0];
}
}
if ($this->slabs) {
foreach ($this->slabs as &$slab) {
foreach ($slabParamNames as $paramName) {
if (!isset($slab[$paramName])) {
$slab[$paramName] = 0;
}
}
}
}
//print_r($this->slabs);
return $this->slabs;
}
private function getTotalStats() {
$res = explode("\r\n", $this->mcQuery('stats'));
$stats = array();
foreach ($res as $line) {
$stat = explode(' ', substr($line, 5), 2);
if (count($stat) == 2) {
$stats[$stat[0]] = $stat[1];
}
}
return $stats;
}
public function saveReport() {
$slabs = array();
foreach ($this->usedSlabs as $slab) {
$slabs[$slab['id']] = $slab;
}
$jsonReport = array(
'host' => $this->host,
'port' => $this->port,
'stats' => $this->getTotalStats(),
'slabs' => $slabs,
'keySize' => $this->keySize,
'keySamples' => $this->keySamples,
'keySlabs' => $this->keySlabs,
'maxKeys' => $this->topEntities,
);
$jsonData = json_encode($jsonReport);
/*
if ($this->maxKeysCount) {
file_put_contents($this->reportName . '.top-entities.json', json_encode($this->maxKeys));
}
*/
file_put_contents($this->reportName . '.json', $jsonData);
file_put_contents($this->reportName . '.html', $this->getHtmlReport($jsonData));
$this->evictionsReport();
if ($this->evictionsReportCommand) {
echo "Many evictions found, recommend to pre-allocate slabs after memcached restart\n";
echo $this->evictionsReportCommand, "\n";
echo "Total pages to pre-allocate: " . $this->evictionsReportTotalPages, "\n";
}
return;
}
private $memcachedSock;
private function initMemcached() {
if (!is_resource($this->memcachedSock)) {
$this->memcachedSock = fsockopen($this->host, $this->port);
if (!$this->memcachedSock) {
die('Could not connect to memcached');
}
}
}
private static $mcEnd = array("END\r\n", "ERROR\r\n", "STORED\r\n", "NOT_STORED\r\n", "EXISTS\r\n", "NOT_FOUND\r\n", "DELETED\r\n");
private $lastEnd;
private function mcQuery($command) {
$this->initMemcached();
$response = '';
$this->totalWrite += strlen($command) + 2;
fwrite($this->memcachedSock, $command . "\r\n");
while (!feof($this->memcachedSock)) {
$chunk = fread($this->memcachedSock, 8192);
$response .= $chunk;
$check = substr($response, -14);
foreach (self::$mcEnd as $end) {
if (substr($check, -strlen($end)) == $end) {
$this->totalRead += strlen($response);
$response = substr($response, 0, -strlen($end));
$this->lastEnd = $end;
break 2;
}
}
}
return $response;
}
private $memcached;
public function getPhpValue($key) {
if (null === $this->memcached) {
$this->memcached = new Memcached();
$this->memcached->addServer($this->host,$this->port);
}
return $this->memcached->get($key);
}
public function getValue($key) {
$command = 'get ' . $key;
$response = $this->mcQuery($command);
$p = strpos($response, "\r\n");
$head = substr($response, 0, $p);
//return substr($response, $p + 2);
return $response;
}
public function __destruct() {
if (is_resource($this->memcachedSock)) {
fclose($this->memcachedSock);
}
}
private function getHtmlReport($jsonData) {
ob_start();
?>
<html>
<head>
<style type="text/css">body,input,pre,select,textarea{font-family:monospace}td,th{border:1px solid #aaa;white-space:pre-wrap;background:#fff;font-weight:400}td:hover{border:1px solid #faa}th{background:#afa}table{border-collapse:separate;border:0}</style>
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="http://www.kryogenix.org/code/browser/sorttable/sorttable.js"></script>
<!--script src="http://www.chartjs.org/assets/Chart.min.js" charset="utf-8"></script-->
<script type="text/javascript" src="http://code.highcharts.com/highcharts.js"></script>
<script type="text/javascript" src="http://code.highcharts.com/highcharts-more.js"></script>
</head>
<body>
<script type="text/javascript">
(function(){
var instance = {}, data;
window.mcReportViewer = instance;
$(function(){
data = <?=$jsonData?>;
makeReport(data);
});
instance.getKey = function(key){
//console.log(data);
//console.log(data.maxKeys[key]);
};
function renderTable(keys, rows) {
var html = '<table class="sortable"><tr>', k, i, row;
if (!keys) {
keys = [];
for (i in rows) {
if (rows.hasOwnProperty(i)) {
row = rows[i];
for (k in row) {
if (row.hasOwnProperty(k)) {
keys.push(k);
}
}
break;
}
}
}
for (k = 0; k < keys.length; ++k) {
html += '<th>' + keys[k] + '</th>';
}
html += '</tr>';
//for (i = 0; i < rows.length; ++i) {
for (i in rows) {
if (rows.hasOwnProperty(i)) {
row = rows[i];
html += '<tr' + (row['RowClass'] ? ' class="' + row['RowClass'] + '"' : '') + '>';
for (k = 0; k < keys.length; ++k) {
html += '<td>' + row[keys[k]] + '</td>';
}
html += '</tr>';
}
}
return html;
}
var filteredSlab = 0;
instance.filterSlab = function(id) {
if (filteredSlab == id) {
$('#report .slab').show();
filteredSlab = 0;
}
else {
$('#report .slab').hide();
$('#report .slab.slab-' + id).show();
filteredSlab = id;
}
};
function keysReportHtml(data) {
//console.log(data);
var reportKeys = ['Mask', 'Count', 'Expired', 'Total Size', 'Average Size', 'Min Size', 'Max Size', 'Affected Slabs', 'Sample Key', 'Top Entities'],
reportData = [],
item = {},
mask = '',
slabId,
slab,
slabCount,
size = {};
for (mask in data.keySize) {
if (data.keySize.hasOwnProperty(mask)) {
size = data.keySize[mask];
item['RowClass'] = 'slab ';
item['Mask'] = mask;
item['Count'] = Math.round(size.c);
item['Expired'] = size.e;
item['Total Size'] = size.t;
item['Average Size'] = Math.round(size.t / size.c);
item['Min Size'] = size.m;
item['Max Size'] = size.M;
item['Affected Slabs'] = '';
for (slabId in data.keySlabs[mask]) {
if (data.keySlabs[mask].hasOwnProperty(slabId)) {
slab = data.slabs[slabId];
slabCount = data.keySlabs[mask][slabId];
item['RowClass'] += 'slab-' + slabId + ' ';
item['Affected Slabs'] += '<a href="#" onclick="mcReportViewer.filterSlab(' + slabId + ');return false;">' + slab.prefix + '</a>(' + slabCount + '), ';
}
}
if (item['Affected Slabs']) {
item['Affected Slabs'] = item['Affected Slabs'].substring(0, item['Affected Slabs'].length - 1);
}
item['Sample Key'] = data.keySamples[mask];
item['Top Entities'] = '';
for (var i in size['Mk']) {
if (size['Mk'].hasOwnProperty(i)) {
item['Top Entities'] += '<a href="#" onclick="mcReportViewer.getKey(\'' + i + '\');return false;">'
+ i + '</a>' + ':' + size['Mk'][i] + ' ';
}
}
reportData.push($.extend({}, item));
}
}
return renderTable(reportKeys, reportData);
}
function countBytes(size) {
var kb = 1024, mb = 1024*kb, gb = 1024*mb;
if (size > gb) {
return (Math.round(100 * size / gb) / 100) + 'G';
}
if (size > mb) {
return (Math.round(100 * size / mb) / 100) + 'M';
}
if (size > kb) {
return (Math.round(100 * size / kb) / 100) + 'K';
}
return size;
}
function slabReport(data) {
var slab,
slabData = [],
slabKeys = [
'id',
'chunk_size',
//'avg',
'number',
//'hit_ratio',
//'get_hits',
'cmd_set',
//'delete_hits',
'evicted',
'prefix',
'mem_usage',
'total_memory',
'ev/set',
'filled_%',
//'chunks_per_page',
//'outofmemory',
//'used_chunks',
//'total_chunks',
//'total_pages',
'cachedump_time'
/*
'age',
'crawler_reclaimed',
'evicted_nonzero',
'evicted_time',
'evicted_unfetched',
'expired_unfetched',
'free_chunks',
'free_chunks_end',
'incr_hits',
'decr_hits',
'lrutail_reflocked',
'mem_requested',
'chunks_per_page',
'outofmemory',
'reclaimed',
'tailrepairs',
'total_chunks',
'total_pages',
'touch_hits',
'used_chunks',
'cas_badval',
'cas_hits'
*/
];
for (slabId in data.slabs) {
if (data.slabs.hasOwnProperty(slabId)) {
slab = data.slabs[slabId];
slab['avg'] = Math.floor(slab['avg']);
slab['hit_ratio'] = slab['get_hits'] / slab['cmd_set'];
slab['used_memory'] = slab['used_chunks'] * slab['chunk_size'];
slab['total_memory'] = slab['total_chunks'] * slab['chunk_size'];
slab['ev/set'] = Math.round(100 * slab['evicted'] / slab['cmd_set']);
slab['filled_%'] = Math.round(100 * slab['used_chunks'] / slab['total_chunks']);
slab['mem_usage'] = countBytes(slab['used_chunks'] * slab['chunk_size'])
+ ' / ' + countBytes(slab['total_chunks'] * slab['chunk_size']);
slabData.push(
$.extend({}, slab, {
prefix: '<a href="#" onclick="mcReportViewer.filterSlab(\'' + slabId + '\');return false;">' + slab.prefix + '</a>'
})
);
}
}
return renderTable(slabKeys, slabData);
}
function statsReportHtml(data) {
var param, value, reportData = [];
for (param in data.stats) {
if (data.stats.hasOwnProperty(param)) {
value = data.stats[param];
reportData.push({'Param': param, 'Value': value});
}
}
return renderTable(['Param', 'Value'], reportData);
}
function appendHChart(records, xAxis, yAxis, options) {
var record, data = [];
for (var id in records) {
if (records.hasOwnProperty(id)) {
record = records[id];
data.push([parseInt(record[xAxis]), parseFloat(record[yAxis])]);
}
}
//console.log(data);
var canvas = $('<div style="width: 500px;height: 200px"></div>');
$(function () {
canvas.highcharts({
title: null,
legend: {enabled: false},
yAxis: {title: null},
credits:{enabled:false},
series: [{
type: "areaspline",
name: yAxis,
data: data
}]
});
});
return canvas;
}
function evictionsReport(data) {
var minAddedPages = 2,
basicTotalPercent = 0.2,
evSetTotalPercentMul = 1;
var slab,
preallocateCommand = '',
total_number,
preallocateNumber,
preallocatePages,
percent,
evSet,
pageSize,
totalPages = 0,
totalPreallocatedMemory = 0;
for (var i in data.slabs) {
if (data.slabs.hasOwnProperty(i)) {
slab = data.slabs[i];
if (typeof slab['max'] == 'undefined') {
return '';
}
evSet = slab['evicted'] / slab['cmd_set'];
//console.log(slab);
if (evSet >= 0.01) {
pageSize = parseInt(slab['chunk_size']) * parseInt(slab['chunks_per_page']);
total_number = slab['total_chunks'];
percent = basicTotalPercent + evSetTotalPercentMul * evSet * 100;
preallocateNumber = Math.round(total_number * (1 + percent / 100));
preallocatePages = Math.ceil(preallocateNumber / slab['chunks_per_page']);
//console.log(slab['chunk_size'], preallocateNumber, preallocatePages);
if (preallocatePages < minAddedPages + 1*slab['total_pages']) {
preallocatePages = minAddedPages + 1*slab['total_pages'];
preallocateNumber = Math.ceil(preallocatePages * slab['chunks_per_page']);
//console.log(slab['chunk_size'], preallocateNumber, preallocatePages);
}
preallocateCommand += ' -p ' + slab['max'] + ':' + preallocateNumber;
preallocatePages = Math.ceil(preallocateNumber / slab['chunks_per_page']);
totalPages += preallocatePages;
totalPreallocatedMemory += preallocatePages * pageSize;
}
}
}
if (!totalPages) {
return '';
}
return '<h2>Many evictions found</h2>'
+ '<div>recommend to preallocate memory after restart</div>'
+ '<pre>php memcached-slabs.php' + preallocateCommand + ' ' + data.host + ':' + data.port + '</pre>'
+ '<pre>Total memory to pre-allocate: ' + countBytes(totalPreallocatedMemory)
+ ', total pages: ' + totalPages + '</pre>'
;
}
function makeReport(data) {
var memUsed = 0, memTotal = 0, slab;
for (var i in data.slabs) {
if (data.slabs.hasOwnProperty(i)) {
slab = data.slabs[i];
memUsed += slab['chunk_size'] * slab['used_chunks'];
memTotal += slab['chunk_size'] * slab['total_chunks']
}
}
var report = $('#report');
report
.html('')
.append('<h2>Memory Used ' + countBytes(memUsed) + ' of ' + countBytes(memTotal) + '</h2>')
.append(evictionsReport(data))
.append('<div style="float:left"><h2>Slabs</h2>' + slabReport(data) + '</div>')
.append(
$('<div id="report-cont" style="float:left"></div>')
.append('<div>Evicted</div>').append(appendHChart(data.slabs, 'id', 'evicted', {scaleShowLabels : false}))
.append('<div>Used Memory</div>').append(appendHChart(data.slabs, 'id', 'used_memory', {scaleShowLabels : false}))
.append('<div>Count</div>').append(appendHChart(data.slabs, 'id', 'number', {scaleShowLabels : false}))
.append('<div>Hit Ratio</div>').append(appendHChart(data.slabs, 'id', 'hit_ratio', {scaleShowLabels : false}))
.append('<div>Hits</div>').append(appendHChart(data.slabs, 'id', 'get_hits', {scaleShowLabels : false}))
.append('<div>Sets</div>').append(appendHChart(data.slabs, 'id', 'cmd_set', {scaleShowLabels : false}))
)
.append('<h2 style="clear:left">Key Groups</h2>' + keysReportHtml(data))
.append('<h2>Stats</h2>' + statsReportHtml(data))
.append('<h2>Slabs Stat</h2>' + renderTable(false, data.slabs))
;
report.find('table.sortable').each(function(){
sorttable.makeSortable(this);
});
}
})();
</script>
<h1 id="title"><?=$this->reportName?></h1>
<div id="report"></div>
</body>
</html>
<?php
$html = ob_get_clean();
return $html;
}
public function store($key, $value, $ttl) {
$this->mcQuery("set $key 0 $ttl " . strlen($value) . "\r\n" . $value);
}
public function delete($key) {
$this->mcQuery("delete $key");
}
public $totalWrite = 0;
public $totalRead = 0;
public function fillData($count, $minLength, $maxLength, $minTtl, $maxTtl, $prefix) {
$k = 0;
for ($i = 0; $i < $count; ++$i) {
$key = $prefix . $i;
$value = str_pad('', rand($minLength, $maxLength), '.');
$this->store($key, $value, rand($minTtl, $maxTtl));
$result = $this->lastEnd;
if ($result != "STORED\r\n") {
echo $this->lastEnd;
}
if (++$k > $this->dotSize) {
echo '.';
$k = 0;
}
}
echo "\n";
}
public $dotSize = 100;
public function deleteData($count, $prefix) {
$k = 0;
$dot = '.';
for ($i = 0; $i < $count; ++$i) {
$key = $prefix . $i;
$this->delete($key);
$result = $this->lastEnd;
if ($result != "DELETED\r\n") {
//echo $this->lastEnd;
$dot = 'e';
}
if (++$k > $this->dotSize) {
echo $dot;
$dot = '.';
$k = 0;
}
}
echo "\n";
}
public function getData($count, $prefix) {
$k = 0;
$dot = '.';
for ($i = 0; $i < $count; ++$i) {
$key = $prefix . $i;
$result = $this->getValue($key);
if (empty($result)) {
//echo $this->lastEnd;
$dot = 'e';
}
if (++$k > $this->dotSize) {
echo $dot;
$dot = '.';
$k = 0;
}
}
echo "\n";
}
public $topEntitiesPath = false;
}
if (!ini_get('date.timezone')) {
date_default_timezone_set('UTC');
}
/*$msc = new MemcacheSlabsCheck('localhost', '11211');
$msc->store('test', 'test', 0);
return;
*/
/*
$msc = new MemcacheSlabsCheck('localhost', '11211');
$msc->sampleFactor = 1;
print_r($msc->getKeys());
$msc->saveReport();
*/
if ($_SERVER['argc'] == 1) {
?>
Usage:
php <?=basename(__FILE__)?> [options] host1:port1 [host2:port2 ...]
Options:
-r generate html report
-p <size:count> preallocate slabs by setting/deleting
-ps <size:count> preallocate slabs by setting (no deleting)
-e process only slabs with evictions-e
-k save dumped keys
-f <count:min_length:max_length:min_ttl:max_ttl:key_prefix> fill memcached with random data
-d <count:key_prefix> delete random data from memcached
-s skip analyzing keys
-ste <count> save top size entities per key group count (print_r'ed), default 10
-g <key> get key value (raw protocol)
-gp <key> print_r key value (Memcached extension required)
-kp <pattern> key name grouping pattern, e.g. -kp search_name_*_my -kp product_name_*
Examples:
# preallocate space for 200k of 160 bytes records and 100k of 350 bytes records at myhost:11211 and myhost2:11011
php <?=basename(__FILE__)?> -p 160:200000 -p 350:100000 myhost:11211 myhost2:11011
# fetch raw values of MY_KEY at myhost:11211 and myhost2:11011
php <?=basename(__FILE__)?> -g MY_KEY myhost:11211 myhost2:11011
# view print_r values of MY_KEY and MY_KEY2 at myhost:11211 retrieved via Memcached ext
php <?=basename(__FILE__)?> -gp MY_KEY -gp MY_KEY2 myhost:11211 myhost2:11011
# generate slabs allocation html reports for myhost:11211 and myhost2:11011 (faster, without keys statistics)
php <?=basename(__FILE__)?> -r -s myhost:11211 myhost2:11011
# generate slabs allocation html reports for myhost:11211 and myhost2:11011 (slower, with keys statistics)
php <?=basename(__FILE__)?> -r myhost:11211 myhost2:11011
# benchmark 192.168.59.105:11211 with 50000 record with random size between 50 and 100,
# zero ttl and names SMALL_0 to SMALL_49999
# in benchmark mode records are set, then read, then deleted
php <?=basename(__FILE__)?> -b 5000:50:10000:0:0:SMALL_ 192.168.59.105:11211
# fill 192.168.59.105:11211 with 50000 record with random size between 50 and 100,
# zero ttl and names SMALL_0 to SMALL_49999
php <?=basename(__FILE__)?> -f 50000:50:100:0:0:SMALL_ 192.168.59.105:11211
# delete items with names SMALL_0 to SMALL_49999 from 192.168.59.105:11211
php <?=basename(__FILE__)?> -d 50000:SMALL_ 192.168.59.105:11211
<?php
}
$evictedOnly = false;
$saveKeys = false;
$analyzeKeys = true;
$fillData = array();
$benchData = array();
$deleteData = array();
$instances = array();
$arguments = $_SERVER['argv'];
$getValues = array();
$getPhpValues = array();
$preallocate = array();
$preallocateSet = array();
$makeReport = false;
$pte = false;
$saveTopEntitiesCount = 0;
$threads = false;
$jsonOutput = false;
$debug = false;
$groupPatterns = array();
for ($i = 1; $i < count($arguments); ++$i) {
$arg = $arguments[$i];
switch ($arg) {
case '-r': $makeReport = true;break;
case '-e': $evictedOnly = true;break;
case '-s': $analyzeKeys = false;break;
case '-k': $saveKeys = true;break;
case '-p': {
$preallocate []= explode(':', $arguments[$i+1]);
$i++;
break;
}
case '-ps': {
$preallocateSet []= explode(':', $arguments[$i+1]);
$i++;
break;
}
case '-t': {
$threads = $arguments[$i+1];
$i++;
break;
}
case '-v': {