You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

profile.c 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  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.
  8. *
  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.
  13. *
  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
  17. * 02110-1301, USA.
  18. *
  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.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdint.h>
  25. #include <stdio.h>
  26. #include <strings.h>
  27. #include <limits.h>
  28. #include <assert.h>
  29. #include <ipxe/isqrt.h>
  30. #include <ipxe/profile.h>
  31. /** @file
  32. *
  33. * Profiling
  34. *
  35. * The profiler computes basic statistics (mean, variance, and
  36. * standard deviation) for the samples which it records. Note that
  37. * these statistics need not be completely accurate; it is sufficient
  38. * to give a rough approximation.
  39. *
  40. * The algorithm for updating the mean and variance estimators is from
  41. * The Art of Computer Programming (via Wikipedia), with adjustments
  42. * to avoid the use of floating-point instructions.
  43. */
  44. /** Accumulated time excluded from profiling */
  45. unsigned long profile_excluded;
  46. /**
  47. * Format a hex fraction (for debugging)
  48. *
  49. * @v value Value
  50. * @v shift Bit shift
  51. * @ret string Formatted hex fraction
  52. */
  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;
  57. uint8_t frac_part;
  58. char *ptr;
  59. if ( value < 0 ) {
  60. value = -value;
  61. ptr = &buf[0];
  62. } else {
  63. ptr = &buf[1];
  64. }
  65. int_part = ( value >> shift );
  66. frac_part = ( value >> ( shift - ( 8 * sizeof ( frac_part ) ) ) );
  67. snprintf ( &buf[1], ( sizeof ( buf ) - 1 ), "%#llx.%02x",
  68. int_part, frac_part );
  69. return ptr;
  70. }
  71. /**
  72. * Calculate bit shift for mean sample value
  73. *
  74. * @v profiler Profiler
  75. * @ret shift Bit shift
  76. */
  77. static inline unsigned int profile_mean_shift ( struct profiler *profiler ) {
  78. return ( ( ( 8 * sizeof ( profiler->mean ) ) - 1 ) /* MSB */
  79. - 1 /* Leave sign bit unused */
  80. - profiler->mean_msb );
  81. }
  82. /**
  83. * Calculate bit shift for accumulated variance value
  84. *
  85. * @v profiler Profiler
  86. * @ret shift Bit shift
  87. */
  88. static inline unsigned int profile_accvar_shift ( struct profiler *profiler ) {
  89. return ( ( ( 8 * sizeof ( profiler->accvar ) ) - 1 ) /* MSB */
  90. - 1 /* Leave top bit unused */
  91. - profiler->accvar_msb );
  92. }
  93. /**
  94. * Update profiler with a new sample
  95. *
  96. * @v profiler Profiler
  97. * @v sample Sample value
  98. */
  99. void profile_update ( struct profiler *profiler, unsigned long sample ) {
  100. unsigned int sample_msb;
  101. unsigned int mean_shift;
  102. unsigned int delta_shift;
  103. signed long pre_delta;
  104. signed long post_delta;
  105. signed long long accvar_delta;
  106. unsigned int accvar_delta_shift;
  107. unsigned int accvar_delta_msb;
  108. unsigned int accvar_shift;
  109. /* Our scaling logic assumes that sample values never overflow
  110. * a signed long (i.e. that the high bit is always zero).
  111. */
  112. assert ( ( ( signed ) sample ) >= 0 );
  113. /* Update sample count, limiting to avoid signed overflow */
  114. if ( profiler->count < INT_MAX )
  115. profiler->count++;
  116. /* Adjust mean sample value scale if necessary. Skip if
  117. * sample is zero (in which case flsl(sample)-1 would
  118. * underflow): in the case of a zero sample we have no need to
  119. * adjust the scale anyway.
  120. */
  121. if ( sample ) {
  122. sample_msb = ( flsl ( sample ) - 1 );
  123. if ( profiler->mean_msb < sample_msb ) {
  124. profiler->mean >>= ( sample_msb - profiler->mean_msb );
  125. profiler->mean_msb = sample_msb;
  126. }
  127. }
  128. /* Scale sample to internal units */
  129. mean_shift = profile_mean_shift ( profiler );
  130. sample <<= mean_shift;
  131. /* Update mean */
  132. pre_delta = ( sample - profiler->mean );
  133. profiler->mean += ( pre_delta / ( ( signed ) profiler->count ) );
  134. post_delta = ( sample - profiler->mean );
  135. delta_shift = mean_shift;
  136. DBGC ( profiler, "PROFILER %p sample %#lx mean %s", profiler,
  137. ( sample >> mean_shift ),
  138. profile_hex_fraction ( profiler->mean, mean_shift ) );
  139. DBGC ( profiler, " pre %s",
  140. profile_hex_fraction ( pre_delta, delta_shift ) );
  141. DBGC ( profiler, " post %s\n",
  142. profile_hex_fraction ( post_delta, delta_shift ) );
  143. /* Scale both deltas to fit in half of an unsigned long long
  144. * to avoid potential overflow on multiplication. Note that
  145. * shifting a signed quantity is "implementation-defined"
  146. * behaviour in the C standard, but gcc documents that it will
  147. * always perform sign extension.
  148. */
  149. if ( sizeof ( pre_delta ) > ( sizeof ( accvar_delta ) / 2 ) ) {
  150. unsigned int shift = ( 8 * ( sizeof ( pre_delta ) -
  151. ( sizeof ( accvar_delta ) / 2 ) ));
  152. pre_delta >>= shift;
  153. post_delta >>= shift;
  154. delta_shift -= shift;
  155. }
  156. /* Update variance, if applicable. Skip if either delta is
  157. * zero (in which case flsl(delta)-1 would underflow): in the
  158. * case of a zero delta there is no change to the accumulated
  159. * variance anyway.
  160. */
  161. if ( pre_delta && post_delta ) {
  162. /* Calculate variance delta */
  163. accvar_delta = ( ( ( signed long long ) pre_delta ) *
  164. ( ( signed long long ) post_delta ) );
  165. accvar_delta_shift = ( 2 * delta_shift );
  166. assert ( accvar_delta > 0 );
  167. /* Calculate variance delta MSB, using flsl() on each
  168. * delta individually to provide an upper bound rather
  169. * than requiring the existence of flsll().
  170. */
  171. accvar_delta_msb = ( flsll ( accvar_delta ) - 1 );
  172. if ( accvar_delta_msb > accvar_delta_shift ) {
  173. accvar_delta_msb -= accvar_delta_shift;
  174. } else {
  175. accvar_delta_msb = 0;
  176. }
  177. /* Adjust scales as necessary */
  178. if ( profiler->accvar_msb < accvar_delta_msb ) {
  179. /* Rescale accumulated variance */
  180. profiler->accvar >>= ( accvar_delta_msb -
  181. profiler->accvar_msb );
  182. profiler->accvar_msb = accvar_delta_msb;
  183. } else {
  184. /* Rescale variance delta */
  185. accvar_delta >>= ( profiler->accvar_msb -
  186. accvar_delta_msb );
  187. accvar_delta_shift -= ( profiler->accvar_msb -
  188. accvar_delta_msb );
  189. }
  190. /* Scale delta to internal units */
  191. accvar_shift = profile_accvar_shift ( profiler );
  192. accvar_delta <<= ( accvar_shift - accvar_delta_shift );
  193. /* Accumulate variance */
  194. profiler->accvar += accvar_delta;
  195. /* Adjust scale if necessary */
  196. if ( profiler->accvar &
  197. ( 1ULL << ( ( 8 * sizeof ( profiler->accvar ) ) - 1 ) ) ) {
  198. profiler->accvar >>= 1;
  199. profiler->accvar_msb++;
  200. accvar_delta >>= 1;
  201. accvar_shift--;
  202. }
  203. DBGC ( profiler, "PROFILER %p accvar %s", profiler,
  204. profile_hex_fraction ( profiler->accvar, accvar_shift ));
  205. DBGC ( profiler, " delta %s\n",
  206. profile_hex_fraction ( accvar_delta, accvar_shift ) );
  207. }
  208. }
  209. /**
  210. * Get mean sample value
  211. *
  212. * @v profiler Profiler
  213. * @ret mean Mean sample value
  214. */
  215. unsigned long profile_mean ( struct profiler *profiler ) {
  216. unsigned int mean_shift = profile_mean_shift ( profiler );
  217. /* Round to nearest and scale down to original units */
  218. return ( ( profiler->mean + ( 1UL << ( mean_shift - 1 ) ) )
  219. >> mean_shift );
  220. }
  221. /**
  222. * Get sample variance
  223. *
  224. * @v profiler Profiler
  225. * @ret variance Sample variance
  226. */
  227. unsigned long profile_variance ( struct profiler *profiler ) {
  228. unsigned int accvar_shift = profile_accvar_shift ( profiler );
  229. /* Variance is zero if fewer than two samples exist (avoiding
  230. * division by zero error).
  231. */
  232. if ( profiler->count < 2 )
  233. return 0;
  234. /* Calculate variance, round to nearest, and scale to original units */
  235. return ( ( ( profiler->accvar / ( profiler->count - 1 ) )
  236. + ( 1ULL << ( accvar_shift - 1 ) ) ) >> accvar_shift );
  237. }
  238. /**
  239. * Get sample standard deviation
  240. *
  241. * @v profiler Profiler
  242. * @ret stddev Sample standard deviation
  243. */
  244. unsigned long profile_stddev ( struct profiler *profiler ) {
  245. return isqrt ( profile_variance ( profiler ) );
  246. }