Debian Codegraph @

src/atlas/runtime/trace/Timings.cc

raw
1 /*
2 * (C) Copyright 2013 ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10
11
12 #include "Timings.h"
13
14 #include <cmath>
15 #include <iomanip>
16 #include <limits>
17 #include <regex>
18 #include <sstream>
19 #include <string>
20
21 #include "eckit/config/Configuration.h"
22 #include "eckit/filesystem/PathName.h"
23
24 #include "atlas/parallel/mpi/mpi.h"
25 #include "atlas/runtime/Exception.h"
26 #include "atlas/runtime/Log.h"
27 #include "atlas/runtime/trace/CallStack.h"
28 #include "atlas/runtime/trace/CodeLocation.h"
29 #include "atlas/util/Config.h"
30
31 //-----------------------------------------------------------------------------------------------------------
32
33 namespace atlas {
34 namespace runtime {
35 namespace trace {
36
37 class TimingsRegistry {
38 private:
39 std::vector<long> counts_;
40 std::vector<double> tot_timings_;
41 std::vector<double> min_timings_;
42 std::vector<double> max_timings_;
43 std::vector<double> var_timings_;
44 std::vector<std::string> titles_;
45 std::vector<CodeLocation> locations_;
46 std::vector<long> nest_;
47 std::vector<CallStack> stack_;
48 std::map<size_t, size_t> index_;
49
50 std::map<std::string, std::vector<size_t>> labels_;
51
52 TimingsRegistry() = default;
53
54 public:
55 static TimingsRegistry& instance() {
56 static TimingsRegistry registry;
57 return registry;
58 }
59
60 size_t add(const CodeLocation&, const CallStack& stack, const std::string& title, const Timings::Labels&);
61
62 void update(size_t idx, double seconds);
63
64 size_t size() const;
65
66 void report(std::ostream& out, const eckit::Configuration& config);
67
68 private:
69 std::string filter_filepath(const std::string& filepath) const;
70
71 friend struct Tree;
72 friend struct Node;
73 };
74
75 struct Node {
76 Node(): index(-1) {}
77 Node(size_t _index): index(_index) {
78 size_t _nest = TimingsRegistry::instance().nest_[index];
79
80
81 auto this_stack_hash = TimingsRegistry::instance().stack_[index].hash();
82 auto is_child = [&](size_t i) -> bool {
83 CallStack child_stack = TimingsRegistry::instance().stack_[i];
84 child_stack.pop();
85 auto child_stack_hash = child_stack.hash();
86 return child_stack_hash == this_stack_hash;
87 };
88
89
90 for (size_t i = index + 1; i < TimingsRegistry::instance().size(); ++i) {
91 if (TimingsRegistry::instance().nest_[i] == _nest + 1) {
92 if (is_child(i)) {
93 children.emplace_back(new Node(i));
94 }
95 }
96 }
97 }
98 std::vector<std::unique_ptr<Node>> children;
99 std::unique_ptr<Node> parent;
100 long index;
101 void print(std::ostream& out) {
102 if (index >= 0) {
103 size_t _nest = nest();
104 for (size_t i = 1; i < _nest; ++i) {
105 out << " ";
106 }
107 out << TimingsRegistry::instance().titles_[index] << std::endl;
108 }
109 for (auto& child : children) {
110 child->print(out);
111 }
112 }
113 size_t nest() const { return TimingsRegistry::instance().nest_[index]; }
114 void order(std::vector<size_t>& order) const {
115 if (index >= 0) {
116 order.emplace_back(index);
117 }
118 for (auto& child : children) {
119 child->order(order);
120 }
121 }
122 };
123 struct Tree {
124 Node root;
125 Tree() {
126 for (size_t j = 0; j < TimingsRegistry::instance().size(); ++j) {
127 auto& nest = TimingsRegistry::instance().nest_[j];
128 if (nest == 1) {
129 auto& children = root.children;
130 children.emplace_back(new Node(j));
131 }
132 }
133 }
134 void print(std::ostream& out) { root.print(out); }
135 std::vector<size_t> order() const {
136 std::vector<size_t> order;
137 order.reserve(TimingsRegistry::instance().size());
138 root.order(order);
139 ATLAS_ASSERT(order.size() == TimingsRegistry::instance().size(),
140 "Likely a atlas_Trace has not finalised properly");
141 return order;
142 }
143 };
144
145 size_t TimingsRegistry::add(const CodeLocation& loc, const CallStack& stack, const std::string& title,
146 const Timings::Labels& labels) {
147 size_t key = stack.hash();
148 auto it = index_.find(key);
149 if (it == index_.end()) {
150 size_t idx = size();
151 index_[key] = idx;
152 counts_.emplace_back(0);
153 tot_timings_.emplace_back(0);
154 min_timings_.emplace_back(std::numeric_limits<double>::max());
155 max_timings_.emplace_back(0);
156 var_timings_.emplace_back(0);
157 titles_.emplace_back(title);
158 locations_.emplace_back(loc);
159 nest_.emplace_back(stack.size());
160 stack_.emplace_back(stack);
161
162 for (const auto& label : labels) {
163 labels_[label].emplace_back(idx);
164 }
165
166 return idx;
167 }
168 else {
169 return it->second;
170 }
171 }
172
173 void TimingsRegistry::update(size_t idx, double seconds) {
174 auto sqr = [](double x) { return x * x; };
175 double n = counts_[idx] + 1;
176 double avg_nm1 = tot_timings_[idx] / std::max(n, 1.);
177 double var_nm1 = var_timings_[idx];
178 var_timings_[idx] = n == 1. ? 0. : (n - 2.) / (n - 1.) * var_nm1 + 1. / n * sqr(seconds - avg_nm1);
179 min_timings_[idx] = std::min(seconds, min_timings_[idx]);
180 max_timings_[idx] = std::max(seconds, max_timings_[idx]);
181 tot_timings_[idx] += seconds;
182 counts_[idx] += 1;
183 }
184
185 size_t TimingsRegistry::size() const {
186 return counts_.size();
187 }
188
189 void TimingsRegistry::report(std::ostream& out, const eckit::Configuration& config) {
190 auto box_horizontal = [](int n) {
191 std::string s;
192 s.reserve(2 * n);
193 for (int i = 0; i < n; ++i) {
194 s += "\u2500";
195 }
196 return s;
197 };
198 std::string box_corner_tl("\u250c");
199 std::string box_corner_tr("\u2510");
200 std::string box_corner_bl("\u2514");
201 std::string box_corner_br("\u2518");
202 std::string box_vertical("\u2502");
203 std::string box_T_down("\u252C");
204 std::string box_T_up("\u2534");
205 std::string box_T_right("\u251C");
206 std::string box_T_left("\u2524");
207 std::string box_cross("\u253C");
208
209 long indent = config.getLong("indent", 2);
210 long depth = config.getLong("depth", 0);
211 long decimals = config.getLong("decimals", 5);
212 bool header = config.getBool("header", true);
213 std::vector<std::string> excluded_labels_vector = config.getStringVector("exclude", std::vector<std::string>());
214 std::vector<std::string> include_back;
215
216 auto order = Tree().order();
217
218 for (auto& label : excluded_labels_vector) {
219 size_t found = label.find("/*");
220 if (found != std::string::npos) {
221 label.erase(found, 2);
222 include_back.push_back(label);
223 }
224 }
225
226 std::set<std::string> excluded_labels(excluded_labels_vector.begin(), excluded_labels_vector.end());
227
228 auto digits_before_decimal = [](double x) -> int {
229 return std::floor(std::log10(std::trunc(std::max(1., x)))) + 1;
230 };
231 auto digits = [](long x) -> long { return std::floor(std::log10(std::max(1l, x))) + 1l; };
232
233 std::vector<size_t> excluded_timers_vector;
234 for (auto& label : labels_) {
235 auto name = label.first;
236 if (excluded_labels.count(name)) {
237 auto timers = label.second;
238 for (size_t j : timers) {
239 excluded_timers_vector.push_back(j);
240 }
241 }
242 }
243 std::set<size_t> excluded_timers(excluded_timers_vector.begin(), excluded_timers_vector.end());
244
245 auto excluded = [&](size_t i) -> bool {
246 if (depth and nest_[i] > depth) {
247 return true;
248 }
249 return excluded_timers.count(i);
250 };
251
252 std::vector<long> excluded_nest_stored(size());
253 long excluded_nest = size();
254 for (size_t jj = 0; jj < size(); ++jj) {
255 size_t j = order[jj];
256 if (nest_[j] > excluded_nest) {
257 excluded_timers.insert(j);
258 }
259 if (not excluded(j)) {
260 excluded_nest = nest_[j] + 1;
261 }
262 else {
263 excluded_nest = std::min(excluded_nest, nest_[j]);
264 }
265 excluded_nest_stored[j] = excluded_nest;
266 }
267 for (auto& label : include_back) {
268 auto timers = labels_[label];
269 for (size_t j : timers) {
270 if (nest_[j] == excluded_nest_stored[j]) {
271 excluded_timers.erase(j);
272 }
273 }
274 }
275
276 size_t max_title_length(2);
277 size_t max_location_length(9);
278 size_t max_nest(0);
279 long max_count(0);
280 double max_seconds(0);
281 for (size_t j = 0; j < size(); ++j) {
282 size_t nest = nest_[j];
283 max_nest = std::max(max_nest, nest);
284 if (not excluded(j)) {
285 const auto& loc = locations_[j];
286 max_title_length = std::max(max_title_length, titles_[j].size() + nest_[j] * indent);
287 max_count = std::max(max_count, counts_[j]);
288 max_seconds = std::max(max_seconds, tot_timings_[j]);
289 size_t location_length = filter_filepath(loc.file()).size() + 2 + digits(loc.line());
290 max_location_length = std::max(max_location_length, location_length);
291 }
292 }
293 size_t max_count_length = digits(max_count);
294 if (header) {
295 max_count_length = std::max(std::string("cnt").size(), max_count_length);
296 }
297 size_t max_digits_before_decimal = digits_before_decimal(max_seconds);
298
299 auto print_time = [max_digits_before_decimal, decimals](double x) -> std::string {
300 std::stringstream out;
301 char unit = 's';
302 if (std::floor(x) >= 60) {
303 x /= 60.;
304 unit = 'm';
305 }
306 out << std::right << std::fixed << std::setprecision(decimals)
307 << std::setw(max_digits_before_decimal + decimals + 1) << x << unit;
308 return out.str();
309 };
310
311 auto print_line = [&](size_t length) -> std::string { return box_horizontal(length); };
312
313 auto print_horizontal = [&](const std::string& sep) -> std::string {
314 std::stringstream ss;
315 ss << print_line(max_title_length + digits(size()) + 3) << sep << print_line(max_count_length) << sep
316 << print_line(max_digits_before_decimal + decimals + 2) << sep
317 << print_line(max_digits_before_decimal + decimals + 2) << sep
318 << print_line(max_digits_before_decimal + decimals + 2) << sep
319 << print_line(max_digits_before_decimal + decimals + 2) << sep
320 << print_line(max_digits_before_decimal + decimals + 2) << sep << print_line(max_location_length);
321 return ss.str();
322 };
323
324 std::string sept = box_horizontal(1) + box_T_down + box_horizontal(1);
325 std::string seph = box_horizontal(1) + box_cross + box_horizontal(1);
326 std::string sep = std::string(" ") + box_vertical + std::string(" ");
327 std::string sepf = box_horizontal(1) + box_T_up + box_horizontal(1);
328
329 out << print_horizontal(sept) << std::endl;
330 out << std::left << std::setw(max_title_length + digits(size()) + 3) << "Timers" << sep
331 << std::setw(max_count_length) << "cnt" << sep << std::setw(max_digits_before_decimal + decimals + 2ul) << "tot"
332 << sep << std::setw(max_digits_before_decimal + decimals + 2ul) << "avg" << sep
333 << std::setw(max_digits_before_decimal + decimals + 2ul) << "std" << sep
334 << std::setw(max_digits_before_decimal + decimals + 2ul) << "min" << sep
335 << std::setw(max_digits_before_decimal + decimals + 2ul) << "max" << sep << "location" << std::endl;
336 out << print_horizontal(seph) << std::endl;
337
338 std::vector<std::string> prefix_(size());
339 if (indent) {
340 std::vector<bool> active(max_nest, false);
341 for (long kk = long(size()) - 1; kk >= 0; --kk) {
342 long k = order[kk];
343 const auto& nest = nest_[k];
344
345 const CallStack& this_stack = stack_[k];
346 const CallStack* next_stack_ptr;
347 if (kk == size() - 1) {
348 next_stack_ptr = &this_stack;
349 }
350 else {
351 next_stack_ptr = &stack_[order[kk + 1]];
352 }
353 const CallStack& next_stack = *next_stack_ptr;
354
355 auto this_it = this_stack.begin();
356 auto next_it = next_stack.begin();
357 for (size_t i = 0; this_it != this_stack.end() && next_it != next_stack.end(); ++i, ++this_it, ++next_it) {
358 if (*this_it == *next_it) {
359 active[i] = active[i] or false;
360 }
361 else {
362 active[i] = true;
363 }
364 }
365 for (size_t i = nest; i < active.size(); ++i) {
366 active[i] = false;
367 }
368
369 std::stringstream out;
370 for (long i = 0; i < nest - 1; ++i) {
371 if (active[i]) {
372 out << box_vertical;
373 }
374 else {
375 out << " ";
376 }
377 for (long j = 1; j < indent; ++j) {
378 out << " ";
379 }
380 }
381 if (active[nest - 1]) {
382 out << box_T_right;
383 }
384 else {
385 out << box_corner_bl;
386 }
387 for (long j = 1; j < indent; ++j) {
388 out << box_horizontal(1);
389 }
390
391 prefix_[k] = out.str();
392 }
393 }
394
395 for (size_t i = 0; i < size(); ++i) {
396 size_t j = order[i];
397 auto& tot = tot_timings_[j];
398 auto& max = max_timings_[j];
399 auto& min = std::min(max, min_timings_[j]);
400 auto& count = counts_[j];
401 auto& title = titles_[j];
402 auto& loc = locations_[j];
403 auto& nest = nest_[j];
404 auto std = std::sqrt(var_timings_[j]);
405 auto avg = (count == 0 ? 0. : tot / double(count));
406
407 // mpi::comm().allReduceInPlace(min,eckit::mpi::min());
408 // mpi::comm().allReduceInPlace(max,eckit::mpi::max());
409
410 if (not excluded(j)) {
411 out << std::setw(digits(long(size()))) << j << " : " << prefix_[j] // prefix(indent,nest,next_nest)
412 << std::left << std::setw(max_title_length - nest * indent) << title << sep
413 << std::string(header ? "" : "count: ") << std::left << std::setw(max_count_length) << count << sep
414 << std::string(header ? "" : "tot: ") << print_time(tot) << sep << std::string(header ? "" : "avg: ")
415 << print_time(avg) << sep << std::string(header ? "" : "std: ") << print_time(std) << sep
416 << std::string(header ? "" : "min: ") << print_time(min) << sep << std::string(header ? "" : "max: ")
417 << print_time(max) << sep << filter_filepath(loc.file()) << " +" << loc.line() << std::endl;
418 }
419 }
420
421 out << print_horizontal(sepf) << std::endl;
422
423 std::string sepc = box_horizontal(3);
424
425 out << std::left << box_horizontal(40) << sept << box_horizontal(5) << sept << box_horizontal(12) << "\n";
426 out << std::left << std::setw(40) << "Timers accumulated by label" << sep << std::left << std::setw(5) << "count"
427 << sep << "time" << std::endl;
428 out << std::left << box_horizontal(40) << seph << box_horizontal(5) << seph << box_horizontal(12) << "\n";
429 for (auto& label : labels_) {
430 auto name = label.first;
431 auto timers = label.second;
432 double tot(0);
433 double count(0);
434 for (size_t j : timers) {
435 tot += tot_timings_[j];
436 count += counts_[j];
437 }
438 out << std::left << std::setw(40) << name << sep << std::left << std::setw(5) << count << sep << print_time(tot)
439 << std::endl;
440 }
441 out << std::left << box_horizontal(40) << sepf << box_horizontal(5) << sepf << box_horizontal(12) << "\n";
442 }
443
444 std::string TimingsRegistry::filter_filepath(const std::string& filepath) const {
445 std::smatch matches;
446 std::string basename = eckit::PathName(filepath).baseName();
447 if (std::regex_search(filepath, matches, std::regex{"(.*)?/atlas/src/(.*)"})) {
448 return "[atlas] " + basename;
449 }
450 if (std::regex_search(filepath, matches, std::regex{"(.*)?/atlas-io/src/(.*)"})) {
451 return "[atlas-io] " + basename;
452 }
453 return basename;
454 }
455
456 Timings::Identifier Timings::add(const CodeLocation& loc, const CallStack& stack, const std::string& title,
457 const Labels& labels) {
458 return TimingsRegistry::instance().add(loc, stack, title, labels);
459 }
460
461 void Timings::update(const Identifier& id, double seconds) {
462 TimingsRegistry::instance().update(id, seconds);
463 }
464
465 std::string Timings::report() {
466 return report(util::NoConfig());
467 }
468
469 std::string Timings::report(const Configuration& config) {
470 std::ostringstream out;
471 TimingsRegistry::instance().report(out, config);
472 return out.str();
473 }
474
475 } // namespace trace
476 } // namespace runtime
477 } // namespace atlas