-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc_sup.c
More file actions
290 lines (199 loc) · 6.63 KB
/
misc_sup.c
File metadata and controls
290 lines (199 loc) · 6.63 KB
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
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
#include "mysccs.h"
SCCSID("@(#)$Id: //tools/src/freeware/gsskrb5/misc_sup.c#2 $")
/************************************************************************
* $Id: //tools/src/freeware/gsskrb5/misc_sup.c#2 $
************************************************************************
*
* Copyright (c) 1997-2000 SAP AG. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by SAP AG"
*
* 4. The name "SAP AG" must not be used to endorse or promote products
* derived from this software without prior written permission.
* For written permission, please contact www.press@sap.com
*
* 5. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by SAP AG"
*
* THIS SOFTWARE IS PROVIDED BY SAP AG ``AS IS'' AND ANY EXPRESSED
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. SAP AG SHALL BE LIABLE FOR ANY DAMAGES
* ARISING OUT OF THE USE OF THIS SOFTWARE ONLY IF CAUSED BY SAP AG'S
* INTENT OR GROSS NEGLIGENCE. IN CASE SAP AG IS LIABLE UNDER THIS
* AGREEMENT FOR DAMAGES CAUSED BY SAP AG'S GROSS NEGLIGENCE SAP AG
* FURTHER SHALL NOT BE LIABLE FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT, AND SHALL NOT BE LIABLE IN EXCESS OF THE AMOUNT OF
* DAMAGES TYPICALLY FORESEEABLE FOR SAP AG, WHICH SHALL IN NO EVENT
* EXCEED US$ 500.000.-
*
************************************************************************/
/* misc support functions (possibly platform dependent) */
#include "test_api.h"
#include <time.h>
#ifdef HAVE_GETTIMEOFDAY
#include <sys/time.h>
#define TIMER_TYPE "gettimeofday()"
static struct timeval start_time = { 0, 0 };
void
init_timer( void )
{
return;
} /* init_timer() */
void
start_timer( void )
{
gettimeofday( &start_time, NULL );
return;
} /* start_timer() */
unsigned long
read_timer( void )
{
unsigned long diff;
struct timeval now;
gettimeofday( &now , NULL );
if ( start_time.tv_sec == 0 )
return (0);
diff = (now.tv_sec - start_time.tv_sec );
if ( diff> ULONG_MAX/1000000 ) {
return(ULONG_MAX);
}
diff *= 1000000;
diff += (now.tv_usec - start_time.tv_usec);
return(diff);
} /* read_timer() */
#else /* !HAVE_GETTIMEOFDAY */
# ifdef _WIN32
#define TIMER_TYPE "QueryPerformanceCounter()"
static LARGE_INTEGER PerfFrequency;
static LARGE_INTEGER StartCount;
void
init_timer( void )
{
QueryPerformanceFrequency( &PerfFrequency );
QueryPerformanceCounter( &StartCount );
return;
} /* init_timer() */
void
start_timer( void )
{
QueryPerformanceCounter( &StartCount );
return;
} /* start_timer() */
unsigned long
read_timer( void )
{
LARGE_INTEGER now;
float diff;
float xdiff;
QueryPerformanceCounter( &now );
if (PerfFrequency.QuadPart == 0)
return(0);
xdiff = ((float) (now.QuadPart - StartCount.QuadPart)) * 1000000;
diff = xdiff / (float)PerfFrequency.QuadPart;
if ( diff > (float)UINT_MAX )
return( UINT_MAX );
return((unsigned long)diff);
} /* read_timer() */
# endif /* !_WIN32 */
#endif /* !HAVE_GETTIMEOFDAY */
char *
sprint_timer( unsigned long timer_val )
{
unsigned long msec, usec;
static char timespan[128];
timespan[0] = '\0';
if ( timer_val==0 ) {
return("unknown timespan");
}
usec = timer_val % 1000;
msec = (timer_val - usec) / 1000;
sprintf(timespan, "%6lu.%03lu msec", msec, usec);
return(timespan);
} /* print_timer() */
static int
sample_compare( const void * s1, const void * s2 )
{
return( (*( (Ulong *)s1)) - (*( (Ulong *)s2)) );
} /* sample_compare() */
/*
* sample_avg()
*
* sort the sample[] array of elapsed time numerically
* and calculate an average time over sample[],
* ignoring
* the worst 50% of the results because of context switches
* the best 25% because of insufficient timer resolution on a few platforms
*/
Ulong
sample_avg( Ulong * p_sample, size_t p_nelements )
{
Uint start = (Uint)p_nelements/4; /* 25% */
Uint stop = start * 2; /* 50% */
Uint i;
Ulong sum, avg, count;
qsort( (void *)p_sample, p_nelements, sizeof(p_sample[0]), sample_compare );
sum = count = 0;
for ( count=0, i=start ; i<stop ; i++ ) {
sum += p_sample[i];
count++;
}
avg = (sum + count/2) / count;
return(avg);
} /* sample_avg() */
void
show_timer_resolution( void )
{
time_t now, start;
unsigned long val, newval, avg;
unsigned long i;
Ulong vals[16];
init_timer();
now = time(NULL);
while ( now==time(NULL) );
start_timer();
val = read_timer();
for ( i=0 ; i<ARRAY_ELEMENTS(vals) ; i++ ) {
do {
newval = read_timer();
} while ( val == newval );
vals[i] = (newval - val);
val = newval;
}
avg = sample_avg(vals, ARRAY_ELEMENTS(vals));
fprintf(fh, "Timer resolution of %s is (at least) %s\n",
TIMER_TYPE, sprint_timer(avg) );
start = time(NULL);
do {
now = time(NULL);
} while ( start == now );
start_timer();
while ( time(NULL)==now );
val = read_timer();
if ( val<950000 || val>1050000 ) {
fprintf(fh, "Whoa! timer resolution wrong??\n");
fprintf(fh, "1 second passed in %s ?!\n\n", sprint_timer(val) );
} else {
fprintf(fh, "1 second passed in %s. \n\n", sprint_timer(val) );
}
return;
} /* show_timer_resolution() */