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