@@ -16,6 +16,7 @@ static const char *progname;
1616
1717static unsigned int test_duration = 3 ;
1818static double max_rprct = 99.99 ;
19+ static bool fast_timing = false;
1920
2021/* record duration in powers of 2 nanoseconds */
2122static long long int histogram [32 ];
@@ -56,6 +57,7 @@ handle_args(int argc, char *argv[])
5657 static struct option long_options [] = {
5758 {"duration" , required_argument , NULL , 'd' },
5859 {"cutoff" , required_argument , NULL , 'c' },
60+ {"fast" , no_argument , NULL , 'f' },
5961 {NULL , 0 , NULL , 0 }
6062 };
6163
@@ -68,7 +70,7 @@ handle_args(int argc, char *argv[])
6870 {
6971 if (strcmp (argv [1 ], "--help" ) == 0 || strcmp (argv [1 ], "-?" ) == 0 )
7072 {
71- printf (_ ("Usage: %s [-d DURATION] [-c CUTOFF]\n" ), progname );
73+ printf (_ ("Usage: %s [-d DURATION] [-c CUTOFF] [--fast] \n" ), progname );
7274 exit (0 );
7375 }
7476 if (strcmp (argv [1 ], "--version" ) == 0 || strcmp (argv [1 ], "-V" ) == 0 )
@@ -78,7 +80,7 @@ handle_args(int argc, char *argv[])
7880 }
7981 }
8082
81- while ((option = getopt_long (argc , argv , "d:c:" ,
83+ while ((option = getopt_long (argc , argv , "d:c:f: " ,
8284 long_options , & optindex )) != -1 )
8385 {
8486 switch (option )
@@ -125,6 +127,10 @@ handle_args(int argc, char *argv[])
125127 }
126128 break ;
127129
130+ case 'f' :
131+ fast_timing = true;
132+ break ;
133+
128134 default :
129135 fprintf (stderr , _ ("Try \"%s --help\" for more information.\n" ),
130136 progname );
@@ -155,11 +161,31 @@ test_timing(unsigned int duration)
155161 uint64 total_time ;
156162 int64 time_elapsed = 0 ;
157163 uint64 loop_count = 0 ;
158- uint64 prev ,
159- cur ;
160164 instr_time start_time ,
161165 end_time ,
162- temp ;
166+ prev ,
167+ cur ;
168+ char * time_source = NULL ;
169+ bool fast_timing_used = false;
170+
171+ INSTR_TIME_INITIALIZE ();
172+
173+ #if !defined(WIN32 ) && defined(__x86_64__ ) && defined(__linux__ )
174+ if (fast_timing && has_rdtsc )
175+ {
176+ time_source = "RDTSC" ;
177+ fast_timing_used = true;
178+ }
179+ else if (has_rdtscp )
180+ time_source = "RDTSCP" ;
181+ else
182+ time_source = PG_INSTR_CLOCK_NAME ;
183+ #else
184+ time_source = PG_INSTR_CLOCK_NAME ;
185+ #endif
186+ if (fast_timing && !fast_timing_used )
187+ printf (_ ("Warning: Fast timing requested, but not available - regular timing source will be used\n" ));
188+ printf (_ ("Time source: %s\n" ), time_source );
163189
164190 /*
165191 * Pre-zero the statistics data structures. They're already zero by
@@ -173,18 +199,23 @@ test_timing(unsigned int duration)
173199
174200 total_time = duration > 0 ? duration * INT64CONST (1000000000 ) : 0 ;
175201
176- INSTR_TIME_SET_CURRENT (start_time );
177- cur = INSTR_TIME_GET_NANOSEC (start_time );
202+ if (fast_timing )
203+ INSTR_TIME_SET_CURRENT_FAST (start_time );
204+ else
205+ INSTR_TIME_SET_CURRENT (start_time );
206+ cur = start_time ;
178207
179208 while (time_elapsed < total_time )
180209 {
181210 int32 diff ,
182211 bits ;
183212
184213 prev = cur ;
185- INSTR_TIME_SET_CURRENT (temp );
186- cur = INSTR_TIME_GET_NANOSEC (temp );
187- diff = cur - prev ;
214+ if (fast_timing )
215+ INSTR_TIME_SET_CURRENT_FAST (cur );
216+ else
217+ INSTR_TIME_SET_CURRENT (cur );
218+ diff = INSTR_TIME_DIFF_NANOSEC (cur , prev );
188219
189220 /* Did time go backwards? */
190221 if (unlikely (diff < 0 ))
@@ -217,11 +248,13 @@ test_timing(unsigned int duration)
217248 largest_diff_count ++ ;
218249
219250 loop_count ++ ;
220- INSTR_TIME_SUBTRACT (temp , start_time );
221- time_elapsed = INSTR_TIME_GET_NANOSEC (temp );
251+ time_elapsed = INSTR_TIME_DIFF_NANOSEC (cur , start_time );
222252 }
223253
224- INSTR_TIME_SET_CURRENT (end_time );
254+ if (fast_timing )
255+ INSTR_TIME_SET_CURRENT_FAST (end_time );
256+ else
257+ INSTR_TIME_SET_CURRENT (end_time );
225258
226259 INSTR_TIME_SUBTRACT (end_time , start_time );
227260
0 commit comments