Botan 2.19.1
Crypto and TLS for C&
Classes | Public Member Functions | List of all members
Botan::Timer Class Referencefinal

#include <timer.h>

Classes

class  Timer_Scope
 

Public Member Functions

size_t buf_size () const
 
double bytes_per_second () const
 
uint64_t cycles_consumed () const
 
const std::string & doing () const
 
uint64_t events () const
 
double events_per_second () const
 
const std::string & get_name () const
 
double milliseconds () const
 
double ms_per_event () const
 
bool operator< (const Timer &other) const
 
Timeroperator= (const Timer &other)=default
 
template<typename F >
auto run (F f) -> decltype(f())
 
template<typename F >
void run_until_elapsed (std::chrono::milliseconds msec, F f)
 
double seconds () const
 
double seconds_per_event () const
 
void set_custom_msg (const std::string &s)
 
void start ()
 
void stop ()
 
 Timer (const std::string &name)
 
 Timer (const std::string &name, const std::string &provider, const std::string &doing, uint64_t event_mult, size_t buf_size, double clock_cycle_ratio, uint64_t clock_speed)
 
 Timer (const std::string &name, size_t buf_size)
 
 Timer (const Timer &other)=default
 
std::string to_string () const
 
bool under (std::chrono::milliseconds msec)
 
uint64_t value () const
 

Detailed Description

Definition at line 16 of file timer.h.

Constructor & Destructor Documentation

◆ Timer() [1/4]

Botan::Timer::Timer ( const std::string &  name,
const std::string &  provider,
const std::string &  doing,
uint64_t  event_mult,
size_t  buf_size,
double  clock_cycle_ratio,
uint64_t  clock_speed 
)
inline

Definition at line 19 of file timer.h.

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 {}
size_t buf_size() const
Definition: timer.h:134
const std::string & doing() const
Definition: timer.h:129
std::string name

◆ Timer() [2/4]

Botan::Timer::Timer ( const std::string &  name)
inline

Definition at line 34 of file timer.h.

34 :
35 Timer(name, "", "", 1, 0, 0.0, 0)
36 {}
Timer(const std::string &name, const std::string &provider, const std::string &doing, uint64_t event_mult, size_t buf_size, double clock_cycle_ratio, uint64_t clock_speed)
Definition: timer.h:19

◆ Timer() [3/4]

Botan::Timer::Timer ( const std::string &  name,
size_t  buf_size 
)
inline

Definition at line 38 of file timer.h.

38 :
39 Timer(name, "", "", buf_size, buf_size, 0.0, 0)
40 {}

◆ Timer() [4/4]

Botan::Timer::Timer ( const Timer other)
default

Member Function Documentation

◆ buf_size()

size_t Botan::Timer::buf_size ( ) const
inline

Definition at line 134 of file timer.h.

135 {
136 return m_buf_size;
137 }

Referenced by to_string().

◆ bytes_per_second()

double Botan::Timer::bytes_per_second ( ) const
inline

Definition at line 139 of file timer.h.

140 {
141 return seconds() > 0.0 ? events() / seconds() : 0.0;
142 }
uint64_t events() const
Definition: timer.h:119
double seconds() const
Definition: timer.h:95

Referenced by Botan::RFC4880_S2K_Family::tune().

◆ cycles_consumed()

uint64_t Botan::Timer::cycles_consumed ( ) const
inline

Definition at line 110 of file timer.h.

111 {
112 if(m_clock_speed != 0)
113 {
114 return static_cast<uint64_t>((m_clock_speed * value()) / 1000.0);
115 }
116 return m_cpu_cycles_used;
117 }
uint64_t value() const
Definition: timer.h:90

◆ doing()

const std::string & Botan::Timer::doing ( ) const
inline

Definition at line 129 of file timer.h.

130 {
131 return m_doing;
132 }

Referenced by operator<().

◆ events()

uint64_t Botan::Timer::events ( ) const
inline

Definition at line 119 of file timer.h.

120 {
121 return m_event_count * m_event_mult;
122 }

Referenced by Botan::Argon2_Family::tune(), Botan::Bcrypt_PBKDF_Family::tune(), and Botan::Scrypt_Family::tune().

◆ events_per_second()

double Botan::Timer::events_per_second ( ) const
inline

Definition at line 144 of file timer.h.

145 {
146 return seconds() > 0.0 ? events() / seconds() : 0.0;
147 }

◆ get_name()

const std::string & Botan::Timer::get_name ( ) const
inline

Definition at line 124 of file timer.h.

125 {
126 return m_name;
127 }

Referenced by operator<().

◆ milliseconds()

double Botan::Timer::milliseconds ( ) const
inline

Definition at line 100 of file timer.h.

101 {
102 return value() / 1000000.0;
103 }

◆ ms_per_event()

double Botan::Timer::ms_per_event ( ) const
inline

Definition at line 105 of file timer.h.

106 {
107 return milliseconds() / events();
108 }
double milliseconds() const
Definition: timer.h:100

◆ operator<()

bool Botan::Timer::operator< ( const Timer other) const

Definition at line 59 of file timer.cpp.

60 {
61 if(this->doing() != other.doing())
62 return (this->doing() < other.doing());
63
64 return (this->get_name() < other.get_name());
65 }
const std::string & get_name() const
Definition: timer.h:124

References doing(), and get_name().

◆ operator=()

Timer & Botan::Timer::operator= ( const Timer other)
default

◆ run()

template<typename F >
auto Botan::Timer::run ( f) -> decltype(f())
inline

Definition at line 75 of file timer.h.

76 {
77 Timer_Scope timer(*this);
78 return f();
79 }

◆ run_until_elapsed()

template<typename F >
void Botan::Timer::run_until_elapsed ( std::chrono::milliseconds  msec,
f 
)
inline

Definition at line 82 of file timer.h.

83 {
84 while(this->under(msec))
85 {
86 run(f);
87 }
88 }
bool under(std::chrono::milliseconds msec)
Definition: timer.h:49
auto run(F f) -> decltype(f())
Definition: timer.h:75

Referenced by Botan::RFC4880_S2K_Family::tune(), Botan::Argon2_Family::tune(), Botan::Bcrypt_PBKDF_Family::tune(), and Botan::Scrypt_Family::tune().

◆ seconds()

double Botan::Timer::seconds ( ) const
inline

Definition at line 95 of file timer.h.

96 {
97 return milliseconds() / 1000.0;
98 }

◆ seconds_per_event()

double Botan::Timer::seconds_per_event ( ) const
inline

Definition at line 149 of file timer.h.

150 {
151 return events() > 0 ? seconds() / events() : 0.0;
152 }

◆ set_custom_msg()

void Botan::Timer::set_custom_msg ( const std::string &  s)
inline

Definition at line 154 of file timer.h.

155 {
156 m_custom_msg = s;
157 }

◆ start()

void Botan::Timer::start ( )

Definition at line 15 of file timer.cpp.

16 {
17 stop();
18 m_timer_start = OS::get_system_timestamp_ns();
19 m_cpu_cycles_start = OS::get_cpu_cycle_counter();
20 }
void stop()
Definition: timer.cpp:22
uint64_t BOTAN_TEST_API get_system_timestamp_ns()
Definition: os_utils.cpp:293
uint64_t BOTAN_TEST_API get_cpu_cycle_counter()
Definition: os_utils.cpp:154

References Botan::OS::get_cpu_cycle_counter(), Botan::OS::get_system_timestamp_ns(), and stop().

◆ stop()

void Botan::Timer::stop ( )

Definition at line 22 of file timer.cpp.

23 {
24 if(m_timer_start)
25 {
26 if(m_cpu_cycles_start != 0)
27 {
28 const uint64_t cycles_taken = OS::get_cpu_cycle_counter() - m_cpu_cycles_start;
29 if(cycles_taken > 0)
30 {
31 m_cpu_cycles_used += static_cast<size_t>(cycles_taken * m_clock_cycle_ratio);
32 }
33 }
34
35 const uint64_t now = OS::get_system_timestamp_ns();
36
37 if(now > m_timer_start)
38 {
39 const uint64_t dur = now - m_timer_start;
40
41 m_time_used += dur;
42
43 if(m_event_count == 0)
44 {
45 m_min_time = m_max_time = dur;
46 }
47 else
48 {
49 m_max_time = std::max(m_max_time, dur);
50 m_min_time = std::min(m_min_time, dur);
51 }
52 }
53
54 m_timer_start = 0;
55 ++m_event_count;
56 }
57 }

References Botan::OS::get_cpu_cycle_counter(), and Botan::OS::get_system_timestamp_ns().

Referenced by start().

◆ to_string()

std::string Botan::Timer::to_string ( ) const

Definition at line 67 of file timer.cpp.

68 {
69 if(m_custom_msg.size() > 0)
70 {
71 return m_custom_msg;
72 }
73 else if(this->buf_size() == 0)
74 {
75 return result_string_ops();
76 }
77 else
78 {
79 return result_string_bps();
80 }
81 }

References buf_size().

◆ under()

bool Botan::Timer::under ( std::chrono::milliseconds  msec)
inline

Definition at line 49 of file timer.h.

50 {
51 return (milliseconds() < msec.count());
52 }

◆ value()

uint64_t Botan::Timer::value ( ) const
inline

Definition at line 90 of file timer.h.

91 {
92 return m_time_used;
93 }

Referenced by Botan::Argon2_Family::tune(), Botan::Bcrypt_PBKDF_Family::tune(), and Botan::Scrypt_Family::tune().


The documentation for this class was generated from the following files: