Skip to content

Commit f70eaea

Browse files
committed
Add Memcached slabs info
1 parent 69ba29b commit f70eaea

File tree

4 files changed

+93
-8
lines changed

4 files changed

+93
-8
lines changed

assets/css/styles.css

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! tailwindcss v4.1.5 | MIT License | https://tailwindcss.com */
1+
/*! tailwindcss v4.1.10 | MIT License | https://tailwindcss.com */
22
@layer properties;
33
@layer theme, base, components, utilities;
44
@layer theme {
@@ -865,6 +865,11 @@
865865
grid-template-columns: repeat(3, minmax(0, 1fr));
866866
}
867867
}
868+
.md\:grid-cols-4 {
869+
@media (width >= 48rem) {
870+
grid-template-columns: repeat(4, minmax(0, 1fr));
871+
}
872+
}
868873
.md\:grid-cols-10 {
869874
@media (width >= 48rem) {
870875
grid-template-columns: repeat(10, minmax(0, 1fr));

src/Dashboards/Memcached/MemcachedTrait.php

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ private function panels(): string {
3131
'title' => $title ?? null,
3232
'moreinfo' => true,
3333
'data' => [
34-
'Version' => $info['version'],
35-
'Open connections' => $info['curr_connections'],
36-
'Uptime' => Format::seconds($info['uptime']),
34+
'Version' => $info['version'],
35+
'Uptime' => Format::seconds($info['uptime']),
3736
],
3837
],
3938
[
@@ -396,6 +395,38 @@ private function commandsStats(): string {
396395
return $this->template->render('dashboards/memcached', ['commands' => $commands]);
397396
}
398397

398+
/**
399+
* @throws MemcachedException
400+
*/
401+
private function slabs(): string {
402+
$slabs_stats = $this->memcached->getSlabsStats();
403+
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']),
421+
];
422+
}, $slabs_stats['slabs']);
423+
424+
return $this->template->render('dashboards/memcached', [
425+
'slabs' => $slabs,
426+
'meta' => $slabs_stats['meta'],
427+
]);
428+
}
429+
399430
/**
400431
* @throws MemcachedException
401432
*/
@@ -411,6 +442,10 @@ private function mainDashboard(): string {
411442
return $this->commandsStats();
412443
}
413444

445+
if (Http::get('tab') === 'slabs') {
446+
return $this->slabs();
447+
}
448+
414449
$keys = $this->getAllKeys();
415450

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

src/Dashboards/Memcached/PHPMem.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ public function flush(): bool {
6969
}
7070

7171
/**
72+
* @param 'settings'|'items'|'sizes'|'slabs'|'conns'|null $type
73+
*
7274
* @return array<string, mixed>
7375
*
7476
* @throws MemcachedException
@@ -88,6 +90,30 @@ public function getServerStats(?string $type = null): array {
8890
return $stats;
8991
}
9092

93+
/**
94+
* @return array<string, mixed>
95+
*
96+
* @throws MemcachedException
97+
*/
98+
public function getSlabsStats(): array {
99+
$stats = $this->getServerStats('slabs');
100+
$result = [
101+
'slabs' => [],
102+
'meta' => [],
103+
];
104+
105+
foreach ($stats as $key => $value) {
106+
if (str_contains($key, ':')) {
107+
[$slab_id, $field] = explode(':', $key, 2);
108+
$result['slabs'][$slab_id][$field] = $value;
109+
} else {
110+
$result['meta'][$key] = $value;
111+
}
112+
}
113+
114+
return $result;
115+
}
116+
91117
public function isConnected(): bool {
92118
try {
93119
$stats = $this->getServerStats();
@@ -150,7 +176,7 @@ public function getKeys(): array {
150176
}
151177

152178
/**
153-
* Convert raw key line to an array.
179+
* Convert a raw key line to an array.
154180
*
155181
* @return array<string, string|int>
156182
*/
@@ -237,7 +263,7 @@ public function version(): string {
237263
/**
238264
* Run command.
239265
*
240-
* These commands should work but not guaranteed to work on any server:
266+
* These commands should work but are not guaranteed to work on any server:
241267
*
242268
* set|add|replace|append|prepend <key> <flags> <ttl> <bytes>\r\n<value>\r\n
243269
* cas <key> <flags> <exptime> <bytes> <cas unique>\r\n
@@ -271,8 +297,8 @@ public function version(): string {
271297
* verbosity <level>\r\n
272298
* quit\r\n
273299
*
274-
* Note: \r\n is added automatically to the end
275-
* and \r\n (as a plain string) is converted to a real end of line.
300+
* Note: \r\n is added automatically to the end,
301+
* and \r\n (as a plain string) is converted to a real end of the line.
276302
*
277303
* @link https://github.com/memcached/memcached/blob/master/doc/protocol.txt
278304
*

templates/dashboards/memcached.twig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
links: {
33
'keys': 'Keys',
44
'commands_stats': 'Commands Stats',
5+
'slabs': 'Slabs',
56
},
67
}) }}
78

@@ -34,3 +35,21 @@
3435
</div>
3536
</div>
3637
{% endif %}
38+
39+
{% if get('tab') == 'slabs' %}
40+
<div class="px-6 py-4 rounded-sm bg-white border border-gray-200 dark:bg-gray-800 dark:border-gray-700">
41+
<div class="flex items-center gap-4 mb-4">
42+
<div class="font-semibold text-gray-600 dark:text-gray-300">Total memory allocated to slab pages</div>
43+
<div class="text-sm font-semibold text-gray-900 dark:text-white">{{ meta.total_malloced|bytes }}</div>
44+
</div>
45+
46+
<div class="md:grid md:grid-cols-4 md:gap-4">
47+
{% for slab, data in slabs %}
48+
{{ include('partials/panel.twig', {
49+
panel_title: 'Slab ' ~ slab,
50+
array: data,
51+
}) }}
52+
{% endfor %}
53+
</div>
54+
</div>
55+
{% endif %}

0 commit comments

Comments
 (0)