[contrib] Allow Network Protocol header to display in rom-o-matic
[gpxe.git] / src / include / gpxe / profile.h
bloba5bdd3a45b4a21858c1be29482f0b2d95e88d14c
1 #ifndef _GPXE_PROFILE_H
2 #define _GPXE_PROFILE_H
4 /** @file
6 * Profiling
8 */
10 FILE_LICENCE ( GPL2_OR_LATER );
12 #include <stdint.h>
14 /**
15 * A data structure for storing profiling information
17 union profiler {
18 /** Timestamp (in CPU-specific "ticks") */
19 uint64_t timestamp;
20 /** Registers returned by rdtsc.
22 * This part should really be architecture-specific code.
24 struct {
25 uint32_t eax;
26 uint32_t edx;
27 } rdtsc;
30 /**
31 * Static per-object profiler, for use with simple_profile()
33 static union profiler simple_profiler;
35 /**
36 * Perform profiling
38 * @v profiler Profiler data structure
39 * @ret delta Elapsed ticks since last call to profile().
41 * Call profile() both before and after the code you wish to measure.
42 * The "after" call will return the measurement. For example:
44 * @code
46 * profile ( &profiler );
47 * ... do something here ...
48 * printf ( "It took %ld ticks to execute\n", profile ( &profiler ) );
50 * @endcode
52 static inline __attribute__ (( always_inline )) unsigned long
53 profile ( union profiler *profiler ) {
54 uint64_t last_timestamp = profiler->timestamp;
56 __asm__ __volatile__ ( "rdtsc" :
57 "=a" ( profiler->rdtsc.eax ),
58 "=d" ( profiler->rdtsc.edx ) );
59 return ( profiler->timestamp - last_timestamp );
62 /**
63 * Perform profiling
65 * @ret delta Elapsed ticks since last call to profile().
67 * When you only need one profiler, you can avoid the hassle of
68 * creating your own @c profiler data structure by using
69 * simple_profile() instead.
71 * simple_profile() is equivalent to profile(&simple_profiler), where
72 * @c simple_profiler is a @c profiler data structure that is static
73 * to each object which includes @c profile.h.
75 static inline __attribute__ (( always_inline )) unsigned long
76 simple_profile ( void ) {
77 return profile ( &simple_profiler );
80 #endif /* _GPXE_PROFILE_H */