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