1 |
|
---|
2 | #ifndef WINDOWS
|
---|
3 | #include <stddef.h>
|
---|
4 | #include <fcntl.h>
|
---|
5 | #include <sys/time.h>
|
---|
6 | #include <sys/types.h>
|
---|
7 | #include <sys/mman.h>
|
---|
8 | #if defined( IRIX ) || defined( IRIX64 )
|
---|
9 | #include <sys/syssgi.h>
|
---|
10 | #endif
|
---|
11 | #include <sys/errno.h>
|
---|
12 | #include <unistd.h>
|
---|
13 | #else
|
---|
14 | #define WIN32_LEAN_AND_MEAN
|
---|
15 | #include <windows.h>
|
---|
16 | #endif
|
---|
17 |
|
---|
18 | #include "cr_timer.h"
|
---|
19 | #include "cr_mem.h"
|
---|
20 | #include "cr_error.h"
|
---|
21 |
|
---|
22 | static double crTimerGetTime( CRTimer *t )
|
---|
23 | {
|
---|
24 | #if defined( IRIX ) || defined( IRIX64 )
|
---|
25 | if (t->iotimer_addr64) {
|
---|
26 | volatile iotimer64_t counter_value;
|
---|
27 | counter_value = *(t->iotimer_addr64);
|
---|
28 | return ((double) counter_value * .000000000001) * (double) t->cycleval;
|
---|
29 | }
|
---|
30 | else {
|
---|
31 | volatile iotimer32_t counter_value;
|
---|
32 | counter_value = *(t->iotimer_addr32);
|
---|
33 | return ((double) counter_value * .000000000001) * (double) t->cycleval;
|
---|
34 | }
|
---|
35 | #elif defined( WINDOWS )
|
---|
36 | QueryPerformanceCounter( &t->performance_counter );
|
---|
37 | return ((double) t->performance_counter.QuadPart)*t->one_over_frequency;
|
---|
38 | #elif defined( Linux ) || defined( FreeBSD ) || defined(DARWIN) || defined(AIX) || defined(SunOS) || defined(OSF1)
|
---|
39 | gettimeofday( &t->timeofday, NULL );
|
---|
40 | return t->timeofday.tv_sec + t->timeofday.tv_usec / 1000000.0;
|
---|
41 | #else
|
---|
42 | #error TIMERS
|
---|
43 | #endif
|
---|
44 | }
|
---|
45 |
|
---|
46 | CRTimer *crTimerNewTimer( void )
|
---|
47 | {
|
---|
48 | CRTimer *t = (CRTimer *) crAlloc( sizeof(*t) );
|
---|
49 | #if defined(IRIX) || defined( IRIX64 )
|
---|
50 | __psunsigned_t phys_addr, raddr;
|
---|
51 | int poffmask = getpagesize() - 1;
|
---|
52 | int counterSize = syssgi(SGI_CYCLECNTR_SIZE);
|
---|
53 |
|
---|
54 | phys_addr = syssgi(SGI_QUERY_CYCLECNTR, &(t->cycleval));
|
---|
55 | if (phys_addr == ENODEV) {
|
---|
56 | crError( "Sorry, this SGI doesn't support timers." );
|
---|
57 | }
|
---|
58 |
|
---|
59 | raddr = phys_addr & ~poffmask;
|
---|
60 | t->fd = open("/dev/mmem", O_RDONLY);
|
---|
61 |
|
---|
62 | if (counterSize == 64) {
|
---|
63 | t->iotimer_addr64 =
|
---|
64 | (volatile iotimer64_t *)mmap(0, poffmask, PROT_READ,
|
---|
65 | MAP_PRIVATE, t->fd, (off_t)raddr);
|
---|
66 | t->unmapLocation = (void *)t->iotimer_addr64;
|
---|
67 | t->unmapSize = poffmask;
|
---|
68 | t->iotimer_addr64 = (iotimer64_t *)((__psunsigned_t)t->iotimer_addr64 +
|
---|
69 | (phys_addr & poffmask));
|
---|
70 | }
|
---|
71 | else if (counterSize == 32) {
|
---|
72 | t->iotimer_addr32 = (volatile iotimer32_t *)mmap(0, poffmask, PROT_READ,
|
---|
73 | MAP_PRIVATE, t->fd,
|
---|
74 | (off_t)raddr);
|
---|
75 | t->unmapLocation = (void *)t->iotimer_addr32;
|
---|
76 | t->unmapSize = poffmask;
|
---|
77 | t->iotimer_addr32 = (iotimer32_t *)((__psunsigned_t)t->iotimer_addr32 +
|
---|
78 | (phys_addr & poffmask));
|
---|
79 | }
|
---|
80 | else {
|
---|
81 | crError( "Fatal timer init error" );
|
---|
82 | }
|
---|
83 | #elif defined( WINDOWS )
|
---|
84 | QueryPerformanceFrequency( &t->performance_frequency );
|
---|
85 | t->one_over_frequency = 1.0/((double)t->performance_frequency.QuadPart);
|
---|
86 | #endif
|
---|
87 | t->time0 = t->elapsed = 0;
|
---|
88 | t->running = 0;
|
---|
89 | return t;
|
---|
90 | }
|
---|
91 |
|
---|
92 | void crDestroyTimer( CRTimer *t )
|
---|
93 | {
|
---|
94 | #if defined( IRIX ) || defined( IRIX64 )
|
---|
95 | close( t->fd );
|
---|
96 | #endif
|
---|
97 | crFree( t );
|
---|
98 | }
|
---|
99 |
|
---|
100 | void crStartTimer( CRTimer *t )
|
---|
101 | {
|
---|
102 | t->running = 1;
|
---|
103 | t->time0 = crTimerGetTime( t );
|
---|
104 | }
|
---|
105 |
|
---|
106 | void crStopTimer( CRTimer *t )
|
---|
107 | {
|
---|
108 | t->running = 0;
|
---|
109 | t->elapsed += crTimerGetTime( t ) - t->time0;
|
---|
110 | }
|
---|
111 |
|
---|
112 | void crResetTimer( CRTimer *t )
|
---|
113 | {
|
---|
114 | t->running = 0;
|
---|
115 | t->elapsed = 0;
|
---|
116 | }
|
---|
117 |
|
---|
118 | double crTimerTime( CRTimer *t )
|
---|
119 | {
|
---|
120 | if (t->running) {
|
---|
121 | crStopTimer( t );
|
---|
122 | crStartTimer( t );
|
---|
123 | }
|
---|
124 | return t->elapsed;
|
---|
125 | }
|
---|