123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- #ifndef _IPXE_PROFILE_H
- #define _IPXE_PROFILE_H
-
- /** @file
- *
- * Profiling
- *
- */
-
- FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
-
- #include <bits/profile.h>
- #include <ipxe/tables.h>
-
- #ifndef PROFILING
- #ifdef NDEBUG
- #define PROFILING 0
- #else
- #define PROFILING 1
- #endif
- #endif
-
- /**
- * A data structure for storing profiling information
- */
- struct profiler {
- /** Name */
- const char *name;
- /** Start timestamp */
- unsigned long started;
- /** Stop timestamp */
- unsigned long stopped;
- /** Number of samples */
- unsigned int count;
- /** Mean sample value (scaled) */
- unsigned long mean;
- /** Mean sample value MSB
- *
- * This is the highest bit set in the raw (unscaled) value
- * (i.e. one less than would be returned by flsl(raw_mean)).
- */
- unsigned int mean_msb;
- /** Accumulated variance (scaled) */
- unsigned long long accvar;
- /** Accumulated variance MSB
- *
- * This is the highest bit set in the raw (unscaled) value
- * (i.e. one less than would be returned by flsll(raw_accvar)).
- */
- unsigned int accvar_msb;
- };
-
- /** Profiler table */
- #define PROFILERS __table ( struct profiler, "profilers" )
-
- /** Declare a profiler */
- #if PROFILING
- #define __profiler __table_entry ( PROFILERS, 01 )
- #else
- #define __profiler
- #endif
-
- extern unsigned long profile_excluded;
-
- extern void profile_update ( struct profiler *profiler, unsigned long sample );
- extern unsigned long profile_mean ( struct profiler *profiler );
- extern unsigned long profile_variance ( struct profiler *profiler );
- extern unsigned long profile_stddev ( struct profiler *profiler );
-
- /**
- * Get start time
- *
- * @v profiler Profiler
- * @ret started Start time
- */
- static inline __attribute__ (( always_inline )) unsigned long
- profile_started ( struct profiler *profiler ) {
-
- /* If profiling is active then return start time */
- if ( PROFILING ) {
- return ( profiler->started + profile_excluded );
- } else {
- return 0;
- }
- }
-
- /**
- * Get stop time
- *
- * @v profiler Profiler
- * @ret stopped Stop time
- */
- static inline __attribute__ (( always_inline )) unsigned long
- profile_stopped ( struct profiler *profiler ) {
-
- /* If profiling is active then return start time */
- if ( PROFILING ) {
- return ( profiler->stopped + profile_excluded );
- } else {
- return 0;
- }
- }
-
- /**
- * Get elapsed time
- *
- * @v profiler Profiler
- * @ret elapsed Elapsed time
- */
- static inline __attribute__ (( always_inline )) unsigned long
- profile_elapsed ( struct profiler *profiler ) {
-
- /* If profiling is active then return elapsed time */
- if ( PROFILING ) {
- return ( profile_stopped ( profiler ) -
- profile_started ( profiler ) );
- } else {
- return 0;
- }
- }
-
- /**
- * Start profiling
- *
- * @v profiler Profiler
- * @v started Start timestamp
- */
- static inline __attribute__ (( always_inline )) void
- profile_start_at ( struct profiler *profiler, unsigned long started ) {
-
- /* If profiling is active then record start timestamp */
- if ( PROFILING )
- profiler->started = ( started - profile_excluded );
- }
-
- /**
- * Stop profiling
- *
- * @v profiler Profiler
- * @v stopped Stop timestamp
- */
- static inline __attribute__ (( always_inline )) void
- profile_stop_at ( struct profiler *profiler, unsigned long stopped ) {
-
- /* If profiling is active then record end timestamp and update stats */
- if ( PROFILING ) {
- profiler->stopped = ( stopped - profile_excluded );
- profile_update ( profiler, profile_elapsed ( profiler ) );
- }
- }
-
- /**
- * Start profiling
- *
- * @v profiler Profiler
- */
- static inline __attribute__ (( always_inline )) void
- profile_start ( struct profiler *profiler ) {
-
- /* If profiling is active then record start timestamp */
- if ( PROFILING )
- profile_start_at ( profiler, profile_timestamp() );
- }
-
- /**
- * Stop profiling
- *
- * @v profiler Profiler
- */
- static inline __attribute__ (( always_inline )) void
- profile_stop ( struct profiler *profiler ) {
-
- /* If profiling is active then record end timestamp and update stats */
- if ( PROFILING )
- profile_stop_at ( profiler, profile_timestamp() );
- }
-
- /**
- * Exclude time from other ongoing profiling results
- *
- * @v profiler Profiler
- */
- static inline __attribute__ (( always_inline )) void
- profile_exclude ( struct profiler *profiler ) {
-
- /* If profiling is active then update accumulated excluded time */
- if ( PROFILING )
- profile_excluded += profile_elapsed ( profiler );
- }
-
- /**
- * Record profiling sample in custom units
- *
- * @v profiler Profiler
- * @v sample Profiling sample
- */
- static inline __attribute__ (( always_inline )) void
- profile_custom ( struct profiler *profiler, unsigned long sample ) {
-
- /* If profiling is active then update stats */
- if ( PROFILING )
- profile_update ( profiler, sample );
- }
-
- #endif /* _IPXE_PROFILE_H */
|