2 * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
26 #include <ipxe/isqrt.h>
27 #include <ipxe/profile.h>
33 * The profiler computes basic statistics (mean, variance, and
34 * standard deviation) for the samples which it records. Note that
35 * these statistics need not be completely accurate; it is sufficient
36 * to give a rough approximation.
38 * The algorithm for updating the mean and variance estimators is from
39 * The Art of Computer Programming (via Wikipedia), with adjustments
40 * to avoid the use of floating-point instructions.
43 /** Accumulated time excluded from profiling */
44 unsigned long profile_excluded;
47 * Format a hex fraction (for debugging)
51 * @ret string Formatted hex fraction
53 static const char * profile_hex_fraction ( signed long long value,
54 unsigned int shift ) {
55 static char buf[23] = "-"; /* -0xXXXXXXXXXXXXXXXX.XX + NUL */
56 unsigned long long int_part;
66 int_part = ( value >> shift );
67 frac_part = ( value >> ( shift - ( 8 * sizeof ( frac_part ) ) ) );
68 snprintf ( &buf[1], ( sizeof ( buf ) - 1 ), "%#llx.%02x",
69 int_part, frac_part );
74 * Calculate bit shift for mean sample value
76 * @v profiler Profiler
77 * @ret shift Bit shift
79 static inline unsigned int profile_mean_shift ( struct profiler *profiler ) {
81 return ( ( ( 8 * sizeof ( profiler->mean ) ) - 1 ) /* MSB */
82 - 1 /* Leave sign bit unused */
83 - profiler->mean_msb );
87 * Calculate bit shift for accumulated variance value
89 * @v profiler Profiler
90 * @ret shift Bit shift
92 static inline unsigned int profile_accvar_shift ( struct profiler *profiler ) {
94 return ( ( ( 8 * sizeof ( profiler->accvar ) ) - 1 ) /* MSB */
95 - 1 /* Leave top bit unused */
96 - profiler->accvar_msb );
100 * Update profiler with a new sample
102 * @v profiler Profiler
103 * @v sample Sample value
105 void profile_update ( struct profiler *profiler, unsigned long sample ) {
106 unsigned int sample_msb;
107 unsigned int mean_shift;
108 unsigned int delta_shift;
109 signed long pre_delta;
110 signed long post_delta;
111 signed long long accvar_delta;
112 unsigned int accvar_delta_shift;
113 unsigned int accvar_delta_msb;
114 unsigned int accvar_shift;
116 /* Our scaling logic assumes that sample values never overflow
117 * a signed long (i.e. that the high bit is always zero).
119 assert ( ( ( signed ) sample ) >= 0 );
121 /* Update sample count */
124 /* Adjust mean sample value scale if necessary. Skip if
125 * sample is zero (in which case flsl(sample)-1 would
126 * underflow): in the case of a zero sample we have no need to
127 * adjust the scale anyway.
130 sample_msb = ( flsl ( sample ) - 1 );
131 if ( profiler->mean_msb < sample_msb ) {
132 profiler->mean >>= ( sample_msb - profiler->mean_msb );
133 profiler->mean_msb = sample_msb;
137 /* Scale sample to internal units */
138 mean_shift = profile_mean_shift ( profiler );
139 sample <<= mean_shift;
142 pre_delta = ( sample - profiler->mean );
143 profiler->mean += ( pre_delta / ( ( signed ) profiler->count ) );
144 post_delta = ( sample - profiler->mean );
145 delta_shift = mean_shift;
146 DBGC ( profiler, "PROFILER %p sample %#lx mean %s", profiler,
147 ( sample >> mean_shift ),
148 profile_hex_fraction ( profiler->mean, mean_shift ) );
149 DBGC ( profiler, " pre %s",
150 profile_hex_fraction ( pre_delta, delta_shift ) );
151 DBGC ( profiler, " post %s\n",
152 profile_hex_fraction ( post_delta, delta_shift ) );
154 /* Scale both deltas to fit in half of an unsigned long long
155 * to avoid potential overflow on multiplication. Note that
156 * shifting a signed quantity is "implementation-defined"
157 * behaviour in the C standard, but gcc documents that it will
158 * always perform sign extension.
160 if ( sizeof ( pre_delta ) > ( sizeof ( accvar_delta ) / 2 ) ) {
161 unsigned int shift = ( 8 * ( sizeof ( pre_delta ) -
162 ( sizeof ( accvar_delta ) / 2 ) ));
164 post_delta >>= shift;
165 delta_shift -= shift;
168 /* Update variance, if applicable. Skip if either delta is
169 * zero (in which case flsl(delta)-1 would underflow): in the
170 * case of a zero delta there is no change to the accumulated
173 if ( pre_delta && post_delta ) {
175 /* Calculate variance delta */
176 accvar_delta = ( ( ( signed long long ) pre_delta ) *
177 ( ( signed long long ) post_delta ) );
178 accvar_delta_shift = ( 2 * delta_shift );
179 assert ( accvar_delta > 0 );
181 /* Calculate variance delta MSB, using flsl() on each
182 * delta individually to provide an upper bound rather
183 * than requiring the existence of flsll().
185 accvar_delta_msb = ( flsll ( accvar_delta ) - 1 );
186 if ( accvar_delta_msb > accvar_delta_shift ) {
187 accvar_delta_msb -= accvar_delta_shift;
189 accvar_delta_msb = 0;
192 /* Adjust scales as necessary */
193 if ( profiler->accvar_msb < accvar_delta_msb ) {
194 /* Rescale accumulated variance */
195 profiler->accvar >>= ( accvar_delta_msb -
196 profiler->accvar_msb );
197 profiler->accvar_msb = accvar_delta_msb;
199 /* Rescale variance delta */
200 accvar_delta >>= ( profiler->accvar_msb -
202 accvar_delta_shift -= ( profiler->accvar_msb -
206 /* Scale delta to internal units */
207 accvar_shift = profile_accvar_shift ( profiler );
208 accvar_delta <<= ( accvar_shift - accvar_delta_shift );
210 /* Accumulate variance */
211 profiler->accvar += accvar_delta;
213 /* Adjust scale if necessary */
214 if ( profiler->accvar &
215 ( 1ULL << ( ( 8 * sizeof ( profiler->accvar ) ) - 1 ) ) ) {
216 profiler->accvar >>= 1;
217 profiler->accvar_msb++;
222 DBGC ( profiler, "PROFILER %p accvar %s", profiler,
223 profile_hex_fraction ( profiler->accvar, accvar_shift ));
224 DBGC ( profiler, " delta %s\n",
225 profile_hex_fraction ( accvar_delta, accvar_shift ) );
230 * Get mean sample value
232 * @v profiler Profiler
233 * @ret mean Mean sample value
235 unsigned long profile_mean ( struct profiler *profiler ) {
236 unsigned int mean_shift = profile_mean_shift ( profiler );
238 /* Round to nearest and scale down to original units */
239 return ( ( profiler->mean + ( 1UL << ( mean_shift - 1 ) ) )
244 * Get sample variance
246 * @v profiler Profiler
247 * @ret variance Sample variance
249 unsigned long profile_variance ( struct profiler *profiler ) {
250 unsigned int accvar_shift = profile_accvar_shift ( profiler );
252 /* Variance is zero if fewer than two samples exist (avoiding
253 * division by zero error).
255 if ( profiler->count < 2 )
258 /* Calculate variance, round to nearest, and scale to original units */
259 return ( ( ( profiler->accvar / ( profiler->count - 1 ) )
260 + ( 1ULL << ( accvar_shift - 1 ) ) ) >> accvar_shift );
264 * Get sample standard deviation
266 * @v profiler Profiler
267 * @ret stddev Sample standard deviation
269 unsigned long profile_stddev ( struct profiler *profiler ) {
271 return isqrt ( profile_variance ( profiler ) );