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.h 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #ifndef _IPXE_PROFILE_H
  2. #define _IPXE_PROFILE_H
  3. /** @file
  4. *
  5. * Profiling
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER );
  9. #include <stdint.h>
  10. #include <bits/profile.h>
  11. #include <ipxe/tables.h>
  12. #ifdef NDEBUG
  13. #define PROFILING 0
  14. #else
  15. #define PROFILING 1
  16. #endif
  17. /**
  18. * A data structure for storing profiling information
  19. */
  20. struct profiler {
  21. /** Name */
  22. const char *name;
  23. /** Start timestamp */
  24. uint64_t started;
  25. /** Number of samples */
  26. unsigned int count;
  27. /** Mean sample value (scaled) */
  28. unsigned long mean;
  29. /** Mean sample value MSB
  30. *
  31. * This is the highest bit set in the raw (unscaled) value
  32. * (i.e. one less than would be returned by flsl(raw_mean)).
  33. */
  34. unsigned int mean_msb;
  35. /** Accumulated variance (scaled) */
  36. unsigned long long accvar;
  37. /** Accumulated variance MSB
  38. *
  39. * This is the highest bit set in the raw (unscaled) value
  40. * (i.e. one less than would be returned by flsll(raw_accvar)).
  41. */
  42. unsigned int accvar_msb;
  43. };
  44. /** Profiler table */
  45. #define PROFILERS __table ( struct profiler, "profilers" )
  46. /** Declare a profiler */
  47. #if PROFILING
  48. #define __profiler __table_entry ( PROFILERS, 01 )
  49. #else
  50. #define __profiler
  51. #endif
  52. extern void profile_update ( struct profiler *profiler, unsigned long sample );
  53. extern unsigned long profile_mean ( struct profiler *profiler );
  54. extern unsigned long profile_variance ( struct profiler *profiler );
  55. extern unsigned long profile_stddev ( struct profiler *profiler );
  56. /**
  57. * Start profiling
  58. *
  59. * @v profiler Profiler
  60. */
  61. static inline __attribute__ (( always_inline )) void
  62. profile_start ( struct profiler *profiler ) {
  63. /* If profiling is active then record start timestamp */
  64. if ( PROFILING )
  65. profiler->started = profile_timestamp();
  66. }
  67. /**
  68. * Record profiling result
  69. *
  70. * @v profiler Profiler
  71. */
  72. static inline __attribute__ (( always_inline )) void
  73. profile_stop ( struct profiler *profiler ) {
  74. uint64_t ended;
  75. /* If profiling is active then record end timestamp and update stats */
  76. if ( PROFILING ) {
  77. ended = profile_timestamp();
  78. profile_update ( profiler, ( ended - profiler->started ) );
  79. }
  80. }
  81. #endif /* _IPXE_PROFILE_H */