-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbench.c
703 lines (504 loc) · 13.8 KB
/
bench.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
#include <actor.h>
#include <linux/module.h>
#include <linux/err.h>
#include <linux/timer.h>
#include <linux/atomic.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/slab.h>
#include <linux/proc_fs.h>
#include <linux/mutex.h>
#include <linux/string.h>
#include <asm/uaccess.h>
#include <linux/cpumask.h>
#include <linux/percpu.h>
struct benchmark;
MODULE_LICENSE("GPL");
/*#####################
* Critical section benchmark
*#####################*/
/**********************
* ACTOR
**********************/
typedef struct actor_bench_private {
volatile long i;
volatile int is_done;
} actor_bench_priv_t;
static actor_t* test_actor;
int actor_test_callback(actor_t* self, actor_work_t* aw) {
actor_bench_priv_t* abp = (actor_bench_priv_t*) self->a_private;
abp->i++;
// return abp->is_done;
return ACTOR_SUCCESS;
}
static DECLARE_ACTOR_EXEC(actor_test_exec, actor_test_callback);
int actor_test_ctor(actor_t* self, void* data) {
actor_bench_priv_t* abp = (actor_bench_priv_t*)
actor_private_allocate(self, sizeof(actor_bench_priv_t));
abp->i = 0;
abp->is_done = ACTOR_INCOMPLETE;
self->a_private = abp;
return 0;
}
int actor_test_dtor(actor_t* self) {
actor_private_free(self);
return 0;
}
int actor_init(void) {
test_actor = actor_create(0, 0, smp_processor_id(),
"test", actor_test_ctor, actor_test_dtor,
&actor_test_exec, NULL);
return 0;
}
int actor_deinit(void) {
actor_destroy(test_actor);
return 0;
}
int actor_bench(struct benchmark* b, void* data) {
amsg_hdr_t* msg = amsg_create(0, 0, NULL, test_actor->a_nodeid);
/*Message is freed on actor-side*/
return actor_communicate_async(test_actor, msg);
}
long actor_bench_results(struct benchmark* b) {
actor_bench_priv_t* abp;
abp = (actor_bench_priv_t*) test_actor->a_private;
abp->is_done = ACTOR_SUCCESS;
return abp->i;
}
/**********************
* MULTIPLE ACTORS
**********************/
static actor_t* test_multi_actor[NR_CPUS];
int multi_actor_init(void) {
int i;
for_each_online_cpu(i) {
test_multi_actor[i] =
actor_create(0, 0, i, "multi",
actor_test_ctor,
actor_test_dtor,
&actor_test_exec,
NULL);
}
return 0;
}
int multi_actor_deinit(void) {
int i;
for_each_online_cpu(i) {
actor_destroy(test_multi_actor[i]);
}
return 0;
}
int multi_actor_bench(struct benchmark* b, void* data) {
int nodeid = smp_processor_id();
actor_t* ac = test_multi_actor[nodeid];
amsg_hdr_t* msg = amsg_create(0, 0, NULL, nodeid);
/*Message is freed on actor-side*/
return actor_communicate_async(ac, msg);
}
long multi_actor_bench_results(struct benchmark* b) {
actor_bench_priv_t* abp;
long result = 0;
int i;
for_each_online_cpu(i) {
abp = (actor_bench_priv_t*) test_multi_actor[i]->a_private;
abp->is_done = ACTOR_SUCCESS;
result += abp->i;
}
return result;
}
/**********************
* ATOMIC
**********************/
static atomic_t test_atom;
int atomic_bench(struct benchmark* b, void* data) {
atomic_inc(&test_atom);
return 0;
}
long atomic_bench_results(struct benchmark* b) {
return atomic_read(&test_atom);
}
/********************
* SPINLOCK
********************/
static long test_sl = 0;
static DEFINE_SPINLOCK(test_lock);
int spinlock_bench(struct benchmark* b, void* data) {
unsigned long flags = 0;
spin_lock_irqsave(&test_lock, flags);
test_sl++;
spin_unlock_irqrestore(&test_lock, flags);
return 0;
}
long spinlock_bench_results(struct benchmark* b) {
return test_sl;
}
/********************
* MUTEX
********************/
static long test_mtx = 0;
static DEFINE_MUTEX(test_mutex);
int mutex_bench(struct benchmark* b, void* data) {
mutex_lock(&test_mutex);
test_mtx++;
mutex_unlock(&test_mutex);
return 0;
}
long mutex_bench_results(struct benchmark* b) {
return test_mtx;
}
/********************
* PER_CPU
********************/
static DEFINE_PER_CPU(long, test_percpu);
int percpu_bench(struct benchmark* b, void* data) {
percpu_add(test_percpu, 1);
return 0;
}
long percpu_bench_results(struct benchmark* b) {
int i = 0;
long res = 0;
for_each_online_node(i) {
res += per_cpu(test_percpu, i);
}
return res;
}
/*#######################
* Ping-Pong Benchmark
*#######################*/
static unsigned int num_clients = 32;
module_param(num_clients, uint, S_IRWXU);
#define NUM_CLIENTS num_clients
static unsigned int num_domains = 8;
module_param(num_domains, uint, S_IRWXU);
#define NUM_DOMAINS num_domains
/********************
* ACTOR
********************/
static atomic_t pp_actor_score;
int pp_actor_is_done = 1;
static actor_t** pp_actors;
struct pp_actor_priv {
actor_t* next;
};
int pp_actor_med_ctor(actor_t* self, void* data) {
struct pp_actor_priv* p =
actor_private_allocate(self, sizeof(struct pp_actor_priv));
p->next = (actor_t*) data;
return 0;
}
int pp_actor_med_cb(actor_t* ac, actor_work_t* aw) {
/*Send message to */
struct pp_actor_priv* p = (struct pp_actor_priv*) ac->a_private;
/*Forward message to next actor*/
actor_communicate_blocked(p->next, aw->aw_msg);
return ACTOR_INCOMPLETE;
}
int pp_actor_med_cb2(actor_t* ac, actor_work_t* aw) {
return ACTOR_SUCCESS;
}
static DECLARE_ACTOR_EXEC(pp_actor_med_exec, pp_actor_med_cb, pp_actor_med_cb2);
int pp_actor_last_cb(actor_t* ac, amsg_hdr_t* msg, int aw_flags) {
/*Last actor in chain simply returns success*/
atomic_inc(&pp_actor_score);
return ACTOR_SUCCESS;
}
static DECLARE_ACTOR_EXEC(pp_actor_last_exec, pp_actor_last_cb);
int pp_actor_init(void) {
int ai = NUM_DOMAINS - 1;
int nodeid = smp_processor_id();
actor_t* prev = NULL;
pp_actors = kmalloc(sizeof(actor_t*) * num_domains, GFP_KERNEL);
//Last actor in chain
prev = actor_create(0, 0, nodeid, "pp_last", NULL, NULL, &pp_actor_last_exec, NULL);
pp_actors[ai--] = prev;
pp_actor_is_done = 0;
for(; ai >= 0; --ai) {
nodeid = cpumask_next(nodeid, cpu_online_mask);
if(nodeid >= nr_cpu_ids)
nodeid = cpumask_first(cpu_online_mask);
prev = actor_create(0, 0, nodeid, "pp_med",
pp_actor_med_ctor,
NULL,
&pp_actor_med_exec, (void*) prev);
pp_actors[ai] = prev;
}
return 0;
}
int pp_actor_deinit(void) {
int ai;
for(ai = 0; ai < NUM_DOMAINS; ++ai)
actor_destroy(pp_actors[ai]);
kfree(pp_actors);
return 0;
}
int pp_actor_bench(struct benchmark* b, void* data) {
/*Send message to first actor and wait*/
amsg_hdr_t* msg = amsg_create(0, 0, NULL, smp_processor_id());
actor_communicate_blocked(pp_actors[0], msg);
return 0;
}
long pp_actor_bench_results(struct benchmark* b) {
pp_actor_is_done = 1;
return atomic_read(&pp_actor_score);
}
/********************
* THREAD
********************/
static long pp_thread_score = 0;
int pp_thread_is_done = 1;
struct pp_thread_message {
struct list_head list;
struct completion com;
};
static struct pp_thread_struct {
struct task_struct* kthread;
struct pp_thread_struct* next;
struct list_head queue;
struct mutex lock;
} *pp_threads;
void pp_thread_forward(struct list_head* l, struct pp_thread_struct* next) {
list_del(l);
mutex_lock(&next->lock);
list_add_tail(l, &next->queue);
mutex_unlock(&next->lock);
}
int pp_thread_complete(struct list_head* q) {
struct list_head* l = NULL, *ln = NULL;
struct pp_thread_message* msg = NULL;
int n = 0;
list_for_each_safe(l, ln, q) {
msg = (struct pp_thread_message*) list_entry(l, struct pp_thread_message, list);
list_del(l);
complete(&msg->com);
++n;
}
return n;
}
int pp_thread_med(void* data) {
// transfer to next thread
struct list_head* l = NULL, *ln = NULL;
struct pp_thread_struct* thr = (struct pp_thread_struct*) data;
struct pp_thread_struct* next = thr->next;
struct list_head actq;
while(!pp_thread_is_done) {
INIT_LIST_HEAD(&actq);
/*Switch queues*/
mutex_lock(&thr->lock);
list_splice_init(&thr->queue, &actq);
mutex_unlock(&thr->lock);
list_for_each_safe(l, ln, &actq) {
pp_thread_forward(l, next);
}
wake_up_process(next->kthread);
might_sleep();
}
pp_thread_complete(&thr->queue);
return 0;
}
int pp_thread_last(void* data) {
struct pp_thread_struct* thr = (struct pp_thread_struct*) data;
while(!pp_thread_is_done) {
mutex_lock(&thr->lock);
pp_thread_score += pp_thread_complete(&thr->queue);
mutex_unlock(&thr->lock);
//Sleep
might_sleep();
}
pp_thread_complete(&thr->queue);
return 0;
}
int pp_thread_init(void) {
int ti = NUM_DOMAINS - 1;
pp_thread_is_done = 0;
pp_threads = kmalloc(sizeof(struct pp_thread_struct) * num_domains, GFP_KERNEL);
for(; ti >= 0; --ti) {
mutex_init(&(pp_threads[ti].lock));
INIT_LIST_HEAD(&(pp_threads[ti].queue));
pp_threads[ti].kthread = kthread_create((ti == NUM_DOMAINS - 1) ? pp_thread_last : pp_thread_med,
(void*) (pp_threads + ti), "ppthread-%d", ti);
pp_threads[ti].next = (ti == NUM_DOMAINS - 1) ? NULL : pp_threads + ti + 1;
}
return 0;
}
int pp_thread_deinit(void) {
return 0;
}
int pp_thread_bench(struct benchmark* b, void* data) {
/*Send message to first actor and wait*/
struct pp_thread_message msg;
struct pp_thread_struct* thr = &pp_threads[0];
INIT_LIST_HEAD(&msg.list);
init_completion(&msg.com);
mutex_lock(&thr->lock);
list_add_tail(&msg.list, &thr->queue);
mutex_unlock(&thr->lock);
wake_up_process(thr->kthread);
wait_for_completion(&msg.com);
return 0;
}
long pp_thread_bench_results(struct benchmark* b) {
pp_thread_is_done = 1;
return pp_thread_score;
}
/*#######################
* Main module
*#######################*/
typedef enum bench_stage {
BENCH_NONE = -1,
BENCH_ACTOR,
BENCH_MULTI_ACTOR,
BENCH_ATOMIC,
BENCH_SPIN,
BENCH_MUTEX,
BENCH_PP_ACTOR,
BENCH_PP_THREAD,
BENCH_DONE
} bench_stage_t;
struct benchmark {
const char* name;
int (*bench_init)(void);
int (*bench_deinit)(void);
int (*bench_func)(struct benchmark*, void* );
long (*bench_get_results)(struct benchmark* );
};
#define BENCH_DURATION 10
static volatile bench_stage_t benchmark_stage = BENCH_NONE;
static struct timer_list bench_timer;
struct task_struct** bench_threads;
struct proc_dir_entry* test_proc_entry;
#define BENCHMARK(bname) \
{ \
.name = #bname , \
.bench_init = bname ## _init, \
.bench_deinit = bname ## _deinit, \
.bench_func = bname ## _bench, \
.bench_get_results = \
bname ## _bench_results \
}
#define BENCHMARK2(bname) \
{ \
.name = #bname , \
.bench_func = bname ## _bench, \
.bench_get_results = \
bname ## _bench_results \
}
struct benchmark benchmarks[] =
{
BENCHMARK(actor),
BENCHMARK(multi_actor),
BENCHMARK2(atomic),
BENCHMARK2(percpu),
BENCHMARK2(spinlock),
BENCHMARK2(mutex),
BENCHMARK(pp_actor),
BENCHMARK(pp_thread),
};
void set_benchmark_stage(int new_stage) {
if(new_stage < BENCH_DONE && new_stage > BENCH_NONE) {
printk("Benchmarking '%s'...", benchmarks[new_stage].name);
benchmark_stage = new_stage;
mod_timer(&bench_timer, jiffies + HZ * BENCH_DURATION);
}
}
void benchmark_timer_func(unsigned long data) {
struct benchmark* finished = benchmarks + benchmark_stage;
if(benchmark_stage == BENCH_DONE)
return;
printk(KERN_INFO "Results for '%s' are: %ld incs / %ds\n",
finished->name, finished->bench_get_results(finished), BENCH_DURATION);
benchmark_stage = BENCH_DONE;
smp_mb();
}
int benchmark_thread(void* data) {
struct benchmark* benchmark = NULL;
bench_stage_t bs = BENCH_DONE;
while((bs = benchmark_stage) != BENCH_DONE) {
benchmark = benchmarks + bs;
benchmark->bench_func(benchmark, data);
smp_mb();
}
return 0;
}
int test_proc_read(char* page, char** start, off_t off, int count, int* eof, void* data) {
if(off > 0) {
*eof = 1;
return 0;
}
*start = page + off;
switch(benchmark_stage) {
case BENCH_NONE: return snprintf(page, count, "none\n");
case BENCH_DONE: return snprintf(page, count, "done\n");
default: return snprintf(page, count, "%s\n", benchmarks[benchmark_stage].name);
}
/*NOTREACHED*/
return 0;
}
int test_proc_write(struct file *file, const char *buffer, unsigned long count, void *data) {
int i = 0;
int new_stage;
char new_stage_name[32];
if(benchmark_stage != BENCH_DONE &&
benchmark_stage != BENCH_NONE)
return -EBUSY;
if(count > 32)
return -EINVAL;
if(copy_from_user(new_stage_name, buffer, count))
return -EINVAL;
new_stage_name[count] = '\0';
/*Detect which benchmark we want*/
for(new_stage = BENCH_NONE + 1;
new_stage < BENCH_DONE;
new_stage++) {
if(strcmp(benchmarks[new_stage].name, new_stage_name) == 0)
break;
}
if(new_stage == BENCH_DONE)
return -EINVAL;
set_benchmark_stage(new_stage);
for(i = 0; i < NUM_CLIENTS; ++i) {
bench_threads[i] = kthread_create(benchmark_thread, NULL, "bench-%d", i);
wake_up_process(bench_threads[i]);
}
return count;
}
int bench_init_benchmarks(void) {
int i = BENCH_NONE + 1;
for(; i < BENCH_DONE; ++i) {
if(benchmarks[i].bench_init)
benchmarks[i].bench_init();
}
return 0;
}
int bench_deinit_benchmarks(void) {
int i = BENCH_NONE + 1;
for(; i < BENCH_DONE; ++i) {
if(benchmarks[i].bench_deinit)
benchmarks[i].bench_deinit();
}
return 0;
}
int benchmark_init(void) {
bench_init_benchmarks();
if(IS_ERR(test_actor))
return PTR_ERR(test_actor);
bench_threads = kmalloc(sizeof(struct task_struct*) * num_clients, GFP_KERNEL);
init_timer(&bench_timer);
setup_timer(&bench_timer, benchmark_timer_func, 0);
test_proc_entry = create_proc_entry("actor_bench", 0600, NULL);
test_proc_entry->read_proc = test_proc_read;
test_proc_entry->write_proc = test_proc_write;
return 0;
}
void benchmark_exit(void) {
int i;
remove_proc_entry("actor_bench", NULL);
/* for_each_online_cpu(i) {
kthread_stop(bench_threads[i]);
} */
kfree(bench_threads);
del_timer(&bench_timer);
bench_deinit_benchmarks();
}
module_init(benchmark_init);
module_exit(benchmark_exit);