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 <algorithm>
5#include <chrono>
6#include <fstream>
7#include <future>
8#include <mutex>
9#include <string>
10#include <thread>
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 BeginSession(const std::string &name, const std::string &filepath = "results.json") {
34 std::lock_guard<std::mutex> lock(m_mutex); // Lock mutex for critical section
35 m_outputStream.open(filepath);
38 }
39
40 void endSession() {
42 m_outputStream.close();
43 delete m_currentSession;
44 m_currentSession = nullptr;
46 }
47
48 void WriteProfileAsync(const ProfileResult &result) {
49 std::future<void> asyncResult = std::async(std::launch::async, [this, result]() { WriteProfile(result); });
50 asyncResult.get();
51 }
52
53 void WriteProfile(const ProfileResult &result) {
54 std::lock_guard<std::mutex> lock(m_mutex); // Lock mutex for critical section
55 if (m_profileCount++ > 0)
56 m_outputStream << ",";
57
58 std::string name = result.name;
59 std::replace(name.begin(), name.end(), '"', '\'');
60
61 m_outputStream << "{";
62 m_outputStream << "\"cat\":\"function\",";
63 m_outputStream << "\"dur\":" << (result.end - result.start) << ',';
64 m_outputStream << "\"name\":\"" << name << "\",";
65 m_outputStream << "\"ph\":\"X\",";
66 m_outputStream << "\"pid\":0,";
67 m_outputStream << "\"tid\":" << result.threadID << ",";
68 m_outputStream << "\"ts\":" << result.start;
69 m_outputStream << "}";
70
71 m_outputStream.flush();
72 }
73
74 void WriteHeader() {
75 m_outputStream << "{\"otherData\": {},\"traceEvents\":[";
76 m_outputStream.flush();
77 }
78
79 void WriteFooter() {
80 m_outputStream << "]}";
81 m_outputStream.flush();
82 }
83
84 static Instrumentor & Get() {
85 static Instrumentor instance;
86 return instance;
87 }
88};
89
91 public:
92 InstrumentationTimer(const char* name): m_name(name), m_stopped(false) {
93 m_startTimepoint = std::chrono::high_resolution_clock::now();
94 }
95
97 if (!m_stopped)
98 Stop();
99 }
100
101 void Stop() {
102 auto endTimepoint = std::chrono::high_resolution_clock::now();
103
104 long long start
105 = std::chrono::time_point_cast<std::chrono::microseconds>(m_startTimepoint).time_since_epoch().count();
106 long long end
107 = std::chrono::time_point_cast<std::chrono::microseconds>(endTimepoint).time_since_epoch().count();
108
109 uint32_t threadID = std::hash<std::thread::id>{}(std::this_thread::get_id());
110
111 Instrumentor::Get().WriteProfileAsync({m_name, start, end, threadID});
112
113 m_stopped = true;
114 }
115
116 private:
117 const char* m_name;
118 std::chrono::time_point<std::chrono::high_resolution_clock> m_startTimepoint;
120};
121
122#endif // !AYONCPPAPIINSTRUMENTOR_H
Definition: Instrumentor.h:90
bool m_stopped
Definition: Instrumentor.h:119
~InstrumentationTimer()
Definition: Instrumentor.h:96
const char * m_name
Definition: Instrumentor.h:117
void Stop()
Definition: Instrumentor.h:101
std::chrono::time_point< std::chrono::high_resolution_clock > m_startTimepoint
Definition: Instrumentor.h:118
InstrumentationTimer(const char *name)
Definition: Instrumentor.h:92
Definition: Instrumentor.h:22
void endSession()
Definition: Instrumentor.h:40
static Instrumentor & Get()
Definition: Instrumentor.h:84
void WriteProfile(const ProfileResult &result)
Definition: Instrumentor.h:53
std::mutex m_mutex
Definition: Instrumentor.h:27
InstrumentationSession * m_currentSession
Definition: Instrumentor.h:24
int m_profileCount
Definition: Instrumentor.h:26
void WriteFooter()
Definition: Instrumentor.h:79
std::ofstream m_outputStream
Definition: Instrumentor.h:25
void WriteHeader()
Definition: Instrumentor.h:74
Instrumentor()
Definition: Instrumentor.h:30
void WriteProfileAsync(const ProfileResult &result)
Definition: Instrumentor.h:48
void BeginSession(const std::string &name, const std::string &filepath="results.json")
Definition: Instrumentor.h:33
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
long long start
Definition: Instrumentor.h:14
uint32_t threadID
Definition: Instrumentor.h:15