Monero
math_helper.h
Go to the documentation of this file.
1 // Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of the Andrey N. Sabelnikov nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26 
27 
28 
29 
30 #pragma once
31 
32 
33 #include <list>
34 #include <numeric>
35 #include <random>
36 #include <boost/timer/timer.hpp>
37 #include <boost/uuid/uuid.hpp>
38 #include <boost/uuid/random_generator.hpp>
39 
40 #include "syncobj.h"
41 #include "time_helper.h"
42 
43 namespace epee
44 {
45 namespace math_helper
46 {
47 
48  template<typename val, int default_base>
49  class average
50  {
51  public:
52 
54  {
55  m_base = default_base;
56  m_last_avg_val = 0;
57  }
58 
59  bool set_base()
60  {
62 
63  m_base = default_base;
64  if(m_list.size() > m_base)
65  m_list.resize(m_base);
66 
67  return true;
68  }
69 
70  typedef val value_type;
71 
72  void push(const value_type& vl)
73  {
75 
76 //#ifndef DEBUG_STUB
77  m_list.push_back(vl);
78  if(m_list.size() > m_base )
79  m_list.pop_front();
80 //#endif
81  }
82 
83  double update(const value_type& vl)
84  {
86 //#ifndef DEBUG_STUB
87  push(vl);
88 //#endif
89 
90  return get_avg();
91  }
92 
93  double get_avg()
94  {
96 
97  value_type vl = std::accumulate(m_list.begin(), m_list.end(), value_type(0));
98  if(m_list.size())
99  return m_last_avg_val = (double)(vl/m_list.size());
100 
101  return m_last_avg_val = (double)vl;
102  }
103 
105  {
107  if(m_list.size())
108  return m_list.back();
109 
110  return 0;
111  }
112 
113  private:
114  unsigned int m_base;
116  std::list<value_type> m_list;
118  };
119 
120 
121 #ifdef WINDOWS_PLATFORM
122 
123  /************************************************************************/
124  /* */
125  /************************************************************************/
126  class timing_guard_base
127  {
128  public:
129  virtual ~timing_guard_base(){};
130  };
131 
132  template<class T>
133  class timing_guard: public timing_guard_base
134  {
135  public:
136  timing_guard(T& avrg):m_avrg(avrg)
137  {
138  m_start_ticks = ::GetTickCount();
139  }
140 
141  ~timing_guard()
142  {
143  m_avrg.push(::GetTickCount()-m_start_ticks);
144  }
145 
146  private:
147  T& m_avrg;
148  DWORD m_start_ticks;
149  };
150 
151  template<class t_timing>
152  timing_guard_base* create_timing_guard(t_timing& timing){return new timing_guard<t_timing>(timing);}
153 
154 #define BEGIN_TIMING_ZONE(timing_var) { boost::shared_ptr<math_helper::timing_guard_base> local_timing_guard_ptr(math_helper::create_timing_guard(timing_var));
155 #define END_TIMING_ZONE() }
156 #endif
157 
158 //#ifdef WINDOWS_PLATFORM_EX
159  template<uint64_t default_time_window>
160  class speed
161  {
162  public:
163 
165  {
166  m_time_window = default_time_window;
167  m_last_speed_value = 0;
168  }
169  bool chick()
170  {
171 #ifndef DEBUG_STUB
174  m_chicks.push_back(ticks);
176  //flush(ticks);
177 #endif
178  return true;
179  }
180 
181  bool chick(size_t count)
182  {
183  for(size_t s = 0; s != count; s++)
184  chick();
185 
186  return true;
187  }
188 
189 
190  size_t get_speed()
191  {
193  return m_last_speed_value = m_chicks.size();
194  }
195  private:
196 
197  bool flush(uint64_t ticks)
198  {
200  std::list<uint64_t>::iterator it = m_chicks.begin();
201  while(it != m_chicks.end())
202  {
203  if(*it + m_time_window < ticks)
204  m_chicks.erase(it++);
205  else
206  break;
207  }
209  return true;
210  }
211 
212  std::list<uint64_t> m_chicks;
216  };
217 //#endif
218 
219  template<class tlist>
220  void randomize_list(tlist& t_list)
221  {
222  for(typename tlist::iterator it = t_list.begin();it!=t_list.end();it++)
223  {
224  size_t offset = rand()%t_list.size();
225  typename tlist::iterator it_2 = t_list.begin();
226  for(size_t local_offset = 0;local_offset!=offset;local_offset++)
227  it_2++;
228  if(it_2 == it)
229  continue;
230  std::swap(*it_2, *it);
231  }
232 
233  }
234  template<typename get_interval, bool start_immediate = true>
236  {
238  {
239 #ifdef _WIN32
240  FILETIME fileTime;
241  GetSystemTimeAsFileTime(&fileTime);
242  unsigned __int64 present = 0;
243  present |= fileTime.dwHighDateTime;
244  present = present << 32;
245  present |= fileTime.dwLowDateTime;
246  present /= 10; // mic-sec
247  return present;
248 #else
249  struct timeval tv;
250  gettimeofday(&tv, NULL);
251  return tv.tv_sec * 1000000 + tv.tv_usec;
252 #endif
253  }
254 
256  {
257  m_interval = get_interval()();
258  }
259 
260  public:
262  {
263  m_last_worked_time = 0;
264  if(!start_immediate)
267  }
268 
269  void trigger()
270  {
271  m_last_worked_time = 0;
272  }
273 
274  template<class functor_t>
275  bool do_call(functor_t functr)
276  {
277  uint64_t current_time = get_time();
278 
279  if(current_time - m_last_worked_time > m_interval)
280  {
281  bool res = functr();
284  return res;
285  }
286  return true;
287  }
288 
289  private:
292  };
293 
294  template<uint64_t N> struct get_constant_interval { public: uint64_t operator()() const { return N; } };
295 
296  template<int default_interval, bool start_immediate = true>
297  class once_a_time_seconds: public once_a_time<get_constant_interval<default_interval * (uint64_t)1000000>, start_immediate> {};
298  template<int default_interval, bool start_immediate = true>
299  class once_a_time_milliseconds: public once_a_time<get_constant_interval<default_interval * (uint64_t)1000>, start_immediate> {};
300  template<typename get_interval, bool start_immediate = true>
301  class once_a_time_seconds_range: public once_a_time<get_interval, start_immediate> {};
302 }
303 }
const char * res
Definition: hmac_keccak.cpp:42
Definition: math_helper.h:294
unsigned int m_base
Definition: math_helper.h:114
uint64_t m_interval
Definition: math_helper.h:291
const uint32_t T[512]
Definition: groestl_tables.h:36
void randomize_list(tlist &t_list)
Definition: math_helper.h:220
uint64_t get_tick_count()
Definition: time_helper.h:82
int * count
Definition: gmock_stress_test.cc:176
Definition: math_helper.h:235
critical_section m_lock
Definition: math_helper.h:117
uint64_t operator()() const
Definition: math_helper.h:294
const char * s
Definition: minissdp.c:596
bool flush(uint64_t ticks)
Definition: math_helper.h:197
void push(const value_type &vl)
Definition: math_helper.h:72
once_a_time()
Definition: math_helper.h:261
#define CRITICAL_REGION_END()
Definition: syncobj.h:158
size_t m_last_speed_value
Definition: math_helper.h:214
critical_section m_lock
Definition: math_helper.h:215
void trigger()
Definition: math_helper.h:269
Definition: syncobj.h:81
void rand(size_t N, uint8_t *bytes)
Definition: crypto.h:167
value_type get_last_val()
Definition: math_helper.h:104
bool do_call(functor_t functr)
Definition: math_helper.h:275
#define CRITICAL_REGION_BEGIN(x)
Definition: syncobj.h:154
val value_type
Definition: math_helper.h:70
unsigned __int64 uint64_t
Definition: stdint.h:136
#define CRITICAL_REGION_LOCAL(x)
Definition: syncobj.h:153
uint64_t m_time_window
Definition: math_helper.h:213
void set_next_interval()
Definition: math_helper.h:255
TODO: (mj-xmr) This will be reduced in an another PR.
Definition: byte_slice.h:39
double get_avg()
Definition: math_helper.h:93
uint64_t get_time() const
Definition: math_helper.h:237
Definition: math_helper.h:299
bool chick(size_t count)
Definition: math_helper.h:181
Definition: speed.py:1
average()
Definition: math_helper.h:53
uint64_t m_last_worked_time
Definition: math_helper.h:290
std::list< uint64_t > m_chicks
Definition: math_helper.h:212
Definition: math_helper.h:49
bool chick()
Definition: math_helper.h:169
Definition: math_helper.h:297
double m_last_avg_val
Definition: math_helper.h:115
double update(const value_type &vl)
Definition: math_helper.h:83
std::list< value_type > m_list
Definition: math_helper.h:116
size_t get_speed()
Definition: math_helper.h:190
speed()
Definition: math_helper.h:164
bool set_base()
Definition: math_helper.h:59