Debian Codegraph @

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