RE: [ROOT] system time in msec ?

From: Robert Hatcher (rhatcher@SLAC.stanford.edu)
Date: Wed Mar 06 2002 - 16:33:09 MET


On Wed, 6 Mar 2002, Radovan Chytracek wrote:

>      what about the following milisecond timer? Works for me on Windows and
> Linux.

I'm not sure what this would have over the existing ROOT class TStopwatch.

But a *timer* wasn't what was asked for.  Timers measure an elapsed time
from a start signal to a stop signal.  Let me repeat Joe's original request:

> > > > > TTime seems only to be a storage class, and TDatime does only return
> > > current time in sec precision. Any class who returns current time in
> > > msec precision ? Since my app must run on Linux AND windoze I do not
> > > want to implement it by myself ( no clue about Linux).

Read this carefully.  He wants something that gives the current time --
that is the wall clock + calender.  But he wants sub-second precison,
which TDatime doesn't have.  TTimeStamp has provision for such precison
but the default ctor (which returns the "current time") has no code
for correctly setting the sub-sec part because there isn't a commonly
implemented standard way of getting that information from the system.

As for the separate suggestion of QTime, well, for MINOS purposes
VldTimeStamp (from when comes TTimeStamp) this is fatally flawed by:

  | It operates in local time; it knows nothing about time zones or
  | daylight savings time

We have a collaboration scattered about the world and need to express
out times in UTC/GMT.

-robert

> Cheers
>               Radovan
>
> ---------- cut here BenchTimer.h -------------------------------------------
> #include <stdlib.h>
> #include <time.h>
>
> #define CLCK_PER_MILISEC (CLOCKS_PER_SEC/1000)
>
> class BenchTimer
> {
> public:
>   typedef enum { eMili, eSec } EUnit;
> public:
>   BenchTimer() : mInProgress(false), mStart(0), mFinish(0) {
>   }
>
>   ~BenchTimer() {
>   }
>
>   inline void Start() {
>     mInProgress = true;
>     mStart = ::clock();
>   }
>
>   inline void Finish() {
>     mFinish = ::clock();
>     mInProgress = false;
>   }
>
>   inline bool Busy() {
>     return mInProgress;
>   }
>
>   inline void Reset() {
>     if( !Busy() ) {
>       mInProgress = false;
>       mStart      = 0;
>       mFinish     = 0;
>     }
>   }
>
>   inline double Report( BenchTimer::EUnit unit = eMili ) {
>     double result = (double)(mFinish-mStart);
>     return( (unit == eMili) ? (result/CLCK_PER_MILISEC) :
> (result/CLOCKS_PER_SEC) );
>   }
>
> private:
>   bool    mInProgress;
>   clock_t mStart;
>   clock_t mFinish;
> };
>
> ---------- cut here BenchTimer.h -------------------------------------------
>
> > -----Original Message-----
> > From: owner-roottalk@pcroot.cern.ch
> > [mailto:owner-roottalk@pcroot.cern.ch]On Behalf Of Robert Hatcher
> > Sent: Tuesday, 05 March 2002 21:37
> > To: Faine, Valeri
> > Cc: 'Joe Robe '; 'roottalk@pcroot.cern.ch '
> > Subject: RE: [ROOT] system time in msec ?
> >
> >
> > On Tue, 5 Mar 2002, Faine, Valeri wrote:
> >
> > > In theory  (;-)the TStopwatch method is a good ROOT candidate.
> > > Have a look at
> > >
> > >
> > http://root.cern.ch/root/htmldoc/src/TStopwatch.cxx.html#TStopwatch:GetRealT
> > > ime
> >
> > Actually, no it isn't.  TStopwatch is a good candidate for a stop watch
> > object.  Stopwatches are good for measuring elapsed times not absolutes.
> > Joe's request was:
> >
> > >>> [...] Any class who returns current time in msec precision ? [...]
> >
> > TStopwatch does not give the current time (in the sense of Tue, 5 Mar
> > 2002, 2:04 CST), but instead gives (in general) the cpu or *delta*
> > wall clock time since the the process began...in clock ticks converted
> > to real units.  Before you suggest Double_t TStopwatch::GetRealTime()
> > you should look at the code, for unix this is:
> >
> > #elif defined(R__UNIX)
> >    struct tms cpt;
> >    Double_t trt =  (Double_t)times(&cpt);
> >    return trt / (double)gTicks;
> >
> > So what does "times()" return?
> > $ man -S 2 times
> >
> >   |  RETURN VALUE
> >   |     The function times returns the number of clock ticks  that
> >   |     have  elapsed  since  an  arbitrary point in the past. For
> >   |     Linux this point is the  moment  the  system  was  booted.
> >   |     This  return value may overflow the possible range of type
> >   |     clock_t.  On error, (clock_t) -1 is returned, and errno is
> >   |     set  appropriately.   The number of clock ticks per second
> >   |     can be obtained using
> >   |            sysconf(_SC_CLK_TCK);
> >   [...]
> >
> > So TStopwatch::GetRealTime, on Linux, returns the seconds since the
> > machine was booted.  I doubt this satisfies Joe's requirements.
> >
> > Brett Viren pointed me to gettimeofday() which is available on Linux
> > (and *probably* most of UNIX as it claims "CONFORMING TO SVr4, BSD 4.3"),
> > but I don't know how to asertain how uniformly it is implemented
> > on other systems nor do I have enough knowledge to say anything about
> > the M$ Windows side.  I'm considering the following change to MINOS's
> > VldTimeStamp (from whence TTimeStamp was derived):
> >
> > up in the beginning:
> > #ifndef WIN32
> > // timeval, timezone, gettimeofday()
> > #include <sys/time.h>
> > #endif
> >
> > in void VldTimeStamp::Set() replace all the code with:
> >
> > #ifndef WIN32
> >    // this should work on UNIX to get microsec precision
> >    // we'll stick to a ns hack to ensure calls are unique
> >    timeval now;
> >    static Int_t fake_ns = 0;
> >    //struct timezone tz;
> >
> >    if (!gettimeofday(&now,0)) {
> >       fSec     = now.tv_sec;
> >       fNanoSec = now.tv_usec*1000 + (++fake_ns)%1000;
> >    }
> >    else {
> >       time_t now;
> >       time(&now);
> >       fSec     = now;
> >       fNanoSec = ++fake_ns;
> >    }
> > #else
> >    time_t now;
> >    time(&now);
> >    fSec     = now;
> >
> >    static Int_t fake_ns = 0;
> >    fNanoSec = ++fake_ns;
> > #endif
> >
> >
> > -robert
> >
> >
> > > -----Original Message-----
> > > From: Robert Hatcher
> > >
> > > On Tue, 5 Mar 2002, Fons Rademakers wrote:
> > >
> > > > Check TTimeStamp.
> > >
> > > Well, I'm not sure that this satisfies Joe's needs (any more than
> > > TDatime)
> > > in that while it can handle sub-second precison it doesn't know how to
> > > "get" that info.  the TTimeStamp() ctor constructs a time based on
> > > seconds
> > > since "epoch" (1970-01-01 00:00:00). The default ctor calls Set() which
> > > has:
> > >
> > >    // Set Date/Time to current time as reported by the system.
> > >    // No accounting for nanoseconds with std ANSI functions,
> > >    // ns part faked so that subsequent calls simply add 1 to it
> > >    // this ensures that calls within the same second come back
> > >    // distinct (and sortable).
> > >
> > >    time_t now;
> > >    time(&now);
> > >    fSec = now;
> > >
> > >    static Int_t fake_ns = 0;
> > >    fNanoSec = fake_ns++;
> > >
> > > If there are standard ways of determining sub-second times I'd be
> > > interested.  But I know of no means that is consistently available on
> > > all platforms.
> > >
> > > -robert
> > >
> > > > On Tue, 2002-03-05 at 15:06, Joe Robe wrote:
> > > > > Hi,
> > > > >
> > > > > TTime seems only to be a storage class, and TDatime does only return
> > > current time in sec precision. Any class who returns current time in
> > > msec precision ? Since my app must run on Linux AND windoze I do not
> > > want to implement it by myself ( no clue about Linux).
> > > > >
> > > > > Joe
> > > > >
> > > > --
> > > > Org:    CERN, European Laboratory for Particle Physics.
> > > > Mail:   1211 Geneve 23, Switzerland
> > > > E-Mail: Fons.Rademakers@cern.ch              Phone: +41 22 7679248
> > > > WWW:    http://root.cern.ch/~rdm/            Fax:   +41 22 7679480
> >
> > Robert W. Hatcher   | rhatcher@fnal.gov   630-840-3102
> > FNAL CD/CP (MINOS)  | MS 220, PO Box 500, Batavia IL 60510
> >
> >
> >
>
>

Robert W. Hatcher   | rhatcher@fnal.gov   630-840-3102
FNAL CD/CP (MINOS)  | MS 220, PO Box 500, Batavia IL 60510



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:44 MET