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.1KB

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