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
|
/*
* Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* You can also choose to distribute this program under the terms of
* the Unmodified Binary Distribution Licence (as given in the file
* COPYING.UBDL), provided that you have satisfied its requirements.
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <stdint.h>
#include <stdio.h>
#include <strings.h>
#include <assert.h>
#include <ipxe/isqrt.h>
#include <ipxe/profile.h>
/** @file
*
* Profiling
*
* The profiler computes basic statistics (mean, variance, and
* standard deviation) for the samples which it records. Note that
* these statistics need not be completely accurate; it is sufficient
* to give a rough approximation.
*
* The algorithm for updating the mean and variance estimators is from
* The Art of Computer Programming (via Wikipedia), with adjustments
* to avoid the use of floating-point instructions.
*/
/** Accumulated time excluded from profiling */
unsigned long profile_excluded;
/**
* Format a hex fraction (for debugging)
*
* @v value Value
* @v shift Bit shift
* @ret string Formatted hex fraction
*/
static const char * profile_hex_fraction ( signed long long value,
unsigned int shift ) {
static char buf[23] = "-"; /* -0xXXXXXXXXXXXXXXXX.XX + NUL */
unsigned long long int_part;
uint8_t frac_part;
char *ptr;
if ( value < 0 ) {
value = -value;
ptr = &buf[0];
} else {
ptr = &buf[1];
}
int_part = ( value >> shift );
frac_part = ( value >> ( shift - ( 8 * sizeof ( frac_part ) ) ) );
snprintf ( &buf[1], ( sizeof ( buf ) - 1 ), "%#llx.%02x",
int_part, frac_part );
return ptr;
}
/**
* Calculate bit shift for mean sample value
*
* @v profiler Profiler
* @ret shift Bit shift
*/
static inline unsigned int profile_mean_shift ( struct profiler *profiler ) {
return ( ( ( 8 * sizeof ( profiler->mean ) ) - 1 ) /* MSB */
- 1 /* Leave sign bit unused */
- profiler->mean_msb );
}
/**
* Calculate bit shift for accumulated variance value
*
* @v profiler Profiler
* @ret shift Bit shift
*/
static inline unsigned int profile_accvar_shift ( struct profiler *profiler ) {
return ( ( ( 8 * sizeof ( profiler->accvar ) ) - 1 ) /* MSB */
- 1 /* Leave top bit unused */
- profiler->accvar_msb );
}
/**
* Update profiler with a new sample
*
* @v profiler Profiler
* @v sample Sample value
*/
void profile_update ( struct profiler *profiler, unsigned long sample ) {
unsigned int sample_msb;
unsigned int mean_shift;
unsigned int delta_shift;
signed long pre_delta;
signed long post_delta;
signed long long accvar_delta;
unsigned int accvar_delta_shift;
unsigned int accvar_delta_msb;
unsigned int accvar_shift;
/* Our scaling logic assumes that sample values never overflow
* a signed long (i.e. that the high bit is always zero).
*/
assert ( ( ( signed ) sample ) >= 0 );
/* Update sample count */
profiler->count++;
/* Adjust mean sample value scale if necessary. Skip if
* sample is zero (in which case flsl(sample)-1 would
* underflow): in the case of a zero sample we have no need to
* adjust the scale anyway.
*/
if ( sample ) {
sample_msb = ( flsl ( sample ) - 1 );
if ( profiler->mean_msb < sample_msb ) {
profiler->mean >>= ( sample_msb - profiler->mean_msb );
profiler->mean_msb = sample_msb;
}
}
/* Scale sample to internal units */
mean_shift = profile_mean_shift ( profiler );
sample <<= mean_shift;
/* Update mean */
pre_delta = ( sample - profiler->mean );
profiler->mean += ( pre_delta / ( ( signed ) profiler->count ) );
post_delta = ( sample - profiler->mean );
delta_shift = mean_shift;
DBGC ( profiler, "PROFILER %p sample %#lx mean %s", profiler,
( sample >> mean_shift ),
profile_hex_fraction ( profiler->mean, mean_shift ) );
DBGC ( profiler, " pre %s",
profile_hex_fraction ( pre_delta, delta_shift ) );
DBGC ( profiler, " post %s\n",
profile_hex_fraction ( post_delta, delta_shift ) );
/* Scale both deltas to fit in half of an unsigned long long
* to avoid potential overflow on multiplication. Note that
* shifting a signed quantity is "implementation-defined"
* behaviour in the C standard, but gcc documents that it will
* always perform sign extension.
*/
if ( sizeof ( pre_delta ) > ( sizeof ( accvar_delta ) / 2 ) ) {
unsigned int shift = ( 8 * ( sizeof ( pre_delta ) -
( sizeof ( accvar_delta ) / 2 ) ));
pre_delta >>= shift;
post_delta >>= shift;
delta_shift -= shift;
}
/* Update variance, if applicable. Skip if either delta is
* zero (in which case flsl(delta)-1 would underflow): in the
* case of a zero delta there is no change to the accumulated
* variance anyway.
*/
if ( pre_delta && post_delta ) {
/* Calculate variance delta */
accvar_delta = ( ( ( signed long long ) pre_delta ) *
( ( signed long long ) post_delta ) );
accvar_delta_shift = ( 2 * delta_shift );
assert ( accvar_delta > 0 );
/* Calculate variance delta MSB, using flsl() on each
* delta individually to provide an upper bound rather
* than requiring the existence of flsll().
*/
accvar_delta_msb = ( flsll ( accvar_delta ) - 1 );
if ( accvar_delta_msb > accvar_delta_shift ) {
accvar_delta_msb -= accvar_delta_shift;
} else {
accvar_delta_msb = 0;
}
/* Adjust scales as necessary */
if ( profiler->accvar_msb < accvar_delta_msb ) {
/* Rescale accumulated variance */
profiler->accvar >>= ( accvar_delta_msb -
profiler->accvar_msb );
profiler->accvar_msb = accvar_delta_msb;
} else {
/* Rescale variance delta */
accvar_delta >>= ( profiler->accvar_msb -
accvar_delta_msb );
accvar_delta_shift -= ( profiler->accvar_msb -
accvar_delta_msb );
}
/* Scale delta to internal units */
accvar_shift = profile_accvar_shift ( profiler );
accvar_delta <<= ( accvar_shift - accvar_delta_shift );
/* Accumulate variance */
profiler->accvar += accvar_delta;
/* Adjust scale if necessary */
if ( profiler->accvar &
( 1ULL << ( ( 8 * sizeof ( profiler->accvar ) ) - 1 ) ) ) {
profiler->accvar >>= 1;
profiler->accvar_msb++;
accvar_delta >>= 1;
accvar_shift--;
}
DBGC ( profiler, "PROFILER %p accvar %s", profiler,
profile_hex_fraction ( profiler->accvar, accvar_shift ));
DBGC ( profiler, " delta %s\n",
profile_hex_fraction ( accvar_delta, accvar_shift ) );
}
}
/**
* Get mean sample value
*
* @v profiler Profiler
* @ret mean Mean sample value
*/
unsigned long profile_mean ( struct profiler *profiler ) {
unsigned int mean_shift = profile_mean_shift ( profiler );
/* Round to nearest and scale down to original units */
return ( ( profiler->mean + ( 1UL << ( mean_shift - 1 ) ) )
>> mean_shift );
}
/**
* Get sample variance
*
* @v profiler Profiler
* @ret variance Sample variance
*/
unsigned long profile_variance ( struct profiler *profiler ) {
unsigned int accvar_shift = profile_accvar_shift ( profiler );
/* Variance is zero if fewer than two samples exist (avoiding
* division by zero error).
*/
if ( profiler->count < 2 )
return 0;
/* Calculate variance, round to nearest, and scale to original units */
return ( ( ( profiler->accvar / ( profiler->count - 1 ) )
+ ( 1ULL << ( accvar_shift - 1 ) ) ) >> accvar_shift );
}
/**
* Get sample standard deviation
*
* @v profiler Profiler
* @ret stddev Sample standard deviation
*/
unsigned long profile_stddev ( struct profiler *profiler ) {
return isqrt ( profile_variance ( profiler ) );
}
|