Electroneum
Toggle main menu visibility
Loading...
Searching...
No Matches
perf_timer.cpp
Go to the documentation of this file.
1
// Copyright (c) 2017-Present, Electroneum
2
// Copyright (c) 2016-2019, 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
#include <vector>
31
#include "
misc_os_dependent.h
"
32
#include "
perf_timer.h
"
33
34
#undef ELECTRONEUM_DEFAULT_LOG_CATEGORY
35
#define ELECTRONEUM_DEFAULT_LOG_CATEGORY "perf"
36
37
#define PERF_LOG_ALWAYS(level, cat, x) \
38
el::base::Writer(level, __FILE__, __LINE__, ELPP_FUNC, el::base::DispatchAction::FileOnlyLog).construct(cat) << x
39
#define PERF_LOG(level, cat, x) \
40
do { \
41
if (ELPP->vRegistry()->allowed(level, cat)) PERF_LOG_ALWAYS(level, cat, x); \
42
} while(0)
43
44
namespace
tools
45
{
46
uint64_t
get_tick_count
()
47
{
48
#if defined(__x86_64__)
49
uint32_t
hi, lo;
50
__asm__
volatile
(
"rdtsc"
:
"=a"
(lo),
"=d"
(hi));
51
return
(((
uint64_t
)hi) << 32) | (
uint64_t
)lo;
52
#else
53
return
epee::misc_utils::get_ns_count
();
54
#endif
55
}
56
57
#ifdef __x86_64__
58
uint64_t
get_ticks_per_ns
()
59
{
60
uint64_t
t0
=
epee::misc_utils::get_ns_count
(),
t1
;
61
uint64_t
r0 =
get_tick_count
();
62
63
while
(1)
64
{
65
t1
=
epee::misc_utils::get_ns_count
();
66
if
(
t1
-
t0
> 1*1000000000)
break
;
// work one second
67
}
68
69
uint64_t
r1 =
get_tick_count
();
70
uint64_t
tpns256 = 256 * (r1 - r0) / (
t1
-
t0
);
71
return
tpns256 ? tpns256 : 1;
72
}
73
#endif
74
75
#ifdef __x86_64__
76
uint64_t
ticks_per_ns =
get_ticks_per_ns
();
77
#endif
78
79
uint64_t
ticks_to_ns
(
uint64_t
ticks)
80
{
81
#if defined(__x86_64__)
82
return
256 * ticks / ticks_per_ns;
83
#else
84
return
ticks;
85
#endif
86
}
87
}
88
89
namespace
tools
90
{
91
92
el::Level
performance_timer_log_level
=
el::Level::Info
;
93
94
static
__thread std::vector<LoggingPerformanceTimer*> *performance_timers = NULL;
95
96
void
set_performance_timer_log_level
(
el::Level
level)
97
{
98
if
(level !=
el::Level::Debug
&& level !=
el::Level::Trace
&& level !=
el::Level::Info
99
&& level !=
el::Level::Warning
&& level !=
el::Level::Error
&& level !=
el::Level::Fatal
)
100
{
101
MERROR
(
"Wrong log level: "
<<
el::LevelHelper::convertToString
(level) <<
", using Info"
);
102
level =
el::Level::Info
;
103
}
104
performance_timer_log_level
= level;
105
}
106
107
PerformanceTimer::PerformanceTimer
(
bool
paused
):
started
(
true
),
paused
(
paused
)
108
{
109
if
(
paused
)
110
ticks
= 0;
111
else
112
ticks
=
get_tick_count
();
113
}
114
115
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)
116
{
117
const
bool
log =
ELPP
->vRegistry()->allowed(level, cat.c_str());
118
if
(!performance_timers)
119
{
120
if
(log)
121
PERF_LOG_ALWAYS
(level, cat.c_str(),
"PERF ----------"
);
122
performance_timers =
new
std::vector<LoggingPerformanceTimer*>();
123
performance_timers->reserve(16);
// how deep before realloc
124
}
125
else
126
{
127
LoggingPerformanceTimer
*pt = performance_timers->back();
128
if
(!pt->
started
&& !pt->
paused
)
129
{
130
if
(log)
131
{
132
size_t
size = 0;
for
(
const
auto
*tmp: *performance_timers)
if
(!tmp->paused) ++size;
133
PERF_LOG_ALWAYS
(pt->level, cat.c_str(),
"PERF "
<< std::string((size-1) * 2,
' '
) <<
" "
<< pt->name);
134
}
135
pt->
started
=
true
;
136
}
137
}
138
performance_timers->push_back(
this
);
139
}
140
141
PerformanceTimer::~PerformanceTimer
()
142
{
143
if
(!
paused
)
144
ticks
=
get_tick_count
() -
ticks
;
145
}
146
147
LoggingPerformanceTimer::~LoggingPerformanceTimer
()
148
{
149
pause
();
150
performance_timers->pop_back();
151
const
bool
log =
ELPP
->vRegistry()->allowed(level, cat.c_str());
152
if
(log)
153
{
154
char
s[12];
155
snprintf(s,
sizeof
(s),
"%8llu "
, (
unsigned
long
long
)(
ticks_to_ns
(
ticks
) / (1000000000 / unit)));
156
size_t
size = 0;
for
(
const
auto
*tmp: *performance_timers)
if
(!tmp->paused || tmp==
this
) ++size;
157
PERF_LOG_ALWAYS
(level, cat.c_str(),
"PERF "
<< s << std::string(size * 2,
' '
) <<
" "
<< name);
158
}
159
if
(performance_timers->empty())
160
{
161
delete
performance_timers;
162
performance_timers = NULL;
163
}
164
}
165
166
void
PerformanceTimer::pause
()
167
{
168
if
(
paused
)
169
return
;
170
ticks
=
get_tick_count
() -
ticks
;
171
paused
=
true
;
172
}
173
174
void
PerformanceTimer::resume
()
175
{
176
if
(!
paused
)
177
return
;
178
ticks
=
get_tick_count
() -
ticks
;
179
paused
=
false
;
180
}
181
182
void
PerformanceTimer::reset
()
183
{
184
if
(
paused
)
185
ticks
= 0;
186
else
187
ticks
=
get_tick_count
();
188
}
189
190
uint64_t
PerformanceTimer::value
()
const
191
{
192
uint64_t
v =
ticks
;
193
if
(!
paused
)
194
v =
get_tick_count
() - v;
195
return
ticks_to_ns
(v);
196
}
197
198
}
if
else if(0==res)
Definition
abstract_tcp_server_cp.h:265
el::LevelHelper::convertToString
static const char * convertToString(Level level)
Converts level to associated const char*.
tools::LoggingPerformanceTimer::LoggingPerformanceTimer
LoggingPerformanceTimer(const std::string &s, const std::string &cat, uint64_t unit, el::Level l=el::Level::Info)
Definition
perf_timer.cpp:115
tools::LoggingPerformanceTimer::~LoggingPerformanceTimer
~LoggingPerformanceTimer()
Definition
perf_timer.cpp:147
tools::PerformanceTimer::resume
void resume()
Definition
perf_timer.cpp:174
tools::PerformanceTimer::PerformanceTimer
PerformanceTimer(bool paused=false)
Definition
perf_timer.cpp:107
tools::PerformanceTimer::~PerformanceTimer
~PerformanceTimer()
Definition
perf_timer.cpp:141
tools::PerformanceTimer::reset
void reset()
Definition
perf_timer.cpp:182
tools::PerformanceTimer::started
bool started
Definition
perf_timer.h:61
tools::PerformanceTimer::pause
void pause()
Definition
perf_timer.cpp:166
tools::PerformanceTimer::paused
bool paused
Definition
perf_timer.h:62
tools::PerformanceTimer::value
uint64_t value() const
Definition
perf_timer.cpp:190
tools::PerformanceTimer::ticks
uint64_t ticks
Definition
perf_timer.h:60
ELPP
#define ELPP
Definition
easylogging++.h:2774
MERROR
#define MERROR(x)
Definition
misc_log_ex.h:73
misc_os_dependent.h
el::Level
Level
Represents enumeration for severity level used to determine level of logging.
Definition
easylogging++.h:585
el::Level::Warning
@ Warning
Useful when application has potentially harmful situtaions.
Definition
easylogging++.h:597
el::Level::Info
@ Info
Mainly useful to represent current progress of application.
Definition
easylogging++.h:601
el::Level::Fatal
@ Fatal
Severe error information that will presumably abort application.
Definition
easylogging++.h:593
el::Level::Error
@ Error
Information representing errors in application but application will keep running.
Definition
easylogging++.h:595
el::Level::Debug
@ Debug
Informational events most useful for developers to debug application.
Definition
easylogging++.h:591
el::Level::Trace
@ Trace
Information that can be useful to back-trace certain events - mostly useful than debug logs.
Definition
easylogging++.h:589
epee::misc_utils::get_ns_count
uint64_t get_ns_count()
Definition
misc_os_dependent.h:58
tools
Various Tools.
Definition
tools.cpp:31
tools::ticks_to_ns
uint64_t ticks_to_ns(uint64_t ticks)
Definition
perf_timer.cpp:79
tools::set_performance_timer_log_level
void set_performance_timer_log_level(el::Level level)
Definition
perf_timer.cpp:96
tools::get_tick_count
uint64_t get_tick_count()
Definition
perf_timer.cpp:46
tools::performance_timer_log_level
el::Level performance_timer_log_level
Definition
perf_timer.cpp:92
tools::get_ticks_per_ns
uint64_t get_ticks_per_ns()
PERF_LOG_ALWAYS
#define PERF_LOG_ALWAYS(level, cat, x)
Definition
perf_timer.cpp:37
perf_timer.h
t1
t1
Definition
pow22523.h:58
t0
t0
Definition
pow22523.h:53
true
#define true
uint32_t
unsigned int uint32_t
Definition
stdint.h:126
uint64_t
unsigned __int64 uint64_t
Definition
stdint.h:136
src
common
perf_timer.cpp
Generated on
for Electroneum by
1.17.0