-
Notifications
You must be signed in to change notification settings - Fork 11
/
test.cpp
386 lines (361 loc) · 9.42 KB
/
test.cpp
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#include "interface.hpp"
#include "collectors/perf.hpp"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/prctl.h>
#include <unistd.h>
#include <mutex>
#include <memory>
#include <condition_variable>
#include "json/writer.h"
#ifndef DEBUG
// otherwise we will get complaints about assert()ed variables being unused
#pragma GCC diagnostic ignored "-Wunused-variable"
#endif
static void test1()
{
printf("Trying to initialize all functional collectors...\n");
Json::Value j;
Collection c(j);
std::vector<std::string> list = c.available();
printf("Supported collectors:\n");
for (const auto& s : list)
{
printf("\t%s\n", s.c_str());
}
std::vector<std::string> list_bad = c.unavailable();
printf("Non-supported collectors:\n");
for (const auto& s : list_bad)
{
printf("\t%s\n", s.c_str());
}
bool result = c.initialize(list);
assert(result);
c.collector("procfs")->useThreading(10);
printf("Starting...\n");
c.start({"result1", "result2", "result3"});
c.collect({0, -1, 0});
for (int i = 1; i < 4; i++)
{
usleep(350000 + random() % 100000);
c.collect({i, -1, i});
}
c.collect({4, -1, 4});
c.collect({5, -1, 5});
usleep(random() % 1000);
printf("Stopping...\n");
c.stop();
Json::Value results = c.results();
Json::StyledWriter writer;
std::string data = writer.write(results);
printf("Results:\n%s", data.c_str());
c.writeCSV_MTV("mtv.csv");
c.writeCSV("excel.csv");
}
static void test2()
{
printf("Trying to initialize misnamed collector through API (should be ignored)...\n");
Json::Value j;
Collection c(j);
bool result = c.initialize({"bad_collector"});
assert(result);
c.start();
usleep(random() % 100);
for (int i = 0; i < 3; i++)
{
c.collect();
usleep(50 + random() % 100);
}
usleep(random() % 100);
c.stop();
usleep(random() % 100);
Json::Value results = c.results();
Json::StyledWriter writer;
std::string data = writer.write(results);
printf("Results:\n%s", data.c_str());
}
static void test3()
{
printf("Trying to initialize misnamed collector through JSON with required (should not work)...\n");
Json::Value j;
Json::Value v;
v["required"] = true;
j["bad_collector"] = v;
Collection c(j);
bool result = c.initialize();
assert(!result); // should fail!
c.start();
for (int i = 0; i < 3; i++)
{
c.collect();
usleep(50 + random() % 100);
}
c.stop();
Json::Value results = c.results();
Json::StyledWriter writer;
std::string data = writer.write(results);
printf("Results:\n%s", data.c_str());
}
static void test4()
{
printf("Trying to initialize collector through JSON (should work)...\n");
Json::Value j;
Json::Value v;
Json::Value p;
Json::Value perf;
v["required"] = true;
v["threaded"] = true;
v["sample_rate"] = 5;
j["procfs"] = v;
j["streamline"] = Json::objectValue;
j["cpufreq"] = v;
p["info"] = "test";
j["provenance"] = p;
Collection c(j);
bool result = c.initialize();
assert(result);
c.start();
for (int i = 0; i < 3; i++)
{
c.collect();
usleep(random() % 10000);
}
c.stop();
Json::Value results = c.results();
Json::StyledWriter writer;
std::string data = writer.write(results);
printf("Results (1):\n%s", data.c_str());
usleep(100 + random() % 1000000);
c.start(); // go again (clears previous results)
for (int i = 0; i < 3; i++)
{
c.collect();
usleep(25000 + random() % 1000000);
}
c.stop();
results = c.results();
data = writer.write(results);
printf("Results (2):\n%s", data.c_str());
assert(results.isMember("provenance"));
for (const std::string& s : results.getMemberNames()) // verify that we get exactly 3 results, no duplicates
{
if (s == "provenance" || s == "streamline")
{
continue;
}
Json::Value collector = results[s];
assert(collector.isObject());
for (const std::string& k : collector.getMemberNames())
{
if (results[s][k].isArray()) assert(results[s][k].size() == 3);
}
}
}
static void test5()
{
printf("Trying to initialize collector through both API and JSON (should work)...\n");
Json::Value j;
Json::Value v;
v["required"] = true;
j["procfs"] = v;
Collection c(j);
bool result = c.initialize({"procfs"});
assert(result);
c.start({"result1", "result2"});
for (int i = 0; i < 3; i++)
{
c.collect({i, -1});
usleep(50 + random() % 100);
}
c.stop();
Json::Value results = c.results();
Json::StyledWriter writer;
std::string data = writer.write(results);
printf("Results:\n%s", data.c_str());
for (const std::string& s : results.getMemberNames()) // verify that we get exactly 3 results, no duplicates
{
Json::Value collector = results[s];
assert(collector.isObject());
for (const std::string& k : collector.getMemberNames())
{
if (results[s][k].isArray()) assert(results[s][k].size() == 3);
}
}
}
static void test6()
{
printf("Trying to initialize collector through JSON without anything set (should work)...\n");
Json::Value v;
Collection c(v);
c.start();
for (int i = 0; i < 3; i++)
{
c.collect();
usleep(50 + random() % 100);
}
c.stop();
Json::Value results = c.results();
Json::StyledWriter writer;
std::string data = writer.write(results);
printf("Results:\n%s", data.c_str());
for (const std::string& s : results.getMemberNames()) // verify that we get exactly 3 results, no duplicates
{
Json::Value collector = results[s];
assert(collector.isObject());
for (const std::string& k : collector.getMemberNames())
{
if (results[s][k].isArray()) assert(results[s][k].size() == 3);
}
}
}
static void test7()
{
printf("Trying to initialize all functional collectors with summaries...\n");
Json::Value j;
Collection c(j);
std::vector<std::string> list = c.available();
printf("Supported collectors:\n");
for (const auto& s : list)
{
printf("\t%s\n", s.c_str());
}
std::vector<std::string> list_bad = c.unavailable();
printf("Non-supported collectors:\n");
for (const auto& s : list_bad)
{
printf("\t%s\n", s.c_str());
}
bool result = c.initialize(list);
assert(result);
c.collector("procfs")->useThreading(10);
printf("Starting...\n");
c.start({"result1", "result2", "result3"});
for (int j = 0; j < 10; j++)
{
c.collect({0, -1, 0});
for (int i = 1; i < 4; i++)
{
usleep(35000);
c.collect({i, -1, i});
}
c.collect({4, -1, 4});
c.collect({5, -1, 5});
usleep(10000);
c.summarize();
}
printf("Stopping...\n");
c.stop();
Json::Value results = c.results();
Json::StyledWriter writer;
std::string data = writer.write(results);
printf("Results:\n%s", data.c_str());
c.writeCSV_MTV("mtv.csv");
c.writeCSV("excel.csv");
}
class Test8 {
public:
Test8() : test8_ready(false) {}
~Test8() {
delete c;
}
void run() {
printf("[test 8]: Testing collect_scope for the perf collector...\n");
std::vector<std::thread> threads;
// Specification:
// https://github.com/ARM-software/patrace/blob/master/patrace/doc/manual.md#generating-cpu-load-with-perf-collector
std::string collectorConfig = R"(
{
"perf": {
"set": 4,
"event": [
{
"name": "CPUCyclesUser",
"type": 4,
"config": 17,
"excludeKernel": true
},
{
"name": "CPUCyclesKernel",
"type": 4,
"config": 17,
"excludeUser": true
},
{
"name": "CPUInstructionUser",
"type": 4,
"config": 8,
"excludeKernel": true
},
{
"name": "CPUInstructionKernel",
"type": 4,
"config": 8,
"excludeUser": true
}
],
}
})";
Json::Value config;
std::stringstream(collectorConfig) >> config;
threads.emplace_back(&Test8::test8_worker, this, "patrace-1", 1000, 0);
threads.emplace_back(&Test8::test8_worker, this, "patrace-2", 1000, 1);
threads.emplace_back(&Test8::test8_worker, this, "mali-1", 100, 2);
threads.emplace_back(&Test8::test8_worker, this, "mali-2", 100, 3);
c = new Collection(config);
c->initialize();
c->start();
test8_ready.store(true);
test8_cv.notify_all();
for (auto &t : threads)
t.join();
c->stop();
Json::Value results = c->results();
Json::StyledWriter writer;
std::string data = writer.write(results);
printf("Results:\n%s", data.c_str());
c->writeJSON("results_collect_scope.json");
}
private:
void test8_worker(std::string const &thread_name, int ops, int scope_label_offset) {
prctl(PR_SET_NAME, (unsigned long)thread_name.c_str(), 0, 0, 0);
std::unique_lock<std::mutex> lk(test8_mtx);
test8_cv.wait(lk, [this] { return test8_ready.load(); });
printf("Thread %s started.\n", thread_name.c_str());
auto payload = [](int ops) {
int tmp = 1;
for (int i = 0; i < ops; i++)
tmp *= rand();
};
if (strncmp(thread_name.c_str(), "patrace", 7) == 0) {
c->collect_scope_start(0 + scope_label_offset, COLLECT_REPLAY_THREADS);
payload(1000);
c->collect_scope_stop(0 + scope_label_offset, COLLECT_REPLAY_THREADS);
}
if (strncmp(thread_name.c_str(), "mali", 4) == 0) {
c->collect_scope_start(1 + scope_label_offset, COLLECT_BG_THREADS);
payload(1000);
c->collect_scope_stop(1 + scope_label_offset, COLLECT_BG_THREADS);
}
printf("Thread %s finished.\n", thread_name.c_str());
}
Collection *c;
std::atomic<bool> test8_ready;
std::condition_variable test8_cv;
std::mutex test8_mtx;
};
int main()
{
srandom(time(NULL));
test1();
test2();
test3();
test4();
test5();
test6();
test7(); // summarized results
auto test8 = std::unique_ptr<Test8>(new Test8());
test8->run();
printf("ALL DONE!\n");
return 0;
}