00001 #ifndef Timer_CPP
00002 #define Timer_CPP
00003
00004 #include <j2k/nto/Timer.hpp>
00005 #include <j2k/nto/fixprio.cpp>
00006
00007 #if defined( __QNXNTO__ )
00008
00009 #include <sys/neutrino.h>
00010 #include <sys/netmgr.h>
00011 #include <sys/syspage.h>
00012
00013 #endif
00014
00015 Timer* Timer::theTimer = NULL;
00016
00017
00018 Timer::Timer( ULONG nsec, ULONG sec = 0 )
00019 {
00020 if ( theTimer != NULL ) {
00021 delete theTimer;
00022 }
00023
00024 theTimer = this;
00025
00026 memset( &event, 0, sizeof( event ) );
00027
00028
00029
00030 event.sigev_priority = fixprio( 63 );
00031
00032 event.sigev_notify = SIGEV_SIGNAL;
00033 event.sigev_signo = TimerSignal;
00034
00035 memset( &t, 0, sizeof( t ) );
00036
00037 t.it_value.tv_nsec = nsec;
00038 t.it_interval.tv_nsec = nsec;
00039
00040 t.it_value.tv_sec = sec;
00041 t.it_interval.tv_sec = sec;
00042
00043
00044
00045 if ( timer_create( CLOCK_REALTIME, &event, &timerid ) == -1 )
00046 {
00047 MC_OnError( errno, Error, "while creating a timer." )
00048 }
00049
00050 struct sigaction act;
00051 sigset_t set;
00052
00053 sigemptyset( &set );
00054 sigaddset( &set, TimerSignal );
00055
00056
00057 act.sa_flags = 0;
00058 act.sa_mask = set;
00059 act.sa_handler = wrapper;
00060
00061 sigaction( TimerSignal, &act, NULL );
00062
00063
00064 #if defined( __QNXNTO__ )
00065 last_cycles = 0;
00066 current_cycles = 0;
00067 cpu_freq = SYSPAGE_ENTRY( qtime )->cycles_per_sec;
00068
00069 clkper.nsec = 999847;
00070 clkper.fract = 0;
00071
00072 ClockPeriod( CLOCK_REALTIME, &clkper, NULL, 0 );
00073 #endif
00074
00075 }
00076
00077
00078 inline void Timer::start() {
00079
00080
00081 #if (__sun) || (__SUN__)
00082
00083 thr_setconcurrency( sysconf(_SC_NPROCESSORS_ONLN) + 3 );
00084
00085 #endif
00086
00087 startedTime = time( NULL );
00088
00089 timer_settime( timerid, 0, &t, NULL );
00090 }
00091
00092 inline _uint64 Timer::getCycles() {
00093 return ClockCycles();
00094 }
00095
00096 inline double Timer::getElapsed( _uint64 cycles )
00097 {
00098
00099
00100 current_cycles = ClockCycles();
00101
00102
00103 elapsedCycles = ( current_cycles - cycles ) / cpu_freq;
00104
00105 return elapsedCycles;
00106 }
00107
00108 inline double Timer::getSpent() {
00109 timer_gettime( timerid, &t );
00110
00111 register double dt = t.it_value.tv_nsec;
00112
00113 dt /= 1000000000ul;
00114 dt += t.it_value.tv_sec;
00115
00116 return dt;
00117 }
00118
00119 #endif