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