slabinfo.c 27 KB

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