Debian Codegraph @

src/mpid/ch3/channels/nemesis/utils/monitor/rdtsc.h

raw
1 /*
2 * Copyright (C) by Argonne National Laboratory
3 * See COPYRIGHT in top-level directory
4 */
5
6 /***********************************************************************
7 This header file provides some simple functions to use the pentium
8 cycle counter to take timings. Let me know if you find any errors.
9 Darius Buntinas (buntinas@cis...)
10
11 Please note that version 2 (this file) has a new interface from the
12 original (rdtsc.h). Here are the changes from rdtsc.h:
13
14 TIME_DECLARE and TIME_DECLARE_EXTERN are no longer used.
15
16 TIME_INIT is not needed, unless you use USECS_DELAY. Use this as a
17 statement in main() (i.e. TIME_INIT; not as a function: TIME_INIT();).
18
19 TIME_PRE(x) now takes an unsigned long long parameter. You should
20 pass this parameter to TIME_POST. This allows you to do nested
21 timings.
22
23 TIME_POST(x) works pretty much as before. The parameter x must have
24 been set by TIME_PRE previously.
25
26 USECS(x) converts processor cycles to microseconds. Note that this
27 will do some file I/O the first time it is called to determine the
28 processor speed, so you probably don't want to use this between a
29 TIME_PRE and TIME_POST. If you used to use this to implement a
30 delay loop, use USECS_DELAY instead (or if you are really stubborn,
31 and want to use it like this, just make sure you do a TIME_INIT
32 first. It will then work as usual.);
33
34 USECS_DELAY(x) is a new function which will loop for x microseconds.
35 Because this is a time-critical function, the processor speed must
36 be initialized before this function is called. This can be done
37 explicitly, by doing a TIME_INIT, or implicitly, by calling USECS().
38 This function returns the actual time delayed in microseconds.
39
40 To time something between modules (i.e. .o files) in one file declare
41 the timing variable as a global, and in the other file declare is as
42 extern. Then use TIME_PRE and TIME_POST as usual. E.g.:
43 fileA.c:
44 unsigned long long my_timing_tmp_var;
45 ...
46 TIME_PRE(my_timing_tmp_var);
47 ...
48 fileB.c
49 extern unsigned long long my_timing_tmp_var;
50 ...
51 TIME_POST(my_timing_tmp_var);
52 ...
53
54 Here is a sample program showing how to use TIME_PRE and TIME_POST.
55
56 #include <stdio.h>
57 #include "rdtsc2.h"
58
59 int
60 main() {
61
62 unsigned long long tmp;
63 int i = 0;
64
65 TIME_PRE(tmp);
66 for (i = 0; i < 1000; ++i)
67 printf("hello\n");
68 TIME_POST(tmp);
69
70 printf ("1000 printfs took %5.3fus. That's %5.3fus per printf!\n", USECS(tmp),
71 USECS(tmp)/1000);
72
73 return 0;
74 }
75 ****************************************************************************/
76
77 #ifndef RDTSC_H_INCLUDED
78 #define RDTSC_H_INCLUDED
79 #include <stdlib.h>
80 #include <stdio.h>
81 /*#include "asm/msr.h" */
82 #define rdtsc(x) __asm__ __volatile__("rdtsc" : "=A" (x))
83
84 #define TIME_INIT do {__cpuMHz = SetMHz();} while(0)
85 #define TIME_PRE(cycles) rdtsc(cycles)
86 #define TIME_POST(cycles) do { unsigned long long __tmp; \
87 rdtsc(__tmp); \
88 (cycles) = __tmp - (cycles); } while (0)
89 #ifdef TIME_DEBUG_MHZ
90 static double __cpuMHz = TIME_DEBUG_MHZ;
91 #else
92 static double __cpuMHz = -1.0;
93 #endif
94
95 static inline double
96 SetMHz()
97 {
98 double mhz;
99 FILE* f;
100
101 f = popen("/bin/sed -n '/cpu MHz/s/[^:]*://p' /proc/cpuinfo", "r");
102 /*f = popen("/home/1/buntinas/bin/catsedn '/cpu MHz/s/[^:]*://p' /proc/cpuinfo", "r");*/
103
104 if (!f) {
105 printf ("RDTSC: Error reading cpu speed\n");
106 exit (-1);
107 }
108
109 if (fscanf(f, "%lf", &mhz) != 1) {
110 printf ("RDTSC: Error reading cpu speed.\n");
111 exit (-1);
112 }
113
114 pclose(f);
115
116 #if TIME_DEBUG
117 printf("Processor speed = %8.3f\n", mhz);
118 #endif
119
120 return mhz;
121 }
122
123 static inline double
124 USECS(unsigned long long cycles)
125 {
126 if (__cpuMHz < 0)
127 __cpuMHz = SetMHz();
128
129 return (double)(cycles)/__cpuMHz;
130 }
131
132 static inline double
133 USECS_DELAY(double usecs)
134 {
135 unsigned long long tmp1, tmp2, delay;
136 double elapsed;
137
138 rdtsc(tmp1);
139
140 if (__cpuMHz < 0) {
141 printf ("USECS_DELAY: You need to do a TIME_PRE or USECS() before calling "
142 "USECS_DELAY\n");
143 exit(-1);
144 }
145
146 delay = usecs * __cpuMHz;
147 do {
148 rdtsc(tmp2);
149 elapsed = tmp2 - tmp1;
150 } while (elapsed < delay);
151
152 return (double)elapsed / __cpuMHz;
153 }
154
155
156 #endif /* RDTSC_H_INCLUDED */
157
158
159
160
161