-
Notifications
You must be signed in to change notification settings - Fork 103
/
slab_bulk_test01.c
293 lines (249 loc) · 6.51 KB
/
slab_bulk_test01.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Synthetic micro-benchmarking of slab bulk
*
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/time.h>
#include <linux/time_bench.h>
#include <linux/spinlock.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/skbuff.h>
static int verbose=1;
/* If SLAB debugging is enabled the per object cost is approx a factor
* between 500 - 1000 times slower. Thus, adjust the default number
* of loops in case CONFIG_SLUB_DEBUG_ON=y
*/
#if defined(CONFIG_SLUB_DEBUG_ON) || defined(CONFIG_DEBUG_SLAB)
# define DEFAULT_LOOPS 10000
#else
# define DEFAULT_LOOPS 10000000
#endif
static uint32_t loops = DEFAULT_LOOPS;
module_param(loops, uint, 0);
MODULE_PARM_DESC(loops, "Parameter for loops in bench");
struct my_elem {
/* element used for benchmark testing */
struct sk_buff skb;
};
/* Timing at the nanosec level, we need to know the overhead
* introduced by the for loop itself */
static int time_bench_for_loop(
struct time_bench_record *rec, void *data)
{
int i;
uint64_t loops_cnt = 0;
time_bench_start(rec);
/** Loop to measure **/
for (i = 0; i < rec->loops; i++) {
loops_cnt++;
barrier(); /* avoid compiler to optimize this loop */
}
time_bench_stop(rec, loops_cnt);
return loops_cnt;
}
/* For comparison benchmark against the fastpath of the
* slab/kmem_cache allocator
*/
static int benchmark_kmem_cache_fastpath_reuse(
struct time_bench_record *rec, void *data)
{
uint64_t loops_cnt = 0;
int i;
struct my_elem *elem;
struct kmem_cache *slab;
slab = kmem_cache_create("slab_bench_test1", sizeof(struct my_elem),
0, SLAB_HWCACHE_ALIGN, NULL);
time_bench_start(rec);
/** Loop to measure **/
for (i = 0; i < rec->loops; i++) {
/* request new elem */
elem = kmem_cache_alloc(slab, GFP_ATOMIC);
if (elem == NULL)
goto out;
barrier(); /* compiler barrier */
/* return elem */
kmem_cache_free(slab, elem);
/* NOTICE THIS COUNTS alloc+free together*/
loops_cnt++;
}
out:
time_bench_stop(rec, loops_cnt);
/* cleanup */
kmem_cache_destroy(slab);
return loops_cnt;
}
/* Fallback versions copy-pasted here, as they are defined in
* slab_common that we cannot link with.
*
* Force them to be "noinlined" as current patch for slab_common cause
* them to be a function call. To keep comparison the same.
*/
noinline
void my__kmem_cache_free_bulk(struct kmem_cache *s, size_t nr, void **p)
{
size_t i;
for (i = 0; i < nr; i++)
kmem_cache_free(s, p[i]);
}
noinline
bool my__kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t nr,
void **p)
{
size_t i;
for (i = 0; i < nr; i++) {
void *x = p[i] = kmem_cache_alloc(s, flags);
if (!x) {
my__kmem_cache_free_bulk(s, i, p);
return false;
}
}
return true;
}
static int benchmark_slab_fallback_bulk(
struct time_bench_record *rec, void *data)
{
#define MAX_BULK 250
void *objs[MAX_BULK];
uint64_t loops_cnt = 0;
int i;
bool success;
struct kmem_cache *slab;
size_t bulk = rec->step;
if (bulk > MAX_BULK) {
pr_warn("%s() bulk(%lu) request too big cap at %d\n",
__func__, bulk, MAX_BULK);
bulk = MAX_BULK;
}
/* loop count is limited to 32-bit due to div_u64_rem() use */
if (((uint64_t)rec->loops * bulk *2) >= ((1ULL<<32)-1)) {
pr_err("Loop cnt too big will overflow 32-bit\n");
return 0;
}
slab = kmem_cache_create("slab_bench_test2", sizeof(struct my_elem),
0, SLAB_HWCACHE_ALIGN, NULL);
time_bench_start(rec);
/** Loop to measure **/
for (i = 0; i < rec->loops; i++) {
/* request bulk elems */
success = my__kmem_cache_alloc_bulk(slab, GFP_ATOMIC, bulk, objs);
if (!success)
goto out;
barrier(); /* compiler barrier */
/* bulk return elems */
my__kmem_cache_free_bulk(slab, bulk, objs);
/* NOTICE THIS COUNTS (bulk) alloc+free together*/
loops_cnt+= bulk;
}
out:
time_bench_stop(rec, loops_cnt);
/* cleanup */
kmem_cache_destroy(slab);
return loops_cnt;
#undef MAX_BULK
}
static int benchmark_slab_bulk01(
struct time_bench_record *rec, void *data)
{
#define MAX_BULK 250
void *objs[MAX_BULK];
uint64_t loops_cnt = 0;
int i;
bool success;
struct kmem_cache *slab;
size_t bulk = rec->step;
if (bulk > MAX_BULK) {
pr_warn("%s() bulk(%lu) request too big cap at %d\n",
__func__, bulk, MAX_BULK);
bulk = MAX_BULK;
}
/* loop count is limited to 32-bit due to div_u64_rem() use */
if (((uint64_t)rec->loops * bulk *2) >= ((1ULL<<32)-1)) {
pr_err("Loop cnt too big will overflow 32-bit\n");
return 0;
}
slab = kmem_cache_create("slab_bench_test3", sizeof(struct my_elem),
0, SLAB_HWCACHE_ALIGN, NULL);
time_bench_start(rec);
/** Loop to measure **/
for (i = 0; i < rec->loops; i++) {
/* request bulk elems */
success = kmem_cache_alloc_bulk(slab, GFP_ATOMIC, bulk, objs);
if (!success)
goto out;
barrier(); /* compiler barrier */
/* bulk return elems */
kmem_cache_free_bulk(slab, bulk, objs);
/* NOTICE THIS COUNTS (bulk) alloc+free together*/
loops_cnt+= bulk;
}
out:
time_bench_stop(rec, loops_cnt);
/* cleanup */
kmem_cache_destroy(slab);
return loops_cnt;
#undef MAX_BULK
}
void bulk_test(int bulk)
{
time_bench_loop(loops/bulk, bulk, "kmem bulk_fallback", NULL,
benchmark_slab_fallback_bulk);
cond_resched();
time_bench_loop(loops/bulk, bulk, "kmem bulk_quick_reuse", NULL,
benchmark_slab_bulk01);
cond_resched();
}
int run_timing_tests(void)
{
time_bench_loop(loops*10, 0, "for_loop",
NULL, time_bench_for_loop);
time_bench_loop(loops, 0, "kmem fastpath reuse", NULL,
benchmark_kmem_cache_fastpath_reuse);
bulk_test(1);
bulk_test(2);
bulk_test(3);
bulk_test(4);
bulk_test(8);
bulk_test(16);
bulk_test(30);
bulk_test(32);
bulk_test(34);
bulk_test(48);
bulk_test(64);
bulk_test(128);
bulk_test(128+30);
bulk_test(250);
return 0;
}
static int __init slab_bulk_test01_module_init(void)
{
if (verbose)
pr_info("Loaded\n");
preempt_disable();
pr_info("DEBUG: cpu:%d\n", smp_processor_id());
preempt_enable();
#ifdef CONFIG_DEBUG_PREEMPT
pr_warn("WARN: CONFIG_DEBUG_PREEMPT is enabled: this affect results\n");
#endif
#ifdef CONFIG_PREEMPT
pr_warn("INFO: CONFIG_PREEMPT is enabled\n");
#endif
#ifdef CONFIG_PREEMPT_COUNT
pr_warn("INFO: CONFIG_PREEMPT_COUNT is enabled\n");
#endif
if (run_timing_tests() < 0) {
return -ECANCELED;
}
return 0;
}
module_init(slab_bulk_test01_module_init);
static void __exit slab_bulk_test01_module_exit(void)
{
if (verbose)
pr_info("Unloaded\n");
}
module_exit(slab_bulk_test01_module_exit);
MODULE_DESCRIPTION("Synthetic micro-benchmarking of slab bulk");
MODULE_AUTHOR("Jesper Dangaard Brouer <netoptimizer@brouer.com>");
MODULE_LICENSE("GPL");