-
Notifications
You must be signed in to change notification settings - Fork 23
/
chrome_tracing.cc
71 lines (60 loc) · 2.15 KB
/
chrome_tracing.cc
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
#include "chrome_tracing.h"
#include <cstdint>
#include <fstream>
namespace chainer_compiler {
namespace runtime {
ChromeTracingEmitter::ChromeTracingEmitter() : base_time_(std::chrono::system_clock::now()) {
}
void ChromeTracingEmitter::AddEvent(Event* event) {
events_.emplace_back(event);
}
ChromeTracingEmitter::Event::Event(const std::string& c, const std::string& n, int p, int64_t f)
: category(c), name(n), pc(p), flops(f), start_time(std::chrono::system_clock::now()) {
}
void ChromeTracingEmitter::Event::Finish() {
end_time = std::chrono::system_clock::now();
}
ChromeTracingEmitter::ScopedEvent::ScopedEvent(
ChromeTracingEmitter* chrome_tracing, const std::string& category, const std::string& name, int pc, int64_t flops)
: event_(nullptr) {
if (chrome_tracing) {
event_ = new ChromeTracingEmitter::Event(category, name, pc, flops);
chrome_tracing->AddEvent(event_);
}
}
ChromeTracingEmitter::ScopedEvent::~ScopedEvent() {
if (event_) {
event_->Finish();
}
}
void ChromeTracingEmitter::Emit(const std::string& output_filename) const {
std::ofstream ofs(output_filename);
ofs << "[\n";
bool is_first = true;
for (const std::unique_ptr<Event>& event : events_) {
int64_t ts = std::chrono::duration_cast<std::chrono::microseconds>(event->start_time - base_time_).count();
int64_t dur = std::chrono::duration_cast<std::chrono::microseconds>(event->end_time - event->start_time).count();
if (!is_first) {
ofs << ",\n";
}
is_first = false;
ofs << "{";
ofs << "\"cat\":\"" << event->category << "\",";
ofs << "\"name\":\"" << event->name << "\",";
ofs << "\"ts\":" << ts << ",";
ofs << "\"dur\":" << dur << ",";
ofs << "\"tid\":1,";
ofs << "\"pid\":1,";
if (event->pc >= 0) {
ofs << "\"args\":{\"pc\":" << event->pc << "},";
}
if (event->flops > 0) {
ofs << "\"args\":{\"flops\":" << event->flops << "},";
}
ofs << "\"ph\":\"X\"";
ofs << "}";
}
ofs << "]\n";
}
} // namespace runtime
} // namespace chainer_compiler