-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflameprofiler.h
181 lines (161 loc) · 5.75 KB
/
flameprofiler.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* MIT License
*
* Copyright (c) 2018 Björn Blissing
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef _FLAMEPROFILER_H_
#define _FLAMEPROFILER_H_
#ifndef USE_PROFILER
// Expand to nothing if profiling is disabled
#define PZone(name)
#define PZoneCat( name, category )
#define PMetadata( title, value )
#else
// Macro concatenation
#define CONCAT_IMPL( x, y ) x##y
#define MACRO_CONCAT( x, y ) CONCAT_IMPL( x, y )
// Macro names
#define PZone( name ) Profiler::Zone MACRO_CONCAT( profileZone, __COUNTER__ )( name )
#define PZoneCat( name, category ) Profiler::Zone MACRO_CONCAT( profileZone, __COUNTER__ )( name , category )
#define PMetadata( title, value ) Profiler::FlameGraphWriter::instance().addMetadata( title, value )
#include <string>
#include <vector>
#include <cstdint>
#include <fstream>
#include <algorithm>
#include <limits>
#include <thread>
#include <cstdio>
#include <chrono>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
namespace Profiler {
struct TracePoint {
char name[64];
char category[40];
uint64_t timeStart;
uint64_t timeEnd;
uint32_t processId;
uint32_t threadId;
}; // 128 byte struct
class FlameGraphWriter {
public:
~FlameGraphWriter()
{
// Find first stored profiler time
uint64_t profilerTimerStart = (std::numeric_limits<uint64_t>::max)();
for (const auto& tracepoint : m_tracepoints) {
profilerTimerStart = (std::min)(profilerTimerStart, tracepoint.timeStart);
}
// Open file
std::ofstream ofs;
std::string filename = "profiler.json";
ofs.open(filename, std::ofstream::out);
// Write content
if (ofs.is_open()) {
// Header
ofs << "{\n";
ofs << "\t\"traceEvents\": ";
// Begin trace events
ofs << "[";
// For each tracepoint write
std::string separator = "";
for (const auto& tracepoint : m_tracepoints) {
uint64_t duration = tracepoint.timeEnd - tracepoint.timeStart;
ofs << separator << "\n";
ofs << "\t\t{";
ofs << " \"pid\":" << tracepoint.processId << ",";
ofs << " \"tid\":" << tracepoint.threadId << ",";
ofs << " \"ts\":" << tracepoint.timeStart - profilerTimerStart << ",";
ofs << " \"dur\":" << duration << ",";
ofs << " \"ph\":\"X\",";
ofs << " \"name\":\"" << tracepoint.name << "\",";
ofs << " \"cat\":\"" << tracepoint.category << "\"";
ofs << "}";
separator = ",";
}
ofs << "\n\t]";
// End trace events
// Begin metadata
for (const auto& metadata : m_metadata) {
ofs << ",\n";
ofs << "\t\"" << metadata.first << "\": \"" << metadata.second << "\"";
}
// End metadata
ofs << "\n}\n";
// Close file
ofs.close();
}
}
static FlameGraphWriter& instance()
{
static FlameGraphWriter instance;
return instance;
}
void addMetadata(const std::string& title, const std::string& value)
{
std::pair<std::string, std::string> metadata = std::make_pair(title, value);
m_metadata.push_back(metadata);
}
void addTracePoint(const TracePoint& point) { m_tracepoints.push_back(point); }
private:
FlameGraphWriter() {
// Reserve points to avoid early memory reallocations
m_tracepoints.reserve(128);
}
FlameGraphWriter(const FlameGraphWriter&) = delete; // No copy allowed
FlameGraphWriter(const FlameGraphWriter&&) = delete; // No move allowed
FlameGraphWriter& operator= (const FlameGraphWriter&) = delete; // No assignment allowed
FlameGraphWriter& operator=(FlameGraphWriter&&) = delete; // No move assignment allowed
std::string m_filename;
std::vector<TracePoint> m_tracepoints;
std::vector<std::pair<std::string, std::string>> m_metadata;
};
class Zone {
public:
Zone(const char* name, const char* category = "default")
{
snprintf(m_tracepoint.name, sizeof(m_tracepoint.name), "%s", name);
snprintf(m_tracepoint.category, sizeof(m_tracepoint.category), "%s", category);
size_t thread = std::hash<std::thread::id>()(std::this_thread::get_id());
m_tracepoint.threadId = static_cast<uint32_t>(thread);
auto timer = std::chrono::high_resolution_clock::now();
m_tracepoint.timeStart = std::chrono::duration_cast<std::chrono::microseconds>(timer.time_since_epoch()).count();
#ifdef _WIN32
m_tracepoint.processId = static_cast<uint32_t>(GetCurrentProcessId());
#else
m_tracepoint.processId = static_cast<uint32_t>(::getpid());
#endif
}
~Zone()
{
auto timer = std::chrono::high_resolution_clock::now();
m_tracepoint.timeEnd = std::chrono::duration_cast<std::chrono::microseconds>(timer.time_since_epoch()).count();
FlameGraphWriter::instance().addTracePoint(m_tracepoint);
}
private:
TracePoint m_tracepoint;
};
} // namespace profiler
#endif // USE_PROFILER
#endif // !_FLAMEPROFILER_H_