-
Notifications
You must be signed in to change notification settings - Fork 0
/
workqueue.hh
148 lines (135 loc) · 3.79 KB
/
workqueue.hh
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
#ifndef WORKQUEUE_HH
#define WORKQUQUE_HH
#include <atomic>
#include <thread>
#include <condition_variable>
#include <memory>
#include <tuple>
#include <utility>
#include "queue.hh"
#include "parser.hh"
namespace amazon {
struct GlobalStats {
std::atomic<int> empty;
std::atomic<int> full;
std::pair<std::atomic<int> , std::atomic<int> >most_filled;
GlobalStats()
{
empty.store(0);
full.store(0);
most_filled.first.store(0);
most_filled.second.store(0);
}
};
class Workqueue {
public:
enum ev_type {STOP, WORK};
struct Event {
ev_type type;
amazon::Data data;
};
Workqueue(unsigned size, unsigned nworkers, std::condition_variable &cond,
amazon::GlobalStats *all_stats)
: size(size), nworkers(nworkers), cond(cond), all_stats(all_stats)
{
active_workers.store(nworkers);
workers = new std::thread*[nworkers];
workqueue = new Queue<Event>(size);
for (unsigned i = 0; i < nworkers; i++)
workers[i] = new std::thread(std::bind(&Workqueue::run, this));
}
void push(Event &ev)
{
workqueue->push(ev);
}
int active()
{
return active_workers.load();
}
~Workqueue()
{
Event ev;
ev.type = ev_type::STOP;
for (unsigned i = active_workers.load(); i > 0; i--)
workqueue->push(ev);
for (unsigned i = 0; i < nworkers; i++)
workers[i]->join();
for (unsigned i = 0; i < nworkers; i++)
delete workers[i];
delete workers;
delete workqueue;
}
private:
struct PerThreadStats {
int empty;
int full;
std::pair<int, int> most_filled;
PerThreadStats() : empty(0), full(0)
{
most_filled.first = 0;
most_filled.second = 0;
}
};
unsigned size;
unsigned nworkers;
std::condition_variable &cond;
std::thread **workers;
Queue<Event> *workqueue;
amazon::GlobalStats *all_stats;
std::atomic<int> active_workers;
void run()
{
PerThreadStats stats[amazon::NUM_INSTANCES];
for (;;) {
Event ev;
workqueue->pop(ev);
switch (ev.type) {
case ev_type::WORK:
do_work(ev.data, stats);
delete[] ev.data.slot_state;
break;
case ev_type::STOP:
accum_stats(stats);
active_workers.fetch_sub(1);
if (active_workers.load() == 0)
cond.notify_one();
return;
}
}
}
void do_work(amazon::Data &data, PerThreadStats stats[])
{
int empty = 0, full = 0;
for (int i = 0; i < data.nslots; i++)
switch(data.slot_state[i]) {
case 0:
++empty;
++stats[data.instance_type].most_filled.first;
break;
case 1:
++full;
++stats[data.instance_type].most_filled.second;
}
if (empty == data.nslots)
++stats[data.instance_type].empty;
if (full == data.nslots)
++stats[data.instance_type].full;
}
void accum_stats(PerThreadStats stats[])
{
all_stats[amazon::Instance::M1].empty.fetch_add(stats[amazon::Instance::M1].empty);
all_stats[amazon::Instance::M1].full.fetch_add(stats[amazon::Instance::M1].full);
all_stats[amazon::Instance::M1].most_filled.first.fetch_add(stats[amazon::Instance::M1].most_filled.first);
all_stats[amazon::Instance::M1].most_filled.second.fetch_add(stats[amazon::Instance::M1].most_filled.second);
all_stats[amazon::Instance::M2].empty.fetch_add(stats[amazon::Instance::M2].empty);
all_stats[amazon::Instance::M2].full.fetch_add(stats[amazon::Instance::M2].full);
all_stats[amazon::Instance::M2].most_filled.first.fetch_add(stats[amazon::Instance::M2].most_filled.first);
all_stats[amazon::Instance::M2].most_filled.second.fetch_add(stats[amazon::Instance::M2].most_filled.second);
all_stats[amazon::Instance::M3].empty.fetch_add(stats[amazon::Instance::M3].empty);
all_stats[amazon::Instance::M3].full.fetch_add(stats[amazon::Instance::M3].full);
all_stats[amazon::Instance::M3].most_filled.first.fetch_add(stats[amazon::Instance::M3].most_filled.first);
all_stats[amazon::Instance::M3].most_filled.second.fetch_add(stats[amazon::Instance::M3].most_filled.second);
}
};
}
#endif