Skip to content

Commit 83b1718

Browse files
committed
Add Memcached items info
1 parent f70eaea commit 83b1718

File tree

4 files changed

+120
-18
lines changed

4 files changed

+120
-18
lines changed

src/Dashboards/Memcached/MemcachedTrait.php

Lines changed: 86 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -401,24 +401,26 @@ private function commandsStats(): string {
401401
private function slabs(): string {
402402
$slabs_stats = $this->memcached->getSlabsStats();
403403

404-
$slabs = array_map(static function ($slab) {
405-
return [
406-
'Chunk Size' => Format::bytes($slab['chunk_size']),
407-
'Chunks per Page' => Format::number($slab['chunks_per_page']),
408-
'Total Pages' => Format::number($slab['total_pages']),
409-
'Total Chunks' => Format::number($slab['total_chunks']),
410-
'Used Chunks' => Format::number($slab['used_chunks']),
411-
'Free Chunks' => Format::number($slab['free_chunks']),
412-
'Free Chunks (End)' => Format::number($slab['free_chunks_end']),
413-
'GET Hits' => Format::number($slab['get_hits']),
414-
'SET Commands' => Format::number($slab['cmd_set']),
415-
'DELETE Hits' => Format::number($slab['delete_hits']),
416-
'INCREMENT Hits' => Format::number($slab['incr_hits']),
417-
'DECREMENT Hits' => Format::number($slab['decr_hits']),
418-
'CAS Hits' => Format::number($slab['cas_hits']),
419-
'CAS Bad Value' => Format::number($slab['cas_badval']),
420-
'TOUCH Hits' => Format::number($slab['touch_hits']),
404+
$slabs = array_map(function (array $slab): array {
405+
$fields = [
406+
'chunk_size' => ['Chunk Size', 'bytes'],
407+
'chunks_per_page' => ['Chunks per Page', 'number'],
408+
'total_pages' => ['Total Pages', 'number'],
409+
'total_chunks' => ['Total Chunks', 'number'],
410+
'used_chunks' => ['Used Chunks', 'number'],
411+
'free_chunks' => ['Free Chunks', 'number'],
412+
'free_chunks_end' => ['Free Chunks (End)', 'number'],
413+
'get_hits' => ['GET Hits', 'number'],
414+
'cmd_set' => ['SET Commands', 'number'],
415+
'delete_hits' => ['DELETE Hits', 'number'],
416+
'incr_hits' => ['INCREMENT Hits', 'number'],
417+
'decr_hits' => ['DECREMENT Hits', 'number'],
418+
'cas_hits' => ['CAS Hits', 'number'],
419+
'cas_badval' => ['CAS Bad Value', 'number'],
420+
'touch_hits' => ['TOUCH Hits', 'number'],
421421
];
422+
423+
return $this->formatField($fields, $slab);
422424
}, $slabs_stats['slabs']);
423425

424426
return $this->template->render('dashboards/memcached', [
@@ -427,6 +429,69 @@ private function slabs(): string {
427429
]);
428430
}
429431

432+
/**
433+
* @throws MemcachedException
434+
*/
435+
private function items(): string {
436+
$stats = $this->memcached->getItemsStats();
437+
438+
$items = array_map(function (array $item): array {
439+
$fields = [
440+
'number' => ['Items', 'number'],
441+
'number_hot' => ['HOT LRU', 'number'],
442+
'number_warm' => ['WARM LRU', 'number'],
443+
'number_cold' => ['COLD LRU', 'number'],
444+
'number_temp' => ['TEMP LRU', 'number'],
445+
'age_hot' => ['Age (HOT)', 'seconds'],
446+
'age_warm' => ['Age (WARM)', 'seconds'],
447+
'age' => ['Age (LRU)', 'seconds'],
448+
'mem_requested' => ['Memory Requested', 'bytes'],
449+
'evicted' => ['Evicted', 'number'],
450+
'evicted_nonzero' => ['Evicted Non-Zero', 'number'],
451+
'evicted_time' => ['Evicted Time', 'number'],
452+
'outofmemory' => ['Out of Memory', 'number'],
453+
'tailrepairs' => ['Tail Repairs', 'number'],
454+
'reclaimed' => ['Reclaimed', 'number'],
455+
'expired_unfetched' => ['Expired Unfetched', 'number'],
456+
'evicted_unfetched' => ['Evicted Unfetched', 'number'],
457+
'evicted_active' => ['Evicted Active', 'number'],
458+
'crawler_reclaimed' => ['Crawler Reclaimed', 'number'],
459+
'crawler_items_checked' => ['Crawler Items Checked', 'number'],
460+
'lrutail_reflocked' => ['LRU Tail Reflocked', 'number'],
461+
'moves_to_cold' => ['Moves to COLD', 'number'],
462+
'moves_to_warm' => ['Moves to WARM', 'number'],
463+
'moves_within_lru' => ['Moves within LRU', 'number'],
464+
'direct_reclaims' => ['Direct Reclaims', 'number'],
465+
'hits_to_hot' => ['Hits to HOT', 'number'],
466+
'hits_to_warm' => ['Hits to WARM', 'number'],
467+
'hits_to_cold' => ['Hits to COLD', 'number'],
468+
'hits_to_temp' => ['Hits to TEMP', 'number'],
469+
];
470+
471+
return $this->formatField($fields, $item);
472+
}, $stats);
473+
474+
return $this->template->render('dashboards/memcached', ['items' => $items]);
475+
}
476+
477+
/**
478+
* @param array<string, mixed> $fields
479+
* @param array<string, mixed> $item
480+
*
481+
* @return array<string, mixed>
482+
*/
483+
private function formatField(array $fields, array $item): array {
484+
$formatted = [];
485+
486+
foreach ($fields as $key => [$label, $type]) {
487+
$value = $item[$key] ?? 0;
488+
jdump([$label, $value, $type]);
489+
$formatted[$label] = Format::{$type}($value);
490+
}
491+
492+
return $formatted;
493+
}
494+
430495
/**
431496
* @throws MemcachedException
432497
*/
@@ -446,6 +511,10 @@ private function mainDashboard(): string {
446511
return $this->slabs();
447512
}
448513

514+
if (Http::get('tab') === 'items') {
515+
return $this->items();
516+
}
517+
449518
$keys = $this->getAllKeys();
450519

451520
if (isset($_GET['export_btn'])) {

src/Dashboards/Memcached/PHPMem.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,25 @@ public function getSlabsStats(): array {
114114
return $result;
115115
}
116116

117+
/**
118+
* @return array<string, mixed>
119+
*
120+
* @throws MemcachedException
121+
*/
122+
public function getItemsStats(): array {
123+
$stats = $this->getServerStats('items');
124+
$result = [];
125+
126+
foreach ($stats as $key => $value) {
127+
if (str_starts_with($key, 'items:') && substr_count($key, ':') === 2) {
128+
[, $slab_id, $field] = explode(':', $key, 3);
129+
$result[$slab_id][$field] = $value;
130+
}
131+
}
132+
133+
return $result;
134+
}
135+
117136
public function isConnected(): bool {
118137
try {
119138
$stats = $this->getServerStats();

src/Format.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public static function seconds(int $time): string {
8484
}
8585
}
8686

87-
return implode(' ', $time_parts);
87+
return $time_parts ? implode(' ', $time_parts) : '0 seconds';
8888
}
8989

9090
public static function time(int $time): string {

templates/dashboards/memcached.twig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
'keys': 'Keys',
44
'commands_stats': 'Commands Stats',
55
'slabs': 'Slabs',
6+
'items': 'Items',
67
},
78
}) }}
89

@@ -53,3 +54,16 @@
5354
</div>
5455
</div>
5556
{% endif %}
57+
58+
{% if get('tab') == 'items' %}
59+
<div class="px-6 py-4 rounded-sm bg-white border border-gray-200 dark:bg-gray-800 dark:border-gray-700">
60+
<div class="md:grid md:grid-cols-4 md:gap-4">
61+
{% for item, data in items %}
62+
{{ include('partials/panel.twig', {
63+
panel_title: 'Item ' ~ item,
64+
array: data,
65+
}) }}
66+
{% endfor %}
67+
</div>
68+
</div>
69+
{% endif %}

0 commit comments

Comments
 (0)