AYON Cpp Api  0.1.0
Loading...
Searching...
No Matches
Instrumentor.h
Go to the documentation of this file.
1#ifndef AYONCPPAPIINSTRUMENTOR_H
2#define AYONCPPAPIINSTRUMENTOR_H
3
4#include <string>
5#include <chrono>
6#include <algorithm>
7#include <fstream>
8#include <mutex>
9#include <thread>
10#include <future>
11
13 std::string Name;
14 long long Start, End;
15 uint32_t ThreadID;
16};
17
19 std::string Name;
20};
21
23 private:
25 std::ofstream m_OutputStream;
27 std::mutex m_Mutex; // Mutex for synchronization
28
29 public:
31 }
32
33 void
34 BeginSession(const std::string &name, const std::string &filepath = "results.json") {
35 std::lock_guard<std::mutex> lock(m_Mutex); // Lock mutex for critical section
36 m_OutputStream.open(filepath);
39 }
40
41 void
44 m_OutputStream.close();
45 delete m_CurrentSession;
46 m_CurrentSession = nullptr;
48 }
49
50 void
52 std::future<void> async = std::async(std::launch::async, [this, result]() { WriteProfile(result); });
53 async.get();
54 }
55
56 void
57 WriteProfile(const ProfileResult &result) {
58 std::lock_guard<std::mutex> lock(m_Mutex); // Lock mutex for critical section
59 if (m_ProfileCount++ > 0)
60 m_OutputStream << ",";
61
62 std::string name = result.Name;
63 std::replace(name.begin(), name.end(), '"', '\'');
64
65 m_OutputStream << "{";
66 m_OutputStream << "\"cat\":\"function\",";
67 m_OutputStream << "\"dur\":" << (result.End - result.Start) << ',';
68 m_OutputStream << "\"name\":\"" << name << "\",";
69 m_OutputStream << "\"ph\":\"X\",";
70 m_OutputStream << "\"pid\":0,";
71 m_OutputStream << "\"tid\":" << result.ThreadID << ",";
72 m_OutputStream << "\"ts\":" << result.Start;
73 m_OutputStream << "}";
74
75 m_OutputStream.flush();
76 }
77
78 void
80 m_OutputStream << "{\"otherData\": {},\"traceEvents\":[";
81 m_OutputStream.flush();
82 }
83
84 void
86 m_OutputStream << "]}";
87 m_OutputStream.flush();
88 }
89
90 static Instrumentor &
91 Get() {
92 static Instrumentor instance;
93 return instance;
94 }
95};
96
98 public:
99 InstrumentationTimer(const char* name): m_Name(name), m_Stopped(false) {
100 m_StartTimepoint = std::chrono::high_resolution_clock::now();
101 }
102
104 if (!m_Stopped)
105 Stop();
106 }
107
108 void
110 auto endTimepoint = std::chrono::high_resolution_clock::now();
111
112 long long start
113 = std::chrono::time_point_cast<std::chrono::microseconds>(m_StartTimepoint).time_since_epoch().count();
114 long long end
115 = std::chrono::time_point_cast<std::chrono::microseconds>(endTimepoint).time_since_epoch().count();
116
117 uint32_t threadID = std::hash<std::thread::id>{}(std::this_thread::get_id());
118
119 Instrumentor::Get().WriteProfileAsync({m_Name, start, end, threadID});
120
121 m_Stopped = true;
122 }
123
124 private:
125 const char* m_Name;
126 std::chrono::time_point<std::chrono::high_resolution_clock> m_StartTimepoint;
128};
129
130#endif // !AYONCPPAPIINSTRUMENTOR_H
Definition: Instrumentor.h:97
std::chrono::time_point< std::chrono::high_resolution_clock > m_StartTimepoint
Definition: Instrumentor.h:126
const char * m_Name
Definition: Instrumentor.h:125
~InstrumentationTimer()
Definition: Instrumentor.h:103
bool m_Stopped
Definition: Instrumentor.h:127
void Stop()
Definition: Instrumentor.h:109
InstrumentationTimer(const char *name)
Definition: Instrumentor.h:99
Definition: Instrumentor.h:22
void EndSession()
Definition: Instrumentor.h:42
static Instrumentor & Get()
Definition: Instrumentor.h:91
void WriteProfile(const ProfileResult &result)
Definition: Instrumentor.h:57
std::mutex m_Mutex
Definition: Instrumentor.h:27
std::ofstream m_OutputStream
Definition: Instrumentor.h:25
InstrumentationSession * m_CurrentSession
Definition: Instrumentor.h:24
void WriteFooter()
Definition: Instrumentor.h:85
int m_ProfileCount
Definition: Instrumentor.h:26
void WriteHeader()
Definition: Instrumentor.h:79
Instrumentor()
Definition: Instrumentor.h:30
void WriteProfileAsync(const ProfileResult &result)
Definition: Instrumentor.h:51
void BeginSession(const std::string &name, const std::string &filepath="results.json")
Definition: Instrumentor.h:34
Definition: Instrumentor.h:18
std::string Name
Definition: Instrumentor.h:19
Definition: Instrumentor.h:12
long long End
Definition: Instrumentor.h:14
std::string Name
Definition: Instrumentor.h:13
uint32_t ThreadID
Definition: Instrumentor.h:15
long long Start
Definition: Instrumentor.h:14