gtsam_points
Loading...
Searching...
No Matches
profiler.hpp
Go to the documentation of this file.
1#ifndef GLK_PROFILER_HPP
2#define GLK_PROFILER_HPP
3
4#include <chrono>
5#include <string>
6#include <vector>
7#include <iomanip>
8#include <sstream>
9#include <iostream>
10
11#include <GL/gl3w.h>
12
13namespace glk {
14
16public:
17 GLProfiler(const std::string& prof_name, bool enabled = true, int max_num_queries = 16) : prof_name(prof_name), enabled(enabled) {
18 if (!enabled) {
19 return;
20 }
21 queries.resize(max_num_queries);
22 glGenQueries(max_num_queries, queries.data());
23 }
24
26 if (!enabled) {
27 return;
28 }
29
30 if (labels.empty()) {
31 glDeleteQueries(queries.size(), queries.data());
32 return;
33 }
34
36
37 std::stringstream sst;
38 sst << "--- " << prof_name << " ---\n";
39
40 int max_name_length = 0;
41 for (const auto& label : labels) {
42 max_name_length = std::max<int>(max_name_length, label.size());
43 }
44 std::string label_format = "%-" + std::to_string(max_name_length) + "s";
45
46 double sum_time_msec = 0.0;
47 for (int i = 0; i < labels.size(); i++) {
48 int result = 0;
50 double time_msec = result / 1e6;
52
53 sst << "- " << std::left << std::setfill(' ') << std::setw(max_name_length) << labels[i] << " : " << std::fixed << std::setprecision(3) << time_msec << "[msec] ("
54 << std::fixed << std::setprecision(3) << sum_time_msec << "[msec])\n";
55 }
56 sst << "***\n";
57 sst << "- total(approx) : " << std::fixed << std::setprecision(3) << sum_time_msec << "[msec]";
58
59 std::cout << sst.str() << std::endl;
60
61 glDeleteQueries(queries.size(), queries.data());
62 }
63
64 void add(const std::string& label) {
65 if (!enabled) {
66 return;
67 }
68
69 int current = labels.size();
70 if (current != 0) {
72 }
73
74 if (current == queries.size()) {
75 int n = queries.size();
76 queries.resize(n * 2);
77 glGenQueries(n, queries.data() + n);
78 }
79
80 glBeginQuery(GL_TIME_ELAPSED, queries[current]);
81 labels.push_back(label);
82 }
83
84private:
85 const std::string prof_name;
86 const bool enabled;
87
88 std::vector<std::string> labels;
89 std::vector<GLuint> queries;
90};
91
93public:
94 RealProfiler(const std::string& prof_name, bool enabled = true) : prof_name(prof_name), enabled(enabled) {}
95
97 if (!enabled) {
98 return;
99 }
100
101 if (labels.empty()) {
102 return;
103 }
104
105 labels.push_back("end");
106 times.push_back(std::chrono::high_resolution_clock::now());
107
108 std::stringstream sst;
109 sst << "--- " << prof_name << " ---\n";
110
111 int max_name_length = 0;
112 for (const auto& label : labels) {
113 max_name_length = std::max<int>(max_name_length, label.size());
114 }
115
116 for (int i = 0; i < labels.size() - 1; i++) {
117 double time_msec = std::chrono::duration_cast<std::chrono::nanoseconds>(times[i + 1] - times[i]).count() / 1e6;
118 double sum_time_msec = std::chrono::duration_cast<std::chrono::nanoseconds>(times[i + 1] - times.front()).count() / 1e6;
119
120 sst << "- " << std::left << std::setfill(' ') << std::setw(max_name_length) << labels[i] << " : " << std::fixed << std::setprecision(3) << time_msec << "[msec] ("
121 << std::fixed << std::setprecision(3) << sum_time_msec << "[msec])\n";
122 }
123
124 sst << "***\n";
125 double sum_time_msec = std::chrono::duration_cast<std::chrono::nanoseconds>(times.back() - times.front()).count() / 1e6;
126
127 sst << " - total(approx) : " << std::fixed << std::setprecision(3) << sum_time_msec << "[msec]";
128 std::cout << sst.str() << std::endl;
129 }
130
131 void add(const std::string& label) {
132 if (!enabled) {
133 return;
134 }
135
136 labels.push_back(label);
137 times.push_back(std::chrono::high_resolution_clock::now());
138 }
139
140private:
141 const std::string prof_name;
142 const bool enabled;
143
144 std::vector<std::string> labels;
145 std::vector<std::chrono::high_resolution_clock::time_point> times;
146};
147
149public:
150 GLRealProfiler(const std::string& prof_name, bool enabled = true, int max_num_queries = 16) : prof_name(prof_name), enabled(enabled) {
151 if (!enabled) {
152 return;
153 }
154 queries.resize(max_num_queries);
155 glGenQueries(max_num_queries, queries.data());
156 }
157
159 if (!enabled) {
160 return;
161 }
162
163 if (labels.empty()) {
164 glDeleteQueries(queries.size(), queries.data());
165 return;
166 }
167
169 times.push_back(std::chrono::high_resolution_clock::now());
170
171 std::stringstream sst;
172 sst << "--- " << prof_name << " ---\n";
173
174 int max_name_length = 0;
175 for (const auto& label : labels) {
176 max_name_length = std::max<int>(max_name_length, label.size());
177 }
178 std::string label_format = "%-" + std::to_string(max_name_length) + "s";
179
180 double sum_time_msec_gl = 0.0;
181 for (int i = 0; i < labels.size(); i++) {
182 int result = 0;
184 double time_msec_gl = result / 1e6;
186
187 double time_msec_real = std::chrono::duration_cast<std::chrono::nanoseconds>(times[i + 1] - times[i]).count() / 1e6;
188 double sum_time_msec_real = std::chrono::duration_cast<std::chrono::nanoseconds>(times[i + 1] - times.front()).count() / 1e6;
189
190 sst << "- " << std::left << std::setfill(' ') << std::setw(max_name_length) << labels[i] << " : " << std::fixed << std::setprecision(3) << time_msec_gl << "[msec] " << "r"
191 << std::fixed << std::setprecision(3) << time_msec_real << "[msec] (" << std::fixed << std::setprecision(3) << sum_time_msec_gl << "[msec] r" << std::fixed
192 << std::setprecision(3) << sum_time_msec_real << "[msec])\n";
193 }
194 sst << "***\n";
195 double sum_time_msec_real = std::chrono::duration_cast<std::chrono::nanoseconds>(times.back() - times.front()).count() / 1e6;
196
197 sst << "- " << std::left << std::setfill(' ') << std::setw(max_name_length) << labels.back() << " : " << std::fixed << std::setprecision(3) << 0.0 << "[msec] " << "r"
198 << std::fixed << std::setprecision(3) << 0.0 << "[msec] (" << std::fixed << std::setprecision(3) << sum_time_msec_gl << "[msec] r" << std::fixed << std::setprecision(3)
199 << sum_time_msec_real << "[msec])\n";
200
201 std::cout << sst.str() << std::endl;
202
203 glDeleteQueries(queries.size(), queries.data());
204 }
205
206 void add(const std::string& label) {
207 if (!enabled) {
208 return;
209 }
210
211 int current = labels.size();
212 if (current != 0) {
214 }
215
216 if (current == queries.size()) {
217 int n = queries.size();
218 queries.resize(n * 2);
219 glGenQueries(n, queries.data() + n);
220 }
221
222 glBeginQuery(GL_TIME_ELAPSED, queries[current]);
223 labels.push_back(label);
224 times.push_back(std::chrono::high_resolution_clock::now());
225 }
226
227private:
228 const std::string prof_name;
229 const bool enabled;
230
231 std::vector<std::string> labels;
232 std::vector<GLuint> queries;
233 std::vector<std::chrono::high_resolution_clock::time_point> times;
234};
235
236} // namespace glk
237
238#endif
Definition profiler.hpp:15
void add(const std::string &label)
Definition profiler.hpp:64
~GLProfiler()
Definition profiler.hpp:25
GLProfiler(const std::string &prof_name, bool enabled=true, int max_num_queries=16)
Definition profiler.hpp:17
Definition profiler.hpp:148
~GLRealProfiler()
Definition profiler.hpp:158
GLRealProfiler(const std::string &prof_name, bool enabled=true, int max_num_queries=16)
Definition profiler.hpp:150
void add(const std::string &label)
Definition profiler.hpp:206
Definition profiler.hpp:92
~RealProfiler()
Definition profiler.hpp:96
void add(const std::string &label)
Definition profiler.hpp:131
RealProfiler(const std::string &prof_name, bool enabled=true)
Definition profiler.hpp:94
Definition async_buffer_copy.hpp:6
std::enable_if_t< needs_aligned_allocator< T >::value, std::shared_ptr< T > > make_shared(Args &&... args)
Definition make_shared.hpp:20