This commit is contained in:
dockes 2006-01-26 12:29:20 +00:00
parent 52aaa52754
commit af105201f9
2 changed files with 118 additions and 4 deletions

View File

@ -1,5 +1,5 @@
#ifndef lint
static char rcsid[] = "@(#$Id: smallut.cpp,v 1.14 2006-01-23 13:32:28 dockes Exp $ (C) 2004 J.F.Dockes";
static char rcsid[] = "@(#$Id: smallut.cpp,v 1.15 2006-01-26 12:29:20 dockes Exp $ (C) 2004 J.F.Dockes";
#endif
/*
* This program is free software; you can redistribute it and/or modify
@ -18,14 +18,18 @@ static char rcsid[] = "@(#$Id: smallut.cpp,v 1.14 2006-01-23 13:32:28 dockes Exp
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef TEST_SMALLUT
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <unistd.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string>
#include "smallut.h"
#include "debuglog.h"
#include "pathut.h"
@ -346,6 +350,92 @@ string escapeHtml(const string &in)
return out;
}
////////////////////
// Internal redefinition of system time interface to help with dependancies
struct m_timespec {
time_t tv_sec;
long tv_nsec;
};
#ifndef CLOCK_REALTIME
#define CLOCK_REALTIME 1
#endif
#define MILLIS(TV) ( (long)(((TV).tv_sec - m_secs) * 1000 + \
((TV).tv_nsec - m_nsecs) / 1000000))
#define MICROS(TV) ( (long)(((TV).tv_sec - m_secs) * 1000000 + \
((TV).tv_nsec - m_nsecs) / 1000))
// We use gettimeofday instead of clock_gettime for now and get only
// uS resolution, because clock_gettime is more configuration trouble
// than it's worth
static void gettime(int, struct m_timespec *ts)
{
struct timeval tv;
gettimeofday(&tv, 0);
ts->tv_sec = tv.tv_sec;
ts->tv_nsec = tv.tv_usec * 1000;
}
///// End system interface
static m_timespec frozen_tv;
void Chrono::refnow()
{
gettime(CLOCK_REALTIME, &frozen_tv);
}
Chrono::Chrono()
{
restart();
}
// Reset and return value before rest in milliseconds
long Chrono::restart()
{
struct m_timespec tv;
gettime(CLOCK_REALTIME, &tv);
long ret = MILLIS(tv);
m_secs = tv.tv_sec;
m_nsecs = tv.tv_nsec;
return ret;
}
// Get current timer value, milliseconds
long Chrono::millis(int frozen)
{
if (frozen) {
return MILLIS(frozen_tv);
} else {
struct m_timespec tv;
gettime(CLOCK_REALTIME, &tv);
return MILLIS(tv);
}
}
//
long Chrono::micros(int frozen)
{
if (frozen) {
return MICROS(frozen_tv);
} else {
struct m_timespec tv;
gettime(CLOCK_REALTIME, &tv);
return MICROS(tv);
}
}
float Chrono::secs(int frozen)
{
struct m_timespec tv;
gettime(CLOCK_REALTIME, &tv);
float secs = (float)(frozen?frozen_tv.tv_sec:tv.tv_sec - m_secs);
float nsecs = (float)(frozen?frozen_tv.tv_nsec:tv.tv_nsec - m_nsecs);
//fprintf(stderr, "secs %.2f nsecs %.2f\n", secs, nsecs);
return secs + nsecs * 1e-9;
}
#else
#include <string>

View File

@ -1,6 +1,6 @@
#ifndef _SMALLUT_H_INCLUDED_
#define _SMALLUT_H_INCLUDED_
/* @(#$Id: smallut.h,v 1.13 2006-01-26 07:03:35 dockes Exp $ (C) 2004 J.F.Dockes */
/* @(#$Id: smallut.h,v 1.14 2006-01-26 12:29:20 dockes Exp $ (C) 2004 J.F.Dockes */
#include <string>
#include <list>
@ -40,6 +40,30 @@ extern bool stringToBool(const string &s);
tab}) at beginning and end of input string */
extern void trimstring(string &s, const char *ws = " \t");
/** Escape things like < or & by turining them to entities */
extern string escapeHtml(const string &in);
class Chrono {
public:
Chrono();
/** Reset origin */
long restart();
/** Snapshot current time */
static void refnow();
/** Get current elapsed since creation or restart
*
* @param frozen give time since the last refnow call (this is to
* allow for using one actual system call to get values from many
* chrono objects, like when examining timeouts in a queue)
*/
long millis(int frozen = 0);
long micros(int frozen = 0);
float secs(int frozen = 0);
private:
long m_secs;
long m_nsecs;
};
#endif /* _SMALLUT_H_INCLUDED_ */