Debian Codegraph @

monero/src/common/perf_timer.h

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 #pragma once
30
31 #include <string>
32 #include <stdio.h>
33 #include <memory>
34 #include "misc_log_ex.h"
35
36 namespace tools
37 {
38
39 class PerformanceTimer;
40
41 extern el::Level performance_timer_log_level;
42
43 uint64_t get_tick_count();
44 uint64_t get_ticks_per_ns();
45 uint64_t ticks_to_ns(uint64_t ticks);
46
47 class PerformanceTimer
48 {
49 public:
50 PerformanceTimer(bool paused = false);
51 ~PerformanceTimer();
52 void pause();
53 void resume();
54 void reset();
55 uint64_t value() const;
56 operator uint64_t() const { return value(); }
57
58 protected:
59 uint64_t ticks;
60 bool started;
61 bool paused;
62 };
63
64 class LoggingPerformanceTimer: public PerformanceTimer
65 {
66 public:
67 LoggingPerformanceTimer(const std::string &s, const std::string &cat, uint64_t unit, el::Level l = el::Level::Info);
68 ~LoggingPerformanceTimer();
69
70 private:
71 std::string name;
72 std::string cat;
73 uint64_t unit;
74 el::Level level;
75 };
76
77 void set_performance_timer_log_level(el::Level level);
78
79 #define PERF_TIMER_NAME(name) pt_##name
80 #define PERF_TIMER_UNIT(name, unit) tools::LoggingPerformanceTimer PERF_TIMER_NAME(name)(#name, "perf." MONERO_DEFAULT_LOG_CATEGORY, unit, tools::performance_timer_log_level)
81 #define PERF_TIMER_UNIT_L(name, unit, l) tools::LoggingPerformanceTimer PERF_TIMER_NAME(name)t_##name(#name, "perf." MONERO_DEFAULT_LOG_CATEGORY, unit, l)
82 #define PERF_TIMER(name) PERF_TIMER_UNIT(name, 1000000)
83 #define PERF_TIMER_L(name, l) PERF_TIMER_UNIT_L(name, 1000000, l)
84 #define PERF_TIMER_START_UNIT(name, unit) std::unique_ptr<tools::LoggingPerformanceTimer> PERF_TIMER_NAME(name)(new tools::LoggingPerformanceTimer(#name, "perf." MONERO_DEFAULT_LOG_CATEGORY, unit, el::Level::Info))
85 #define PERF_TIMER_START(name) PERF_TIMER_START_UNIT(name, 1000000)
86 #define PERF_TIMER_STOP(name) do { PERF_TIMER_NAME(name).reset(NULL); } while(0)
87 #define PERF_TIMER_PAUSE(name) PERF_TIMER_NAME(name).pause()
88 #define PERF_TIMER_RESUME(name) PERF_TIMER_NAME(name).resume()
89
90 }