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