blob: 2ef7f0ca01684b67ea653b4f58a00eaa764e4d21 [file] [log] [blame]
Christoph Lameterc09d8752007-05-06 14:49:48 -07001/*
2 * Slabinfo: Tool to get reports about slabs
3 *
Christoph Lametercde53532008-07-04 09:59:22 -07004 * (C) 2007 sgi, Christoph Lameter
Christoph Lameter9da47142011-06-01 12:26:00 -05005 * (C) 2011 Linux Foundation, Christoph Lameter
Christoph Lameterc09d8752007-05-06 14:49:48 -07006 *
Christoph Lameter9da47142011-06-01 12:26:00 -05007 * Compile with:
Christoph Lameterc09d8752007-05-06 14:49:48 -07008 *
9 * gcc -o slabinfo slabinfo.c
10 */
11#include <stdio.h>
12#include <stdlib.h>
13#include <sys/types.h>
14#include <dirent.h>
WANG Congf32143a2007-10-16 23:31:29 -070015#include <strings.h>
Christoph Lameterc09d8752007-05-06 14:49:48 -070016#include <string.h>
17#include <unistd.h>
18#include <stdarg.h>
19#include <getopt.h>
20#include <regex.h>
Christoph Lametera87615b2007-05-09 02:32:37 -070021#include <errno.h>
Christoph Lameterc09d8752007-05-06 14:49:48 -070022
23#define MAX_SLABS 500
24#define MAX_ALIASES 500
25#define MAX_NODES 1024
26
27struct slabinfo {
28 char *name;
29 int alias;
30 int refs;
31 int aliases, align, cache_dma, cpu_slabs, destroy_by_rcu;
32 int hwcache_align, object_size, objs_per_slab;
33 int sanity_checks, slab_size, store_user, trace;
34 int order, poison, reclaim_account, red_zone;
Christoph Lameter205ab992008-04-14 19:11:40 +030035 unsigned long partial, objects, slabs, objects_partial, objects_total;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -080036 unsigned long alloc_fastpath, alloc_slowpath;
37 unsigned long free_fastpath, free_slowpath;
38 unsigned long free_frozen, free_add_partial, free_remove_partial;
39 unsigned long alloc_from_partial, alloc_slab, free_slab, alloc_refill;
40 unsigned long cpuslab_flush, deactivate_full, deactivate_empty;
41 unsigned long deactivate_to_head, deactivate_to_tail;
Christoph Lameterf715e6f2008-04-29 16:14:46 -070042 unsigned long deactivate_remote_frees, order_fallback;
Christoph Lameter9da47142011-06-01 12:26:00 -050043 unsigned long cmpxchg_double_cpu_fail, cmpxchg_double_fail;
44 unsigned long alloc_node_mismatch, deactivate_bypass;
Christoph Lameteraca726a2011-08-09 16:12:28 -050045 unsigned long cpu_partial_alloc, cpu_partial_free;
Christoph Lameterc09d8752007-05-06 14:49:48 -070046 int numa[MAX_NODES];
47 int numa_partial[MAX_NODES];
48} slabinfo[MAX_SLABS];
49
50struct aliasinfo {
51 char *name;
52 char *ref;
53 struct slabinfo *slab;
54} aliasinfo[MAX_ALIASES];
55
56int slabs = 0;
Christoph Lametera87615b2007-05-09 02:32:37 -070057int actual_slabs = 0;
Christoph Lameterc09d8752007-05-06 14:49:48 -070058int aliases = 0;
59int alias_targets = 0;
60int highest_node = 0;
61
62char buffer[4096];
63
Christoph Lametera87615b2007-05-09 02:32:37 -070064int show_empty = 0;
65int show_report = 0;
Christoph Lameterc09d8752007-05-06 14:49:48 -070066int show_alias = 0;
67int show_slab = 0;
68int skip_zero = 1;
69int show_numa = 0;
70int show_track = 0;
71int show_first_alias = 0;
72int validate = 0;
73int shrink = 0;
74int show_inverted = 0;
75int show_single_ref = 0;
76int show_totals = 0;
77int sort_size = 0;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -080078int sort_active = 0;
Christoph Lametera87615b2007-05-09 02:32:37 -070079int set_debug = 0;
80int show_ops = 0;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -080081int show_activity = 0;
Sergey Senozhatsky4980a962015-11-05 18:45:20 -080082int output_lines = -1;
Christoph Lametera87615b2007-05-09 02:32:37 -070083
84/* Debug options */
85int sanity = 0;
86int redzone = 0;
87int poison = 0;
88int tracking = 0;
89int tracing = 0;
Christoph Lameterc09d8752007-05-06 14:49:48 -070090
91int page_size;
92
93regex_t pattern;
94
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -070095static void fatal(const char *x, ...)
Christoph Lameterc09d8752007-05-06 14:49:48 -070096{
97 va_list ap;
98
99 va_start(ap, x);
100 vfprintf(stderr, x, ap);
101 va_end(ap);
WANG Congf32143a2007-10-16 23:31:29 -0700102 exit(EXIT_FAILURE);
Christoph Lameterc09d8752007-05-06 14:49:48 -0700103}
104
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700105static void usage(void)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700106{
Christoph Lameter9da47142011-06-01 12:26:00 -0500107 printf("slabinfo 4/15/2011. (c) 2007 sgi/(c) 2011 Linux Foundation.\n\n"
Christoph Lametera87615b2007-05-09 02:32:37 -0700108 "slabinfo [-ahnpvtsz] [-d debugopts] [slab-regexp]\n"
Christoph Lameterc09d8752007-05-06 14:49:48 -0700109 "-a|--aliases Show aliases\n"
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800110 "-A|--activity Most active slabs first\n"
Christoph Lametera87615b2007-05-09 02:32:37 -0700111 "-d<options>|--debug=<options> Set/Clear Debug options\n"
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800112 "-D|--display-active Switch line format to activity\n"
113 "-e|--empty Show empty slabs\n"
Christoph Lametera87615b2007-05-09 02:32:37 -0700114 "-f|--first-alias Show first alias\n"
Christoph Lameterc09d8752007-05-06 14:49:48 -0700115 "-h|--help Show usage information\n"
Christoph Lametera87615b2007-05-09 02:32:37 -0700116 "-i|--inverted Inverted list\n"
117 "-l|--slabs Show slabs\n"
Christoph Lameterc09d8752007-05-06 14:49:48 -0700118 "-n|--numa Show NUMA information\n"
Christoph Lametera87615b2007-05-09 02:32:37 -0700119 "-o|--ops Show kmem_cache_ops\n"
Christoph Lameterc09d8752007-05-06 14:49:48 -0700120 "-s|--shrink Shrink slabs\n"
Christoph Lametera87615b2007-05-09 02:32:37 -0700121 "-r|--report Detailed report on single slabs\n"
122 "-S|--Size Sort by size\n"
Christoph Lameterc09d8752007-05-06 14:49:48 -0700123 "-t|--tracking Show alloc/free information\n"
124 "-T|--Totals Show summary information\n"
Christoph Lametera87615b2007-05-09 02:32:37 -0700125 "-v|--validate Validate slabs\n"
Christoph Lameterc09d8752007-05-06 14:49:48 -0700126 "-z|--zero Include empty slabs\n"
Christoph Lameterc09d8752007-05-06 14:49:48 -0700127 "-1|--1ref Single reference\n"
Sergey Senozhatsky4980a962015-11-05 18:45:20 -0800128 "-N|--lines=K Show the first K slabs\n"
Christoph Lametera87615b2007-05-09 02:32:37 -0700129 "\nValid debug options (FZPUT may be combined)\n"
130 "a / A Switch on all debug options (=FZUP)\n"
131 "- Switch off all debug options\n"
132 "f / F Sanity Checks (SLAB_DEBUG_FREE)\n"
133 "z / Z Redzoning\n"
134 "p / P Poisoning\n"
135 "u / U Tracking\n"
136 "t / T Tracing\n"
Christoph Lameterc09d8752007-05-06 14:49:48 -0700137 );
138}
139
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700140static unsigned long read_obj(const char *name)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700141{
142 FILE *f = fopen(name, "r");
143
144 if (!f)
145 buffer[0] = 0;
146 else {
WANG Congf32143a2007-10-16 23:31:29 -0700147 if (!fgets(buffer, sizeof(buffer), f))
Christoph Lameterc09d8752007-05-06 14:49:48 -0700148 buffer[0] = 0;
149 fclose(f);
150 if (buffer[strlen(buffer)] == '\n')
151 buffer[strlen(buffer)] = 0;
152 }
153 return strlen(buffer);
154}
155
156
157/*
158 * Get the contents of an attribute
159 */
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700160static unsigned long get_obj(const char *name)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700161{
162 if (!read_obj(name))
163 return 0;
164
165 return atol(buffer);
166}
167
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700168static unsigned long get_obj_and_str(const char *name, char **x)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700169{
170 unsigned long result = 0;
171 char *p;
172
173 *x = NULL;
174
175 if (!read_obj(name)) {
176 x = NULL;
177 return 0;
178 }
179 result = strtoul(buffer, &p, 10);
180 while (*p == ' ')
181 p++;
182 if (*p)
183 *x = strdup(p);
184 return result;
185}
186
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700187static void set_obj(struct slabinfo *s, const char *name, int n)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700188{
189 char x[100];
Christoph Lametera87615b2007-05-09 02:32:37 -0700190 FILE *f;
Christoph Lameterc09d8752007-05-06 14:49:48 -0700191
WANG Congf32143a2007-10-16 23:31:29 -0700192 snprintf(x, 100, "%s/%s", s->name, name);
Christoph Lametera87615b2007-05-09 02:32:37 -0700193 f = fopen(x, "w");
Christoph Lameterc09d8752007-05-06 14:49:48 -0700194 if (!f)
195 fatal("Cannot write to %s\n", x);
196
197 fprintf(f, "%d\n", n);
198 fclose(f);
199}
200
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700201static unsigned long read_slab_obj(struct slabinfo *s, const char *name)
Christoph Lametera87615b2007-05-09 02:32:37 -0700202{
203 char x[100];
204 FILE *f;
WANG Congf32143a2007-10-16 23:31:29 -0700205 size_t l;
Christoph Lametera87615b2007-05-09 02:32:37 -0700206
WANG Congf32143a2007-10-16 23:31:29 -0700207 snprintf(x, 100, "%s/%s", s->name, name);
Christoph Lametera87615b2007-05-09 02:32:37 -0700208 f = fopen(x, "r");
209 if (!f) {
210 buffer[0] = 0;
211 l = 0;
212 } else {
213 l = fread(buffer, 1, sizeof(buffer), f);
214 buffer[l] = 0;
215 fclose(f);
216 }
217 return l;
218}
219
220
Christoph Lameterc09d8752007-05-06 14:49:48 -0700221/*
222 * Put a size string together
223 */
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700224static int store_size(char *buffer, unsigned long value)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700225{
226 unsigned long divisor = 1;
227 char trailer = 0;
228 int n;
229
230 if (value > 1000000000UL) {
231 divisor = 100000000UL;
232 trailer = 'G';
233 } else if (value > 1000000UL) {
234 divisor = 100000UL;
235 trailer = 'M';
236 } else if (value > 1000UL) {
237 divisor = 100;
238 trailer = 'K';
239 }
240
241 value /= divisor;
242 n = sprintf(buffer, "%ld",value);
243 if (trailer) {
244 buffer[n] = trailer;
245 n++;
246 buffer[n] = 0;
247 }
248 if (divisor != 1) {
249 memmove(buffer + n - 2, buffer + n - 3, 4);
250 buffer[n-2] = '.';
251 n++;
252 }
253 return n;
254}
255
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700256static void decode_numa_list(int *numa, char *t)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700257{
258 int node;
259 int nr;
260
261 memset(numa, 0, MAX_NODES * sizeof(int));
262
Christoph Lametereefaca92007-05-16 22:10:55 -0700263 if (!t)
264 return;
265
Christoph Lameterc09d8752007-05-06 14:49:48 -0700266 while (*t == 'N') {
267 t++;
268 node = strtoul(t, &t, 10);
269 if (*t == '=') {
270 t++;
271 nr = strtoul(t, &t, 10);
272 numa[node] = nr;
273 if (node > highest_node)
274 highest_node = node;
275 }
276 while (*t == ' ')
277 t++;
278 }
279}
280
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700281static void slab_validate(struct slabinfo *s)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700282{
Christoph Lameter32f93062007-05-18 00:36:43 -0700283 if (strcmp(s->name, "*") == 0)
284 return;
285
Christoph Lameterc09d8752007-05-06 14:49:48 -0700286 set_obj(s, "validate", 1);
287}
288
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700289static void slab_shrink(struct slabinfo *s)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700290{
Christoph Lameter32f93062007-05-18 00:36:43 -0700291 if (strcmp(s->name, "*") == 0)
292 return;
293
Christoph Lameterc09d8752007-05-06 14:49:48 -0700294 set_obj(s, "shrink", 1);
295}
296
297int line = 0;
298
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700299static void first_line(void)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700300{
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800301 if (show_activity)
Christoph Lameter9da47142011-06-01 12:26:00 -0500302 printf("Name Objects Alloc Free %%Fast Fallb O CmpX UL\n");
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800303 else
304 printf("Name Objects Objsize Space "
305 "Slabs/Part/Cpu O/S O %%Fr %%Ef Flg\n");
Christoph Lameterc09d8752007-05-06 14:49:48 -0700306}
307
308/*
309 * Find the shortest alias of a slab
310 */
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700311static struct aliasinfo *find_one_alias(struct slabinfo *find)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700312{
313 struct aliasinfo *a;
314 struct aliasinfo *best = NULL;
315
316 for(a = aliasinfo;a < aliasinfo + aliases; a++) {
317 if (a->slab == find &&
318 (!best || strlen(best->name) < strlen(a->name))) {
319 best = a;
320 if (strncmp(a->name,"kmall", 5) == 0)
321 return best;
322 }
323 }
Christoph Lametera87615b2007-05-09 02:32:37 -0700324 return best;
Christoph Lameterc09d8752007-05-06 14:49:48 -0700325}
326
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700327static unsigned long slab_size(struct slabinfo *s)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700328{
329 return s->slabs * (page_size << s->order);
330}
331
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700332static unsigned long slab_activity(struct slabinfo *s)
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800333{
334 return s->alloc_fastpath + s->free_fastpath +
335 s->alloc_slowpath + s->free_slowpath;
336}
337
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700338static void slab_numa(struct slabinfo *s, int mode)
Christoph Lametera87615b2007-05-09 02:32:37 -0700339{
340 int node;
341
342 if (strcmp(s->name, "*") == 0)
343 return;
344
345 if (!highest_node) {
346 printf("\n%s: No NUMA information available.\n", s->name);
347 return;
348 }
349
350 if (skip_zero && !s->slabs)
351 return;
352
353 if (!line) {
354 printf("\n%-21s:", mode ? "NUMA nodes" : "Slab");
355 for(node = 0; node <= highest_node; node++)
356 printf(" %4d", node);
357 printf("\n----------------------");
358 for(node = 0; node <= highest_node; node++)
359 printf("-----");
360 printf("\n");
361 }
362 printf("%-21s ", mode ? "All slabs" : s->name);
363 for(node = 0; node <= highest_node; node++) {
364 char b[20];
365
366 store_size(b, s->numa[node]);
367 printf(" %4s", b);
368 }
369 printf("\n");
370 if (mode) {
371 printf("%-21s ", "Partial slabs");
372 for(node = 0; node <= highest_node; node++) {
373 char b[20];
374
375 store_size(b, s->numa_partial[node]);
376 printf(" %4s", b);
377 }
378 printf("\n");
379 }
380 line++;
381}
382
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700383static void show_tracking(struct slabinfo *s)
Christoph Lametera87615b2007-05-09 02:32:37 -0700384{
385 printf("\n%s: Kernel object allocation\n", s->name);
386 printf("-----------------------------------------------------------------------\n");
387 if (read_slab_obj(s, "alloc_calls"))
Christoph Lameter9da47142011-06-01 12:26:00 -0500388 printf("%s", buffer);
Christoph Lametera87615b2007-05-09 02:32:37 -0700389 else
390 printf("No Data\n");
391
392 printf("\n%s: Kernel object freeing\n", s->name);
393 printf("------------------------------------------------------------------------\n");
394 if (read_slab_obj(s, "free_calls"))
Christoph Lameter9da47142011-06-01 12:26:00 -0500395 printf("%s", buffer);
Christoph Lametera87615b2007-05-09 02:32:37 -0700396 else
397 printf("No Data\n");
398
399}
400
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700401static void ops(struct slabinfo *s)
Christoph Lametera87615b2007-05-09 02:32:37 -0700402{
403 if (strcmp(s->name, "*") == 0)
404 return;
405
406 if (read_slab_obj(s, "ops")) {
407 printf("\n%s: kmem_cache operations\n", s->name);
408 printf("--------------------------------------------\n");
Christoph Lameter9da47142011-06-01 12:26:00 -0500409 printf("%s", buffer);
Christoph Lametera87615b2007-05-09 02:32:37 -0700410 } else
411 printf("\n%s has no kmem_cache operations\n", s->name);
412}
413
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700414static const char *onoff(int x)
Christoph Lametera87615b2007-05-09 02:32:37 -0700415{
416 if (x)
417 return "On ";
418 return "Off";
419}
420
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700421static void slab_stats(struct slabinfo *s)
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800422{
423 unsigned long total_alloc;
424 unsigned long total_free;
425 unsigned long total;
426
427 if (!s->alloc_slab)
428 return;
429
430 total_alloc = s->alloc_fastpath + s->alloc_slowpath;
431 total_free = s->free_fastpath + s->free_slowpath;
432
433 if (!total_alloc)
434 return;
435
436 printf("\n");
437 printf("Slab Perf Counter Alloc Free %%Al %%Fr\n");
438 printf("--------------------------------------------------\n");
439 printf("Fastpath %8lu %8lu %3lu %3lu\n",
440 s->alloc_fastpath, s->free_fastpath,
441 s->alloc_fastpath * 100 / total_alloc,
majianpeng4b57ad92012-06-26 09:30:31 +0800442 total_free ? s->free_fastpath * 100 / total_free : 0);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800443 printf("Slowpath %8lu %8lu %3lu %3lu\n",
444 total_alloc - s->alloc_fastpath, s->free_slowpath,
445 (total_alloc - s->alloc_fastpath) * 100 / total_alloc,
majianpeng4b57ad92012-06-26 09:30:31 +0800446 total_free ? s->free_slowpath * 100 / total_free : 0);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800447 printf("Page Alloc %8lu %8lu %3lu %3lu\n",
448 s->alloc_slab, s->free_slab,
449 s->alloc_slab * 100 / total_alloc,
majianpeng4b57ad92012-06-26 09:30:31 +0800450 total_free ? s->free_slab * 100 / total_free : 0);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800451 printf("Add partial %8lu %8lu %3lu %3lu\n",
452 s->deactivate_to_head + s->deactivate_to_tail,
453 s->free_add_partial,
454 (s->deactivate_to_head + s->deactivate_to_tail) * 100 / total_alloc,
majianpeng4b57ad92012-06-26 09:30:31 +0800455 total_free ? s->free_add_partial * 100 / total_free : 0);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800456 printf("Remove partial %8lu %8lu %3lu %3lu\n",
457 s->alloc_from_partial, s->free_remove_partial,
458 s->alloc_from_partial * 100 / total_alloc,
majianpeng4b57ad92012-06-26 09:30:31 +0800459 total_free ? s->free_remove_partial * 100 / total_free : 0);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800460
Christoph Lameteraca726a2011-08-09 16:12:28 -0500461 printf("Cpu partial list %8lu %8lu %3lu %3lu\n",
462 s->cpu_partial_alloc, s->cpu_partial_free,
463 s->cpu_partial_alloc * 100 / total_alloc,
majianpeng4b57ad92012-06-26 09:30:31 +0800464 total_free ? s->cpu_partial_free * 100 / total_free : 0);
Christoph Lameteraca726a2011-08-09 16:12:28 -0500465
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800466 printf("RemoteObj/SlabFrozen %8lu %8lu %3lu %3lu\n",
467 s->deactivate_remote_frees, s->free_frozen,
468 s->deactivate_remote_frees * 100 / total_alloc,
majianpeng4b57ad92012-06-26 09:30:31 +0800469 total_free ? s->free_frozen * 100 / total_free : 0);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800470
471 printf("Total %8lu %8lu\n\n", total_alloc, total_free);
472
473 if (s->cpuslab_flush)
474 printf("Flushes %8lu\n", s->cpuslab_flush);
475
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800476 total = s->deactivate_full + s->deactivate_empty +
Christoph Lameter9da47142011-06-01 12:26:00 -0500477 s->deactivate_to_head + s->deactivate_to_tail + s->deactivate_bypass;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800478
Christoph Lameter9da47142011-06-01 12:26:00 -0500479 if (total) {
480 printf("\nSlab Deactivation Ocurrences %%\n");
481 printf("-------------------------------------------------\n");
482 printf("Slab full %7lu %3lu%%\n",
483 s->deactivate_full, (s->deactivate_full * 100) / total);
484 printf("Slab empty %7lu %3lu%%\n",
485 s->deactivate_empty, (s->deactivate_empty * 100) / total);
486 printf("Moved to head of partial list %7lu %3lu%%\n",
487 s->deactivate_to_head, (s->deactivate_to_head * 100) / total);
488 printf("Moved to tail of partial list %7lu %3lu%%\n",
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800489 s->deactivate_to_tail, (s->deactivate_to_tail * 100) / total);
Christoph Lameter9da47142011-06-01 12:26:00 -0500490 printf("Deactivation bypass %7lu %3lu%%\n",
491 s->deactivate_bypass, (s->deactivate_bypass * 100) / total);
492 printf("Refilled from foreign frees %7lu %3lu%%\n",
493 s->alloc_refill, (s->alloc_refill * 100) / total);
494 printf("Node mismatch %7lu %3lu%%\n",
495 s->alloc_node_mismatch, (s->alloc_node_mismatch * 100) / total);
496 }
497
498 if (s->cmpxchg_double_fail || s->cmpxchg_double_cpu_fail)
499 printf("\nCmpxchg_double Looping\n------------------------\n");
500 printf("Locked Cmpxchg Double redos %lu\nUnlocked Cmpxchg Double redos %lu\n",
501 s->cmpxchg_double_fail, s->cmpxchg_double_cpu_fail);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800502}
503
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700504static void report(struct slabinfo *s)
Christoph Lametera87615b2007-05-09 02:32:37 -0700505{
506 if (strcmp(s->name, "*") == 0)
507 return;
Christoph Lametereefaca92007-05-16 22:10:55 -0700508
Jesper Juhlac078602007-08-08 16:31:30 -0700509 printf("\nSlabcache: %-20s Aliases: %2d Order : %2d Objects: %lu\n",
Christoph Lametereefaca92007-05-16 22:10:55 -0700510 s->name, s->aliases, s->order, s->objects);
Christoph Lametera87615b2007-05-09 02:32:37 -0700511 if (s->hwcache_align)
512 printf("** Hardware cacheline aligned\n");
513 if (s->cache_dma)
514 printf("** Memory is allocated in a special DMA zone\n");
515 if (s->destroy_by_rcu)
516 printf("** Slabs are destroyed via RCU\n");
517 if (s->reclaim_account)
518 printf("** Reclaim accounting active\n");
519
520 printf("\nSizes (bytes) Slabs Debug Memory\n");
521 printf("------------------------------------------------------------------------\n");
522 printf("Object : %7d Total : %7ld Sanity Checks : %s Total: %7ld\n",
523 s->object_size, s->slabs, onoff(s->sanity_checks),
524 s->slabs * (page_size << s->order));
525 printf("SlabObj: %7d Full : %7ld Redzoning : %s Used : %7ld\n",
526 s->slab_size, s->slabs - s->partial - s->cpu_slabs,
527 onoff(s->red_zone), s->objects * s->object_size);
528 printf("SlabSiz: %7d Partial: %7ld Poisoning : %s Loss : %7ld\n",
529 page_size << s->order, s->partial, onoff(s->poison),
530 s->slabs * (page_size << s->order) - s->objects * s->object_size);
531 printf("Loss : %7d CpuSlab: %7d Tracking : %s Lalig: %7ld\n",
532 s->slab_size - s->object_size, s->cpu_slabs, onoff(s->store_user),
533 (s->slab_size - s->object_size) * s->objects);
534 printf("Align : %7d Objects: %7d Tracing : %s Lpadd: %7ld\n",
535 s->align, s->objs_per_slab, onoff(s->trace),
536 ((page_size << s->order) - s->objs_per_slab * s->slab_size) *
537 s->slabs);
538
539 ops(s);
540 show_tracking(s);
541 slab_numa(s, 1);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800542 slab_stats(s);
Christoph Lametera87615b2007-05-09 02:32:37 -0700543}
Christoph Lameterc09d8752007-05-06 14:49:48 -0700544
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700545static void slabcache(struct slabinfo *s)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700546{
547 char size_str[20];
548 char dist_str[40];
549 char flags[20];
550 char *p = flags;
551
Christoph Lametera87615b2007-05-09 02:32:37 -0700552 if (strcmp(s->name, "*") == 0)
553 return;
554
555 if (actual_slabs == 1) {
556 report(s);
557 return;
558 }
559
560 if (skip_zero && !show_empty && !s->slabs)
561 return;
562
563 if (show_empty && s->slabs)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700564 return;
565
566 store_size(size_str, slab_size(s));
Christoph Lameter205ab992008-04-14 19:11:40 +0300567 snprintf(dist_str, 40, "%lu/%lu/%d", s->slabs - s->cpu_slabs,
568 s->partial, s->cpu_slabs);
Christoph Lameterc09d8752007-05-06 14:49:48 -0700569
570 if (!line++)
571 first_line();
572
573 if (s->aliases)
574 *p++ = '*';
575 if (s->cache_dma)
576 *p++ = 'd';
577 if (s->hwcache_align)
578 *p++ = 'A';
579 if (s->poison)
580 *p++ = 'P';
581 if (s->reclaim_account)
582 *p++ = 'a';
583 if (s->red_zone)
584 *p++ = 'Z';
585 if (s->sanity_checks)
586 *p++ = 'F';
587 if (s->store_user)
588 *p++ = 'U';
589 if (s->trace)
590 *p++ = 'T';
591
592 *p = 0;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800593 if (show_activity) {
594 unsigned long total_alloc;
595 unsigned long total_free;
596
597 total_alloc = s->alloc_fastpath + s->alloc_slowpath;
598 total_free = s->free_fastpath + s->free_slowpath;
599
Christoph Lameter9da47142011-06-01 12:26:00 -0500600 printf("%-21s %8ld %10ld %10ld %3ld %3ld %5ld %1d %4ld %4ld\n",
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800601 s->name, s->objects,
602 total_alloc, total_free,
603 total_alloc ? (s->alloc_fastpath * 100 / total_alloc) : 0,
Christoph Lameterf715e6f2008-04-29 16:14:46 -0700604 total_free ? (s->free_fastpath * 100 / total_free) : 0,
Christoph Lameter9da47142011-06-01 12:26:00 -0500605 s->order_fallback, s->order, s->cmpxchg_double_fail,
606 s->cmpxchg_double_cpu_fail);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800607 }
608 else
609 printf("%-21s %8ld %7d %8s %14s %4d %1d %3ld %3ld %s\n",
610 s->name, s->objects, s->object_size, size_str, dist_str,
611 s->objs_per_slab, s->order,
612 s->slabs ? (s->partial * 100) / s->slabs : 100,
613 s->slabs ? (s->objects * s->object_size * 100) /
614 (s->slabs * (page_size << s->order)) : 100,
615 flags);
Christoph Lameterc09d8752007-05-06 14:49:48 -0700616}
617
Christoph Lametera87615b2007-05-09 02:32:37 -0700618/*
619 * Analyze debug options. Return false if something is amiss.
620 */
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700621static int debug_opt_scan(char *opt)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700622{
Christoph Lametera87615b2007-05-09 02:32:37 -0700623 if (!opt || !opt[0] || strcmp(opt, "-") == 0)
624 return 1;
Christoph Lameterc09d8752007-05-06 14:49:48 -0700625
Christoph Lametera87615b2007-05-09 02:32:37 -0700626 if (strcasecmp(opt, "a") == 0) {
627 sanity = 1;
628 poison = 1;
629 redzone = 1;
630 tracking = 1;
631 return 1;
Christoph Lameterc09d8752007-05-06 14:49:48 -0700632 }
Christoph Lameterc09d8752007-05-06 14:49:48 -0700633
Christoph Lametera87615b2007-05-09 02:32:37 -0700634 for ( ; *opt; opt++)
Christoph Lameter0d24db32010-10-21 13:01:56 -0500635 switch (*opt) {
Christoph Lametera87615b2007-05-09 02:32:37 -0700636 case 'F' : case 'f':
637 if (sanity)
638 return 0;
639 sanity = 1;
640 break;
641 case 'P' : case 'p':
642 if (poison)
643 return 0;
644 poison = 1;
645 break;
646
647 case 'Z' : case 'z':
648 if (redzone)
649 return 0;
650 redzone = 1;
651 break;
652
653 case 'U' : case 'u':
654 if (tracking)
655 return 0;
656 tracking = 1;
657 break;
658
659 case 'T' : case 't':
660 if (tracing)
661 return 0;
662 tracing = 1;
663 break;
664 default:
665 return 0;
666 }
667 return 1;
Christoph Lameterc09d8752007-05-06 14:49:48 -0700668}
669
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700670static int slab_empty(struct slabinfo *s)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700671{
Christoph Lametera87615b2007-05-09 02:32:37 -0700672 if (s->objects > 0)
673 return 0;
Christoph Lameterc09d8752007-05-06 14:49:48 -0700674
Christoph Lametera87615b2007-05-09 02:32:37 -0700675 /*
676 * We may still have slabs even if there are no objects. Shrinking will
677 * remove them.
678 */
679 if (s->slabs != 0)
680 set_obj(s, "shrink", 1);
Christoph Lameterc09d8752007-05-06 14:49:48 -0700681
Christoph Lametera87615b2007-05-09 02:32:37 -0700682 return 1;
683}
684
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700685static void slab_debug(struct slabinfo *s)
Christoph Lametera87615b2007-05-09 02:32:37 -0700686{
Christoph Lameter32f93062007-05-18 00:36:43 -0700687 if (strcmp(s->name, "*") == 0)
688 return;
689
Christoph Lametera87615b2007-05-09 02:32:37 -0700690 if (sanity && !s->sanity_checks) {
691 set_obj(s, "sanity", 1);
692 }
693 if (!sanity && s->sanity_checks) {
694 if (slab_empty(s))
695 set_obj(s, "sanity", 0);
696 else
697 fprintf(stderr, "%s not empty cannot disable sanity checks\n", s->name);
698 }
699 if (redzone && !s->red_zone) {
700 if (slab_empty(s))
701 set_obj(s, "red_zone", 1);
702 else
703 fprintf(stderr, "%s not empty cannot enable redzoning\n", s->name);
704 }
705 if (!redzone && s->red_zone) {
706 if (slab_empty(s))
707 set_obj(s, "red_zone", 0);
708 else
709 fprintf(stderr, "%s not empty cannot disable redzoning\n", s->name);
710 }
711 if (poison && !s->poison) {
712 if (slab_empty(s))
713 set_obj(s, "poison", 1);
714 else
715 fprintf(stderr, "%s not empty cannot enable poisoning\n", s->name);
716 }
717 if (!poison && s->poison) {
718 if (slab_empty(s))
719 set_obj(s, "poison", 0);
720 else
721 fprintf(stderr, "%s not empty cannot disable poisoning\n", s->name);
722 }
723 if (tracking && !s->store_user) {
724 if (slab_empty(s))
725 set_obj(s, "store_user", 1);
726 else
727 fprintf(stderr, "%s not empty cannot enable tracking\n", s->name);
728 }
729 if (!tracking && s->store_user) {
730 if (slab_empty(s))
731 set_obj(s, "store_user", 0);
732 else
733 fprintf(stderr, "%s not empty cannot disable tracking\n", s->name);
734 }
735 if (tracing && !s->trace) {
736 if (slabs == 1)
737 set_obj(s, "trace", 1);
738 else
739 fprintf(stderr, "%s can only enable trace for one slab at a time\n", s->name);
740 }
741 if (!tracing && s->trace)
742 set_obj(s, "trace", 1);
Christoph Lameterc09d8752007-05-06 14:49:48 -0700743}
744
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700745static void totals(void)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700746{
747 struct slabinfo *s;
748
749 int used_slabs = 0;
750 char b1[20], b2[20], b3[20], b4[20];
751 unsigned long long max = 1ULL << 63;
752
753 /* Object size */
754 unsigned long long min_objsize = max, max_objsize = 0, avg_objsize;
755
756 /* Number of partial slabs in a slabcache */
757 unsigned long long min_partial = max, max_partial = 0,
758 avg_partial, total_partial = 0;
759
760 /* Number of slabs in a slab cache */
761 unsigned long long min_slabs = max, max_slabs = 0,
762 avg_slabs, total_slabs = 0;
763
764 /* Size of the whole slab */
765 unsigned long long min_size = max, max_size = 0,
766 avg_size, total_size = 0;
767
768 /* Bytes used for object storage in a slab */
769 unsigned long long min_used = max, max_used = 0,
770 avg_used, total_used = 0;
771
772 /* Waste: Bytes used for alignment and padding */
773 unsigned long long min_waste = max, max_waste = 0,
774 avg_waste, total_waste = 0;
775 /* Number of objects in a slab */
776 unsigned long long min_objects = max, max_objects = 0,
777 avg_objects, total_objects = 0;
778 /* Waste per object */
779 unsigned long long min_objwaste = max,
780 max_objwaste = 0, avg_objwaste,
781 total_objwaste = 0;
782
783 /* Memory per object */
784 unsigned long long min_memobj = max,
785 max_memobj = 0, avg_memobj,
786 total_objsize = 0;
787
788 /* Percentage of partial slabs per slab */
789 unsigned long min_ppart = 100, max_ppart = 0,
790 avg_ppart, total_ppart = 0;
791
792 /* Number of objects in partial slabs */
793 unsigned long min_partobj = max, max_partobj = 0,
794 avg_partobj, total_partobj = 0;
795
796 /* Percentage of partial objects of all objects in a slab */
797 unsigned long min_ppartobj = 100, max_ppartobj = 0,
798 avg_ppartobj, total_ppartobj = 0;
799
800
801 for (s = slabinfo; s < slabinfo + slabs; s++) {
802 unsigned long long size;
803 unsigned long used;
804 unsigned long long wasted;
805 unsigned long long objwaste;
Christoph Lameterc09d8752007-05-06 14:49:48 -0700806 unsigned long percentage_partial_slabs;
807 unsigned long percentage_partial_objs;
808
809 if (!s->slabs || !s->objects)
810 continue;
811
812 used_slabs++;
813
814 size = slab_size(s);
815 used = s->objects * s->object_size;
816 wasted = size - used;
817 objwaste = s->slab_size - s->object_size;
818
Christoph Lameterc09d8752007-05-06 14:49:48 -0700819 percentage_partial_slabs = s->partial * 100 / s->slabs;
820 if (percentage_partial_slabs > 100)
821 percentage_partial_slabs = 100;
822
Christoph Lameter205ab992008-04-14 19:11:40 +0300823 percentage_partial_objs = s->objects_partial * 100
Christoph Lameterc09d8752007-05-06 14:49:48 -0700824 / s->objects;
825
826 if (percentage_partial_objs > 100)
827 percentage_partial_objs = 100;
828
829 if (s->object_size < min_objsize)
830 min_objsize = s->object_size;
831 if (s->partial < min_partial)
832 min_partial = s->partial;
833 if (s->slabs < min_slabs)
834 min_slabs = s->slabs;
835 if (size < min_size)
836 min_size = size;
837 if (wasted < min_waste)
838 min_waste = wasted;
839 if (objwaste < min_objwaste)
840 min_objwaste = objwaste;
841 if (s->objects < min_objects)
842 min_objects = s->objects;
843 if (used < min_used)
844 min_used = used;
Christoph Lameter205ab992008-04-14 19:11:40 +0300845 if (s->objects_partial < min_partobj)
846 min_partobj = s->objects_partial;
Christoph Lameterc09d8752007-05-06 14:49:48 -0700847 if (percentage_partial_slabs < min_ppart)
848 min_ppart = percentage_partial_slabs;
849 if (percentage_partial_objs < min_ppartobj)
850 min_ppartobj = percentage_partial_objs;
851 if (s->slab_size < min_memobj)
852 min_memobj = s->slab_size;
853
854 if (s->object_size > max_objsize)
855 max_objsize = s->object_size;
856 if (s->partial > max_partial)
857 max_partial = s->partial;
858 if (s->slabs > max_slabs)
859 max_slabs = s->slabs;
860 if (size > max_size)
861 max_size = size;
862 if (wasted > max_waste)
863 max_waste = wasted;
864 if (objwaste > max_objwaste)
865 max_objwaste = objwaste;
866 if (s->objects > max_objects)
867 max_objects = s->objects;
868 if (used > max_used)
869 max_used = used;
Christoph Lameter205ab992008-04-14 19:11:40 +0300870 if (s->objects_partial > max_partobj)
871 max_partobj = s->objects_partial;
Christoph Lameterc09d8752007-05-06 14:49:48 -0700872 if (percentage_partial_slabs > max_ppart)
873 max_ppart = percentage_partial_slabs;
874 if (percentage_partial_objs > max_ppartobj)
875 max_ppartobj = percentage_partial_objs;
876 if (s->slab_size > max_memobj)
877 max_memobj = s->slab_size;
878
879 total_partial += s->partial;
880 total_slabs += s->slabs;
881 total_size += size;
882 total_waste += wasted;
883
884 total_objects += s->objects;
885 total_used += used;
Christoph Lameter205ab992008-04-14 19:11:40 +0300886 total_partobj += s->objects_partial;
Christoph Lameterc09d8752007-05-06 14:49:48 -0700887 total_ppart += percentage_partial_slabs;
888 total_ppartobj += percentage_partial_objs;
889
890 total_objwaste += s->objects * objwaste;
891 total_objsize += s->objects * s->slab_size;
892 }
893
894 if (!total_objects) {
895 printf("No objects\n");
896 return;
897 }
898 if (!used_slabs) {
899 printf("No slabs\n");
900 return;
901 }
902
903 /* Per slab averages */
904 avg_partial = total_partial / used_slabs;
905 avg_slabs = total_slabs / used_slabs;
906 avg_size = total_size / used_slabs;
907 avg_waste = total_waste / used_slabs;
908
909 avg_objects = total_objects / used_slabs;
910 avg_used = total_used / used_slabs;
911 avg_partobj = total_partobj / used_slabs;
912 avg_ppart = total_ppart / used_slabs;
913 avg_ppartobj = total_ppartobj / used_slabs;
914
915 /* Per object object sizes */
916 avg_objsize = total_used / total_objects;
917 avg_objwaste = total_objwaste / total_objects;
918 avg_partobj = total_partobj * 100 / total_objects;
919 avg_memobj = total_objsize / total_objects;
920
921 printf("Slabcache Totals\n");
922 printf("----------------\n");
923 printf("Slabcaches : %3d Aliases : %3d->%-3d Active: %3d\n",
924 slabs, aliases, alias_targets, used_slabs);
925
926 store_size(b1, total_size);store_size(b2, total_waste);
927 store_size(b3, total_waste * 100 / total_used);
Christoph Lametereefaca92007-05-16 22:10:55 -0700928 printf("Memory used: %6s # Loss : %6s MRatio:%6s%%\n", b1, b2, b3);
Christoph Lameterc09d8752007-05-06 14:49:48 -0700929
930 store_size(b1, total_objects);store_size(b2, total_partobj);
931 store_size(b3, total_partobj * 100 / total_objects);
Christoph Lametereefaca92007-05-16 22:10:55 -0700932 printf("# Objects : %6s # PartObj: %6s ORatio:%6s%%\n", b1, b2, b3);
Christoph Lameterc09d8752007-05-06 14:49:48 -0700933
934 printf("\n");
935 printf("Per Cache Average Min Max Total\n");
936 printf("---------------------------------------------------------\n");
937
938 store_size(b1, avg_objects);store_size(b2, min_objects);
939 store_size(b3, max_objects);store_size(b4, total_objects);
940 printf("#Objects %10s %10s %10s %10s\n",
941 b1, b2, b3, b4);
942
943 store_size(b1, avg_slabs);store_size(b2, min_slabs);
944 store_size(b3, max_slabs);store_size(b4, total_slabs);
945 printf("#Slabs %10s %10s %10s %10s\n",
946 b1, b2, b3, b4);
947
948 store_size(b1, avg_partial);store_size(b2, min_partial);
949 store_size(b3, max_partial);store_size(b4, total_partial);
950 printf("#PartSlab %10s %10s %10s %10s\n",
951 b1, b2, b3, b4);
952 store_size(b1, avg_ppart);store_size(b2, min_ppart);
953 store_size(b3, max_ppart);
954 store_size(b4, total_partial * 100 / total_slabs);
Christoph Lametereefaca92007-05-16 22:10:55 -0700955 printf("%%PartSlab%10s%% %10s%% %10s%% %10s%%\n",
Christoph Lameterc09d8752007-05-06 14:49:48 -0700956 b1, b2, b3, b4);
957
958 store_size(b1, avg_partobj);store_size(b2, min_partobj);
959 store_size(b3, max_partobj);
960 store_size(b4, total_partobj);
961 printf("PartObjs %10s %10s %10s %10s\n",
962 b1, b2, b3, b4);
963
964 store_size(b1, avg_ppartobj);store_size(b2, min_ppartobj);
965 store_size(b3, max_ppartobj);
966 store_size(b4, total_partobj * 100 / total_objects);
Christoph Lametereefaca92007-05-16 22:10:55 -0700967 printf("%% PartObj%10s%% %10s%% %10s%% %10s%%\n",
Christoph Lameterc09d8752007-05-06 14:49:48 -0700968 b1, b2, b3, b4);
969
970 store_size(b1, avg_size);store_size(b2, min_size);
971 store_size(b3, max_size);store_size(b4, total_size);
972 printf("Memory %10s %10s %10s %10s\n",
973 b1, b2, b3, b4);
974
975 store_size(b1, avg_used);store_size(b2, min_used);
976 store_size(b3, max_used);store_size(b4, total_used);
977 printf("Used %10s %10s %10s %10s\n",
978 b1, b2, b3, b4);
979
980 store_size(b1, avg_waste);store_size(b2, min_waste);
981 store_size(b3, max_waste);store_size(b4, total_waste);
982 printf("Loss %10s %10s %10s %10s\n",
983 b1, b2, b3, b4);
984
985 printf("\n");
986 printf("Per Object Average Min Max\n");
987 printf("---------------------------------------------\n");
988
989 store_size(b1, avg_memobj);store_size(b2, min_memobj);
990 store_size(b3, max_memobj);
991 printf("Memory %10s %10s %10s\n",
992 b1, b2, b3);
993 store_size(b1, avg_objsize);store_size(b2, min_objsize);
994 store_size(b3, max_objsize);
995 printf("User %10s %10s %10s\n",
996 b1, b2, b3);
997
998 store_size(b1, avg_objwaste);store_size(b2, min_objwaste);
999 store_size(b3, max_objwaste);
1000 printf("Loss %10s %10s %10s\n",
1001 b1, b2, b3);
1002}
1003
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -07001004static void sort_slabs(void)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001005{
1006 struct slabinfo *s1,*s2;
1007
1008 for (s1 = slabinfo; s1 < slabinfo + slabs; s1++) {
1009 for (s2 = s1 + 1; s2 < slabinfo + slabs; s2++) {
1010 int result;
1011
1012 if (sort_size)
1013 result = slab_size(s1) < slab_size(s2);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001014 else if (sort_active)
1015 result = slab_activity(s1) < slab_activity(s2);
Christoph Lameterc09d8752007-05-06 14:49:48 -07001016 else
1017 result = strcasecmp(s1->name, s2->name);
1018
1019 if (show_inverted)
1020 result = -result;
1021
1022 if (result > 0) {
1023 struct slabinfo t;
1024
1025 memcpy(&t, s1, sizeof(struct slabinfo));
1026 memcpy(s1, s2, sizeof(struct slabinfo));
1027 memcpy(s2, &t, sizeof(struct slabinfo));
1028 }
1029 }
1030 }
1031}
1032
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -07001033static void sort_aliases(void)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001034{
1035 struct aliasinfo *a1,*a2;
1036
1037 for (a1 = aliasinfo; a1 < aliasinfo + aliases; a1++) {
1038 for (a2 = a1 + 1; a2 < aliasinfo + aliases; a2++) {
1039 char *n1, *n2;
1040
1041 n1 = a1->name;
1042 n2 = a2->name;
1043 if (show_alias && !show_inverted) {
1044 n1 = a1->ref;
1045 n2 = a2->ref;
1046 }
1047 if (strcasecmp(n1, n2) > 0) {
1048 struct aliasinfo t;
1049
1050 memcpy(&t, a1, sizeof(struct aliasinfo));
1051 memcpy(a1, a2, sizeof(struct aliasinfo));
1052 memcpy(a2, &t, sizeof(struct aliasinfo));
1053 }
1054 }
1055 }
1056}
1057
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -07001058static void link_slabs(void)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001059{
1060 struct aliasinfo *a;
1061 struct slabinfo *s;
1062
1063 for (a = aliasinfo; a < aliasinfo + aliases; a++) {
1064
Christoph Lametera87615b2007-05-09 02:32:37 -07001065 for (s = slabinfo; s < slabinfo + slabs; s++)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001066 if (strcmp(a->ref, s->name) == 0) {
1067 a->slab = s;
1068 s->refs++;
1069 break;
1070 }
1071 if (s == slabinfo + slabs)
1072 fatal("Unresolved alias %s\n", a->ref);
1073 }
1074}
1075
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -07001076static void alias(void)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001077{
1078 struct aliasinfo *a;
1079 char *active = NULL;
1080
1081 sort_aliases();
1082 link_slabs();
1083
1084 for(a = aliasinfo; a < aliasinfo + aliases; a++) {
1085
1086 if (!show_single_ref && a->slab->refs == 1)
1087 continue;
1088
1089 if (!show_inverted) {
1090 if (active) {
1091 if (strcmp(a->slab->name, active) == 0) {
1092 printf(" %s", a->name);
1093 continue;
1094 }
1095 }
Christoph Lametera87615b2007-05-09 02:32:37 -07001096 printf("\n%-12s <- %s", a->slab->name, a->name);
Christoph Lameterc09d8752007-05-06 14:49:48 -07001097 active = a->slab->name;
1098 }
1099 else
1100 printf("%-20s -> %s\n", a->name, a->slab->name);
1101 }
1102 if (active)
1103 printf("\n");
1104}
1105
1106
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -07001107static void rename_slabs(void)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001108{
1109 struct slabinfo *s;
1110 struct aliasinfo *a;
1111
1112 for (s = slabinfo; s < slabinfo + slabs; s++) {
1113 if (*s->name != ':')
1114 continue;
1115
1116 if (s->refs > 1 && !show_first_alias)
1117 continue;
1118
1119 a = find_one_alias(s);
1120
Christoph Lametera87615b2007-05-09 02:32:37 -07001121 if (a)
1122 s->name = a->name;
1123 else {
1124 s->name = "*";
1125 actual_slabs--;
1126 }
Christoph Lameterc09d8752007-05-06 14:49:48 -07001127 }
1128}
1129
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -07001130static int slab_mismatch(char *slab)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001131{
1132 return regexec(&pattern, slab, 0, NULL, 0);
1133}
1134
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -07001135static void read_slab_dir(void)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001136{
1137 DIR *dir;
1138 struct dirent *de;
1139 struct slabinfo *slab = slabinfo;
1140 struct aliasinfo *alias = aliasinfo;
1141 char *p;
1142 char *t;
1143 int count;
1144
Christoph Lameterb6c24de2008-02-04 23:35:48 -08001145 if (chdir("/sys/kernel/slab") && chdir("/sys/slab"))
Christoph Lametera87615b2007-05-09 02:32:37 -07001146 fatal("SYSFS support for SLUB not active\n");
1147
Christoph Lameterc09d8752007-05-06 14:49:48 -07001148 dir = opendir(".");
1149 while ((de = readdir(dir))) {
1150 if (de->d_name[0] == '.' ||
Christoph Lametera87615b2007-05-09 02:32:37 -07001151 (de->d_name[0] != ':' && slab_mismatch(de->d_name)))
1152 continue;
Christoph Lameterc09d8752007-05-06 14:49:48 -07001153 switch (de->d_type) {
1154 case DT_LNK:
Christoph Lameter0d24db32010-10-21 13:01:56 -05001155 alias->name = strdup(de->d_name);
Thomas Jaroschfe353172011-10-17 16:48:10 +02001156 count = readlink(de->d_name, buffer, sizeof(buffer)-1);
Christoph Lameterc09d8752007-05-06 14:49:48 -07001157
1158 if (count < 0)
1159 fatal("Cannot read symlink %s\n", de->d_name);
1160
1161 buffer[count] = 0;
1162 p = buffer + count;
1163 while (p > buffer && p[-1] != '/')
1164 p--;
1165 alias->ref = strdup(p);
1166 alias++;
1167 break;
1168 case DT_DIR:
1169 if (chdir(de->d_name))
1170 fatal("Unable to access slab %s\n", slab->name);
Christoph Lameter0d24db32010-10-21 13:01:56 -05001171 slab->name = strdup(de->d_name);
Christoph Lameterc09d8752007-05-06 14:49:48 -07001172 slab->alias = 0;
1173 slab->refs = 0;
1174 slab->aliases = get_obj("aliases");
1175 slab->align = get_obj("align");
1176 slab->cache_dma = get_obj("cache_dma");
1177 slab->cpu_slabs = get_obj("cpu_slabs");
1178 slab->destroy_by_rcu = get_obj("destroy_by_rcu");
1179 slab->hwcache_align = get_obj("hwcache_align");
1180 slab->object_size = get_obj("object_size");
1181 slab->objects = get_obj("objects");
Christoph Lameter205ab992008-04-14 19:11:40 +03001182 slab->objects_partial = get_obj("objects_partial");
1183 slab->objects_total = get_obj("objects_total");
Christoph Lameterc09d8752007-05-06 14:49:48 -07001184 slab->objs_per_slab = get_obj("objs_per_slab");
1185 slab->order = get_obj("order");
1186 slab->partial = get_obj("partial");
1187 slab->partial = get_obj_and_str("partial", &t);
1188 decode_numa_list(slab->numa_partial, t);
WANG Congf32143a2007-10-16 23:31:29 -07001189 free(t);
Christoph Lameterc09d8752007-05-06 14:49:48 -07001190 slab->poison = get_obj("poison");
1191 slab->reclaim_account = get_obj("reclaim_account");
1192 slab->red_zone = get_obj("red_zone");
1193 slab->sanity_checks = get_obj("sanity_checks");
1194 slab->slab_size = get_obj("slab_size");
1195 slab->slabs = get_obj_and_str("slabs", &t);
1196 decode_numa_list(slab->numa, t);
WANG Congf32143a2007-10-16 23:31:29 -07001197 free(t);
Christoph Lameterc09d8752007-05-06 14:49:48 -07001198 slab->store_user = get_obj("store_user");
1199 slab->trace = get_obj("trace");
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001200 slab->alloc_fastpath = get_obj("alloc_fastpath");
1201 slab->alloc_slowpath = get_obj("alloc_slowpath");
1202 slab->free_fastpath = get_obj("free_fastpath");
1203 slab->free_slowpath = get_obj("free_slowpath");
1204 slab->free_frozen= get_obj("free_frozen");
1205 slab->free_add_partial = get_obj("free_add_partial");
1206 slab->free_remove_partial = get_obj("free_remove_partial");
1207 slab->alloc_from_partial = get_obj("alloc_from_partial");
1208 slab->alloc_slab = get_obj("alloc_slab");
1209 slab->alloc_refill = get_obj("alloc_refill");
1210 slab->free_slab = get_obj("free_slab");
1211 slab->cpuslab_flush = get_obj("cpuslab_flush");
1212 slab->deactivate_full = get_obj("deactivate_full");
1213 slab->deactivate_empty = get_obj("deactivate_empty");
1214 slab->deactivate_to_head = get_obj("deactivate_to_head");
1215 slab->deactivate_to_tail = get_obj("deactivate_to_tail");
1216 slab->deactivate_remote_frees = get_obj("deactivate_remote_frees");
Christoph Lameterf715e6f2008-04-29 16:14:46 -07001217 slab->order_fallback = get_obj("order_fallback");
Christoph Lameter9da47142011-06-01 12:26:00 -05001218 slab->cmpxchg_double_cpu_fail = get_obj("cmpxchg_double_cpu_fail");
1219 slab->cmpxchg_double_fail = get_obj("cmpxchg_double_fail");
Christoph Lameteraca726a2011-08-09 16:12:28 -05001220 slab->cpu_partial_alloc = get_obj("cpu_partial_alloc");
1221 slab->cpu_partial_free = get_obj("cpu_partial_free");
Christoph Lameter9da47142011-06-01 12:26:00 -05001222 slab->alloc_node_mismatch = get_obj("alloc_node_mismatch");
1223 slab->deactivate_bypass = get_obj("deactivate_bypass");
Christoph Lameterc09d8752007-05-06 14:49:48 -07001224 chdir("..");
1225 if (slab->name[0] == ':')
1226 alias_targets++;
1227 slab++;
1228 break;
1229 default :
1230 fatal("Unknown file type %lx\n", de->d_type);
1231 }
1232 }
1233 closedir(dir);
1234 slabs = slab - slabinfo;
Christoph Lametera87615b2007-05-09 02:32:37 -07001235 actual_slabs = slabs;
Christoph Lameterc09d8752007-05-06 14:49:48 -07001236 aliases = alias - aliasinfo;
1237 if (slabs > MAX_SLABS)
1238 fatal("Too many slabs\n");
1239 if (aliases > MAX_ALIASES)
1240 fatal("Too many aliases\n");
1241}
1242
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -07001243static void output_slabs(void)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001244{
1245 struct slabinfo *slab;
1246
Sergey Senozhatsky4980a962015-11-05 18:45:20 -08001247 for (slab = slabinfo; (slab < slabinfo + slabs) &&
1248 output_lines != 0; slab++) {
Christoph Lameterc09d8752007-05-06 14:49:48 -07001249
1250 if (slab->alias)
1251 continue;
1252
Sergey Senozhatsky4980a962015-11-05 18:45:20 -08001253 if (output_lines != -1)
1254 output_lines--;
Christoph Lameterc09d8752007-05-06 14:49:48 -07001255
1256 if (show_numa)
Christoph Lametera87615b2007-05-09 02:32:37 -07001257 slab_numa(slab, 0);
1258 else if (show_track)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001259 show_tracking(slab);
Christoph Lametera87615b2007-05-09 02:32:37 -07001260 else if (validate)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001261 slab_validate(slab);
Christoph Lametera87615b2007-05-09 02:32:37 -07001262 else if (shrink)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001263 slab_shrink(slab);
Christoph Lametera87615b2007-05-09 02:32:37 -07001264 else if (set_debug)
1265 slab_debug(slab);
1266 else if (show_ops)
1267 ops(slab);
1268 else if (show_slab)
1269 slabcache(slab);
Christoph Lametereefaca92007-05-16 22:10:55 -07001270 else if (show_report)
1271 report(slab);
Christoph Lameterc09d8752007-05-06 14:49:48 -07001272 }
1273}
1274
1275struct option opts[] = {
Sergey Senozhatsky2b100752015-11-05 18:45:17 -08001276 { "aliases", no_argument, NULL, 'a' },
1277 { "activity", no_argument, NULL, 'A' },
1278 { "debug", optional_argument, NULL, 'd' },
1279 { "display-activity", no_argument, NULL, 'D' },
1280 { "empty", no_argument, NULL, 'e' },
1281 { "first-alias", no_argument, NULL, 'f' },
1282 { "help", no_argument, NULL, 'h' },
1283 { "inverted", no_argument, NULL, 'i'},
1284 { "numa", no_argument, NULL, 'n' },
1285 { "ops", no_argument, NULL, 'o' },
1286 { "report", no_argument, NULL, 'r' },
1287 { "shrink", no_argument, NULL, 's' },
1288 { "slabs", no_argument, NULL, 'l' },
1289 { "track", no_argument, NULL, 't'},
1290 { "validate", no_argument, NULL, 'v' },
1291 { "zero", no_argument, NULL, 'z' },
1292 { "1ref", no_argument, NULL, '1'},
Sergey Senozhatsky4980a962015-11-05 18:45:20 -08001293 { "lines", required_argument, NULL, 'N'},
Christoph Lameterc09d8752007-05-06 14:49:48 -07001294 { NULL, 0, NULL, 0 }
1295};
1296
1297int main(int argc, char *argv[])
1298{
1299 int c;
1300 int err;
1301 char *pattern_source;
1302
1303 page_size = getpagesize();
Christoph Lameterc09d8752007-05-06 14:49:48 -07001304
Sergey Senozhatsky4980a962015-11-05 18:45:20 -08001305 while ((c = getopt_long(argc, argv, "aAd::Defhil1noprstvzTSN:",
Christoph Lametera87615b2007-05-09 02:32:37 -07001306 opts, NULL)) != -1)
WANG Congf32143a2007-10-16 23:31:29 -07001307 switch (c) {
Christoph Lameterc09d8752007-05-06 14:49:48 -07001308 case '1':
1309 show_single_ref = 1;
1310 break;
1311 case 'a':
1312 show_alias = 1;
1313 break;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001314 case 'A':
1315 sort_active = 1;
1316 break;
Christoph Lametera87615b2007-05-09 02:32:37 -07001317 case 'd':
1318 set_debug = 1;
1319 if (!debug_opt_scan(optarg))
1320 fatal("Invalid debug option '%s'\n", optarg);
1321 break;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001322 case 'D':
1323 show_activity = 1;
1324 break;
Christoph Lametera87615b2007-05-09 02:32:37 -07001325 case 'e':
1326 show_empty = 1;
1327 break;
Christoph Lameterc09d8752007-05-06 14:49:48 -07001328 case 'f':
1329 show_first_alias = 1;
1330 break;
1331 case 'h':
1332 usage();
1333 return 0;
1334 case 'i':
1335 show_inverted = 1;
1336 break;
1337 case 'n':
1338 show_numa = 1;
1339 break;
Christoph Lametera87615b2007-05-09 02:32:37 -07001340 case 'o':
1341 show_ops = 1;
1342 break;
1343 case 'r':
1344 show_report = 1;
1345 break;
Christoph Lameterc09d8752007-05-06 14:49:48 -07001346 case 's':
1347 shrink = 1;
1348 break;
1349 case 'l':
1350 show_slab = 1;
1351 break;
1352 case 't':
1353 show_track = 1;
1354 break;
1355 case 'v':
1356 validate = 1;
1357 break;
1358 case 'z':
1359 skip_zero = 0;
1360 break;
1361 case 'T':
1362 show_totals = 1;
1363 break;
1364 case 'S':
1365 sort_size = 1;
1366 break;
Sergey Senozhatsky4980a962015-11-05 18:45:20 -08001367 case 'N':
1368 if (optarg) {
1369 output_lines = atoi(optarg);
1370 if (output_lines < 1)
1371 output_lines = 1;
1372 }
1373 break;
Christoph Lameterc09d8752007-05-06 14:49:48 -07001374 default:
1375 fatal("%s: Invalid option '%c'\n", argv[0], optopt);
1376
1377 }
1378
Christoph Lametera87615b2007-05-09 02:32:37 -07001379 if (!show_slab && !show_alias && !show_track && !show_report
1380 && !validate && !shrink && !set_debug && !show_ops)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001381 show_slab = 1;
1382
1383 if (argc > optind)
1384 pattern_source = argv[optind];
1385 else
1386 pattern_source = ".*";
1387
1388 err = regcomp(&pattern, pattern_source, REG_ICASE|REG_NOSUB);
1389 if (err)
1390 fatal("%s: Invalid pattern '%s' code %d\n",
1391 argv[0], pattern_source, err);
1392 read_slab_dir();
1393 if (show_alias)
1394 alias();
1395 else
1396 if (show_totals)
1397 totals();
1398 else {
1399 link_slabs();
1400 rename_slabs();
1401 sort_slabs();
1402 output_slabs();
1403 }
1404 return 0;
1405}