Debian Codegraph @

monero/src/common/perf_timer.cpp

raw symbols SCIP
1 // Copyright (c) 2016-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #include <vector>
30 #include "time_helper.h"
31 #include "perf_timer.h"
32
33 #undef MONERO_DEFAULT_LOG_CATEGORY
34 #define MONERO_DEFAULT_LOG_CATEGORY "perf"
35
36 #define PERF_LOG_ALWAYS(level, cat, x) \
37 el::base::Writer(level, el::Color::Default, __FILE__, __LINE__, ELPP_FUNC, el::base::DispatchAction::FileOnlyLog).construct(cat) << x
38 #define PERF_LOG(level, cat, x) \
39 do { \
40 if (ELPP->vRegistry()->allowed(level, cat)) PERF_LOG_ALWAYS(level, cat, x); \
41 } while(0)
42
43 namespace tools
44 {
45 uint64_t get_tick_count()
46 {
47 return epee::misc_utils::get_ns_count();
48 }
49
50 uint64_t ticks_to_ns(uint64_t ticks)
51 {
52 return ticks;
53 }
54 }
55
56 namespace tools
57 {
58
59 el::Level performance_timer_log_level = el::Level::Info;
60
61 static __thread std::vector<LoggingPerformanceTimer*> *performance_timers = NULL;
62
63 void set_performance_timer_log_level(el::Level level)
64 {
65 if (level != el::Level::Debug && level != el::Level::Trace && level != el::Level::Info
66 && level != el::Level::Warning && level != el::Level::Error && level != el::Level::Fatal)
67 {
68 MERROR("Wrong log level: " << el::LevelHelper::convertToString(level) << ", using Info");
69 level = el::Level::Info;
70 }
71 performance_timer_log_level = level;
72 }
73
74 PerformanceTimer::PerformanceTimer(bool paused): started(true), paused(paused)
75 {
76 if (paused)
77 ticks = 0;
78 else
79 ticks = get_tick_count();
80 }
81
82 LoggingPerformanceTimer::LoggingPerformanceTimer(const std::string &s, const std::string &cat, uint64_t unit, el::Level l): PerformanceTimer(), name(s), cat(cat), unit(unit), level(l)
83 {
84 const bool log = ELPP->vRegistry()->allowed(level, cat.c_str());
85 if (!performance_timers)
86 {
87 if (log)
88 PERF_LOG_ALWAYS(level, cat.c_str(), "PERF ----------");
89 performance_timers = new std::vector<LoggingPerformanceTimer*>();
90 performance_timers->reserve(16); // how deep before realloc
91 }
92 else
93 {
94 LoggingPerformanceTimer *pt = performance_timers->back();
95 if (!pt->started && !pt->paused)
96 {
97 if (log)
98 {
99 size_t size = 0; for (const auto *tmp: *performance_timers) if (!tmp->paused) ++size;
100 PERF_LOG_ALWAYS(pt->level, cat.c_str(), "PERF " << std::string((size-1) * 2, ' ') << " " << pt->name);
101 }
102 pt->started = true;
103 }
104 }
105 performance_timers->push_back(this);
106 }
107
108 PerformanceTimer::~PerformanceTimer()
109 {
110 if (!paused)
111 ticks = get_tick_count() - ticks;
112 }
113
114 LoggingPerformanceTimer::~LoggingPerformanceTimer()
115 {
116 pause();
117 performance_timers->pop_back();
118 const bool log = ELPP->vRegistry()->allowed(level, cat.c_str());
119 if (log)
120 {
121 char s[12];
122 snprintf(s, sizeof(s), "%8llu ", (unsigned long long)(ticks_to_ns(ticks) / (1000000000 / unit)));
123 size_t size = 0; for (const auto *tmp: *performance_timers) if (!tmp->paused || tmp==this) ++size;
124 PERF_LOG_ALWAYS(level, cat.c_str(), "PERF " << s << std::string(size * 2, ' ') << " " << name);
125 }
126 if (performance_timers->empty())
127 {
128 delete performance_timers;
129 performance_timers = NULL;
130 }
131 }
132
133 void PerformanceTimer::pause()
134 {
135 if (paused)
136 return;
137 ticks = get_tick_count() - ticks;
138 paused = true;
139 }
140
141 void PerformanceTimer::resume()
142 {
143 if (!paused)
144 return;
145 ticks = get_tick_count() - ticks;
146 paused = false;
147 }
148
149 void PerformanceTimer::reset()
150 {
151 if (paused)
152 ticks = 0;
153 else
154 ticks = get_tick_count();
155 }
156
157 uint64_t PerformanceTimer::value() const
158 {
159 uint64_t v = ticks;
160 if (!paused)
161 v = get_tick_count() - v;
162 return ticks_to_ns(v);
163 }
164
165 }