slabinfo.c 27 KB

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