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
|
#include <cstdio>
#include <thread>
#include <atomic>
#include "salticidae/event.h"
#include "salticidae/util.h"
using salticidae::TimerEvent;
using salticidae::Config;
void test_mpsc(int nproducers, int nops, size_t burst_size) {
size_t total = nproducers * nops;
salticidae::EventContext ec;
std::atomic<size_t> collected(0);
using queue_t = salticidae::MPSCQueueEventDriven<int>;
queue_t q;
q.set_capacity(65536);
q.reg_handler(ec, [&collected, burst_size](queue_t &q) {
size_t cnt = burst_size;
int x;
while (q.try_dequeue(x))
{
printf("%d\n", x);
collected.fetch_add(1);
if (!--cnt) return true;
}
return false;
});
std::vector<std::thread> producers;
std::thread consumer([&collected, total, &ec]() {
TimerEvent timer(ec, [&ec, &collected, total](TimerEvent &timer) {
if (collected.load() == total) ec.stop();
timer.add(1);
});
timer.add(1);
ec.dispatch();
});
for (int i = 0; i < nproducers; i++)
{
producers.emplace(producers.end(), std::thread([&q, nops, i, nproducers]() {
int x = i;
for (int j = 0; j < nops; j++)
{
//usleep(rand() / double(RAND_MAX) * 100);
while (!q.enqueue(x, false))
std::this_thread::yield();
x += nproducers;
}
}));
}
for (auto &t: producers) t.join();
SALTICIDAE_LOG_INFO("producers terminate");
consumer.join();
SALTICIDAE_LOG_INFO("consumers terminate");
}
void test_mpmc(int nproducers, int nconsumers, int nops, size_t burst_size) {
size_t total = nproducers * nops;
using queue_t = salticidae::MPMCQueueEventDriven<int>;
queue_t q;
std::vector<std::thread> producers;
std::vector<std::thread> consumers;
std::vector<salticidae::EventContext> ecs;
std::atomic<size_t> collected(0);
ecs.resize(nconsumers);
q.set_capacity(65536 * nproducers);
for (int i = 0; i < nconsumers; i++)
{
q.reg_handler(ecs[i], [&collected, burst_size](queue_t &q) {
size_t cnt = burst_size;
int x;
while (q.try_dequeue(x))
{
//usleep(10);
printf("%d\n", x);
collected.fetch_add(1);
if (!--cnt) return true;
}
return false;
});
}
for (int i = 0; i < nconsumers; i++)
{
consumers.emplace(consumers.end(), std::thread(
[&collected, total, &ec = ecs[i]]() {
TimerEvent timer(ec, [&ec, &collected, total](TimerEvent &timer) {
if (collected.load() == total) ec.stop();
timer.add(1);
});
timer.add(1);
ec.dispatch();
}));
}
for (int i = 0; i < nproducers; i++)
{
producers.emplace(producers.end(), std::thread([&q, nops, i, nproducers]() {
int x = i;
for (int j = 0; j < nops; j++)
{
//usleep(rand() / double(RAND_MAX) * 100);
while (!q.enqueue(x, false))
std::this_thread::yield();
x += nproducers;
}
}));
}
for (auto &t: producers) t.join();
SALTICIDAE_LOG_INFO("producers terminate");
for (auto &t: consumers) t.join();
SALTICIDAE_LOG_INFO("consumers terminate");
}
int main(int argc, char **argv) {
Config config;
auto opt_nproducers = Config::OptValInt::create(16);
auto opt_nconsumers = Config::OptValInt::create(4);
auto opt_burst_size = Config::OptValInt::create(128);
auto opt_nops = Config::OptValInt::create(100000);
auto opt_mpmc = Config::OptValFlag::create(false);
auto opt_help = Config::OptValFlag::create(false);
config.add_opt("nproducers", opt_nproducers, Config::SET_VAL);
config.add_opt("nconsumers", opt_nconsumers, Config::SET_VAL);
config.add_opt("burst-size", opt_burst_size, Config::SET_VAL);
config.add_opt("nops", opt_nops, Config::SET_VAL);
config.add_opt("mpmc", opt_mpmc, Config::SWITCH_ON);
config.add_opt("help", opt_help, Config::SWITCH_ON, 'h', "show this help info");
config.parse(argc, argv);
if (opt_help->get())
{
config.print_help();
exit(0);
}
if (!opt_mpmc->get())
{
SALTICIDAE_LOG_INFO("testing an MPSC queue...");
test_mpsc(opt_nproducers->get(), opt_nops->get(),
opt_burst_size->get());
}
else
{
SALTICIDAE_LOG_INFO("testing an MPMC queue...");
test_mpmc(opt_nproducers->get(), opt_nconsumers->get(),
opt_nops->get(), opt_burst_size->get());
}
return 0;
}
|