/src/botan/build/include/botan/internal/timer.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * (C) 2018 Jack Lloyd |
3 | | * |
4 | | * Botan is released under the Simplified BSD License (see license.txt) |
5 | | */ |
6 | | |
7 | | #ifndef BOTAN_TIMER_H_ |
8 | | #define BOTAN_TIMER_H_ |
9 | | |
10 | | #include <botan/types.h> |
11 | | #include <string> |
12 | | #include <chrono> |
13 | | |
14 | | namespace Botan { |
15 | | |
16 | | class BOTAN_TEST_API Timer final |
17 | | { |
18 | | public: |
19 | | Timer(const std::string& name, |
20 | | const std::string& provider, |
21 | | const std::string& doing, |
22 | | uint64_t event_mult, |
23 | | size_t buf_size, |
24 | | double clock_cycle_ratio, |
25 | | uint64_t clock_speed) |
26 | | : m_name(name + ((provider.empty() || provider == "base") ? "" : " [" + provider + "]")) |
27 | | , m_doing(doing) |
28 | | , m_buf_size(buf_size) |
29 | | , m_event_mult(event_mult) |
30 | | , m_clock_cycle_ratio(clock_cycle_ratio) |
31 | | , m_clock_speed(clock_speed) |
32 | 0 | {} |
33 | | |
34 | | Timer(const std::string& name) : |
35 | | Timer(name, "", "", 1, 0, 0.0, 0) |
36 | 0 | {} |
37 | | |
38 | | Timer(const std::string& name, size_t buf_size) : |
39 | | Timer(name, "", "", buf_size, buf_size, 0.0, 0) |
40 | 0 | {} |
41 | | |
42 | | Timer(const Timer& other) = default; |
43 | | Timer& operator=(const Timer& other) = default; |
44 | | |
45 | | void start(); |
46 | | |
47 | | void stop(); |
48 | | |
49 | | bool under(std::chrono::milliseconds msec) |
50 | 0 | { |
51 | 0 | return (milliseconds() < msec.count()); |
52 | 0 | } |
53 | | |
54 | | class Timer_Scope final |
55 | | { |
56 | | public: |
57 | | explicit Timer_Scope(Timer& timer) |
58 | | : m_timer(timer) |
59 | 0 | { |
60 | 0 | m_timer.start(); |
61 | 0 | } |
62 | | ~Timer_Scope() |
63 | 0 | { |
64 | 0 | try |
65 | 0 | { |
66 | 0 | m_timer.stop(); |
67 | 0 | } |
68 | 0 | catch(...) {} |
69 | 0 | } |
70 | | private: |
71 | | Timer& m_timer; |
72 | | }; |
73 | | |
74 | | template<typename F> |
75 | | auto run(F f) -> decltype(f()) |
76 | 0 | { |
77 | 0 | Timer_Scope timer(*this); |
78 | 0 | return f(); |
79 | 0 | } Unexecuted instantiation: pbkdf2.cpp:decltype ({parm#1}()) Botan::Timer::run<Botan::(anonymous namespace)::tune_pbkdf2(Botan::MessageAuthenticationCode&, unsigned long, unsigned int)::$_0>(Botan::(anonymous namespace)::tune_pbkdf2(Botan::MessageAuthenticationCode&, unsigned long, unsigned int)::$_0) Unexecuted instantiation: pgp_s2k.cpp:decltype ({parm#1}()) Botan::Timer::run<Botan::RFC4880_S2K_Family::tune(unsigned long, std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000l> >, unsigned long) const::$_0>(Botan::RFC4880_S2K_Family::tune(unsigned long, std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000l> >, unsigned long) const::$_0) Unexecuted instantiation: scrypt.cpp:decltype ({parm#1}()) Botan::Timer::run<Botan::Scrypt_Family::tune(unsigned long, std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000l> >, unsigned long) const::$_0>(Botan::Scrypt_Family::tune(unsigned long, std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000l> >, unsigned long) const::$_0) Unexecuted instantiation: argon2pwhash.cpp:decltype ({parm#1}()) Botan::Timer::run<Botan::Argon2_Family::tune(unsigned long, std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000l> >, unsigned long) const::$_0>(Botan::Argon2_Family::tune(unsigned long, std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000l> >, unsigned long) const::$_0) Unexecuted instantiation: bcrypt_pbkdf.cpp:decltype ({parm#1}()) Botan::Timer::run<Botan::Bcrypt_PBKDF_Family::tune(unsigned long, std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000l> >, unsigned long) const::$_0>(Botan::Bcrypt_PBKDF_Family::tune(unsigned long, std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000l> >, unsigned long) const::$_0) |
80 | | |
81 | | template<typename F> |
82 | | void run_until_elapsed(std::chrono::milliseconds msec, F f) |
83 | 0 | { |
84 | 0 | while(this->under(msec)) |
85 | 0 | { |
86 | 0 | run(f); |
87 | 0 | } |
88 | 0 | } Unexecuted instantiation: pbkdf2.cpp:void Botan::Timer::run_until_elapsed<Botan::(anonymous namespace)::tune_pbkdf2(Botan::MessageAuthenticationCode&, unsigned long, unsigned int)::$_0>(std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000l> >, Botan::(anonymous namespace)::tune_pbkdf2(Botan::MessageAuthenticationCode&, unsigned long, unsigned int)::$_0) Unexecuted instantiation: pgp_s2k.cpp:void Botan::Timer::run_until_elapsed<Botan::RFC4880_S2K_Family::tune(unsigned long, std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000l> >, unsigned long) const::$_0>(std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000l> >, Botan::RFC4880_S2K_Family::tune(unsigned long, std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000l> >, unsigned long) const::$_0) Unexecuted instantiation: scrypt.cpp:void Botan::Timer::run_until_elapsed<Botan::Scrypt_Family::tune(unsigned long, std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000l> >, unsigned long) const::$_0>(std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000l> >, Botan::Scrypt_Family::tune(unsigned long, std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000l> >, unsigned long) const::$_0) Unexecuted instantiation: argon2pwhash.cpp:void Botan::Timer::run_until_elapsed<Botan::Argon2_Family::tune(unsigned long, std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000l> >, unsigned long) const::$_0>(std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000l> >, Botan::Argon2_Family::tune(unsigned long, std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000l> >, unsigned long) const::$_0) Unexecuted instantiation: bcrypt_pbkdf.cpp:void Botan::Timer::run_until_elapsed<Botan::Bcrypt_PBKDF_Family::tune(unsigned long, std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000l> >, unsigned long) const::$_0>(std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000l> >, Botan::Bcrypt_PBKDF_Family::tune(unsigned long, std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000l> >, unsigned long) const::$_0) |
89 | | |
90 | | uint64_t value() const |
91 | 0 | { |
92 | 0 | return m_time_used; |
93 | 0 | } |
94 | | |
95 | | double seconds() const |
96 | 0 | { |
97 | 0 | return milliseconds() / 1000.0; |
98 | 0 | } |
99 | | |
100 | | double milliseconds() const |
101 | 0 | { |
102 | 0 | return value() / 1000000.0; |
103 | 0 | } |
104 | | |
105 | | double ms_per_event() const |
106 | 0 | { |
107 | 0 | return milliseconds() / events(); |
108 | 0 | } |
109 | | |
110 | | uint64_t cycles_consumed() const |
111 | 0 | { |
112 | 0 | if(m_clock_speed != 0) |
113 | 0 | { |
114 | 0 | return static_cast<uint64_t>((m_clock_speed * value()) / 1000.0); |
115 | 0 | } |
116 | 0 | return m_cpu_cycles_used; |
117 | 0 | } |
118 | | |
119 | | uint64_t events() const |
120 | 0 | { |
121 | 0 | return m_event_count * m_event_mult; |
122 | 0 | } |
123 | | |
124 | | const std::string& get_name() const |
125 | 0 | { |
126 | 0 | return m_name; |
127 | 0 | } |
128 | | |
129 | | const std::string& doing() const |
130 | 0 | { |
131 | 0 | return m_doing; |
132 | 0 | } |
133 | | |
134 | | size_t buf_size() const |
135 | 0 | { |
136 | 0 | return m_buf_size; |
137 | 0 | } |
138 | | |
139 | | double bytes_per_second() const |
140 | 0 | { |
141 | 0 | return seconds() > 0.0 ? events() / seconds() : 0.0; |
142 | 0 | } |
143 | | |
144 | | double events_per_second() const |
145 | 0 | { |
146 | 0 | return seconds() > 0.0 ? events() / seconds() : 0.0; |
147 | 0 | } |
148 | | |
149 | | double seconds_per_event() const |
150 | 0 | { |
151 | 0 | return events() > 0 ? seconds() / events() : 0.0; |
152 | 0 | } |
153 | | |
154 | | void set_custom_msg(const std::string& s) |
155 | 0 | { |
156 | 0 | m_custom_msg = s; |
157 | 0 | } |
158 | | |
159 | | bool operator<(const Timer& other) const; |
160 | | |
161 | | std::string to_string() const; |
162 | | |
163 | | private: |
164 | | std::string result_string_bps() const; |
165 | | std::string result_string_ops() const; |
166 | | |
167 | | // const data |
168 | | std::string m_name, m_doing; |
169 | | size_t m_buf_size; |
170 | | uint64_t m_event_mult; |
171 | | double m_clock_cycle_ratio; |
172 | | uint64_t m_clock_speed; |
173 | | |
174 | | // set at runtime |
175 | | std::string m_custom_msg; |
176 | | uint64_t m_time_used = 0, m_timer_start = 0; |
177 | | uint64_t m_event_count = 0; |
178 | | |
179 | | uint64_t m_max_time = 0, m_min_time = 0; |
180 | | uint64_t m_cpu_cycles_start = 0, m_cpu_cycles_used = 0; |
181 | | }; |
182 | | |
183 | | } |
184 | | |
185 | | #endif |