slabinfo.c 36 KB

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