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