blob: 434af27a32ac127bf6304108f8548764ecc9b1d0 [file] [log] [blame]
Christoph Lameterc09d8752007-05-06 14:49:48 -07001/*
2 * Slabinfo: Tool to get reports about slabs
3 *
4 * (C) 2007 sgi, Christoph Lameter <clameter@sgi.com>
5 *
6 * Compile by:
7 *
8 * gcc -o slabinfo slabinfo.c
9 */
10#include <stdio.h>
11#include <stdlib.h>
12#include <sys/types.h>
13#include <dirent.h>
14#include <string.h>
15#include <unistd.h>
16#include <stdarg.h>
17#include <getopt.h>
18#include <regex.h>
Christoph Lametera87615b2007-05-09 02:32:37 -070019#include <errno.h>
Christoph Lameterc09d8752007-05-06 14:49:48 -070020
21#define MAX_SLABS 500
22#define MAX_ALIASES 500
23#define MAX_NODES 1024
24
25struct slabinfo {
26 char *name;
27 int alias;
28 int refs;
29 int aliases, align, cache_dma, cpu_slabs, destroy_by_rcu;
30 int hwcache_align, object_size, objs_per_slab;
31 int sanity_checks, slab_size, store_user, trace;
32 int order, poison, reclaim_account, red_zone;
33 unsigned long partial, objects, slabs;
34 int numa[MAX_NODES];
35 int numa_partial[MAX_NODES];
36} slabinfo[MAX_SLABS];
37
38struct aliasinfo {
39 char *name;
40 char *ref;
41 struct slabinfo *slab;
42} aliasinfo[MAX_ALIASES];
43
44int slabs = 0;
Christoph Lametera87615b2007-05-09 02:32:37 -070045int actual_slabs = 0;
Christoph Lameterc09d8752007-05-06 14:49:48 -070046int aliases = 0;
47int alias_targets = 0;
48int highest_node = 0;
49
50char buffer[4096];
51
Christoph Lametera87615b2007-05-09 02:32:37 -070052int show_empty = 0;
53int show_report = 0;
Christoph Lameterc09d8752007-05-06 14:49:48 -070054int show_alias = 0;
55int show_slab = 0;
56int skip_zero = 1;
57int show_numa = 0;
58int show_track = 0;
59int show_first_alias = 0;
60int validate = 0;
61int shrink = 0;
62int show_inverted = 0;
63int show_single_ref = 0;
64int show_totals = 0;
65int sort_size = 0;
Christoph Lametera87615b2007-05-09 02:32:37 -070066int set_debug = 0;
67int show_ops = 0;
68
69/* Debug options */
70int sanity = 0;
71int redzone = 0;
72int poison = 0;
73int tracking = 0;
74int tracing = 0;
Christoph Lameterc09d8752007-05-06 14:49:48 -070075
76int page_size;
77
78regex_t pattern;
79
80void fatal(const char *x, ...)
81{
82 va_list ap;
83
84 va_start(ap, x);
85 vfprintf(stderr, x, ap);
86 va_end(ap);
87 exit(1);
88}
89
90void usage(void)
91{
Christoph Lametera87615b2007-05-09 02:32:37 -070092 printf("slabinfo 5/7/2007. (c) 2007 sgi. clameter@sgi.com\n\n"
93 "slabinfo [-ahnpvtsz] [-d debugopts] [slab-regexp]\n"
Christoph Lameterc09d8752007-05-06 14:49:48 -070094 "-a|--aliases Show aliases\n"
Christoph Lametera87615b2007-05-09 02:32:37 -070095 "-d<options>|--debug=<options> Set/Clear Debug options\n"
96 "-e|--empty Show empty slabs\n"
97 "-f|--first-alias Show first alias\n"
Christoph Lameterc09d8752007-05-06 14:49:48 -070098 "-h|--help Show usage information\n"
Christoph Lametera87615b2007-05-09 02:32:37 -070099 "-i|--inverted Inverted list\n"
100 "-l|--slabs Show slabs\n"
Christoph Lameterc09d8752007-05-06 14:49:48 -0700101 "-n|--numa Show NUMA information\n"
Christoph Lametera87615b2007-05-09 02:32:37 -0700102 "-o|--ops Show kmem_cache_ops\n"
Christoph Lameterc09d8752007-05-06 14:49:48 -0700103 "-s|--shrink Shrink slabs\n"
Christoph Lametera87615b2007-05-09 02:32:37 -0700104 "-r|--report Detailed report on single slabs\n"
105 "-S|--Size Sort by size\n"
Christoph Lameterc09d8752007-05-06 14:49:48 -0700106 "-t|--tracking Show alloc/free information\n"
107 "-T|--Totals Show summary information\n"
Christoph Lametera87615b2007-05-09 02:32:37 -0700108 "-v|--validate Validate slabs\n"
Christoph Lameterc09d8752007-05-06 14:49:48 -0700109 "-z|--zero Include empty slabs\n"
Christoph Lameterc09d8752007-05-06 14:49:48 -0700110 "-1|--1ref Single reference\n"
Christoph Lametera87615b2007-05-09 02:32:37 -0700111 "\nValid debug options (FZPUT may be combined)\n"
112 "a / A Switch on all debug options (=FZUP)\n"
113 "- Switch off all debug options\n"
114 "f / F Sanity Checks (SLAB_DEBUG_FREE)\n"
115 "z / Z Redzoning\n"
116 "p / P Poisoning\n"
117 "u / U Tracking\n"
118 "t / T Tracing\n"
Christoph Lameterc09d8752007-05-06 14:49:48 -0700119 );
120}
121
122unsigned long read_obj(char *name)
123{
124 FILE *f = fopen(name, "r");
125
126 if (!f)
127 buffer[0] = 0;
128 else {
129 if (!fgets(buffer,sizeof(buffer), f))
130 buffer[0] = 0;
131 fclose(f);
132 if (buffer[strlen(buffer)] == '\n')
133 buffer[strlen(buffer)] = 0;
134 }
135 return strlen(buffer);
136}
137
138
139/*
140 * Get the contents of an attribute
141 */
142unsigned long get_obj(char *name)
143{
144 if (!read_obj(name))
145 return 0;
146
147 return atol(buffer);
148}
149
150unsigned long get_obj_and_str(char *name, char **x)
151{
152 unsigned long result = 0;
153 char *p;
154
155 *x = NULL;
156
157 if (!read_obj(name)) {
158 x = NULL;
159 return 0;
160 }
161 result = strtoul(buffer, &p, 10);
162 while (*p == ' ')
163 p++;
164 if (*p)
165 *x = strdup(p);
166 return result;
167}
168
169void set_obj(struct slabinfo *s, char *name, int n)
170{
171 char x[100];
Christoph Lametera87615b2007-05-09 02:32:37 -0700172 FILE *f;
Christoph Lameterc09d8752007-05-06 14:49:48 -0700173
174 sprintf(x, "%s/%s", s->name, name);
Christoph Lametera87615b2007-05-09 02:32:37 -0700175 f = fopen(x, "w");
Christoph Lameterc09d8752007-05-06 14:49:48 -0700176 if (!f)
177 fatal("Cannot write to %s\n", x);
178
179 fprintf(f, "%d\n", n);
180 fclose(f);
181}
182
Christoph Lametera87615b2007-05-09 02:32:37 -0700183unsigned long read_slab_obj(struct slabinfo *s, char *name)
184{
185 char x[100];
186 FILE *f;
187 int l;
188
189 sprintf(x, "%s/%s", s->name, name);
190 f = fopen(x, "r");
191 if (!f) {
192 buffer[0] = 0;
193 l = 0;
194 } else {
195 l = fread(buffer, 1, sizeof(buffer), f);
196 buffer[l] = 0;
197 fclose(f);
198 }
199 return l;
200}
201
202
Christoph Lameterc09d8752007-05-06 14:49:48 -0700203/*
204 * Put a size string together
205 */
206int store_size(char *buffer, unsigned long value)
207{
208 unsigned long divisor = 1;
209 char trailer = 0;
210 int n;
211
212 if (value > 1000000000UL) {
213 divisor = 100000000UL;
214 trailer = 'G';
215 } else if (value > 1000000UL) {
216 divisor = 100000UL;
217 trailer = 'M';
218 } else if (value > 1000UL) {
219 divisor = 100;
220 trailer = 'K';
221 }
222
223 value /= divisor;
224 n = sprintf(buffer, "%ld",value);
225 if (trailer) {
226 buffer[n] = trailer;
227 n++;
228 buffer[n] = 0;
229 }
230 if (divisor != 1) {
231 memmove(buffer + n - 2, buffer + n - 3, 4);
232 buffer[n-2] = '.';
233 n++;
234 }
235 return n;
236}
237
238void decode_numa_list(int *numa, char *t)
239{
240 int node;
241 int nr;
242
243 memset(numa, 0, MAX_NODES * sizeof(int));
244
Christoph Lametereefaca92007-05-16 22:10:55 -0700245 if (!t)
246 return;
247
Christoph Lameterc09d8752007-05-06 14:49:48 -0700248 while (*t == 'N') {
249 t++;
250 node = strtoul(t, &t, 10);
251 if (*t == '=') {
252 t++;
253 nr = strtoul(t, &t, 10);
254 numa[node] = nr;
255 if (node > highest_node)
256 highest_node = node;
257 }
258 while (*t == ' ')
259 t++;
260 }
261}
262
263void slab_validate(struct slabinfo *s)
264{
265 set_obj(s, "validate", 1);
266}
267
268void slab_shrink(struct slabinfo *s)
269{
270 set_obj(s, "shrink", 1);
271}
272
273int line = 0;
274
275void first_line(void)
276{
Christoph Lametera87615b2007-05-09 02:32:37 -0700277 printf("Name Objects Objsize Space "
Christoph Lameterc09d8752007-05-06 14:49:48 -0700278 "Slabs/Part/Cpu O/S O %%Fr %%Ef Flg\n");
279}
280
281/*
282 * Find the shortest alias of a slab
283 */
284struct aliasinfo *find_one_alias(struct slabinfo *find)
285{
286 struct aliasinfo *a;
287 struct aliasinfo *best = NULL;
288
289 for(a = aliasinfo;a < aliasinfo + aliases; a++) {
290 if (a->slab == find &&
291 (!best || strlen(best->name) < strlen(a->name))) {
292 best = a;
293 if (strncmp(a->name,"kmall", 5) == 0)
294 return best;
295 }
296 }
Christoph Lametera87615b2007-05-09 02:32:37 -0700297 return best;
Christoph Lameterc09d8752007-05-06 14:49:48 -0700298}
299
300unsigned long slab_size(struct slabinfo *s)
301{
302 return s->slabs * (page_size << s->order);
303}
304
Christoph Lametera87615b2007-05-09 02:32:37 -0700305void slab_numa(struct slabinfo *s, int mode)
306{
307 int node;
308
309 if (strcmp(s->name, "*") == 0)
310 return;
311
312 if (!highest_node) {
313 printf("\n%s: No NUMA information available.\n", s->name);
314 return;
315 }
316
317 if (skip_zero && !s->slabs)
318 return;
319
320 if (!line) {
321 printf("\n%-21s:", mode ? "NUMA nodes" : "Slab");
322 for(node = 0; node <= highest_node; node++)
323 printf(" %4d", node);
324 printf("\n----------------------");
325 for(node = 0; node <= highest_node; node++)
326 printf("-----");
327 printf("\n");
328 }
329 printf("%-21s ", mode ? "All slabs" : s->name);
330 for(node = 0; node <= highest_node; node++) {
331 char b[20];
332
333 store_size(b, s->numa[node]);
334 printf(" %4s", b);
335 }
336 printf("\n");
337 if (mode) {
338 printf("%-21s ", "Partial slabs");
339 for(node = 0; node <= highest_node; node++) {
340 char b[20];
341
342 store_size(b, s->numa_partial[node]);
343 printf(" %4s", b);
344 }
345 printf("\n");
346 }
347 line++;
348}
349
350void show_tracking(struct slabinfo *s)
351{
352 printf("\n%s: Kernel object allocation\n", s->name);
353 printf("-----------------------------------------------------------------------\n");
354 if (read_slab_obj(s, "alloc_calls"))
355 printf(buffer);
356 else
357 printf("No Data\n");
358
359 printf("\n%s: Kernel object freeing\n", s->name);
360 printf("------------------------------------------------------------------------\n");
361 if (read_slab_obj(s, "free_calls"))
362 printf(buffer);
363 else
364 printf("No Data\n");
365
366}
367
368void ops(struct slabinfo *s)
369{
370 if (strcmp(s->name, "*") == 0)
371 return;
372
373 if (read_slab_obj(s, "ops")) {
374 printf("\n%s: kmem_cache operations\n", s->name);
375 printf("--------------------------------------------\n");
376 printf(buffer);
377 } else
378 printf("\n%s has no kmem_cache operations\n", s->name);
379}
380
381const char *onoff(int x)
382{
383 if (x)
384 return "On ";
385 return "Off";
386}
387
388void report(struct slabinfo *s)
389{
390 if (strcmp(s->name, "*") == 0)
391 return;
Christoph Lametereefaca92007-05-16 22:10:55 -0700392
393 printf("\nSlabcache: %-20s Aliases: %2d Order : %2d Objects: %d\n",
394 s->name, s->aliases, s->order, s->objects);
Christoph Lametera87615b2007-05-09 02:32:37 -0700395 if (s->hwcache_align)
396 printf("** Hardware cacheline aligned\n");
397 if (s->cache_dma)
398 printf("** Memory is allocated in a special DMA zone\n");
399 if (s->destroy_by_rcu)
400 printf("** Slabs are destroyed via RCU\n");
401 if (s->reclaim_account)
402 printf("** Reclaim accounting active\n");
403
404 printf("\nSizes (bytes) Slabs Debug Memory\n");
405 printf("------------------------------------------------------------------------\n");
406 printf("Object : %7d Total : %7ld Sanity Checks : %s Total: %7ld\n",
407 s->object_size, s->slabs, onoff(s->sanity_checks),
408 s->slabs * (page_size << s->order));
409 printf("SlabObj: %7d Full : %7ld Redzoning : %s Used : %7ld\n",
410 s->slab_size, s->slabs - s->partial - s->cpu_slabs,
411 onoff(s->red_zone), s->objects * s->object_size);
412 printf("SlabSiz: %7d Partial: %7ld Poisoning : %s Loss : %7ld\n",
413 page_size << s->order, s->partial, onoff(s->poison),
414 s->slabs * (page_size << s->order) - s->objects * s->object_size);
415 printf("Loss : %7d CpuSlab: %7d Tracking : %s Lalig: %7ld\n",
416 s->slab_size - s->object_size, s->cpu_slabs, onoff(s->store_user),
417 (s->slab_size - s->object_size) * s->objects);
418 printf("Align : %7d Objects: %7d Tracing : %s Lpadd: %7ld\n",
419 s->align, s->objs_per_slab, onoff(s->trace),
420 ((page_size << s->order) - s->objs_per_slab * s->slab_size) *
421 s->slabs);
422
423 ops(s);
424 show_tracking(s);
425 slab_numa(s, 1);
426}
Christoph Lameterc09d8752007-05-06 14:49:48 -0700427
428void slabcache(struct slabinfo *s)
429{
430 char size_str[20];
431 char dist_str[40];
432 char flags[20];
433 char *p = flags;
434
Christoph Lametera87615b2007-05-09 02:32:37 -0700435 if (strcmp(s->name, "*") == 0)
436 return;
437
438 if (actual_slabs == 1) {
439 report(s);
440 return;
441 }
442
443 if (skip_zero && !show_empty && !s->slabs)
444 return;
445
446 if (show_empty && s->slabs)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700447 return;
448
449 store_size(size_str, slab_size(s));
450 sprintf(dist_str,"%lu/%lu/%d", s->slabs, s->partial, s->cpu_slabs);
451
452 if (!line++)
453 first_line();
454
455 if (s->aliases)
456 *p++ = '*';
457 if (s->cache_dma)
458 *p++ = 'd';
459 if (s->hwcache_align)
460 *p++ = 'A';
461 if (s->poison)
462 *p++ = 'P';
463 if (s->reclaim_account)
464 *p++ = 'a';
465 if (s->red_zone)
466 *p++ = 'Z';
467 if (s->sanity_checks)
468 *p++ = 'F';
469 if (s->store_user)
470 *p++ = 'U';
471 if (s->trace)
472 *p++ = 'T';
473
474 *p = 0;
475 printf("%-21s %8ld %7d %8s %14s %4d %1d %3ld %3ld %s\n",
476 s->name, s->objects, s->object_size, size_str, dist_str,
477 s->objs_per_slab, s->order,
478 s->slabs ? (s->partial * 100) / s->slabs : 100,
479 s->slabs ? (s->objects * s->object_size * 100) /
480 (s->slabs * (page_size << s->order)) : 100,
481 flags);
482}
483
Christoph Lametera87615b2007-05-09 02:32:37 -0700484/*
485 * Analyze debug options. Return false if something is amiss.
486 */
487int debug_opt_scan(char *opt)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700488{
Christoph Lametera87615b2007-05-09 02:32:37 -0700489 if (!opt || !opt[0] || strcmp(opt, "-") == 0)
490 return 1;
Christoph Lameterc09d8752007-05-06 14:49:48 -0700491
Christoph Lametera87615b2007-05-09 02:32:37 -0700492 if (strcasecmp(opt, "a") == 0) {
493 sanity = 1;
494 poison = 1;
495 redzone = 1;
496 tracking = 1;
497 return 1;
Christoph Lameterc09d8752007-05-06 14:49:48 -0700498 }
Christoph Lameterc09d8752007-05-06 14:49:48 -0700499
Christoph Lametera87615b2007-05-09 02:32:37 -0700500 for ( ; *opt; opt++)
501 switch (*opt) {
502 case 'F' : case 'f':
503 if (sanity)
504 return 0;
505 sanity = 1;
506 break;
507 case 'P' : case 'p':
508 if (poison)
509 return 0;
510 poison = 1;
511 break;
512
513 case 'Z' : case 'z':
514 if (redzone)
515 return 0;
516 redzone = 1;
517 break;
518
519 case 'U' : case 'u':
520 if (tracking)
521 return 0;
522 tracking = 1;
523 break;
524
525 case 'T' : case 't':
526 if (tracing)
527 return 0;
528 tracing = 1;
529 break;
530 default:
531 return 0;
532 }
533 return 1;
Christoph Lameterc09d8752007-05-06 14:49:48 -0700534}
535
Christoph Lametera87615b2007-05-09 02:32:37 -0700536int slab_empty(struct slabinfo *s)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700537{
Christoph Lametera87615b2007-05-09 02:32:37 -0700538 if (s->objects > 0)
539 return 0;
Christoph Lameterc09d8752007-05-06 14:49:48 -0700540
Christoph Lametera87615b2007-05-09 02:32:37 -0700541 /*
542 * We may still have slabs even if there are no objects. Shrinking will
543 * remove them.
544 */
545 if (s->slabs != 0)
546 set_obj(s, "shrink", 1);
Christoph Lameterc09d8752007-05-06 14:49:48 -0700547
Christoph Lametera87615b2007-05-09 02:32:37 -0700548 return 1;
549}
550
551void slab_debug(struct slabinfo *s)
552{
553 if (sanity && !s->sanity_checks) {
554 set_obj(s, "sanity", 1);
555 }
556 if (!sanity && s->sanity_checks) {
557 if (slab_empty(s))
558 set_obj(s, "sanity", 0);
559 else
560 fprintf(stderr, "%s not empty cannot disable sanity checks\n", s->name);
561 }
562 if (redzone && !s->red_zone) {
563 if (slab_empty(s))
564 set_obj(s, "red_zone", 1);
565 else
566 fprintf(stderr, "%s not empty cannot enable redzoning\n", s->name);
567 }
568 if (!redzone && s->red_zone) {
569 if (slab_empty(s))
570 set_obj(s, "red_zone", 0);
571 else
572 fprintf(stderr, "%s not empty cannot disable redzoning\n", s->name);
573 }
574 if (poison && !s->poison) {
575 if (slab_empty(s))
576 set_obj(s, "poison", 1);
577 else
578 fprintf(stderr, "%s not empty cannot enable poisoning\n", s->name);
579 }
580 if (!poison && s->poison) {
581 if (slab_empty(s))
582 set_obj(s, "poison", 0);
583 else
584 fprintf(stderr, "%s not empty cannot disable poisoning\n", s->name);
585 }
586 if (tracking && !s->store_user) {
587 if (slab_empty(s))
588 set_obj(s, "store_user", 1);
589 else
590 fprintf(stderr, "%s not empty cannot enable tracking\n", s->name);
591 }
592 if (!tracking && s->store_user) {
593 if (slab_empty(s))
594 set_obj(s, "store_user", 0);
595 else
596 fprintf(stderr, "%s not empty cannot disable tracking\n", s->name);
597 }
598 if (tracing && !s->trace) {
599 if (slabs == 1)
600 set_obj(s, "trace", 1);
601 else
602 fprintf(stderr, "%s can only enable trace for one slab at a time\n", s->name);
603 }
604 if (!tracing && s->trace)
605 set_obj(s, "trace", 1);
Christoph Lameterc09d8752007-05-06 14:49:48 -0700606}
607
608void totals(void)
609{
610 struct slabinfo *s;
611
612 int used_slabs = 0;
613 char b1[20], b2[20], b3[20], b4[20];
614 unsigned long long max = 1ULL << 63;
615
616 /* Object size */
617 unsigned long long min_objsize = max, max_objsize = 0, avg_objsize;
618
619 /* Number of partial slabs in a slabcache */
620 unsigned long long min_partial = max, max_partial = 0,
621 avg_partial, total_partial = 0;
622
623 /* Number of slabs in a slab cache */
624 unsigned long long min_slabs = max, max_slabs = 0,
625 avg_slabs, total_slabs = 0;
626
627 /* Size of the whole slab */
628 unsigned long long min_size = max, max_size = 0,
629 avg_size, total_size = 0;
630
631 /* Bytes used for object storage in a slab */
632 unsigned long long min_used = max, max_used = 0,
633 avg_used, total_used = 0;
634
635 /* Waste: Bytes used for alignment and padding */
636 unsigned long long min_waste = max, max_waste = 0,
637 avg_waste, total_waste = 0;
638 /* Number of objects in a slab */
639 unsigned long long min_objects = max, max_objects = 0,
640 avg_objects, total_objects = 0;
641 /* Waste per object */
642 unsigned long long min_objwaste = max,
643 max_objwaste = 0, avg_objwaste,
644 total_objwaste = 0;
645
646 /* Memory per object */
647 unsigned long long min_memobj = max,
648 max_memobj = 0, avg_memobj,
649 total_objsize = 0;
650
651 /* Percentage of partial slabs per slab */
652 unsigned long min_ppart = 100, max_ppart = 0,
653 avg_ppart, total_ppart = 0;
654
655 /* Number of objects in partial slabs */
656 unsigned long min_partobj = max, max_partobj = 0,
657 avg_partobj, total_partobj = 0;
658
659 /* Percentage of partial objects of all objects in a slab */
660 unsigned long min_ppartobj = 100, max_ppartobj = 0,
661 avg_ppartobj, total_ppartobj = 0;
662
663
664 for (s = slabinfo; s < slabinfo + slabs; s++) {
665 unsigned long long size;
666 unsigned long used;
667 unsigned long long wasted;
668 unsigned long long objwaste;
669 long long objects_in_partial_slabs;
670 unsigned long percentage_partial_slabs;
671 unsigned long percentage_partial_objs;
672
673 if (!s->slabs || !s->objects)
674 continue;
675
676 used_slabs++;
677
678 size = slab_size(s);
679 used = s->objects * s->object_size;
680 wasted = size - used;
681 objwaste = s->slab_size - s->object_size;
682
683 objects_in_partial_slabs = s->objects -
684 (s->slabs - s->partial - s ->cpu_slabs) *
685 s->objs_per_slab;
686
687 if (objects_in_partial_slabs < 0)
688 objects_in_partial_slabs = 0;
689
690 percentage_partial_slabs = s->partial * 100 / s->slabs;
691 if (percentage_partial_slabs > 100)
692 percentage_partial_slabs = 100;
693
694 percentage_partial_objs = objects_in_partial_slabs * 100
695 / s->objects;
696
697 if (percentage_partial_objs > 100)
698 percentage_partial_objs = 100;
699
700 if (s->object_size < min_objsize)
701 min_objsize = s->object_size;
702 if (s->partial < min_partial)
703 min_partial = s->partial;
704 if (s->slabs < min_slabs)
705 min_slabs = s->slabs;
706 if (size < min_size)
707 min_size = size;
708 if (wasted < min_waste)
709 min_waste = wasted;
710 if (objwaste < min_objwaste)
711 min_objwaste = objwaste;
712 if (s->objects < min_objects)
713 min_objects = s->objects;
714 if (used < min_used)
715 min_used = used;
716 if (objects_in_partial_slabs < min_partobj)
717 min_partobj = objects_in_partial_slabs;
718 if (percentage_partial_slabs < min_ppart)
719 min_ppart = percentage_partial_slabs;
720 if (percentage_partial_objs < min_ppartobj)
721 min_ppartobj = percentage_partial_objs;
722 if (s->slab_size < min_memobj)
723 min_memobj = s->slab_size;
724
725 if (s->object_size > max_objsize)
726 max_objsize = s->object_size;
727 if (s->partial > max_partial)
728 max_partial = s->partial;
729 if (s->slabs > max_slabs)
730 max_slabs = s->slabs;
731 if (size > max_size)
732 max_size = size;
733 if (wasted > max_waste)
734 max_waste = wasted;
735 if (objwaste > max_objwaste)
736 max_objwaste = objwaste;
737 if (s->objects > max_objects)
738 max_objects = s->objects;
739 if (used > max_used)
740 max_used = used;
741 if (objects_in_partial_slabs > max_partobj)
742 max_partobj = objects_in_partial_slabs;
743 if (percentage_partial_slabs > max_ppart)
744 max_ppart = percentage_partial_slabs;
745 if (percentage_partial_objs > max_ppartobj)
746 max_ppartobj = percentage_partial_objs;
747 if (s->slab_size > max_memobj)
748 max_memobj = s->slab_size;
749
750 total_partial += s->partial;
751 total_slabs += s->slabs;
752 total_size += size;
753 total_waste += wasted;
754
755 total_objects += s->objects;
756 total_used += used;
757 total_partobj += objects_in_partial_slabs;
758 total_ppart += percentage_partial_slabs;
759 total_ppartobj += percentage_partial_objs;
760
761 total_objwaste += s->objects * objwaste;
762 total_objsize += s->objects * s->slab_size;
763 }
764
765 if (!total_objects) {
766 printf("No objects\n");
767 return;
768 }
769 if (!used_slabs) {
770 printf("No slabs\n");
771 return;
772 }
773
774 /* Per slab averages */
775 avg_partial = total_partial / used_slabs;
776 avg_slabs = total_slabs / used_slabs;
777 avg_size = total_size / used_slabs;
778 avg_waste = total_waste / used_slabs;
779
780 avg_objects = total_objects / used_slabs;
781 avg_used = total_used / used_slabs;
782 avg_partobj = total_partobj / used_slabs;
783 avg_ppart = total_ppart / used_slabs;
784 avg_ppartobj = total_ppartobj / used_slabs;
785
786 /* Per object object sizes */
787 avg_objsize = total_used / total_objects;
788 avg_objwaste = total_objwaste / total_objects;
789 avg_partobj = total_partobj * 100 / total_objects;
790 avg_memobj = total_objsize / total_objects;
791
792 printf("Slabcache Totals\n");
793 printf("----------------\n");
794 printf("Slabcaches : %3d Aliases : %3d->%-3d Active: %3d\n",
795 slabs, aliases, alias_targets, used_slabs);
796
797 store_size(b1, total_size);store_size(b2, total_waste);
798 store_size(b3, total_waste * 100 / total_used);
Christoph Lametereefaca92007-05-16 22:10:55 -0700799 printf("Memory used: %6s # Loss : %6s MRatio:%6s%%\n", b1, b2, b3);
Christoph Lameterc09d8752007-05-06 14:49:48 -0700800
801 store_size(b1, total_objects);store_size(b2, total_partobj);
802 store_size(b3, total_partobj * 100 / total_objects);
Christoph Lametereefaca92007-05-16 22:10:55 -0700803 printf("# Objects : %6s # PartObj: %6s ORatio:%6s%%\n", b1, b2, b3);
Christoph Lameterc09d8752007-05-06 14:49:48 -0700804
805 printf("\n");
806 printf("Per Cache Average Min Max Total\n");
807 printf("---------------------------------------------------------\n");
808
809 store_size(b1, avg_objects);store_size(b2, min_objects);
810 store_size(b3, max_objects);store_size(b4, total_objects);
811 printf("#Objects %10s %10s %10s %10s\n",
812 b1, b2, b3, b4);
813
814 store_size(b1, avg_slabs);store_size(b2, min_slabs);
815 store_size(b3, max_slabs);store_size(b4, total_slabs);
816 printf("#Slabs %10s %10s %10s %10s\n",
817 b1, b2, b3, b4);
818
819 store_size(b1, avg_partial);store_size(b2, min_partial);
820 store_size(b3, max_partial);store_size(b4, total_partial);
821 printf("#PartSlab %10s %10s %10s %10s\n",
822 b1, b2, b3, b4);
823 store_size(b1, avg_ppart);store_size(b2, min_ppart);
824 store_size(b3, max_ppart);
825 store_size(b4, total_partial * 100 / total_slabs);
Christoph Lametereefaca92007-05-16 22:10:55 -0700826 printf("%%PartSlab%10s%% %10s%% %10s%% %10s%%\n",
Christoph Lameterc09d8752007-05-06 14:49:48 -0700827 b1, b2, b3, b4);
828
829 store_size(b1, avg_partobj);store_size(b2, min_partobj);
830 store_size(b3, max_partobj);
831 store_size(b4, total_partobj);
832 printf("PartObjs %10s %10s %10s %10s\n",
833 b1, b2, b3, b4);
834
835 store_size(b1, avg_ppartobj);store_size(b2, min_ppartobj);
836 store_size(b3, max_ppartobj);
837 store_size(b4, total_partobj * 100 / total_objects);
Christoph Lametereefaca92007-05-16 22:10:55 -0700838 printf("%% PartObj%10s%% %10s%% %10s%% %10s%%\n",
Christoph Lameterc09d8752007-05-06 14:49:48 -0700839 b1, b2, b3, b4);
840
841 store_size(b1, avg_size);store_size(b2, min_size);
842 store_size(b3, max_size);store_size(b4, total_size);
843 printf("Memory %10s %10s %10s %10s\n",
844 b1, b2, b3, b4);
845
846 store_size(b1, avg_used);store_size(b2, min_used);
847 store_size(b3, max_used);store_size(b4, total_used);
848 printf("Used %10s %10s %10s %10s\n",
849 b1, b2, b3, b4);
850
851 store_size(b1, avg_waste);store_size(b2, min_waste);
852 store_size(b3, max_waste);store_size(b4, total_waste);
853 printf("Loss %10s %10s %10s %10s\n",
854 b1, b2, b3, b4);
855
856 printf("\n");
857 printf("Per Object Average Min Max\n");
858 printf("---------------------------------------------\n");
859
860 store_size(b1, avg_memobj);store_size(b2, min_memobj);
861 store_size(b3, max_memobj);
862 printf("Memory %10s %10s %10s\n",
863 b1, b2, b3);
864 store_size(b1, avg_objsize);store_size(b2, min_objsize);
865 store_size(b3, max_objsize);
866 printf("User %10s %10s %10s\n",
867 b1, b2, b3);
868
869 store_size(b1, avg_objwaste);store_size(b2, min_objwaste);
870 store_size(b3, max_objwaste);
871 printf("Loss %10s %10s %10s\n",
872 b1, b2, b3);
873}
874
875void sort_slabs(void)
876{
877 struct slabinfo *s1,*s2;
878
879 for (s1 = slabinfo; s1 < slabinfo + slabs; s1++) {
880 for (s2 = s1 + 1; s2 < slabinfo + slabs; s2++) {
881 int result;
882
883 if (sort_size)
884 result = slab_size(s1) < slab_size(s2);
885 else
886 result = strcasecmp(s1->name, s2->name);
887
888 if (show_inverted)
889 result = -result;
890
891 if (result > 0) {
892 struct slabinfo t;
893
894 memcpy(&t, s1, sizeof(struct slabinfo));
895 memcpy(s1, s2, sizeof(struct slabinfo));
896 memcpy(s2, &t, sizeof(struct slabinfo));
897 }
898 }
899 }
900}
901
902void sort_aliases(void)
903{
904 struct aliasinfo *a1,*a2;
905
906 for (a1 = aliasinfo; a1 < aliasinfo + aliases; a1++) {
907 for (a2 = a1 + 1; a2 < aliasinfo + aliases; a2++) {
908 char *n1, *n2;
909
910 n1 = a1->name;
911 n2 = a2->name;
912 if (show_alias && !show_inverted) {
913 n1 = a1->ref;
914 n2 = a2->ref;
915 }
916 if (strcasecmp(n1, n2) > 0) {
917 struct aliasinfo t;
918
919 memcpy(&t, a1, sizeof(struct aliasinfo));
920 memcpy(a1, a2, sizeof(struct aliasinfo));
921 memcpy(a2, &t, sizeof(struct aliasinfo));
922 }
923 }
924 }
925}
926
927void link_slabs(void)
928{
929 struct aliasinfo *a;
930 struct slabinfo *s;
931
932 for (a = aliasinfo; a < aliasinfo + aliases; a++) {
933
Christoph Lametera87615b2007-05-09 02:32:37 -0700934 for (s = slabinfo; s < slabinfo + slabs; s++)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700935 if (strcmp(a->ref, s->name) == 0) {
936 a->slab = s;
937 s->refs++;
938 break;
939 }
940 if (s == slabinfo + slabs)
941 fatal("Unresolved alias %s\n", a->ref);
942 }
943}
944
945void alias(void)
946{
947 struct aliasinfo *a;
948 char *active = NULL;
949
950 sort_aliases();
951 link_slabs();
952
953 for(a = aliasinfo; a < aliasinfo + aliases; a++) {
954
955 if (!show_single_ref && a->slab->refs == 1)
956 continue;
957
958 if (!show_inverted) {
959 if (active) {
960 if (strcmp(a->slab->name, active) == 0) {
961 printf(" %s", a->name);
962 continue;
963 }
964 }
Christoph Lametera87615b2007-05-09 02:32:37 -0700965 printf("\n%-12s <- %s", a->slab->name, a->name);
Christoph Lameterc09d8752007-05-06 14:49:48 -0700966 active = a->slab->name;
967 }
968 else
969 printf("%-20s -> %s\n", a->name, a->slab->name);
970 }
971 if (active)
972 printf("\n");
973}
974
975
976void rename_slabs(void)
977{
978 struct slabinfo *s;
979 struct aliasinfo *a;
980
981 for (s = slabinfo; s < slabinfo + slabs; s++) {
982 if (*s->name != ':')
983 continue;
984
985 if (s->refs > 1 && !show_first_alias)
986 continue;
987
988 a = find_one_alias(s);
989
Christoph Lametera87615b2007-05-09 02:32:37 -0700990 if (a)
991 s->name = a->name;
992 else {
993 s->name = "*";
994 actual_slabs--;
995 }
Christoph Lameterc09d8752007-05-06 14:49:48 -0700996 }
997}
998
999int slab_mismatch(char *slab)
1000{
1001 return regexec(&pattern, slab, 0, NULL, 0);
1002}
1003
1004void read_slab_dir(void)
1005{
1006 DIR *dir;
1007 struct dirent *de;
1008 struct slabinfo *slab = slabinfo;
1009 struct aliasinfo *alias = aliasinfo;
1010 char *p;
1011 char *t;
1012 int count;
1013
Christoph Lametera87615b2007-05-09 02:32:37 -07001014 if (chdir("/sys/slab"))
1015 fatal("SYSFS support for SLUB not active\n");
1016
Christoph Lameterc09d8752007-05-06 14:49:48 -07001017 dir = opendir(".");
1018 while ((de = readdir(dir))) {
1019 if (de->d_name[0] == '.' ||
Christoph Lametera87615b2007-05-09 02:32:37 -07001020 (de->d_name[0] != ':' && slab_mismatch(de->d_name)))
1021 continue;
Christoph Lameterc09d8752007-05-06 14:49:48 -07001022 switch (de->d_type) {
1023 case DT_LNK:
1024 alias->name = strdup(de->d_name);
1025 count = readlink(de->d_name, buffer, sizeof(buffer));
1026
1027 if (count < 0)
1028 fatal("Cannot read symlink %s\n", de->d_name);
1029
1030 buffer[count] = 0;
1031 p = buffer + count;
1032 while (p > buffer && p[-1] != '/')
1033 p--;
1034 alias->ref = strdup(p);
1035 alias++;
1036 break;
1037 case DT_DIR:
1038 if (chdir(de->d_name))
1039 fatal("Unable to access slab %s\n", slab->name);
1040 slab->name = strdup(de->d_name);
1041 slab->alias = 0;
1042 slab->refs = 0;
1043 slab->aliases = get_obj("aliases");
1044 slab->align = get_obj("align");
1045 slab->cache_dma = get_obj("cache_dma");
1046 slab->cpu_slabs = get_obj("cpu_slabs");
1047 slab->destroy_by_rcu = get_obj("destroy_by_rcu");
1048 slab->hwcache_align = get_obj("hwcache_align");
1049 slab->object_size = get_obj("object_size");
1050 slab->objects = get_obj("objects");
1051 slab->objs_per_slab = get_obj("objs_per_slab");
1052 slab->order = get_obj("order");
1053 slab->partial = get_obj("partial");
1054 slab->partial = get_obj_and_str("partial", &t);
1055 decode_numa_list(slab->numa_partial, t);
1056 slab->poison = get_obj("poison");
1057 slab->reclaim_account = get_obj("reclaim_account");
1058 slab->red_zone = get_obj("red_zone");
1059 slab->sanity_checks = get_obj("sanity_checks");
1060 slab->slab_size = get_obj("slab_size");
1061 slab->slabs = get_obj_and_str("slabs", &t);
1062 decode_numa_list(slab->numa, t);
1063 slab->store_user = get_obj("store_user");
1064 slab->trace = get_obj("trace");
1065 chdir("..");
1066 if (slab->name[0] == ':')
1067 alias_targets++;
1068 slab++;
1069 break;
1070 default :
1071 fatal("Unknown file type %lx\n", de->d_type);
1072 }
1073 }
1074 closedir(dir);
1075 slabs = slab - slabinfo;
Christoph Lametera87615b2007-05-09 02:32:37 -07001076 actual_slabs = slabs;
Christoph Lameterc09d8752007-05-06 14:49:48 -07001077 aliases = alias - aliasinfo;
1078 if (slabs > MAX_SLABS)
1079 fatal("Too many slabs\n");
1080 if (aliases > MAX_ALIASES)
1081 fatal("Too many aliases\n");
1082}
1083
1084void output_slabs(void)
1085{
1086 struct slabinfo *slab;
1087
1088 for (slab = slabinfo; slab < slabinfo + slabs; slab++) {
1089
1090 if (slab->alias)
1091 continue;
1092
1093
1094 if (show_numa)
Christoph Lametera87615b2007-05-09 02:32:37 -07001095 slab_numa(slab, 0);
1096 else if (show_track)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001097 show_tracking(slab);
Christoph Lametera87615b2007-05-09 02:32:37 -07001098 else if (validate)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001099 slab_validate(slab);
Christoph Lametera87615b2007-05-09 02:32:37 -07001100 else if (shrink)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001101 slab_shrink(slab);
Christoph Lametera87615b2007-05-09 02:32:37 -07001102 else if (set_debug)
1103 slab_debug(slab);
1104 else if (show_ops)
1105 ops(slab);
1106 else if (show_slab)
1107 slabcache(slab);
Christoph Lametereefaca92007-05-16 22:10:55 -07001108 else if (show_report)
1109 report(slab);
Christoph Lameterc09d8752007-05-06 14:49:48 -07001110 }
1111}
1112
1113struct option opts[] = {
1114 { "aliases", 0, NULL, 'a' },
Christoph Lametera87615b2007-05-09 02:32:37 -07001115 { "debug", 2, NULL, 'd' },
1116 { "empty", 0, NULL, 'e' },
Christoph Lameterc09d8752007-05-06 14:49:48 -07001117 { "first-alias", 0, NULL, 'f' },
Christoph Lametera87615b2007-05-09 02:32:37 -07001118 { "help", 0, NULL, 'h' },
Christoph Lameterc09d8752007-05-06 14:49:48 -07001119 { "inverted", 0, NULL, 'i'},
Christoph Lametera87615b2007-05-09 02:32:37 -07001120 { "numa", 0, NULL, 'n' },
1121 { "ops", 0, NULL, 'o' },
1122 { "report", 0, NULL, 'r' },
1123 { "shrink", 0, NULL, 's' },
1124 { "slabs", 0, NULL, 'l' },
1125 { "track", 0, NULL, 't'},
1126 { "validate", 0, NULL, 'v' },
1127 { "zero", 0, NULL, 'z' },
Christoph Lameterc09d8752007-05-06 14:49:48 -07001128 { "1ref", 0, NULL, '1'},
1129 { NULL, 0, NULL, 0 }
1130};
1131
1132int main(int argc, char *argv[])
1133{
1134 int c;
1135 int err;
1136 char *pattern_source;
1137
1138 page_size = getpagesize();
Christoph Lameterc09d8752007-05-06 14:49:48 -07001139
Christoph Lametera87615b2007-05-09 02:32:37 -07001140 while ((c = getopt_long(argc, argv, "ad::efhil1noprstvzTS",
1141 opts, NULL)) != -1)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001142 switch(c) {
1143 case '1':
1144 show_single_ref = 1;
1145 break;
1146 case 'a':
1147 show_alias = 1;
1148 break;
Christoph Lametera87615b2007-05-09 02:32:37 -07001149 case 'd':
1150 set_debug = 1;
1151 if (!debug_opt_scan(optarg))
1152 fatal("Invalid debug option '%s'\n", optarg);
1153 break;
1154 case 'e':
1155 show_empty = 1;
1156 break;
Christoph Lameterc09d8752007-05-06 14:49:48 -07001157 case 'f':
1158 show_first_alias = 1;
1159 break;
1160 case 'h':
1161 usage();
1162 return 0;
1163 case 'i':
1164 show_inverted = 1;
1165 break;
1166 case 'n':
1167 show_numa = 1;
1168 break;
Christoph Lametera87615b2007-05-09 02:32:37 -07001169 case 'o':
1170 show_ops = 1;
1171 break;
1172 case 'r':
1173 show_report = 1;
1174 break;
Christoph Lameterc09d8752007-05-06 14:49:48 -07001175 case 's':
1176 shrink = 1;
1177 break;
1178 case 'l':
1179 show_slab = 1;
1180 break;
1181 case 't':
1182 show_track = 1;
1183 break;
1184 case 'v':
1185 validate = 1;
1186 break;
1187 case 'z':
1188 skip_zero = 0;
1189 break;
1190 case 'T':
1191 show_totals = 1;
1192 break;
1193 case 'S':
1194 sort_size = 1;
1195 break;
1196
1197 default:
1198 fatal("%s: Invalid option '%c'\n", argv[0], optopt);
1199
1200 }
1201
Christoph Lametera87615b2007-05-09 02:32:37 -07001202 if (!show_slab && !show_alias && !show_track && !show_report
1203 && !validate && !shrink && !set_debug && !show_ops)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001204 show_slab = 1;
1205
1206 if (argc > optind)
1207 pattern_source = argv[optind];
1208 else
1209 pattern_source = ".*";
1210
1211 err = regcomp(&pattern, pattern_source, REG_ICASE|REG_NOSUB);
1212 if (err)
1213 fatal("%s: Invalid pattern '%s' code %d\n",
1214 argv[0], pattern_source, err);
1215 read_slab_dir();
1216 if (show_alias)
1217 alias();
1218 else
1219 if (show_totals)
1220 totals();
1221 else {
1222 link_slabs();
1223 rename_slabs();
1224 sort_slabs();
1225 output_slabs();
1226 }
1227 return 0;
1228}