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