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