13 #include <sys/types.h>
24 #define MAX_ALIASES 500
25 #define MAX_NODES 1024
94 static void fatal(
const char *
x, ...)
104 static void usage(
void)
106 printf(
"slabinfo 4/15/2011. (c) 2007 sgi/(c) 2011 Linux Foundation.\n\n"
107 "slabinfo [-ahnpvtsz] [-d debugopts] [slab-regexp]\n"
108 "-a|--aliases Show aliases\n"
109 "-A|--activity Most active slabs first\n"
110 "-d<options>|--debug=<options> Set/Clear Debug options\n"
111 "-D|--display-active Switch line format to activity\n"
112 "-e|--empty Show empty slabs\n"
113 "-f|--first-alias Show first alias\n"
114 "-h|--help Show usage information\n"
115 "-i|--inverted Inverted list\n"
116 "-l|--slabs Show slabs\n"
117 "-n|--numa Show NUMA information\n"
118 "-o|--ops Show kmem_cache_ops\n"
119 "-s|--shrink Shrink slabs\n"
120 "-r|--report Detailed report on single slabs\n"
121 "-S|--Size Sort by size\n"
122 "-t|--tracking Show alloc/free information\n"
123 "-T|--Totals Show summary information\n"
124 "-v|--validate Validate slabs\n"
125 "-z|--zero Include empty slabs\n"
126 "-1|--1ref Single reference\n"
127 "\nValid debug options (FZPUT may be combined)\n"
128 "a / A Switch on all debug options (=FZUP)\n"
129 "- Switch off all debug options\n"
130 "f / F Sanity Checks (SLAB_DEBUG_FREE)\n"
138 static unsigned long read_obj(
const char *
name)
140 FILE *
f = fopen(name,
"r");
158 static unsigned long get_obj(
const char *name)
166 static unsigned long get_obj_and_str(
const char *name,
char **
x)
173 if (!read_obj(name)) {
177 result = strtoul(
buffer, &p, 10);
185 static void set_obj(
struct slabinfo *
s,
const char *name,
int n)
193 fatal(
"Cannot write to %s\n", x);
199 static unsigned long read_slab_obj(
struct slabinfo *s,
const char *name)
222 static int store_size(
char *
buffer,
unsigned long value)
228 if (value > 1000000000
UL) {
229 divisor = 100000000
UL;
231 }
else if (value > 1000000
UL) {
234 }
else if (value > 1000
UL) {
240 n =
sprintf(buffer,
"%ld",value);
247 memmove(buffer + n - 2, buffer + n - 3, 4);
254 static void decode_numa_list(
int *
numa,
char *
t)
266 node = strtoul(t, &t, 10);
269 nr = strtoul(t, &t, 10);
279 static void slab_validate(
struct slabinfo *s)
284 set_obj(s,
"validate", 1);
287 static void slab_shrink(
struct slabinfo *s)
292 set_obj(s,
"shrink", 1);
297 static void first_line(
void)
300 printf(
"Name Objects Alloc Free %%Fast Fallb O CmpX UL\n");
302 printf(
"Name Objects Objsize Space "
303 "Slabs/Part/Cpu O/S O %%Fr %%Ef Flg\n");
315 if (a->
slab == find &&
325 static unsigned long slab_size(
struct slabinfo *s)
330 static unsigned long slab_activity(
struct slabinfo *s)
344 printf(
"\n%s: No NUMA information available.\n", s->
name);
352 printf(
"\n%-21s:", mode ?
"NUMA nodes" :
"Slab");
355 printf(
"\n----------------------");
360 printf(
"%-21s ", mode ?
"All slabs" : s->
name);
364 store_size(b, s->
numa[node]);
369 printf(
"%-21s ",
"Partial slabs");
381 static void show_tracking(
struct slabinfo *s)
383 printf(
"\n%s: Kernel object allocation\n", s->
name);
384 printf(
"-----------------------------------------------------------------------\n");
385 if (read_slab_obj(s,
"alloc_calls"))
390 printf(
"\n%s: Kernel object freeing\n", s->
name);
391 printf(
"------------------------------------------------------------------------\n");
392 if (read_slab_obj(s,
"free_calls"))
404 if (read_slab_obj(s,
"ops")) {
405 printf(
"\n%s: kmem_cache operations\n", s->
name);
406 printf(
"--------------------------------------------\n");
409 printf(
"\n%s has no kmem_cache operations\n", s->
name);
412 static const char *
onoff(
int x)
419 static void slab_stats(
struct slabinfo *s)
421 unsigned long total_alloc;
422 unsigned long total_free;
435 printf(
"Slab Perf Counter Alloc Free %%Al %%Fr\n");
436 printf(
"--------------------------------------------------\n");
437 printf(
"Fastpath %8lu %8lu %3lu %3lu\n",
441 printf(
"Slowpath %8lu %8lu %3lu %3lu\n",
445 printf(
"Page Alloc %8lu %8lu %3lu %3lu\n",
448 total_free ? s->
free_slab * 100 / total_free : 0);
449 printf(
"Add partial %8lu %8lu %3lu %3lu\n",
454 printf(
"Remove partial %8lu %8lu %3lu %3lu\n",
459 printf(
"Cpu partial list %8lu %8lu %3lu %3lu\n",
464 printf(
"RemoteObj/SlabFrozen %8lu %8lu %3lu %3lu\n",
467 total_free ? s->
free_frozen * 100 / total_free : 0);
469 printf(
"Total %8lu %8lu\n\n", total_alloc, total_free);
478 printf(
"\nSlab Deactivation Ocurrences %%\n");
479 printf(
"-------------------------------------------------\n");
480 printf(
"Slab full %7lu %3lu%%\n",
482 printf(
"Slab empty %7lu %3lu%%\n",
484 printf(
"Moved to head of partial list %7lu %3lu%%\n",
486 printf(
"Moved to tail of partial list %7lu %3lu%%\n",
488 printf(
"Deactivation bypass %7lu %3lu%%\n",
490 printf(
"Refilled from foreign frees %7lu %3lu%%\n",
492 printf(
"Node mismatch %7lu %3lu%%\n",
497 printf(
"\nCmpxchg_double Looping\n------------------------\n");
498 printf(
"Locked Cmpxchg Double redos %lu\nUnlocked Cmpxchg Double redos %lu\n",
507 printf(
"\nSlabcache: %-20s Aliases: %2d Order : %2d Objects: %lu\n",
510 printf(
"** Hardware cacheline aligned\n");
512 printf(
"** Memory is allocated in a special DMA zone\n");
514 printf(
"** Slabs are destroyed via RCU\n");
516 printf(
"** Reclaim accounting active\n");
518 printf(
"\nSizes (bytes) Slabs Debug Memory\n");
519 printf(
"------------------------------------------------------------------------\n");
520 printf(
"Object : %7d Total : %7ld Sanity Checks : %s Total: %7ld\n",
523 printf(
"SlabObj: %7d Full : %7ld Redzoning : %s Used : %7ld\n",
526 printf(
"SlabSiz: %7d Partial: %7ld Poisoning : %s Loss : %7ld\n",
529 printf(
"Loss : %7d CpuSlab: %7d Tracking : %s Lalig: %7ld\n",
532 printf(
"Align : %7d Objects: %7d Tracing : %s Lpadd: %7ld\n",
543 static void slabcache(
struct slabinfo *s)
564 store_size(size_str, slab_size(s));
592 unsigned long total_alloc;
593 unsigned long total_free;
598 printf(
"%-21s %8ld %10ld %10ld %3ld %3ld %5ld %1d %4ld %4ld\n",
600 total_alloc, total_free,
607 printf(
"%-21s %8ld %7d %8s %14s %4d %1d %3ld %3ld %s\n",
619 static int debug_opt_scan(
char *
opt)
621 if (!opt || !opt[0] ||
strcmp(opt,
"-") == 0)
668 static int slab_empty(
struct slabinfo *s)
678 set_obj(s,
"shrink", 1);
683 static void slab_debug(
struct slabinfo *s)
689 set_obj(s,
"sanity", 1);
693 set_obj(s,
"sanity", 0);
695 fprintf(stderr,
"%s not empty cannot disable sanity checks\n", s->
name);
699 set_obj(s,
"red_zone", 1);
701 fprintf(stderr,
"%s not empty cannot enable redzoning\n", s->
name);
705 set_obj(s,
"red_zone", 0);
707 fprintf(stderr,
"%s not empty cannot disable redzoning\n", s->
name);
711 set_obj(s,
"poison", 1);
713 fprintf(stderr,
"%s not empty cannot enable poisoning\n", s->
name);
717 set_obj(s,
"poison", 0);
719 fprintf(stderr,
"%s not empty cannot disable poisoning\n", s->
name);
723 set_obj(s,
"store_user", 1);
725 fprintf(stderr,
"%s not empty cannot enable tracking\n", s->
name);
729 set_obj(s,
"store_user", 0);
731 fprintf(stderr,
"%s not empty cannot disable tracking\n", s->
name);
735 set_obj(s,
"trace", 1);
737 fprintf(stderr,
"%s can only enable trace for one slab at a time\n", s->
name);
740 set_obj(s,
"trace", 1);
743 static void totals(
void)
748 char b1[20], b2[20], b3[20], b4[20];
749 unsigned long long max = 1ULL << 63;
752 unsigned long long min_objsize =
max, max_objsize = 0, avg_objsize;
755 unsigned long long min_partial =
max, max_partial = 0,
756 avg_partial, total_partial = 0;
759 unsigned long long min_slabs =
max, max_slabs = 0,
760 avg_slabs, total_slabs = 0;
767 unsigned long long min_used =
max, max_used = 0,
768 avg_used, total_used = 0;
771 unsigned long long min_waste =
max, max_waste = 0,
772 avg_waste, total_waste = 0;
774 unsigned long long min_objects =
max, max_objects = 0,
775 avg_objects, total_objects = 0;
777 unsigned long long min_objwaste =
max,
778 max_objwaste = 0, avg_objwaste,
782 unsigned long long min_memobj =
max,
783 max_memobj = 0, avg_memobj,
787 unsigned long min_ppart = 100, max_ppart = 0,
788 avg_ppart, total_ppart = 0;
791 unsigned long min_partobj =
max, max_partobj = 0,
792 avg_partobj, total_partobj = 0;
795 unsigned long min_ppartobj = 100, max_ppartobj = 0,
796 avg_ppartobj, total_ppartobj = 0;
800 unsigned long long size;
802 unsigned long long wasted;
803 unsigned long long objwaste;
804 unsigned long percentage_partial_slabs;
805 unsigned long percentage_partial_objs;
814 wasted = size -
used;
818 if (percentage_partial_slabs > 100)
819 percentage_partial_slabs = 100;
824 if (percentage_partial_objs > 100)
825 percentage_partial_objs = 100;
831 if (s->
slabs < min_slabs)
832 min_slabs = s->
slabs;
835 if (wasted < min_waste)
837 if (objwaste < min_objwaste)
838 min_objwaste = objwaste;
845 if (percentage_partial_slabs < min_ppart)
846 min_ppart = percentage_partial_slabs;
847 if (percentage_partial_objs < min_ppartobj)
848 min_ppartobj = percentage_partial_objs;
856 if (s->
slabs > max_slabs)
857 max_slabs = s->
slabs;
860 if (wasted > max_waste)
862 if (objwaste > max_objwaste)
863 max_objwaste = objwaste;
870 if (percentage_partial_slabs > max_ppart)
871 max_ppart = percentage_partial_slabs;
872 if (percentage_partial_objs > max_ppartobj)
873 max_ppartobj = percentage_partial_objs;
878 total_slabs += s->
slabs;
880 total_waste += wasted;
885 total_ppart += percentage_partial_slabs;
886 total_ppartobj += percentage_partial_objs;
888 total_objwaste += s->
objects * objwaste;
892 if (!total_objects) {
902 avg_partial = total_partial / used_slabs;
903 avg_slabs = total_slabs / used_slabs;
904 avg_size = total_size / used_slabs;
905 avg_waste = total_waste / used_slabs;
907 avg_objects = total_objects / used_slabs;
908 avg_used = total_used / used_slabs;
909 avg_partobj = total_partobj / used_slabs;
910 avg_ppart = total_ppart / used_slabs;
911 avg_ppartobj = total_ppartobj / used_slabs;
914 avg_objsize = total_used / total_objects;
915 avg_objwaste = total_objwaste / total_objects;
916 avg_partobj = total_partobj * 100 / total_objects;
917 avg_memobj = total_objsize / total_objects;
919 printf(
"Slabcache Totals\n");
920 printf(
"----------------\n");
921 printf(
"Slabcaches : %3d Aliases : %3d->%-3d Active: %3d\n",
924 store_size(b1, total_size);store_size(b2, total_waste);
925 store_size(b3, total_waste * 100 / total_used);
926 printf(
"Memory used: %6s # Loss : %6s MRatio:%6s%%\n", b1, b2, b3);
928 store_size(b1, total_objects);store_size(b2, total_partobj);
929 store_size(b3, total_partobj * 100 / total_objects);
930 printf(
"# Objects : %6s # PartObj: %6s ORatio:%6s%%\n", b1, b2, b3);
933 printf(
"Per Cache Average Min Max Total\n");
934 printf(
"---------------------------------------------------------\n");
936 store_size(b1, avg_objects);store_size(b2, min_objects);
937 store_size(b3, max_objects);store_size(b4, total_objects);
938 printf(
"#Objects %10s %10s %10s %10s\n",
941 store_size(b1, avg_slabs);store_size(b2, min_slabs);
942 store_size(b3, max_slabs);store_size(b4, total_slabs);
943 printf(
"#Slabs %10s %10s %10s %10s\n",
946 store_size(b1, avg_partial);store_size(b2, min_partial);
947 store_size(b3, max_partial);store_size(b4, total_partial);
948 printf(
"#PartSlab %10s %10s %10s %10s\n",
950 store_size(b1, avg_ppart);store_size(b2, min_ppart);
951 store_size(b3, max_ppart);
952 store_size(b4, total_partial * 100 / total_slabs);
953 printf(
"%%PartSlab%10s%% %10s%% %10s%% %10s%%\n",
956 store_size(b1, avg_partobj);store_size(b2, min_partobj);
957 store_size(b3, max_partobj);
958 store_size(b4, total_partobj);
959 printf(
"PartObjs %10s %10s %10s %10s\n",
962 store_size(b1, avg_ppartobj);store_size(b2, min_ppartobj);
963 store_size(b3, max_ppartobj);
964 store_size(b4, total_partobj * 100 / total_objects);
965 printf(
"%% PartObj%10s%% %10s%% %10s%% %10s%%\n",
968 store_size(b1, avg_size);store_size(b2, min_size);
969 store_size(b3, max_size);store_size(b4, total_size);
970 printf(
"Memory %10s %10s %10s %10s\n",
973 store_size(b1, avg_used);store_size(b2, min_used);
974 store_size(b3, max_used);store_size(b4, total_used);
975 printf(
"Used %10s %10s %10s %10s\n",
978 store_size(b1, avg_waste);store_size(b2, min_waste);
979 store_size(b3, max_waste);store_size(b4, total_waste);
980 printf(
"Loss %10s %10s %10s %10s\n",
984 printf(
"Per Object Average Min Max\n");
985 printf(
"---------------------------------------------\n");
987 store_size(b1, avg_memobj);store_size(b2, min_memobj);
988 store_size(b3, max_memobj);
989 printf(
"Memory %10s %10s %10s\n",
991 store_size(b1, avg_objsize);store_size(b2, min_objsize);
992 store_size(b3, max_objsize);
993 printf(
"User %10s %10s %10s\n",
996 store_size(b1, avg_objwaste);store_size(b2, min_objwaste);
997 store_size(b3, max_objwaste);
998 printf(
"Loss %10s %10s %10s\n",
1002 static void sort_slabs(
void)
1013 result = slab_activity(s1) < slab_activity(s2);
1031 static void sort_aliases(
void)
1056 static void link_slabs(
void)
1070 fatal(
"Unresolved alias %s\n", a->
ref);
1074 static void alias(
void)
1095 active = a->
slab->name;
1105 static void rename_slabs(
void)
1111 if (*s->
name !=
':')
1117 a = find_one_alias(s);
1128 static int slab_mismatch(
char *
slab)
1133 static void read_slab_dir(
void)
1143 if (chdir(
"/sys/kernel/slab") && chdir(
"/sys/slab"))
1144 fatal(
"SYSFS support for SLUB not active\n");
1147 while ((de = readdir(dir))) {
1148 if (de->d_name[0] ==
'.' ||
1149 (de->d_name[0] !=
':' && slab_mismatch(de->d_name)))
1151 switch (de->d_type) {
1153 alias->
name = strdup(de->d_name);
1154 count = readlink(de->d_name, buffer,
sizeof(buffer)-1);
1157 fatal(
"Cannot read symlink %s\n", de->d_name);
1161 while (p > buffer && p[-1] !=
'/')
1163 alias->
ref = strdup(p);
1167 if (chdir(de->d_name))
1168 fatal(
"Unable to access slab %s\n", slab->
name);
1169 slab->
name = strdup(de->d_name);
1172 slab->
aliases = get_obj(
"aliases");
1173 slab->
align = get_obj(
"align");
1179 slab->
objects = get_obj(
"objects");
1183 slab->
order = get_obj(
"order");
1184 slab->
partial = get_obj(
"partial");
1185 slab->
partial = get_obj_and_str(
"partial", &t);
1188 slab->
poison = get_obj(
"poison");
1190 slab->
red_zone = get_obj(
"red_zone");
1193 slab->
slabs = get_obj_and_str(
"slabs", &t);
1194 decode_numa_list(slab->
numa, t);
1197 slab->
trace = get_obj(
"trace");
1223 if (slab->
name[0] ==
':')
1228 fatal(
"Unknown file type %lx\n", de->d_type);
1236 fatal(
"Too many slabs\n");
1238 fatal(
"Too many aliases\n");
1241 static void output_slabs(
void)
1243 struct slabinfo *
slab;
1245 for (slab = slabinfo; slab < slabinfo +
slabs; slab++) {
1254 show_tracking(slab);
1256 slab_validate(slab);
1271 {
"aliases", 0,
NULL,
'a' },
1272 {
"activity", 0,
NULL,
'A' },
1273 {
"debug", 2,
NULL,
'd' },
1274 {
"display-activity", 0,
NULL,
'D' },
1275 {
"empty", 0,
NULL,
'e' },
1276 {
"first-alias", 0,
NULL,
'f' },
1277 {
"help", 0,
NULL,
'h' },
1278 {
"inverted", 0,
NULL,
'i'},
1279 {
"numa", 0,
NULL,
'n' },
1280 {
"ops", 0,
NULL,
'o' },
1281 {
"report", 0,
NULL,
'r' },
1282 {
"shrink", 0,
NULL,
's' },
1283 {
"slabs", 0,
NULL,
'l' },
1284 {
"track", 0,
NULL,
't'},
1285 {
"validate", 0,
NULL,
'v' },
1286 {
"zero", 0,
NULL,
'z' },
1287 {
"1ref", 0,
NULL,
'1'},
1295 char *pattern_source;
1299 while ((c = getopt_long(argc, argv,
"aAd::Defhil1noprstvzTS",
1313 if (!debug_opt_scan(optarg))
1314 fatal(
"Invalid debug option '%s'\n", optarg);
1363 fatal(
"%s: Invalid option '%c'\n", argv[0], optopt);
1372 pattern_source = argv[optind];
1374 pattern_source =
".*";
1376 err = regcomp(&
pattern, pattern_source, REG_ICASE|REG_NOSUB);
1378 fatal(
"%s: Invalid pattern '%s' code %d\n",
1379 argv[0], pattern_source, err);