-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofiler.h
123 lines (102 loc) · 3.6 KB
/
profiler.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
// Copyright (c) 2017, Raffaele Solcà
// All rights reserved.
//
// See LICENSE.txt for terms of usage.
#ifndef PROFILER_H
#define PROFILER_H
#include <chrono>
#include <deque>
#include <string>
#include <vector>
#include <fstream>
#include <unistd.h>
#include "util.h"
namespace profiler {
using TimeType = unsigned long long;
class TaskProfileData {
public:
TaskProfileData(const std::string& task_name, const std::string& task_group_name,
int thread_id_start, TimeType time_start, int thread_id_end, TimeType time_end)
: task_name_(task_name), task_group_name_(task_group_name), thread_id_start_(thread_id_start),
time_start_(time_start), thread_id_end_(thread_id_end), time_end_(time_end) {}
template <class Out>
void write(Out& out_stream) {
out_stream << task_name_ << ", ";
out_stream << task_group_name_ << ", ";
out_stream << thread_id_start_ << ", ";
out_stream << time_start_ << ", ";
out_stream << thread_id_end_ << ", ";
out_stream << time_end_ << std::endl;
}
private:
std::string task_name_;
std::string task_group_name_;
int thread_id_start_;
TimeType time_start_;
int thread_id_end_;
TimeType time_end_;
};
class ThreadProfiler {
public:
void add(const std::string& task_name, const std::string& task_group_name, int thread_id_start,
TimeType time_start, int thread_id_end, TimeType time_end) {
task_profiles.emplace_back(task_name, task_group_name, thread_id_start, time_start,
thread_id_end, time_end);
}
template <class Out>
void write(Out& out_stream) {
for (auto& task_profile : task_profiles)
task_profile.write(out_stream);
}
private:
std::deque<TaskProfileData> task_profiles;
};
// TODO: Fix interface
class Profiler {
public:
Profiler() : filename_("profile_" + std::to_string(getpid()) + ".txt"), profilers_(257) {}
~Profiler() {
std::ofstream fout(filename_);
for (auto& profiler : profilers_)
profiler.write(fout);
}
void setOutputFilename(std::string output_name) {
filename_ = output_name;
}
void add(const std::string& task_name, const std::string& task_group_name, int thread_id_start,
TimeType time_start, int thread_id_end, TimeType time_end) {
profilers_[thread_id_end + 1].add(task_name, task_group_name, thread_id_start, time_start,
thread_id_end, time_end);
}
TimeType getTime() {
return std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::high_resolution_clock::now().time_since_epoch())
.count();
}
static Profiler& getProfiler() {
static Profiler profiler;
return profiler;
}
private:
std::string filename_;
std::vector<ThreadProfiler> profilers_;
};
class GenericTaskProfiler {
public:
GenericTaskProfiler(int thread_id, std::string task_name, std::string task_group_name)
: task_name_(task_name), task_group_name_(task_group_name), thread_id_start_(thread_id),
time_start_(Profiler::getProfiler().getTime()) {}
~GenericTaskProfiler() {
int thread_id_end = thread_id_start_;
TimeType time_end = Profiler::getProfiler().getTime();
Profiler::getProfiler().add(task_name_, task_group_name_, thread_id_start_, time_start_,
thread_id_end, time_end);
}
private:
std::string task_name_;
std::string task_group_name_;
int thread_id_start_;
TimeType time_start_;
};
}
#endif // PROFILER_H