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
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
#include <boost/scoped_ptr.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/program_options/option.hpp>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/variables_map.hpp>
#include <boost/program_options/cmdline.hpp>
#include <boost/program_options/parsers.hpp>
#include <iostream>
#include <set>
#include <sstream>
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include "common/Formatter.h"
#include "bencher.h"
#include "rados_backend.h"
#include "detailed_stat_collector.h"
#include "distribution.h"
#include "global/global_context.h"
#include "global/global_init.h"
#include "common/WorkQueue.h"
#include "common/Semaphore.h"
#include "common/Finisher.h"
namespace po = boost::program_options;
using namespace std;
class Queueable {
public:
virtual void queue(unsigned *) = 0;
virtual void start() = 0;
virtual void stop() = 0;
virtual ~Queueable() {};
};
class Base : public Queueable {
DetailedStatCollector *col;
Semaphore *sem;
public:
Base(DetailedStatCollector *col,
Semaphore *sem) : col(col), sem(sem) {}
void queue(unsigned *item) {
col->read_complete(*item);
sem->Put();
delete item;
}
void start() {}
void stop() {}
};
class WQWrapper : public Queueable {
boost::scoped_ptr<ThreadPool::WorkQueue<unsigned> > wq;
boost::scoped_ptr<ThreadPool> tp;
public:
WQWrapper(ThreadPool::WorkQueue<unsigned> *wq, ThreadPool *tp):
wq(wq), tp(tp) {}
void queue(unsigned *item) { wq->queue(item); }
void start() { tp->start(); }
void stop() { tp->stop(); }
};
class FinisherWrapper : public Queueable {
class CB : public Context {
Queueable *next;
unsigned *item;
public:
CB(Queueable *next, unsigned *item) : next(next), item(item) {}
void finish(int) {
next->queue(item);
}
};
Finisher f;
Queueable *next;
public:
FinisherWrapper(CephContext *cct, Queueable *next) :
f(cct), next(next) {}
void queue(unsigned *item) {
f.queue(new CB(next, item));
}
void start() { f.start(); }
void stop() { f.stop(); }
};
class PassAlong : public ThreadPool::WorkQueue<unsigned> {
Queueable *next;
list<unsigned*> q;
bool _enqueue(unsigned *item) {
q.push_back(item);
return true;
}
void _dequeue(unsigned *item) { assert(0); }
unsigned *_dequeue() {
if (q.empty())
return 0;
unsigned *val = q.front();
q.pop_front();
return val;
}
void _process(unsigned *item) {
next->queue(item);
}
void _clear() { q.clear(); }
bool _empty() { return q.empty(); }
public:
PassAlong(ThreadPool *tp, Queueable *next) :
ThreadPool::WorkQueue<unsigned>("TestQueue", 100, 100, tp), next(next) {}
};
int main(int argc, char **argv)
{
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("num-threads", po::value<unsigned>()->default_value(10),
"set number of threads")
("queue-size", po::value<unsigned>()->default_value(30),
"queue size")
("num-items", po::value<unsigned>()->default_value(3000000),
"num items")
("layers", po::value<string>()->default_value(""),
"layer desc")
;
po::variables_map vm;
po::parsed_options parsed =
po::command_line_parser(argc, argv).options(desc).allow_unregistered().run();
po::store(
parsed,
vm);
po::notify(vm);
vector<const char *> ceph_options, def_args;
vector<string> ceph_option_strings = po::collect_unrecognized(
parsed.options, po::include_positional);
ceph_options.reserve(ceph_option_strings.size());
for (vector<string>::iterator i = ceph_option_strings.begin();
i != ceph_option_strings.end();
++i) {
ceph_options.push_back(i->c_str());
}
global_init(
&def_args, ceph_options, CEPH_ENTITY_TYPE_CLIENT,
CODE_ENVIRONMENT_UTILITY,
CINIT_FLAG_NO_DEFAULT_CONFIG_FILE);
common_init_finish(g_ceph_context);
g_ceph_context->_conf->apply_changes(NULL);
if (vm.count("help")) {
cout << desc << std::endl;
return 1;
}
DetailedStatCollector col(1, new JSONFormatter, 0, &cout);
Semaphore sem;
for (unsigned i = 0; i < vm["queue-size"].as<unsigned>(); ++i)
sem.Put();
typedef list<Queueable*> QQ;
QQ wqs;
wqs.push_back(
new Base(&col, &sem));
string layers(vm["layers"].as<string>());
unsigned num = 0;
for (string::reverse_iterator i = layers.rbegin();
i != layers.rend(); ++i) {
stringstream ss;
ss << "Test " << num;
if (*i == 'q') {
ThreadPool *tp =
new ThreadPool(
g_ceph_context, ss.str(), vm["num-threads"].as<unsigned>(), 0);
wqs.push_back(
new WQWrapper(
new PassAlong(tp, wqs.back()),
tp
));
} else if (*i == 'f') {
wqs.push_back(
new FinisherWrapper(
g_ceph_context, wqs.back()));
}
++num;
}
for (QQ::iterator i = wqs.begin();
i != wqs.end();
++i) {
(*i)->start();
}
for (uint64_t i = 0; i < vm["num-items"].as<unsigned>(); ++i) {
sem.Get();
unsigned *item = new unsigned(col.next_seq());
col.start_read(*item, 1);
wqs.back()->queue(item);
}
for (QQ::iterator i = wqs.begin();
i != wqs.end();
++i) {
(*i)->stop();
}
for (QQ::iterator i = wqs.begin(); i != wqs.end(); wqs.erase(i++)) {
delete *i;
}
return 0;
}
|