This repository was archived by the owner on Dec 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathTreadmill.h
270 lines (222 loc) · 7.91 KB
/
Treadmill.h
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#pragma once
#include <string>
#include <thread>
#include <vector>
#include <folly/String.h>
#include <folly/futures/Future.h>
#include <folly/json.h>
#include <gflags/gflags.h>
#include <glog/logging.h>
#include "common/stats/ServiceData.h"
#include "treadmill/Scheduler.h"
#include "treadmill/TreadmillFB303.h"
#include "treadmill/Worker.h"
#include "common/fb303/cpp/FacebookBase2.h"
#include "common/services/cpp/ServiceFramework.h"
#include "thrift/lib/cpp2/server/ThriftServer.h"
#include "treadmill/if/gen-cpp2/TreadmillService.h"
// The path to the workload configuration file
DECLARE_string(config_file);
// The hostname of the server
DECLARE_string(hostname);
DECLARE_string(counter_name);
DECLARE_int32(counter_threshold);
// The number of connections each worker thread handles
DECLARE_int32(number_of_connections);
// The total number of workers
DECLARE_int32(number_of_workers);
// The number of keys in the workload
DECLARE_int64(number_of_keys);
// The port number to connect
DECLARE_int32(port);
DECLARE_int32(control_port);
// The request per second trying to send
DECLARE_int32(request_per_second);
// The total testing time in second
DECLARE_int32(runtime);
// The file to store the JSON output statistics
DECLARE_string(output_file);
// The max number of requests to have outstanding per worker
DECLARE_int32(max_outstanding_requests);
// Config filename to pass into the workload in JSON format
DECLARE_string(config_in_file);
// Config string to pass into the workload in JSON format
DECLARE_string(config_in_json);
// Config filename to export from the workload in JSON format
DECLARE_string(config_out_file);
// Comma-separated list of CPU IDs to pin the workers
DECLARE_string(cpu_affinity);
// Default number of calibration samples for continuous statistics
DECLARE_int32(default_calibration_samples);
// Default number of warm-up samples for continuous statistics
DECLARE_int32(default_warmup_samples);
// Number of calibration samples for latency statistics
DECLARE_int32(latency_calibration_samples);
// Number of warm-up samples for latency statistics
DECLARE_int32(latency_warmup_samples);
// Port for fb303 server
DECLARE_int32(server_port);
// How many seconds to give workers to finish requests
DECLARE_int32(worker_shutdown_delay);
namespace facebook {
namespace windtunnel {
namespace treadmill {
template <class Service>
class TreadmillRunner {
public:
TreadmillRunner() {
rps = FLAGS_request_per_second / (double)FLAGS_number_of_workers;
max_outstanding_requests_per_worker =
FLAGS_max_outstanding_requests / FLAGS_number_of_workers;
config = folly::dynamic::object;
scheduler = std::make_unique<Scheduler>(
FLAGS_request_per_second,
FLAGS_number_of_workers,
FLAGS_max_outstanding_requests,
max_outstanding_requests_per_worker);
cpu_affinity_list = std::vector<int>(FLAGS_number_of_workers, -1);
terminate_early_fn = [&scheduler = scheduler]() { scheduler->stop(); };
}
virtual ~TreadmillRunner(){};
/**
* Press start to rock
*
* @param argc Argument count
* @param argv Argument vector
*/
int run(int /*argc*/, char* /*argv*/[]) {
LOG(INFO) << "Desired rps per worker: " << rps;
LOG(INFO) << "Max outstanding requests per worker: "
<< max_outstanding_requests_per_worker;
LOG(INFO) << "N Workers: " << FLAGS_number_of_workers;
LOG(INFO) << "N Connections: " << FLAGS_number_of_connections;
if (FLAGS_config_in_file != "") {
config = readDynamicFromFile(FLAGS_config_in_file);
}
if (FLAGS_config_in_json != "") {
folly::dynamic config2 = folly::parseJson(FLAGS_config_in_json);
config.update(config2);
}
if (FLAGS_cpu_affinity != "") {
int total_number_of_cores = std::thread::hardware_concurrency();
std::vector<folly::StringPiece> affinity_string_list;
folly::split(",", FLAGS_cpu_affinity, affinity_string_list);
if (affinity_string_list.size() != FLAGS_number_of_workers) {
LOG(FATAL) << "Length of the CPU affinity list ("
<< affinity_string_list.size()
<< ") does not match the number of workers ("
<< FLAGS_number_of_workers << ")";
} else {
for (int i = 0; i < FLAGS_number_of_workers; i++) {
int cpu_affinity = folly::to<int>(affinity_string_list[i]);
if (cpu_affinity >= 0 && cpu_affinity < total_number_of_cores) {
cpu_affinity_list[i] = cpu_affinity;
} else {
LOG(FATAL) << "Core " << cpu_affinity << " does not exist";
}
}
}
}
// Init fb303
std::shared_ptr<std::thread> server_thread;
if (FLAGS_server_port > 0) {
TreadmillFB303::make_fb303(server_thread, FLAGS_server_port, *scheduler);
}
initializeWorkers();
// Start testing
for (int i = 0; i < FLAGS_number_of_workers; i++) {
workers[i]->run();
}
// Start the test and wait for it to finish.
std::vector<folly::SemiFuture<folly::Unit>> futs;
futs.push_back(scheduler->run());
futs.push_back(folly::futures::sleep(std::chrono::seconds(FLAGS_runtime)));
folly::collectAny(futs).wait();
LOG(INFO) << "Stopping and joining scheduler thread";
scheduler->stop();
scheduler->join();
if (FLAGS_worker_shutdown_delay > 0) {
// Wait for workers to finish requests
size_t secondsToWait = FLAGS_worker_shutdown_delay;
size_t remaining;
do {
remaining = 0;
for (auto& it : workers) {
if (it->hasMoreWork())
remaining++;
}
if (remaining > 0) {
LOG(INFO) << "waiting for " << remaining << " worker(s)";
sleep(1);
--secondsToWait;
}
} while (secondsToWait > 0 && remaining > 0);
}
StatisticsManager::get()->print();
LOG(INFO) << "Stopping workers";
// We already stored stats, so just drop all remaining scheduled request.
for (int i = 0; i < FLAGS_number_of_workers; i++) {
workers[i]->stop();
}
LOG(INFO) << "Joining worker threads";
// Join worker threads
for (int i = 0; i < FLAGS_number_of_workers; i++) {
workers[i]->join();
}
if (FLAGS_config_out_file != "") {
LOG(INFO) << "Saving config";
std::vector<Worker<Service>*> workerRefs;
for (auto& worker : workers) {
workerRefs.push_back(worker.get());
}
auto config_output = workers[0]->makeConfigOutputs(workerRefs);
writeDynamicToFile(FLAGS_config_out_file, config_output);
}
auto counters = stats::ServiceData::get()->getCounters();
for (auto& pair : counters) {
LOG(INFO) << pair.first << ": " << pair.second;
}
LOG(INFO) << "Complete";
return 0;
}
virtual void initializeWorkers() {
for (int i = 0; i < FLAGS_number_of_workers; i++) {
workers.push_back(std::make_unique<Worker<Service>>(
i,
scheduler->getWorkerQueue(i),
FLAGS_number_of_workers,
FLAGS_number_of_connections,
max_outstanding_requests_per_worker,
config,
cpu_affinity_list[i],
terminate_early_fn));
}
}
protected:
std::vector<std::unique_ptr<Worker<Service>>> workers;
std::unique_ptr<Scheduler> scheduler;
int max_outstanding_requests_per_worker;
folly::dynamic config;
std::vector<int> cpu_affinity_list;
std::function<void()> terminate_early_fn;
private:
double rps;
};
void init(int argc, char* argv[]);
template <class Service>
int run(int argc, char* argv[]) {
TreadmillRunner runner = TreadmillRunner<Service>();
return runner.run(argc, argv);
};
} // namespace treadmill
} // namespace windtunnel
} // namespace facebook