2 * Slabinfo: Tool to get reports about slabs
4 * (C) 2007 sgi, Christoph Lameter <clameter@sgi.com>
8 * gcc -o slabinfo slabinfo.c
12 #include <sys/types.h>
21 #define MAX_ALIASES 500
22 #define MAX_NODES 1024
28 int aliases, align, cache_dma, cpu_slabs, destroy_by_rcu;
29 int hwcache_align, object_size, objs_per_slab;
30 int sanity_checks, slab_size, store_user, trace;
31 int order, poison, reclaim_account, red_zone;
32 unsigned long partial, objects, slabs;
34 int numa_partial[MAX_NODES];
35 } slabinfo[MAX_SLABS];
40 struct slabinfo *slab;
41 } aliasinfo[MAX_ALIASES];
45 int alias_targets = 0;
55 int show_first_alias = 0;
58 int show_inverted = 0;
59 int show_single_ref = 0;
67 void fatal(const char *x, ...)
72 vfprintf(stderr, x, ap);
79 printf("slabinfo [-ahnpvtsz] [slab-regexp]\n"
80 "-a|--aliases Show aliases\n"
81 "-h|--help Show usage information\n"
82 "-n|--numa Show NUMA information\n"
83 "-s|--shrink Shrink slabs\n"
84 "-v|--validate Validate slabs\n"
85 "-t|--tracking Show alloc/free information\n"
86 "-T|--Totals Show summary information\n"
87 "-l|--slabs Show slabs\n"
88 "-S|--Size Sort by size\n"
89 "-z|--zero Include empty slabs\n"
90 "-f|--first-alias Show first alias\n"
91 "-i|--inverted Inverted list\n"
92 "-1|--1ref Single reference\n"
96 unsigned long read_obj(char *name)
98 FILE *f = fopen(name, "r");
103 if (!fgets(buffer,sizeof(buffer), f))
106 if (buffer[strlen(buffer)] == '\n')
107 buffer[strlen(buffer)] = 0;
109 return strlen(buffer);
114 * Get the contents of an attribute
116 unsigned long get_obj(char *name)
124 unsigned long get_obj_and_str(char *name, char **x)
126 unsigned long result = 0;
131 if (!read_obj(name)) {
135 result = strtoul(buffer, &p, 10);
143 void set_obj(struct slabinfo *s, char *name, int n)
147 sprintf(x, "%s/%s", s->name, name);
149 FILE *f = fopen(x, "w");
152 fatal("Cannot write to %s\n", x);
154 fprintf(f, "%d\n", n);
159 * Put a size string together
161 int store_size(char *buffer, unsigned long value)
163 unsigned long divisor = 1;
167 if (value > 1000000000UL) {
168 divisor = 100000000UL;
170 } else if (value > 1000000UL) {
173 } else if (value > 1000UL) {
179 n = sprintf(buffer, "%ld",value);
186 memmove(buffer + n - 2, buffer + n - 3, 4);
193 void decode_numa_list(int *numa, char *t)
198 memset(numa, 0, MAX_NODES * sizeof(int));
202 node = strtoul(t, &t, 10);
205 nr = strtoul(t, &t, 10);
207 if (node > highest_node)
215 void slab_validate(struct slabinfo *s)
217 set_obj(s, "validate", 1);
220 void slab_shrink(struct slabinfo *s)
222 set_obj(s, "shrink", 1);
227 void first_line(void)
229 printf("Name Objects Objsize Space "
230 "Slabs/Part/Cpu O/S O %%Fr %%Ef Flg\n");
234 * Find the shortest alias of a slab
236 struct aliasinfo *find_one_alias(struct slabinfo *find)
239 struct aliasinfo *best = NULL;
241 for(a = aliasinfo;a < aliasinfo + aliases; a++) {
242 if (a->slab == find &&
243 (!best || strlen(best->name) < strlen(a->name))) {
245 if (strncmp(a->name,"kmall", 5) == 0)
251 fatal("Cannot find alias for %s\n", find->name);
255 unsigned long slab_size(struct slabinfo *s)
257 return s->slabs * (page_size << s->order);
261 void slabcache(struct slabinfo *s)
268 if (skip_zero && !s->slabs)
271 store_size(size_str, slab_size(s));
272 sprintf(dist_str,"%lu/%lu/%d", s->slabs, s->partial, s->cpu_slabs);
281 if (s->hwcache_align)
285 if (s->reclaim_account)
289 if (s->sanity_checks)
297 printf("%-21s %8ld %7d %8s %14s %4d %1d %3ld %3ld %s\n",
298 s->name, s->objects, s->object_size, size_str, dist_str,
299 s->objs_per_slab, s->order,
300 s->slabs ? (s->partial * 100) / s->slabs : 100,
301 s->slabs ? (s->objects * s->object_size * 100) /
302 (s->slabs * (page_size << s->order)) : 100,
306 void slab_numa(struct slabinfo *s)
311 fatal("No NUMA information available.\n");
313 if (skip_zero && !s->slabs)
317 printf("\nSlab Node ");
318 for(node = 0; node <= highest_node; node++)
319 printf(" %4d", node);
320 printf("\n----------------------");
321 for(node = 0; node <= highest_node; node++)
325 printf("%-21s ", s->name);
326 for(node = 0; node <= highest_node; node++) {
329 store_size(b, s->numa[node]);
336 void show_tracking(struct slabinfo *s)
338 printf("\n%s: Calls to allocate a slab object\n", s->name);
339 printf("---------------------------------------------------\n");
340 if (read_obj("alloc_calls"))
343 printf("%s: Calls to free a slab object\n", s->name);
344 printf("-----------------------------------------------\n");
345 if (read_obj("free_calls"))
355 char b1[20], b2[20], b3[20], b4[20];
356 unsigned long long max = 1ULL << 63;
359 unsigned long long min_objsize = max, max_objsize = 0, avg_objsize;
361 /* Number of partial slabs in a slabcache */
362 unsigned long long min_partial = max, max_partial = 0,
363 avg_partial, total_partial = 0;
365 /* Number of slabs in a slab cache */
366 unsigned long long min_slabs = max, max_slabs = 0,
367 avg_slabs, total_slabs = 0;
369 /* Size of the whole slab */
370 unsigned long long min_size = max, max_size = 0,
371 avg_size, total_size = 0;
373 /* Bytes used for object storage in a slab */
374 unsigned long long min_used = max, max_used = 0,
375 avg_used, total_used = 0;
377 /* Waste: Bytes used for alignment and padding */
378 unsigned long long min_waste = max, max_waste = 0,
379 avg_waste, total_waste = 0;
380 /* Number of objects in a slab */
381 unsigned long long min_objects = max, max_objects = 0,
382 avg_objects, total_objects = 0;
383 /* Waste per object */
384 unsigned long long min_objwaste = max,
385 max_objwaste = 0, avg_objwaste,
388 /* Memory per object */
389 unsigned long long min_memobj = max,
390 max_memobj = 0, avg_memobj,
393 /* Percentage of partial slabs per slab */
394 unsigned long min_ppart = 100, max_ppart = 0,
395 avg_ppart, total_ppart = 0;
397 /* Number of objects in partial slabs */
398 unsigned long min_partobj = max, max_partobj = 0,
399 avg_partobj, total_partobj = 0;
401 /* Percentage of partial objects of all objects in a slab */
402 unsigned long min_ppartobj = 100, max_ppartobj = 0,
403 avg_ppartobj, total_ppartobj = 0;
406 for (s = slabinfo; s < slabinfo + slabs; s++) {
407 unsigned long long size;
409 unsigned long long wasted;
410 unsigned long long objwaste;
411 long long objects_in_partial_slabs;
412 unsigned long percentage_partial_slabs;
413 unsigned long percentage_partial_objs;
415 if (!s->slabs || !s->objects)
421 used = s->objects * s->object_size;
422 wasted = size - used;
423 objwaste = s->slab_size - s->object_size;
425 objects_in_partial_slabs = s->objects -
426 (s->slabs - s->partial - s ->cpu_slabs) *
429 if (objects_in_partial_slabs < 0)
430 objects_in_partial_slabs = 0;
432 percentage_partial_slabs = s->partial * 100 / s->slabs;
433 if (percentage_partial_slabs > 100)
434 percentage_partial_slabs = 100;
436 percentage_partial_objs = objects_in_partial_slabs * 100
439 if (percentage_partial_objs > 100)
440 percentage_partial_objs = 100;
442 if (s->object_size < min_objsize)
443 min_objsize = s->object_size;
444 if (s->partial < min_partial)
445 min_partial = s->partial;
446 if (s->slabs < min_slabs)
447 min_slabs = s->slabs;
450 if (wasted < min_waste)
452 if (objwaste < min_objwaste)
453 min_objwaste = objwaste;
454 if (s->objects < min_objects)
455 min_objects = s->objects;
458 if (objects_in_partial_slabs < min_partobj)
459 min_partobj = objects_in_partial_slabs;
460 if (percentage_partial_slabs < min_ppart)
461 min_ppart = percentage_partial_slabs;
462 if (percentage_partial_objs < min_ppartobj)
463 min_ppartobj = percentage_partial_objs;
464 if (s->slab_size < min_memobj)
465 min_memobj = s->slab_size;
467 if (s->object_size > max_objsize)
468 max_objsize = s->object_size;
469 if (s->partial > max_partial)
470 max_partial = s->partial;
471 if (s->slabs > max_slabs)
472 max_slabs = s->slabs;
475 if (wasted > max_waste)
477 if (objwaste > max_objwaste)
478 max_objwaste = objwaste;
479 if (s->objects > max_objects)
480 max_objects = s->objects;
483 if (objects_in_partial_slabs > max_partobj)
484 max_partobj = objects_in_partial_slabs;
485 if (percentage_partial_slabs > max_ppart)
486 max_ppart = percentage_partial_slabs;
487 if (percentage_partial_objs > max_ppartobj)
488 max_ppartobj = percentage_partial_objs;
489 if (s->slab_size > max_memobj)
490 max_memobj = s->slab_size;
492 total_partial += s->partial;
493 total_slabs += s->slabs;
495 total_waste += wasted;
497 total_objects += s->objects;
499 total_partobj += objects_in_partial_slabs;
500 total_ppart += percentage_partial_slabs;
501 total_ppartobj += percentage_partial_objs;
503 total_objwaste += s->objects * objwaste;
504 total_objsize += s->objects * s->slab_size;
507 if (!total_objects) {
508 printf("No objects\n");
512 printf("No slabs\n");
516 /* Per slab averages */
517 avg_partial = total_partial / used_slabs;
518 avg_slabs = total_slabs / used_slabs;
519 avg_size = total_size / used_slabs;
520 avg_waste = total_waste / used_slabs;
522 avg_objects = total_objects / used_slabs;
523 avg_used = total_used / used_slabs;
524 avg_partobj = total_partobj / used_slabs;
525 avg_ppart = total_ppart / used_slabs;
526 avg_ppartobj = total_ppartobj / used_slabs;
528 /* Per object object sizes */
529 avg_objsize = total_used / total_objects;
530 avg_objwaste = total_objwaste / total_objects;
531 avg_partobj = total_partobj * 100 / total_objects;
532 avg_memobj = total_objsize / total_objects;
534 printf("Slabcache Totals\n");
535 printf("----------------\n");
536 printf("Slabcaches : %3d Aliases : %3d->%-3d Active: %3d\n",
537 slabs, aliases, alias_targets, used_slabs);
539 store_size(b1, total_size);store_size(b2, total_waste);
540 store_size(b3, total_waste * 100 / total_used);
541 printf("Memory used: %6s # Loss : %6s MRatio: %6s%%\n", b1, b2, b3);
543 store_size(b1, total_objects);store_size(b2, total_partobj);
544 store_size(b3, total_partobj * 100 / total_objects);
545 printf("# Objects : %6s # PartObj: %6s ORatio: %6s%%\n", b1, b2, b3);
548 printf("Per Cache Average Min Max Total\n");
549 printf("---------------------------------------------------------\n");
551 store_size(b1, avg_objects);store_size(b2, min_objects);
552 store_size(b3, max_objects);store_size(b4, total_objects);
553 printf("#Objects %10s %10s %10s %10s\n",
556 store_size(b1, avg_slabs);store_size(b2, min_slabs);
557 store_size(b3, max_slabs);store_size(b4, total_slabs);
558 printf("#Slabs %10s %10s %10s %10s\n",
561 store_size(b1, avg_partial);store_size(b2, min_partial);
562 store_size(b3, max_partial);store_size(b4, total_partial);
563 printf("#PartSlab %10s %10s %10s %10s\n",
565 store_size(b1, avg_ppart);store_size(b2, min_ppart);
566 store_size(b3, max_ppart);
567 store_size(b4, total_partial * 100 / total_slabs);
568 printf("%%PartSlab %10s%% %10s%% %10s%% %10s%%\n",
571 store_size(b1, avg_partobj);store_size(b2, min_partobj);
572 store_size(b3, max_partobj);
573 store_size(b4, total_partobj);
574 printf("PartObjs %10s %10s %10s %10s\n",
577 store_size(b1, avg_ppartobj);store_size(b2, min_ppartobj);
578 store_size(b3, max_ppartobj);
579 store_size(b4, total_partobj * 100 / total_objects);
580 printf("%% PartObj %10s%% %10s%% %10s%% %10s%%\n",
583 store_size(b1, avg_size);store_size(b2, min_size);
584 store_size(b3, max_size);store_size(b4, total_size);
585 printf("Memory %10s %10s %10s %10s\n",
588 store_size(b1, avg_used);store_size(b2, min_used);
589 store_size(b3, max_used);store_size(b4, total_used);
590 printf("Used %10s %10s %10s %10s\n",
593 store_size(b1, avg_waste);store_size(b2, min_waste);
594 store_size(b3, max_waste);store_size(b4, total_waste);
595 printf("Loss %10s %10s %10s %10s\n",
599 printf("Per Object Average Min Max\n");
600 printf("---------------------------------------------\n");
602 store_size(b1, avg_memobj);store_size(b2, min_memobj);
603 store_size(b3, max_memobj);
604 printf("Memory %10s %10s %10s\n",
606 store_size(b1, avg_objsize);store_size(b2, min_objsize);
607 store_size(b3, max_objsize);
608 printf("User %10s %10s %10s\n",
611 store_size(b1, avg_objwaste);store_size(b2, min_objwaste);
612 store_size(b3, max_objwaste);
613 printf("Loss %10s %10s %10s\n",
617 void sort_slabs(void)
619 struct slabinfo *s1,*s2;
621 for (s1 = slabinfo; s1 < slabinfo + slabs; s1++) {
622 for (s2 = s1 + 1; s2 < slabinfo + slabs; s2++) {
626 result = slab_size(s1) < slab_size(s2);
628 result = strcasecmp(s1->name, s2->name);
636 memcpy(&t, s1, sizeof(struct slabinfo));
637 memcpy(s1, s2, sizeof(struct slabinfo));
638 memcpy(s2, &t, sizeof(struct slabinfo));
644 void sort_aliases(void)
646 struct aliasinfo *a1,*a2;
648 for (a1 = aliasinfo; a1 < aliasinfo + aliases; a1++) {
649 for (a2 = a1 + 1; a2 < aliasinfo + aliases; a2++) {
654 if (show_alias && !show_inverted) {
658 if (strcasecmp(n1, n2) > 0) {
661 memcpy(&t, a1, sizeof(struct aliasinfo));
662 memcpy(a1, a2, sizeof(struct aliasinfo));
663 memcpy(a2, &t, sizeof(struct aliasinfo));
669 void link_slabs(void)
674 for (a = aliasinfo; a < aliasinfo + aliases; a++) {
676 for(s = slabinfo; s < slabinfo + slabs; s++)
677 if (strcmp(a->ref, s->name) == 0) {
682 if (s == slabinfo + slabs)
683 fatal("Unresolved alias %s\n", a->ref);
695 for(a = aliasinfo; a < aliasinfo + aliases; a++) {
697 if (!show_single_ref && a->slab->refs == 1)
700 if (!show_inverted) {
702 if (strcmp(a->slab->name, active) == 0) {
703 printf(" %s", a->name);
707 printf("\n%-20s <- %s", a->slab->name, a->name);
708 active = a->slab->name;
711 printf("%-20s -> %s\n", a->name, a->slab->name);
718 void rename_slabs(void)
723 for (s = slabinfo; s < slabinfo + slabs; s++) {
727 if (s->refs > 1 && !show_first_alias)
730 a = find_one_alias(s);
736 int slab_mismatch(char *slab)
738 return regexec(&pattern, slab, 0, NULL, 0);
741 void read_slab_dir(void)
745 struct slabinfo *slab = slabinfo;
746 struct aliasinfo *alias = aliasinfo;
752 while ((de = readdir(dir))) {
753 if (de->d_name[0] == '.' ||
754 slab_mismatch(de->d_name))
756 switch (de->d_type) {
758 alias->name = strdup(de->d_name);
759 count = readlink(de->d_name, buffer, sizeof(buffer));
762 fatal("Cannot read symlink %s\n", de->d_name);
766 while (p > buffer && p[-1] != '/')
768 alias->ref = strdup(p);
772 if (chdir(de->d_name))
773 fatal("Unable to access slab %s\n", slab->name);
774 slab->name = strdup(de->d_name);
777 slab->aliases = get_obj("aliases");
778 slab->align = get_obj("align");
779 slab->cache_dma = get_obj("cache_dma");
780 slab->cpu_slabs = get_obj("cpu_slabs");
781 slab->destroy_by_rcu = get_obj("destroy_by_rcu");
782 slab->hwcache_align = get_obj("hwcache_align");
783 slab->object_size = get_obj("object_size");
784 slab->objects = get_obj("objects");
785 slab->objs_per_slab = get_obj("objs_per_slab");
786 slab->order = get_obj("order");
787 slab->partial = get_obj("partial");
788 slab->partial = get_obj_and_str("partial", &t);
789 decode_numa_list(slab->numa_partial, t);
790 slab->poison = get_obj("poison");
791 slab->reclaim_account = get_obj("reclaim_account");
792 slab->red_zone = get_obj("red_zone");
793 slab->sanity_checks = get_obj("sanity_checks");
794 slab->slab_size = get_obj("slab_size");
795 slab->slabs = get_obj_and_str("slabs", &t);
796 decode_numa_list(slab->numa, t);
797 slab->store_user = get_obj("store_user");
798 slab->trace = get_obj("trace");
800 if (slab->name[0] == ':')
805 fatal("Unknown file type %lx\n", de->d_type);
809 slabs = slab - slabinfo;
810 aliases = alias - aliasinfo;
811 if (slabs > MAX_SLABS)
812 fatal("Too many slabs\n");
813 if (aliases > MAX_ALIASES)
814 fatal("Too many aliases\n");
817 void output_slabs(void)
819 struct slabinfo *slab;
821 for (slab = slabinfo; slab < slabinfo + slabs; slab++) {
845 struct option opts[] = {
846 { "aliases", 0, NULL, 'a' },
847 { "slabs", 0, NULL, 'l' },
848 { "numa", 0, NULL, 'n' },
849 { "zero", 0, NULL, 'z' },
850 { "help", 0, NULL, 'h' },
851 { "validate", 0, NULL, 'v' },
852 { "first-alias", 0, NULL, 'f' },
853 { "shrink", 0, NULL, 's' },
854 { "track", 0, NULL, 't'},
855 { "inverted", 0, NULL, 'i'},
856 { "1ref", 0, NULL, '1'},
860 int main(int argc, char *argv[])
864 char *pattern_source;
866 page_size = getpagesize();
867 if (chdir("/sys/slab"))
868 fatal("This kernel does not have SLUB support.\n");
870 while ((c = getopt_long(argc, argv, "afhil1npstvzTS", opts, NULL)) != -1)
879 show_first_alias = 1;
913 fatal("%s: Invalid option '%c'\n", argv[0], optopt);
917 if (!show_slab && !show_alias && !show_track
918 && !validate && !shrink)
922 pattern_source = argv[optind];
924 pattern_source = ".*";
926 err = regcomp(&pattern, pattern_source, REG_ICASE|REG_NOSUB);
928 fatal("%s: Invalid pattern '%s' code %d\n",
929 argv[0], pattern_source, err);