aboutsummaryrefslogtreecommitdiffstats
path: root/bin/rtspin.c
blob: 0995d01696880642bd8e1ca132a8334bc00562af (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#include <sys/time.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <math.h>

#include "litmus.h"
#include "common.h"


static double cputime()
{
	struct timespec ts;
	int err;
	err = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
	if (err != 0)
		perror("clock_gettime");
	return (ts.tv_sec + 1E-9 * ts.tv_nsec);
}

static double wctime()
{
	struct timeval tv;
	gettimeofday(&tv, NULL);
	return (tv.tv_sec + 1E-6 * tv.tv_usec);
}

void usage(char *error) {
	fprintf(stderr, "Error: %s\n", error);
	fprintf(stderr,
		"Usage: rt_spin [-w] [-p PARTITION] [-c CLASS] [-A fp# -B fp# -W wave# -M max_wcet -P #jobs [-D #jobs]] WCET PERIOD DURATION\n"
		"       rt_spin -l\n"
		"  -A: Feedback control 'A' parameter\n"
		"  -B: Feedback control 'B' parameter\n"
		"  -W: 0 = flat; 1 = sine; 2 = triangle; 3 = box; 4 = sawtooth; 5 = reverse sawtooth\n"
		"  -M: max wcet (WCET is assumed min wcet)\n"
		"  -P: period of execution behavior wave\n"
		"  -D: Number of jobs wave should hold at extrema points (default = 0)\n");
	exit(1);
}

#define NUMS 4096
static int num[NUMS];
static double loop_length = 1.0;
static char* progname;

static int loop_once(void)
{
	int i, j = 0;
	for (i = 0; i < NUMS; i++)
		j += num[i]++;
	return j;
}

static int loop_for(double exec_time)
{
	double t = 0;
	int tmp = 0;	
/*	while (t + loop_length < exec_time) {
		tmp += loop_once();
		t += loop_length;
	}
*/
	double start = cputime();
	double now = cputime();
	while (now + loop_length < start + exec_time) {
		tmp += loop_once();
		t += loop_length;
		now = cputime();
	}

	return tmp;
}

static void fine_tune(double interval)
{
	double start, end, delta;

	start = wctime();
	loop_for(interval);
	end = wctime();
	delta = (end - start - interval) / interval;
	if (delta != 0)
		loop_length = loop_length / (1 - delta);
}

static void configure_loop(void)
{
	double start;

	/* prime cache */
	loop_once();
	loop_once();
	loop_once();

	/* measure */
	start = wctime();
	loop_once(); /* hope we didn't get preempted  */
	loop_length = wctime();
	loop_length -= start;

	/* fine tune */
	fine_tune(0.1);
	fine_tune(0.1);
	fine_tune(0.1);
}

static void show_loop_length(void)
{
	printf("%s/%d: loop_length=%f (%ldus)\n",
	       progname, getpid(), loop_length,
	       (long) (loop_length * 1000000));
}

static void debug_delay_loop(void)
{
	double start, end, delay;
	show_loop_length();
	while (1) {
		for (delay = 0.5; delay > 0.01; delay -= 0.01) {
			start = wctime();
			loop_for(delay);
			end = wctime();
			printf("%6.4fs: looped for %10.8fs, delta=%11.8fs, error=%7.4f%%\n",
			       delay,
			       end - start,
			       end - start - delay,
			       100 * (end - start - delay) / delay);
		}
	}
}

static int job(double exec_time)
{
	loop_for(exec_time);
	sleep_next_period();
	return 0;
}

fp_t float_to_frac(double f)
{
	fpbuf_t num;
	fpbuf_t denom = 1000; /* express in 1000ths */

	num = (fpbuf_t)f * denom;
	if(f != floor(f))
	{
		num += (fpbuf_t)(f - floor(f)) * denom;
	}

	return(_frac(num, denom));
}

#define OPTSTR "p:c:wld:veA:B:W:M:P:D:"

struct wave_data;
typedef double(*wave_func)(int, struct wave_data*);

struct wave_data
{
	double min_wcet_ms;
	double max_wcet_ms;
	double amplitude;
	int period;
	int extrema_delay;
	int min_point;
	int max_point;
	wave_func f;
};

double flat_wave(int count, struct wave_data* w)
{
	return(w->min_wcet_ms);
}

double sin_wave(int count, struct wave_data* w)
{
	double step = count % w->period;
	double exe = w->min_wcet_ms + w->amplitude * sin(M_PI*step/w->period);
	return(exe);
}

double tri_wave(int count, struct wave_data* w)
{
	double step = count % w->period;
	double v = 2.0 * (step/w->period - floor(step/w->period + 0.5));
	double exe = w->min_wcet_ms + w->amplitude * fabs(v);
	return(exe);
}

double box_wave(int count, struct wave_data* w)
{
	double step = count % w->period;
	double exe = w->min_wcet_ms + w->amplitude *((step/w->period < 0.5) ? 0 : 1);
	return(exe);
}

double saw_wave(int count, struct wave_data* w)
{
	double step = (count % w->period)/2.0;
	double v = 2.0 * (step/w->period - floor(step/w->period + 0.5));
	double exe;
	if(v < 0) v += 1.0;
	exe = w->min_wcet_ms + w->amplitude * v;
	return(exe);
}

double rsaw_wave(int count, struct wave_data* w)
{
	double step = (count % w->period)/2.0;
	double v = 2.0 * (step/w->period - floor(step/w->period + 0.5));
	double exe;
	if(v < 0) v += 1.0;
	exe = w->max_wcet_ms - w->amplitude * v;
	return(exe);
}

double get_execution_time(int count, struct wave_data* w)
{
	static int skips = 0;
	static int delay_count = 0;

	if(w->extrema_delay)
	{
		int at_max = (((count - skips) % w->period) == w->max_point);
		int at_min = (((count - skips) % w->period) == w->min_point);

		if((at_max || at_min) && delay_count++ < w->extrema_delay)
		{
			++skips;
			return((at_max) ? w->max_wcet_ms : w->min_wcet_ms);
		}
		else
		{
			delay_count = 0;
		}
	}

	return(w->f(count - skips, w));
}

int main(int argc, char** argv) 
{
	int ret;
	lt_t wcet;
	lt_t period;
	double wcet_ms, period_ms;
	int migrate = 0;
	int cpu = 0;
	int opt;
	int wait = 0;
	int test_loop = 0;
	int skip_config = 0;
	int verbose = 0;
	int want_enforcement = 0;
	double duration, start;
	task_class_t class = RT_CLASS_HARD;

	fp_t A = LITMUS_FP_ZERO, B = LITMUS_FP_ZERO;
	struct wave_data wave = {
			.min_wcet_ms = 0.0,
			.max_wcet_ms = 0.0,
			.amplitude = 0.0,
			.period = 0,
			.extrema_delay = 0,
			.max_point = 0,
			.min_point = 0,
			.f = NULL
	};

	progname = argv[0];

	while ((opt = getopt(argc, argv, OPTSTR)) != -1) {
		switch (opt) {
		case 'w':
			wait = 1;
			break;
		case 'p':
			cpu = atoi(optarg);
			migrate = 1;
			break;
		case 'c':
			class = str2class(optarg);
			if (class == -1)
				usage("Unknown task class.");
			break;
		case 'e':
			want_enforcement = 1;
			break;
		case 'l':
			test_loop = 1;
			break;
		case 'd':
			/* manually configure delay per loop iteration 
			 * unit: microseconds */
			loop_length = atof(optarg) / 1000000;
			skip_config = 1;
			break;
		case 'v':
			verbose = 1;
			break;
		case 'A':
			A = float_to_frac(atof(optarg));
			break;
		case 'B':
			B = float_to_frac(atof(optarg));
			break;
		case 'W':
			switch(atoi(optarg))
			{
				case 0:
					wave.f = flat_wave;
					break;
				case 1:
					wave.f = sin_wave;
					break;
				case 2:
					wave.f = tri_wave;
					break;
				case 3:
					wave.f = box_wave;
					break;
				case 4:
					wave.f = saw_wave;
					break;
				case 5:
					wave.f = rsaw_wave;
					break;
				default:
					usage("Bad wave identifier.");
					break;
			}
			break;
		case 'M':
			wave.max_wcet_ms = atof(optarg);
			break;
		case 'P':
			wave.period = atoi(optarg);
			if(wave.period <= 0)
				usage("Execution behavior period must be a positive number.");
			break;
		case 'D':
			wave.extrema_delay = atoi(optarg);
			break;
		case ':':
			usage("Argument missing.");
			break;
		case '?':
		default:
			usage("Bad argument.");
			break;
		}
	}


	if (!skip_config)
		configure_loop();

	if (test_loop) {
		debug_delay_loop();
		return 0;
	}

	if (argc - optind < 3)
		usage("Arguments missing.");
	wcet_ms   = atof(argv[optind + 0]);
	period_ms = atof(argv[optind + 1]);
	duration  = atof(argv[optind + 2]);
	wcet   = wcet_ms * __NS_PER_MS;
	period = period_ms * __NS_PER_MS;
	if (wcet <= 0)
		usage("The worst-case execution time must be a "
		      "positive number.");
	if (period <= 0)
		usage("The period must be a positive number.");
	if (wcet > period) {
		usage("The worst-case execution time must not "
		      "exceed the period.");
	}

	if(wave.f != NULL)
	{
		wave.min_wcet_ms = wcet_ms;
		wave.amplitude = wave.max_wcet_ms - wave.min_wcet_ms;
		if(wave.amplitude < 0.0)
		{
			usage("The max wcet must be greater than min wcet.");
		}

		if(wave.f == flat_wave)
		{
			wave.max_point = 0;
			wave.min_point = 0;
		}
		else if((wave.f == sin_wave) || (wave.f == tri_wave) || (wave.f == box_wave))
		{
			wave.max_point = wave.period/2;
			wave.min_point = 0;
		}
		else if(wave.f == saw_wave)
		{
			wave.max_point = wave.period-1;
			wave.min_point = 0;
		}
		else if(wave.f == rsaw_wave)
		{
			wave.max_point = 0;
			wave.min_point = wave.period-1;
		}

		if((wave.f == flat_wave) || (wave.f == box_wave))
		{
			wave.extrema_delay = 0;
		}
	}

	if (migrate) {
		ret = be_migrate_to(cpu);
		if (ret < 0)
			bail_out("could not migrate to target partition");
	}

	ret = sporadic_task_ns(wcet, period, 0,
				   A, B,
				   cpu, class,
			       want_enforcement ? PRECISE_ENFORCEMENT
			                        : NO_ENFORCEMENT,
			       migrate);

	if (ret < 0)
		bail_out("could not setup rt task params");

	if (verbose)
		show_loop_length();

	init_litmus();

	ret = task_mode(LITMUS_RT_TASK);
	if (ret != 0)
		bail_out("could not become RT task");

	if (wait) {
		ret = wait_for_ts_release();
		if (ret != 0)
			bail_out("wait_for_ts_release()");
	}

	start = wctime();

	int count = 0;
	while (start + duration > wctime())
	{
		if(wave.f == NULL)
		{
			job(wcet_ms * 0.0009); /* 90% wcet, in seconds */
		}
		else
		{
			job(get_execution_time(count++, &wave) * 0.0009);
		}
	}

	return 0;
}