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
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
30 #include <ipxe/isqrt.h>
31 #include <ipxe/profile.h>
37 * The profiler computes basic statistics (mean, variance, and
38 * standard deviation) for the samples which it records. Note that
39 * these statistics need not be completely accurate; it is sufficient
40 * to give a rough approximation.
42 * The algorithm for updating the mean and variance estimators is from
43 * The Art of Computer Programming (via Wikipedia), with adjustments
44 * to avoid the use of floating-point instructions.
47 /** Accumulated time excluded from profiling */
48 unsigned long profile_excluded;
51 * Format a hex fraction (for debugging)
55 * @ret string Formatted hex fraction
57 static const char * profile_hex_fraction ( signed long long value,
58 unsigned int shift ) {
59 static char buf[23] = "-"; /* -0xXXXXXXXXXXXXXXXX.XX + NUL */
60 unsigned long long int_part;
70 int_part = ( value >> shift );
71 frac_part = ( value >> ( shift - ( 8 * sizeof ( frac_part ) ) ) );
72 snprintf ( &buf[1], ( sizeof ( buf ) - 1 ), "%#llx.%02x",
73 int_part, frac_part );
78 * Calculate bit shift for mean sample value
80 * @v profiler Profiler
81 * @ret shift Bit shift
83 static inline unsigned int profile_mean_shift ( struct profiler *profiler ) {
85 return ( ( ( 8 * sizeof ( profiler->mean ) ) - 1 ) /* MSB */
86 - 1 /* Leave sign bit unused */
87 - profiler->mean_msb );
91 * Calculate bit shift for accumulated variance value
93 * @v profiler Profiler
94 * @ret shift Bit shift
96 static inline unsigned int profile_accvar_shift ( struct profiler *profiler ) {
98 return ( ( ( 8 * sizeof ( profiler->accvar ) ) - 1 ) /* MSB */
99 - 1 /* Leave top bit unused */
100 - profiler->accvar_msb );
104 * Update profiler with a new sample
106 * @v profiler Profiler
107 * @v sample Sample value
109 void profile_update ( struct profiler *profiler, unsigned long sample ) {
110 unsigned int sample_msb;
111 unsigned int mean_shift;
112 unsigned int delta_shift;
113 signed long pre_delta;
114 signed long post_delta;
115 signed long long accvar_delta;
116 unsigned int accvar_delta_shift;
117 unsigned int accvar_delta_msb;
118 unsigned int accvar_shift;
120 /* Our scaling logic assumes that sample values never overflow
121 * a signed long (i.e. that the high bit is always zero).
123 assert ( ( ( signed ) sample ) >= 0 );
125 /* Update sample count */
128 /* Adjust mean sample value scale if necessary. Skip if
129 * sample is zero (in which case flsl(sample)-1 would
130 * underflow): in the case of a zero sample we have no need to
131 * adjust the scale anyway.
134 sample_msb = ( flsl ( sample ) - 1 );
135 if ( profiler->mean_msb < sample_msb ) {
136 profiler->mean >>= ( sample_msb - profiler->mean_msb );
137 profiler->mean_msb = sample_msb;
141 /* Scale sample to internal units */
142 mean_shift = profile_mean_shift ( profiler );
143 sample <<= mean_shift;
146 pre_delta = ( sample - profiler->mean );
147 profiler->mean += ( pre_delta / ( ( signed ) profiler->count ) );
148 post_delta = ( sample - profiler->mean );
149 delta_shift = mean_shift;
150 DBGC ( profiler, "PROFILER %p sample %#lx mean %s", profiler,
151 ( sample >> mean_shift ),
152 profile_hex_fraction ( profiler->mean, mean_shift ) );
153 DBGC ( profiler, " pre %s",
154 profile_hex_fraction ( pre_delta, delta_shift ) );
155 DBGC ( profiler, " post %s\n",
156 profile_hex_fraction ( post_delta, delta_shift ) );
158 /* Scale both deltas to fit in half of an unsigned long long
159 * to avoid potential overflow on multiplication. Note that
160 * shifting a signed quantity is "implementation-defined"
161 * behaviour in the C standard, but gcc documents that it will
162 * always perform sign extension.
164 if ( sizeof ( pre_delta ) > ( sizeof ( accvar_delta ) / 2 ) ) {
165 unsigned int shift = ( 8 * ( sizeof ( pre_delta ) -
166 ( sizeof ( accvar_delta ) / 2 ) ));
168 post_delta >>= shift;
169 delta_shift -= shift;
172 /* Update variance, if applicable. Skip if either delta is
173 * zero (in which case flsl(delta)-1 would underflow): in the
174 * case of a zero delta there is no change to the accumulated
177 if ( pre_delta && post_delta ) {
179 /* Calculate variance delta */
180 accvar_delta = ( ( ( signed long long ) pre_delta ) *
181 ( ( signed long long ) post_delta ) );
182 accvar_delta_shift = ( 2 * delta_shift );
183 assert ( accvar_delta > 0 );
185 /* Calculate variance delta MSB, using flsl() on each
186 * delta individually to provide an upper bound rather
187 * than requiring the existence of flsll().
189 accvar_delta_msb = ( flsll ( accvar_delta ) - 1 );
190 if ( accvar_delta_msb > accvar_delta_shift ) {
191 accvar_delta_msb -= accvar_delta_shift;
193 accvar_delta_msb = 0;
196 /* Adjust scales as necessary */
197 if ( profiler->accvar_msb < accvar_delta_msb ) {
198 /* Rescale accumulated variance */
199 profiler->accvar >>= ( accvar_delta_msb -
200 profiler->accvar_msb );
201 profiler->accvar_msb = accvar_delta_msb;
203 /* Rescale variance delta */
204 accvar_delta >>= ( profiler->accvar_msb -
206 accvar_delta_shift -= ( profiler->accvar_msb -
210 /* Scale delta to internal units */
211 accvar_shift = profile_accvar_shift ( profiler );
212 accvar_delta <<= ( accvar_shift - accvar_delta_shift );
214 /* Accumulate variance */
215 profiler->accvar += accvar_delta;
217 /* Adjust scale if necessary */
218 if ( profiler->accvar &
219 ( 1ULL << ( ( 8 * sizeof ( profiler->accvar ) ) - 1 ) ) ) {
220 profiler->accvar >>= 1;
221 profiler->accvar_msb++;
226 DBGC ( profiler, "PROFILER %p accvar %s", profiler,
227 profile_hex_fraction ( profiler->accvar, accvar_shift ));
228 DBGC ( profiler, " delta %s\n",
229 profile_hex_fraction ( accvar_delta, accvar_shift ) );
234 * Get mean sample value
236 * @v profiler Profiler
237 * @ret mean Mean sample value
239 unsigned long profile_mean ( struct profiler *profiler ) {
240 unsigned int mean_shift = profile_mean_shift ( profiler );
242 /* Round to nearest and scale down to original units */
243 return ( ( profiler->mean + ( 1UL << ( mean_shift - 1 ) ) )
248 * Get sample variance
250 * @v profiler Profiler
251 * @ret variance Sample variance
253 unsigned long profile_variance ( struct profiler *profiler ) {
254 unsigned int accvar_shift = profile_accvar_shift ( profiler );
256 /* Variance is zero if fewer than two samples exist (avoiding
257 * division by zero error).
259 if ( profiler->count < 2 )
262 /* Calculate variance, round to nearest, and scale to original units */
263 return ( ( ( profiler->accvar / ( profiler->count - 1 ) )
264 + ( 1ULL << ( accvar_shift - 1 ) ) ) >> accvar_shift );
268 * Get sample standard deviation
270 * @v profiler Profiler
271 * @ret stddev Sample standard deviation
273 unsigned long profile_stddev ( struct profiler *profiler ) {
275 return isqrt ( profile_variance ( profiler ) );