blob: ffffc0a838c38ef14492a4eb760e9df4cda6c97d [file] [log] [blame]
nstraz89c4abc2002-08-01 22:56:55 +00001/*
2 * lat-sched by Davide Libenzi ( linux kernel scheduler latency tester )
3 * Version 0.21 - Copyright (C) 2001 Davide Libenzi
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 the
13 * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * Davide Libenzi <davidel@xmailserver.org>
20 *
21 *
22 * The purpose of this tool is to measure the scheduler latency over a given
23 * runqueue length. The major difference with lat_ctx is that during the test
24 * time the runqueue is exactly NRTASK, while with lat_ctx ( that uses pipes )
25 * processes are typically sleeping on the pipe read.
26 * Build:
27 *
28 * gcc -O0 -o lat-sched lat-sched.c
29 *
30 * Use:
31 *
32 * lat-sched [--ntasks n] [--ttime s] [--size b] [--stksize b] [--stkalign b]
33 * [--max-eck k] [--verbose] [--help]
34 *
35 * --ntask = Set the number of tasks ( runqueue length )
36 * --ttime = Set thetest measure time in seconds
37 * --size = Set the cache drain size in Kb
38 * --stksize = Set the stack size for tasks
39 * --stkalign = Set the shift each task stack will have over stksize
40 * --max-eck = Set the maximum correction factor above which the measure is invalid
41 * --verbose = Activate verbose mode
42 * --help = Print help screen
43 *
44 *
45 * Output: NRTASK MSRUN CSSEC LATSCH AVGWRK THRZ CHISQR
46 *
47 * NRTASK = Number of test tasks
48 * MSRUN = Number of milliseconds the test is ran
49 * CSSEC = Number of context switches / sec
50 * LATSCH = Number of seconds for a context switch
51 * AVGWRK = Number of context switches / NRTASK
52 * THRZ = Number of task with zero context switch
53 * CHISQR = Context switches chi-square over tasks
54 *
55 * In case the measure will result invalid ( according to eck ) the output line
56 * will begin with a '*' character.
57 * Messages are printed on <stderr> while results are printed on <stdout>
58 *
59 * IMPORTANT: To make this test work with a number of tasks that is less or equal
60 * to the number of CPUs the sys_sched_yield() optimization must be removed from
61 * the kernel sources in <kernel/sched.c>.
62 * This is a working sys_sched_yield() for kernel 2.4.10 :
63 *
64 * asmlinkage long sys_sched_yield(void) {
65 * if (current->policy == SCHED_OTHER)
66 * current->policy |= SCHED_YIELD;
67 * current->need_resched = 1;
68 * return 0;
69 * }
70 *
71 */
72
73
74#include <stdio.h>
75#include <signal.h>
76#include <sys/types.h>
77#include <unistd.h>
78#include <sched.h>
79#include <sys/time.h>
80#include <sys/wait.h>
81#include <time.h>
82#include <string.h>
83#include <asm/atomic.h>
84
85
86
87#define CACHELINE_SIZE 32
88
89
90#define SSUMTIME 1
91#define MAXTASKS 2048
92#define MEASURE_TIME 4
93#define STK_SIZE 512
94
95
96
97char *stacks;
98int *data;
99int datasize = 0, stksize = STK_SIZE, stkalign = 0;
100pid_t thr[MAXTASKS];
101int nbtasks = 2;
102int measure_time = MEASURE_TIME;
103double eck = 0.9;
104volatile atomic_t actthreads = ATOMIC_INIT(0);
105long long int totalwork[MAXTASKS];
106volatile int stop = 0, start = 0, count = 0;
107int verbose = 0;
108
109
110/*
111 * cacheload() code comes from Larry McVoy LMBench
112 * Bring howmuch data into the cache, assuming that the smallest cache
113 * line is 16/32 bytes.
114 */
115int cacheload(int howmuch) {
116 int done, sum = 0;
117 register int *d = data;
118
119#if CACHELINE_SIZE == 16
120
121#define ASUM sum+=d[0]+d[4]+d[8]+d[12]+d[16]+d[20]+d[24]+d[28]+\
122 d[32]+d[36]+d[40]+d[44]+d[48]+d[52]+d[56]+d[60]+\
123 d[64]+d[68]+d[72]+d[76]+d[80]+d[84]+d[88]+d[92]+\
124 d[96]+d[100]+d[104]+d[108]+d[112]+d[116]+d[120]+d[124];\
125 d+=128; /* ints; bytes == 512 */
126
127#elif CACHELINE_SIZE == 32
128
129#define ASUM sum+=d[0]+d[8]+d[16]+d[24]+d[32]+d[40]+d[48]+d[56]+\
130 d[64]+d[72]+d[80]+d[88]+d[96]+d[104]+d[112]+d[120];\
131 d+=128; /* ints; bytes == 512 */
132
133#else
134
135#define ASUM sum+=d[0]+d[1]+d[2]+d[3]+d[4]+d[5]+d[6]+d[7]+d[8]+d[9]+\
136 d[10]+d[11]+d[12]+d[13]+d[14]+d[15]+d[16]+d[17]+d[18]+d[19]+\
137 d[20]+d[21]+d[22]+d[23]+d[24]+d[25]+d[26]+d[27]+d[28]+d[29]+\
138 d[30]+d[31]+d[32]+d[33]+d[34]+d[35]+d[36]+d[37]+d[38]+d[39]+\
139 d[40]+d[41]+d[42]+d[43]+d[44]+d[45]+d[46]+d[47]+d[48]+d[49]+\
140 d[50]+d[51]+d[52]+d[53]+d[54]+d[55]+d[56]+d[57]+d[58]+d[59]+\
141 d[60]+d[61]+d[62]+d[63]+d[64]+d[65]+d[66]+d[67]+d[68]+d[69]+\
142 d[70]+d[71]+d[72]+d[73]+d[74]+d[75]+d[76]+d[77]+d[78]+d[79]+\
143 d[80]+d[81]+d[82]+d[83]+d[84]+d[85]+d[86]+d[87]+d[88]+d[89]+\
144 d[90]+d[91]+d[92]+d[93]+d[94]+d[95]+d[96]+d[97]+d[98]+d[99]+\
145 d[100]+d[101]+d[102]+d[103]+d[104]+\
146 d[105]+d[106]+d[107]+d[108]+d[109]+\
147 d[110]+d[111]+d[112]+d[113]+d[114]+\
148 d[115]+d[116]+d[117]+d[118]+d[119]+\
149 d[120]+d[121]+d[122]+d[123]+d[124]+d[125]+d[126]+d[127];\
150 d+=128; /* ints; bytes == 512 */
151
152#endif
153
154#define ONEKB ASUM ASUM
155
156 for (done = 0; done < howmuch; done += 1024) {
157 ONEKB
158 }
159 return sum;
160}
161
162
163void taskproc(unsigned int thr) {
164 long long int counter = 0;
165
166 while (!start)
167 usleep(100000);
168
169 atomic_inc((atomic_t *) &actthreads);
170
171 while (!stop) {
172 if (count) {
173 ++counter;
174 cacheload(datasize);
175 }
176 syscall(158); /* sys_sched_yield() */
177 }
178 totalwork[thr] = counter;
179 atomic_dec((atomic_t *) &actthreads);
180 exit(0);
181}
182
183
184
185unsigned long long getmstics(void) {
186 struct timeval tv;
187
188 if (gettimeofday(&tv, NULL) != 0)
189 return (0);
190
191 return 1000 * (unsigned long long) tv.tv_sec + (unsigned long long) tv.tv_usec / 1000;
192}
193
194
195void usage(char *prg) {
196 fprintf(stderr,
197 "use: %s [--ntasks n {%d}] [--ttime s {%d}] [--size b {%d}] [--stksize b {%d}]\n"
198 "\t[--stkalign b {%d}] [--max-eck k {%lf}] [--verbose] [--help]\n",
199 prg, nbtasks, measure_time, datasize, stksize, stkalign, eck);
200}
201
202
203main(int argc, char **argv) {
204 int i, status, avgwork, thrzero = 0, stkrsiz;
205 long long int value = 0, avgvalue;
206 double sqrdev;
207 unsigned long long ts, te, sts, ste, ttime, sits, is, tcorr;
208 char const *ustr = "";
209
210 for (i = 1; i < argc; i++) {
211 if (strcmp(argv[i], "--ntasks") == 0) {
212 if (++i < argc)
213 nbtasks = atoi(argv[i]);
214 continue;
215 }
216 if (strcmp(argv[i], "--ttime") == 0) {
217 if (++i < argc)
218 measure_time = atoi(argv[i]);
219 continue;
220 }
221 if (strcmp(argv[i], "--stksize") == 0) {
222 if (++i < argc)
223 stksize = atoi(argv[i]), stksize &= ~(CACHELINE_SIZE - 1);
224 continue;
225 }
226 if (strcmp(argv[i], "--size") == 0) {
227 if (++i < argc)
228 datasize = atoi(argv[i]) * 1024;
229 continue;
230 }
231 if (strcmp(argv[i], "--max-eck") == 0) {
232 if (++i < argc)
233 eck = atof(argv[i]);
234 continue;
235 }
236 if (strcmp(argv[i], "--stkalign") == 0) {
237 if (++i < argc)
238 stkalign = atoi(argv[i]), stkalign &= ~(CACHELINE_SIZE - 1);
239 continue;
240 }
241 if (strcmp(argv[i], "--verbose") == 0) {
242 verbose = 1;
243 continue;
244 }
245 if (strcmp(argv[i], "--help") == 0) {
246 usage(argv[0]);
247 exit(9);
248 }
249 }
250
251 if (nbtasks > MAXTASKS)
252 nbtasks = MAXTASKS;
253
254 if (datasize)
255 data = (int *) malloc(datasize);
256
257 stkrsiz = stkalign + stksize;
258 if (!(stacks = (char *) malloc((nbtasks + 1) * stkrsiz))) {
259 perror("stack alloc");
260 exit(1);
261 }
262
263 if (verbose) fprintf(stderr, "\ncreating %d tasks ...", nbtasks);
264 for (i = 0; i < nbtasks; i++) {
265 thr[i] = __clone((void *) &taskproc, stacks + (i + 1) * stkrsiz,
266 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND, (void *) i);
267 if (thr[i] == -1) {
268 perror("clone");
269 exit(2);
270 }
271 }
272
273 for (i = 0; i < nbtasks; i++)
274 totalwork[i] = 0;
275
276 if (verbose) fprintf(stderr, " ok\nwaiting for all tasks to start ...");
277
278 start = 1;
279 while (atomic_read(&actthreads) != nbtasks)
280 usleep(100000);
281
282 if (verbose) fprintf(stderr, " ok\nrunning test ...");
283
284 count = 1;
285 ts = getmstics();
286
287 sleep(measure_time);
288
289 count = 0;
290 stop = 1;
291 te = getmstics();
292
293 if (verbose) fprintf(stderr, " ok\nwaiting completion ...");
294
295 while (atomic_read(&actthreads) > 0)
296 usleep(100000);
297
298 for (i = 0; i < nbtasks; i++)
299 wait(&status);
300
301 if (verbose) fprintf(stderr, " ok\n");
302
303 for (i = 0; i < nbtasks; i++) {
304 value += totalwork[i];
305 if (totalwork[i] == 0)
306 ++thrzero;
307 }
308
309 if (verbose) fprintf(stderr, "compensation loop ...");
310 is = (value * 1000 * SSUMTIME) / (te - ts);
311 do {
312 sits = is;
313 sts = getmstics();
314
315 for (; is; is--)
316 cacheload(datasize);
317
318 ste = getmstics();
319 is = 3 * (sits * 1000 * SSUMTIME) / (2 * (ste - sts));
320 } while ((ste - sts) < 1000 * SSUMTIME);
321 tcorr = ((ste - sts) * value) / sits;
322 if (verbose) fprintf(stderr, " sits=%llu comptime=%llu corr=%llu value=%llu citt=%e\n",
323 sits, ste - sts, tcorr, value, (double) (ste - sts) / ((double) sits * 1000.0));
324 if ((double) tcorr > eck * (te - ts)) {
325 if (verbose) fprintf(stderr, "measure unstable: corr{%llu} > (eck{%lf} * ttime{%llu})\n",
326 tcorr, eck, te - ts);
327 ustr = "*";
328 }
329 ttime = (te - ts) - tcorr;
330 avgvalue = value / nbtasks;
331 value *= 1000;
332 value /= ttime;
333 avgwork = (int) (value / nbtasks);
334
335 for (i = 0, sqrdev = 0; i < nbtasks; i++) {
336 double difvv = (double) (totalwork[i] - avgvalue);
337
338 sqrdev += (difvv * difvv) / (double) avgvalue;
339 }
340
341 fprintf(stdout, "%s%d\t%lu\t%lld\t%e\t%d\t%d\t%.0f\n",
342 ustr, nbtasks, (unsigned long) (te - ts), value, 1.0 / (double) value, avgwork, thrzero, sqrdev);
343
344 exit(0);
345}
346