Debian Codegraph @

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 #if defined(__x86_64__)
48 uint32_t hi, lo;
49 __asm__ volatile("rdtsc" : "=a"(lo), "=d"(hi));
50 return (((uint64_t)hi) << 32) | (uint64_t)lo;
51 #else
52 return epee::misc_utils::get_ns_count();
53 #endif
54 }
55
56 #ifdef __x86_64__
57 uint64_t get_ticks_per_ns()
58 {
59 uint64_t t0 = epee::misc_utils::get_ns_count(), t1;
60 uint64_t r0 = get_tick_count();
61
62 while (1)
63 {
64 t1 = epee::misc_utils::get_ns_count();
65 if (t1 - t0 > 1*100000000) break; // work 0.1 seconds
66 }
67
68 uint64_t r1 = get_tick_count();
69 uint64_t tpns256 = 256 * (r1 - r0) / (t1 - t0);
70 return tpns256 ? tpns256 : 1;
71 }
72 #endif
73
74 #ifdef __x86_64__
75 uint64_t ticks_per_ns = get_ticks_per_ns();
76 #endif
77
78 uint64_t ticks_to_ns(uint64_t ticks)
79 {
80 #if defined(__x86_64__)
81 return 256 * ticks / ticks_per_ns;
82 #else
83 return ticks;
84 #endif
85 }
86 }
87
88 namespace tools
89 {
90
91 el::Level performance_timer_log_level = el::Level::Info;
92
93 static __thread std::vector<LoggingPerformanceTimer*> *performance_timers = NULL;
94
95 void set_performance_timer_log_level(el::Level level)
96 {
97 if (level != el::Level::Debug && level != el::Level::Trace && level != el::Level::Info
98 && level != el::Level::Warning && level != el::Level::Error && level != el::Level::Fatal)
99 {
100 MERROR("Wrong log level: " << el::LevelHelper::convertToString(level) << ", using Info");
101 level = el::Level::Info;
102 }
103 performance_timer_log_level = level;
104 }
105
106 PerformanceTimer::PerformanceTimer(bool paused): started(true), paused(paused)
107 {
108 if (paused)
109 ticks = 0;
110 else
111 ticks = get_tick_count();
112 }
113
114 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)
115 {
116 const bool log = ELPP->vRegistry()->allowed(level, cat.c_str());
117 if (!performance_timers)
118 {
119 if (log)
120 PERF_LOG_ALWAYS(level, cat.c_str(), "PERF ----------");
121 performance_timers = new std::vector<LoggingPerformanceTimer*>();
122 performance_timers->reserve(16); // how deep before realloc
123 }
124 else
125 {
126 LoggingPerformanceTimer *pt = performance_timers->back();
127 if (!pt->started && !pt->paused)
128 {
129 if (log)
130 {
131 size_t size = 0; for (const auto *tmp: *performance_timers) if (!tmp->paused) ++size;
132 PERF_LOG_ALWAYS(pt->level, cat.c_str(), "PERF " << std::string((size-1) * 2, ' ') << " " << pt->name);
133 }
134 pt->started = true;
135 }
136 }
137 performance_timers->push_back(this);
138 }
139
140 PerformanceTimer::~PerformanceTimer()
141 {
142 if (!paused)
143 ticks = get_tick_count() - ticks;
144 }
145
146 LoggingPerformanceTimer::~LoggingPerformanceTimer()
147 {
148 pause();
149 performance_timers->pop_back();
150 const bool log = ELPP->vRegistry()->allowed(level, cat.c_str());
151 if (log)
152 {
153 char s[12];
154 snprintf(s, sizeof(s), "%8llu ", (unsigned long long)(ticks_to_ns(ticks) / (1000000000 / unit)));
155 size_t size = 0; for (const auto *tmp: *performance_timers) if (!tmp->paused || tmp==this) ++size;
156 PERF_LOG_ALWAYS(level, cat.c_str(), "PERF " << s << std::string(size * 2, ' ') << " " << name);
157 }
158 if (performance_timers->empty())
159 {
160 delete performance_timers;
161 performance_timers = NULL;
162 }
163 }
164
165 void PerformanceTimer::pause()
166 {
167 if (paused)
168 return;
169 ticks = get_tick_count() - ticks;
170 paused = true;
171 }
172
173 void PerformanceTimer::resume()
174 {
175 if (!paused)
176 return;
177 ticks = get_tick_count() - ticks;
178 paused = false;
179 }
180
181 void PerformanceTimer::reset()
182 {
183 if (paused)
184 ticks = 0;
185 else
186 ticks = get_tick_count();
187 }
188
189 uint64_t PerformanceTimer::value() const
190 {
191 uint64_t v = ticks;
192 if (!paused)
193 v = get_tick_count() - v;
194 return ticks_to_ns(v);
195 }
196
197 }