Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

profile.h 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #ifndef _GPXE_PROFILE_H
  2. #define _GPXE_PROFILE_H
  3. /** @file
  4. *
  5. * Profiling
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER );
  9. #include <stdint.h>
  10. /**
  11. * A data structure for storing profiling information
  12. */
  13. union profiler {
  14. /** Timestamp (in CPU-specific "ticks") */
  15. uint64_t timestamp;
  16. /** Registers returned by rdtsc.
  17. *
  18. * This part should really be architecture-specific code.
  19. */
  20. struct {
  21. uint32_t eax;
  22. uint32_t edx;
  23. } rdtsc;
  24. };
  25. /**
  26. * Static per-object profiler, for use with simple_profile()
  27. */
  28. static union profiler simple_profiler;
  29. /**
  30. * Perform profiling
  31. *
  32. * @v profiler Profiler data structure
  33. * @ret delta Elapsed ticks since last call to profile().
  34. *
  35. * Call profile() both before and after the code you wish to measure.
  36. * The "after" call will return the measurement. For example:
  37. *
  38. * @code
  39. *
  40. * profile ( &profiler );
  41. * ... do something here ...
  42. * printf ( "It took %ld ticks to execute\n", profile ( &profiler ) );
  43. *
  44. * @endcode
  45. */
  46. static inline __attribute__ (( always_inline )) unsigned long
  47. profile ( union profiler *profiler ) {
  48. uint64_t last_timestamp = profiler->timestamp;
  49. __asm__ __volatile__ ( "rdtsc" :
  50. "=a" ( profiler->rdtsc.eax ),
  51. "=d" ( profiler->rdtsc.edx ) );
  52. return ( profiler->timestamp - last_timestamp );
  53. }
  54. /**
  55. * Perform profiling
  56. *
  57. * @ret delta Elapsed ticks since last call to profile().
  58. *
  59. * When you only need one profiler, you can avoid the hassle of
  60. * creating your own @c profiler data structure by using
  61. * simple_profile() instead.
  62. *
  63. * simple_profile() is equivalent to profile(&simple_profiler), where
  64. * @c simple_profiler is a @c profiler data structure that is static
  65. * to each object which includes @c profile.h.
  66. */
  67. static inline __attribute__ (( always_inline )) unsigned long
  68. simple_profile ( void ) {
  69. return profile ( &simple_profiler );
  70. }
  71. #endif /* _GPXE_PROFILE_H */