15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
**
*******************************************************************************
**
** This file contains code for miscellaneous utility routines.
*/
#include "config.h"
#include "util.h"
/*
** Exit. Take care to close the database first.
*/
NORETURN void fossil_exit(int rc){
db_close(1);
exit(rc);
|
>
>
>
>
>
>
>
>
>
>
>
|
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
|
**
*******************************************************************************
**
** This file contains code for miscellaneous utility routines.
*/
#include "config.h"
#include "util.h"
/*
** For the fossil_timer_xxx() family of functions...
*/
#ifdef _WIN32
# include <windows.h>
#else
# include <sys/time.h>
# include <sys/resource.h>
#endif
/*
** Exit. Take care to close the database first.
*/
NORETURN void fossil_exit(int rc){
db_close(1);
exit(rc);
|
132
133
134
135
136
137
138
|
return +1;
}
nByte = strlen(zB);
rc = sqlite3_strnicmp(zA, zB, nByte);
if( rc==0 && zA[nByte] ) rc = 1;
return rc;
}
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
return +1;
}
nByte = strlen(zB);
rc = sqlite3_strnicmp(zA, zB, nByte);
if( rc==0 && zA[nByte] ) rc = 1;
return rc;
}
/*
** Get user and kernel times in microseconds.
*/
void fossil_cpu_times(sqlite3_uint64 *piUser, sqlite3_uint64 *piKernel){
#ifdef _WIN32
FILETIME not_used;
FILETIME kernel_time;
FILETIME user_time;
GetProcessTimes(GetCurrentProcess(), ¬_used, ¬_used,
&kernel_time, &user_time);
if( piUser ){
*piUser = ((((sqlite3_uint64)user_time.dwHighDateTime)<<32) +
(sqlite3_uint64)user_time.dwLowDateTime + 5)/10;
}
if( piKernel ){
*piKernel = ((((sqlite3_uint64)kernel_time.dwHighDateTime)<<32) +
(sqlite3_uint64)kernel_time.dwLowDateTime + 5)/10;
}
#else
struct rusage s;
getrusage(RUSAGE_SELF, &s);
if( piUser ){
*piUser = ((sqlite3_uint64)s.ru_utime.tv_sec)*1000000 + s.ru_utime.tv_usec;
}
if( piKernel ){
*piKernel =
((sqlite3_uint64)s.ru_stime.tv_sec)*1000000 + s.ru_stime.tv_usec;
}
#endif
}
/*
** Internal helper type for fossil_timer_xxx().
*/
static struct FossilTimer {
sqlite3_uint64 u; /* "User" CPU times */
sqlite3_uint64 s; /* "System" CPU times */
char used; /* 1 if timer is allocated, else 0. */
} fossilTimer = { 0U, 0U, 0 };
enum FossilTimerEnum {
FOSSIL_TIMER_COUNT = 10 /* Number of timers we can stack. */
};
static struct FossilTimer fossilTimerList[FOSSIL_TIMER_COUNT] = {{0,0,0}};
/*
** Stores the current CPU times into the shared timer list
** and returns that timer's internal ID. Pass that ID to
** fossil_timer_fetch() to get the elapsed time for that
** timer.
**
** The system has a fixed number of timers, and they can be
** "deallocated" by passing this function's return value to
** fossil_timer_stop() Adjust FOSSIL_TIMER_COUNT to set the number of
** available timers.
**
** Returns -1 on error (no more timers available), with 0 or greater
** being valid timer IDs.
*/
int fossil_timer_start(){
int i;
static char once = 0;
if(!once){
memset(&fossilTimerList, 0,
sizeof(fossilTimerList)/sizeof(fossilTimerList[0]));
}
for( i = 0; i < FOSSIL_TIMER_COUNT; ++i ){
struct FossilTimer * ft = &fossilTimerList[i];
if(ft->used) continue;
ft->used = 1;
fossil_cpu_times( &ft->u, &ft->s );
break;
}
return (i<FOSSIL_TIMER_COUNT) ? i : -1;
}
/*
** Returns the difference in CPU times in microseconds since
** fossil_timer_start() was called and returned the given timer ID (or
** since it was last reset). Returns 0 if timerId is out of range.
*/
sqlite3_uint64 fossil_timer_fetch(int timerId){
if(timerId<0 || timerId>=FOSSIL_TIMER_COUNT){
return 0;
}else{
struct FossilTimer * start = &fossilTimerList[timerId];
if( ! start->used ){
fossil_fatal("Invalid call to reset a non-allocated "
"timer (#%d)", timerId);
/*NOTREACHED*/
}else{
sqlite3_uint64 eu = 0, es = 0;
fossil_cpu_times( &eu, &es );
return (eu - start->u) + (es - start->s);
}
}
}
/*
** Resets the timer associated with the given ID, as obtained via
** fossil_timer_start(), to the current CPU time values.
*/
sqlite3_uint64 fossil_timer_reset(int timerId){
if(timerId<0 || timerId>=FOSSIL_TIMER_COUNT){
return 0;
}else{
struct FossilTimer * start = &fossilTimerList[timerId];
if( ! start->used ){
fossil_fatal("Invalid call to reset a non-allocated "
"timer (#%d)", timerId);
/*NOTREACHED*/
}else{
sqlite3_uint64 const rc = fossil_timer_fetch(timerId);
fossil_cpu_times( &start->u, &start->s );
return rc;
}
}
}
/**
"Deallocates" the fossil timer identified by the given timer ID.
returns the difference (in uSec) between the last time that timer
was started or reset. Returns 0 if timerId is out of range. It is
not legal to re-use the passed-in timerId after calling this
until/unless it is re-initialized using fossil_timer_start() (NOT
fossil_timer_reset()).
*/
sqlite3_uint64 fossil_timer_stop(int timerId){
if(timerId<0 || timerId>=FOSSIL_TIMER_COUNT){
return 0;
}else{
sqlite3_uint64 const rc = fossil_timer_fetch(timerId);
struct FossilTimer * t = &fossilTimerList[timerId];
t->used = 0;
t->u = t->s = 0U;
return rc;
}
}
/*
** Returns 1 if fossil has timer capabilities.
*/
int fossil_has_timer(){
return 1;
}
|