1 // SPDX-License-Identifier: GPL-2.0
3 * Slabinfo: Tool to get reports about slabs
5 * (C) 2007 sgi, Christoph Lameter
6 * (C) 2011 Linux Foundation, Christoph Lameter
10 * gcc -o slabinfo slabinfo.c
14 #include <sys/types.h>
25 #define MAX_ALIASES 500
26 #define MAX_NODES 1024
32 int aliases, align, cache_dma, cpu_slabs, destroy_by_rcu;
33 unsigned int hwcache_align, object_size, objs_per_slab;
34 unsigned int sanity_checks, slab_size, store_user, trace;
35 int order, poison, reclaim_account, red_zone;
36 unsigned long partial, objects, slabs, objects_partial, objects_total;
37 unsigned long alloc_fastpath, alloc_slowpath;
38 unsigned long free_fastpath, free_slowpath;
39 unsigned long free_frozen, free_add_partial, free_remove_partial;
40 unsigned long alloc_from_partial, alloc_slab, free_slab, alloc_refill;
41 unsigned long cpuslab_flush, deactivate_full, deactivate_empty;
42 unsigned long deactivate_to_head, deactivate_to_tail;
43 unsigned long deactivate_remote_frees, order_fallback;
44 unsigned long cmpxchg_double_cpu_fail, cmpxchg_double_fail;
45 unsigned long alloc_node_mismatch, deactivate_bypass;
46 unsigned long cpu_partial_alloc, cpu_partial_free;
48 int numa_partial[MAX_NODES];
49 } slabinfo[MAX_SLABS];
54 struct slabinfo *slab;
55 } aliasinfo[MAX_ALIASES];
84 int output_lines = -1;
101 static void fatal(const char *x, ...)
106 vfprintf(stderr, x, ap);
111 static void usage(void)
113 printf("slabinfo 4/15/2011. (c) 2007 sgi/(c) 2011 Linux Foundation.\n\n"
114 "slabinfo [-aABDefhilLnoPrsStTUvXz1] [N=K] [-dafzput] [slab-regexp]\n"
115 "-a|--aliases Show aliases\n"
116 "-A|--activity Most active slabs first\n"
117 "-B|--Bytes Show size in bytes\n"
118 "-D|--display-active Switch line format to activity\n"
119 "-e|--empty Show empty slabs\n"
120 "-f|--first-alias Show first alias\n"
121 "-h|--help Show usage information\n"
122 "-i|--inverted Inverted list\n"
123 "-l|--slabs Show slabs\n"
124 "-L|--Loss Sort by loss\n"
125 "-n|--numa Show NUMA information\n"
126 "-N|--lines=K Show the first K slabs\n"
127 "-o|--ops Show kmem_cache_ops\n"
128 "-P|--partial Sort by number of partial slabs\n"
129 "-r|--report Detailed report on single slabs\n"
130 "-s|--shrink Shrink slabs\n"
131 "-S|--Size Sort by size\n"
132 "-t|--tracking Show alloc/free information\n"
133 "-T|--Totals Show summary information\n"
134 "-U|--Unreclaim Show unreclaimable slabs only\n"
135 "-v|--validate Validate slabs\n"
136 "-X|--Xtotals Show extended summary information\n"
137 "-z|--zero Include empty slabs\n"
138 "-1|--1ref Single reference\n"
141 "-d | --debug Switch off all debug options\n"
142 "-da | --debug=a Switch on all debug options (--debug=FZPU)\n"
145 "-d[afzput] | --debug=[afzput]\n"
146 " f | F Sanity Checks (SLAB_CONSISTENCY_CHECKS)\n"
152 "\nSorting options (--Loss, --Size, --Partial) are mutually exclusive\n"
156 static unsigned long read_obj(const char *name)
158 FILE *f = fopen(name, "r");
163 fatal("%s, Try using superuser\n", strerror(errno));
165 if (!fgets(buffer, sizeof(buffer), f))
168 if (buffer[strlen(buffer)] == '\n')
169 buffer[strlen(buffer)] = 0;
171 return strlen(buffer);
176 * Get the contents of an attribute
178 static unsigned long get_obj(const char *name)
186 static unsigned long get_obj_and_str(const char *name, char **x)
188 unsigned long result = 0;
193 if (!read_obj(name)) {
197 result = strtoul(buffer, &p, 10);
205 static void set_obj(struct slabinfo *s, const char *name, int n)
210 snprintf(x, 100, "%s/%s", s->name, name);
213 fatal("Cannot write to %s\n", x);
215 fprintf(f, "%d\n", n);
219 static unsigned long read_slab_obj(struct slabinfo *s, const char *name)
225 snprintf(x, 100, "%s/%s", s->name, name);
231 l = fread(buffer, 1, sizeof(buffer), f);
238 static unsigned long read_debug_slab_obj(struct slabinfo *s, const char *name)
244 snprintf(x, 128, "/sys/kernel/debug/slab/%s/%s", s->name, name);
250 l = fread(buffer, 1, sizeof(buffer), f);
258 * Put a size string together
260 static int store_size(char *buffer, unsigned long value)
262 unsigned long divisor = 1;
267 if (value > 1000000000UL) {
268 divisor = 100000000UL;
270 } else if (value > 1000000UL) {
273 } else if (value > 1000UL) {
280 n = sprintf(buffer, "%ld",value);
287 memmove(buffer + n - 2, buffer + n - 3, 4);
294 static void decode_numa_list(int *numa, char *t)
299 memset(numa, 0, MAX_NODES * sizeof(int));
306 node = strtoul(t, &t, 10);
309 nr = strtoul(t, &t, 10);
311 if (node > highest_node)
319 static void slab_validate(struct slabinfo *s)
321 if (strcmp(s->name, "*") == 0)
324 set_obj(s, "validate", 1);
327 static void slab_shrink(struct slabinfo *s)
329 if (strcmp(s->name, "*") == 0)
332 set_obj(s, "shrink", 1);
337 static void first_line(void)
340 printf("Name Objects Alloc Free"
341 " %%Fast Fallb O CmpX UL\n");
343 printf("Name Objects Objsize %s "
344 "Slabs/Part/Cpu O/S O %%Fr %%Ef Flg\n",
345 sort_loss ? " Loss" : "Space");
349 * Find the shortest alias of a slab
351 static struct aliasinfo *find_one_alias(struct slabinfo *find)
354 struct aliasinfo *best = NULL;
356 for(a = aliasinfo;a < aliasinfo + aliases; a++) {
357 if (a->slab == find &&
358 (!best || strlen(best->name) < strlen(a->name))) {
360 if (strncmp(a->name,"kmall", 5) == 0)
367 static unsigned long slab_size(struct slabinfo *s)
369 return s->slabs * (page_size << s->order);
372 static unsigned long slab_activity(struct slabinfo *s)
374 return s->alloc_fastpath + s->free_fastpath +
375 s->alloc_slowpath + s->free_slowpath;
378 static unsigned long slab_waste(struct slabinfo *s)
380 return slab_size(s) - s->objects * s->object_size;
383 static void slab_numa(struct slabinfo *s, int mode)
387 if (strcmp(s->name, "*") == 0)
391 printf("\n%s: No NUMA information available.\n", s->name);
395 if (skip_zero && !s->slabs)
399 printf("\n%-21s:", mode ? "NUMA nodes" : "Slab");
400 for(node = 0; node <= highest_node; node++)
401 printf(" %4d", node);
402 printf("\n----------------------");
403 for(node = 0; node <= highest_node; node++)
407 printf("%-21s ", mode ? "All slabs" : s->name);
408 for(node = 0; node <= highest_node; node++) {
411 store_size(b, s->numa[node]);
416 printf("%-21s ", "Partial slabs");
417 for(node = 0; node <= highest_node; node++) {
420 store_size(b, s->numa_partial[node]);
428 static void show_tracking(struct slabinfo *s)
430 printf("\n%s: Kernel object allocation\n", s->name);
431 printf("-----------------------------------------------------------------------\n");
432 if (read_debug_slab_obj(s, "alloc_traces"))
433 printf("%s", buffer);
434 else if (read_slab_obj(s, "alloc_calls"))
435 printf("%s", buffer);
439 printf("\n%s: Kernel object freeing\n", s->name);
440 printf("------------------------------------------------------------------------\n");
441 if (read_debug_slab_obj(s, "free_traces"))
442 printf("%s", buffer);
443 else if (read_slab_obj(s, "free_calls"))
444 printf("%s", buffer);
450 static void ops(struct slabinfo *s)
452 if (strcmp(s->name, "*") == 0)
455 if (read_slab_obj(s, "ops")) {
456 printf("\n%s: kmem_cache operations\n", s->name);
457 printf("--------------------------------------------\n");
458 printf("%s", buffer);
460 printf("\n%s has no kmem_cache operations\n", s->name);
463 static const char *onoff(int x)
470 static void slab_stats(struct slabinfo *s)
472 unsigned long total_alloc;
473 unsigned long total_free;
479 total_alloc = s->alloc_fastpath + s->alloc_slowpath;
480 total_free = s->free_fastpath + s->free_slowpath;
486 printf("Slab Perf Counter Alloc Free %%Al %%Fr\n");
487 printf("--------------------------------------------------\n");
488 printf("Fastpath %8lu %8lu %3lu %3lu\n",
489 s->alloc_fastpath, s->free_fastpath,
490 s->alloc_fastpath * 100 / total_alloc,
491 total_free ? s->free_fastpath * 100 / total_free : 0);
492 printf("Slowpath %8lu %8lu %3lu %3lu\n",
493 total_alloc - s->alloc_fastpath, s->free_slowpath,
494 (total_alloc - s->alloc_fastpath) * 100 / total_alloc,
495 total_free ? s->free_slowpath * 100 / total_free : 0);
496 printf("Page Alloc %8lu %8lu %3lu %3lu\n",
497 s->alloc_slab, s->free_slab,
498 s->alloc_slab * 100 / total_alloc,
499 total_free ? s->free_slab * 100 / total_free : 0);
500 printf("Add partial %8lu %8lu %3lu %3lu\n",
501 s->deactivate_to_head + s->deactivate_to_tail,
503 (s->deactivate_to_head + s->deactivate_to_tail) * 100 / total_alloc,
504 total_free ? s->free_add_partial * 100 / total_free : 0);
505 printf("Remove partial %8lu %8lu %3lu %3lu\n",
506 s->alloc_from_partial, s->free_remove_partial,
507 s->alloc_from_partial * 100 / total_alloc,
508 total_free ? s->free_remove_partial * 100 / total_free : 0);
510 printf("Cpu partial list %8lu %8lu %3lu %3lu\n",
511 s->cpu_partial_alloc, s->cpu_partial_free,
512 s->cpu_partial_alloc * 100 / total_alloc,
513 total_free ? s->cpu_partial_free * 100 / total_free : 0);
515 printf("RemoteObj/SlabFrozen %8lu %8lu %3lu %3lu\n",
516 s->deactivate_remote_frees, s->free_frozen,
517 s->deactivate_remote_frees * 100 / total_alloc,
518 total_free ? s->free_frozen * 100 / total_free : 0);
520 printf("Total %8lu %8lu\n\n", total_alloc, total_free);
522 if (s->cpuslab_flush)
523 printf("Flushes %8lu\n", s->cpuslab_flush);
525 total = s->deactivate_full + s->deactivate_empty +
526 s->deactivate_to_head + s->deactivate_to_tail + s->deactivate_bypass;
529 printf("\nSlab Deactivation Occurrences %%\n");
530 printf("-------------------------------------------------\n");
531 printf("Slab full %7lu %3lu%%\n",
532 s->deactivate_full, (s->deactivate_full * 100) / total);
533 printf("Slab empty %7lu %3lu%%\n",
534 s->deactivate_empty, (s->deactivate_empty * 100) / total);
535 printf("Moved to head of partial list %7lu %3lu%%\n",
536 s->deactivate_to_head, (s->deactivate_to_head * 100) / total);
537 printf("Moved to tail of partial list %7lu %3lu%%\n",
538 s->deactivate_to_tail, (s->deactivate_to_tail * 100) / total);
539 printf("Deactivation bypass %7lu %3lu%%\n",
540 s->deactivate_bypass, (s->deactivate_bypass * 100) / total);
541 printf("Refilled from foreign frees %7lu %3lu%%\n",
542 s->alloc_refill, (s->alloc_refill * 100) / total);
543 printf("Node mismatch %7lu %3lu%%\n",
544 s->alloc_node_mismatch, (s->alloc_node_mismatch * 100) / total);
547 if (s->cmpxchg_double_fail || s->cmpxchg_double_cpu_fail) {
548 printf("\nCmpxchg_double Looping\n------------------------\n");
549 printf("Locked Cmpxchg Double redos %lu\nUnlocked Cmpxchg Double redos %lu\n",
550 s->cmpxchg_double_fail, s->cmpxchg_double_cpu_fail);
554 static void report(struct slabinfo *s)
556 if (strcmp(s->name, "*") == 0)
559 printf("\nSlabcache: %-15s Aliases: %2d Order : %2d Objects: %lu\n",
560 s->name, s->aliases, s->order, s->objects);
561 if (s->hwcache_align)
562 printf("** Hardware cacheline aligned\n");
564 printf("** Memory is allocated in a special DMA zone\n");
565 if (s->destroy_by_rcu)
566 printf("** Slabs are destroyed via RCU\n");
567 if (s->reclaim_account)
568 printf("** Reclaim accounting active\n");
570 printf("\nSizes (bytes) Slabs Debug Memory\n");
571 printf("------------------------------------------------------------------------\n");
572 printf("Object : %7d Total : %7ld Sanity Checks : %s Total: %7ld\n",
573 s->object_size, s->slabs, onoff(s->sanity_checks),
574 s->slabs * (page_size << s->order));
575 printf("SlabObj: %7d Full : %7ld Redzoning : %s Used : %7ld\n",
576 s->slab_size, s->slabs - s->partial - s->cpu_slabs,
577 onoff(s->red_zone), s->objects * s->object_size);
578 printf("SlabSiz: %7d Partial: %7ld Poisoning : %s Loss : %7ld\n",
579 page_size << s->order, s->partial, onoff(s->poison),
580 s->slabs * (page_size << s->order) - s->objects * s->object_size);
581 printf("Loss : %7d CpuSlab: %7d Tracking : %s Lalig: %7ld\n",
582 s->slab_size - s->object_size, s->cpu_slabs, onoff(s->store_user),
583 (s->slab_size - s->object_size) * s->objects);
584 printf("Align : %7d Objects: %7d Tracing : %s Lpadd: %7ld\n",
585 s->align, s->objs_per_slab, onoff(s->trace),
586 ((page_size << s->order) - s->objs_per_slab * s->slab_size) *
595 static void slabcache(struct slabinfo *s)
602 if (strcmp(s->name, "*") == 0)
605 if (unreclaim_only && s->reclaim_account)
608 if (actual_slabs == 1) {
613 if (skip_zero && !show_empty && !s->slabs)
616 if (show_empty && s->slabs)
620 store_size(size_str, slab_size(s));
622 store_size(size_str, slab_waste(s));
623 snprintf(dist_str, 40, "%lu/%lu/%d", s->slabs - s->cpu_slabs,
624 s->partial, s->cpu_slabs);
633 if (s->hwcache_align)
637 if (s->reclaim_account)
641 if (s->sanity_checks)
650 unsigned long total_alloc;
651 unsigned long total_free;
653 total_alloc = s->alloc_fastpath + s->alloc_slowpath;
654 total_free = s->free_fastpath + s->free_slowpath;
656 printf("%-21s %8ld %10ld %10ld %3ld %3ld %5ld %1d %4ld %4ld\n",
658 total_alloc, total_free,
659 total_alloc ? (s->alloc_fastpath * 100 / total_alloc) : 0,
660 total_free ? (s->free_fastpath * 100 / total_free) : 0,
661 s->order_fallback, s->order, s->cmpxchg_double_fail,
662 s->cmpxchg_double_cpu_fail);
664 printf("%-21s %8ld %7d %15s %14s %4d %1d %3ld %3ld %s\n",
665 s->name, s->objects, s->object_size, size_str, dist_str,
666 s->objs_per_slab, s->order,
667 s->slabs ? (s->partial * 100) / s->slabs : 100,
668 s->slabs ? (s->objects * s->object_size * 100) /
669 (s->slabs * (page_size << s->order)) : 100,
675 * Analyze debug options. Return false if something is amiss.
677 static int debug_opt_scan(char *opt)
679 if (!opt || !opt[0] || strcmp(opt, "-") == 0)
682 if (strcasecmp(opt, "a") == 0) {
726 static int slab_empty(struct slabinfo *s)
732 * We may still have slabs even if there are no objects. Shrinking will
736 set_obj(s, "shrink", 1);
741 static void slab_debug(struct slabinfo *s)
743 if (strcmp(s->name, "*") == 0)
746 if (sanity && !s->sanity_checks) {
747 set_obj(s, "sanity_checks", 1);
749 if (!sanity && s->sanity_checks) {
751 set_obj(s, "sanity_checks", 0);
753 fprintf(stderr, "%s not empty cannot disable sanity checks\n", s->name);
755 if (redzone && !s->red_zone) {
757 set_obj(s, "red_zone", 1);
759 fprintf(stderr, "%s not empty cannot enable redzoning\n", s->name);
761 if (!redzone && s->red_zone) {
763 set_obj(s, "red_zone", 0);
765 fprintf(stderr, "%s not empty cannot disable redzoning\n", s->name);
767 if (poison && !s->poison) {
769 set_obj(s, "poison", 1);
771 fprintf(stderr, "%s not empty cannot enable poisoning\n", s->name);
773 if (!poison && s->poison) {
775 set_obj(s, "poison", 0);
777 fprintf(stderr, "%s not empty cannot disable poisoning\n", s->name);
779 if (tracking && !s->store_user) {
781 set_obj(s, "store_user", 1);
783 fprintf(stderr, "%s not empty cannot enable tracking\n", s->name);
785 if (!tracking && s->store_user) {
787 set_obj(s, "store_user", 0);
789 fprintf(stderr, "%s not empty cannot disable tracking\n", s->name);
791 if (tracing && !s->trace) {
793 set_obj(s, "trace", 1);
795 fprintf(stderr, "%s can only enable trace for one slab at a time\n", s->name);
797 if (!tracing && s->trace)
798 set_obj(s, "trace", 1);
801 static void totals(void)
806 char b1[20], b2[20], b3[20], b4[20];
807 unsigned long long max = 1ULL << 63;
810 unsigned long long min_objsize = max, max_objsize = 0, avg_objsize;
812 /* Number of partial slabs in a slabcache */
813 unsigned long long min_partial = max, max_partial = 0,
814 avg_partial, total_partial = 0;
816 /* Number of slabs in a slab cache */
817 unsigned long long min_slabs = max, max_slabs = 0,
818 avg_slabs, total_slabs = 0;
820 /* Size of the whole slab */
821 unsigned long long min_size = max, max_size = 0,
822 avg_size, total_size = 0;
824 /* Bytes used for object storage in a slab */
825 unsigned long long min_used = max, max_used = 0,
826 avg_used, total_used = 0;
828 /* Waste: Bytes used for alignment and padding */
829 unsigned long long min_waste = max, max_waste = 0,
830 avg_waste, total_waste = 0;
831 /* Number of objects in a slab */
832 unsigned long long min_objects = max, max_objects = 0,
833 avg_objects, total_objects = 0;
834 /* Waste per object */
835 unsigned long long min_objwaste = max,
836 max_objwaste = 0, avg_objwaste,
839 /* Memory per object */
840 unsigned long long min_memobj = max,
841 max_memobj = 0, avg_memobj,
844 /* Percentage of partial slabs per slab */
845 unsigned long min_ppart = 100, max_ppart = 0,
846 avg_ppart, total_ppart = 0;
848 /* Number of objects in partial slabs */
849 unsigned long min_partobj = max, max_partobj = 0,
850 avg_partobj, total_partobj = 0;
852 /* Percentage of partial objects of all objects in a slab */
853 unsigned long min_ppartobj = 100, max_ppartobj = 0,
854 avg_ppartobj, total_ppartobj = 0;
857 for (s = slabinfo; s < slabinfo + slabs; s++) {
858 unsigned long long size;
860 unsigned long long wasted;
861 unsigned long long objwaste;
862 unsigned long percentage_partial_slabs;
863 unsigned long percentage_partial_objs;
865 if (!s->slabs || !s->objects)
871 used = s->objects * s->object_size;
872 wasted = size - used;
873 objwaste = s->slab_size - s->object_size;
875 percentage_partial_slabs = s->partial * 100 / s->slabs;
876 if (percentage_partial_slabs > 100)
877 percentage_partial_slabs = 100;
879 percentage_partial_objs = s->objects_partial * 100
882 if (percentage_partial_objs > 100)
883 percentage_partial_objs = 100;
885 if (s->object_size < min_objsize)
886 min_objsize = s->object_size;
887 if (s->partial < min_partial)
888 min_partial = s->partial;
889 if (s->slabs < min_slabs)
890 min_slabs = s->slabs;
893 if (wasted < min_waste)
895 if (objwaste < min_objwaste)
896 min_objwaste = objwaste;
897 if (s->objects < min_objects)
898 min_objects = s->objects;
901 if (s->objects_partial < min_partobj)
902 min_partobj = s->objects_partial;
903 if (percentage_partial_slabs < min_ppart)
904 min_ppart = percentage_partial_slabs;
905 if (percentage_partial_objs < min_ppartobj)
906 min_ppartobj = percentage_partial_objs;
907 if (s->slab_size < min_memobj)
908 min_memobj = s->slab_size;
910 if (s->object_size > max_objsize)
911 max_objsize = s->object_size;
912 if (s->partial > max_partial)
913 max_partial = s->partial;
914 if (s->slabs > max_slabs)
915 max_slabs = s->slabs;
918 if (wasted > max_waste)
920 if (objwaste > max_objwaste)
921 max_objwaste = objwaste;
922 if (s->objects > max_objects)
923 max_objects = s->objects;
926 if (s->objects_partial > max_partobj)
927 max_partobj = s->objects_partial;
928 if (percentage_partial_slabs > max_ppart)
929 max_ppart = percentage_partial_slabs;
930 if (percentage_partial_objs > max_ppartobj)
931 max_ppartobj = percentage_partial_objs;
932 if (s->slab_size > max_memobj)
933 max_memobj = s->slab_size;
935 total_partial += s->partial;
936 total_slabs += s->slabs;
938 total_waste += wasted;
940 total_objects += s->objects;
942 total_partobj += s->objects_partial;
943 total_ppart += percentage_partial_slabs;
944 total_ppartobj += percentage_partial_objs;
946 total_objwaste += s->objects * objwaste;
947 total_objsize += s->objects * s->slab_size;
950 if (!total_objects) {
951 printf("No objects\n");
955 printf("No slabs\n");
959 /* Per slab averages */
960 avg_partial = total_partial / used_slabs;
961 avg_slabs = total_slabs / used_slabs;
962 avg_size = total_size / used_slabs;
963 avg_waste = total_waste / used_slabs;
965 avg_objects = total_objects / used_slabs;
966 avg_used = total_used / used_slabs;
967 avg_partobj = total_partobj / used_slabs;
968 avg_ppart = total_ppart / used_slabs;
969 avg_ppartobj = total_ppartobj / used_slabs;
971 /* Per object object sizes */
972 avg_objsize = total_used / total_objects;
973 avg_objwaste = total_objwaste / total_objects;
974 avg_partobj = total_partobj * 100 / total_objects;
975 avg_memobj = total_objsize / total_objects;
977 printf("Slabcache Totals\n");
978 printf("----------------\n");
979 printf("Slabcaches : %15d Aliases : %11d->%-3d Active: %3d\n",
980 slabs, aliases, alias_targets, used_slabs);
982 store_size(b1, total_size);store_size(b2, total_waste);
983 store_size(b3, total_waste * 100 / total_used);
984 printf("Memory used: %15s # Loss : %15s MRatio:%6s%%\n", b1, b2, b3);
986 store_size(b1, total_objects);store_size(b2, total_partobj);
987 store_size(b3, total_partobj * 100 / total_objects);
988 printf("# Objects : %15s # PartObj: %15s ORatio:%6s%%\n", b1, b2, b3);
991 printf("Per Cache Average "
993 printf("---------------------------------------"
994 "-------------------------------------\n");
996 store_size(b1, avg_objects);store_size(b2, min_objects);
997 store_size(b3, max_objects);store_size(b4, total_objects);
998 printf("#Objects %15s %15s %15s %15s\n",
1001 store_size(b1, avg_slabs);store_size(b2, min_slabs);
1002 store_size(b3, max_slabs);store_size(b4, total_slabs);
1003 printf("#Slabs %15s %15s %15s %15s\n",
1006 store_size(b1, avg_partial);store_size(b2, min_partial);
1007 store_size(b3, max_partial);store_size(b4, total_partial);
1008 printf("#PartSlab %15s %15s %15s %15s\n",
1010 store_size(b1, avg_ppart);store_size(b2, min_ppart);
1011 store_size(b3, max_ppart);
1012 store_size(b4, total_partial * 100 / total_slabs);
1013 printf("%%PartSlab%15s%% %15s%% %15s%% %15s%%\n",
1016 store_size(b1, avg_partobj);store_size(b2, min_partobj);
1017 store_size(b3, max_partobj);
1018 store_size(b4, total_partobj);
1019 printf("PartObjs %15s %15s %15s %15s\n",
1022 store_size(b1, avg_ppartobj);store_size(b2, min_ppartobj);
1023 store_size(b3, max_ppartobj);
1024 store_size(b4, total_partobj * 100 / total_objects);
1025 printf("%% PartObj%15s%% %15s%% %15s%% %15s%%\n",
1028 store_size(b1, avg_size);store_size(b2, min_size);
1029 store_size(b3, max_size);store_size(b4, total_size);
1030 printf("Memory %15s %15s %15s %15s\n",
1033 store_size(b1, avg_used);store_size(b2, min_used);
1034 store_size(b3, max_used);store_size(b4, total_used);
1035 printf("Used %15s %15s %15s %15s\n",
1038 store_size(b1, avg_waste);store_size(b2, min_waste);
1039 store_size(b3, max_waste);store_size(b4, total_waste);
1040 printf("Loss %15s %15s %15s %15s\n",
1044 printf("Per Object Average "
1046 printf("---------------------------------------"
1047 "--------------------\n");
1049 store_size(b1, avg_memobj);store_size(b2, min_memobj);
1050 store_size(b3, max_memobj);
1051 printf("Memory %15s %15s %15s\n",
1053 store_size(b1, avg_objsize);store_size(b2, min_objsize);
1054 store_size(b3, max_objsize);
1055 printf("User %15s %15s %15s\n",
1058 store_size(b1, avg_objwaste);store_size(b2, min_objwaste);
1059 store_size(b3, max_objwaste);
1060 printf("Loss %15s %15s %15s\n",
1064 static void sort_slabs(void)
1066 struct slabinfo *s1,*s2;
1068 for (s1 = slabinfo; s1 < slabinfo + slabs; s1++) {
1069 for (s2 = s1 + 1; s2 < slabinfo + slabs; s2++) {
1073 if (slab_size(s1) == slab_size(s2))
1074 result = strcasecmp(s1->name, s2->name);
1076 result = slab_size(s1) < slab_size(s2);
1077 } else if (sort_active) {
1078 if (slab_activity(s1) == slab_activity(s2))
1079 result = strcasecmp(s1->name, s2->name);
1081 result = slab_activity(s1) < slab_activity(s2);
1082 } else if (sort_loss) {
1083 if (slab_waste(s1) == slab_waste(s2))
1084 result = strcasecmp(s1->name, s2->name);
1086 result = slab_waste(s1) < slab_waste(s2);
1087 } else if (sort_partial) {
1088 if (s1->partial == s2->partial)
1089 result = strcasecmp(s1->name, s2->name);
1091 result = s1->partial < s2->partial;
1093 result = strcasecmp(s1->name, s2->name);
1101 memcpy(&t, s1, sizeof(struct slabinfo));
1102 memcpy(s1, s2, sizeof(struct slabinfo));
1103 memcpy(s2, &t, sizeof(struct slabinfo));
1109 static void sort_aliases(void)
1111 struct aliasinfo *a1,*a2;
1113 for (a1 = aliasinfo; a1 < aliasinfo + aliases; a1++) {
1114 for (a2 = a1 + 1; a2 < aliasinfo + aliases; a2++) {
1119 if (show_alias && !show_inverted) {
1123 if (strcasecmp(n1, n2) > 0) {
1126 memcpy(&t, a1, sizeof(struct aliasinfo));
1127 memcpy(a1, a2, sizeof(struct aliasinfo));
1128 memcpy(a2, &t, sizeof(struct aliasinfo));
1134 static void link_slabs(void)
1136 struct aliasinfo *a;
1139 for (a = aliasinfo; a < aliasinfo + aliases; a++) {
1141 for (s = slabinfo; s < slabinfo + slabs; s++)
1142 if (strcmp(a->ref, s->name) == 0) {
1147 if (s == slabinfo + slabs)
1148 fatal("Unresolved alias %s\n", a->ref);
1152 static void alias(void)
1154 struct aliasinfo *a;
1155 char *active = NULL;
1160 for(a = aliasinfo; a < aliasinfo + aliases; a++) {
1162 if (!show_single_ref && a->slab->refs == 1)
1165 if (!show_inverted) {
1167 if (strcmp(a->slab->name, active) == 0) {
1168 printf(" %s", a->name);
1172 printf("\n%-12s <- %s", a->slab->name, a->name);
1173 active = a->slab->name;
1176 printf("%-15s -> %s\n", a->name, a->slab->name);
1183 static void rename_slabs(void)
1186 struct aliasinfo *a;
1188 for (s = slabinfo; s < slabinfo + slabs; s++) {
1189 if (*s->name != ':')
1192 if (s->refs > 1 && !show_first_alias)
1195 a = find_one_alias(s);
1206 static int slab_mismatch(char *slab)
1208 return regexec(&pattern, slab, 0, NULL, 0);
1211 static void read_slab_dir(void)
1215 struct slabinfo *slab = slabinfo;
1216 struct aliasinfo *alias = aliasinfo;
1221 if (chdir("/sys/kernel/slab") && chdir("/sys/slab"))
1222 fatal("SYSFS support for SLUB not active\n");
1225 while ((de = readdir(dir))) {
1226 if (de->d_name[0] == '.' ||
1227 (de->d_name[0] != ':' && slab_mismatch(de->d_name)))
1229 switch (de->d_type) {
1231 alias->name = strdup(de->d_name);
1232 count = readlink(de->d_name, buffer, sizeof(buffer)-1);
1235 fatal("Cannot read symlink %s\n", de->d_name);
1239 while (p > buffer && p[-1] != '/')
1241 alias->ref = strdup(p);
1245 if (chdir(de->d_name))
1246 fatal("Unable to access slab %s\n", slab->name);
1247 slab->name = strdup(de->d_name);
1250 slab->aliases = get_obj("aliases");
1251 slab->align = get_obj("align");
1252 slab->cache_dma = get_obj("cache_dma");
1253 slab->cpu_slabs = get_obj("cpu_slabs");
1254 slab->destroy_by_rcu = get_obj("destroy_by_rcu");
1255 slab->hwcache_align = get_obj("hwcache_align");
1256 slab->object_size = get_obj("object_size");
1257 slab->objects = get_obj("objects");
1258 slab->objects_partial = get_obj("objects_partial");
1259 slab->objects_total = get_obj("objects_total");
1260 slab->objs_per_slab = get_obj("objs_per_slab");
1261 slab->order = get_obj("order");
1262 slab->partial = get_obj("partial");
1263 slab->partial = get_obj_and_str("partial", &t);
1264 decode_numa_list(slab->numa_partial, t);
1266 slab->poison = get_obj("poison");
1267 slab->reclaim_account = get_obj("reclaim_account");
1268 slab->red_zone = get_obj("red_zone");
1269 slab->sanity_checks = get_obj("sanity_checks");
1270 slab->slab_size = get_obj("slab_size");
1271 slab->slabs = get_obj_and_str("slabs", &t);
1272 decode_numa_list(slab->numa, t);
1274 slab->store_user = get_obj("store_user");
1275 slab->trace = get_obj("trace");
1276 slab->alloc_fastpath = get_obj("alloc_fastpath");
1277 slab->alloc_slowpath = get_obj("alloc_slowpath");
1278 slab->free_fastpath = get_obj("free_fastpath");
1279 slab->free_slowpath = get_obj("free_slowpath");
1280 slab->free_frozen= get_obj("free_frozen");
1281 slab->free_add_partial = get_obj("free_add_partial");
1282 slab->free_remove_partial = get_obj("free_remove_partial");
1283 slab->alloc_from_partial = get_obj("alloc_from_partial");
1284 slab->alloc_slab = get_obj("alloc_slab");
1285 slab->alloc_refill = get_obj("alloc_refill");
1286 slab->free_slab = get_obj("free_slab");
1287 slab->cpuslab_flush = get_obj("cpuslab_flush");
1288 slab->deactivate_full = get_obj("deactivate_full");
1289 slab->deactivate_empty = get_obj("deactivate_empty");
1290 slab->deactivate_to_head = get_obj("deactivate_to_head");
1291 slab->deactivate_to_tail = get_obj("deactivate_to_tail");
1292 slab->deactivate_remote_frees = get_obj("deactivate_remote_frees");
1293 slab->order_fallback = get_obj("order_fallback");
1294 slab->cmpxchg_double_cpu_fail = get_obj("cmpxchg_double_cpu_fail");
1295 slab->cmpxchg_double_fail = get_obj("cmpxchg_double_fail");
1296 slab->cpu_partial_alloc = get_obj("cpu_partial_alloc");
1297 slab->cpu_partial_free = get_obj("cpu_partial_free");
1298 slab->alloc_node_mismatch = get_obj("alloc_node_mismatch");
1299 slab->deactivate_bypass = get_obj("deactivate_bypass");
1301 if (slab->name[0] == ':')
1306 fatal("Unknown file type %lx\n", de->d_type);
1310 slabs = slab - slabinfo;
1311 actual_slabs = slabs;
1312 aliases = alias - aliasinfo;
1313 if (slabs > MAX_SLABS)
1314 fatal("Too many slabs\n");
1315 if (aliases > MAX_ALIASES)
1316 fatal("Too many aliases\n");
1319 static void output_slabs(void)
1321 struct slabinfo *slab;
1322 int lines = output_lines;
1324 for (slab = slabinfo; (slab < slabinfo + slabs) &&
1325 lines != 0; slab++) {
1335 else if (show_track)
1336 show_tracking(slab);
1338 slab_validate(slab);
1347 else if (show_report)
1352 static void _xtotals(char *heading, char *underline,
1353 int loss, int size, int partial)
1355 printf("%s%s", heading, underline);
1359 sort_partial = partial;
1364 static void xtotals(void)
1366 char *heading, *underline;
1373 heading = "\nSlabs sorted by size\n";
1374 underline = "--------------------\n";
1375 _xtotals(heading, underline, 0, 1, 0);
1377 heading = "\nSlabs sorted by loss\n";
1378 underline = "--------------------\n";
1379 _xtotals(heading, underline, 1, 0, 0);
1381 heading = "\nSlabs sorted by number of partial slabs\n";
1382 underline = "---------------------------------------\n";
1383 _xtotals(heading, underline, 0, 0, 1);
1388 struct option opts[] = {
1389 { "aliases", no_argument, NULL, 'a' },
1390 { "activity", no_argument, NULL, 'A' },
1391 { "Bytes", no_argument, NULL, 'B'},
1392 { "debug", optional_argument, NULL, 'd' },
1393 { "display-activity", no_argument, NULL, 'D' },
1394 { "empty", no_argument, NULL, 'e' },
1395 { "first-alias", no_argument, NULL, 'f' },
1396 { "help", no_argument, NULL, 'h' },
1397 { "inverted", no_argument, NULL, 'i'},
1398 { "slabs", no_argument, NULL, 'l' },
1399 { "Loss", no_argument, NULL, 'L'},
1400 { "numa", no_argument, NULL, 'n' },
1401 { "lines", required_argument, NULL, 'N'},
1402 { "ops", no_argument, NULL, 'o' },
1403 { "partial", no_argument, NULL, 'p'},
1404 { "report", no_argument, NULL, 'r' },
1405 { "shrink", no_argument, NULL, 's' },
1406 { "Size", no_argument, NULL, 'S'},
1407 { "tracking", no_argument, NULL, 't'},
1408 { "Totals", no_argument, NULL, 'T'},
1409 { "Unreclaim", no_argument, NULL, 'U'},
1410 { "validate", no_argument, NULL, 'v' },
1411 { "Xtotals", no_argument, NULL, 'X'},
1412 { "zero", no_argument, NULL, 'z' },
1413 { "1ref", no_argument, NULL, '1'},
1414 { NULL, 0, NULL, 0 }
1417 int main(int argc, char *argv[])
1421 char *pattern_source;
1423 page_size = getpagesize();
1425 while ((c = getopt_long(argc, argv, "aABd::DefhilLnN:oPrsStTUvXz1",
1439 if (!debug_opt_scan(optarg))
1440 fatal("Invalid debug option '%s'\n", optarg);
1449 show_first_alias = 1;
1468 output_lines = atoi(optarg);
1469 if (output_lines < 1)
1501 if (output_lines == -1)
1503 extended_totals = 1;
1510 show_single_ref = 1;
1513 fatal("%s: Invalid option '%c'\n", argv[0], optopt);
1517 if (!show_slab && !show_alias && !show_track && !show_report
1518 && !validate && !shrink && !set_debug && !show_ops)
1522 pattern_source = argv[optind];
1524 pattern_source = ".*";
1526 err = regcomp(&pattern, pattern_source, REG_ICASE|REG_NOSUB);
1528 fatal("%s: Invalid pattern '%s' code %d\n",
1529 argv[0], pattern_source, err);
1533 } else if (extended_totals) {
1535 } else if (show_totals) {