Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

profile.c 7.9KB

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