blob: 808d5a9d5dcfb062a5dc9d9fc10246cb6e7e00d2 [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;
Christoph Lametera87615b2007-05-09 02:32:37 -070082
83/* Debug options */
84int sanity = 0;
85int redzone = 0;
86int poison = 0;
87int tracking = 0;
88int tracing = 0;
Christoph Lameterc09d8752007-05-06 14:49:48 -070089
90int page_size;
91
92regex_t pattern;
93
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -070094static void fatal(const char *x, ...)
Christoph Lameterc09d8752007-05-06 14:49:48 -070095{
96 va_list ap;
97
98 va_start(ap, x);
99 vfprintf(stderr, x, ap);
100 va_end(ap);
WANG Congf32143a2007-10-16 23:31:29 -0700101 exit(EXIT_FAILURE);
Christoph Lameterc09d8752007-05-06 14:49:48 -0700102}
103
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700104static void usage(void)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700105{
Christoph Lameter9da47142011-06-01 12:26:00 -0500106 printf("slabinfo 4/15/2011. (c) 2007 sgi/(c) 2011 Linux Foundation.\n\n"
Christoph Lametera87615b2007-05-09 02:32:37 -0700107 "slabinfo [-ahnpvtsz] [-d debugopts] [slab-regexp]\n"
Christoph Lameterc09d8752007-05-06 14:49:48 -0700108 "-a|--aliases Show aliases\n"
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800109 "-A|--activity Most active slabs first\n"
Christoph Lametera87615b2007-05-09 02:32:37 -0700110 "-d<options>|--debug=<options> Set/Clear Debug options\n"
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800111 "-D|--display-active Switch line format to activity\n"
112 "-e|--empty Show empty slabs\n"
Christoph Lametera87615b2007-05-09 02:32:37 -0700113 "-f|--first-alias Show first alias\n"
Christoph Lameterc09d8752007-05-06 14:49:48 -0700114 "-h|--help Show usage information\n"
Christoph Lametera87615b2007-05-09 02:32:37 -0700115 "-i|--inverted Inverted list\n"
116 "-l|--slabs Show slabs\n"
Christoph Lameterc09d8752007-05-06 14:49:48 -0700117 "-n|--numa Show NUMA information\n"
Christoph Lametera87615b2007-05-09 02:32:37 -0700118 "-o|--ops Show kmem_cache_ops\n"
Christoph Lameterc09d8752007-05-06 14:49:48 -0700119 "-s|--shrink Shrink slabs\n"
Christoph Lametera87615b2007-05-09 02:32:37 -0700120 "-r|--report Detailed report on single slabs\n"
121 "-S|--Size Sort by size\n"
Christoph Lameterc09d8752007-05-06 14:49:48 -0700122 "-t|--tracking Show alloc/free information\n"
123 "-T|--Totals Show summary information\n"
Christoph Lametera87615b2007-05-09 02:32:37 -0700124 "-v|--validate Validate slabs\n"
Christoph Lameterc09d8752007-05-06 14:49:48 -0700125 "-z|--zero Include empty slabs\n"
Christoph Lameterc09d8752007-05-06 14:49:48 -0700126 "-1|--1ref Single reference\n"
Christoph Lametera87615b2007-05-09 02:32:37 -0700127 "\nValid debug options (FZPUT may be combined)\n"
128 "a / A Switch on all debug options (=FZUP)\n"
129 "- Switch off all debug options\n"
130 "f / F Sanity Checks (SLAB_DEBUG_FREE)\n"
131 "z / Z Redzoning\n"
132 "p / P Poisoning\n"
133 "u / U Tracking\n"
134 "t / T Tracing\n"
Christoph Lameterc09d8752007-05-06 14:49:48 -0700135 );
136}
137
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700138static unsigned long read_obj(const char *name)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700139{
140 FILE *f = fopen(name, "r");
141
142 if (!f)
143 buffer[0] = 0;
144 else {
WANG Congf32143a2007-10-16 23:31:29 -0700145 if (!fgets(buffer, sizeof(buffer), f))
Christoph Lameterc09d8752007-05-06 14:49:48 -0700146 buffer[0] = 0;
147 fclose(f);
148 if (buffer[strlen(buffer)] == '\n')
149 buffer[strlen(buffer)] = 0;
150 }
151 return strlen(buffer);
152}
153
154
155/*
156 * Get the contents of an attribute
157 */
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700158static unsigned long get_obj(const char *name)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700159{
160 if (!read_obj(name))
161 return 0;
162
163 return atol(buffer);
164}
165
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700166static unsigned long get_obj_and_str(const char *name, char **x)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700167{
168 unsigned long result = 0;
169 char *p;
170
171 *x = NULL;
172
173 if (!read_obj(name)) {
174 x = NULL;
175 return 0;
176 }
177 result = strtoul(buffer, &p, 10);
178 while (*p == ' ')
179 p++;
180 if (*p)
181 *x = strdup(p);
182 return result;
183}
184
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700185static void set_obj(struct slabinfo *s, const char *name, int n)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700186{
187 char x[100];
Christoph Lametera87615b2007-05-09 02:32:37 -0700188 FILE *f;
Christoph Lameterc09d8752007-05-06 14:49:48 -0700189
WANG Congf32143a2007-10-16 23:31:29 -0700190 snprintf(x, 100, "%s/%s", s->name, name);
Christoph Lametera87615b2007-05-09 02:32:37 -0700191 f = fopen(x, "w");
Christoph Lameterc09d8752007-05-06 14:49:48 -0700192 if (!f)
193 fatal("Cannot write to %s\n", x);
194
195 fprintf(f, "%d\n", n);
196 fclose(f);
197}
198
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700199static unsigned long read_slab_obj(struct slabinfo *s, const char *name)
Christoph Lametera87615b2007-05-09 02:32:37 -0700200{
201 char x[100];
202 FILE *f;
WANG Congf32143a2007-10-16 23:31:29 -0700203 size_t l;
Christoph Lametera87615b2007-05-09 02:32:37 -0700204
WANG Congf32143a2007-10-16 23:31:29 -0700205 snprintf(x, 100, "%s/%s", s->name, name);
Christoph Lametera87615b2007-05-09 02:32:37 -0700206 f = fopen(x, "r");
207 if (!f) {
208 buffer[0] = 0;
209 l = 0;
210 } else {
211 l = fread(buffer, 1, sizeof(buffer), f);
212 buffer[l] = 0;
213 fclose(f);
214 }
215 return l;
216}
217
218
Christoph Lameterc09d8752007-05-06 14:49:48 -0700219/*
220 * Put a size string together
221 */
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700222static int store_size(char *buffer, unsigned long value)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700223{
224 unsigned long divisor = 1;
225 char trailer = 0;
226 int n;
227
228 if (value > 1000000000UL) {
229 divisor = 100000000UL;
230 trailer = 'G';
231 } else if (value > 1000000UL) {
232 divisor = 100000UL;
233 trailer = 'M';
234 } else if (value > 1000UL) {
235 divisor = 100;
236 trailer = 'K';
237 }
238
239 value /= divisor;
240 n = sprintf(buffer, "%ld",value);
241 if (trailer) {
242 buffer[n] = trailer;
243 n++;
244 buffer[n] = 0;
245 }
246 if (divisor != 1) {
247 memmove(buffer + n - 2, buffer + n - 3, 4);
248 buffer[n-2] = '.';
249 n++;
250 }
251 return n;
252}
253
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700254static void decode_numa_list(int *numa, char *t)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700255{
256 int node;
257 int nr;
258
259 memset(numa, 0, MAX_NODES * sizeof(int));
260
Christoph Lametereefaca92007-05-16 22:10:55 -0700261 if (!t)
262 return;
263
Christoph Lameterc09d8752007-05-06 14:49:48 -0700264 while (*t == 'N') {
265 t++;
266 node = strtoul(t, &t, 10);
267 if (*t == '=') {
268 t++;
269 nr = strtoul(t, &t, 10);
270 numa[node] = nr;
271 if (node > highest_node)
272 highest_node = node;
273 }
274 while (*t == ' ')
275 t++;
276 }
277}
278
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700279static void slab_validate(struct slabinfo *s)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700280{
Christoph Lameter32f93062007-05-18 00:36:43 -0700281 if (strcmp(s->name, "*") == 0)
282 return;
283
Christoph Lameterc09d8752007-05-06 14:49:48 -0700284 set_obj(s, "validate", 1);
285}
286
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700287static void slab_shrink(struct slabinfo *s)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700288{
Christoph Lameter32f93062007-05-18 00:36:43 -0700289 if (strcmp(s->name, "*") == 0)
290 return;
291
Christoph Lameterc09d8752007-05-06 14:49:48 -0700292 set_obj(s, "shrink", 1);
293}
294
295int line = 0;
296
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700297static void first_line(void)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700298{
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800299 if (show_activity)
Christoph Lameter9da47142011-06-01 12:26:00 -0500300 printf("Name Objects Alloc Free %%Fast Fallb O CmpX UL\n");
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800301 else
302 printf("Name Objects Objsize Space "
303 "Slabs/Part/Cpu O/S O %%Fr %%Ef Flg\n");
Christoph Lameterc09d8752007-05-06 14:49:48 -0700304}
305
306/*
307 * Find the shortest alias of a slab
308 */
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700309static struct aliasinfo *find_one_alias(struct slabinfo *find)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700310{
311 struct aliasinfo *a;
312 struct aliasinfo *best = NULL;
313
314 for(a = aliasinfo;a < aliasinfo + aliases; a++) {
315 if (a->slab == find &&
316 (!best || strlen(best->name) < strlen(a->name))) {
317 best = a;
318 if (strncmp(a->name,"kmall", 5) == 0)
319 return best;
320 }
321 }
Christoph Lametera87615b2007-05-09 02:32:37 -0700322 return best;
Christoph Lameterc09d8752007-05-06 14:49:48 -0700323}
324
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700325static unsigned long slab_size(struct slabinfo *s)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700326{
327 return s->slabs * (page_size << s->order);
328}
329
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700330static unsigned long slab_activity(struct slabinfo *s)
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800331{
332 return s->alloc_fastpath + s->free_fastpath +
333 s->alloc_slowpath + s->free_slowpath;
334}
335
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700336static void slab_numa(struct slabinfo *s, int mode)
Christoph Lametera87615b2007-05-09 02:32:37 -0700337{
338 int node;
339
340 if (strcmp(s->name, "*") == 0)
341 return;
342
343 if (!highest_node) {
344 printf("\n%s: No NUMA information available.\n", s->name);
345 return;
346 }
347
348 if (skip_zero && !s->slabs)
349 return;
350
351 if (!line) {
352 printf("\n%-21s:", mode ? "NUMA nodes" : "Slab");
353 for(node = 0; node <= highest_node; node++)
354 printf(" %4d", node);
355 printf("\n----------------------");
356 for(node = 0; node <= highest_node; node++)
357 printf("-----");
358 printf("\n");
359 }
360 printf("%-21s ", mode ? "All slabs" : s->name);
361 for(node = 0; node <= highest_node; node++) {
362 char b[20];
363
364 store_size(b, s->numa[node]);
365 printf(" %4s", b);
366 }
367 printf("\n");
368 if (mode) {
369 printf("%-21s ", "Partial slabs");
370 for(node = 0; node <= highest_node; node++) {
371 char b[20];
372
373 store_size(b, s->numa_partial[node]);
374 printf(" %4s", b);
375 }
376 printf("\n");
377 }
378 line++;
379}
380
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700381static void show_tracking(struct slabinfo *s)
Christoph Lametera87615b2007-05-09 02:32:37 -0700382{
383 printf("\n%s: Kernel object allocation\n", s->name);
384 printf("-----------------------------------------------------------------------\n");
385 if (read_slab_obj(s, "alloc_calls"))
Christoph Lameter9da47142011-06-01 12:26:00 -0500386 printf("%s", buffer);
Christoph Lametera87615b2007-05-09 02:32:37 -0700387 else
388 printf("No Data\n");
389
390 printf("\n%s: Kernel object freeing\n", s->name);
391 printf("------------------------------------------------------------------------\n");
392 if (read_slab_obj(s, "free_calls"))
Christoph Lameter9da47142011-06-01 12:26:00 -0500393 printf("%s", buffer);
Christoph Lametera87615b2007-05-09 02:32:37 -0700394 else
395 printf("No Data\n");
396
397}
398
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700399static void ops(struct slabinfo *s)
Christoph Lametera87615b2007-05-09 02:32:37 -0700400{
401 if (strcmp(s->name, "*") == 0)
402 return;
403
404 if (read_slab_obj(s, "ops")) {
405 printf("\n%s: kmem_cache operations\n", s->name);
406 printf("--------------------------------------------\n");
Christoph Lameter9da47142011-06-01 12:26:00 -0500407 printf("%s", buffer);
Christoph Lametera87615b2007-05-09 02:32:37 -0700408 } else
409 printf("\n%s has no kmem_cache operations\n", s->name);
410}
411
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700412static const char *onoff(int x)
Christoph Lametera87615b2007-05-09 02:32:37 -0700413{
414 if (x)
415 return "On ";
416 return "Off";
417}
418
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700419static void slab_stats(struct slabinfo *s)
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800420{
421 unsigned long total_alloc;
422 unsigned long total_free;
423 unsigned long total;
424
425 if (!s->alloc_slab)
426 return;
427
428 total_alloc = s->alloc_fastpath + s->alloc_slowpath;
429 total_free = s->free_fastpath + s->free_slowpath;
430
431 if (!total_alloc)
432 return;
433
434 printf("\n");
435 printf("Slab Perf Counter Alloc Free %%Al %%Fr\n");
436 printf("--------------------------------------------------\n");
437 printf("Fastpath %8lu %8lu %3lu %3lu\n",
438 s->alloc_fastpath, s->free_fastpath,
439 s->alloc_fastpath * 100 / total_alloc,
majianpeng4b57ad92012-06-26 09:30:31 +0800440 total_free ? s->free_fastpath * 100 / total_free : 0);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800441 printf("Slowpath %8lu %8lu %3lu %3lu\n",
442 total_alloc - s->alloc_fastpath, s->free_slowpath,
443 (total_alloc - s->alloc_fastpath) * 100 / total_alloc,
majianpeng4b57ad92012-06-26 09:30:31 +0800444 total_free ? s->free_slowpath * 100 / total_free : 0);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800445 printf("Page Alloc %8lu %8lu %3lu %3lu\n",
446 s->alloc_slab, s->free_slab,
447 s->alloc_slab * 100 / total_alloc,
majianpeng4b57ad92012-06-26 09:30:31 +0800448 total_free ? s->free_slab * 100 / total_free : 0);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800449 printf("Add partial %8lu %8lu %3lu %3lu\n",
450 s->deactivate_to_head + s->deactivate_to_tail,
451 s->free_add_partial,
452 (s->deactivate_to_head + s->deactivate_to_tail) * 100 / total_alloc,
majianpeng4b57ad92012-06-26 09:30:31 +0800453 total_free ? s->free_add_partial * 100 / total_free : 0);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800454 printf("Remove partial %8lu %8lu %3lu %3lu\n",
455 s->alloc_from_partial, s->free_remove_partial,
456 s->alloc_from_partial * 100 / total_alloc,
majianpeng4b57ad92012-06-26 09:30:31 +0800457 total_free ? s->free_remove_partial * 100 / total_free : 0);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800458
Christoph Lameteraca726a2011-08-09 16:12:28 -0500459 printf("Cpu partial list %8lu %8lu %3lu %3lu\n",
460 s->cpu_partial_alloc, s->cpu_partial_free,
461 s->cpu_partial_alloc * 100 / total_alloc,
majianpeng4b57ad92012-06-26 09:30:31 +0800462 total_free ? s->cpu_partial_free * 100 / total_free : 0);
Christoph Lameteraca726a2011-08-09 16:12:28 -0500463
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800464 printf("RemoteObj/SlabFrozen %8lu %8lu %3lu %3lu\n",
465 s->deactivate_remote_frees, s->free_frozen,
466 s->deactivate_remote_frees * 100 / total_alloc,
majianpeng4b57ad92012-06-26 09:30:31 +0800467 total_free ? s->free_frozen * 100 / total_free : 0);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800468
469 printf("Total %8lu %8lu\n\n", total_alloc, total_free);
470
471 if (s->cpuslab_flush)
472 printf("Flushes %8lu\n", s->cpuslab_flush);
473
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800474 total = s->deactivate_full + s->deactivate_empty +
Christoph Lameter9da47142011-06-01 12:26:00 -0500475 s->deactivate_to_head + s->deactivate_to_tail + s->deactivate_bypass;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800476
Christoph Lameter9da47142011-06-01 12:26:00 -0500477 if (total) {
478 printf("\nSlab Deactivation Ocurrences %%\n");
479 printf("-------------------------------------------------\n");
480 printf("Slab full %7lu %3lu%%\n",
481 s->deactivate_full, (s->deactivate_full * 100) / total);
482 printf("Slab empty %7lu %3lu%%\n",
483 s->deactivate_empty, (s->deactivate_empty * 100) / total);
484 printf("Moved to head of partial list %7lu %3lu%%\n",
485 s->deactivate_to_head, (s->deactivate_to_head * 100) / total);
486 printf("Moved to tail of partial list %7lu %3lu%%\n",
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800487 s->deactivate_to_tail, (s->deactivate_to_tail * 100) / total);
Christoph Lameter9da47142011-06-01 12:26:00 -0500488 printf("Deactivation bypass %7lu %3lu%%\n",
489 s->deactivate_bypass, (s->deactivate_bypass * 100) / total);
490 printf("Refilled from foreign frees %7lu %3lu%%\n",
491 s->alloc_refill, (s->alloc_refill * 100) / total);
492 printf("Node mismatch %7lu %3lu%%\n",
493 s->alloc_node_mismatch, (s->alloc_node_mismatch * 100) / total);
494 }
495
496 if (s->cmpxchg_double_fail || s->cmpxchg_double_cpu_fail)
497 printf("\nCmpxchg_double Looping\n------------------------\n");
498 printf("Locked Cmpxchg Double redos %lu\nUnlocked Cmpxchg Double redos %lu\n",
499 s->cmpxchg_double_fail, s->cmpxchg_double_cpu_fail);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800500}
501
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700502static void report(struct slabinfo *s)
Christoph Lametera87615b2007-05-09 02:32:37 -0700503{
504 if (strcmp(s->name, "*") == 0)
505 return;
Christoph Lametereefaca92007-05-16 22:10:55 -0700506
Jesper Juhlac078602007-08-08 16:31:30 -0700507 printf("\nSlabcache: %-20s Aliases: %2d Order : %2d Objects: %lu\n",
Christoph Lametereefaca92007-05-16 22:10:55 -0700508 s->name, s->aliases, s->order, s->objects);
Christoph Lametera87615b2007-05-09 02:32:37 -0700509 if (s->hwcache_align)
510 printf("** Hardware cacheline aligned\n");
511 if (s->cache_dma)
512 printf("** Memory is allocated in a special DMA zone\n");
513 if (s->destroy_by_rcu)
514 printf("** Slabs are destroyed via RCU\n");
515 if (s->reclaim_account)
516 printf("** Reclaim accounting active\n");
517
518 printf("\nSizes (bytes) Slabs Debug Memory\n");
519 printf("------------------------------------------------------------------------\n");
520 printf("Object : %7d Total : %7ld Sanity Checks : %s Total: %7ld\n",
521 s->object_size, s->slabs, onoff(s->sanity_checks),
522 s->slabs * (page_size << s->order));
523 printf("SlabObj: %7d Full : %7ld Redzoning : %s Used : %7ld\n",
524 s->slab_size, s->slabs - s->partial - s->cpu_slabs,
525 onoff(s->red_zone), s->objects * s->object_size);
526 printf("SlabSiz: %7d Partial: %7ld Poisoning : %s Loss : %7ld\n",
527 page_size << s->order, s->partial, onoff(s->poison),
528 s->slabs * (page_size << s->order) - s->objects * s->object_size);
529 printf("Loss : %7d CpuSlab: %7d Tracking : %s Lalig: %7ld\n",
530 s->slab_size - s->object_size, s->cpu_slabs, onoff(s->store_user),
531 (s->slab_size - s->object_size) * s->objects);
532 printf("Align : %7d Objects: %7d Tracing : %s Lpadd: %7ld\n",
533 s->align, s->objs_per_slab, onoff(s->trace),
534 ((page_size << s->order) - s->objs_per_slab * s->slab_size) *
535 s->slabs);
536
537 ops(s);
538 show_tracking(s);
539 slab_numa(s, 1);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800540 slab_stats(s);
Christoph Lametera87615b2007-05-09 02:32:37 -0700541}
Christoph Lameterc09d8752007-05-06 14:49:48 -0700542
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700543static void slabcache(struct slabinfo *s)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700544{
545 char size_str[20];
546 char dist_str[40];
547 char flags[20];
548 char *p = flags;
549
Christoph Lametera87615b2007-05-09 02:32:37 -0700550 if (strcmp(s->name, "*") == 0)
551 return;
552
553 if (actual_slabs == 1) {
554 report(s);
555 return;
556 }
557
558 if (skip_zero && !show_empty && !s->slabs)
559 return;
560
561 if (show_empty && s->slabs)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700562 return;
563
564 store_size(size_str, slab_size(s));
Christoph Lameter205ab992008-04-14 19:11:40 +0300565 snprintf(dist_str, 40, "%lu/%lu/%d", s->slabs - s->cpu_slabs,
566 s->partial, s->cpu_slabs);
Christoph Lameterc09d8752007-05-06 14:49:48 -0700567
568 if (!line++)
569 first_line();
570
571 if (s->aliases)
572 *p++ = '*';
573 if (s->cache_dma)
574 *p++ = 'd';
575 if (s->hwcache_align)
576 *p++ = 'A';
577 if (s->poison)
578 *p++ = 'P';
579 if (s->reclaim_account)
580 *p++ = 'a';
581 if (s->red_zone)
582 *p++ = 'Z';
583 if (s->sanity_checks)
584 *p++ = 'F';
585 if (s->store_user)
586 *p++ = 'U';
587 if (s->trace)
588 *p++ = 'T';
589
590 *p = 0;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800591 if (show_activity) {
592 unsigned long total_alloc;
593 unsigned long total_free;
594
595 total_alloc = s->alloc_fastpath + s->alloc_slowpath;
596 total_free = s->free_fastpath + s->free_slowpath;
597
Christoph Lameter9da47142011-06-01 12:26:00 -0500598 printf("%-21s %8ld %10ld %10ld %3ld %3ld %5ld %1d %4ld %4ld\n",
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800599 s->name, s->objects,
600 total_alloc, total_free,
601 total_alloc ? (s->alloc_fastpath * 100 / total_alloc) : 0,
Christoph Lameterf715e6f2008-04-29 16:14:46 -0700602 total_free ? (s->free_fastpath * 100 / total_free) : 0,
Christoph Lameter9da47142011-06-01 12:26:00 -0500603 s->order_fallback, s->order, s->cmpxchg_double_fail,
604 s->cmpxchg_double_cpu_fail);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800605 }
606 else
607 printf("%-21s %8ld %7d %8s %14s %4d %1d %3ld %3ld %s\n",
608 s->name, s->objects, s->object_size, size_str, dist_str,
609 s->objs_per_slab, s->order,
610 s->slabs ? (s->partial * 100) / s->slabs : 100,
611 s->slabs ? (s->objects * s->object_size * 100) /
612 (s->slabs * (page_size << s->order)) : 100,
613 flags);
Christoph Lameterc09d8752007-05-06 14:49:48 -0700614}
615
Christoph Lametera87615b2007-05-09 02:32:37 -0700616/*
617 * Analyze debug options. Return false if something is amiss.
618 */
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700619static int debug_opt_scan(char *opt)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700620{
Christoph Lametera87615b2007-05-09 02:32:37 -0700621 if (!opt || !opt[0] || strcmp(opt, "-") == 0)
622 return 1;
Christoph Lameterc09d8752007-05-06 14:49:48 -0700623
Christoph Lametera87615b2007-05-09 02:32:37 -0700624 if (strcasecmp(opt, "a") == 0) {
625 sanity = 1;
626 poison = 1;
627 redzone = 1;
628 tracking = 1;
629 return 1;
Christoph Lameterc09d8752007-05-06 14:49:48 -0700630 }
Christoph Lameterc09d8752007-05-06 14:49:48 -0700631
Christoph Lametera87615b2007-05-09 02:32:37 -0700632 for ( ; *opt; opt++)
Christoph Lameter0d24db32010-10-21 13:01:56 -0500633 switch (*opt) {
Christoph Lametera87615b2007-05-09 02:32:37 -0700634 case 'F' : case 'f':
635 if (sanity)
636 return 0;
637 sanity = 1;
638 break;
639 case 'P' : case 'p':
640 if (poison)
641 return 0;
642 poison = 1;
643 break;
644
645 case 'Z' : case 'z':
646 if (redzone)
647 return 0;
648 redzone = 1;
649 break;
650
651 case 'U' : case 'u':
652 if (tracking)
653 return 0;
654 tracking = 1;
655 break;
656
657 case 'T' : case 't':
658 if (tracing)
659 return 0;
660 tracing = 1;
661 break;
662 default:
663 return 0;
664 }
665 return 1;
Christoph Lameterc09d8752007-05-06 14:49:48 -0700666}
667
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700668static int slab_empty(struct slabinfo *s)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700669{
Christoph Lametera87615b2007-05-09 02:32:37 -0700670 if (s->objects > 0)
671 return 0;
Christoph Lameterc09d8752007-05-06 14:49:48 -0700672
Christoph Lametera87615b2007-05-09 02:32:37 -0700673 /*
674 * We may still have slabs even if there are no objects. Shrinking will
675 * remove them.
676 */
677 if (s->slabs != 0)
678 set_obj(s, "shrink", 1);
Christoph Lameterc09d8752007-05-06 14:49:48 -0700679
Christoph Lametera87615b2007-05-09 02:32:37 -0700680 return 1;
681}
682
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700683static void slab_debug(struct slabinfo *s)
Christoph Lametera87615b2007-05-09 02:32:37 -0700684{
Christoph Lameter32f93062007-05-18 00:36:43 -0700685 if (strcmp(s->name, "*") == 0)
686 return;
687
Christoph Lametera87615b2007-05-09 02:32:37 -0700688 if (sanity && !s->sanity_checks) {
689 set_obj(s, "sanity", 1);
690 }
691 if (!sanity && s->sanity_checks) {
692 if (slab_empty(s))
693 set_obj(s, "sanity", 0);
694 else
695 fprintf(stderr, "%s not empty cannot disable sanity checks\n", s->name);
696 }
697 if (redzone && !s->red_zone) {
698 if (slab_empty(s))
699 set_obj(s, "red_zone", 1);
700 else
701 fprintf(stderr, "%s not empty cannot enable redzoning\n", s->name);
702 }
703 if (!redzone && s->red_zone) {
704 if (slab_empty(s))
705 set_obj(s, "red_zone", 0);
706 else
707 fprintf(stderr, "%s not empty cannot disable redzoning\n", s->name);
708 }
709 if (poison && !s->poison) {
710 if (slab_empty(s))
711 set_obj(s, "poison", 1);
712 else
713 fprintf(stderr, "%s not empty cannot enable poisoning\n", s->name);
714 }
715 if (!poison && s->poison) {
716 if (slab_empty(s))
717 set_obj(s, "poison", 0);
718 else
719 fprintf(stderr, "%s not empty cannot disable poisoning\n", s->name);
720 }
721 if (tracking && !s->store_user) {
722 if (slab_empty(s))
723 set_obj(s, "store_user", 1);
724 else
725 fprintf(stderr, "%s not empty cannot enable tracking\n", s->name);
726 }
727 if (!tracking && s->store_user) {
728 if (slab_empty(s))
729 set_obj(s, "store_user", 0);
730 else
731 fprintf(stderr, "%s not empty cannot disable tracking\n", s->name);
732 }
733 if (tracing && !s->trace) {
734 if (slabs == 1)
735 set_obj(s, "trace", 1);
736 else
737 fprintf(stderr, "%s can only enable trace for one slab at a time\n", s->name);
738 }
739 if (!tracing && s->trace)
740 set_obj(s, "trace", 1);
Christoph Lameterc09d8752007-05-06 14:49:48 -0700741}
742
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -0700743static void totals(void)
Christoph Lameterc09d8752007-05-06 14:49:48 -0700744{
745 struct slabinfo *s;
746
747 int used_slabs = 0;
748 char b1[20], b2[20], b3[20], b4[20];
749 unsigned long long max = 1ULL << 63;
750
751 /* Object size */
752 unsigned long long min_objsize = max, max_objsize = 0, avg_objsize;
753
754 /* Number of partial slabs in a slabcache */
755 unsigned long long min_partial = max, max_partial = 0,
756 avg_partial, total_partial = 0;
757
758 /* Number of slabs in a slab cache */
759 unsigned long long min_slabs = max, max_slabs = 0,
760 avg_slabs, total_slabs = 0;
761
762 /* Size of the whole slab */
763 unsigned long long min_size = max, max_size = 0,
764 avg_size, total_size = 0;
765
766 /* Bytes used for object storage in a slab */
767 unsigned long long min_used = max, max_used = 0,
768 avg_used, total_used = 0;
769
770 /* Waste: Bytes used for alignment and padding */
771 unsigned long long min_waste = max, max_waste = 0,
772 avg_waste, total_waste = 0;
773 /* Number of objects in a slab */
774 unsigned long long min_objects = max, max_objects = 0,
775 avg_objects, total_objects = 0;
776 /* Waste per object */
777 unsigned long long min_objwaste = max,
778 max_objwaste = 0, avg_objwaste,
779 total_objwaste = 0;
780
781 /* Memory per object */
782 unsigned long long min_memobj = max,
783 max_memobj = 0, avg_memobj,
784 total_objsize = 0;
785
786 /* Percentage of partial slabs per slab */
787 unsigned long min_ppart = 100, max_ppart = 0,
788 avg_ppart, total_ppart = 0;
789
790 /* Number of objects in partial slabs */
791 unsigned long min_partobj = max, max_partobj = 0,
792 avg_partobj, total_partobj = 0;
793
794 /* Percentage of partial objects of all objects in a slab */
795 unsigned long min_ppartobj = 100, max_ppartobj = 0,
796 avg_ppartobj, total_ppartobj = 0;
797
798
799 for (s = slabinfo; s < slabinfo + slabs; s++) {
800 unsigned long long size;
801 unsigned long used;
802 unsigned long long wasted;
803 unsigned long long objwaste;
Christoph Lameterc09d8752007-05-06 14:49:48 -0700804 unsigned long percentage_partial_slabs;
805 unsigned long percentage_partial_objs;
806
807 if (!s->slabs || !s->objects)
808 continue;
809
810 used_slabs++;
811
812 size = slab_size(s);
813 used = s->objects * s->object_size;
814 wasted = size - used;
815 objwaste = s->slab_size - s->object_size;
816
Christoph Lameterc09d8752007-05-06 14:49:48 -0700817 percentage_partial_slabs = s->partial * 100 / s->slabs;
818 if (percentage_partial_slabs > 100)
819 percentage_partial_slabs = 100;
820
Christoph Lameter205ab992008-04-14 19:11:40 +0300821 percentage_partial_objs = s->objects_partial * 100
Christoph Lameterc09d8752007-05-06 14:49:48 -0700822 / s->objects;
823
824 if (percentage_partial_objs > 100)
825 percentage_partial_objs = 100;
826
827 if (s->object_size < min_objsize)
828 min_objsize = s->object_size;
829 if (s->partial < min_partial)
830 min_partial = s->partial;
831 if (s->slabs < min_slabs)
832 min_slabs = s->slabs;
833 if (size < min_size)
834 min_size = size;
835 if (wasted < min_waste)
836 min_waste = wasted;
837 if (objwaste < min_objwaste)
838 min_objwaste = objwaste;
839 if (s->objects < min_objects)
840 min_objects = s->objects;
841 if (used < min_used)
842 min_used = used;
Christoph Lameter205ab992008-04-14 19:11:40 +0300843 if (s->objects_partial < min_partobj)
844 min_partobj = s->objects_partial;
Christoph Lameterc09d8752007-05-06 14:49:48 -0700845 if (percentage_partial_slabs < min_ppart)
846 min_ppart = percentage_partial_slabs;
847 if (percentage_partial_objs < min_ppartobj)
848 min_ppartobj = percentage_partial_objs;
849 if (s->slab_size < min_memobj)
850 min_memobj = s->slab_size;
851
852 if (s->object_size > max_objsize)
853 max_objsize = s->object_size;
854 if (s->partial > max_partial)
855 max_partial = s->partial;
856 if (s->slabs > max_slabs)
857 max_slabs = s->slabs;
858 if (size > max_size)
859 max_size = size;
860 if (wasted > max_waste)
861 max_waste = wasted;
862 if (objwaste > max_objwaste)
863 max_objwaste = objwaste;
864 if (s->objects > max_objects)
865 max_objects = s->objects;
866 if (used > max_used)
867 max_used = used;
Christoph Lameter205ab992008-04-14 19:11:40 +0300868 if (s->objects_partial > max_partobj)
869 max_partobj = s->objects_partial;
Christoph Lameterc09d8752007-05-06 14:49:48 -0700870 if (percentage_partial_slabs > max_ppart)
871 max_ppart = percentage_partial_slabs;
872 if (percentage_partial_objs > max_ppartobj)
873 max_ppartobj = percentage_partial_objs;
874 if (s->slab_size > max_memobj)
875 max_memobj = s->slab_size;
876
877 total_partial += s->partial;
878 total_slabs += s->slabs;
879 total_size += size;
880 total_waste += wasted;
881
882 total_objects += s->objects;
883 total_used += used;
Christoph Lameter205ab992008-04-14 19:11:40 +0300884 total_partobj += s->objects_partial;
Christoph Lameterc09d8752007-05-06 14:49:48 -0700885 total_ppart += percentage_partial_slabs;
886 total_ppartobj += percentage_partial_objs;
887
888 total_objwaste += s->objects * objwaste;
889 total_objsize += s->objects * s->slab_size;
890 }
891
892 if (!total_objects) {
893 printf("No objects\n");
894 return;
895 }
896 if (!used_slabs) {
897 printf("No slabs\n");
898 return;
899 }
900
901 /* Per slab averages */
902 avg_partial = total_partial / used_slabs;
903 avg_slabs = total_slabs / used_slabs;
904 avg_size = total_size / used_slabs;
905 avg_waste = total_waste / used_slabs;
906
907 avg_objects = total_objects / used_slabs;
908 avg_used = total_used / used_slabs;
909 avg_partobj = total_partobj / used_slabs;
910 avg_ppart = total_ppart / used_slabs;
911 avg_ppartobj = total_ppartobj / used_slabs;
912
913 /* Per object object sizes */
914 avg_objsize = total_used / total_objects;
915 avg_objwaste = total_objwaste / total_objects;
916 avg_partobj = total_partobj * 100 / total_objects;
917 avg_memobj = total_objsize / total_objects;
918
919 printf("Slabcache Totals\n");
920 printf("----------------\n");
921 printf("Slabcaches : %3d Aliases : %3d->%-3d Active: %3d\n",
922 slabs, aliases, alias_targets, used_slabs);
923
924 store_size(b1, total_size);store_size(b2, total_waste);
925 store_size(b3, total_waste * 100 / total_used);
Christoph Lametereefaca92007-05-16 22:10:55 -0700926 printf("Memory used: %6s # Loss : %6s MRatio:%6s%%\n", b1, b2, b3);
Christoph Lameterc09d8752007-05-06 14:49:48 -0700927
928 store_size(b1, total_objects);store_size(b2, total_partobj);
929 store_size(b3, total_partobj * 100 / total_objects);
Christoph Lametereefaca92007-05-16 22:10:55 -0700930 printf("# Objects : %6s # PartObj: %6s ORatio:%6s%%\n", b1, b2, b3);
Christoph Lameterc09d8752007-05-06 14:49:48 -0700931
932 printf("\n");
933 printf("Per Cache Average Min Max Total\n");
934 printf("---------------------------------------------------------\n");
935
936 store_size(b1, avg_objects);store_size(b2, min_objects);
937 store_size(b3, max_objects);store_size(b4, total_objects);
938 printf("#Objects %10s %10s %10s %10s\n",
939 b1, b2, b3, b4);
940
941 store_size(b1, avg_slabs);store_size(b2, min_slabs);
942 store_size(b3, max_slabs);store_size(b4, total_slabs);
943 printf("#Slabs %10s %10s %10s %10s\n",
944 b1, b2, b3, b4);
945
946 store_size(b1, avg_partial);store_size(b2, min_partial);
947 store_size(b3, max_partial);store_size(b4, total_partial);
948 printf("#PartSlab %10s %10s %10s %10s\n",
949 b1, b2, b3, b4);
950 store_size(b1, avg_ppart);store_size(b2, min_ppart);
951 store_size(b3, max_ppart);
952 store_size(b4, total_partial * 100 / total_slabs);
Christoph Lametereefaca92007-05-16 22:10:55 -0700953 printf("%%PartSlab%10s%% %10s%% %10s%% %10s%%\n",
Christoph Lameterc09d8752007-05-06 14:49:48 -0700954 b1, b2, b3, b4);
955
956 store_size(b1, avg_partobj);store_size(b2, min_partobj);
957 store_size(b3, max_partobj);
958 store_size(b4, total_partobj);
959 printf("PartObjs %10s %10s %10s %10s\n",
960 b1, b2, b3, b4);
961
962 store_size(b1, avg_ppartobj);store_size(b2, min_ppartobj);
963 store_size(b3, max_ppartobj);
964 store_size(b4, total_partobj * 100 / total_objects);
Christoph Lametereefaca92007-05-16 22:10:55 -0700965 printf("%% PartObj%10s%% %10s%% %10s%% %10s%%\n",
Christoph Lameterc09d8752007-05-06 14:49:48 -0700966 b1, b2, b3, b4);
967
968 store_size(b1, avg_size);store_size(b2, min_size);
969 store_size(b3, max_size);store_size(b4, total_size);
970 printf("Memory %10s %10s %10s %10s\n",
971 b1, b2, b3, b4);
972
973 store_size(b1, avg_used);store_size(b2, min_used);
974 store_size(b3, max_used);store_size(b4, total_used);
975 printf("Used %10s %10s %10s %10s\n",
976 b1, b2, b3, b4);
977
978 store_size(b1, avg_waste);store_size(b2, min_waste);
979 store_size(b3, max_waste);store_size(b4, total_waste);
980 printf("Loss %10s %10s %10s %10s\n",
981 b1, b2, b3, b4);
982
983 printf("\n");
984 printf("Per Object Average Min Max\n");
985 printf("---------------------------------------------\n");
986
987 store_size(b1, avg_memobj);store_size(b2, min_memobj);
988 store_size(b3, max_memobj);
989 printf("Memory %10s %10s %10s\n",
990 b1, b2, b3);
991 store_size(b1, avg_objsize);store_size(b2, min_objsize);
992 store_size(b3, max_objsize);
993 printf("User %10s %10s %10s\n",
994 b1, b2, b3);
995
996 store_size(b1, avg_objwaste);store_size(b2, min_objwaste);
997 store_size(b3, max_objwaste);
998 printf("Loss %10s %10s %10s\n",
999 b1, b2, b3);
1000}
1001
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -07001002static void sort_slabs(void)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001003{
1004 struct slabinfo *s1,*s2;
1005
1006 for (s1 = slabinfo; s1 < slabinfo + slabs; s1++) {
1007 for (s2 = s1 + 1; s2 < slabinfo + slabs; s2++) {
1008 int result;
1009
1010 if (sort_size)
1011 result = slab_size(s1) < slab_size(s2);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001012 else if (sort_active)
1013 result = slab_activity(s1) < slab_activity(s2);
Christoph Lameterc09d8752007-05-06 14:49:48 -07001014 else
1015 result = strcasecmp(s1->name, s2->name);
1016
1017 if (show_inverted)
1018 result = -result;
1019
1020 if (result > 0) {
1021 struct slabinfo t;
1022
1023 memcpy(&t, s1, sizeof(struct slabinfo));
1024 memcpy(s1, s2, sizeof(struct slabinfo));
1025 memcpy(s2, &t, sizeof(struct slabinfo));
1026 }
1027 }
1028 }
1029}
1030
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -07001031static void sort_aliases(void)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001032{
1033 struct aliasinfo *a1,*a2;
1034
1035 for (a1 = aliasinfo; a1 < aliasinfo + aliases; a1++) {
1036 for (a2 = a1 + 1; a2 < aliasinfo + aliases; a2++) {
1037 char *n1, *n2;
1038
1039 n1 = a1->name;
1040 n2 = a2->name;
1041 if (show_alias && !show_inverted) {
1042 n1 = a1->ref;
1043 n2 = a2->ref;
1044 }
1045 if (strcasecmp(n1, n2) > 0) {
1046 struct aliasinfo t;
1047
1048 memcpy(&t, a1, sizeof(struct aliasinfo));
1049 memcpy(a1, a2, sizeof(struct aliasinfo));
1050 memcpy(a2, &t, sizeof(struct aliasinfo));
1051 }
1052 }
1053 }
1054}
1055
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -07001056static void link_slabs(void)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001057{
1058 struct aliasinfo *a;
1059 struct slabinfo *s;
1060
1061 for (a = aliasinfo; a < aliasinfo + aliases; a++) {
1062
Christoph Lametera87615b2007-05-09 02:32:37 -07001063 for (s = slabinfo; s < slabinfo + slabs; s++)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001064 if (strcmp(a->ref, s->name) == 0) {
1065 a->slab = s;
1066 s->refs++;
1067 break;
1068 }
1069 if (s == slabinfo + slabs)
1070 fatal("Unresolved alias %s\n", a->ref);
1071 }
1072}
1073
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -07001074static void alias(void)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001075{
1076 struct aliasinfo *a;
1077 char *active = NULL;
1078
1079 sort_aliases();
1080 link_slabs();
1081
1082 for(a = aliasinfo; a < aliasinfo + aliases; a++) {
1083
1084 if (!show_single_ref && a->slab->refs == 1)
1085 continue;
1086
1087 if (!show_inverted) {
1088 if (active) {
1089 if (strcmp(a->slab->name, active) == 0) {
1090 printf(" %s", a->name);
1091 continue;
1092 }
1093 }
Christoph Lametera87615b2007-05-09 02:32:37 -07001094 printf("\n%-12s <- %s", a->slab->name, a->name);
Christoph Lameterc09d8752007-05-06 14:49:48 -07001095 active = a->slab->name;
1096 }
1097 else
1098 printf("%-20s -> %s\n", a->name, a->slab->name);
1099 }
1100 if (active)
1101 printf("\n");
1102}
1103
1104
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -07001105static void rename_slabs(void)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001106{
1107 struct slabinfo *s;
1108 struct aliasinfo *a;
1109
1110 for (s = slabinfo; s < slabinfo + slabs; s++) {
1111 if (*s->name != ':')
1112 continue;
1113
1114 if (s->refs > 1 && !show_first_alias)
1115 continue;
1116
1117 a = find_one_alias(s);
1118
Christoph Lametera87615b2007-05-09 02:32:37 -07001119 if (a)
1120 s->name = a->name;
1121 else {
1122 s->name = "*";
1123 actual_slabs--;
1124 }
Christoph Lameterc09d8752007-05-06 14:49:48 -07001125 }
1126}
1127
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -07001128static int slab_mismatch(char *slab)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001129{
1130 return regexec(&pattern, slab, 0, NULL, 0);
1131}
1132
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -07001133static void read_slab_dir(void)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001134{
1135 DIR *dir;
1136 struct dirent *de;
1137 struct slabinfo *slab = slabinfo;
1138 struct aliasinfo *alias = aliasinfo;
1139 char *p;
1140 char *t;
1141 int count;
1142
Christoph Lameterb6c24de2008-02-04 23:35:48 -08001143 if (chdir("/sys/kernel/slab") && chdir("/sys/slab"))
Christoph Lametera87615b2007-05-09 02:32:37 -07001144 fatal("SYSFS support for SLUB not active\n");
1145
Christoph Lameterc09d8752007-05-06 14:49:48 -07001146 dir = opendir(".");
1147 while ((de = readdir(dir))) {
1148 if (de->d_name[0] == '.' ||
Christoph Lametera87615b2007-05-09 02:32:37 -07001149 (de->d_name[0] != ':' && slab_mismatch(de->d_name)))
1150 continue;
Christoph Lameterc09d8752007-05-06 14:49:48 -07001151 switch (de->d_type) {
1152 case DT_LNK:
Christoph Lameter0d24db32010-10-21 13:01:56 -05001153 alias->name = strdup(de->d_name);
Thomas Jaroschfe353172011-10-17 16:48:10 +02001154 count = readlink(de->d_name, buffer, sizeof(buffer)-1);
Christoph Lameterc09d8752007-05-06 14:49:48 -07001155
1156 if (count < 0)
1157 fatal("Cannot read symlink %s\n", de->d_name);
1158
1159 buffer[count] = 0;
1160 p = buffer + count;
1161 while (p > buffer && p[-1] != '/')
1162 p--;
1163 alias->ref = strdup(p);
1164 alias++;
1165 break;
1166 case DT_DIR:
1167 if (chdir(de->d_name))
1168 fatal("Unable to access slab %s\n", slab->name);
Christoph Lameter0d24db32010-10-21 13:01:56 -05001169 slab->name = strdup(de->d_name);
Christoph Lameterc09d8752007-05-06 14:49:48 -07001170 slab->alias = 0;
1171 slab->refs = 0;
1172 slab->aliases = get_obj("aliases");
1173 slab->align = get_obj("align");
1174 slab->cache_dma = get_obj("cache_dma");
1175 slab->cpu_slabs = get_obj("cpu_slabs");
1176 slab->destroy_by_rcu = get_obj("destroy_by_rcu");
1177 slab->hwcache_align = get_obj("hwcache_align");
1178 slab->object_size = get_obj("object_size");
1179 slab->objects = get_obj("objects");
Christoph Lameter205ab992008-04-14 19:11:40 +03001180 slab->objects_partial = get_obj("objects_partial");
1181 slab->objects_total = get_obj("objects_total");
Christoph Lameterc09d8752007-05-06 14:49:48 -07001182 slab->objs_per_slab = get_obj("objs_per_slab");
1183 slab->order = get_obj("order");
1184 slab->partial = get_obj("partial");
1185 slab->partial = get_obj_and_str("partial", &t);
1186 decode_numa_list(slab->numa_partial, t);
WANG Congf32143a2007-10-16 23:31:29 -07001187 free(t);
Christoph Lameterc09d8752007-05-06 14:49:48 -07001188 slab->poison = get_obj("poison");
1189 slab->reclaim_account = get_obj("reclaim_account");
1190 slab->red_zone = get_obj("red_zone");
1191 slab->sanity_checks = get_obj("sanity_checks");
1192 slab->slab_size = get_obj("slab_size");
1193 slab->slabs = get_obj_and_str("slabs", &t);
1194 decode_numa_list(slab->numa, t);
WANG Congf32143a2007-10-16 23:31:29 -07001195 free(t);
Christoph Lameterc09d8752007-05-06 14:49:48 -07001196 slab->store_user = get_obj("store_user");
1197 slab->trace = get_obj("trace");
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001198 slab->alloc_fastpath = get_obj("alloc_fastpath");
1199 slab->alloc_slowpath = get_obj("alloc_slowpath");
1200 slab->free_fastpath = get_obj("free_fastpath");
1201 slab->free_slowpath = get_obj("free_slowpath");
1202 slab->free_frozen= get_obj("free_frozen");
1203 slab->free_add_partial = get_obj("free_add_partial");
1204 slab->free_remove_partial = get_obj("free_remove_partial");
1205 slab->alloc_from_partial = get_obj("alloc_from_partial");
1206 slab->alloc_slab = get_obj("alloc_slab");
1207 slab->alloc_refill = get_obj("alloc_refill");
1208 slab->free_slab = get_obj("free_slab");
1209 slab->cpuslab_flush = get_obj("cpuslab_flush");
1210 slab->deactivate_full = get_obj("deactivate_full");
1211 slab->deactivate_empty = get_obj("deactivate_empty");
1212 slab->deactivate_to_head = get_obj("deactivate_to_head");
1213 slab->deactivate_to_tail = get_obj("deactivate_to_tail");
1214 slab->deactivate_remote_frees = get_obj("deactivate_remote_frees");
Christoph Lameterf715e6f2008-04-29 16:14:46 -07001215 slab->order_fallback = get_obj("order_fallback");
Christoph Lameter9da47142011-06-01 12:26:00 -05001216 slab->cmpxchg_double_cpu_fail = get_obj("cmpxchg_double_cpu_fail");
1217 slab->cmpxchg_double_fail = get_obj("cmpxchg_double_fail");
Christoph Lameteraca726a2011-08-09 16:12:28 -05001218 slab->cpu_partial_alloc = get_obj("cpu_partial_alloc");
1219 slab->cpu_partial_free = get_obj("cpu_partial_free");
Christoph Lameter9da47142011-06-01 12:26:00 -05001220 slab->alloc_node_mismatch = get_obj("alloc_node_mismatch");
1221 slab->deactivate_bypass = get_obj("deactivate_bypass");
Christoph Lameterc09d8752007-05-06 14:49:48 -07001222 chdir("..");
1223 if (slab->name[0] == ':')
1224 alias_targets++;
1225 slab++;
1226 break;
1227 default :
1228 fatal("Unknown file type %lx\n", de->d_type);
1229 }
1230 }
1231 closedir(dir);
1232 slabs = slab - slabinfo;
Christoph Lametera87615b2007-05-09 02:32:37 -07001233 actual_slabs = slabs;
Christoph Lameterc09d8752007-05-06 14:49:48 -07001234 aliases = alias - aliasinfo;
1235 if (slabs > MAX_SLABS)
1236 fatal("Too many slabs\n");
1237 if (aliases > MAX_ALIASES)
1238 fatal("Too many aliases\n");
1239}
1240
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -07001241static void output_slabs(void)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001242{
1243 struct slabinfo *slab;
1244
1245 for (slab = slabinfo; slab < slabinfo + slabs; slab++) {
1246
1247 if (slab->alias)
1248 continue;
1249
1250
1251 if (show_numa)
Christoph Lametera87615b2007-05-09 02:32:37 -07001252 slab_numa(slab, 0);
1253 else if (show_track)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001254 show_tracking(slab);
Christoph Lametera87615b2007-05-09 02:32:37 -07001255 else if (validate)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001256 slab_validate(slab);
Christoph Lametera87615b2007-05-09 02:32:37 -07001257 else if (shrink)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001258 slab_shrink(slab);
Christoph Lametera87615b2007-05-09 02:32:37 -07001259 else if (set_debug)
1260 slab_debug(slab);
1261 else if (show_ops)
1262 ops(slab);
1263 else if (show_slab)
1264 slabcache(slab);
Christoph Lametereefaca92007-05-16 22:10:55 -07001265 else if (show_report)
1266 report(slab);
Christoph Lameterc09d8752007-05-06 14:49:48 -07001267 }
1268}
1269
1270struct option opts[] = {
1271 { "aliases", 0, NULL, 'a' },
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001272 { "activity", 0, NULL, 'A' },
Christoph Lametera87615b2007-05-09 02:32:37 -07001273 { "debug", 2, NULL, 'd' },
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001274 { "display-activity", 0, NULL, 'D' },
Christoph Lametera87615b2007-05-09 02:32:37 -07001275 { "empty", 0, NULL, 'e' },
Christoph Lameterc09d8752007-05-06 14:49:48 -07001276 { "first-alias", 0, NULL, 'f' },
Christoph Lametera87615b2007-05-09 02:32:37 -07001277 { "help", 0, NULL, 'h' },
Christoph Lameterc09d8752007-05-06 14:49:48 -07001278 { "inverted", 0, NULL, 'i'},
Christoph Lametera87615b2007-05-09 02:32:37 -07001279 { "numa", 0, NULL, 'n' },
1280 { "ops", 0, NULL, 'o' },
1281 { "report", 0, NULL, 'r' },
1282 { "shrink", 0, NULL, 's' },
1283 { "slabs", 0, NULL, 'l' },
1284 { "track", 0, NULL, 't'},
1285 { "validate", 0, NULL, 'v' },
1286 { "zero", 0, NULL, 'z' },
Christoph Lameterc09d8752007-05-06 14:49:48 -07001287 { "1ref", 0, NULL, '1'},
1288 { NULL, 0, NULL, 0 }
1289};
1290
1291int main(int argc, char *argv[])
1292{
1293 int c;
1294 int err;
1295 char *pattern_source;
1296
1297 page_size = getpagesize();
Christoph Lameterc09d8752007-05-06 14:49:48 -07001298
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001299 while ((c = getopt_long(argc, argv, "aAd::Defhil1noprstvzTS",
Christoph Lametera87615b2007-05-09 02:32:37 -07001300 opts, NULL)) != -1)
WANG Congf32143a2007-10-16 23:31:29 -07001301 switch (c) {
Christoph Lameterc09d8752007-05-06 14:49:48 -07001302 case '1':
1303 show_single_ref = 1;
1304 break;
1305 case 'a':
1306 show_alias = 1;
1307 break;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001308 case 'A':
1309 sort_active = 1;
1310 break;
Christoph Lametera87615b2007-05-09 02:32:37 -07001311 case 'd':
1312 set_debug = 1;
1313 if (!debug_opt_scan(optarg))
1314 fatal("Invalid debug option '%s'\n", optarg);
1315 break;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001316 case 'D':
1317 show_activity = 1;
1318 break;
Christoph Lametera87615b2007-05-09 02:32:37 -07001319 case 'e':
1320 show_empty = 1;
1321 break;
Christoph Lameterc09d8752007-05-06 14:49:48 -07001322 case 'f':
1323 show_first_alias = 1;
1324 break;
1325 case 'h':
1326 usage();
1327 return 0;
1328 case 'i':
1329 show_inverted = 1;
1330 break;
1331 case 'n':
1332 show_numa = 1;
1333 break;
Christoph Lametera87615b2007-05-09 02:32:37 -07001334 case 'o':
1335 show_ops = 1;
1336 break;
1337 case 'r':
1338 show_report = 1;
1339 break;
Christoph Lameterc09d8752007-05-06 14:49:48 -07001340 case 's':
1341 shrink = 1;
1342 break;
1343 case 'l':
1344 show_slab = 1;
1345 break;
1346 case 't':
1347 show_track = 1;
1348 break;
1349 case 'v':
1350 validate = 1;
1351 break;
1352 case 'z':
1353 skip_zero = 0;
1354 break;
1355 case 'T':
1356 show_totals = 1;
1357 break;
1358 case 'S':
1359 sort_size = 1;
1360 break;
1361
1362 default:
1363 fatal("%s: Invalid option '%c'\n", argv[0], optopt);
1364
1365 }
1366
Christoph Lametera87615b2007-05-09 02:32:37 -07001367 if (!show_slab && !show_alias && !show_track && !show_report
1368 && !validate && !shrink && !set_debug && !show_ops)
Christoph Lameterc09d8752007-05-06 14:49:48 -07001369 show_slab = 1;
1370
1371 if (argc > optind)
1372 pattern_source = argv[optind];
1373 else
1374 pattern_source = ".*";
1375
1376 err = regcomp(&pattern, pattern_source, REG_ICASE|REG_NOSUB);
1377 if (err)
1378 fatal("%s: Invalid pattern '%s' code %d\n",
1379 argv[0], pattern_source, err);
1380 read_slab_dir();
1381 if (show_alias)
1382 alias();
1383 else
1384 if (show_totals)
1385 totals();
1386 else {
1387 link_slabs();
1388 rename_slabs();
1389 sort_slabs();
1390 output_slabs();
1391 }
1392 return 0;
1393}