2 * Slabinfo: Tool to get reports about slabs
4 * (C) 2007 sgi, Christoph Lameter
5 * (C) 2011 Linux Foundation, Christoph Lameter
9 * gcc -o slabinfo slabinfo.c
13 #include <sys/types.h>
24 #define MAX_ALIASES 500
25 #define MAX_NODES 1024
31 int aliases
, align
, cache_dma
, cpu_slabs
, destroy_by_rcu
;
32 int hwcache_align
, object_size
, objs_per_slab
;
33 int sanity_checks
, slab_size
, store_user
, trace
;
34 int order
, poison
, reclaim_account
, red_zone
;
35 unsigned long partial
, objects
, slabs
, objects_partial
, objects_total
;
36 unsigned long alloc_fastpath
, alloc_slowpath
;
37 unsigned long free_fastpath
, free_slowpath
;
38 unsigned long free_frozen
, free_add_partial
, free_remove_partial
;
39 unsigned long alloc_from_partial
, alloc_slab
, free_slab
, alloc_refill
;
40 unsigned long cpuslab_flush
, deactivate_full
, deactivate_empty
;
41 unsigned long deactivate_to_head
, deactivate_to_tail
;
42 unsigned long deactivate_remote_frees
, order_fallback
;
43 unsigned long cmpxchg_double_cpu_fail
, cmpxchg_double_fail
;
44 unsigned long alloc_node_mismatch
, deactivate_bypass
;
45 unsigned long cpu_partial_alloc
, cpu_partial_free
;
47 int numa_partial
[MAX_NODES
];
48 } slabinfo
[MAX_SLABS
];
53 struct slabinfo
*slab
;
54 } aliasinfo
[MAX_ALIASES
];
82 int output_lines
= -1;
98 static void fatal(const char *x
, ...)
103 vfprintf(stderr
, x
, ap
);
108 static void usage(void)
110 printf("slabinfo 4/15/2011. (c) 2007 sgi/(c) 2011 Linux Foundation.\n\n"
111 "slabinfo [-ahnpvtsz] [-d debugopts] [slab-regexp]\n"
112 "-a|--aliases Show aliases\n"
113 "-A|--activity Most active slabs first\n"
114 "-d<options>|--debug=<options> Set/Clear Debug options\n"
115 "-D|--display-active Switch line format to activity\n"
116 "-e|--empty Show empty slabs\n"
117 "-f|--first-alias Show first alias\n"
118 "-h|--help Show usage information\n"
119 "-i|--inverted Inverted list\n"
120 "-l|--slabs Show slabs\n"
121 "-n|--numa Show NUMA information\n"
122 "-o|--ops Show kmem_cache_ops\n"
123 "-s|--shrink Shrink slabs\n"
124 "-r|--report Detailed report on single slabs\n"
125 "-S|--Size Sort by size\n"
126 "-t|--tracking Show alloc/free information\n"
127 "-T|--Totals Show summary information\n"
128 "-v|--validate Validate slabs\n"
129 "-z|--zero Include empty slabs\n"
130 "-1|--1ref Single reference\n"
131 "-N|--lines=K Show the first K slabs\n"
132 "-L|--Loss Sort by loss\n"
133 "-X|--Xtotals Show extended summary information\n"
134 "-B|--Bytes Show size in bytes\n"
135 "\nValid debug options (FZPUT may be combined)\n"
136 "a / A Switch on all debug options (=FZUP)\n"
137 "- Switch off all debug options\n"
138 "f / F Sanity Checks (SLAB_DEBUG_FREE)\n"
146 static unsigned long read_obj(const char *name
)
148 FILE *f
= fopen(name
, "r");
153 if (!fgets(buffer
, sizeof(buffer
), f
))
156 if (buffer
[strlen(buffer
)] == '\n')
157 buffer
[strlen(buffer
)] = 0;
159 return strlen(buffer
);
164 * Get the contents of an attribute
166 static unsigned long get_obj(const char *name
)
174 static unsigned long get_obj_and_str(const char *name
, char **x
)
176 unsigned long result
= 0;
181 if (!read_obj(name
)) {
185 result
= strtoul(buffer
, &p
, 10);
193 static void set_obj(struct slabinfo
*s
, const char *name
, int n
)
198 snprintf(x
, 100, "%s/%s", s
->name
, name
);
201 fatal("Cannot write to %s\n", x
);
203 fprintf(f
, "%d\n", n
);
207 static unsigned long read_slab_obj(struct slabinfo
*s
, const char *name
)
213 snprintf(x
, 100, "%s/%s", s
->name
, name
);
219 l
= fread(buffer
, 1, sizeof(buffer
), f
);
228 * Put a size string together
230 static int store_size(char *buffer
, unsigned long value
)
232 unsigned long divisor
= 1;
237 if (value
> 1000000000UL) {
238 divisor
= 100000000UL;
240 } else if (value
> 1000000UL) {
243 } else if (value
> 1000UL) {
250 n
= sprintf(buffer
, "%ld",value
);
257 memmove(buffer
+ n
- 2, buffer
+ n
- 3, 4);
264 static void decode_numa_list(int *numa
, char *t
)
269 memset(numa
, 0, MAX_NODES
* sizeof(int));
276 node
= strtoul(t
, &t
, 10);
279 nr
= strtoul(t
, &t
, 10);
281 if (node
> highest_node
)
289 static void slab_validate(struct slabinfo
*s
)
291 if (strcmp(s
->name
, "*") == 0)
294 set_obj(s
, "validate", 1);
297 static void slab_shrink(struct slabinfo
*s
)
299 if (strcmp(s
->name
, "*") == 0)
302 set_obj(s
, "shrink", 1);
307 static void first_line(void)
310 printf("Name Objects Alloc Free"
311 " %%Fast Fallb O CmpX UL\n");
313 printf("Name Objects Objsize %s "
314 "Slabs/Part/Cpu O/S O %%Fr %%Ef Flg\n",
315 sort_loss
? " Loss" : "Space");
319 * Find the shortest alias of a slab
321 static struct aliasinfo
*find_one_alias(struct slabinfo
*find
)
324 struct aliasinfo
*best
= NULL
;
326 for(a
= aliasinfo
;a
< aliasinfo
+ aliases
; a
++) {
327 if (a
->slab
== find
&&
328 (!best
|| strlen(best
->name
) < strlen(a
->name
))) {
330 if (strncmp(a
->name
,"kmall", 5) == 0)
337 static unsigned long slab_size(struct slabinfo
*s
)
339 return s
->slabs
* (page_size
<< s
->order
);
342 static unsigned long slab_activity(struct slabinfo
*s
)
344 return s
->alloc_fastpath
+ s
->free_fastpath
+
345 s
->alloc_slowpath
+ s
->free_slowpath
;
348 static unsigned long slab_waste(struct slabinfo
*s
)
350 return slab_size(s
) - s
->objects
* s
->object_size
;
353 static void slab_numa(struct slabinfo
*s
, int mode
)
357 if (strcmp(s
->name
, "*") == 0)
361 printf("\n%s: No NUMA information available.\n", s
->name
);
365 if (skip_zero
&& !s
->slabs
)
369 printf("\n%-21s:", mode
? "NUMA nodes" : "Slab");
370 for(node
= 0; node
<= highest_node
; node
++)
371 printf(" %4d", node
);
372 printf("\n----------------------");
373 for(node
= 0; node
<= highest_node
; node
++)
377 printf("%-21s ", mode
? "All slabs" : s
->name
);
378 for(node
= 0; node
<= highest_node
; node
++) {
381 store_size(b
, s
->numa
[node
]);
386 printf("%-21s ", "Partial slabs");
387 for(node
= 0; node
<= highest_node
; node
++) {
390 store_size(b
, s
->numa_partial
[node
]);
398 static void show_tracking(struct slabinfo
*s
)
400 printf("\n%s: Kernel object allocation\n", s
->name
);
401 printf("-----------------------------------------------------------------------\n");
402 if (read_slab_obj(s
, "alloc_calls"))
403 printf("%s", buffer
);
407 printf("\n%s: Kernel object freeing\n", s
->name
);
408 printf("------------------------------------------------------------------------\n");
409 if (read_slab_obj(s
, "free_calls"))
410 printf("%s", buffer
);
416 static void ops(struct slabinfo
*s
)
418 if (strcmp(s
->name
, "*") == 0)
421 if (read_slab_obj(s
, "ops")) {
422 printf("\n%s: kmem_cache operations\n", s
->name
);
423 printf("--------------------------------------------\n");
424 printf("%s", buffer
);
426 printf("\n%s has no kmem_cache operations\n", s
->name
);
429 static const char *onoff(int x
)
436 static void slab_stats(struct slabinfo
*s
)
438 unsigned long total_alloc
;
439 unsigned long total_free
;
445 total_alloc
= s
->alloc_fastpath
+ s
->alloc_slowpath
;
446 total_free
= s
->free_fastpath
+ s
->free_slowpath
;
452 printf("Slab Perf Counter Alloc Free %%Al %%Fr\n");
453 printf("--------------------------------------------------\n");
454 printf("Fastpath %8lu %8lu %3lu %3lu\n",
455 s
->alloc_fastpath
, s
->free_fastpath
,
456 s
->alloc_fastpath
* 100 / total_alloc
,
457 total_free
? s
->free_fastpath
* 100 / total_free
: 0);
458 printf("Slowpath %8lu %8lu %3lu %3lu\n",
459 total_alloc
- s
->alloc_fastpath
, s
->free_slowpath
,
460 (total_alloc
- s
->alloc_fastpath
) * 100 / total_alloc
,
461 total_free
? s
->free_slowpath
* 100 / total_free
: 0);
462 printf("Page Alloc %8lu %8lu %3lu %3lu\n",
463 s
->alloc_slab
, s
->free_slab
,
464 s
->alloc_slab
* 100 / total_alloc
,
465 total_free
? s
->free_slab
* 100 / total_free
: 0);
466 printf("Add partial %8lu %8lu %3lu %3lu\n",
467 s
->deactivate_to_head
+ s
->deactivate_to_tail
,
469 (s
->deactivate_to_head
+ s
->deactivate_to_tail
) * 100 / total_alloc
,
470 total_free
? s
->free_add_partial
* 100 / total_free
: 0);
471 printf("Remove partial %8lu %8lu %3lu %3lu\n",
472 s
->alloc_from_partial
, s
->free_remove_partial
,
473 s
->alloc_from_partial
* 100 / total_alloc
,
474 total_free
? s
->free_remove_partial
* 100 / total_free
: 0);
476 printf("Cpu partial list %8lu %8lu %3lu %3lu\n",
477 s
->cpu_partial_alloc
, s
->cpu_partial_free
,
478 s
->cpu_partial_alloc
* 100 / total_alloc
,
479 total_free
? s
->cpu_partial_free
* 100 / total_free
: 0);
481 printf("RemoteObj/SlabFrozen %8lu %8lu %3lu %3lu\n",
482 s
->deactivate_remote_frees
, s
->free_frozen
,
483 s
->deactivate_remote_frees
* 100 / total_alloc
,
484 total_free
? s
->free_frozen
* 100 / total_free
: 0);
486 printf("Total %8lu %8lu\n\n", total_alloc
, total_free
);
488 if (s
->cpuslab_flush
)
489 printf("Flushes %8lu\n", s
->cpuslab_flush
);
491 total
= s
->deactivate_full
+ s
->deactivate_empty
+
492 s
->deactivate_to_head
+ s
->deactivate_to_tail
+ s
->deactivate_bypass
;
495 printf("\nSlab Deactivation Ocurrences %%\n");
496 printf("-------------------------------------------------\n");
497 printf("Slab full %7lu %3lu%%\n",
498 s
->deactivate_full
, (s
->deactivate_full
* 100) / total
);
499 printf("Slab empty %7lu %3lu%%\n",
500 s
->deactivate_empty
, (s
->deactivate_empty
* 100) / total
);
501 printf("Moved to head of partial list %7lu %3lu%%\n",
502 s
->deactivate_to_head
, (s
->deactivate_to_head
* 100) / total
);
503 printf("Moved to tail of partial list %7lu %3lu%%\n",
504 s
->deactivate_to_tail
, (s
->deactivate_to_tail
* 100) / total
);
505 printf("Deactivation bypass %7lu %3lu%%\n",
506 s
->deactivate_bypass
, (s
->deactivate_bypass
* 100) / total
);
507 printf("Refilled from foreign frees %7lu %3lu%%\n",
508 s
->alloc_refill
, (s
->alloc_refill
* 100) / total
);
509 printf("Node mismatch %7lu %3lu%%\n",
510 s
->alloc_node_mismatch
, (s
->alloc_node_mismatch
* 100) / total
);
513 if (s
->cmpxchg_double_fail
|| s
->cmpxchg_double_cpu_fail
)
514 printf("\nCmpxchg_double Looping\n------------------------\n");
515 printf("Locked Cmpxchg Double redos %lu\nUnlocked Cmpxchg Double redos %lu\n",
516 s
->cmpxchg_double_fail
, s
->cmpxchg_double_cpu_fail
);
519 static void report(struct slabinfo
*s
)
521 if (strcmp(s
->name
, "*") == 0)
524 printf("\nSlabcache: %-15s Aliases: %2d Order : %2d Objects: %lu\n",
525 s
->name
, s
->aliases
, s
->order
, s
->objects
);
526 if (s
->hwcache_align
)
527 printf("** Hardware cacheline aligned\n");
529 printf("** Memory is allocated in a special DMA zone\n");
530 if (s
->destroy_by_rcu
)
531 printf("** Slabs are destroyed via RCU\n");
532 if (s
->reclaim_account
)
533 printf("** Reclaim accounting active\n");
535 printf("\nSizes (bytes) Slabs Debug Memory\n");
536 printf("------------------------------------------------------------------------\n");
537 printf("Object : %7d Total : %7ld Sanity Checks : %s Total: %7ld\n",
538 s
->object_size
, s
->slabs
, onoff(s
->sanity_checks
),
539 s
->slabs
* (page_size
<< s
->order
));
540 printf("SlabObj: %7d Full : %7ld Redzoning : %s Used : %7ld\n",
541 s
->slab_size
, s
->slabs
- s
->partial
- s
->cpu_slabs
,
542 onoff(s
->red_zone
), s
->objects
* s
->object_size
);
543 printf("SlabSiz: %7d Partial: %7ld Poisoning : %s Loss : %7ld\n",
544 page_size
<< s
->order
, s
->partial
, onoff(s
->poison
),
545 s
->slabs
* (page_size
<< s
->order
) - s
->objects
* s
->object_size
);
546 printf("Loss : %7d CpuSlab: %7d Tracking : %s Lalig: %7ld\n",
547 s
->slab_size
- s
->object_size
, s
->cpu_slabs
, onoff(s
->store_user
),
548 (s
->slab_size
- s
->object_size
) * s
->objects
);
549 printf("Align : %7d Objects: %7d Tracing : %s Lpadd: %7ld\n",
550 s
->align
, s
->objs_per_slab
, onoff(s
->trace
),
551 ((page_size
<< s
->order
) - s
->objs_per_slab
* s
->slab_size
) *
560 static void slabcache(struct slabinfo
*s
)
567 if (strcmp(s
->name
, "*") == 0)
570 if (actual_slabs
== 1) {
575 if (skip_zero
&& !show_empty
&& !s
->slabs
)
578 if (show_empty
&& s
->slabs
)
582 store_size(size_str
, slab_size(s
));
584 store_size(size_str
, slab_waste(s
));
585 snprintf(dist_str
, 40, "%lu/%lu/%d", s
->slabs
- s
->cpu_slabs
,
586 s
->partial
, s
->cpu_slabs
);
595 if (s
->hwcache_align
)
599 if (s
->reclaim_account
)
603 if (s
->sanity_checks
)
612 unsigned long total_alloc
;
613 unsigned long total_free
;
615 total_alloc
= s
->alloc_fastpath
+ s
->alloc_slowpath
;
616 total_free
= s
->free_fastpath
+ s
->free_slowpath
;
618 printf("%-21s %8ld %10ld %10ld %3ld %3ld %5ld %1d %4ld %4ld\n",
620 total_alloc
, total_free
,
621 total_alloc
? (s
->alloc_fastpath
* 100 / total_alloc
) : 0,
622 total_free
? (s
->free_fastpath
* 100 / total_free
) : 0,
623 s
->order_fallback
, s
->order
, s
->cmpxchg_double_fail
,
624 s
->cmpxchg_double_cpu_fail
);
626 printf("%-21s %8ld %7d %15s %14s %4d %1d %3ld %3ld %s\n",
627 s
->name
, s
->objects
, s
->object_size
, size_str
, dist_str
,
628 s
->objs_per_slab
, s
->order
,
629 s
->slabs
? (s
->partial
* 100) / s
->slabs
: 100,
630 s
->slabs
? (s
->objects
* s
->object_size
* 100) /
631 (s
->slabs
* (page_size
<< s
->order
)) : 100,
637 * Analyze debug options. Return false if something is amiss.
639 static int debug_opt_scan(char *opt
)
641 if (!opt
|| !opt
[0] || strcmp(opt
, "-") == 0)
644 if (strcasecmp(opt
, "a") == 0) {
688 static int slab_empty(struct slabinfo
*s
)
694 * We may still have slabs even if there are no objects. Shrinking will
698 set_obj(s
, "shrink", 1);
703 static void slab_debug(struct slabinfo
*s
)
705 if (strcmp(s
->name
, "*") == 0)
708 if (sanity
&& !s
->sanity_checks
) {
709 set_obj(s
, "sanity", 1);
711 if (!sanity
&& s
->sanity_checks
) {
713 set_obj(s
, "sanity", 0);
715 fprintf(stderr
, "%s not empty cannot disable sanity checks\n", s
->name
);
717 if (redzone
&& !s
->red_zone
) {
719 set_obj(s
, "red_zone", 1);
721 fprintf(stderr
, "%s not empty cannot enable redzoning\n", s
->name
);
723 if (!redzone
&& s
->red_zone
) {
725 set_obj(s
, "red_zone", 0);
727 fprintf(stderr
, "%s not empty cannot disable redzoning\n", s
->name
);
729 if (poison
&& !s
->poison
) {
731 set_obj(s
, "poison", 1);
733 fprintf(stderr
, "%s not empty cannot enable poisoning\n", s
->name
);
735 if (!poison
&& s
->poison
) {
737 set_obj(s
, "poison", 0);
739 fprintf(stderr
, "%s not empty cannot disable poisoning\n", s
->name
);
741 if (tracking
&& !s
->store_user
) {
743 set_obj(s
, "store_user", 1);
745 fprintf(stderr
, "%s not empty cannot enable tracking\n", s
->name
);
747 if (!tracking
&& s
->store_user
) {
749 set_obj(s
, "store_user", 0);
751 fprintf(stderr
, "%s not empty cannot disable tracking\n", s
->name
);
753 if (tracing
&& !s
->trace
) {
755 set_obj(s
, "trace", 1);
757 fprintf(stderr
, "%s can only enable trace for one slab at a time\n", s
->name
);
759 if (!tracing
&& s
->trace
)
760 set_obj(s
, "trace", 1);
763 static void totals(void)
768 char b1
[20], b2
[20], b3
[20], b4
[20];
769 unsigned long long max
= 1ULL << 63;
772 unsigned long long min_objsize
= max
, max_objsize
= 0, avg_objsize
;
774 /* Number of partial slabs in a slabcache */
775 unsigned long long min_partial
= max
, max_partial
= 0,
776 avg_partial
, total_partial
= 0;
778 /* Number of slabs in a slab cache */
779 unsigned long long min_slabs
= max
, max_slabs
= 0,
780 avg_slabs
, total_slabs
= 0;
782 /* Size of the whole slab */
783 unsigned long long min_size
= max
, max_size
= 0,
784 avg_size
, total_size
= 0;
786 /* Bytes used for object storage in a slab */
787 unsigned long long min_used
= max
, max_used
= 0,
788 avg_used
, total_used
= 0;
790 /* Waste: Bytes used for alignment and padding */
791 unsigned long long min_waste
= max
, max_waste
= 0,
792 avg_waste
, total_waste
= 0;
793 /* Number of objects in a slab */
794 unsigned long long min_objects
= max
, max_objects
= 0,
795 avg_objects
, total_objects
= 0;
796 /* Waste per object */
797 unsigned long long min_objwaste
= max
,
798 max_objwaste
= 0, avg_objwaste
,
801 /* Memory per object */
802 unsigned long long min_memobj
= max
,
803 max_memobj
= 0, avg_memobj
,
806 /* Percentage of partial slabs per slab */
807 unsigned long min_ppart
= 100, max_ppart
= 0,
808 avg_ppart
, total_ppart
= 0;
810 /* Number of objects in partial slabs */
811 unsigned long min_partobj
= max
, max_partobj
= 0,
812 avg_partobj
, total_partobj
= 0;
814 /* Percentage of partial objects of all objects in a slab */
815 unsigned long min_ppartobj
= 100, max_ppartobj
= 0,
816 avg_ppartobj
, total_ppartobj
= 0;
819 for (s
= slabinfo
; s
< slabinfo
+ slabs
; s
++) {
820 unsigned long long size
;
822 unsigned long long wasted
;
823 unsigned long long objwaste
;
824 unsigned long percentage_partial_slabs
;
825 unsigned long percentage_partial_objs
;
827 if (!s
->slabs
|| !s
->objects
)
833 used
= s
->objects
* s
->object_size
;
834 wasted
= size
- used
;
835 objwaste
= s
->slab_size
- s
->object_size
;
837 percentage_partial_slabs
= s
->partial
* 100 / s
->slabs
;
838 if (percentage_partial_slabs
> 100)
839 percentage_partial_slabs
= 100;
841 percentage_partial_objs
= s
->objects_partial
* 100
844 if (percentage_partial_objs
> 100)
845 percentage_partial_objs
= 100;
847 if (s
->object_size
< min_objsize
)
848 min_objsize
= s
->object_size
;
849 if (s
->partial
< min_partial
)
850 min_partial
= s
->partial
;
851 if (s
->slabs
< min_slabs
)
852 min_slabs
= s
->slabs
;
855 if (wasted
< min_waste
)
857 if (objwaste
< min_objwaste
)
858 min_objwaste
= objwaste
;
859 if (s
->objects
< min_objects
)
860 min_objects
= s
->objects
;
863 if (s
->objects_partial
< min_partobj
)
864 min_partobj
= s
->objects_partial
;
865 if (percentage_partial_slabs
< min_ppart
)
866 min_ppart
= percentage_partial_slabs
;
867 if (percentage_partial_objs
< min_ppartobj
)
868 min_ppartobj
= percentage_partial_objs
;
869 if (s
->slab_size
< min_memobj
)
870 min_memobj
= s
->slab_size
;
872 if (s
->object_size
> max_objsize
)
873 max_objsize
= s
->object_size
;
874 if (s
->partial
> max_partial
)
875 max_partial
= s
->partial
;
876 if (s
->slabs
> max_slabs
)
877 max_slabs
= s
->slabs
;
880 if (wasted
> max_waste
)
882 if (objwaste
> max_objwaste
)
883 max_objwaste
= objwaste
;
884 if (s
->objects
> max_objects
)
885 max_objects
= s
->objects
;
888 if (s
->objects_partial
> max_partobj
)
889 max_partobj
= s
->objects_partial
;
890 if (percentage_partial_slabs
> max_ppart
)
891 max_ppart
= percentage_partial_slabs
;
892 if (percentage_partial_objs
> max_ppartobj
)
893 max_ppartobj
= percentage_partial_objs
;
894 if (s
->slab_size
> max_memobj
)
895 max_memobj
= s
->slab_size
;
897 total_partial
+= s
->partial
;
898 total_slabs
+= s
->slabs
;
900 total_waste
+= wasted
;
902 total_objects
+= s
->objects
;
904 total_partobj
+= s
->objects_partial
;
905 total_ppart
+= percentage_partial_slabs
;
906 total_ppartobj
+= percentage_partial_objs
;
908 total_objwaste
+= s
->objects
* objwaste
;
909 total_objsize
+= s
->objects
* s
->slab_size
;
912 if (!total_objects
) {
913 printf("No objects\n");
917 printf("No slabs\n");
921 /* Per slab averages */
922 avg_partial
= total_partial
/ used_slabs
;
923 avg_slabs
= total_slabs
/ used_slabs
;
924 avg_size
= total_size
/ used_slabs
;
925 avg_waste
= total_waste
/ used_slabs
;
927 avg_objects
= total_objects
/ used_slabs
;
928 avg_used
= total_used
/ used_slabs
;
929 avg_partobj
= total_partobj
/ used_slabs
;
930 avg_ppart
= total_ppart
/ used_slabs
;
931 avg_ppartobj
= total_ppartobj
/ used_slabs
;
933 /* Per object object sizes */
934 avg_objsize
= total_used
/ total_objects
;
935 avg_objwaste
= total_objwaste
/ total_objects
;
936 avg_partobj
= total_partobj
* 100 / total_objects
;
937 avg_memobj
= total_objsize
/ total_objects
;
939 printf("Slabcache Totals\n");
940 printf("----------------\n");
941 printf("Slabcaches : %15d Aliases : %11d->%-3d Active: %3d\n",
942 slabs
, aliases
, alias_targets
, used_slabs
);
944 store_size(b1
, total_size
);store_size(b2
, total_waste
);
945 store_size(b3
, total_waste
* 100 / total_used
);
946 printf("Memory used: %15s # Loss : %15s MRatio:%6s%%\n", b1
, b2
, b3
);
948 store_size(b1
, total_objects
);store_size(b2
, total_partobj
);
949 store_size(b3
, total_partobj
* 100 / total_objects
);
950 printf("# Objects : %15s # PartObj: %15s ORatio:%6s%%\n", b1
, b2
, b3
);
953 printf("Per Cache Average "
955 printf("---------------------------------------"
956 "-------------------------------------\n");
958 store_size(b1
, avg_objects
);store_size(b2
, min_objects
);
959 store_size(b3
, max_objects
);store_size(b4
, total_objects
);
960 printf("#Objects %15s %15s %15s %15s\n",
963 store_size(b1
, avg_slabs
);store_size(b2
, min_slabs
);
964 store_size(b3
, max_slabs
);store_size(b4
, total_slabs
);
965 printf("#Slabs %15s %15s %15s %15s\n",
968 store_size(b1
, avg_partial
);store_size(b2
, min_partial
);
969 store_size(b3
, max_partial
);store_size(b4
, total_partial
);
970 printf("#PartSlab %15s %15s %15s %15s\n",
972 store_size(b1
, avg_ppart
);store_size(b2
, min_ppart
);
973 store_size(b3
, max_ppart
);
974 store_size(b4
, total_partial
* 100 / total_slabs
);
975 printf("%%PartSlab%15s%% %15s%% %15s%% %15s%%\n",
978 store_size(b1
, avg_partobj
);store_size(b2
, min_partobj
);
979 store_size(b3
, max_partobj
);
980 store_size(b4
, total_partobj
);
981 printf("PartObjs %15s %15s %15s %15s\n",
984 store_size(b1
, avg_ppartobj
);store_size(b2
, min_ppartobj
);
985 store_size(b3
, max_ppartobj
);
986 store_size(b4
, total_partobj
* 100 / total_objects
);
987 printf("%% PartObj%15s%% %15s%% %15s%% %15s%%\n",
990 store_size(b1
, avg_size
);store_size(b2
, min_size
);
991 store_size(b3
, max_size
);store_size(b4
, total_size
);
992 printf("Memory %15s %15s %15s %15s\n",
995 store_size(b1
, avg_used
);store_size(b2
, min_used
);
996 store_size(b3
, max_used
);store_size(b4
, total_used
);
997 printf("Used %15s %15s %15s %15s\n",
1000 store_size(b1
, avg_waste
);store_size(b2
, min_waste
);
1001 store_size(b3
, max_waste
);store_size(b4
, total_waste
);
1002 printf("Loss %15s %15s %15s %15s\n",
1006 printf("Per Object Average "
1008 printf("---------------------------------------"
1009 "--------------------\n");
1011 store_size(b1
, avg_memobj
);store_size(b2
, min_memobj
);
1012 store_size(b3
, max_memobj
);
1013 printf("Memory %15s %15s %15s\n",
1015 store_size(b1
, avg_objsize
);store_size(b2
, min_objsize
);
1016 store_size(b3
, max_objsize
);
1017 printf("User %15s %15s %15s\n",
1020 store_size(b1
, avg_objwaste
);store_size(b2
, min_objwaste
);
1021 store_size(b3
, max_objwaste
);
1022 printf("Loss %15s %15s %15s\n",
1026 static void sort_slabs(void)
1028 struct slabinfo
*s1
,*s2
;
1030 for (s1
= slabinfo
; s1
< slabinfo
+ slabs
; s1
++) {
1031 for (s2
= s1
+ 1; s2
< slabinfo
+ slabs
; s2
++) {
1035 result
= slab_size(s1
) < slab_size(s2
);
1036 else if (sort_active
)
1037 result
= slab_activity(s1
) < slab_activity(s2
);
1039 result
= slab_waste(s1
) < slab_waste(s2
);
1041 result
= strcasecmp(s1
->name
, s2
->name
);
1049 memcpy(&t
, s1
, sizeof(struct slabinfo
));
1050 memcpy(s1
, s2
, sizeof(struct slabinfo
));
1051 memcpy(s2
, &t
, sizeof(struct slabinfo
));
1057 static void sort_aliases(void)
1059 struct aliasinfo
*a1
,*a2
;
1061 for (a1
= aliasinfo
; a1
< aliasinfo
+ aliases
; a1
++) {
1062 for (a2
= a1
+ 1; a2
< aliasinfo
+ aliases
; a2
++) {
1067 if (show_alias
&& !show_inverted
) {
1071 if (strcasecmp(n1
, n2
) > 0) {
1074 memcpy(&t
, a1
, sizeof(struct aliasinfo
));
1075 memcpy(a1
, a2
, sizeof(struct aliasinfo
));
1076 memcpy(a2
, &t
, sizeof(struct aliasinfo
));
1082 static void link_slabs(void)
1084 struct aliasinfo
*a
;
1087 for (a
= aliasinfo
; a
< aliasinfo
+ aliases
; a
++) {
1089 for (s
= slabinfo
; s
< slabinfo
+ slabs
; s
++)
1090 if (strcmp(a
->ref
, s
->name
) == 0) {
1095 if (s
== slabinfo
+ slabs
)
1096 fatal("Unresolved alias %s\n", a
->ref
);
1100 static void alias(void)
1102 struct aliasinfo
*a
;
1103 char *active
= NULL
;
1108 for(a
= aliasinfo
; a
< aliasinfo
+ aliases
; a
++) {
1110 if (!show_single_ref
&& a
->slab
->refs
== 1)
1113 if (!show_inverted
) {
1115 if (strcmp(a
->slab
->name
, active
) == 0) {
1116 printf(" %s", a
->name
);
1120 printf("\n%-12s <- %s", a
->slab
->name
, a
->name
);
1121 active
= a
->slab
->name
;
1124 printf("%-15s -> %s\n", a
->name
, a
->slab
->name
);
1131 static void rename_slabs(void)
1134 struct aliasinfo
*a
;
1136 for (s
= slabinfo
; s
< slabinfo
+ slabs
; s
++) {
1137 if (*s
->name
!= ':')
1140 if (s
->refs
> 1 && !show_first_alias
)
1143 a
= find_one_alias(s
);
1154 static int slab_mismatch(char *slab
)
1156 return regexec(&pattern
, slab
, 0, NULL
, 0);
1159 static void read_slab_dir(void)
1163 struct slabinfo
*slab
= slabinfo
;
1164 struct aliasinfo
*alias
= aliasinfo
;
1169 if (chdir("/sys/kernel/slab") && chdir("/sys/slab"))
1170 fatal("SYSFS support for SLUB not active\n");
1173 while ((de
= readdir(dir
))) {
1174 if (de
->d_name
[0] == '.' ||
1175 (de
->d_name
[0] != ':' && slab_mismatch(de
->d_name
)))
1177 switch (de
->d_type
) {
1179 alias
->name
= strdup(de
->d_name
);
1180 count
= readlink(de
->d_name
, buffer
, sizeof(buffer
)-1);
1183 fatal("Cannot read symlink %s\n", de
->d_name
);
1187 while (p
> buffer
&& p
[-1] != '/')
1189 alias
->ref
= strdup(p
);
1193 if (chdir(de
->d_name
))
1194 fatal("Unable to access slab %s\n", slab
->name
);
1195 slab
->name
= strdup(de
->d_name
);
1198 slab
->aliases
= get_obj("aliases");
1199 slab
->align
= get_obj("align");
1200 slab
->cache_dma
= get_obj("cache_dma");
1201 slab
->cpu_slabs
= get_obj("cpu_slabs");
1202 slab
->destroy_by_rcu
= get_obj("destroy_by_rcu");
1203 slab
->hwcache_align
= get_obj("hwcache_align");
1204 slab
->object_size
= get_obj("object_size");
1205 slab
->objects
= get_obj("objects");
1206 slab
->objects_partial
= get_obj("objects_partial");
1207 slab
->objects_total
= get_obj("objects_total");
1208 slab
->objs_per_slab
= get_obj("objs_per_slab");
1209 slab
->order
= get_obj("order");
1210 slab
->partial
= get_obj("partial");
1211 slab
->partial
= get_obj_and_str("partial", &t
);
1212 decode_numa_list(slab
->numa_partial
, t
);
1214 slab
->poison
= get_obj("poison");
1215 slab
->reclaim_account
= get_obj("reclaim_account");
1216 slab
->red_zone
= get_obj("red_zone");
1217 slab
->sanity_checks
= get_obj("sanity_checks");
1218 slab
->slab_size
= get_obj("slab_size");
1219 slab
->slabs
= get_obj_and_str("slabs", &t
);
1220 decode_numa_list(slab
->numa
, t
);
1222 slab
->store_user
= get_obj("store_user");
1223 slab
->trace
= get_obj("trace");
1224 slab
->alloc_fastpath
= get_obj("alloc_fastpath");
1225 slab
->alloc_slowpath
= get_obj("alloc_slowpath");
1226 slab
->free_fastpath
= get_obj("free_fastpath");
1227 slab
->free_slowpath
= get_obj("free_slowpath");
1228 slab
->free_frozen
= get_obj("free_frozen");
1229 slab
->free_add_partial
= get_obj("free_add_partial");
1230 slab
->free_remove_partial
= get_obj("free_remove_partial");
1231 slab
->alloc_from_partial
= get_obj("alloc_from_partial");
1232 slab
->alloc_slab
= get_obj("alloc_slab");
1233 slab
->alloc_refill
= get_obj("alloc_refill");
1234 slab
->free_slab
= get_obj("free_slab");
1235 slab
->cpuslab_flush
= get_obj("cpuslab_flush");
1236 slab
->deactivate_full
= get_obj("deactivate_full");
1237 slab
->deactivate_empty
= get_obj("deactivate_empty");
1238 slab
->deactivate_to_head
= get_obj("deactivate_to_head");
1239 slab
->deactivate_to_tail
= get_obj("deactivate_to_tail");
1240 slab
->deactivate_remote_frees
= get_obj("deactivate_remote_frees");
1241 slab
->order_fallback
= get_obj("order_fallback");
1242 slab
->cmpxchg_double_cpu_fail
= get_obj("cmpxchg_double_cpu_fail");
1243 slab
->cmpxchg_double_fail
= get_obj("cmpxchg_double_fail");
1244 slab
->cpu_partial_alloc
= get_obj("cpu_partial_alloc");
1245 slab
->cpu_partial_free
= get_obj("cpu_partial_free");
1246 slab
->alloc_node_mismatch
= get_obj("alloc_node_mismatch");
1247 slab
->deactivate_bypass
= get_obj("deactivate_bypass");
1249 if (slab
->name
[0] == ':')
1254 fatal("Unknown file type %lx\n", de
->d_type
);
1258 slabs
= slab
- slabinfo
;
1259 actual_slabs
= slabs
;
1260 aliases
= alias
- aliasinfo
;
1261 if (slabs
> MAX_SLABS
)
1262 fatal("Too many slabs\n");
1263 if (aliases
> MAX_ALIASES
)
1264 fatal("Too many aliases\n");
1267 static void output_slabs(void)
1269 struct slabinfo
*slab
;
1270 int lines
= output_lines
;
1272 for (slab
= slabinfo
; (slab
< slabinfo
+ slabs
) &&
1273 lines
!= 0; slab
++) {
1283 else if (show_track
)
1284 show_tracking(slab
);
1286 slab_validate(slab
);
1295 else if (show_report
)
1300 static void xtotals(void)
1307 printf("\nSlabs sorted by size\n");
1308 printf("--------------------\n");
1314 printf("\nSlabs sorted by loss\n");
1315 printf("--------------------\n");
1324 struct option opts
[] = {
1325 { "aliases", no_argument
, NULL
, 'a' },
1326 { "activity", no_argument
, NULL
, 'A' },
1327 { "debug", optional_argument
, NULL
, 'd' },
1328 { "display-activity", no_argument
, NULL
, 'D' },
1329 { "empty", no_argument
, NULL
, 'e' },
1330 { "first-alias", no_argument
, NULL
, 'f' },
1331 { "help", no_argument
, NULL
, 'h' },
1332 { "inverted", no_argument
, NULL
, 'i'},
1333 { "slabs", no_argument
, NULL
, 'l' },
1334 { "numa", no_argument
, NULL
, 'n' },
1335 { "ops", no_argument
, NULL
, 'o' },
1336 { "shrink", no_argument
, NULL
, 's' },
1337 { "report", no_argument
, NULL
, 'r' },
1338 { "Size", no_argument
, NULL
, 'S'},
1339 { "tracking", no_argument
, NULL
, 't'},
1340 { "Totals", no_argument
, NULL
, 'T'},
1341 { "validate", no_argument
, NULL
, 'v' },
1342 { "zero", no_argument
, NULL
, 'z' },
1343 { "1ref", no_argument
, NULL
, '1'},
1344 { "lines", required_argument
, NULL
, 'N'},
1345 { "Loss", no_argument
, NULL
, 'L'},
1346 { "Xtotals", no_argument
, NULL
, 'X'},
1347 { "Bytes", no_argument
, NULL
, 'B'},
1348 { NULL
, 0, NULL
, 0 }
1351 int main(int argc
, char *argv
[])
1355 char *pattern_source
;
1357 page_size
= getpagesize();
1359 while ((c
= getopt_long(argc
, argv
, "aAd::Defhil1noprstvzTSN:LXB",
1363 show_single_ref
= 1;
1373 if (!debug_opt_scan(optarg
))
1374 fatal("Invalid debug option '%s'\n", optarg
);
1383 show_first_alias
= 1;
1423 output_lines
= atoi(optarg
);
1424 if (output_lines
< 1)
1432 if (output_lines
== -1)
1434 extended_totals
= 1;
1441 fatal("%s: Invalid option '%c'\n", argv
[0], optopt
);
1445 if (!show_slab
&& !show_alias
&& !show_track
&& !show_report
1446 && !validate
&& !shrink
&& !set_debug
&& !show_ops
)
1450 pattern_source
= argv
[optind
];
1452 pattern_source
= ".*";
1454 err
= regcomp(&pattern
, pattern_source
, REG_ICASE
|REG_NOSUB
);
1456 fatal("%s: Invalid pattern '%s' code %d\n",
1457 argv
[0], pattern_source
, err
);
1461 } else if (extended_totals
) {
1463 } else if (show_totals
) {