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