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
|
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (C) 2018 Thomas Gschwantner <tharre3@gmail.com>. All Rights Reserved.
*/
#ifdef DEBUG
#include "../mpmc_ptr_ring.h"
#include <linux/kthread.h>
#include <linux/workqueue.h>
#include <linux/wait.h>
#define THREADS_PRODUCER 16
#define THREADS_CONSUMER 16
#define ELEMENT_COUNT 1000000L /* divisible by threads_{consumer,producer} */
#define QUEUE_SIZE 1024
#define EXPECTED_TOTAL ((ELEMENT_COUNT * (ELEMENT_COUNT + 1)) / 2)
#define PER_PRODUCER (ELEMENT_COUNT/THREADS_PRODUCER)
#define PER_CONSUMER (ELEMENT_COUNT/THREADS_CONSUMER)
#define THREADS_TOTAL (THREADS_PRODUCER + THREADS_CONSUMER)
struct mpmc_ptr_ring *ring;
struct worker_producer {
struct work_struct work;
int thread_num;
};
struct worker_consumer {
struct work_struct work;
int thread_num;
long total;
long count;
};
static void producer_function(struct work_struct *work)
{
struct worker_producer *td = container_of(work, struct worker_producer, work);
uintptr_t count = (td->thread_num * PER_PRODUCER) + 1;
for (; count <= (td->thread_num + 1) * PER_PRODUCER; ++count) {
while (mpmc_ptr_ring_produce(ring, (const void *) count))
schedule();
}
}
static void consumer_function(struct work_struct *work)
{
struct worker_consumer *td = container_of(work, struct worker_consumer, work);
int i;
for (i = 0; i < PER_CONSUMER; ++i) {
uintptr_t value;
while (!(value = (uintptr_t) mpmc_ptr_ring_consume(ring)))
schedule();
td->total += value;
++(td->count);
}
}
bool __init mpmc_ring_selftest(void)
{
struct workqueue_struct *wq;
struct worker_producer *producers;
struct worker_consumer *consumers;
long total = 0, count = 0;
int i;
producers = kmalloc_array(THREADS_PRODUCER, sizeof(*producers), GFP_KERNEL);
consumers = kmalloc_array(THREADS_CONSUMER, sizeof(*consumers), GFP_KERNEL);
ring = kmalloc(sizeof(*ring), GFP_KERNEL);
BUG_ON(!ring || !producers || !consumers);
BUG_ON(mpmc_ptr_ring_init(ring, QUEUE_SIZE, GFP_KERNEL));
wq = alloc_workqueue("mpmc_ring_selftest", WQ_UNBOUND, 0);
for (i = 0; i < THREADS_PRODUCER; ++i) {
producers[i].thread_num = i;
INIT_WORK(&producers[i].work, producer_function);
queue_work(wq, &producers[i].work);
}
for (i = 0; i < THREADS_CONSUMER; ++i) {
consumers[i] = (struct worker_consumer) {
.thread_num = i,
.total = 0,
.count = 0,
};
INIT_WORK(&consumers[i].work, consumer_function);
queue_work(wq, &consumers[i].work);
}
destroy_workqueue(wq);
BUG_ON(!mpmc_ptr_ring_empty(ring));
mpmc_ptr_ring_cleanup(ring, NULL);
kfree(ring);
for (i = 0; i < THREADS_CONSUMER; ++i) {
total += consumers[i].total;
count += consumers[i].count;
}
kfree(producers);
kfree(consumers);
if (count == ELEMENT_COUNT && total == EXPECTED_TOTAL) {
pr_info("mpmc_ring self-tests: pass");
return true;
}
pr_info("mpmc_ring self-test failed:");
pr_info("Count: %lu, expected: %lu", count, ELEMENT_COUNT);
pr_info("Total: %lu, expected: %lu", total, EXPECTED_TOTAL);
return false;
}
#endif
|