1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
// ------------------------------------------------------------------------
// kvu_procedure_timer.cpp: Procedure timer. Meant for timing and gathering
// statistics of repeating events.
// Copyright (C) 2000,2004,2012 Kai Vehmanen
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// ------------------------------------------------------------------------
#include <cmath>
#include <math.h> /* floor() */
#include "kvu_numtostr.h"
#include "kvu_procedure_timer.h"
#include <cstring>
PROCEDURE_TIMER::PROCEDURE_TIMER(int id) {
reset();
idstr_rep = "Timer-" + kvu_numtostr(id);
}
PROCEDURE_TIMER::~PROCEDURE_TIMER(void) {
}
bool PROCEDURE_TIMER::less_than(const struct timespec *i,
const struct timespec *ii) const
{
bool result = false;
if (i->tv_sec < ii->tv_sec)
result = true;
if (i->tv_sec == ii->tv_sec) {
result = false;
if (i->tv_nsec < ii->tv_nsec)
result = true;
}
return result;
}
void PROCEDURE_TIMER::subtract(struct timespec *i,
const struct timespec *ii) const
{
i->tv_nsec -= ii->tv_nsec;
if (i->tv_nsec < 0) {
i->tv_nsec += 1000000000;
i->tv_sec -= 1;
}
i->tv_sec -= ii->tv_sec;
}
double PROCEDURE_TIMER::to_seconds(const struct timeval *i) const {
return(i->tv_sec + static_cast<double>(i->tv_usec) / 1000000.0);
}
double PROCEDURE_TIMER::to_seconds(const struct timespec *i) const {
return(i->tv_sec + static_cast<double>(i->tv_nsec) / 1000000000.0);
}
void PROCEDURE_TIMER::set_upper_bound(const struct timeval *p) {
upper_bound_rep.tv_sec = p->tv_sec;
upper_bound_rep.tv_nsec = p->tv_usec * 1000;
}
void PROCEDURE_TIMER::set_upper_bound_seconds(double secs) {
struct timeval buf;
buf.tv_sec = static_cast<time_t>(floor(secs));
buf.tv_usec = static_cast<time_t>(1000000.0f * (secs - static_cast<double>(buf.tv_sec)));
set_upper_bound(&buf);
}
void PROCEDURE_TIMER::set_lower_bound(const struct timeval *p) {
lower_bound_rep.tv_sec = p->tv_sec;
lower_bound_rep.tv_nsec = p->tv_usec * 1000;
}
void PROCEDURE_TIMER::set_lower_bound_seconds(double secs) {
struct timeval buf;
buf.tv_sec = static_cast<time_t>(floor(secs));
buf.tv_usec = static_cast<time_t>(1000000.0f * (secs - static_cast<double>(buf.tv_sec)));
set_lower_bound(&buf);
}
static void priv_gettime(struct timespec *dst)
{
#if HAVE_CLOCK_GETTIME
#if defined CLOCK_MONOTONIC
clock_gettime(CLOCK_MONOTONIC, dst);
#else
clock_gettime(CLOCK_REALTIME, dst);
#endif
#else
struct timeval tmp;
gettimeofday(&tmp, 0);
dst->tv_sec = tmp.tv_sec;
dst->tv_nsec = tmp.tv_usec * 1000;
#endif
}
void PROCEDURE_TIMER::start(void)
{
priv_gettime(&start_rep);
}
void PROCEDURE_TIMER::stop(void)
{
priv_gettime(&now_rep);
stop_helper();
}
/**
* Do heavy operations in a non-inlined helper
* functions for stop().
*
* This is especially useful for the inlined stop()
* variant.
*/
void PROCEDURE_TIMER::stop_helper(void)
{
subtract(&now_rep, &start_rep);
last_duration_rep = to_seconds(&now_rep);
// cerr << idstr_rep << ": " << kvu_numtostr(length, 16) << " secs." << endl;
events_rep++;
event_time_total_rep += last_duration_rep;
if (events_rep == 1)
memcpy(&min_event_rep, &now_rep, sizeof(struct timeval));
if (less_than(&now_rep, &min_event_rep))
memcpy(&min_event_rep, &now_rep, sizeof(struct timeval));
if (less_than(&max_event_rep, &now_rep))
memcpy(&max_event_rep, &now_rep, sizeof(struct timeval));
if (less_than(&now_rep, &lower_bound_rep))
events_under_bound_rep++;
if (less_than(&upper_bound_rep, &now_rep))
events_over_bound_rep++;
}
void PROCEDURE_TIMER::reset(void) {
events_over_bound_rep = 0;
events_under_bound_rep = 0;
events_rep = 0;
event_time_total_rep = 0.0f;
last_duration_rep = 0.0f;
memset(&now_rep, 0, sizeof(struct timeval));
memset(&start_rep, 0, sizeof(struct timeval));
memset(&min_event_rep, 0, sizeof(struct timeval));
memset(&max_event_rep, 0, sizeof(struct timeval));
memset(&lower_bound_rep, 0, sizeof(struct timeval));
memset(&upper_bound_rep, 0, sizeof(struct timeval));
}
long int PROCEDURE_TIMER::events_over_upper_bound(void) const { return(events_over_bound_rep); }
long int PROCEDURE_TIMER::events_under_lower_bound(void) const { return(events_under_bound_rep); }
long int PROCEDURE_TIMER::event_count(void) const { return(events_rep); }
double PROCEDURE_TIMER::max_duration_seconds(void) const { return(to_seconds(&max_event_rep)); }
double PROCEDURE_TIMER::min_duration_seconds(void) const { return(to_seconds(&min_event_rep)); }
double PROCEDURE_TIMER::average_duration_seconds(void) const { return(event_time_total_rep / event_count()); }
double PROCEDURE_TIMER::last_duration_seconds(void) const { return(last_duration_rep); }
const struct timespec* PROCEDURE_TIMER::min_duration(void) const { return(&max_event_rep); }
const struct timespec* PROCEDURE_TIMER::max_duration(void) const { return(&min_event_rep); }
static std::string priv_s2us(double sec)
{
return kvu_numtostr(sec * 1000000.0, 6);
}
std::string PROCEDURE_TIMER::to_string(void) const
{
std::string res;
res = idstr_rep + ":\n";
res += "Number of events: " + kvu_numtostr(event_count()) + "\n";
res += "Events over bound: " + kvu_numtostr(events_over_upper_bound());
res += " (" + kvu_numtostr(to_seconds(&upper_bound_rep), 8) + "sec)\n";
res += "Events under bound: " + kvu_numtostr(events_under_lower_bound());
res += " (" + kvu_numtostr(to_seconds(&lower_bound_rep), 8) + "sec)\n";
res += "Min duration, us: " + priv_s2us(min_duration_seconds()) + "\n";
res += "Max duration, us: " + priv_s2us(max_duration_seconds()) + "\n";
res += "Average duration, us: " + priv_s2us(average_duration_seconds()) + "\n";
res += "Duration of last event, us: " + priv_s2us(last_duration_rep) + "\n";
return(res);
}
|