Add a periodic statistics printer to time

This commit is contained in:
Danny Robson 2012-08-07 18:35:38 +10:00
parent 4a54e2be60
commit 7107968cdb
2 changed files with 44 additions and 0 deletions

View File

@ -20,6 +20,7 @@
#include "time.hpp"
#include "debug.hpp"
#include "log.hpp"
#include "platform.hpp"
#include "types/casts.hpp"
@ -77,4 +78,28 @@ util::sleep (uint64_t ns) {
#endif
util::polled_duration::polled_duration (std::string name, uint64_t interval):
m_name (name),
m_interval (interval),
m_next (nanoseconds () + interval)
{ ; }
void
util::polled_duration::start (void) {
m_last = nanoseconds ();
}
void
util::polled_duration::stop (void) {
uint64_t now = nanoseconds ();
uint64_t dt = now - m_last;
m_series.add (dt / 1000000);
if (m_next < now) {
LOG_DEBUG ("timing: '%s'. %s", m_name, m_series);
m_series.reset ();
m_next = now + m_interval;
}
}

View File

@ -21,10 +21,29 @@
#define __UTIL_TIME_HPP
#include <cstdint>
#include <string>
#include "stats.hpp"
namespace util {
uint64_t nanoseconds (void);
void sleep (uint64_t ns);
class polled_duration {
public:
polled_duration (std::string name, uint64_t interval);
void start (void);
void stop (void);
protected:
std::string m_name;
uint64_t m_last;
uint64_t m_interval;
uint64_t m_next;
stats::accumulator<uint64_t> m_series;
};
}
#endif // __UTIL_TIME_HPP