@@ -14,16 +14,24 @@ static const char *progname;
14
14
static int32 test_duration = 3 ;
15
15
16
16
static void handle_args (int argc , char * argv []);
17
- static void test_timing (int32 );
17
+ static uint64 test_timing (int32 );
18
+ static void output (uint64 loop_count );
19
+
20
+ /* record duration in powers of 2 microseconds */
21
+ int64 histogram [32 ];
18
22
19
23
int
20
24
main (int argc , char * argv [])
21
25
{
26
+ uint64 loop_count ;
27
+
22
28
progname = get_progname (argv [0 ]);
23
29
24
30
handle_args (argc , argv );
25
31
26
- test_timing (test_duration );
32
+ loop_count = test_timing (test_duration );
33
+
34
+ output (loop_count );
27
35
28
36
return 0 ;
29
37
}
@@ -95,25 +103,14 @@ handle_args(int argc, char *argv[])
95
103
}
96
104
}
97
105
98
- static void
106
+ static uint64
99
107
test_timing (int32 duration )
100
108
{
101
109
uint64 total_time ;
102
110
int64 time_elapsed = 0 ;
103
111
uint64 loop_count = 0 ;
104
- uint64 prev ,
105
- cur ;
106
- int32 diff ,
107
- i ,
108
- bits ,
109
- found ;
110
-
111
- instr_time start_time ,
112
- end_time ,
113
- temp ;
114
-
115
- static int64 histogram [32 ];
116
- char buf [100 ];
112
+ uint64 prev , cur ;
113
+ instr_time start_time , end_time , temp ;
117
114
118
115
total_time = duration > 0 ? duration * 1000000 : 0 ;
119
116
@@ -122,24 +119,29 @@ test_timing(int32 duration)
122
119
123
120
while (time_elapsed < total_time )
124
121
{
122
+ int32 diff , bits = 0 ;
123
+
125
124
prev = cur ;
126
125
INSTR_TIME_SET_CURRENT (temp );
127
126
cur = INSTR_TIME_GET_MICROSEC (temp );
128
127
diff = cur - prev ;
129
128
129
+ /* Did time go backwards? */
130
130
if (diff < 0 )
131
131
{
132
132
printf ("Detected clock going backwards in time.\n" );
133
133
printf ("Time warp: %d microseconds\n" , diff );
134
134
exit (1 );
135
135
}
136
136
137
- bits = 0 ;
137
+ /* What is the highest bit in the time diff? */
138
138
while (diff )
139
139
{
140
140
diff >>= 1 ;
141
141
bits ++ ;
142
142
}
143
+
144
+ /* Update appropriate duration bucket */
143
145
histogram [bits ]++ ;
144
146
145
147
loop_count ++ ;
@@ -153,19 +155,29 @@ test_timing(int32 duration)
153
155
154
156
printf ("Per loop time including overhead: %0.2f nsec\n" ,
155
157
INSTR_TIME_GET_DOUBLE (end_time ) * 1e9 / loop_count );
158
+
159
+ return loop_count ;
160
+ }
161
+
162
+ static void
163
+ output (uint64 loop_count )
164
+ {
165
+ int64 max_bit = 31 , i ;
166
+
167
+ /* find highest bit value */
168
+ while (max_bit > 0 && histogram [max_bit ] == 0 )
169
+ max_bit -- ;
170
+
156
171
printf ("Histogram of timing durations:\n" );
157
- printf ("%9s: %10s %9s \n" , "< usec" , "count " , "percent " );
172
+ printf ("%6s %10s %10s \n" , "< usec" , "% of total " , "count " );
158
173
159
- found = 0 ;
160
- for (i = 31 ; i >= 0 ; i -- )
174
+ for (i = 0 ; i <= max_bit ; i ++ )
161
175
{
162
- if (found || histogram [i ])
163
- {
164
- found = 1 ;
165
- /* lame hack to work around INT64_FORMAT deficiencies */
166
- snprintf (buf , sizeof (buf ), INT64_FORMAT , histogram [i ]);
167
- printf ("%9ld: %10s %8.5f%%\n" , 1l << i , buf ,
168
- (double ) histogram [i ] * 100 / loop_count );
169
- }
176
+ char buf [100 ];
177
+
178
+ /* lame hack to work around INT64_FORMAT deficiencies */
179
+ snprintf (buf , sizeof (buf ), INT64_FORMAT , histogram [i ]);
180
+ printf ("%6ld %9.5f %10s\n" , 1l << i ,
181
+ (double ) histogram [i ] * 100 / loop_count , buf );
170
182
}
171
183
}
0 commit comments