2 * Slabinfo: Tool to get reports about slabs
4 * (C) 2007 sgi, Christoph Lameter
5 * (C) 2011 Linux Foundation, Christoph Lameter
9 * gcc -o slabinfo slabinfo.c
13 #include <sys/types.h>
24 #define MAX_ALIASES 500
25 #define MAX_NODES 1024
31 int aliases, align, cache_dma, cpu_slabs, destroy_by_rcu;
32 int hwcache_align, object_size, objs_per_slab;
33 int sanity_checks, slab_size, store_user, trace;
34 int order, poison, reclaim_account, red_zone;
35 unsigned long partial, objects, slabs, objects_partial, objects_total;
36 unsigned long alloc_fastpath, alloc_slowpath;
37 unsigned long free_fastpath, free_slowpath;
38 unsigned long free_frozen, free_add_partial, free_remove_partial;
39 unsigned long alloc_from_partial, alloc_slab, free_slab, alloc_refill;
40 unsigned long cpuslab_flush, deactivate_full, deactivate_empty;
41 unsigned long deactivate_to_head, deactivate_to_tail;
42 unsigned long deactivate_remote_frees, order_fallback;
43 unsigned long cmpxchg_double_cpu_fail, cmpxchg_double_fail;
44 unsigned long alloc_node_mismatch, deactivate_bypass;
45 unsigned long cpu_partial_alloc, cpu_partial_free;
47 int numa_partial[MAX_NODES];
48 } slabinfo[MAX_SLABS];
53 struct slabinfo *slab;
54 } aliasinfo[MAX_ALIASES];
59 int alias_targets = 0;
71 int show_first_alias = 0;
74 int show_inverted = 0;
75 int show_single_ref = 0;
81 int show_activity = 0;
82 int output_lines = -1;
96 static void fatal(const char *x, ...)
101 vfprintf(stderr, x, ap);
106 static void usage(void)
108 printf("slabinfo 4/15/2011. (c) 2007 sgi/(c) 2011 Linux Foundation.\n\n"
109 "slabinfo [-ahnpvtsz] [-d debugopts] [slab-regexp]\n"
110 "-a|--aliases Show aliases\n"
111 "-A|--activity Most active slabs first\n"
112 "-d<options>|--debug=<options> Set/Clear Debug options\n"
113 "-D|--display-active Switch line format to activity\n"
114 "-e|--empty Show empty slabs\n"
115 "-f|--first-alias Show first alias\n"
116 "-h|--help Show usage information\n"
117 "-i|--inverted Inverted list\n"
118 "-l|--slabs Show slabs\n"
119 "-n|--numa Show NUMA information\n"
120 "-o|--ops Show kmem_cache_ops\n"
121 "-s|--shrink Shrink slabs\n"
122 "-r|--report Detailed report on single slabs\n"
123 "-S|--Size Sort by size\n"
124 "-t|--tracking Show alloc/free information\n"
125 "-T|--Totals Show summary information\n"
126 "-v|--validate Validate slabs\n"
127 "-z|--zero Include empty slabs\n"
128 "-1|--1ref Single reference\n"
129 "-N|--lines=K Show the first K slabs\n"
130 "-L|--Loss Sort by loss\n"
131 "\nValid debug options (FZPUT may be combined)\n"
132 "a / A Switch on all debug options (=FZUP)\n"
133 "- Switch off all debug options\n"
134 "f / F Sanity Checks (SLAB_DEBUG_FREE)\n"
142 static unsigned long read_obj(const char *name)
144 FILE *f = fopen(name, "r");
149 if (!fgets(buffer, sizeof(buffer), f))
152 if (buffer[strlen(buffer)] == '\n')
153 buffer[strlen(buffer)] = 0;
155 return strlen(buffer);
160 * Get the contents of an attribute
162 static unsigned long get_obj(const char *name)
170 static unsigned long get_obj_and_str(const char *name, char **x)
172 unsigned long result = 0;
177 if (!read_obj(name)) {
181 result = strtoul(buffer, &p, 10);
189 static void set_obj(struct slabinfo *s, const char *name, int n)
194 snprintf(x, 100, "%s/%s", s->name, name);
197 fatal("Cannot write to %s\n", x);
199 fprintf(f, "%d\n", n);
203 static unsigned long read_slab_obj(struct slabinfo *s, const char *name)
209 snprintf(x, 100, "%s/%s", s->name, name);
215 l = fread(buffer, 1, sizeof(buffer), f);
224 * Put a size string together
226 static int store_size(char *buffer, unsigned long value)
228 unsigned long divisor = 1;
232 if (value > 1000000000UL) {
233 divisor = 100000000UL;
235 } else if (value > 1000000UL) {
238 } else if (value > 1000UL) {
244 n = sprintf(buffer, "%ld",value);
251 memmove(buffer + n - 2, buffer + n - 3, 4);
258 static void decode_numa_list(int *numa, char *t)
263 memset(numa, 0, MAX_NODES * sizeof(int));
270 node = strtoul(t, &t, 10);
273 nr = strtoul(t, &t, 10);
275 if (node > highest_node)
283 static void slab_validate(struct slabinfo *s)
285 if (strcmp(s->name, "*") == 0)
288 set_obj(s, "validate", 1);
291 static void slab_shrink(struct slabinfo *s)
293 if (strcmp(s->name, "*") == 0)
296 set_obj(s, "shrink", 1);
301 static void first_line(void)
304 printf("Name Objects Alloc Free %%Fast Fallb O CmpX UL\n");
306 printf("Name Objects Objsize %s "
307 "Slabs/Part/Cpu O/S O %%Fr %%Ef Flg\n",
308 sort_loss ? "Loss" : "Space");
312 * Find the shortest alias of a slab
314 static struct aliasinfo *find_one_alias(struct slabinfo *find)
317 struct aliasinfo *best = NULL;
319 for(a = aliasinfo;a < aliasinfo + aliases; a++) {
320 if (a->slab == find &&
321 (!best || strlen(best->name) < strlen(a->name))) {
323 if (strncmp(a->name,"kmall", 5) == 0)
330 static unsigned long slab_size(struct slabinfo *s)
332 return s->slabs * (page_size << s->order);
335 static unsigned long slab_activity(struct slabinfo *s)
337 return s->alloc_fastpath + s->free_fastpath +
338 s->alloc_slowpath + s->free_slowpath;
341 static unsigned long slab_waste(struct slabinfo *s)
343 return slab_size(s) - s->objects * s->object_size;
346 static void slab_numa(struct slabinfo *s, int mode)
350 if (strcmp(s->name, "*") == 0)
354 printf("\n%s: No NUMA information available.\n", s->name);
358 if (skip_zero && !s->slabs)
362 printf("\n%-21s:", mode ? "NUMA nodes" : "Slab");
363 for(node = 0; node <= highest_node; node++)
364 printf(" %4d", node);
365 printf("\n----------------------");
366 for(node = 0; node <= highest_node; node++)
370 printf("%-21s ", mode ? "All slabs" : s->name);
371 for(node = 0; node <= highest_node; node++) {
374 store_size(b, s->numa[node]);
379 printf("%-21s ", "Partial slabs");
380 for(node = 0; node <= highest_node; node++) {
383 store_size(b, s->numa_partial[node]);
391 static void show_tracking(struct slabinfo *s)
393 printf("\n%s: Kernel object allocation\n", s->name);
394 printf("-----------------------------------------------------------------------\n");
395 if (read_slab_obj(s, "alloc_calls"))
396 printf("%s", buffer);
400 printf("\n%s: Kernel object freeing\n", s->name);
401 printf("------------------------------------------------------------------------\n");
402 if (read_slab_obj(s, "free_calls"))
403 printf("%s", buffer);
409 static void ops(struct slabinfo *s)
411 if (strcmp(s->name, "*") == 0)
414 if (read_slab_obj(s, "ops")) {
415 printf("\n%s: kmem_cache operations\n", s->name);
416 printf("--------------------------------------------\n");
417 printf("%s", buffer);
419 printf("\n%s has no kmem_cache operations\n", s->name);
422 static const char *onoff(int x)
429 static void slab_stats(struct slabinfo *s)
431 unsigned long total_alloc;
432 unsigned long total_free;
438 total_alloc = s->alloc_fastpath + s->alloc_slowpath;
439 total_free = s->free_fastpath + s->free_slowpath;
445 printf("Slab Perf Counter Alloc Free %%Al %%Fr\n");
446 printf("--------------------------------------------------\n");
447 printf("Fastpath %8lu %8lu %3lu %3lu\n",
448 s->alloc_fastpath, s->free_fastpath,
449 s->alloc_fastpath * 100 / total_alloc,
450 total_free ? s->free_fastpath * 100 / total_free : 0);
451 printf("Slowpath %8lu %8lu %3lu %3lu\n",
452 total_alloc - s->alloc_fastpath, s->free_slowpath,
453 (total_alloc - s->alloc_fastpath) * 100 / total_alloc,
454 total_free ? s->free_slowpath * 100 / total_free : 0);
455 printf("Page Alloc %8lu %8lu %3lu %3lu\n",
456 s->alloc_slab, s->free_slab,
457 s->alloc_slab * 100 / total_alloc,
458 total_free ? s->free_slab * 100 / total_free : 0);
459 printf("Add partial %8lu %8lu %3lu %3lu\n",
460 s->deactivate_to_head + s->deactivate_to_tail,
462 (s->deactivate_to_head + s->deactivate_to_tail) * 100 / total_alloc,
463 total_free ? s->free_add_partial * 100 / total_free : 0);
464 printf("Remove partial %8lu %8lu %3lu %3lu\n",
465 s->alloc_from_partial, s->free_remove_partial,
466 s->alloc_from_partial * 100 / total_alloc,
467 total_free ? s->free_remove_partial * 100 / total_free : 0);
469 printf("Cpu partial list %8lu %8lu %3lu %3lu\n",
470 s->cpu_partial_alloc, s->cpu_partial_free,
471 s->cpu_partial_alloc * 100 / total_alloc,
472 total_free ? s->cpu_partial_free * 100 / total_free : 0);
474 printf("RemoteObj/SlabFrozen %8lu %8lu %3lu %3lu\n",
475 s->deactivate_remote_frees, s->free_frozen,
476 s->deactivate_remote_frees * 100 / total_alloc,
477 total_free ? s->free_frozen * 100 / total_free : 0);
479 printf("Total %8lu %8lu\n\n", total_alloc, total_free);
481 if (s->cpuslab_flush)
482 printf("Flushes %8lu\n", s->cpuslab_flush);
484 total = s->deactivate_full + s->deactivate_empty +
485 s->deactivate_to_head + s->deactivate_to_tail + s->deactivate_bypass;
488 printf("\nSlab Deactivation Ocurrences %%\n");
489 printf("-------------------------------------------------\n");
490 printf("Slab full %7lu %3lu%%\n",
491 s->deactivate_full, (s->deactivate_full * 100) / total);
492 printf("Slab empty %7lu %3lu%%\n",
493 s->deactivate_empty, (s->deactivate_empty * 100) / total);
494 printf("Moved to head of partial list %7lu %3lu%%\n",
495 s->deactivate_to_head, (s->deactivate_to_head * 100) / total);
496 printf("Moved to tail of partial list %7lu %3lu%%\n",
497 s->deactivate_to_tail, (s->deactivate_to_tail * 100) / total);
498 printf("Deactivation bypass %7lu %3lu%%\n",
499 s->deactivate_bypass, (s->deactivate_bypass * 100) / total);
500 printf("Refilled from foreign frees %7lu %3lu%%\n",
501 s->alloc_refill, (s->alloc_refill * 100) / total);
502 printf("Node mismatch %7lu %3lu%%\n",
503 s->alloc_node_mismatch, (s->alloc_node_mismatch * 100) / total);
506 if (s->cmpxchg_double_fail || s->cmpxchg_double_cpu_fail)
507 printf("\nCmpxchg_double Looping\n------------------------\n");
508 printf("Locked Cmpxchg Double redos %lu\nUnlocked Cmpxchg Double redos %lu\n",
509 s->cmpxchg_double_fail, s->cmpxchg_double_cpu_fail);
512 static void report(struct slabinfo *s)
514 if (strcmp(s->name, "*") == 0)
517 printf("\nSlabcache: %-20s Aliases: %2d Order : %2d Objects: %lu\n",
518 s->name, s->aliases, s->order, s->objects);
519 if (s->hwcache_align)
520 printf("** Hardware cacheline aligned\n");
522 printf("** Memory is allocated in a special DMA zone\n");
523 if (s->destroy_by_rcu)
524 printf("** Slabs are destroyed via RCU\n");
525 if (s->reclaim_account)
526 printf("** Reclaim accounting active\n");
528 printf("\nSizes (bytes) Slabs Debug Memory\n");
529 printf("------------------------------------------------------------------------\n");
530 printf("Object : %7d Total : %7ld Sanity Checks : %s Total: %7ld\n",
531 s->object_size, s->slabs, onoff(s->sanity_checks),
532 s->slabs * (page_size << s->order));
533 printf("SlabObj: %7d Full : %7ld Redzoning : %s Used : %7ld\n",
534 s->slab_size, s->slabs - s->partial - s->cpu_slabs,
535 onoff(s->red_zone), s->objects * s->object_size);
536 printf("SlabSiz: %7d Partial: %7ld Poisoning : %s Loss : %7ld\n",
537 page_size << s->order, s->partial, onoff(s->poison),
538 s->slabs * (page_size << s->order) - s->objects * s->object_size);
539 printf("Loss : %7d CpuSlab: %7d Tracking : %s Lalig: %7ld\n",
540 s->slab_size - s->object_size, s->cpu_slabs, onoff(s->store_user),
541 (s->slab_size - s->object_size) * s->objects);
542 printf("Align : %7d Objects: %7d Tracing : %s Lpadd: %7ld\n",
543 s->align, s->objs_per_slab, onoff(s->trace),
544 ((page_size << s->order) - s->objs_per_slab * s->slab_size) *
553 static void slabcache(struct slabinfo *s)
560 if (strcmp(s->name, "*") == 0)
563 if (actual_slabs == 1) {
568 if (skip_zero && !show_empty && !s->slabs)
571 if (show_empty && s->slabs)
575 store_size(size_str, slab_size(s));
577 store_size(size_str, slab_waste(s));
578 snprintf(dist_str, 40, "%lu/%lu/%d", s->slabs - s->cpu_slabs,
579 s->partial, s->cpu_slabs);
588 if (s->hwcache_align)
592 if (s->reclaim_account)
596 if (s->sanity_checks)
605 unsigned long total_alloc;
606 unsigned long total_free;
608 total_alloc = s->alloc_fastpath + s->alloc_slowpath;
609 total_free = s->free_fastpath + s->free_slowpath;
611 printf("%-21s %8ld %10ld %10ld %3ld %3ld %5ld %1d %4ld %4ld\n",
613 total_alloc, total_free,
614 total_alloc ? (s->alloc_fastpath * 100 / total_alloc) : 0,
615 total_free ? (s->free_fastpath * 100 / total_free) : 0,
616 s->order_fallback, s->order, s->cmpxchg_double_fail,
617 s->cmpxchg_double_cpu_fail);
620 printf("%-21s %8ld %7d %8s %14s %4d %1d %3ld %3ld %s\n",
621 s->name, s->objects, s->object_size, size_str, dist_str,
622 s->objs_per_slab, s->order,
623 s->slabs ? (s->partial * 100) / s->slabs : 100,
624 s->slabs ? (s->objects * s->object_size * 100) /
625 (s->slabs * (page_size << s->order)) : 100,
630 * Analyze debug options. Return false if something is amiss.
632 static int debug_opt_scan(char *opt)
634 if (!opt || !opt[0] || strcmp(opt, "-") == 0)
637 if (strcasecmp(opt, "a") == 0) {
681 static int slab_empty(struct slabinfo *s)
687 * We may still have slabs even if there are no objects. Shrinking will
691 set_obj(s, "shrink", 1);
696 static void slab_debug(struct slabinfo *s)
698 if (strcmp(s->name, "*") == 0)
701 if (sanity && !s->sanity_checks) {
702 set_obj(s, "sanity", 1);
704 if (!sanity && s->sanity_checks) {
706 set_obj(s, "sanity", 0);
708 fprintf(stderr, "%s not empty cannot disable sanity checks\n", s->name);
710 if (redzone && !s->red_zone) {
712 set_obj(s, "red_zone", 1);
714 fprintf(stderr, "%s not empty cannot enable redzoning\n", s->name);
716 if (!redzone && s->red_zone) {
718 set_obj(s, "red_zone", 0);
720 fprintf(stderr, "%s not empty cannot disable redzoning\n", s->name);
722 if (poison && !s->poison) {
724 set_obj(s, "poison", 1);
726 fprintf(stderr, "%s not empty cannot enable poisoning\n", s->name);
728 if (!poison && s->poison) {
730 set_obj(s, "poison", 0);
732 fprintf(stderr, "%s not empty cannot disable poisoning\n", s->name);
734 if (tracking && !s->store_user) {
736 set_obj(s, "store_user", 1);
738 fprintf(stderr, "%s not empty cannot enable tracking\n", s->name);
740 if (!tracking && s->store_user) {
742 set_obj(s, "store_user", 0);
744 fprintf(stderr, "%s not empty cannot disable tracking\n", s->name);
746 if (tracing && !s->trace) {
748 set_obj(s, "trace", 1);
750 fprintf(stderr, "%s can only enable trace for one slab at a time\n", s->name);
752 if (!tracing && s->trace)
753 set_obj(s, "trace", 1);
756 static void totals(void)
761 char b1[20], b2[20], b3[20], b4[20];
762 unsigned long long max = 1ULL << 63;
765 unsigned long long min_objsize = max, max_objsize = 0, avg_objsize;
767 /* Number of partial slabs in a slabcache */
768 unsigned long long min_partial = max, max_partial = 0,
769 avg_partial, total_partial = 0;
771 /* Number of slabs in a slab cache */
772 unsigned long long min_slabs = max, max_slabs = 0,
773 avg_slabs, total_slabs = 0;
775 /* Size of the whole slab */
776 unsigned long long min_size = max, max_size = 0,
777 avg_size, total_size = 0;
779 /* Bytes used for object storage in a slab */
780 unsigned long long min_used = max, max_used = 0,
781 avg_used, total_used = 0;
783 /* Waste: Bytes used for alignment and padding */
784 unsigned long long min_waste = max, max_waste = 0,
785 avg_waste, total_waste = 0;
786 /* Number of objects in a slab */
787 unsigned long long min_objects = max, max_objects = 0,
788 avg_objects, total_objects = 0;
789 /* Waste per object */
790 unsigned long long min_objwaste = max,
791 max_objwaste = 0, avg_objwaste,
794 /* Memory per object */
795 unsigned long long min_memobj = max,
796 max_memobj = 0, avg_memobj,
799 /* Percentage of partial slabs per slab */
800 unsigned long min_ppart = 100, max_ppart = 0,
801 avg_ppart, total_ppart = 0;
803 /* Number of objects in partial slabs */
804 unsigned long min_partobj = max, max_partobj = 0,
805 avg_partobj, total_partobj = 0;
807 /* Percentage of partial objects of all objects in a slab */
808 unsigned long min_ppartobj = 100, max_ppartobj = 0,
809 avg_ppartobj, total_ppartobj = 0;
812 for (s = slabinfo; s < slabinfo + slabs; s++) {
813 unsigned long long size;
815 unsigned long long wasted;
816 unsigned long long objwaste;
817 unsigned long percentage_partial_slabs;
818 unsigned long percentage_partial_objs;
820 if (!s->slabs || !s->objects)
826 used = s->objects * s->object_size;
827 wasted = size - used;
828 objwaste = s->slab_size - s->object_size;
830 percentage_partial_slabs = s->partial * 100 / s->slabs;
831 if (percentage_partial_slabs > 100)
832 percentage_partial_slabs = 100;
834 percentage_partial_objs = s->objects_partial * 100
837 if (percentage_partial_objs > 100)
838 percentage_partial_objs = 100;
840 if (s->object_size < min_objsize)
841 min_objsize = s->object_size;
842 if (s->partial < min_partial)
843 min_partial = s->partial;
844 if (s->slabs < min_slabs)
845 min_slabs = s->slabs;
848 if (wasted < min_waste)
850 if (objwaste < min_objwaste)
851 min_objwaste = objwaste;
852 if (s->objects < min_objects)
853 min_objects = s->objects;
856 if (s->objects_partial < min_partobj)
857 min_partobj = s->objects_partial;
858 if (percentage_partial_slabs < min_ppart)
859 min_ppart = percentage_partial_slabs;
860 if (percentage_partial_objs < min_ppartobj)
861 min_ppartobj = percentage_partial_objs;
862 if (s->slab_size < min_memobj)
863 min_memobj = s->slab_size;
865 if (s->object_size > max_objsize)
866 max_objsize = s->object_size;
867 if (s->partial > max_partial)
868 max_partial = s->partial;
869 if (s->slabs > max_slabs)
870 max_slabs = s->slabs;
873 if (wasted > max_waste)
875 if (objwaste > max_objwaste)
876 max_objwaste = objwaste;
877 if (s->objects > max_objects)
878 max_objects = s->objects;
881 if (s->objects_partial > max_partobj)
882 max_partobj = s->objects_partial;
883 if (percentage_partial_slabs > max_ppart)
884 max_ppart = percentage_partial_slabs;
885 if (percentage_partial_objs > max_ppartobj)
886 max_ppartobj = percentage_partial_objs;
887 if (s->slab_size > max_memobj)
888 max_memobj = s->slab_size;
890 total_partial += s->partial;
891 total_slabs += s->slabs;
893 total_waste += wasted;
895 total_objects += s->objects;
897 total_partobj += s->objects_partial;
898 total_ppart += percentage_partial_slabs;
899 total_ppartobj += percentage_partial_objs;
901 total_objwaste += s->objects * objwaste;
902 total_objsize += s->objects * s->slab_size;
905 if (!total_objects) {
906 printf("No objects\n");
910 printf("No slabs\n");
914 /* Per slab averages */
915 avg_partial = total_partial / used_slabs;
916 avg_slabs = total_slabs / used_slabs;
917 avg_size = total_size / used_slabs;
918 avg_waste = total_waste / used_slabs;
920 avg_objects = total_objects / used_slabs;
921 avg_used = total_used / used_slabs;
922 avg_partobj = total_partobj / used_slabs;
923 avg_ppart = total_ppart / used_slabs;
924 avg_ppartobj = total_ppartobj / used_slabs;
926 /* Per object object sizes */
927 avg_objsize = total_used / total_objects;
928 avg_objwaste = total_objwaste / total_objects;
929 avg_partobj = total_partobj * 100 / total_objects;
930 avg_memobj = total_objsize / total_objects;
932 printf("Slabcache Totals\n");
933 printf("----------------\n");
934 printf("Slabcaches : %3d Aliases : %3d->%-3d Active: %3d\n",
935 slabs, aliases, alias_targets, used_slabs);
937 store_size(b1, total_size);store_size(b2, total_waste);
938 store_size(b3, total_waste * 100 / total_used);
939 printf("Memory used: %6s # Loss : %6s MRatio:%6s%%\n", b1, b2, b3);
941 store_size(b1, total_objects);store_size(b2, total_partobj);
942 store_size(b3, total_partobj * 100 / total_objects);
943 printf("# Objects : %6s # PartObj: %6s ORatio:%6s%%\n", b1, b2, b3);
946 printf("Per Cache Average Min Max Total\n");
947 printf("---------------------------------------------------------\n");
949 store_size(b1, avg_objects);store_size(b2, min_objects);
950 store_size(b3, max_objects);store_size(b4, total_objects);
951 printf("#Objects %10s %10s %10s %10s\n",
954 store_size(b1, avg_slabs);store_size(b2, min_slabs);
955 store_size(b3, max_slabs);store_size(b4, total_slabs);
956 printf("#Slabs %10s %10s %10s %10s\n",
959 store_size(b1, avg_partial);store_size(b2, min_partial);
960 store_size(b3, max_partial);store_size(b4, total_partial);
961 printf("#PartSlab %10s %10s %10s %10s\n",
963 store_size(b1, avg_ppart);store_size(b2, min_ppart);
964 store_size(b3, max_ppart);
965 store_size(b4, total_partial * 100 / total_slabs);
966 printf("%%PartSlab%10s%% %10s%% %10s%% %10s%%\n",
969 store_size(b1, avg_partobj);store_size(b2, min_partobj);
970 store_size(b3, max_partobj);
971 store_size(b4, total_partobj);
972 printf("PartObjs %10s %10s %10s %10s\n",
975 store_size(b1, avg_ppartobj);store_size(b2, min_ppartobj);
976 store_size(b3, max_ppartobj);
977 store_size(b4, total_partobj * 100 / total_objects);
978 printf("%% PartObj%10s%% %10s%% %10s%% %10s%%\n",
981 store_size(b1, avg_size);store_size(b2, min_size);
982 store_size(b3, max_size);store_size(b4, total_size);
983 printf("Memory %10s %10s %10s %10s\n",
986 store_size(b1, avg_used);store_size(b2, min_used);
987 store_size(b3, max_used);store_size(b4, total_used);
988 printf("Used %10s %10s %10s %10s\n",
991 store_size(b1, avg_waste);store_size(b2, min_waste);
992 store_size(b3, max_waste);store_size(b4, total_waste);
993 printf("Loss %10s %10s %10s %10s\n",
997 printf("Per Object Average Min Max\n");
998 printf("---------------------------------------------\n");
1000 store_size(b1, avg_memobj);store_size(b2, min_memobj);
1001 store_size(b3, max_memobj);
1002 printf("Memory %10s %10s %10s\n",
1004 store_size(b1, avg_objsize);store_size(b2, min_objsize);
1005 store_size(b3, max_objsize);
1006 printf("User %10s %10s %10s\n",
1009 store_size(b1, avg_objwaste);store_size(b2, min_objwaste);
1010 store_size(b3, max_objwaste);
1011 printf("Loss %10s %10s %10s\n",
1015 static void sort_slabs(void)
1017 struct slabinfo *s1,*s2;
1019 for (s1 = slabinfo; s1 < slabinfo + slabs; s1++) {
1020 for (s2 = s1 + 1; s2 < slabinfo + slabs; s2++) {
1024 result = slab_size(s1) < slab_size(s2);
1025 else if (sort_active)
1026 result = slab_activity(s1) < slab_activity(s2);
1028 result = slab_waste(s1) < slab_waste(s2);
1030 result = strcasecmp(s1->name, s2->name);
1038 memcpy(&t, s1, sizeof(struct slabinfo));
1039 memcpy(s1, s2, sizeof(struct slabinfo));
1040 memcpy(s2, &t, sizeof(struct slabinfo));
1046 static void sort_aliases(void)
1048 struct aliasinfo *a1,*a2;
1050 for (a1 = aliasinfo; a1 < aliasinfo + aliases; a1++) {
1051 for (a2 = a1 + 1; a2 < aliasinfo + aliases; a2++) {
1056 if (show_alias && !show_inverted) {
1060 if (strcasecmp(n1, n2) > 0) {
1063 memcpy(&t, a1, sizeof(struct aliasinfo));
1064 memcpy(a1, a2, sizeof(struct aliasinfo));
1065 memcpy(a2, &t, sizeof(struct aliasinfo));
1071 static void link_slabs(void)
1073 struct aliasinfo *a;
1076 for (a = aliasinfo; a < aliasinfo + aliases; a++) {
1078 for (s = slabinfo; s < slabinfo + slabs; s++)
1079 if (strcmp(a->ref, s->name) == 0) {
1084 if (s == slabinfo + slabs)
1085 fatal("Unresolved alias %s\n", a->ref);
1089 static void alias(void)
1091 struct aliasinfo *a;
1092 char *active = NULL;
1097 for(a = aliasinfo; a < aliasinfo + aliases; a++) {
1099 if (!show_single_ref && a->slab->refs == 1)
1102 if (!show_inverted) {
1104 if (strcmp(a->slab->name, active) == 0) {
1105 printf(" %s", a->name);
1109 printf("\n%-12s <- %s", a->slab->name, a->name);
1110 active = a->slab->name;
1113 printf("%-20s -> %s\n", a->name, a->slab->name);
1120 static void rename_slabs(void)
1123 struct aliasinfo *a;
1125 for (s = slabinfo; s < slabinfo + slabs; s++) {
1126 if (*s->name != ':')
1129 if (s->refs > 1 && !show_first_alias)
1132 a = find_one_alias(s);
1143 static int slab_mismatch(char *slab)
1145 return regexec(&pattern, slab, 0, NULL, 0);
1148 static void read_slab_dir(void)
1152 struct slabinfo *slab = slabinfo;
1153 struct aliasinfo *alias = aliasinfo;
1158 if (chdir("/sys/kernel/slab") && chdir("/sys/slab"))
1159 fatal("SYSFS support for SLUB not active\n");
1162 while ((de = readdir(dir))) {
1163 if (de->d_name[0] == '.' ||
1164 (de->d_name[0] != ':' && slab_mismatch(de->d_name)))
1166 switch (de->d_type) {
1168 alias->name = strdup(de->d_name);
1169 count = readlink(de->d_name, buffer, sizeof(buffer)-1);
1172 fatal("Cannot read symlink %s\n", de->d_name);
1176 while (p > buffer && p[-1] != '/')
1178 alias->ref = strdup(p);
1182 if (chdir(de->d_name))
1183 fatal("Unable to access slab %s\n", slab->name);
1184 slab->name = strdup(de->d_name);
1187 slab->aliases = get_obj("aliases");
1188 slab->align = get_obj("align");
1189 slab->cache_dma = get_obj("cache_dma");
1190 slab->cpu_slabs = get_obj("cpu_slabs");
1191 slab->destroy_by_rcu = get_obj("destroy_by_rcu");
1192 slab->hwcache_align = get_obj("hwcache_align");
1193 slab->object_size = get_obj("object_size");
1194 slab->objects = get_obj("objects");
1195 slab->objects_partial = get_obj("objects_partial");
1196 slab->objects_total = get_obj("objects_total");
1197 slab->objs_per_slab = get_obj("objs_per_slab");
1198 slab->order = get_obj("order");
1199 slab->partial = get_obj("partial");
1200 slab->partial = get_obj_and_str("partial", &t);
1201 decode_numa_list(slab->numa_partial, t);
1203 slab->poison = get_obj("poison");
1204 slab->reclaim_account = get_obj("reclaim_account");
1205 slab->red_zone = get_obj("red_zone");
1206 slab->sanity_checks = get_obj("sanity_checks");
1207 slab->slab_size = get_obj("slab_size");
1208 slab->slabs = get_obj_and_str("slabs", &t);
1209 decode_numa_list(slab->numa, t);
1211 slab->store_user = get_obj("store_user");
1212 slab->trace = get_obj("trace");
1213 slab->alloc_fastpath = get_obj("alloc_fastpath");
1214 slab->alloc_slowpath = get_obj("alloc_slowpath");
1215 slab->free_fastpath = get_obj("free_fastpath");
1216 slab->free_slowpath = get_obj("free_slowpath");
1217 slab->free_frozen= get_obj("free_frozen");
1218 slab->free_add_partial = get_obj("free_add_partial");
1219 slab->free_remove_partial = get_obj("free_remove_partial");
1220 slab->alloc_from_partial = get_obj("alloc_from_partial");
1221 slab->alloc_slab = get_obj("alloc_slab");
1222 slab->alloc_refill = get_obj("alloc_refill");
1223 slab->free_slab = get_obj("free_slab");
1224 slab->cpuslab_flush = get_obj("cpuslab_flush");
1225 slab->deactivate_full = get_obj("deactivate_full");
1226 slab->deactivate_empty = get_obj("deactivate_empty");
1227 slab->deactivate_to_head = get_obj("deactivate_to_head");
1228 slab->deactivate_to_tail = get_obj("deactivate_to_tail");
1229 slab->deactivate_remote_frees = get_obj("deactivate_remote_frees");
1230 slab->order_fallback = get_obj("order_fallback");
1231 slab->cmpxchg_double_cpu_fail = get_obj("cmpxchg_double_cpu_fail");
1232 slab->cmpxchg_double_fail = get_obj("cmpxchg_double_fail");
1233 slab->cpu_partial_alloc = get_obj("cpu_partial_alloc");
1234 slab->cpu_partial_free = get_obj("cpu_partial_free");
1235 slab->alloc_node_mismatch = get_obj("alloc_node_mismatch");
1236 slab->deactivate_bypass = get_obj("deactivate_bypass");
1238 if (slab->name[0] == ':')
1243 fatal("Unknown file type %lx\n", de->d_type);
1247 slabs = slab - slabinfo;
1248 actual_slabs = slabs;
1249 aliases = alias - aliasinfo;
1250 if (slabs > MAX_SLABS)
1251 fatal("Too many slabs\n");
1252 if (aliases > MAX_ALIASES)
1253 fatal("Too many aliases\n");
1256 static void output_slabs(void)
1258 struct slabinfo *slab;
1260 for (slab = slabinfo; (slab < slabinfo + slabs) &&
1261 output_lines != 0; slab++) {
1266 if (output_lines != -1)
1271 else if (show_track)
1272 show_tracking(slab);
1274 slab_validate(slab);
1283 else if (show_report)
1288 struct option opts[] = {
1289 { "aliases", no_argument, NULL, 'a' },
1290 { "activity", no_argument, NULL, 'A' },
1291 { "debug", optional_argument, NULL, 'd' },
1292 { "display-activity", no_argument, NULL, 'D' },
1293 { "empty", no_argument, NULL, 'e' },
1294 { "first-alias", no_argument, NULL, 'f' },
1295 { "help", no_argument, NULL, 'h' },
1296 { "inverted", no_argument, NULL, 'i'},
1297 { "numa", no_argument, NULL, 'n' },
1298 { "ops", no_argument, NULL, 'o' },
1299 { "report", no_argument, NULL, 'r' },
1300 { "shrink", no_argument, NULL, 's' },
1301 { "slabs", no_argument, NULL, 'l' },
1302 { "track", no_argument, NULL, 't'},
1303 { "validate", no_argument, NULL, 'v' },
1304 { "zero", no_argument, NULL, 'z' },
1305 { "1ref", no_argument, NULL, '1'},
1306 { "lines", required_argument, NULL, 'N'},
1307 { "Loss", no_argument, NULL, 'L'},
1308 { NULL, 0, NULL, 0 }
1311 int main(int argc, char *argv[])
1315 char *pattern_source;
1317 page_size = getpagesize();
1319 while ((c = getopt_long(argc, argv, "aAd::Defhil1noprstvzTSN:L",
1323 show_single_ref = 1;
1333 if (!debug_opt_scan(optarg))
1334 fatal("Invalid debug option '%s'\n", optarg);
1343 show_first_alias = 1;
1383 output_lines = atoi(optarg);
1384 if (output_lines < 1)
1392 fatal("%s: Invalid option '%c'\n", argv[0], optopt);
1396 if (!show_slab && !show_alias && !show_track && !show_report
1397 && !validate && !shrink && !set_debug && !show_ops)
1401 pattern_source = argv[optind];
1403 pattern_source = ".*";
1405 err = regcomp(&pattern, pattern_source, REG_ICASE|REG_NOSUB);
1407 fatal("%s: Invalid pattern '%s' code %d\n",
1408 argv[0], pattern_source, err);