1 // SPDX-License-Identifier: GPL-2.0
3 * Slabinfo: Tool to get reports about slabs
5 * (C) 2007 sgi, Christoph Lameter
6 * (C) 2011 Linux Foundation, Christoph Lameter
10 * gcc -o slabinfo slabinfo.c
14 #include <sys/types.h>
25 #define MAX_ALIASES 500
26 #define MAX_NODES 1024
32 int aliases
, align
, cache_dma
, cpu_slabs
, destroy_by_rcu
;
33 unsigned int hwcache_align
, object_size
, objs_per_slab
;
34 unsigned int sanity_checks
, slab_size
, store_user
, trace
;
35 int order
, poison
, reclaim_account
, red_zone
;
36 unsigned long partial
, objects
, slabs
, objects_partial
, objects_total
;
37 unsigned long alloc_fastpath
, alloc_slowpath
;
38 unsigned long free_fastpath
, free_slowpath
;
39 unsigned long free_frozen
, free_add_partial
, free_remove_partial
;
40 unsigned long alloc_from_partial
, alloc_slab
, free_slab
, alloc_refill
;
41 unsigned long cpuslab_flush
, deactivate_full
, deactivate_empty
;
42 unsigned long deactivate_to_head
, deactivate_to_tail
;
43 unsigned long deactivate_remote_frees
, order_fallback
;
44 unsigned long cmpxchg_double_cpu_fail
, cmpxchg_double_fail
;
45 unsigned long alloc_node_mismatch
, deactivate_bypass
;
46 unsigned long cpu_partial_alloc
, cpu_partial_free
;
48 int numa_partial
[MAX_NODES
];
49 } slabinfo
[MAX_SLABS
];
54 struct slabinfo
*slab
;
55 } aliasinfo
[MAX_ALIASES
];
84 int output_lines
= -1;
101 static void fatal(const char *x
, ...)
106 vfprintf(stderr
, x
, ap
);
111 static void usage(void)
113 printf("slabinfo 4/15/2011. (c) 2007 sgi/(c) 2011 Linux Foundation.\n\n"
114 "slabinfo [-aABDefhilLnoPrsStTUvXz1] [N=K] [-dafzput] [slab-regexp]\n"
115 "-a|--aliases Show aliases\n"
116 "-A|--activity Most active slabs first\n"
117 "-B|--Bytes Show size in bytes\n"
118 "-D|--display-active Switch line format to activity\n"
119 "-e|--empty Show empty slabs\n"
120 "-f|--first-alias Show first alias\n"
121 "-h|--help Show usage information\n"
122 "-i|--inverted Inverted list\n"
123 "-l|--slabs Show slabs\n"
124 "-L|--Loss Sort by loss\n"
125 "-n|--numa Show NUMA information\n"
126 "-N|--lines=K Show the first K slabs\n"
127 "-o|--ops Show kmem_cache_ops\n"
128 "-P|--partial Sort by number of partial slabs\n"
129 "-r|--report Detailed report on single slabs\n"
130 "-s|--shrink Shrink slabs\n"
131 "-S|--Size Sort by size\n"
132 "-t|--tracking Show alloc/free information\n"
133 "-T|--Totals Show summary information\n"
134 "-U|--Unreclaim Show unreclaimable slabs only\n"
135 "-v|--validate Validate slabs\n"
136 "-X|--Xtotals Show extended summary information\n"
137 "-z|--zero Include empty slabs\n"
138 "-1|--1ref Single reference\n"
141 "-d | --debug Switch off all debug options\n"
142 "-da | --debug=a Switch on all debug options (--debug=FZPU)\n"
145 "-d[afzput] | --debug=[afzput]\n"
146 " f | F Sanity Checks (SLAB_CONSISTENCY_CHECKS)\n"
152 "\nSorting options (--Loss, --Size, --Partial) are mutually exclusive\n"
156 static unsigned long read_obj(const char *name
)
158 FILE *f
= fopen(name
, "r");
163 if (!fgets(buffer
, sizeof(buffer
), f
))
166 if (buffer
[strlen(buffer
)] == '\n')
167 buffer
[strlen(buffer
)] = 0;
169 return strlen(buffer
);
174 * Get the contents of an attribute
176 static unsigned long get_obj(const char *name
)
184 static unsigned long get_obj_and_str(const char *name
, char **x
)
186 unsigned long result
= 0;
191 if (!read_obj(name
)) {
195 result
= strtoul(buffer
, &p
, 10);
203 static void set_obj(struct slabinfo
*s
, const char *name
, int n
)
208 snprintf(x
, 100, "%s/%s", s
->name
, name
);
211 fatal("Cannot write to %s\n", x
);
213 fprintf(f
, "%d\n", n
);
217 static unsigned long read_slab_obj(struct slabinfo
*s
, const char *name
)
223 snprintf(x
, 100, "%s/%s", s
->name
, name
);
229 l
= fread(buffer
, 1, sizeof(buffer
), f
);
238 * Put a size string together
240 static int store_size(char *buffer
, unsigned long value
)
242 unsigned long divisor
= 1;
247 if (value
> 1000000000UL) {
248 divisor
= 100000000UL;
250 } else if (value
> 1000000UL) {
253 } else if (value
> 1000UL) {
260 n
= sprintf(buffer
, "%ld",value
);
267 memmove(buffer
+ n
- 2, buffer
+ n
- 3, 4);
274 static void decode_numa_list(int *numa
, char *t
)
279 memset(numa
, 0, MAX_NODES
* sizeof(int));
286 node
= strtoul(t
, &t
, 10);
289 nr
= strtoul(t
, &t
, 10);
291 if (node
> highest_node
)
299 static void slab_validate(struct slabinfo
*s
)
301 if (strcmp(s
->name
, "*") == 0)
304 set_obj(s
, "validate", 1);
307 static void slab_shrink(struct slabinfo
*s
)
309 if (strcmp(s
->name
, "*") == 0)
312 set_obj(s
, "shrink", 1);
317 static void first_line(void)
320 printf("Name Objects Alloc Free"
321 " %%Fast Fallb O CmpX UL\n");
323 printf("Name Objects Objsize %s "
324 "Slabs/Part/Cpu O/S O %%Fr %%Ef Flg\n",
325 sort_loss
? " Loss" : "Space");
329 * Find the shortest alias of a slab
331 static struct aliasinfo
*find_one_alias(struct slabinfo
*find
)
334 struct aliasinfo
*best
= NULL
;
336 for(a
= aliasinfo
;a
< aliasinfo
+ aliases
; a
++) {
337 if (a
->slab
== find
&&
338 (!best
|| strlen(best
->name
) < strlen(a
->name
))) {
340 if (strncmp(a
->name
,"kmall", 5) == 0)
347 static unsigned long slab_size(struct slabinfo
*s
)
349 return s
->slabs
* (page_size
<< s
->order
);
352 static unsigned long slab_activity(struct slabinfo
*s
)
354 return s
->alloc_fastpath
+ s
->free_fastpath
+
355 s
->alloc_slowpath
+ s
->free_slowpath
;
358 static unsigned long slab_waste(struct slabinfo
*s
)
360 return slab_size(s
) - s
->objects
* s
->object_size
;
363 static void slab_numa(struct slabinfo
*s
, int mode
)
367 if (strcmp(s
->name
, "*") == 0)
371 printf("\n%s: No NUMA information available.\n", s
->name
);
375 if (skip_zero
&& !s
->slabs
)
379 printf("\n%-21s:", mode
? "NUMA nodes" : "Slab");
380 for(node
= 0; node
<= highest_node
; node
++)
381 printf(" %4d", node
);
382 printf("\n----------------------");
383 for(node
= 0; node
<= highest_node
; node
++)
387 printf("%-21s ", mode
? "All slabs" : s
->name
);
388 for(node
= 0; node
<= highest_node
; node
++) {
391 store_size(b
, s
->numa
[node
]);
396 printf("%-21s ", "Partial slabs");
397 for(node
= 0; node
<= highest_node
; node
++) {
400 store_size(b
, s
->numa_partial
[node
]);
408 static void show_tracking(struct slabinfo
*s
)
410 printf("\n%s: Kernel object allocation\n", s
->name
);
411 printf("-----------------------------------------------------------------------\n");
412 if (read_slab_obj(s
, "alloc_calls"))
413 printf("%s", buffer
);
417 printf("\n%s: Kernel object freeing\n", s
->name
);
418 printf("------------------------------------------------------------------------\n");
419 if (read_slab_obj(s
, "free_calls"))
420 printf("%s", buffer
);
426 static void ops(struct slabinfo
*s
)
428 if (strcmp(s
->name
, "*") == 0)
431 if (read_slab_obj(s
, "ops")) {
432 printf("\n%s: kmem_cache operations\n", s
->name
);
433 printf("--------------------------------------------\n");
434 printf("%s", buffer
);
436 printf("\n%s has no kmem_cache operations\n", s
->name
);
439 static const char *onoff(int x
)
446 static void slab_stats(struct slabinfo
*s
)
448 unsigned long total_alloc
;
449 unsigned long total_free
;
455 total_alloc
= s
->alloc_fastpath
+ s
->alloc_slowpath
;
456 total_free
= s
->free_fastpath
+ s
->free_slowpath
;
462 printf("Slab Perf Counter Alloc Free %%Al %%Fr\n");
463 printf("--------------------------------------------------\n");
464 printf("Fastpath %8lu %8lu %3lu %3lu\n",
465 s
->alloc_fastpath
, s
->free_fastpath
,
466 s
->alloc_fastpath
* 100 / total_alloc
,
467 total_free
? s
->free_fastpath
* 100 / total_free
: 0);
468 printf("Slowpath %8lu %8lu %3lu %3lu\n",
469 total_alloc
- s
->alloc_fastpath
, s
->free_slowpath
,
470 (total_alloc
- s
->alloc_fastpath
) * 100 / total_alloc
,
471 total_free
? s
->free_slowpath
* 100 / total_free
: 0);
472 printf("Page Alloc %8lu %8lu %3lu %3lu\n",
473 s
->alloc_slab
, s
->free_slab
,
474 s
->alloc_slab
* 100 / total_alloc
,
475 total_free
? s
->free_slab
* 100 / total_free
: 0);
476 printf("Add partial %8lu %8lu %3lu %3lu\n",
477 s
->deactivate_to_head
+ s
->deactivate_to_tail
,
479 (s
->deactivate_to_head
+ s
->deactivate_to_tail
) * 100 / total_alloc
,
480 total_free
? s
->free_add_partial
* 100 / total_free
: 0);
481 printf("Remove partial %8lu %8lu %3lu %3lu\n",
482 s
->alloc_from_partial
, s
->free_remove_partial
,
483 s
->alloc_from_partial
* 100 / total_alloc
,
484 total_free
? s
->free_remove_partial
* 100 / total_free
: 0);
486 printf("Cpu partial list %8lu %8lu %3lu %3lu\n",
487 s
->cpu_partial_alloc
, s
->cpu_partial_free
,
488 s
->cpu_partial_alloc
* 100 / total_alloc
,
489 total_free
? s
->cpu_partial_free
* 100 / total_free
: 0);
491 printf("RemoteObj/SlabFrozen %8lu %8lu %3lu %3lu\n",
492 s
->deactivate_remote_frees
, s
->free_frozen
,
493 s
->deactivate_remote_frees
* 100 / total_alloc
,
494 total_free
? s
->free_frozen
* 100 / total_free
: 0);
496 printf("Total %8lu %8lu\n\n", total_alloc
, total_free
);
498 if (s
->cpuslab_flush
)
499 printf("Flushes %8lu\n", s
->cpuslab_flush
);
501 total
= s
->deactivate_full
+ s
->deactivate_empty
+
502 s
->deactivate_to_head
+ s
->deactivate_to_tail
+ s
->deactivate_bypass
;
505 printf("\nSlab Deactivation Occurrences %%\n");
506 printf("-------------------------------------------------\n");
507 printf("Slab full %7lu %3lu%%\n",
508 s
->deactivate_full
, (s
->deactivate_full
* 100) / total
);
509 printf("Slab empty %7lu %3lu%%\n",
510 s
->deactivate_empty
, (s
->deactivate_empty
* 100) / total
);
511 printf("Moved to head of partial list %7lu %3lu%%\n",
512 s
->deactivate_to_head
, (s
->deactivate_to_head
* 100) / total
);
513 printf("Moved to tail of partial list %7lu %3lu%%\n",
514 s
->deactivate_to_tail
, (s
->deactivate_to_tail
* 100) / total
);
515 printf("Deactivation bypass %7lu %3lu%%\n",
516 s
->deactivate_bypass
, (s
->deactivate_bypass
* 100) / total
);
517 printf("Refilled from foreign frees %7lu %3lu%%\n",
518 s
->alloc_refill
, (s
->alloc_refill
* 100) / total
);
519 printf("Node mismatch %7lu %3lu%%\n",
520 s
->alloc_node_mismatch
, (s
->alloc_node_mismatch
* 100) / total
);
523 if (s
->cmpxchg_double_fail
|| s
->cmpxchg_double_cpu_fail
) {
524 printf("\nCmpxchg_double Looping\n------------------------\n");
525 printf("Locked Cmpxchg Double redos %lu\nUnlocked Cmpxchg Double redos %lu\n",
526 s
->cmpxchg_double_fail
, s
->cmpxchg_double_cpu_fail
);
530 static void report(struct slabinfo
*s
)
532 if (strcmp(s
->name
, "*") == 0)
535 printf("\nSlabcache: %-15s Aliases: %2d Order : %2d Objects: %lu\n",
536 s
->name
, s
->aliases
, s
->order
, s
->objects
);
537 if (s
->hwcache_align
)
538 printf("** Hardware cacheline aligned\n");
540 printf("** Memory is allocated in a special DMA zone\n");
541 if (s
->destroy_by_rcu
)
542 printf("** Slabs are destroyed via RCU\n");
543 if (s
->reclaim_account
)
544 printf("** Reclaim accounting active\n");
546 printf("\nSizes (bytes) Slabs Debug Memory\n");
547 printf("------------------------------------------------------------------------\n");
548 printf("Object : %7d Total : %7ld Sanity Checks : %s Total: %7ld\n",
549 s
->object_size
, s
->slabs
, onoff(s
->sanity_checks
),
550 s
->slabs
* (page_size
<< s
->order
));
551 printf("SlabObj: %7d Full : %7ld Redzoning : %s Used : %7ld\n",
552 s
->slab_size
, s
->slabs
- s
->partial
- s
->cpu_slabs
,
553 onoff(s
->red_zone
), s
->objects
* s
->object_size
);
554 printf("SlabSiz: %7d Partial: %7ld Poisoning : %s Loss : %7ld\n",
555 page_size
<< s
->order
, s
->partial
, onoff(s
->poison
),
556 s
->slabs
* (page_size
<< s
->order
) - s
->objects
* s
->object_size
);
557 printf("Loss : %7d CpuSlab: %7d Tracking : %s Lalig: %7ld\n",
558 s
->slab_size
- s
->object_size
, s
->cpu_slabs
, onoff(s
->store_user
),
559 (s
->slab_size
- s
->object_size
) * s
->objects
);
560 printf("Align : %7d Objects: %7d Tracing : %s Lpadd: %7ld\n",
561 s
->align
, s
->objs_per_slab
, onoff(s
->trace
),
562 ((page_size
<< s
->order
) - s
->objs_per_slab
* s
->slab_size
) *
571 static void slabcache(struct slabinfo
*s
)
578 if (strcmp(s
->name
, "*") == 0)
581 if (unreclaim_only
&& s
->reclaim_account
)
584 if (actual_slabs
== 1) {
589 if (skip_zero
&& !show_empty
&& !s
->slabs
)
592 if (show_empty
&& s
->slabs
)
596 store_size(size_str
, slab_size(s
));
598 store_size(size_str
, slab_waste(s
));
599 snprintf(dist_str
, 40, "%lu/%lu/%d", s
->slabs
- s
->cpu_slabs
,
600 s
->partial
, s
->cpu_slabs
);
609 if (s
->hwcache_align
)
613 if (s
->reclaim_account
)
617 if (s
->sanity_checks
)
626 unsigned long total_alloc
;
627 unsigned long total_free
;
629 total_alloc
= s
->alloc_fastpath
+ s
->alloc_slowpath
;
630 total_free
= s
->free_fastpath
+ s
->free_slowpath
;
632 printf("%-21s %8ld %10ld %10ld %3ld %3ld %5ld %1d %4ld %4ld\n",
634 total_alloc
, total_free
,
635 total_alloc
? (s
->alloc_fastpath
* 100 / total_alloc
) : 0,
636 total_free
? (s
->free_fastpath
* 100 / total_free
) : 0,
637 s
->order_fallback
, s
->order
, s
->cmpxchg_double_fail
,
638 s
->cmpxchg_double_cpu_fail
);
640 printf("%-21s %8ld %7d %15s %14s %4d %1d %3ld %3ld %s\n",
641 s
->name
, s
->objects
, s
->object_size
, size_str
, dist_str
,
642 s
->objs_per_slab
, s
->order
,
643 s
->slabs
? (s
->partial
* 100) / s
->slabs
: 100,
644 s
->slabs
? (s
->objects
* s
->object_size
* 100) /
645 (s
->slabs
* (page_size
<< s
->order
)) : 100,
651 * Analyze debug options. Return false if something is amiss.
653 static int debug_opt_scan(char *opt
)
655 if (!opt
|| !opt
[0] || strcmp(opt
, "-") == 0)
658 if (strcasecmp(opt
, "a") == 0) {
702 static int slab_empty(struct slabinfo
*s
)
708 * We may still have slabs even if there are no objects. Shrinking will
712 set_obj(s
, "shrink", 1);
717 static void slab_debug(struct slabinfo
*s
)
719 if (strcmp(s
->name
, "*") == 0)
722 if (sanity
&& !s
->sanity_checks
) {
723 set_obj(s
, "sanity", 1);
725 if (!sanity
&& s
->sanity_checks
) {
727 set_obj(s
, "sanity", 0);
729 fprintf(stderr
, "%s not empty cannot disable sanity checks\n", s
->name
);
731 if (redzone
&& !s
->red_zone
) {
733 set_obj(s
, "red_zone", 1);
735 fprintf(stderr
, "%s not empty cannot enable redzoning\n", s
->name
);
737 if (!redzone
&& s
->red_zone
) {
739 set_obj(s
, "red_zone", 0);
741 fprintf(stderr
, "%s not empty cannot disable redzoning\n", s
->name
);
743 if (poison
&& !s
->poison
) {
745 set_obj(s
, "poison", 1);
747 fprintf(stderr
, "%s not empty cannot enable poisoning\n", s
->name
);
749 if (!poison
&& s
->poison
) {
751 set_obj(s
, "poison", 0);
753 fprintf(stderr
, "%s not empty cannot disable poisoning\n", s
->name
);
755 if (tracking
&& !s
->store_user
) {
757 set_obj(s
, "store_user", 1);
759 fprintf(stderr
, "%s not empty cannot enable tracking\n", s
->name
);
761 if (!tracking
&& s
->store_user
) {
763 set_obj(s
, "store_user", 0);
765 fprintf(stderr
, "%s not empty cannot disable tracking\n", s
->name
);
767 if (tracing
&& !s
->trace
) {
769 set_obj(s
, "trace", 1);
771 fprintf(stderr
, "%s can only enable trace for one slab at a time\n", s
->name
);
773 if (!tracing
&& s
->trace
)
774 set_obj(s
, "trace", 1);
777 static void totals(void)
782 char b1
[20], b2
[20], b3
[20], b4
[20];
783 unsigned long long max
= 1ULL << 63;
786 unsigned long long min_objsize
= max
, max_objsize
= 0, avg_objsize
;
788 /* Number of partial slabs in a slabcache */
789 unsigned long long min_partial
= max
, max_partial
= 0,
790 avg_partial
, total_partial
= 0;
792 /* Number of slabs in a slab cache */
793 unsigned long long min_slabs
= max
, max_slabs
= 0,
794 avg_slabs
, total_slabs
= 0;
796 /* Size of the whole slab */
797 unsigned long long min_size
= max
, max_size
= 0,
798 avg_size
, total_size
= 0;
800 /* Bytes used for object storage in a slab */
801 unsigned long long min_used
= max
, max_used
= 0,
802 avg_used
, total_used
= 0;
804 /* Waste: Bytes used for alignment and padding */
805 unsigned long long min_waste
= max
, max_waste
= 0,
806 avg_waste
, total_waste
= 0;
807 /* Number of objects in a slab */
808 unsigned long long min_objects
= max
, max_objects
= 0,
809 avg_objects
, total_objects
= 0;
810 /* Waste per object */
811 unsigned long long min_objwaste
= max
,
812 max_objwaste
= 0, avg_objwaste
,
815 /* Memory per object */
816 unsigned long long min_memobj
= max
,
817 max_memobj
= 0, avg_memobj
,
820 /* Percentage of partial slabs per slab */
821 unsigned long min_ppart
= 100, max_ppart
= 0,
822 avg_ppart
, total_ppart
= 0;
824 /* Number of objects in partial slabs */
825 unsigned long min_partobj
= max
, max_partobj
= 0,
826 avg_partobj
, total_partobj
= 0;
828 /* Percentage of partial objects of all objects in a slab */
829 unsigned long min_ppartobj
= 100, max_ppartobj
= 0,
830 avg_ppartobj
, total_ppartobj
= 0;
833 for (s
= slabinfo
; s
< slabinfo
+ slabs
; s
++) {
834 unsigned long long size
;
836 unsigned long long wasted
;
837 unsigned long long objwaste
;
838 unsigned long percentage_partial_slabs
;
839 unsigned long percentage_partial_objs
;
841 if (!s
->slabs
|| !s
->objects
)
847 used
= s
->objects
* s
->object_size
;
848 wasted
= size
- used
;
849 objwaste
= s
->slab_size
- s
->object_size
;
851 percentage_partial_slabs
= s
->partial
* 100 / s
->slabs
;
852 if (percentage_partial_slabs
> 100)
853 percentage_partial_slabs
= 100;
855 percentage_partial_objs
= s
->objects_partial
* 100
858 if (percentage_partial_objs
> 100)
859 percentage_partial_objs
= 100;
861 if (s
->object_size
< min_objsize
)
862 min_objsize
= s
->object_size
;
863 if (s
->partial
< min_partial
)
864 min_partial
= s
->partial
;
865 if (s
->slabs
< min_slabs
)
866 min_slabs
= s
->slabs
;
869 if (wasted
< min_waste
)
871 if (objwaste
< min_objwaste
)
872 min_objwaste
= objwaste
;
873 if (s
->objects
< min_objects
)
874 min_objects
= s
->objects
;
877 if (s
->objects_partial
< min_partobj
)
878 min_partobj
= s
->objects_partial
;
879 if (percentage_partial_slabs
< min_ppart
)
880 min_ppart
= percentage_partial_slabs
;
881 if (percentage_partial_objs
< min_ppartobj
)
882 min_ppartobj
= percentage_partial_objs
;
883 if (s
->slab_size
< min_memobj
)
884 min_memobj
= s
->slab_size
;
886 if (s
->object_size
> max_objsize
)
887 max_objsize
= s
->object_size
;
888 if (s
->partial
> max_partial
)
889 max_partial
= s
->partial
;
890 if (s
->slabs
> max_slabs
)
891 max_slabs
= s
->slabs
;
894 if (wasted
> max_waste
)
896 if (objwaste
> max_objwaste
)
897 max_objwaste
= objwaste
;
898 if (s
->objects
> max_objects
)
899 max_objects
= s
->objects
;
902 if (s
->objects_partial
> max_partobj
)
903 max_partobj
= s
->objects_partial
;
904 if (percentage_partial_slabs
> max_ppart
)
905 max_ppart
= percentage_partial_slabs
;
906 if (percentage_partial_objs
> max_ppartobj
)
907 max_ppartobj
= percentage_partial_objs
;
908 if (s
->slab_size
> max_memobj
)
909 max_memobj
= s
->slab_size
;
911 total_partial
+= s
->partial
;
912 total_slabs
+= s
->slabs
;
914 total_waste
+= wasted
;
916 total_objects
+= s
->objects
;
918 total_partobj
+= s
->objects_partial
;
919 total_ppart
+= percentage_partial_slabs
;
920 total_ppartobj
+= percentage_partial_objs
;
922 total_objwaste
+= s
->objects
* objwaste
;
923 total_objsize
+= s
->objects
* s
->slab_size
;
926 if (!total_objects
) {
927 printf("No objects\n");
931 printf("No slabs\n");
935 /* Per slab averages */
936 avg_partial
= total_partial
/ used_slabs
;
937 avg_slabs
= total_slabs
/ used_slabs
;
938 avg_size
= total_size
/ used_slabs
;
939 avg_waste
= total_waste
/ used_slabs
;
941 avg_objects
= total_objects
/ used_slabs
;
942 avg_used
= total_used
/ used_slabs
;
943 avg_partobj
= total_partobj
/ used_slabs
;
944 avg_ppart
= total_ppart
/ used_slabs
;
945 avg_ppartobj
= total_ppartobj
/ used_slabs
;
947 /* Per object object sizes */
948 avg_objsize
= total_used
/ total_objects
;
949 avg_objwaste
= total_objwaste
/ total_objects
;
950 avg_partobj
= total_partobj
* 100 / total_objects
;
951 avg_memobj
= total_objsize
/ total_objects
;
953 printf("Slabcache Totals\n");
954 printf("----------------\n");
955 printf("Slabcaches : %15d Aliases : %11d->%-3d Active: %3d\n",
956 slabs
, aliases
, alias_targets
, used_slabs
);
958 store_size(b1
, total_size
);store_size(b2
, total_waste
);
959 store_size(b3
, total_waste
* 100 / total_used
);
960 printf("Memory used: %15s # Loss : %15s MRatio:%6s%%\n", b1
, b2
, b3
);
962 store_size(b1
, total_objects
);store_size(b2
, total_partobj
);
963 store_size(b3
, total_partobj
* 100 / total_objects
);
964 printf("# Objects : %15s # PartObj: %15s ORatio:%6s%%\n", b1
, b2
, b3
);
967 printf("Per Cache Average "
969 printf("---------------------------------------"
970 "-------------------------------------\n");
972 store_size(b1
, avg_objects
);store_size(b2
, min_objects
);
973 store_size(b3
, max_objects
);store_size(b4
, total_objects
);
974 printf("#Objects %15s %15s %15s %15s\n",
977 store_size(b1
, avg_slabs
);store_size(b2
, min_slabs
);
978 store_size(b3
, max_slabs
);store_size(b4
, total_slabs
);
979 printf("#Slabs %15s %15s %15s %15s\n",
982 store_size(b1
, avg_partial
);store_size(b2
, min_partial
);
983 store_size(b3
, max_partial
);store_size(b4
, total_partial
);
984 printf("#PartSlab %15s %15s %15s %15s\n",
986 store_size(b1
, avg_ppart
);store_size(b2
, min_ppart
);
987 store_size(b3
, max_ppart
);
988 store_size(b4
, total_partial
* 100 / total_slabs
);
989 printf("%%PartSlab%15s%% %15s%% %15s%% %15s%%\n",
992 store_size(b1
, avg_partobj
);store_size(b2
, min_partobj
);
993 store_size(b3
, max_partobj
);
994 store_size(b4
, total_partobj
);
995 printf("PartObjs %15s %15s %15s %15s\n",
998 store_size(b1
, avg_ppartobj
);store_size(b2
, min_ppartobj
);
999 store_size(b3
, max_ppartobj
);
1000 store_size(b4
, total_partobj
* 100 / total_objects
);
1001 printf("%% PartObj%15s%% %15s%% %15s%% %15s%%\n",
1004 store_size(b1
, avg_size
);store_size(b2
, min_size
);
1005 store_size(b3
, max_size
);store_size(b4
, total_size
);
1006 printf("Memory %15s %15s %15s %15s\n",
1009 store_size(b1
, avg_used
);store_size(b2
, min_used
);
1010 store_size(b3
, max_used
);store_size(b4
, total_used
);
1011 printf("Used %15s %15s %15s %15s\n",
1014 store_size(b1
, avg_waste
);store_size(b2
, min_waste
);
1015 store_size(b3
, max_waste
);store_size(b4
, total_waste
);
1016 printf("Loss %15s %15s %15s %15s\n",
1020 printf("Per Object Average "
1022 printf("---------------------------------------"
1023 "--------------------\n");
1025 store_size(b1
, avg_memobj
);store_size(b2
, min_memobj
);
1026 store_size(b3
, max_memobj
);
1027 printf("Memory %15s %15s %15s\n",
1029 store_size(b1
, avg_objsize
);store_size(b2
, min_objsize
);
1030 store_size(b3
, max_objsize
);
1031 printf("User %15s %15s %15s\n",
1034 store_size(b1
, avg_objwaste
);store_size(b2
, min_objwaste
);
1035 store_size(b3
, max_objwaste
);
1036 printf("Loss %15s %15s %15s\n",
1040 static void sort_slabs(void)
1042 struct slabinfo
*s1
,*s2
;
1044 for (s1
= slabinfo
; s1
< slabinfo
+ slabs
; s1
++) {
1045 for (s2
= s1
+ 1; s2
< slabinfo
+ slabs
; s2
++) {
1049 result
= slab_size(s1
) < slab_size(s2
);
1050 else if (sort_active
)
1051 result
= slab_activity(s1
) < slab_activity(s2
);
1053 result
= slab_waste(s1
) < slab_waste(s2
);
1054 else if (sort_partial
)
1055 result
= s1
->partial
< s2
->partial
;
1057 result
= strcasecmp(s1
->name
, s2
->name
);
1065 memcpy(&t
, s1
, sizeof(struct slabinfo
));
1066 memcpy(s1
, s2
, sizeof(struct slabinfo
));
1067 memcpy(s2
, &t
, sizeof(struct slabinfo
));
1073 static void sort_aliases(void)
1075 struct aliasinfo
*a1
,*a2
;
1077 for (a1
= aliasinfo
; a1
< aliasinfo
+ aliases
; a1
++) {
1078 for (a2
= a1
+ 1; a2
< aliasinfo
+ aliases
; a2
++) {
1083 if (show_alias
&& !show_inverted
) {
1087 if (strcasecmp(n1
, n2
) > 0) {
1090 memcpy(&t
, a1
, sizeof(struct aliasinfo
));
1091 memcpy(a1
, a2
, sizeof(struct aliasinfo
));
1092 memcpy(a2
, &t
, sizeof(struct aliasinfo
));
1098 static void link_slabs(void)
1100 struct aliasinfo
*a
;
1103 for (a
= aliasinfo
; a
< aliasinfo
+ aliases
; a
++) {
1105 for (s
= slabinfo
; s
< slabinfo
+ slabs
; s
++)
1106 if (strcmp(a
->ref
, s
->name
) == 0) {
1111 if (s
== slabinfo
+ slabs
)
1112 fatal("Unresolved alias %s\n", a
->ref
);
1116 static void alias(void)
1118 struct aliasinfo
*a
;
1119 char *active
= NULL
;
1124 for(a
= aliasinfo
; a
< aliasinfo
+ aliases
; a
++) {
1126 if (!show_single_ref
&& a
->slab
->refs
== 1)
1129 if (!show_inverted
) {
1131 if (strcmp(a
->slab
->name
, active
) == 0) {
1132 printf(" %s", a
->name
);
1136 printf("\n%-12s <- %s", a
->slab
->name
, a
->name
);
1137 active
= a
->slab
->name
;
1140 printf("%-15s -> %s\n", a
->name
, a
->slab
->name
);
1147 static void rename_slabs(void)
1150 struct aliasinfo
*a
;
1152 for (s
= slabinfo
; s
< slabinfo
+ slabs
; s
++) {
1153 if (*s
->name
!= ':')
1156 if (s
->refs
> 1 && !show_first_alias
)
1159 a
= find_one_alias(s
);
1170 static int slab_mismatch(char *slab
)
1172 return regexec(&pattern
, slab
, 0, NULL
, 0);
1175 static void read_slab_dir(void)
1179 struct slabinfo
*slab
= slabinfo
;
1180 struct aliasinfo
*alias
= aliasinfo
;
1185 if (chdir("/sys/kernel/slab") && chdir("/sys/slab"))
1186 fatal("SYSFS support for SLUB not active\n");
1189 while ((de
= readdir(dir
))) {
1190 if (de
->d_name
[0] == '.' ||
1191 (de
->d_name
[0] != ':' && slab_mismatch(de
->d_name
)))
1193 switch (de
->d_type
) {
1195 alias
->name
= strdup(de
->d_name
);
1196 count
= readlink(de
->d_name
, buffer
, sizeof(buffer
)-1);
1199 fatal("Cannot read symlink %s\n", de
->d_name
);
1203 while (p
> buffer
&& p
[-1] != '/')
1205 alias
->ref
= strdup(p
);
1209 if (chdir(de
->d_name
))
1210 fatal("Unable to access slab %s\n", slab
->name
);
1211 slab
->name
= strdup(de
->d_name
);
1214 slab
->aliases
= get_obj("aliases");
1215 slab
->align
= get_obj("align");
1216 slab
->cache_dma
= get_obj("cache_dma");
1217 slab
->cpu_slabs
= get_obj("cpu_slabs");
1218 slab
->destroy_by_rcu
= get_obj("destroy_by_rcu");
1219 slab
->hwcache_align
= get_obj("hwcache_align");
1220 slab
->object_size
= get_obj("object_size");
1221 slab
->objects
= get_obj("objects");
1222 slab
->objects_partial
= get_obj("objects_partial");
1223 slab
->objects_total
= get_obj("objects_total");
1224 slab
->objs_per_slab
= get_obj("objs_per_slab");
1225 slab
->order
= get_obj("order");
1226 slab
->partial
= get_obj("partial");
1227 slab
->partial
= get_obj_and_str("partial", &t
);
1228 decode_numa_list(slab
->numa_partial
, t
);
1230 slab
->poison
= get_obj("poison");
1231 slab
->reclaim_account
= get_obj("reclaim_account");
1232 slab
->red_zone
= get_obj("red_zone");
1233 slab
->sanity_checks
= get_obj("sanity_checks");
1234 slab
->slab_size
= get_obj("slab_size");
1235 slab
->slabs
= get_obj_and_str("slabs", &t
);
1236 decode_numa_list(slab
->numa
, t
);
1238 slab
->store_user
= get_obj("store_user");
1239 slab
->trace
= get_obj("trace");
1240 slab
->alloc_fastpath
= get_obj("alloc_fastpath");
1241 slab
->alloc_slowpath
= get_obj("alloc_slowpath");
1242 slab
->free_fastpath
= get_obj("free_fastpath");
1243 slab
->free_slowpath
= get_obj("free_slowpath");
1244 slab
->free_frozen
= get_obj("free_frozen");
1245 slab
->free_add_partial
= get_obj("free_add_partial");
1246 slab
->free_remove_partial
= get_obj("free_remove_partial");
1247 slab
->alloc_from_partial
= get_obj("alloc_from_partial");
1248 slab
->alloc_slab
= get_obj("alloc_slab");
1249 slab
->alloc_refill
= get_obj("alloc_refill");
1250 slab
->free_slab
= get_obj("free_slab");
1251 slab
->cpuslab_flush
= get_obj("cpuslab_flush");
1252 slab
->deactivate_full
= get_obj("deactivate_full");
1253 slab
->deactivate_empty
= get_obj("deactivate_empty");
1254 slab
->deactivate_to_head
= get_obj("deactivate_to_head");
1255 slab
->deactivate_to_tail
= get_obj("deactivate_to_tail");
1256 slab
->deactivate_remote_frees
= get_obj("deactivate_remote_frees");
1257 slab
->order_fallback
= get_obj("order_fallback");
1258 slab
->cmpxchg_double_cpu_fail
= get_obj("cmpxchg_double_cpu_fail");
1259 slab
->cmpxchg_double_fail
= get_obj("cmpxchg_double_fail");
1260 slab
->cpu_partial_alloc
= get_obj("cpu_partial_alloc");
1261 slab
->cpu_partial_free
= get_obj("cpu_partial_free");
1262 slab
->alloc_node_mismatch
= get_obj("alloc_node_mismatch");
1263 slab
->deactivate_bypass
= get_obj("deactivate_bypass");
1265 if (slab
->name
[0] == ':')
1270 fatal("Unknown file type %lx\n", de
->d_type
);
1274 slabs
= slab
- slabinfo
;
1275 actual_slabs
= slabs
;
1276 aliases
= alias
- aliasinfo
;
1277 if (slabs
> MAX_SLABS
)
1278 fatal("Too many slabs\n");
1279 if (aliases
> MAX_ALIASES
)
1280 fatal("Too many aliases\n");
1283 static void output_slabs(void)
1285 struct slabinfo
*slab
;
1286 int lines
= output_lines
;
1288 for (slab
= slabinfo
; (slab
< slabinfo
+ slabs
) &&
1289 lines
!= 0; slab
++) {
1299 else if (show_track
)
1300 show_tracking(slab
);
1302 slab_validate(slab
);
1311 else if (show_report
)
1316 static void _xtotals(char *heading
, char *underline
,
1317 int loss
, int size
, int partial
)
1319 printf("%s%s", heading
, underline
);
1323 sort_partial
= partial
;
1328 static void xtotals(void)
1330 char *heading
, *underline
;
1337 heading
= "\nSlabs sorted by size\n";
1338 underline
= "--------------------\n";
1339 _xtotals(heading
, underline
, 0, 1, 0);
1341 heading
= "\nSlabs sorted by loss\n";
1342 underline
= "--------------------\n";
1343 _xtotals(heading
, underline
, 1, 0, 0);
1345 heading
= "\nSlabs sorted by number of partial slabs\n";
1346 underline
= "---------------------------------------\n";
1347 _xtotals(heading
, underline
, 0, 0, 1);
1352 struct option opts
[] = {
1353 { "aliases", no_argument
, NULL
, 'a' },
1354 { "activity", no_argument
, NULL
, 'A' },
1355 { "Bytes", no_argument
, NULL
, 'B'},
1356 { "debug", optional_argument
, NULL
, 'd' },
1357 { "display-activity", no_argument
, NULL
, 'D' },
1358 { "empty", no_argument
, NULL
, 'e' },
1359 { "first-alias", no_argument
, NULL
, 'f' },
1360 { "help", no_argument
, NULL
, 'h' },
1361 { "inverted", no_argument
, NULL
, 'i'},
1362 { "slabs", no_argument
, NULL
, 'l' },
1363 { "Loss", no_argument
, NULL
, 'L'},
1364 { "numa", no_argument
, NULL
, 'n' },
1365 { "lines", required_argument
, NULL
, 'N'},
1366 { "ops", no_argument
, NULL
, 'o' },
1367 { "partial", no_argument
, NULL
, 'p'},
1368 { "report", no_argument
, NULL
, 'r' },
1369 { "shrink", no_argument
, NULL
, 's' },
1370 { "Size", no_argument
, NULL
, 'S'},
1371 { "tracking", no_argument
, NULL
, 't'},
1372 { "Totals", no_argument
, NULL
, 'T'},
1373 { "Unreclaim", no_argument
, NULL
, 'U'},
1374 { "validate", no_argument
, NULL
, 'v' },
1375 { "Xtotals", no_argument
, NULL
, 'X'},
1376 { "zero", no_argument
, NULL
, 'z' },
1377 { "1ref", no_argument
, NULL
, '1'},
1378 { NULL
, 0, NULL
, 0 }
1381 int main(int argc
, char *argv
[])
1385 char *pattern_source
;
1387 page_size
= getpagesize();
1389 while ((c
= getopt_long(argc
, argv
, "aABd::DefhilLnN:oPrsStTUvXz1",
1403 if (!debug_opt_scan(optarg
))
1404 fatal("Invalid debug option '%s'\n", optarg
);
1413 show_first_alias
= 1;
1432 output_lines
= atoi(optarg
);
1433 if (output_lines
< 1)
1465 if (output_lines
== -1)
1467 extended_totals
= 1;
1474 show_single_ref
= 1;
1477 fatal("%s: Invalid option '%c'\n", argv
[0], optopt
);
1481 if (!show_slab
&& !show_alias
&& !show_track
&& !show_report
1482 && !validate
&& !shrink
&& !set_debug
&& !show_ops
)
1486 pattern_source
= argv
[optind
];
1488 pattern_source
= ".*";
1490 err
= regcomp(&pattern
, pattern_source
, REG_ICASE
|REG_NOSUB
);
1492 fatal("%s: Invalid pattern '%s' code %d\n",
1493 argv
[0], pattern_source
, err
);
1497 } else if (extended_totals
) {
1499 } else if (show_totals
) {