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 * preempt_timing.c
21 *
22 * DESCRIPTION
23 * This program indicated the preemption delays that may be encountered
24 * by realtime apps. The program runs with the scheduling policy of
25 * SCHED_FIFO at a maximum SCHED_FIFO priority. It is bound to a single
26 * processor and its address space is locked as well. It makes successive
27 * calls to the gettimeofday() function(via inlined assembly to read the
28 * TSC).The value returned between two such consecutive calls is reported
29 * as the latency.
30 * The maximum, minimum and average delays are reported for x pairs of such
31 * calls.
32 *
33 * USAGE:
34 * Use run_auto.sh script in current directory to build and run test.
35 *
36 * AUTHOR
37 *
38 *
39 * HISTORY
40 *
41 *
42 *****************************************************************************/
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <sys/time.h>
48 #include <sys/types.h>
49 #include <sched.h>
50 #include <string.h>
51 #include <errno.h>
52 #include <sys/mman.h>
53 #include <stdint.h>
54 #include <librttest.h>
55 #include <libtsc.h>
56
57 #define ITERATIONS 1000000ULL
58 #define INTERVALS 10
59
usage(void)60 void usage(void)
61 {
62 rt_help();
63 printf("preempt_timing specific options:\n");
64 }
65
parse_args(int c,char * v)66 int parse_args(int c, char *v)
67 {
68
69 int handled = 1;
70 switch (c) {
71 case 'h':
72 usage();
73 exit(0);
74 default:
75 handled = 0;
76 break;
77 }
78 return handled;
79 }
80
81 unsigned long long sample_list[ITERATIONS];
main(int argc,char * argv[])82 int main(int argc, char *argv[])
83 {
84 unsigned long long i, j, delta, min, max, avg;
85 struct sched_param param;
86 cpu_set_t mask;
87 int err;
88
89 #ifdef TSC_UNSUPPORTED
90 printf("Error: test cannot be executed on an arch wihout TSC.\n");
91 return ENOTSUP;
92 #endif
93
94 max = avg = 0;
95 min = -1;
96 setup();
97
98 rt_init("h", parse_args, argc, argv);
99
100 /* switch to SCHED_FIFO 99 */
101 param.sched_priority = sched_get_priority_max(SCHED_FIFO);
102 err = sched_setscheduler(0, SCHED_FIFO, ¶m);
103
104 /* Check that the user has the appropriate privileges */
105 if (err) {
106 if (errno == EPERM) {
107 fprintf(stderr,
108 "This program runs with a scheduling policy of SCHED_FIFO at priority %d\n",
109 param.sched_priority);
110 fprintf(stderr,
111 "You don't have the necessary privileges to create such a real-time process.\n");
112 } else {
113 fprintf(stderr, "Failed to set scheduler, errno %d\n",
114 errno);
115 }
116 exit(1);
117 }
118
119 /* BIND TO A SINGLE CPU */
120 CPU_ZERO(&mask);
121 CPU_SET(0, &mask);
122 err = sched_setaffinity(0, sizeof(mask), &mask);
123 if (err < 0) {
124 printf("Can't set affinity: %d %s\n", err, strerror(err));
125 exit(-1);
126 }
127
128 for (j = 0; j < INTERVALS; j++) {
129 /* Collect samples */
130 for (i = 0; i < ITERATIONS; i++)
131 rdtscll(sample_list[i]);
132
133 /* Process samples */
134 for (i = 0; i < (ITERATIONS - 1); i++) {
135 delta = sample_list[i + 1] - sample_list[i];
136 if (delta < min)
137 min = delta;
138 if (delta > max)
139 max = delta;
140 if (delta > 100000)
141 printf("maxd(%llu:%llu): %llu %llu = %llu\n", j,
142 i, sample_list[i], sample_list[i + 1],
143 delta);
144 avg += delta;
145 }
146 usleep(100); /*let necessary things happen */
147 }
148 avg /= (ITERATIONS * INTERVALS);
149
150 printf("%lld pairs of gettimeofday() calls completed\n",
151 ITERATIONS * INTERVALS);
152 printf("Time between calls:\n");
153 printf("Minimum: %llu \n", min);
154 printf("Maximum: %llu \n", max);
155 printf("Average: %llu \n", avg);
156
157 return 0;
158 }
159