-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathhv_sample_fifo.cpp
More file actions
208 lines (193 loc) · 5.39 KB
/
Copy pathhv_sample_fifo.cpp
File metadata and controls
208 lines (193 loc) · 5.39 KB
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
// Libhalog
// Author: likai.root@gmail.com
#include <unistd.h>
#include "clib/hal_hazard_version.h"
#include "clib/hal_util.h"
#include <gtest/gtest.h>
using namespace libhalog;
using namespace libhalog::clib;
template <typename T>
class LockFreeQueue;
template <typename T>
class FIFONode : public HALHazardNodeI {
friend class LockFreeQueue<T>;
public:
FIFONode() : next_(NULL) {}
FIFONode(const T &v) : next_(NULL), v_(v) {}
~FIFONode() {};
public:
void retire() {memset(&v_, 0, sizeof(v_));}//{ delete this; }
public:
void set_next(FIFONode *next) { next_ = next; }
FIFONode *get_next() const { return next_; }
public:
T get_v() { return v_; }
private:
FIFONode *next_;
T v_;
};
template <typename T>
class LockFreeQueue {
typedef FIFONode<T> Node;
public:
LockFreeQueue() : hazard_version_(), head_(NULL) {
head_ = new Node();
tail_ = head_;
}
~LockFreeQueue() {
while (NULL != head_) {
Node *tmp = head_;
head_ = head_->get_next();
tmp->retire();
}
}
public:
void push(const T &v, Node *node) {
#ifdef DO_NOT_CHECK
UNUSED(v);
#else
node = new(node) Node(v);
#endif
uint64_t handle = 0;
hazard_version_.acquire(handle);
Node *curr = ATOMIC_LOAD(&tail_);
Node *old = curr;
while (old != (curr = __sync_val_compare_and_swap(&tail_, old, node))) {
old = curr;
}
curr->set_next(node);
hazard_version_.release(handle);
}
bool pop(T &v) {
bool bret = false;
uint64_t handle = 0;
hazard_version_.acquire(handle);
Node *curr = ATOMIC_LOAD(&head_);
Node *old = curr;
Node *node = curr->get_next();
while (NULL != node
&& old != (curr = __sync_val_compare_and_swap(&head_, old, node))) {
old = curr;
node = curr->get_next();
}
if (NULL != node) {
#ifdef DO_NOT_CHECK
UNUSED(v);
#else
v = node->get_v();
#endif
hazard_version_.add_node(curr);
bret = true;
}
hazard_version_.release(handle);
return bret;
}
private:
HALHazardVersion hazard_version_;
Node *head_ CACHE_ALIGNED;
Node *tail_ CACHE_ALIGNED;
};
struct QueueValue {
int64_t a;
int64_t b;
int64_t sum;
QueueValue() : a(0), b(0), sum(0) {};
};
struct GConf {
LockFreeQueue<QueueValue> queue;
int64_t loop_times;
int64_t producer_count;
};
void set_cpu_affinity() {
int64_t cpu_count = sysconf(_SC_NPROCESSORS_ONLN);
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(gettn() % cpu_count, &cpuset);
if (0 == pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset)) {
//LOG_INFO(CLIB, "pthread_setaffinity_np succ %ld", gettn() % cpu_count);
} else {
LOG_WARN(CLIB, "pthread_setaffinity_np fail %ld", gettn() % cpu_count);
}
}
void *thread_consumer(void *data) {
set_cpu_affinity();
GConf *g_conf = (GConf*)data;
QueueValue stack_value;
bool skip = false;
while (true) {
if (g_conf->queue.pop(stack_value)) {
#ifndef DO_NOT_CHECK
assert((stack_value.a + stack_value.b) == stack_value.sum);
#endif
skip = false;
} else {
if (0 == ATOMIC_LOAD(&(g_conf->producer_count))) {
if (skip) {
break;
} else {
skip = true;
}
}
}
}
return NULL;
}
void *thread_producer(void *data) {
set_cpu_affinity();
GConf *g_conf = (GConf*)data;
FIFONode<QueueValue> *nodes = new FIFONode<QueueValue>[g_conf->loop_times];
#ifndef DO_NOT_CHECK
int64_t sum_base = gettn() * g_conf->loop_times;
#endif
QueueValue stack_value;
for (int64_t i = 0; i < g_conf->loop_times; i++) {
#ifndef DO_NOT_CHECK
stack_value.a = sum_base + i;
stack_value.b = i;
stack_value.sum = sum_base + 2*i;
#endif
g_conf->queue.push(stack_value, &nodes[i]);
}
__sync_add_and_fetch(&(g_conf->producer_count), -1);
return NULL;
}
void run_test(GConf *g_conf, const int64_t thread_count) {
pthread_t *pds_consumer = new pthread_t[thread_count];
pthread_t *pds_producer = new pthread_t[thread_count];
g_conf->producer_count = thread_count;
int64_t timeu = get_cur_microseconds_time();
for (int64_t i = 0; i < thread_count; i++) {
pthread_create(&pds_consumer[i], NULL, thread_consumer, g_conf);
pthread_create(&pds_producer[i], NULL, thread_producer, g_conf);
}
for (int64_t i = 0; i < thread_count; i++) {
pthread_join(pds_consumer[i], NULL);
pthread_join(pds_producer[i], NULL);
}
timeu = get_cur_microseconds_time() - timeu;
fprintf(stdout, "threads=%ld+%ld push+pop=%ld timeu=%ld tps=%0.2f\n",
thread_count, thread_count, thread_count * 2 * g_conf->loop_times, timeu, 1000000.0 * (double)(thread_count * 2 * g_conf->loop_times) / (double)(timeu));
delete[] pds_producer;
delete[] pds_consumer;
}
int main(const int argc, char **argv) {
int64_t cpu_count = 0;
if (1 < argc) {
cpu_count = atoi(argv[1]);
}
if (0 >= cpu_count) {
cpu_count = sysconf(_SC_NPROCESSORS_ONLN);
}
int64_t producer_count = (cpu_count + 1) / 2;
int64_t memory = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGE_SIZE);
int64_t available = memory * 4 / 10;
int64_t count = available / sizeof(FIFONode<QueueValue>) / producer_count;
GConf g_conf;
g_conf.loop_times = count;
#ifdef DO_NOT_CHECK
fprintf(stdout, "Run without check pop result...\n");
#else
fprintf(stdout, "Run and check pop result...\n");
#endif
run_test(&g_conf, producer_count);
}