summaryrefslogtreecommitdiff
path: root/portable/src/pcputimer.c
blob: 33384750ddad5072ab267a32e7f07c5797a9a355 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*---------------------------------------------------------------------------*
 *  pcputimer.c  *
 *                                                                           *
 *  Copyright 2007, 2008 Nuance Communciations, Inc.                               *
 *                                                                           *
 *  Licensed under the Apache License, Version 2.0 (the 'License');          *
 *  you may not use this file except in compliance with the License.         *
 *                                                                           *
 *  You may obtain a copy of the License at                                  *
 *      http://www.apache.org/licenses/LICENSE-2.0                           *
 *                                                                           *
 *  Unless required by applicable law or agreed to in writing, software      *
 *  distributed under the License is distributed on an 'AS IS' BASIS,        *
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 
 *  See the License for the specific language governing permissions and      *
 *  limitations under the License.                                           *
 *                                                                           *
 *---------------------------------------------------------------------------*/



#include "pcputimer.h"
#include "pmemory.h"

#if defined(_WIN32)

/*
  Note that this implementation assumes that GetThreadTimes is
  available (requires NT 3.5 and above) and that 64 bit arithmetic is
  available (requires VC)
*/

struct PCPUTimer_t
{
  HANDLE hThread;
  LARGE_INTEGER RefTime;
  asr_uint32_t elapsed;
};


/**
 * Creates a new timer object.
 **/
ESR_ReturnCode PCPUTimerCreate(PCPUTimer **timer)
{
  PCPUTimer *tmp = NULL;
  
  if (timer == NULL)
    return ESR_INVALID_ARGUMENT;
  tmp = NEW(PCPUTimer, "PCPUTimer");
  if (tmp == NULL) return ESR_OUT_OF_MEMORY;
  
  tmp->hThread = GetCurrentThread();
  tmp->RefTime.QuadPart = -1;
  tmp->elapsed = 0;
  *timer = tmp;
  
  return ESR_SUCCESS;
}

ESR_ReturnCode PCPUTimerDestroy(PCPUTimer *timer)
{
  if (timer == NULL) return ESR_INVALID_ARGUMENT;
  FREE(timer);
  return ESR_SUCCESS;
}

/**
 * Starts the timer. This sets the reference time from which all new elapsed
 * time are computed.  This does not reset the elapsed time to 0.  This is
 * useful to pause the timer.
 **/
ESR_ReturnCode PCPUTimerStart(PCPUTimer *timer)
{
  FILETIME CreationTime;
  FILETIME ExitTime;
  FILETIME KernelTime;
  FILETIME UserTime;
  
  if (timer == NULL) return ESR_INVALID_ARGUMENT;
  if (!GetThreadTimes(timer->hThread,
                      &CreationTime, &ExitTime, &KernelTime, &UserTime))
  {
    return ESR_FATAL_ERROR;
  }
  
  timer->RefTime.QuadPart = (((LARGE_INTEGER*) & KernelTime)->QuadPart +
                             ((LARGE_INTEGER*) & UserTime)->QuadPart);
                             
  return ESR_SUCCESS;
}

/**
 * Stops the timer.
 **/
ESR_ReturnCode PCPUTimerStop(PCPUTimer *timer)
{
  if (timer == NULL) return ESR_INVALID_ARGUMENT;
  if (timer->RefTime.QuadPart != -1)
  {
    FILETIME CreationTime;
    FILETIME ExitTime;
    FILETIME KernelTime;
    FILETIME UserTime;
    
    if (!GetThreadTimes(timer->hThread,
                        &CreationTime, &ExitTime, &KernelTime, &UserTime))
      return ESR_FATAL_ERROR;
      
    timer->elapsed =
      (asr_uint32_t) (((LARGE_INTEGER*) &KernelTime)->QuadPart +
                  ((LARGE_INTEGER*) &UserTime)->QuadPart -
                  timer->RefTime.QuadPart) / 10;
  }
  return ESR_SUCCESS;
}

/**
 * Returns the timer elapsed time.  If the Timer is in the stopped state,
 * successive calls to getElapsed() will always return the same value.  If
 * the Timer is in the started state, successive calls will return the
 * elapsed time since the last time PCPUTimerStart() was called.
 */
ESR_ReturnCode PCPUTimerGetElapsed(PCPUTimer *timer, asr_uint32_t *elapsed)
{
  if (timer == NULL || elapsed == NULL) return ESR_INVALID_ARGUMENT;
  if (timer->RefTime.QuadPart != -1)
  {
    FILETIME CreationTime;
    FILETIME ExitTime;
    FILETIME KernelTime;
    FILETIME UserTime;
    
    if (!GetThreadTimes(timer->hThread,
                        &CreationTime, &ExitTime, &KernelTime, &UserTime))
      return ESR_FATAL_ERROR;
      
    *elapsed = timer->elapsed +
               (asr_uint32_t)(((LARGE_INTEGER*) & KernelTime)->QuadPart +
                              ((LARGE_INTEGER*) & UserTime)->QuadPart -
                              timer->RefTime.QuadPart) / 10;
  }
  else
    *elapsed = timer->elapsed;
  return ESR_SUCCESS;
}


/**
 * Resets the elapsed time to 0 and resets the reference time of the Timer.
 * This effectively reset the timer in the same state it was right after creation.
 **/
ESR_ReturnCode PCPUTimerReset(PCPUTimer *timer)
{
  if (timer == NULL) return ESR_INVALID_ARGUMENT;
  timer->RefTime.QuadPart = -1;
  timer->elapsed = 0;
  return ESR_SUCCESS;
}

#elif defined(POSIX)
/*
*/

struct PCPUTimer_t
{
  HANDLE   hThread;
  asr_uint32_t RefTime;
  asr_uint32_t elapsed;
};

/**
* Creates a new timer object.
**/
ESR_ReturnCode PCPUTimerCreate(PCPUTimer **timer)
{
  PCPUTimer *tmp = NULL;

  if (timer == NULL) return ESR_INVALID_ARGUMENT;
  tmp = NEW(PCPUTimer, "PCPUTimer");
  if (tmp == NULL) return ESR_OUT_OF_MEMORY;

  tmp->hThread = (HANDLE)pthread_self();
  tmp->elapsed = 0;
  *timer = tmp;

  return ESR_SUCCESS;
}

ESR_ReturnCode PCPUTimerDestroy(PCPUTimer *timer)
{
  if (timer == NULL) return ESR_INVALID_ARGUMENT;
  FREE(timer);
  return ESR_SUCCESS;
}

/**
* Starts the timer. This sets the reference time from which all new elapsed
* time are computed.  This does not reset the elapsed time to 0.  This is
* useful to pause the timer.
**/
ESR_ReturnCode PCPUTimerStart(PCPUTimer *timer)
{
  return ESR_SUCCESS;
}

/**
* Stops the timer.
**/
ESR_ReturnCode PCPUTimerStop(PCPUTimer *timer)
{
  return ESR_SUCCESS;
}

/**
* Returns the timer elapsed time.  If the Timer is in the stopped state,
* successive calls to getElapsed() will always return the same value.  If
* the Timer is in the started state, successive calls will return the
* elapsed time since the last time PCPUTimerStart() was called.
*/
ESR_ReturnCode PCPUTimerGetElapsed(PCPUTimer *timer, asr_uint32_t *elapsed)
{
  return ESR_SUCCESS;
}


/**
* Resets the elapsed time to 0 and resets the reference time of the Timer.
* This effectively reset the timer in the same state it was right after creation.
**/
ESR_ReturnCode PCPUTimerReset(PCPUTimer *timer)
{
  return ESR_SUCCESS;
}

#else
/* #error "Ptimer not implemented for this platform." */
#endif