Botan  2.11.0
Crypto and TLS for C++11
timer.cpp
Go to the documentation of this file.
1 /*
2 * (C) 2018 Jack Lloyd
3 *
4 * Botan is released under the Simplified BSD License (see license.txt)
5 */
6 
7 #include <botan/internal/timer.h>
8 #include <botan/internal/os_utils.h>
9 #include <algorithm>
10 #include <sstream>
11 #include <iomanip>
12 
13 namespace Botan {
14 
16  {
17  stop();
18  m_timer_start = OS::get_system_timestamp_ns();
19  m_cpu_cycles_start = OS::get_cpu_cycle_counter();
20  }
21 
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  }
58 
59 bool Timer::operator<(const Timer& other) const
60  {
61  if(this->doing() != other.doing())
62  return (this->doing() < other.doing());
63 
64  return (this->get_name() < other.get_name());
65  }
66 
67 std::string Timer::to_string() const
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  }
82 
83 std::string Timer::result_string_bps() const
84  {
85  const size_t MiB = 1024 * 1024;
86 
87  const double MiB_total = static_cast<double>(events()) / MiB;
88  const double MiB_per_sec = MiB_total / seconds();
89 
90  std::ostringstream oss;
91  oss << get_name();
92 
93  if(!doing().empty())
94  {
95  oss << " " << doing();
96  }
97 
98  if(buf_size() > 0)
99  {
100  oss << " buffer size " << buf_size() << " bytes:";
101  }
102 
103  if(events() == 0)
104  oss << " " << "N/A";
105  else
106  oss << " " << std::fixed << std::setprecision(3) << MiB_per_sec << " MiB/sec";
107 
108  if(cycles_consumed() != 0)
109  {
110  const double cycles_per_byte = static_cast<double>(cycles_consumed()) / events();
111  oss << " " << std::fixed << std::setprecision(2) << cycles_per_byte << " cycles/byte";
112  }
113 
114  oss << " (" << MiB_total << " MiB in " << milliseconds() << " ms)\n";
115 
116  return oss.str();
117  }
118 
119 std::string Timer::result_string_ops() const
120  {
121  std::ostringstream oss;
122 
123  oss << get_name() << " ";
124 
125  if(events() == 0)
126  {
127  oss << "no events\n";
128  }
129  else
130  {
131  oss << static_cast<uint64_t>(events_per_second())
132  << ' ' << doing() << "/sec; "
133  << std::setprecision(2) << std::fixed
134  << ms_per_event() << " ms/op";
135 
136  if(cycles_consumed() != 0)
137  {
138  const double cycles_per_op = static_cast<double>(cycles_consumed()) / events();
139  const size_t precision = (cycles_per_op < 10000) ? 2 : 0;
140  oss << " " << std::fixed << std::setprecision(precision) << cycles_per_op << " cycles/op";
141  }
142 
143  oss << " (" << events() << " " << (events() == 1 ? "op" : "ops")
144  << " in " << milliseconds() << " ms)\n";
145  }
146 
147  return oss.str();
148  }
149 
150 }
uint64_t events() const
Definition: timer.h:119
double events_per_second() const
Definition: timer.h:144
bool operator<(const Timer &other) const
Definition: timer.cpp:59
uint64_t BOTAN_TEST_API get_cpu_cycle_counter()
Definition: os_utils.cpp:146
double milliseconds() const
Definition: timer.h:100
size_t buf_size() const
Definition: timer.h:134
std::string to_string() const
Definition: timer.cpp:67
uint64_t cycles_consumed() const
Definition: timer.h:110
const std::string & doing() const
Definition: timer.h:129
uint64_t BOTAN_TEST_API get_system_timestamp_ns()
Definition: os_utils.cpp:289
double ms_per_event() const
Definition: timer.h:105
double seconds() const
Definition: timer.h:95
Definition: alg_id.cpp:13
const std::string & get_name() const
Definition: timer.h:124
void start()
Definition: timer.cpp:15
void stop()
Definition: timer.cpp:22