-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tracer.hpp
145 lines (119 loc) · 6.45 KB
/
Tracer.hpp
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
/*
MIT License
Copyright(c) 2024 George Fotopoulos
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.
*/
#pragma once
#include <chrono>
#include <fstream>
#include <mutex>
#include <string>
#include <nlohmann/json.hpp>
#ifdef _WIN32
#define NOMINMAX
#include <Windows.h>
#else
#include <unistd.h>
#endif
#define TRACER_CONCAT_IMPL(a, b) a##b
#define TRACER_CONCAT(a, b) TRACER_CONCAT_IMPL(a, b)
#if defined(__GNUC__) || defined(__clang__)
#define TRACER_FUNCTION(categories, ...) TracerScope TRACER_CONCAT(_tracer_scope_, __LINE__)(__PRETTY_FUNCTION__, categories, #__VA_ARGS__)
#elif defined(_MSC_VER)
#define TRACER_FUNCTION(categories, ...) TracerScope TRACER_CONCAT(_tracer_scope_, __LINE__)(__FUNCSIG__, categories, #__VA_ARGS__)
#else
#define TRACER_FUNCTION(categories, ...) TracerScope TRACER_CONCAT(_tracer_scope_, __LINE__)(__func__, categories, #__VA_ARGS__)
#endif
#define TRACER_SCOPE(name, categories, ...) TracerScope TRACER_CONCAT(_tracer_scope_, __LINE__)(name, categories, #__VA_ARGS__)
#define TRACER_DURATION_EVENT_BEGIN(name, categories, ...) Tracer::getInstance().traceEvent(name, categories, "B", Tracer::getProcessId(), Tracer::getThreadId(), Tracer::getTimestamp(), #__VA_ARGS__)
#define TRACER_DURATION_EVENT_END(name, categories, ...) Tracer::getInstance().traceEvent(name, categories, "E", Tracer::getProcessId(), Tracer::getThreadId(), Tracer::getTimestamp(), #__VA_ARGS__)
#define TRACER_INSTANT_EVENT(name, categories, ...) Tracer::getInstance().traceEvent(name, categories, "i", Tracer::getProcessId(), Tracer::getThreadId(), Tracer::getTimestamp(), #__VA_ARGS__)
#define TRACER_COUNTER_EVENT(name, categories, ...) Tracer::getInstance().traceEvent(name, categories, "C", Tracer::getProcessId(), Tracer::getThreadId(), Tracer::getTimestamp(), #__VA_ARGS__)
#define TRACER_ASYNC_EVENT_BEGIN(name, categories, ...) Tracer::getInstance().traceEvent(name, categories, "b", Tracer::getProcessId(), Tracer::getThreadId(), Tracer::getTimestamp(), #__VA_ARGS__)
#define TRACER_ASYNC_EVENT_INSTANT(name, categories, ...) Tracer::getInstance().traceEvent(name, categories, "n", Tracer::getProcessId(), Tracer::getThreadId(), Tracer::getTimestamp(), #__VA_ARGS__)
#define TRACER_ASYNC_EVENT_END(name, categories, ...) Tracer::getInstance().traceEvent(name, categories, "e", Tracer::getProcessId(), Tracer::getThreadId(), Tracer::getTimestamp(), #__VA_ARGS__)
#define TRACER_FLOW_EVENT_BEGIN(name, categories, ...) Tracer::getInstance().traceEvent(name, categories, "s", Tracer::getProcessId(), Tracer::getThreadId(), Tracer::getTimestamp(), #__VA_ARGS__)
#define TRACER_FLOW_EVENT_INSTANT(name, categories, ...) Tracer::getInstance().traceEvent(name, categories, "t", Tracer::getProcessId(), Tracer::getThreadId(), Tracer::getTimestamp(), #__VA_ARGS__)
#define TRACER_FLOW_EVENT_END(name, categories, ...) Tracer::getInstance().traceEvent(name, categories, "f", Tracer::getProcessId(), Tracer::getThreadId(), Tracer::getTimestamp(), #__VA_ARGS__)
#define TRACER_DUMP(filename, indentation) Tracer::getInstance().dump(filename, indentation)
class Tracer {
public:
Tracer(const Tracer&) = delete;
Tracer(Tracer&&) = delete;
static Tracer& getInstance() {
static Tracer instance;
return instance;
}
static std::uint64_t getTimestamp() {
const auto now = std::chrono::high_resolution_clock::now();
const auto duration = now.time_since_epoch();
const auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(duration);
return static_cast<std::uint64_t>(microseconds.count());
}
static std::uint64_t getProcessId() {
#ifdef _WIN32
return GetCurrentProcessId();
#else
return static_cast<std::uint64_t>(getpid());
#endif
}
static std::uint64_t getThreadId() {
#ifdef _WIN32
return GetCurrentThreadId();
#else
return static_cast<std::uint64_t>(pthread_self());
#endif
}
void traceEvent(const char *name, const char *categories, const char *phase, std::uint64_t pid, std::uint64_t tid, std::uint64_t timestamp, const nlohmann::json& args = nlohmann::json::object()) {
std::lock_guard<std::mutex> lock(mDataMutex);
nlohmann::json traceEvent;
traceEvent["name"] = name;
traceEvent["cat"] = categories;
traceEvent["ph"] = phase;
traceEvent["pid"] = pid;
traceEvent["tid"] = tid;
traceEvent["ts"] = timestamp;
traceEvent["args"] = args;
mData["traceEvents"].emplace_back(traceEvent);
}
void dump(const char* filename, int indent = 4) {
std::lock_guard<std::mutex> lock(mDataMutex);
std::ofstream ofs(filename);
ofs << mData.dump(indent);
}
private:
Tracer() = default;
~Tracer() = default;
nlohmann::json mData;
std::mutex mDataMutex;
};
class TracerScope {
public:
TracerScope(const char* name, const char* categories, const nlohmann::json& args) : mName(name), mCategories(categories), mArgs(args) {
Tracer::getInstance().traceEvent(name, categories, "B", Tracer::getProcessId(), Tracer::getThreadId(), Tracer::getTimestamp(), args);
}
TracerScope(const char* name, const char* categories) : mName(name), mCategories(categories), mArgs(nlohmann::json::object()) {
Tracer::getInstance().traceEvent(name, categories, "B", Tracer::getProcessId(), Tracer::getThreadId(), Tracer::getTimestamp(), mArgs);
}
~TracerScope() {
Tracer::getInstance().traceEvent(mName, mCategories, "E", Tracer::getProcessId(), Tracer::getThreadId(), Tracer::getTimestamp(), mArgs);
}
private:
const char* mName;
const char* mCategories;
const nlohmann::json mArgs;
};