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