-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathtracing.cpp
More file actions
127 lines (98 loc) · 2.86 KB
/
tracing.cpp
File metadata and controls
127 lines (98 loc) · 2.86 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
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "tracing.h"
#include <chrono>
#include <fstream>
#include <mutex>
#include <optional>
#include <sstream>
#include <thread>
#include "models/env_utils.h"
namespace Generators {
#if defined(ORTGENAI_ENABLE_TRACING)
namespace {
// Writes trace events to a file in Chrome tracing format.
// See more details about the format here:
// https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU
class FileTraceSink : public TraceSink {
public:
FileTraceSink(std::string_view file_path)
: ostream_{std::ofstream{file_path.data()}},
start_{Clock::now()},
insert_event_delimiter_{false} {
ostream_ << "[";
}
~FileTraceSink() {
ostream_ << "]\n";
}
void BeginDuration(std::string_view label) {
LogEvent("B", label);
}
void EndDuration() {
LogEvent("E");
}
private:
using Clock = std::chrono::steady_clock;
void LogEvent(std::string_view phase_type, std::optional<std::string_view> label = std::nullopt) {
const auto thread_id = std::this_thread::get_id();
const auto ts = std::chrono::duration_cast<std::chrono::microseconds>(Clock::now() - start_);
std::ostringstream event{};
event << "{";
if (label.has_value()) {
event << "\"name\": \"" << *label << "\", ";
}
event << "\"cat\": \"perf\", "
<< "\"ph\": \"" << phase_type << "\", "
<< "\"pid\": 0, "
<< "\"tid\": " << thread_id << ", "
<< "\"ts\": " << ts.count()
<< "}";
{
std::scoped_lock g{output_mutex_};
// add the delimiter only after writing the first event
if (insert_event_delimiter_) {
ostream_ << ",\n";
} else {
insert_event_delimiter_ = true;
}
ostream_ << event.str();
}
}
std::ofstream ostream_;
const Clock::time_point start_;
bool insert_event_delimiter_;
std::mutex output_mutex_;
};
std::string GetTraceFileName() {
constexpr const char* kTraceFileEnvironmentVariableName = "ORTGENAI_TRACE_FILE_PATH";
auto trace_file_name = GetEnv(kTraceFileEnvironmentVariableName);
if (trace_file_name.empty()) {
trace_file_name = "ortgenai_trace.log";
}
return trace_file_name;
}
} // namespace
#endif // defined(ORTGENAI_ENABLE_TRACING)
Tracer::Tracer() {
#if defined(ORTGENAI_ENABLE_TRACING)
const auto trace_file_name = GetTraceFileName();
sink_ = std::make_unique<FileTraceSink>(trace_file_name);
#endif
}
void Tracer::BeginDuration(std::string_view label) {
#if defined(ORTGENAI_ENABLE_TRACING)
sink_->BeginDuration(label);
#else
static_cast<void>(label);
#endif
}
void Tracer::EndDuration() {
#if defined(ORTGENAI_ENABLE_TRACING)
sink_->EndDuration();
#endif
}
Tracer& DefaultTracerInstance() {
static auto tracer = Tracer{};
return tracer;
}
} // namespace Generators