Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

radix tree test suite: add allocation counts and size to kmem_cache

Add functions to get the number of allocations, and total allocations from
a kmem_cache. Also add a function to get the allocated size and a way to
zero the total allocations.

Link: https://lkml.kernel.org/r/20220906194824.2110408-5-Liam.Howlett@oracle.com
Signed-off-by: Liam R. Howlett <Liam.Howlett@Oracle.com>
Tested-by: Yu Zhao <yuzhao@google.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: David Howells <dhowells@redhat.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: "Matthew Wilcox (Oracle)" <willy@infradead.org>
Cc: SeongJae Park <sj@kernel.org>
Cc: Sven Schnelle <svens@linux.ibm.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: Will Deacon <will@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Liam R. Howlett and committed by
Andrew Morton
000a4493 e73cb368

+27 -1
+27 -1
tools/testing/radix-tree/linux.c
··· 24 24 void *objs; 25 25 void (*ctor)(void *); 26 26 unsigned int non_kernel; 27 + unsigned long nr_allocated; 28 + unsigned long nr_tallocated; 27 29 }; 28 30 29 31 void kmem_cache_set_non_kernel(struct kmem_cache *cachep, unsigned int val) ··· 33 31 cachep->non_kernel = val; 34 32 } 35 33 34 + unsigned long kmem_cache_get_alloc(struct kmem_cache *cachep) 35 + { 36 + return cachep->size * cachep->nr_allocated; 37 + } 38 + 39 + unsigned long kmem_cache_nr_allocated(struct kmem_cache *cachep) 40 + { 41 + return cachep->nr_allocated; 42 + } 43 + 44 + unsigned long kmem_cache_nr_tallocated(struct kmem_cache *cachep) 45 + { 46 + return cachep->nr_tallocated; 47 + } 48 + 49 + void kmem_cache_zero_nr_tallocated(struct kmem_cache *cachep) 50 + { 51 + cachep->nr_tallocated = 0; 52 + } 53 + 36 54 void *kmem_cache_alloc_lru(struct kmem_cache *cachep, struct list_lru *lru, 37 55 int gfp) 38 - 39 56 { 40 57 void *p; 41 58 ··· 85 64 memset(p, 0, cachep->size); 86 65 } 87 66 67 + uatomic_inc(&cachep->nr_allocated); 88 68 uatomic_inc(&nr_allocated); 69 + uatomic_inc(&cachep->nr_tallocated); 89 70 if (kmalloc_verbose) 90 71 printf("Allocating %p from slab\n", p); 91 72 return p; ··· 97 74 { 98 75 assert(objp); 99 76 uatomic_dec(&nr_allocated); 77 + uatomic_dec(&cachep->nr_allocated); 100 78 if (kmalloc_verbose) 101 79 printf("Freeing %p to slab\n", objp); 102 80 pthread_mutex_lock(&cachep->lock); ··· 123 99 ret->size = size; 124 100 ret->align = align; 125 101 ret->nr_objs = 0; 102 + ret->nr_allocated = 0; 103 + ret->nr_tallocated = 0; 126 104 ret->objs = NULL; 127 105 ret->ctor = ctor; 128 106 ret->non_kernel = 0;