UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
profile_timer.hpp
Go to the documentation of this file.
1#ifndef PROFILE_TIMER_HPP
2#define PROFILE_TIMER_HPP
3
4#ifdef BUILD_PROFILED
5
6#define C_PROFILER_PUSH(name) \
7 push_prof_timer((name))
8
9#define C_PROFILER_POP \
10 pop_prof_timer()
11
12#ifdef __cplusplus
13
14#include <chrono>
15#include <vector>
16#include <mutex>
17#include <thread>
18#include <fstream>
19#include <functional>
20
21#define THREAD_EVENT_BUF_SIZE 256
22
23#define PROFILE_FUNC \
24 Profile_timer PROFILER_PROFILE_TIMER_FUNC(Profiler_thread_inst::get_instance(), __PRETTY_FUNCTION__); \
25 Profile_timer PROFILER_PROFILE_TIMER_DETAIL(Profiler_thread_inst::get_instance(), "");
26
27#define PROFILE_DETAIL(name) \
28 PROFILER_PROFILE_TIMER_DETAIL = Profile_timer(Profiler_thread_inst::get_instance(), (name));
29
30
31struct Profile_event {
32 Profile_event(std::string name,
33 std::thread::id thread_id) :
34 name(name),
35 start(),
36 end(),
37 thread_id(thread_id) { }
38
39 Profile_event() = default;
40
41 std::string name;
42 std::chrono::steady_clock::time_point::rep start;
43 std::chrono::steady_clock::time_point::rep end;
44
45 std::thread::id thread_id;
46};
47
48class Profiler_thread_inst;
49
50class Profiler {
51 friend Profiler_thread_inst;
52 public:
53 static Profiler& get_instance();
54
55 Profiler(const Profiler&) = delete;
56 Profiler(Profiler&&) = delete;
57 Profiler& operator=(Profiler&&) = delete;
58 Profiler& operator=(const Profiler&) = delete;
59
60 void write_events(const std::vector<Profile_event>& events);
61
62 static std::chrono::steady_clock::time_point get_start_point(){
63 return start_point;
64 }
65
66 private:
67 Profiler();
68
69 std::mutex io_mut;
70 std::ofstream out_file;
71 std::thread::id pid;
72
73 bool active = false;
74
75 static std::chrono::steady_clock::time_point start_point;
76};
77
78class Profile_timer;
79
80class Profiler_thread_inst {
81 friend Profile_timer;
82 public:
83
84 Profiler_thread_inst(Profiler& profiler);
85 ~Profiler_thread_inst();
86
87 Profiler_thread_inst(const Profiler_thread_inst&) = delete;
88 Profiler_thread_inst(Profiler_thread_inst&&) = delete;
89 Profiler_thread_inst& operator=(Profiler_thread_inst&&) = delete;
90 Profiler_thread_inst& operator=(const Profiler_thread_inst&) = delete;
91
92 static Profiler_thread_inst& get_instance();
93
94 void add_event(Profile_event&& event);
95
96 void push_timer(const char *name);
97 void pop_timer();
98
99 private:
100 Profiler& profiler;
101 std::vector<Profile_event> thread_events;
102 std::chrono::steady_clock::time_point::rep last_ts;
103 std::vector<Profile_timer> c_timers;
104};
105
106class Profile_timer {
107 friend Profiler;
108 public:
109 Profile_timer(Profiler_thread_inst& profiler, std::string name);
110 Profile_timer(Profile_timer&&);
111 Profile_timer(const Profile_timer&) = delete;
112 ~Profile_timer();
113
114 Profile_timer& operator=(Profile_timer&& rhs);
115 Profile_timer& operator=(const Profile_timer&) = delete;
116
117 private:
118 void commit();
119
120 std::reference_wrapper<Profiler_thread_inst> profiler;
121 Profile_event event;
122};
123
124#endif //__cplusplus
125
126#ifdef __cplusplus
127extern "C" {
128#endif //__cplusplus
129
130 void push_prof_timer(const char *name);
131 void pop_prof_timer();
132
133#ifdef __cplusplus
134}
135#endif //__cplusplus
136
137#else //BUILD_PROFILED
138
139#define PROFILE_FUNC
140#define PROFILE_DETAIL(name)
141#define C_PROFILER_PUSH(name)
142#define C_PROFILER_POP
143
144#endif //BUILD_PROFILED
145
146#endif
static void start()
Definition sdp.c:202
const char * name
Definition settings.cpp:193