This repository was archived by the owner on May 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathMain.cpp
More file actions
67 lines (55 loc) · 2.12 KB
/
Main.cpp
File metadata and controls
67 lines (55 loc) · 2.12 KB
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
#include <chrono>
#include <iostream>
#include <string>
#include <thread>
#include <vector>
#include <gflags/gflags.h>
#include <jemalloc/jemalloc.h>
#include "Mixer.h"
#include "Distribution.h"
DEFINE_int32(num_producers, 1000, "number of producers to run on each thread");
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::make_shared;
using std::vector;
void createAndRunMixer(const Distribution &distr, int me,
vector<shared_ptr<ToFreeQueue>> toFreeQueues) {
Mixer m(FLAGS_num_producers, distr, me, toFreeQueues);
m.run();
}
int main(int argc, char **argv) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
Distribution distr = parseDistribution(FLAGS_distribution_file.c_str());
// Set up a work queue for each thread
vector<std::thread> threads;
vector<shared_ptr<ToFreeQueue>> toFreeQueues;
for (int i = 0; i < FLAGS_num_threads; i++) {
shared_ptr<ToFreeQueue> toFreeQ = make_shared<ToFreeQueue>();
toFreeQueues.push_back(toFreeQ);
}
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, toFreeQueues));
}
using namespace std::chrono;
high_resolution_clock::time_point beginTime = high_resolution_clock::now();
for (auto it = begin(threads); it != end(threads); ++it) {
it->join();
}
// Cleanup any remaining memory
for (int i = 0; i < FLAGS_num_threads; i++) {
toFreeQueues[i]->freeIgnoreLifetime();
}
high_resolution_clock::time_point endTime = high_resolution_clock::now();
duration<double> span = duration_cast<duration<double>>(endTime - beginTime);
if (FLAGS_print_malloc_stats) {
je_malloc_stats_print(NULL, NULL, NULL);
}
std::cout << "Elapsed time: " << span.count() << std::endl;
}