Line data Source code
1 : // DUNE DAQ modification notice:
2 : // This file has been modified from the original ATLAS oks source for the DUNE DAQ project.
3 : // Fork baseline commit: oks-08-03-04 (2022-04-14).
4 : // Renamed since fork: no.
5 :
6 : #define _OksBuildDll_
7 :
8 : #include "oks/profiler.hpp"
9 : #include "oks/kernel.hpp"
10 :
11 : namespace dunedaq {
12 : namespace oks {
13 :
14 : static const char* OksProfilerFunctionsStr[] = { "OksKernel::Destructor",
15 : "OksKernel::operator<<",
16 : "OksKernel::load_schema",
17 : "OksKernel::save_schema",
18 : "OksKernel::close_schema",
19 : "OksKernel::CreateSchemaClassList",
20 : "OksKernel::load_data",
21 : "OksKernel::save_data",
22 : "OksKernel::close_data",
23 : "OksKernel::CreateDataObjectList",
24 : "OksKernel::is_danglingObject",
25 : "OksKernel::is_danglingClass",
26 : "OksObject::StreamConstructor",
27 : "OksObject::NormalConstructor",
28 : "OksObject::CopyConstructor",
29 : "OksObject::Destructor",
30 : "OksObject::operator==",
31 : "OksObject::operator<<",
32 : "OksObject::SatisfiesQueryExpression",
33 : "OksObject::GetFreeObjectId",
34 : "OksObject::PutObject",
35 : "OksClass::Constructor",
36 : "OksClass::Destructor",
37 : "OksClass::operator<<",
38 : "OksClass::GetObject",
39 : "OksClass::execute_query",
40 : "OksClass::RegistrateClass",
41 : "OksClass::RegistrateClassChange",
42 : "OksClass::RegistrateAttributeChange",
43 : "OksClass::RegistrateRelationshipChange",
44 : "OksClass::RegistrateInstances",
45 : "OksClass::CreateSubClassesList",
46 : "oksStringToQuery",
47 : "oksStringToQueryExpression",
48 : "OksQuery::operator<<" };
49 :
50 : namespace oks {
51 : double
52 0 : get_time_interval(const timeval* t1, const timeval* t2)
53 : {
54 0 : return (t1->tv_sec - t2->tv_sec) + (t1->tv_usec - t2->tv_usec) / 1000000.;
55 : }
56 : }
57 :
58 0 : OksProfiler::OksProfiler()
59 : {
60 0 : for (short i = 0; i <= (short)QueryOperatorFrom; i++) {
61 0 : t_total[i] = 0.0;
62 0 : c_total[i] = 0;
63 : }
64 :
65 0 : p_start_time_point = std::chrono::steady_clock::now();
66 0 : }
67 :
68 : static void
69 0 : printTableSeparator(std::ostream& s, unsigned char c)
70 : {
71 0 : s << '+';
72 0 : s.fill(c);
73 0 : s.width(41);
74 0 : s << "" << '+';
75 0 : s.width(12);
76 0 : s << "" << '+';
77 0 : s.width(10);
78 0 : s << "" << '+';
79 0 : s.width(10);
80 0 : s << "" << '+' << std::endl;
81 0 : }
82 :
83 : static void
84 0 : printTableLine(std::ostream& s, unsigned char c)
85 : {
86 0 : s << '+';
87 0 : s.fill(c);
88 0 : s.width(76);
89 0 : s << "" << '+' << std::endl;
90 0 : }
91 :
92 : std::ostream&
93 0 : operator<<(std::ostream& s, const OksProfiler& t)
94 : {
95 0 : const int p = s.precision(4);
96 :
97 0 : printTableLine(s, '=');
98 :
99 0 : s << '|';
100 0 : s.fill(' ');
101 0 : s.width(76);
102 0 : s.setf(std::ios::left, std::ios::adjustfield);
103 0 : s << " O K S P R O F I L E R " << '|' << std::endl;
104 :
105 0 : printTableLine(s, '-');
106 :
107 0 : s << "| Time: ";
108 0 : s.setf(std::ios::right, std::ios::adjustfield);
109 0 : s << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - t.p_start_time_point)
110 0 : .count() /
111 0 : 1000.;
112 0 : s.fill(' ');
113 0 : s.width(64);
114 0 : s.setf(std::ios::left, std::ios::adjustfield);
115 0 : s << " seconds" << '|' << std::endl;
116 :
117 0 : printTableSeparator(s, '-');
118 :
119 0 : s.fill(' ');
120 0 : s.width(42);
121 0 : s << "| Function name" << '|';
122 0 : s.width(12);
123 0 : s << " # of calls" << '|';
124 0 : s.width(10);
125 0 : s << " total T," << '|';
126 0 : s.width(10);
127 0 : s << " ~T/call," << '|' << std::endl;
128 :
129 0 : s.fill(' ');
130 0 : s.width(42);
131 0 : s << '|' << "" << '|';
132 0 : s.width(12);
133 0 : s << "" << '|';
134 0 : s.width(10);
135 0 : s << " ms" << '|';
136 0 : s.width(10);
137 0 : s << " µs" << '|' << std::endl;
138 :
139 0 : for (short i = 0; i <= (short)OksProfiler::QueryOperatorFrom; i++) {
140 : if (i == 0 || i == 12 || i == 21 || i == 32)
141 0 : printTableSeparator(s, '-');
142 :
143 0 : s << "| ";
144 :
145 0 : s.width(40);
146 0 : s.fill(' ');
147 0 : s.setf(std::ios::left, std::ios::adjustfield);
148 0 : s << OksProfilerFunctionsStr[i] << '|';
149 :
150 0 : s.width(11);
151 0 : s.setf(std::ios::right, std::ios::adjustfield);
152 0 : s << t.c_total[i] << ' ' << '|';
153 :
154 0 : if (t.c_total[i]) {
155 0 : s.width(9);
156 0 : s << t.t_total[i] / 1000. << ' ' << '|';
157 :
158 0 : s.width(9);
159 0 : s << t.t_total[i] / (double)t.c_total[i];
160 : } else {
161 0 : s.width(10);
162 0 : s << " - " << '|';
163 :
164 0 : s.width(9);
165 0 : s << " -";
166 : }
167 :
168 0 : s << " |\n";
169 : }
170 :
171 0 : printTableSeparator(s, '=');
172 :
173 0 : s.precision(p);
174 :
175 0 : return s;
176 : }
177 :
178 : void
179 0 : OksProfiler::Start(OksProfiler::FunctionID fid, std::chrono::time_point<std::chrono::steady_clock>& time_point)
180 : {
181 0 : time_point = std::chrono::steady_clock::now();
182 0 : c_total[fid]++;
183 0 : }
184 :
185 : void
186 0 : OksProfiler::Stop(OksProfiler::FunctionID fid, const std::chrono::time_point<std::chrono::steady_clock>& time_point)
187 : {
188 0 : t_total[fid] +=
189 0 : std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - time_point).count();
190 0 : }
191 :
192 56697 : OksFunctionProfiler::OksFunctionProfiler(OksProfiler::FunctionID fid, const OksKernel* k)
193 56697 : : kernel(k)
194 56697 : , func_id(fid)
195 : {
196 56698 : if (k && k->get_profiling_mode())
197 0 : k->GetOksProfiler()->Start(fid, p_start_time_point);
198 56698 : }
199 :
200 56698 : OksFunctionProfiler::~OksFunctionProfiler()
201 : {
202 56698 : if (kernel && kernel->get_profiling_mode())
203 0 : kernel->GetOksProfiler()->Stop(func_id, p_start_time_point);
204 56698 : }
205 :
206 : } // namespace oks
207 : } // namespace dunedaq
|