Electroneum
perf_timer.h
Go to the documentation of this file.
1 // Copyright (c) 2017-2019, The Electroneum Project
2 // Copyright (c) 2016, The Monero Project
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without modification, are
7 // permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this list of
10 // conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 // of conditions and the following disclaimer in the documentation and/or other
14 // materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its contributors may be
17 // used to endorse or promote products derived from this software without specific
18 // prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 #pragma once
31 
32 #include <string>
33 #include <stdio.h>
34 #include "misc_log_ex.h"
35 
36 #undef ELECTRONEUM_DEFAULT_LOG_CATEGORY
37 #define ELECTRONEUM_DEFAULT_LOG_CATEGORY "perf"
38 
39 namespace tools
40 {
41 
42 class PerformanceTimer;
43 
44 extern el::Level performance_timer_log_level;
45 extern __thread std::vector<PerformanceTimer*> *performance_timers;
46 
48 {
49 public:
50  PerformanceTimer(const std::string &s, uint64_t unit, el::Level l = el::Level::Debug): name(s), unit(unit), level(l), started(false)
51  {
52  ticks = epee::misc_utils::get_ns_count();
53  if (!performance_timers)
54  {
55  MLOG(level, "PERF ----------");
56  performance_timers = new std::vector<PerformanceTimer*>();
57  }
58  else
59  {
61  if (!pt->started)
62  {
63  MLOG(pt->level, "PERF " << std::string((performance_timers->size()-1) * 2, ' ') << " " << pt->name);
64  pt->started = true;
65  }
66  }
67  performance_timers->push_back(this);
68  }
69 
71  {
72  performance_timers->pop_back();
73  ticks = epee::misc_utils::get_ns_count() - ticks;
74  char s[12];
75  snprintf(s, sizeof(s), "%8llu ", (unsigned long long)ticks / (1000000000 / unit));
76  MLOG(level, "PERF " << s << std::string(performance_timers->size() * 2, ' ') << " " << name);
77  if (performance_timers->empty())
78  {
79  delete performance_timers;
80  performance_timers = NULL;
81  }
82  }
83 
84 private:
85  std::string name;
86  uint64_t unit;
87  el::Level level;
88  uint64_t ticks;
89  bool started;
90 };
91 
92 void set_performance_timer_log_level(el::Level level);
93 
94 #define PERF_TIMER_UNIT(name, unit) tools::PerformanceTimer pt_##name(#name, unit, tools::performance_timer_log_level)
95 #define PERF_TIMER_UNIT_L(name, unit, l) tools::PerformanceTimer pt_##name(#name, unit, l)
96 #define PERF_TIMER(name) PERF_TIMER_UNIT(name, 1000)
97 #define PERF_TIMER_L(name, l) PERF_TIMER_UNIT_L(name, 1000, l)
98 
99 }
PerformanceTimer(const std::string &s, uint64_t unit, el::Level l=el::Level::Debug)
Definition: perf_timer.h:50
void set_performance_timer_log_level(el::Level level)
Definition: perf_timer.cpp:41
std::string name
Definition: perf_timer.h:85
~PerformanceTimer()
Definition: perf_timer.h:70
Various Tools.
Definition: base58.cpp:43
uint64_t ticks
Definition: perf_timer.h:88
Definition: perf_timer.h:47
uint64_t unit
Definition: perf_timer.h:86
#define false
Definition: stdbool.h:38
int l
Definition: base.py:3
__thread std::vector< PerformanceTimer * > * performance_timers
Definition: perf_timer.cpp:39
bool started
Definition: perf_timer.h:89
el::Level performance_timer_log_level
Definition: perf_timer.cpp:38
el::Level level
Definition: perf_timer.h:87
#define s(x, c)
Definition: aesb.c:46