-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathMain.cpp
77 lines (62 loc) · 2.2 KB
/
Main.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
#include <chrono>
#include <iostream>
#include <string>
#include <thread>
#include <vector>
#include <gflags/gflags.h>
#include <jemalloc/jemalloc.h>
#include "Distribution.h"
#include "Mixer.h"
DEFINE_int32(num_threads, 1, "number of threads to run");
DEFINE_bool(print_malloc_stats, false, "print out malloc stats after running");
DEFINE_string(distribution_file, "", "path to distribution file");
static bool validateDistributionFile(const char *flagName,
const std::string &val) {
return val.length() != 0;
}
DEFINE_validator(distribution_file, &validateDistributionFile);
using std::shared_ptr;
using std::vector;
void createAndRunMixer(const Distribution *distr, int me,
vector<shared_ptr<ThreadObject>> threadObjects) {
Mixer m(distr, me, threadObjects);
m.run();
}
double run() {
initInstBurner();
Distribution distr = parseDistribution(FLAGS_distribution_file.c_str());
// Set up a work queue for each thread
vector<std::thread> threads;
vector<shared_ptr<ThreadObject>> threadObjects;
for (int i = 0; i < FLAGS_num_threads; i++) {
auto threadObject = shared_ptr<ThreadObject>(new ThreadObject());
threadObjects.push_back(threadObject);
}
for (int i = 0; i < FLAGS_num_threads; i++) {
// each thread gets an arbitrary id given by [i]
threads.push_back(std::thread(createAndRunMixer, &distr, i, threadObjects));
}
using namespace std::chrono;
high_resolution_clock::time_point beginTime = high_resolution_clock::now();
for (auto &t : threads) {
t.join();
}
// Cleanup any remaining memory
for (auto &t : threadObjects) {
t->freeIgnoreLifetime();
}
high_resolution_clock::time_point endTime = high_resolution_clock::now();
duration<double> span = duration_cast<duration<double>>(endTime - beginTime);
return span.count();
}
int main(int argc, char **argv) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
double time = run();
if (FLAGS_print_malloc_stats) {
if (mallctl("thread.tcache.flush", NULL, NULL, NULL, 0)) {
std::cout << "je_mallctl failed. Exiting..." << std::endl;
}
malloc_stats_print(NULL, NULL, NULL);
}
std::cout << "Elapsed time: " << time << std::endl;
}