slabinfo.c 36 KB

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