Merge branch 'reset-seq' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik/libat...
[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
20 #define MAX_SLABS 500
21 #define MAX_ALIASES 500
22 #define MAX_NODES 1024
23
24 struct slabinfo {
25         char *name;
26         int alias;
27         int refs;
28         int aliases, align, cache_dma, cpu_slabs, destroy_by_rcu;
29         int hwcache_align, object_size, objs_per_slab;
30         int sanity_checks, slab_size, store_user, trace;
31         int order, poison, reclaim_account, red_zone;
32         unsigned long partial, objects, slabs;
33         int numa[MAX_NODES];
34         int numa_partial[MAX_NODES];
35 } slabinfo[MAX_SLABS];
36
37 struct aliasinfo {
38         char *name;
39         char *ref;
40         struct slabinfo *slab;
41 } aliasinfo[MAX_ALIASES];
42
43 int slabs = 0;
44 int aliases = 0;
45 int alias_targets = 0;
46 int highest_node = 0;
47
48 char buffer[4096];
49
50 int show_alias = 0;
51 int show_slab = 0;
52 int skip_zero = 1;
53 int show_numa = 0;
54 int show_track = 0;
55 int show_first_alias = 0;
56 int validate = 0;
57 int shrink = 0;
58 int show_inverted = 0;
59 int show_single_ref = 0;
60 int show_totals = 0;
61 int sort_size = 0;
62
63 int page_size;
64
65 regex_t pattern;
66
67 void fatal(const char *x, ...)
68 {
69         va_list ap;
70
71         va_start(ap, x);
72         vfprintf(stderr, x, ap);
73         va_end(ap);
74         exit(1);
75 }
76
77 void usage(void)
78 {
79         printf("slabinfo [-ahnpvtsz] [slab-regexp]\n"
80                 "-a|--aliases           Show aliases\n"
81                 "-h|--help              Show usage information\n"
82                 "-n|--numa              Show NUMA information\n"
83                 "-s|--shrink            Shrink slabs\n"
84                 "-v|--validate          Validate slabs\n"
85                 "-t|--tracking          Show alloc/free information\n"
86                 "-T|--Totals            Show summary information\n"
87                 "-l|--slabs             Show slabs\n"
88                 "-S|--Size              Sort by size\n"
89                 "-z|--zero              Include empty slabs\n"
90                 "-f|--first-alias       Show first alias\n"
91                 "-i|--inverted          Inverted list\n"
92                 "-1|--1ref              Single reference\n"
93         );
94 }
95
96 unsigned long read_obj(char *name)
97 {
98         FILE *f = fopen(name, "r");
99
100         if (!f)
101                 buffer[0] = 0;
102         else {
103                 if (!fgets(buffer,sizeof(buffer), f))
104                         buffer[0] = 0;
105                 fclose(f);
106                 if (buffer[strlen(buffer)] == '\n')
107                         buffer[strlen(buffer)] = 0;
108         }
109         return strlen(buffer);
110 }
111
112
113 /*
114  * Get the contents of an attribute
115  */
116 unsigned long get_obj(char *name)
117 {
118         if (!read_obj(name))
119                 return 0;
120
121         return atol(buffer);
122 }
123
124 unsigned long get_obj_and_str(char *name, char **x)
125 {
126         unsigned long result = 0;
127         char *p;
128
129         *x = NULL;
130
131         if (!read_obj(name)) {
132                 x = NULL;
133                 return 0;
134         }
135         result = strtoul(buffer, &p, 10);
136         while (*p == ' ')
137                 p++;
138         if (*p)
139                 *x = strdup(p);
140         return result;
141 }
142
143 void set_obj(struct slabinfo *s, char *name, int n)
144 {
145         char x[100];
146
147         sprintf(x, "%s/%s", s->name, name);
148
149         FILE *f = fopen(x, "w");
150
151         if (!f)
152                 fatal("Cannot write to %s\n", x);
153
154         fprintf(f, "%d\n", n);
155         fclose(f);
156 }
157
158 /*
159  * Put a size string together
160  */
161 int store_size(char *buffer, unsigned long value)
162 {
163         unsigned long divisor = 1;
164         char trailer = 0;
165         int n;
166
167         if (value > 1000000000UL) {
168                 divisor = 100000000UL;
169                 trailer = 'G';
170         } else if (value > 1000000UL) {
171                 divisor = 100000UL;
172                 trailer = 'M';
173         } else if (value > 1000UL) {
174                 divisor = 100;
175                 trailer = 'K';
176         }
177
178         value /= divisor;
179         n = sprintf(buffer, "%ld",value);
180         if (trailer) {
181                 buffer[n] = trailer;
182                 n++;
183                 buffer[n] = 0;
184         }
185         if (divisor != 1) {
186                 memmove(buffer + n - 2, buffer + n - 3, 4);
187                 buffer[n-2] = '.';
188                 n++;
189         }
190         return n;
191 }
192
193 void decode_numa_list(int *numa, char *t)
194 {
195         int node;
196         int nr;
197
198         memset(numa, 0, MAX_NODES * sizeof(int));
199
200         while (*t == 'N') {
201                 t++;
202                 node = strtoul(t, &t, 10);
203                 if (*t == '=') {
204                         t++;
205                         nr = strtoul(t, &t, 10);
206                         numa[node] = nr;
207                         if (node > highest_node)
208                                 highest_node = node;
209                 }
210                 while (*t == ' ')
211                         t++;
212         }
213 }
214
215 void slab_validate(struct slabinfo *s)
216 {
217         set_obj(s, "validate", 1);
218 }
219
220 void slab_shrink(struct slabinfo *s)
221 {
222         set_obj(s, "shrink", 1);
223 }
224
225 int line = 0;
226
227 void first_line(void)
228 {
229         printf("Name                 Objects   Objsize    Space "
230                 "Slabs/Part/Cpu  O/S O %%Fr %%Ef Flg\n");
231 }
232
233 /*
234  * Find the shortest alias of a slab
235  */
236 struct aliasinfo *find_one_alias(struct slabinfo *find)
237 {
238         struct aliasinfo *a;
239         struct aliasinfo *best = NULL;
240
241         for(a = aliasinfo;a < aliasinfo + aliases; a++) {
242                 if (a->slab == find &&
243                         (!best || strlen(best->name) < strlen(a->name))) {
244                                 best = a;
245                                 if (strncmp(a->name,"kmall", 5) == 0)
246                                         return best;
247                         }
248         }
249         if (best)
250                 return best;
251         fatal("Cannot find alias for %s\n", find->name);
252         return NULL;
253 }
254
255 unsigned long slab_size(struct slabinfo *s)
256 {
257         return  s->slabs * (page_size << s->order);
258 }
259
260
261 void slabcache(struct slabinfo *s)
262 {
263         char size_str[20];
264         char dist_str[40];
265         char flags[20];
266         char *p = flags;
267
268         if (skip_zero && !s->slabs)
269                 return;
270
271         store_size(size_str, slab_size(s));
272         sprintf(dist_str,"%lu/%lu/%d", s->slabs, s->partial, s->cpu_slabs);
273
274         if (!line++)
275                 first_line();
276
277         if (s->aliases)
278                 *p++ = '*';
279         if (s->cache_dma)
280                 *p++ = 'd';
281         if (s->hwcache_align)
282                 *p++ = 'A';
283         if (s->poison)
284                 *p++ = 'P';
285         if (s->reclaim_account)
286                 *p++ = 'a';
287         if (s->red_zone)
288                 *p++ = 'Z';
289         if (s->sanity_checks)
290                 *p++ = 'F';
291         if (s->store_user)
292                 *p++ = 'U';
293         if (s->trace)
294                 *p++ = 'T';
295
296         *p = 0;
297         printf("%-21s %8ld %7d %8s %14s %4d %1d %3ld %3ld %s\n",
298                 s->name, s->objects, s->object_size, size_str, dist_str,
299                 s->objs_per_slab, s->order,
300                 s->slabs ? (s->partial * 100) / s->slabs : 100,
301                 s->slabs ? (s->objects * s->object_size * 100) /
302                         (s->slabs * (page_size << s->order)) : 100,
303                 flags);
304 }
305
306 void slab_numa(struct slabinfo *s)
307 {
308         int node;
309
310         if (!highest_node)
311                 fatal("No NUMA information available.\n");
312
313         if (skip_zero && !s->slabs)
314                 return;
315
316         if (!line) {
317                 printf("\nSlab             Node ");
318                 for(node = 0; node <= highest_node; node++)
319                         printf(" %4d", node);
320                 printf("\n----------------------");
321                 for(node = 0; node <= highest_node; node++)
322                         printf("-----");
323                 printf("\n");
324         }
325         printf("%-21s ", s->name);
326         for(node = 0; node <= highest_node; node++) {
327                 char b[20];
328
329                 store_size(b, s->numa[node]);
330                 printf(" %4s", b);
331         }
332         printf("\n");
333         line++;
334 }
335
336 void show_tracking(struct slabinfo *s)
337 {
338         printf("\n%s: Calls to allocate a slab object\n", s->name);
339         printf("---------------------------------------------------\n");
340         if (read_obj("alloc_calls"))
341                 printf(buffer);
342
343         printf("%s: Calls to free a slab object\n", s->name);
344         printf("-----------------------------------------------\n");
345         if (read_obj("free_calls"))
346                 printf(buffer);
347
348 }
349
350 void totals(void)
351 {
352         struct slabinfo *s;
353
354         int used_slabs = 0;
355         char b1[20], b2[20], b3[20], b4[20];
356         unsigned long long max = 1ULL << 63;
357
358         /* Object size */
359         unsigned long long min_objsize = max, max_objsize = 0, avg_objsize;
360
361         /* Number of partial slabs in a slabcache */
362         unsigned long long min_partial = max, max_partial = 0,
363                                 avg_partial, total_partial = 0;
364
365         /* Number of slabs in a slab cache */
366         unsigned long long min_slabs = max, max_slabs = 0,
367                                 avg_slabs, total_slabs = 0;
368
369         /* Size of the whole slab */
370         unsigned long long min_size = max, max_size = 0,
371                                 avg_size, total_size = 0;
372
373         /* Bytes used for object storage in a slab */
374         unsigned long long min_used = max, max_used = 0,
375                                 avg_used, total_used = 0;
376
377         /* Waste: Bytes used for alignment and padding */
378         unsigned long long min_waste = max, max_waste = 0,
379                                 avg_waste, total_waste = 0;
380         /* Number of objects in a slab */
381         unsigned long long min_objects = max, max_objects = 0,
382                                 avg_objects, total_objects = 0;
383         /* Waste per object */
384         unsigned long long min_objwaste = max,
385                                 max_objwaste = 0, avg_objwaste,
386                                 total_objwaste = 0;
387
388         /* Memory per object */
389         unsigned long long min_memobj = max,
390                                 max_memobj = 0, avg_memobj,
391                                 total_objsize = 0;
392
393         /* Percentage of partial slabs per slab */
394         unsigned long min_ppart = 100, max_ppart = 0,
395                                 avg_ppart, total_ppart = 0;
396
397         /* Number of objects in partial slabs */
398         unsigned long min_partobj = max, max_partobj = 0,
399                                 avg_partobj, total_partobj = 0;
400
401         /* Percentage of partial objects of all objects in a slab */
402         unsigned long min_ppartobj = 100, max_ppartobj = 0,
403                                 avg_ppartobj, total_ppartobj = 0;
404
405
406         for (s = slabinfo; s < slabinfo + slabs; s++) {
407                 unsigned long long size;
408                 unsigned long used;
409                 unsigned long long wasted;
410                 unsigned long long objwaste;
411                 long long objects_in_partial_slabs;
412                 unsigned long percentage_partial_slabs;
413                 unsigned long percentage_partial_objs;
414
415                 if (!s->slabs || !s->objects)
416                         continue;
417
418                 used_slabs++;
419
420                 size = slab_size(s);
421                 used = s->objects * s->object_size;
422                 wasted = size - used;
423                 objwaste = s->slab_size - s->object_size;
424
425                 objects_in_partial_slabs = s->objects -
426                         (s->slabs - s->partial - s ->cpu_slabs) *
427                         s->objs_per_slab;
428
429                 if (objects_in_partial_slabs < 0)
430                         objects_in_partial_slabs = 0;
431
432                 percentage_partial_slabs = s->partial * 100 / s->slabs;
433                 if (percentage_partial_slabs > 100)
434                         percentage_partial_slabs = 100;
435
436                 percentage_partial_objs = objects_in_partial_slabs * 100
437                                                         / s->objects;
438
439                 if (percentage_partial_objs > 100)
440                         percentage_partial_objs = 100;
441
442                 if (s->object_size < min_objsize)
443                         min_objsize = s->object_size;
444                 if (s->partial < min_partial)
445                         min_partial = s->partial;
446                 if (s->slabs < min_slabs)
447                         min_slabs = s->slabs;
448                 if (size < min_size)
449                         min_size = size;
450                 if (wasted < min_waste)
451                         min_waste = wasted;
452                 if (objwaste < min_objwaste)
453                         min_objwaste = objwaste;
454                 if (s->objects < min_objects)
455                         min_objects = s->objects;
456                 if (used < min_used)
457                         min_used = used;
458                 if (objects_in_partial_slabs < min_partobj)
459                         min_partobj = objects_in_partial_slabs;
460                 if (percentage_partial_slabs < min_ppart)
461                         min_ppart = percentage_partial_slabs;
462                 if (percentage_partial_objs < min_ppartobj)
463                         min_ppartobj = percentage_partial_objs;
464                 if (s->slab_size < min_memobj)
465                         min_memobj = s->slab_size;
466
467                 if (s->object_size > max_objsize)
468                         max_objsize = s->object_size;
469                 if (s->partial > max_partial)
470                         max_partial = s->partial;
471                 if (s->slabs > max_slabs)
472                         max_slabs = s->slabs;
473                 if (size > max_size)
474                         max_size = size;
475                 if (wasted > max_waste)
476                         max_waste = wasted;
477                 if (objwaste > max_objwaste)
478                         max_objwaste = objwaste;
479                 if (s->objects > max_objects)
480                         max_objects = s->objects;
481                 if (used > max_used)
482                         max_used = used;
483                 if (objects_in_partial_slabs > max_partobj)
484                         max_partobj = objects_in_partial_slabs;
485                 if (percentage_partial_slabs > max_ppart)
486                         max_ppart = percentage_partial_slabs;
487                 if (percentage_partial_objs > max_ppartobj)
488                         max_ppartobj = percentage_partial_objs;
489                 if (s->slab_size > max_memobj)
490                         max_memobj = s->slab_size;
491
492                 total_partial += s->partial;
493                 total_slabs += s->slabs;
494                 total_size += size;
495                 total_waste += wasted;
496
497                 total_objects += s->objects;
498                 total_used += used;
499                 total_partobj += objects_in_partial_slabs;
500                 total_ppart += percentage_partial_slabs;
501                 total_ppartobj += percentage_partial_objs;
502
503                 total_objwaste += s->objects * objwaste;
504                 total_objsize += s->objects * s->slab_size;
505         }
506
507         if (!total_objects) {
508                 printf("No objects\n");
509                 return;
510         }
511         if (!used_slabs) {
512                 printf("No slabs\n");
513                 return;
514         }
515
516         /* Per slab averages */
517         avg_partial = total_partial / used_slabs;
518         avg_slabs = total_slabs / used_slabs;
519         avg_size = total_size / used_slabs;
520         avg_waste = total_waste / used_slabs;
521
522         avg_objects = total_objects / used_slabs;
523         avg_used = total_used / used_slabs;
524         avg_partobj = total_partobj / used_slabs;
525         avg_ppart = total_ppart / used_slabs;
526         avg_ppartobj = total_ppartobj / used_slabs;
527
528         /* Per object object sizes */
529         avg_objsize = total_used / total_objects;
530         avg_objwaste = total_objwaste / total_objects;
531         avg_partobj = total_partobj * 100 / total_objects;
532         avg_memobj = total_objsize / total_objects;
533
534         printf("Slabcache Totals\n");
535         printf("----------------\n");
536         printf("Slabcaches : %3d      Aliases  : %3d->%-3d Active: %3d\n",
537                         slabs, aliases, alias_targets, used_slabs);
538
539         store_size(b1, total_size);store_size(b2, total_waste);
540         store_size(b3, total_waste * 100 / total_used);
541         printf("Memory used: %6s   # Loss   : %6s   MRatio: %6s%%\n", b1, b2, b3);
542
543         store_size(b1, total_objects);store_size(b2, total_partobj);
544         store_size(b3, total_partobj * 100 / total_objects);
545         printf("# Objects  : %6s   # PartObj: %6s   ORatio: %6s%%\n", b1, b2, b3);
546
547         printf("\n");
548         printf("Per Cache    Average         Min         Max       Total\n");
549         printf("---------------------------------------------------------\n");
550
551         store_size(b1, avg_objects);store_size(b2, min_objects);
552         store_size(b3, max_objects);store_size(b4, total_objects);
553         printf("#Objects  %10s  %10s  %10s  %10s\n",
554                         b1,     b2,     b3,     b4);
555
556         store_size(b1, avg_slabs);store_size(b2, min_slabs);
557         store_size(b3, max_slabs);store_size(b4, total_slabs);
558         printf("#Slabs    %10s  %10s  %10s  %10s\n",
559                         b1,     b2,     b3,     b4);
560
561         store_size(b1, avg_partial);store_size(b2, min_partial);
562         store_size(b3, max_partial);store_size(b4, total_partial);
563         printf("#PartSlab %10s  %10s  %10s  %10s\n",
564                         b1,     b2,     b3,     b4);
565         store_size(b1, avg_ppart);store_size(b2, min_ppart);
566         store_size(b3, max_ppart);
567         store_size(b4, total_partial * 100  / total_slabs);
568         printf("%%PartSlab %10s%% %10s%% %10s%% %10s%%\n",
569                         b1,     b2,     b3,     b4);
570
571         store_size(b1, avg_partobj);store_size(b2, min_partobj);
572         store_size(b3, max_partobj);
573         store_size(b4, total_partobj);
574         printf("PartObjs  %10s  %10s  %10s  %10s\n",
575                         b1,     b2,     b3,     b4);
576
577         store_size(b1, avg_ppartobj);store_size(b2, min_ppartobj);
578         store_size(b3, max_ppartobj);
579         store_size(b4, total_partobj * 100 / total_objects);
580         printf("%% PartObj %10s%% %10s%% %10s%% %10s%%\n",
581                         b1,     b2,     b3,     b4);
582
583         store_size(b1, avg_size);store_size(b2, min_size);
584         store_size(b3, max_size);store_size(b4, total_size);
585         printf("Memory    %10s  %10s  %10s  %10s\n",
586                         b1,     b2,     b3,     b4);
587
588         store_size(b1, avg_used);store_size(b2, min_used);
589         store_size(b3, max_used);store_size(b4, total_used);
590         printf("Used      %10s  %10s  %10s  %10s\n",
591                         b1,     b2,     b3,     b4);
592
593         store_size(b1, avg_waste);store_size(b2, min_waste);
594         store_size(b3, max_waste);store_size(b4, total_waste);
595         printf("Loss      %10s  %10s  %10s  %10s\n",
596                         b1,     b2,     b3,     b4);
597
598         printf("\n");
599         printf("Per Object   Average         Min         Max\n");
600         printf("---------------------------------------------\n");
601
602         store_size(b1, avg_memobj);store_size(b2, min_memobj);
603         store_size(b3, max_memobj);
604         printf("Memory    %10s  %10s  %10s\n",
605                         b1,     b2,     b3);
606         store_size(b1, avg_objsize);store_size(b2, min_objsize);
607         store_size(b3, max_objsize);
608         printf("User      %10s  %10s  %10s\n",
609                         b1,     b2,     b3);
610
611         store_size(b1, avg_objwaste);store_size(b2, min_objwaste);
612         store_size(b3, max_objwaste);
613         printf("Loss      %10s  %10s  %10s\n",
614                         b1,     b2,     b3);
615 }
616
617 void sort_slabs(void)
618 {
619         struct slabinfo *s1,*s2;
620
621         for (s1 = slabinfo; s1 < slabinfo + slabs; s1++) {
622                 for (s2 = s1 + 1; s2 < slabinfo + slabs; s2++) {
623                         int result;
624
625                         if (sort_size)
626                                 result = slab_size(s1) < slab_size(s2);
627                         else
628                                 result = strcasecmp(s1->name, s2->name);
629
630                         if (show_inverted)
631                                 result = -result;
632
633                         if (result > 0) {
634                                 struct slabinfo t;
635
636                                 memcpy(&t, s1, sizeof(struct slabinfo));
637                                 memcpy(s1, s2, sizeof(struct slabinfo));
638                                 memcpy(s2, &t, sizeof(struct slabinfo));
639                         }
640                 }
641         }
642 }
643
644 void sort_aliases(void)
645 {
646         struct aliasinfo *a1,*a2;
647
648         for (a1 = aliasinfo; a1 < aliasinfo + aliases; a1++) {
649                 for (a2 = a1 + 1; a2 < aliasinfo + aliases; a2++) {
650                         char *n1, *n2;
651
652                         n1 = a1->name;
653                         n2 = a2->name;
654                         if (show_alias && !show_inverted) {
655                                 n1 = a1->ref;
656                                 n2 = a2->ref;
657                         }
658                         if (strcasecmp(n1, n2) > 0) {
659                                 struct aliasinfo t;
660
661                                 memcpy(&t, a1, sizeof(struct aliasinfo));
662                                 memcpy(a1, a2, sizeof(struct aliasinfo));
663                                 memcpy(a2, &t, sizeof(struct aliasinfo));
664                         }
665                 }
666         }
667 }
668
669 void link_slabs(void)
670 {
671         struct aliasinfo *a;
672         struct slabinfo *s;
673
674         for (a = aliasinfo; a < aliasinfo + aliases; a++) {
675
676                 for(s = slabinfo; s < slabinfo + slabs; s++)
677                         if (strcmp(a->ref, s->name) == 0) {
678                                 a->slab = s;
679                                 s->refs++;
680                                 break;
681                         }
682                 if (s == slabinfo + slabs)
683                         fatal("Unresolved alias %s\n", a->ref);
684         }
685 }
686
687 void alias(void)
688 {
689         struct aliasinfo *a;
690         char *active = NULL;
691
692         sort_aliases();
693         link_slabs();
694
695         for(a = aliasinfo; a < aliasinfo + aliases; a++) {
696
697                 if (!show_single_ref && a->slab->refs == 1)
698                         continue;
699
700                 if (!show_inverted) {
701                         if (active) {
702                                 if (strcmp(a->slab->name, active) == 0) {
703                                         printf(" %s", a->name);
704                                         continue;
705                                 }
706                         }
707                         printf("\n%-20s <- %s", a->slab->name, a->name);
708                         active = a->slab->name;
709                 }
710                 else
711                         printf("%-20s -> %s\n", a->name, a->slab->name);
712         }
713         if (active)
714                 printf("\n");
715 }
716
717
718 void rename_slabs(void)
719 {
720         struct slabinfo *s;
721         struct aliasinfo *a;
722
723         for (s = slabinfo; s < slabinfo + slabs; s++) {
724                 if (*s->name != ':')
725                         continue;
726
727                 if (s->refs > 1 && !show_first_alias)
728                         continue;
729
730                 a = find_one_alias(s);
731
732                 s->name = a->name;
733         }
734 }
735
736 int slab_mismatch(char *slab)
737 {
738         return regexec(&pattern, slab, 0, NULL, 0);
739 }
740
741 void read_slab_dir(void)
742 {
743         DIR *dir;
744         struct dirent *de;
745         struct slabinfo *slab = slabinfo;
746         struct aliasinfo *alias = aliasinfo;
747         char *p;
748         char *t;
749         int count;
750
751         dir = opendir(".");
752         while ((de = readdir(dir))) {
753                 if (de->d_name[0] == '.' ||
754                                 slab_mismatch(de->d_name))
755                         continue;
756                 switch (de->d_type) {
757                    case DT_LNK:
758                         alias->name = strdup(de->d_name);
759                         count = readlink(de->d_name, buffer, sizeof(buffer));
760
761                         if (count < 0)
762                                 fatal("Cannot read symlink %s\n", de->d_name);
763
764                         buffer[count] = 0;
765                         p = buffer + count;
766                         while (p > buffer && p[-1] != '/')
767                                 p--;
768                         alias->ref = strdup(p);
769                         alias++;
770                         break;
771                    case DT_DIR:
772                         if (chdir(de->d_name))
773                                 fatal("Unable to access slab %s\n", slab->name);
774                         slab->name = strdup(de->d_name);
775                         slab->alias = 0;
776                         slab->refs = 0;
777                         slab->aliases = get_obj("aliases");
778                         slab->align = get_obj("align");
779                         slab->cache_dma = get_obj("cache_dma");
780                         slab->cpu_slabs = get_obj("cpu_slabs");
781                         slab->destroy_by_rcu = get_obj("destroy_by_rcu");
782                         slab->hwcache_align = get_obj("hwcache_align");
783                         slab->object_size = get_obj("object_size");
784                         slab->objects = get_obj("objects");
785                         slab->objs_per_slab = get_obj("objs_per_slab");
786                         slab->order = get_obj("order");
787                         slab->partial = get_obj("partial");
788                         slab->partial = get_obj_and_str("partial", &t);
789                         decode_numa_list(slab->numa_partial, t);
790                         slab->poison = get_obj("poison");
791                         slab->reclaim_account = get_obj("reclaim_account");
792                         slab->red_zone = get_obj("red_zone");
793                         slab->sanity_checks = get_obj("sanity_checks");
794                         slab->slab_size = get_obj("slab_size");
795                         slab->slabs = get_obj_and_str("slabs", &t);
796                         decode_numa_list(slab->numa, t);
797                         slab->store_user = get_obj("store_user");
798                         slab->trace = get_obj("trace");
799                         chdir("..");
800                         if (slab->name[0] == ':')
801                                 alias_targets++;
802                         slab++;
803                         break;
804                    default :
805                         fatal("Unknown file type %lx\n", de->d_type);
806                 }
807         }
808         closedir(dir);
809         slabs = slab - slabinfo;
810         aliases = alias - aliasinfo;
811         if (slabs > MAX_SLABS)
812                 fatal("Too many slabs\n");
813         if (aliases > MAX_ALIASES)
814                 fatal("Too many aliases\n");
815 }
816
817 void output_slabs(void)
818 {
819         struct slabinfo *slab;
820
821         for (slab = slabinfo; slab < slabinfo + slabs; slab++) {
822
823                 if (slab->alias)
824                         continue;
825
826
827                 if (show_numa)
828                         slab_numa(slab);
829                 else
830                 if (show_track)
831                         show_tracking(slab);
832                 else
833                 if (validate)
834                         slab_validate(slab);
835                 else
836                 if (shrink)
837                         slab_shrink(slab);
838                 else {
839                         if (show_slab)
840                                 slabcache(slab);
841                 }
842         }
843 }
844
845 struct option opts[] = {
846         { "aliases", 0, NULL, 'a' },
847         { "slabs", 0, NULL, 'l' },
848         { "numa", 0, NULL, 'n' },
849         { "zero", 0, NULL, 'z' },
850         { "help", 0, NULL, 'h' },
851         { "validate", 0, NULL, 'v' },
852         { "first-alias", 0, NULL, 'f' },
853         { "shrink", 0, NULL, 's' },
854         { "track", 0, NULL, 't'},
855         { "inverted", 0, NULL, 'i'},
856         { "1ref", 0, NULL, '1'},
857         { NULL, 0, NULL, 0 }
858 };
859
860 int main(int argc, char *argv[])
861 {
862         int c;
863         int err;
864         char *pattern_source;
865
866         page_size = getpagesize();
867         if (chdir("/sys/slab"))
868                 fatal("This kernel does not have SLUB support.\n");
869
870         while ((c = getopt_long(argc, argv, "afhil1npstvzTS", opts, NULL)) != -1)
871         switch(c) {
872                 case '1':
873                         show_single_ref = 1;
874                         break;
875                 case 'a':
876                         show_alias = 1;
877                         break;
878                 case 'f':
879                         show_first_alias = 1;
880                         break;
881                 case 'h':
882                         usage();
883                         return 0;
884                 case 'i':
885                         show_inverted = 1;
886                         break;
887                 case 'n':
888                         show_numa = 1;
889                         break;
890                 case 's':
891                         shrink = 1;
892                         break;
893                 case 'l':
894                         show_slab = 1;
895                         break;
896                 case 't':
897                         show_track = 1;
898                         break;
899                 case 'v':
900                         validate = 1;
901                         break;
902                 case 'z':
903                         skip_zero = 0;
904                         break;
905                 case 'T':
906                         show_totals = 1;
907                         break;
908                 case 'S':
909                         sort_size = 1;
910                         break;
911
912                 default:
913                         fatal("%s: Invalid option '%c'\n", argv[0], optopt);
914
915         }
916
917         if (!show_slab && !show_alias && !show_track
918                 && !validate && !shrink)
919                         show_slab = 1;
920
921         if (argc > optind)
922                 pattern_source = argv[optind];
923         else
924                 pattern_source = ".*";
925
926         err = regcomp(&pattern, pattern_source, REG_ICASE|REG_NOSUB);
927         if (err)
928                 fatal("%s: Invalid pattern '%s' code %d\n",
929                         argv[0], pattern_source, err);
930         read_slab_dir();
931         if (show_alias)
932                 alias();
933         else
934         if (show_totals)
935                 totals();
936         else {
937                 link_slabs();
938                 rename_slabs();
939                 sort_slabs();
940                 output_slabs();
941         }
942         return 0;
943 }