2004-02-11 18:18:34 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
|
2003-01-22 00:45:36 +08:00
|
|
|
*
|
2004-02-11 18:18:34 +08:00
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
2003-01-22 00:45:36 +08:00
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2004-02-11 18:18:34 +08:00
|
|
|
*
|
2003-01-22 00:45:36 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2004-02-11 18:18:34 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2003-01-22 00:45:36 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
2001-09-22 10:42:08 +08:00
|
|
|
//#include <stdlib.h>
|
2001-01-11 00:32:10 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
#include <osg/Timer>
|
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
// follows are the constructors of the Timer class, once version
|
|
|
|
// for each OS combination. The order is WIN32, FreeBSD, Linux, IRIX,
|
|
|
|
// and the rest of the world.
|
|
|
|
//
|
|
|
|
// all the rest of the timer methods are implemented within the header.
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2003-07-16 05:19:03 +08:00
|
|
|
|
|
|
|
const Timer* Timer::instance()
|
|
|
|
{
|
|
|
|
static Timer s_timer;
|
|
|
|
return &s_timer;
|
|
|
|
}
|
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
#ifdef WIN32
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#include <winbase.h>
|
|
|
|
Timer::Timer()
|
|
|
|
{
|
|
|
|
_useStandardClock = false;
|
2004-02-11 18:18:34 +08:00
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
if (_useStandardClock)
|
|
|
|
{
|
2004-02-11 18:18:34 +08:00
|
|
|
_secsPerTick = (1.0 / (double) CLOCKS_PER_SEC);
|
2001-09-22 10:42:08 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-03-03 05:37:44 +08:00
|
|
|
// use a static here to ensure that the time to determine
|
|
|
|
// the cpu frequency is not incurred more than once
|
|
|
|
// per app execution.
|
2001-12-17 00:30:34 +08:00
|
|
|
static double _tempSecsPerClick=0.0;
|
|
|
|
if (_tempSecsPerClick==0.0)
|
|
|
|
{
|
2004-03-03 05:37:44 +08:00
|
|
|
// QueryPerformanceCounter under Windows 2000 Service Pack 3
|
|
|
|
// two 2.4 GHz cpus was timed to take about 70 times longer
|
|
|
|
// than the RDTSC assembly instruction sequence, but if
|
|
|
|
// that counter is available we use it to quickly determine
|
|
|
|
// the Time Stamp Counter rate, quickly as in 240 microseconds
|
|
|
|
LARGE_INTEGER frequency;
|
|
|
|
if(QueryPerformanceFrequency(&frequency))
|
|
|
|
{
|
|
|
|
//#define DEBUG_TIME_OUTPUT 1
|
|
|
|
LARGE_INTEGER QPCstart, QPCstop,
|
|
|
|
QPCstartAfter, QPCstopAfter;
|
|
|
|
Timer_t TSCstart, TSCstop;
|
|
|
|
double QPCSecsPerClock = 1.0/frequency.QuadPart;
|
|
|
|
double elapsed, last, current, bound;
|
|
|
|
|
|
|
|
QueryPerformanceCounter(&QPCstart);
|
|
|
|
TSCstart=tick();
|
|
|
|
QueryPerformanceCounter(&QPCstartAfter);
|
2001-12-17 00:30:34 +08:00
|
|
|
|
2004-03-03 05:37:44 +08:00
|
|
|
current = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
// store the seconds per clock
|
|
|
|
last = current;
|
|
|
|
// read the clocks
|
|
|
|
QueryPerformanceCounter(&QPCstop);
|
|
|
|
TSCstop=tick();
|
|
|
|
QueryPerformanceCounter(&QPCstopAfter);
|
|
|
|
// average before and after to approximate reading
|
|
|
|
// both clocks at the same time
|
|
|
|
elapsed = ((QPCstop.QuadPart+QPCstopAfter.QuadPart)
|
|
|
|
-(QPCstart.QuadPart+QPCstartAfter.QuadPart))/2.0
|
|
|
|
*QPCSecsPerClock;
|
|
|
|
// TSC seconds per clock
|
|
|
|
current = elapsed / (TSCstop-TSCstart);
|
|
|
|
// calculate a bound to check against
|
|
|
|
bound = current/1000000;
|
|
|
|
// break if current-bound<last && current+bound>last
|
|
|
|
}while(current-bound>last || current+bound<last);
|
|
|
|
_tempSecsPerClick = current;
|
|
|
|
#ifdef DEBUG_TIME_OUTPUT
|
|
|
|
fprintf(stderr, "current %e, last %e\n",
|
|
|
|
1.0/current, 1.0/last);
|
|
|
|
fprintf(stderr, "Total Time %e\n",
|
|
|
|
(QPCstop.QuadPart-QPCstart.QuadPart)*QPCSecsPerClock);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#ifndef DEBUG_TIME_OUTPUT
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
Timer_t start_time = tick();
|
|
|
|
Sleep (1000);
|
|
|
|
Timer_t end_time = tick();
|
|
|
|
|
|
|
|
_tempSecsPerClick = 1.0/(double)(end_time-start_time);
|
|
|
|
#ifdef DEBUG_TIME_OUTPUT
|
|
|
|
fprintf(stderr, "Sec delay rate %e\n",
|
|
|
|
1.0/_tempSecsPerClick);
|
|
|
|
#endif
|
|
|
|
}
|
2001-12-17 00:30:34 +08:00
|
|
|
}
|
2003-08-20 20:51:40 +08:00
|
|
|
_secsPerTick = _tempSecsPerClick;
|
2001-09-22 10:42:08 +08:00
|
|
|
}
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
#elif defined(__FreeBSD__)
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
Timer::Timer()
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2001-09-22 10:42:08 +08:00
|
|
|
_useStandardClock = false;
|
2004-02-11 18:18:34 +08:00
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
if (_useStandardClock)
|
|
|
|
{
|
2003-08-20 20:51:40 +08:00
|
|
|
_secsPerTick = 1e-6; // gettimeofday()'s precision.
|
2001-09-22 10:42:08 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int cpuspeed;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = sizeof(cpuspeed);
|
|
|
|
if (sysctlbyname("machdep.tsc_freq", &cpuspeed, &len, NULL, NULL) == -1)
|
|
|
|
{
|
|
|
|
_useStandardClock = true;
|
2003-09-04 16:50:06 +08:00
|
|
|
perror("sysctlbyname(machdep.tsc_freq)");
|
2001-09-22 10:42:08 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-08-20 20:51:40 +08:00
|
|
|
_secsPerTick = 1.0/cpuspeed;
|
2001-09-22 10:42:08 +08:00
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
2004-02-11 18:18:34 +08:00
|
|
|
|
|
|
|
#elif defined(__linux)
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/types.h>
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
Timer::Timer()
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2004-10-19 02:59:05 +08:00
|
|
|
#if defined(__ia64) || defined(__x86_64__) || defined(__powerpc)
|
2003-09-04 16:50:06 +08:00
|
|
|
_useStandardClock = true;
|
2002-12-20 03:50:16 +08:00
|
|
|
#else
|
2004-02-22 00:59:14 +08:00
|
|
|
_useStandardClock = true;
|
2002-12-20 03:50:16 +08:00
|
|
|
#endif
|
2004-02-11 18:18:34 +08:00
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
if (_useStandardClock)
|
|
|
|
{
|
2003-08-20 20:51:40 +08:00
|
|
|
_secsPerTick = 1e-6; // gettimeofday()'s precision.
|
2001-09-22 10:42:08 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char buff[128];
|
|
|
|
FILE *fp = fopen( "/proc/cpuinfo", "r" );
|
|
|
|
|
|
|
|
double cpu_mhz=0.0f;
|
|
|
|
|
|
|
|
while( fgets( buff, sizeof( buff ), fp ) > 0 )
|
|
|
|
{
|
|
|
|
if( !strncmp( buff, "cpu MHz", strlen( "cpu MHz" )))
|
2003-09-04 16:50:06 +08:00
|
|
|
{
|
|
|
|
char *ptr = buff;
|
|
|
|
|
|
|
|
while( ptr && *ptr != ':' ) ptr++;
|
2004-02-11 18:18:34 +08:00
|
|
|
if( ptr )
|
2003-09-04 16:50:06 +08:00
|
|
|
{
|
|
|
|
ptr++;
|
|
|
|
sscanf( ptr, "%lf", &cpu_mhz );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2001-09-22 10:42:08 +08:00
|
|
|
}
|
|
|
|
fclose( fp );
|
|
|
|
|
|
|
|
if (cpu_mhz==0.0f)
|
|
|
|
{
|
|
|
|
// error - no cpu_mhz found.
|
|
|
|
Timer_t start_time = tick();
|
|
|
|
sleep (1);
|
|
|
|
Timer_t end_time = tick();
|
2003-08-20 20:51:40 +08:00
|
|
|
_secsPerTick = 1.0/(double)(end_time-start_time);
|
2001-09-22 10:42:08 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-08-20 20:51:40 +08:00
|
|
|
_secsPerTick = 1e-6/cpu_mhz;
|
2001-09-22 10:42:08 +08:00
|
|
|
}
|
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
#elif defined(__sgi)
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/syssgi.h>
|
|
|
|
#include <sys/mman.h>
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
Timer::Timer( void )
|
|
|
|
{
|
2001-12-17 00:30:34 +08:00
|
|
|
_useStandardClock = false; // default to false.
|
2001-09-22 10:42:08 +08:00
|
|
|
|
2001-12-17 00:30:34 +08:00
|
|
|
if (!_useStandardClock)
|
2001-09-22 10:42:08 +08:00
|
|
|
{
|
|
|
|
__psunsigned_t phys_addr, raddr;
|
|
|
|
unsigned int cycleval;
|
2002-06-14 02:27:14 +08:00
|
|
|
volatile unsigned long long *iotimer_addr;
|
2001-09-22 10:42:08 +08:00
|
|
|
int fd, poffmask;
|
|
|
|
|
|
|
|
poffmask = getpagesize() - 1;
|
2002-06-14 02:27:14 +08:00
|
|
|
phys_addr = syssgi( SGI_QUERY_CYCLECNTR, &cycleval );
|
2001-09-22 10:42:08 +08:00
|
|
|
raddr = phys_addr & ~poffmask;
|
|
|
|
|
2002-06-14 02:27:14 +08:00
|
|
|
_clockAddress_32 = 0;
|
|
|
|
_clockAddress_64 = 0;
|
2002-08-09 05:36:22 +08:00
|
|
|
_rollOver = 0;
|
|
|
|
_lastClockValue = 0;
|
2001-09-22 10:42:08 +08:00
|
|
|
|
|
|
|
if( (fd = open( "/dev/mmem", O_RDONLY )) < 0 )
|
|
|
|
{
|
|
|
|
perror( "/dev/mmem" );
|
2002-06-14 02:27:14 +08:00
|
|
|
_useStandardClock=true;
|
2001-09-22 10:42:08 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-06-14 02:27:14 +08:00
|
|
|
iotimer_addr = (volatile unsigned long long *)mmap(
|
2001-09-22 10:42:08 +08:00
|
|
|
(void *)0L,
|
|
|
|
(size_t)poffmask,
|
|
|
|
(int)PROT_READ,
|
|
|
|
(int)MAP_PRIVATE, fd, (off_t)raddr);
|
|
|
|
|
2002-06-14 02:27:14 +08:00
|
|
|
iotimer_addr = (unsigned long long *)(
|
2001-09-22 10:42:08 +08:00
|
|
|
(__psunsigned_t)iotimer_addr + (phys_addr & poffmask)
|
|
|
|
);
|
|
|
|
|
|
|
|
_cycleCntrSize = syssgi( SGI_CYCLECNTR_SIZE );
|
|
|
|
|
2002-06-14 02:27:14 +08:00
|
|
|
// Warning: this casts away the volatile; not good
|
2001-09-22 10:42:08 +08:00
|
|
|
if( _cycleCntrSize > 32 )
|
2002-06-14 02:27:14 +08:00
|
|
|
_clockAddress_32 = 0,
|
|
|
|
_clockAddress_64 = (unsigned long long *) iotimer_addr;
|
|
|
|
else
|
|
|
|
_clockAddress_32 = (unsigned long *) iotimer_addr,
|
|
|
|
_clockAddress_64 = 0;
|
2001-09-22 10:42:08 +08:00
|
|
|
|
2003-08-20 20:51:40 +08:00
|
|
|
_secsPerTick = (double)(cycleval)* 1e-12;
|
2001-12-17 00:30:34 +08:00
|
|
|
|
2002-06-14 03:39:28 +08:00
|
|
|
#if 0 // Obsolete
|
2001-12-17 00:30:34 +08:00
|
|
|
// this is to force the use of the standard clock in
|
|
|
|
// instances which the realtime clock is of such a small
|
|
|
|
// size that it will loop too rapidly for proper realtime work.
|
|
|
|
// this happens on the O2 for instance.
|
|
|
|
if (_cycleCntrSize<=32) _useStandardClock=true;
|
2002-06-14 03:39:28 +08:00
|
|
|
#endif // Obsolete
|
2001-12-17 00:30:34 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_useStandardClock)
|
|
|
|
{
|
2003-08-20 20:51:40 +08:00
|
|
|
_secsPerTick = 1e-6; // gettimeofday()'s precision.
|
2001-09-22 10:42:08 +08:00
|
|
|
}
|
2001-12-17 00:30:34 +08:00
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
}
|
2002-01-18 22:11:46 +08:00
|
|
|
|
2004-03-08 19:02:05 +08:00
|
|
|
#elif defined (__APPLE__) || defined (macintosh)
|
2001-10-17 04:56:46 +08:00
|
|
|
|
2004-04-12 05:21:38 +08:00
|
|
|
Timer::Timer()
|
2002-01-18 22:11:46 +08:00
|
|
|
{
|
2004-04-12 05:21:38 +08:00
|
|
|
_useStandardClock = true;
|
|
|
|
_secsPerTick = 1e-6; // gettimeofday()'s precision.
|
2002-01-18 22:11:46 +08:00
|
|
|
}
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
#elif defined(unix)
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
Timer::Timer( void )
|
|
|
|
{
|
|
|
|
_useStandardClock = true;
|
2003-08-20 20:51:40 +08:00
|
|
|
_secsPerTick = 1e-6; // gettimeofday()'s precision.
|
2001-09-22 10:42:08 +08:00
|
|
|
}
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
#else
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
// handle the rest of the OS world by just using the std::clock,
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
Timer::Timer( void )
|
|
|
|
{
|
|
|
|
_useStandardClock = true;
|
2003-08-20 20:51:40 +08:00
|
|
|
_secsPerTick = (1.0 / (double) CLOCKS_PER_SEC);
|
2001-09-22 10:42:08 +08:00
|
|
|
}
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
#endif
|