• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *   Copyright © International Business Machines  Corp., 2006, 2008
4  *
5  *   This program is free software;  you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  *   the GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program;  if not, write to the Free Software
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * NAME
20  *      rdtsc-latency.c
21  *
22  * DESCRIPTION
23  *       Simple program to measure the time between several pairs of calls to
24  *       rdtsc().  Based off of gtod-latency.c
25  *
26  * USAGE:
27  *      Use run_auto.sh script in current directory to build and run test.
28  *      Use "-j" to enable jvm simulator.
29  *
30  * AUTHOR
31  *      Darren Hart <dvhltc@us.ibm.com>
32  *
33  * HISTORY
34  *      2006-Nov-15: Initial version by Darren Hart <dvhltc@us.ibm.com>
35  *
36  *****************************************************************************/
37 
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <sys/time.h>
42 #include <sys/types.h>
43 #include <sched.h>
44 #include <errno.h>
45 #include <stdint.h>
46 #include <librttest.h>
47 
48 #include "tst_tsc.h"
49 
50 #define ITERATIONS 1000000
51 
usage(void)52 void usage(void)
53 {
54 	rt_help();
55 	printf("rdtsc-latency specific options:\n");
56 	printf(" This testcase don't expect any commandline options\n");
57 }
58 
parse_args(int c,char * v)59 int parse_args(int c, char *v)
60 {
61 
62 	int handled = 1;
63 	switch (c) {
64 	case 'h':
65 		usage();
66 		exit(0);
67 	default:
68 		handled = 0;
69 		break;
70 	}
71 	return handled;
72 }
73 
74 /* return difference in nanoseconds */
tv_minus(struct timeval * tv_start,struct timeval * tv_end)75 unsigned long long tv_minus(struct timeval *tv_start, struct timeval *tv_end)
76 {
77 	unsigned long long nsecs;
78 	nsecs = (tv_end->tv_sec - tv_start->tv_sec) * 1000000000ULL;
79 	nsecs += (tv_end->tv_usec - tv_start->tv_usec) * 1000;
80 	return nsecs;
81 }
82 
83 /* calculate the tsc period */
tsc_period_ps(void)84 unsigned long long tsc_period_ps(void)
85 {
86 	struct timeval tv_start;
87 	struct timeval tv_end;
88 	unsigned long long tsc_start, tsc_end;
89 
90 	rdtscll(tsc_start);
91 	gettimeofday(&tv_start, NULL);
92 	sleep(1);
93 	rdtscll(tsc_end);
94 	gettimeofday(&tv_end, NULL);
95 
96 	return (1000 * tv_minus(&tv_start, &tv_end)) / tsc_minus(tsc_start,
97 								 tsc_end);
98 }
99 
main(int argc,char * argv[])100 int main(int argc, char *argv[])
101 {
102 	int i, err;
103 	unsigned long long deltas[ITERATIONS];
104 	unsigned long long max, min, avg, tsc_a, tsc_b, tsc_period;
105 	struct sched_param param;
106 
107 #ifdef TSC_UNSUPPORTED
108 	printf("Error: test cannot be executed on an arch wihout TSC.\n");
109 	return ENOTSUP;
110 #endif
111 
112 	setup();
113 
114 	rt_init("h", parse_args, argc, argv);
115 
116 	/* no arguments */
117 	if (argc > 1) {
118 		fprintf(stderr, "%s accepts no arguments\n", argv[0]);
119 		exit(1);
120 	}
121 
122 	/* switch to SCHED_FIFO 99 */
123 	param.sched_priority = sched_get_priority_max(SCHED_FIFO);
124 	err = sched_setscheduler(0, SCHED_FIFO, &param);
125 
126 	/* Check that the user has the appropriate privileges */
127 	if (err) {
128 		if (errno == EPERM) {
129 			fprintf(stderr,
130 				"This program runs with a scheduling policy of SCHED_FIFO at priority %d\n",
131 				param.sched_priority);
132 			fprintf(stderr,
133 				"You don't have the necessary privileges to create such a real-time process.\n");
134 		} else {
135 			fprintf(stderr, "Failed to set scheduler, errno %d\n",
136 				errno);
137 		}
138 		exit(1);
139 	}
140 
141 	/* calculate the tsc period in picoseconds */
142 	tsc_period = tsc_period_ps();
143 
144 	/* collect ITERATIONS pairs of gtod calls */
145 	max = min = avg = 0;
146 	for (i = 0; i < ITERATIONS; i++) {
147 		rdtscll(tsc_a);
148 		rdtscll(tsc_b);
149 		deltas[i] = (tsc_minus(tsc_a, tsc_b) * tsc_period) / 1000;	/* tsc period is in ps */
150 		if (i == 0 || deltas[i] < min)
151 			min = deltas[i];
152 		if (deltas[i] > max)
153 			max = deltas[i];
154 		avg += deltas[i];
155 	}
156 	avg /= ITERATIONS;
157 
158 	/* report on deltas */
159 	printf("Calculated tsc period = %llu ps\n", tsc_period);
160 	printf("%d pairs of rdtsc() calls completed\n", ITERATIONS);
161 	printf("Time between calls:\n");
162 	printf("     Max: %llu ns\n", max);
163 	printf("     Min: %llu ns\n", min);
164 	printf("     Avg: %llu ns\n", avg);
165 
166 	return 0;
167 }
168