aboutsummaryrefslogtreecommitdiffstats
path: root/bin/rt_pointer.c
blob: bfd9fec87efb861f63f57e66045185311e32d743 (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
#include <sys/time.h>
#include <sys/mman.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <assert.h>
#include <limits.h>


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

#define MIN_FIELD_SIZE 16
#define MAX_FIELD_SIZE 16777216
#define MIN_WINDOW_SIZE 1
#define MAX_WINDOW_SIZE 15
#define MIN_HOP_LIMIT 1

#define MAX_HOP_LIMIT 4294967295U

#define MIN_SEED -2147483647
#define MAX_SEED -1
#define MIN_THREADS 1
#define MAX_THREADS 256

static char* progname;
int loops = 1;
struct timeval t1, t2;

unsigned int *field;
unsigned int f_max;
unsigned short int w;
unsigned int maxhops;
int seed = -2;
unsigned int n_max;

clock_t startTime;

struct threadS{
unsigned int initial;
unsigned int minStop;
unsigned int maxStop;
unsigned int hops;
}*thread;

int init_job() {
 //fscanf(stdin, "%lu %u %lu %ld %u", 
 //	&f, &l, &maxhops, &seed, &n);
 
 //f_max = 102400; //1048570;
 f_max = 409600;
 //f_max = 240000; // case study
 maxhops = 512000;
 //n_max = 50;
 n_max = 10;
 
 assert ((f_max >= MIN_FIELD_SIZE) && (f_max <= MAX_FIELD_SIZE));
 assert ((maxhops >= MIN_HOP_LIMIT) && (maxhops <= MAX_HOP_LIMIT));
 assert ((n_max >= MIN_THREADS) && (n_max <= MAX_THREADS));
 if ((thread = (struct threadS *)malloc(n_max*sizeof(struct threadS))) == NULL)
   return (-1);

 /*for (l=0; l<n; l++){
   //fscanf(stdin, "%lu %lu %lu", 
	  //&(thread[l].initial), &(thread[l].minStop), &(thread[l].maxStop));
   thread[l].initial = 10;
   thread[l].minStop = f - thread[l].initial;
   thread[l].maxStop = f - thread[l].initial;
   
   assert ((thread[l].initial >= 0) && (thread[l].initial < f));
   assert ((thread[l].minStop >= 0) && (thread[l].minStop < f));
   assert ((thread[l].maxStop >= 0) && (thread[l].maxStop < f));
 }
*/
 if ((field = (unsigned int *)malloc(f_max*sizeof(int))) == NULL)
     return (-1);

 //randInit(seed);
 /*
 for (l=0; l<f; l++){
   field[l] = randInt(0, f-w);
 }
 */
 return 0;
}

int main_job() {
	unsigned int l;
	unsigned int f, n;
	unsigned short int w;

	/* online appendix setting */
	
	f = randInt(2048,f_max);
	w = 1; //randInt(0,4)*2 + 1;
	maxhops = 204800; //4096;
	n = randInt(1,20);
	

	/* case-study */
/*	
	f = randInt(1024,240000); //409600);
	w = 1; //randInt(0,4)*2 + 1;
	maxhops = 1024; //4096;
	n = 1; //randInt(4,30);
*/	
	
	assert ((w >= MIN_WINDOW_SIZE) && (w <= MAX_WINDOW_SIZE));
	assert (w % 2 == 1);
	assert (f > w);
 
 for (l=0; l<n; l++){
   //fscanf(stdin, "%lu %lu %lu", 
	  //&(thread[l].initial), &(thread[l].minStop), &(thread[l].maxStop));
   thread[l].initial = 1;
   thread[l].minStop = f - thread[l].initial;
   thread[l].maxStop = f - thread[l].initial;
   
   assert ((thread[l].initial >= 0) && (thread[l].initial < f));
   assert ((thread[l].minStop >= 0) && (thread[l].minStop < f));
   assert ((thread[l].maxStop >= 0) && (thread[l].maxStop < f));
 }
 
 for (l=0; l<f; l++){
   field[l] = randInt(0, f-w);
 }
 
	for (l=0; l<n; l++) {
	  unsigned int index;
	  unsigned int minStop, maxStop;
	  unsigned int hops;
	 
	  hops = 0;
	  minStop = thread[l].minStop;
	  maxStop = thread[l].maxStop;
	  index = thread[l].initial;
	  while ((hops < maxhops) &&
			(!((index >= minStop) &&
			 (index < maxStop)))){

		unsigned int ll, lll;
		unsigned int max, min;
		unsigned int partition;
		unsigned int high;
	 
		partition = field[index];
		max = MAX_FIELD_SIZE;
		min = 0;
		high = 0;
	 
	 for (ll=0; ll<w; ll++){
	   unsigned int balance;
	   unsigned int x;
		x = field[index+ll];

	   if (x > max) high++;
	   else if (x > min){ /* start else* */
		 partition = x;
		 balance = 0;
		 for (lll=ll+1; lll<w; lll++){
		   if (field[index+lll] > partition) balance++;
		 }/* end for loop */

		 if (balance+high == w/2) break;
		 else if (balance+high > w/2){
			  min = partition;
		 }/* end if */
		 else { 
			  max = partition;
			  high++;
		 }/* end else */
	  }
	   if (min == max) break;
	 } /* end else* */
	  index = (partition+hops)%(f-w);
	  hops++;
	 }/* end loop ll */
	  thread[l].hops = hops;
	} /* end while */
 
 return(0);
}

int post_job() {
	if (field) {
		free(field);
		field = NULL;
	}
	if (thread) {
		free(thread);
		thread = NULL;
	}
	return 0;
}

static void usage(char *error) {
	fprintf(stderr, "Error: %s\n", error);
	fprintf(stderr,
		"Usage:\n"
		"	rt_spin [COMMON-OPTS] WCET PERIOD DURATION\n"
		"	rt_spin [COMMON-OPTS] -f FILE [-o COLUMN] WCET PERIOD\n"
		"	rt_spin -l\n"
		"\n"
		"COMMON-OPTS = [-w] [-s SCALE]\n"
		"              [-p PARTITION/CLUSTER [-z CLUSTER SIZE]] [-c CLASS] [-m CRITICALITY LEVEL]\n"
		"\n"
		"WCET and PERIOD are microseconds, DURATION is seconds.\n");
	exit(EXIT_FAILURE);
}

inline unsigned long get_cyclecount (void)
{
	unsigned long value;
	// Read CCNT Register
	asm volatile ("MRC p15, 0, %0, c9, c13, 0\t\n": "=r"(value));
	return value;
}

static int job(double exec_time, double program_end)
{
	if (wctime() > program_end)
		return 0;
	else {
		register int iter = 0;
		//register unsigned long t;
		//t = get_cyclecount();
		//init_job();
		//gettimeofday(&t1, NULL);
		//while (iter++ < loops) {
			main_job();
		//}
		//t = get_cyclecount() - t;
		//printf("%ld cycles\n", t);
		//gettimeofday(&t2, NULL);
		//printf("%ld\n", ((t2.tv_sec * 1000000 + t2.tv_usec) - (t1.tv_sec * 1000000 + t1.tv_usec)));
		//post_job();
		sleep_next_period();
		return 1;
	}
}

#define OPTSTR "p:wves:l:m:i:b:"
int main(int argc, char** argv)
{
	int ret;
	lt_t wcet;
	lt_t period;
	lt_t budget;
	double wcet_ms, period_ms, budget_ms;
	unsigned int priority = LITMUS_NO_PRIORITY;
	int migrate = 0;
	int cluster = 0;
	int opt;
	int wait = 0;
	int want_enforcement = 0;
	double duration = 0, start = 0;
	double scale = 1.0;
	task_class_t class = RT_CLASS_HARD;
	struct rt_task param;
	struct mc2_task mc2_param;
	struct reservation_config config;
	int res_type = PERIODIC_POLLING;

	progname = argv[0];

	/* default for reservation */
	config.id = 0;
	config.priority = LITMUS_NO_PRIORITY; /* use EDF by default */
	config.cpu = -1;
	
	mc2_param.crit = CRIT_LEVEL_C;
	
	budget_ms = 10;

	while ((opt = getopt(argc, argv, OPTSTR)) != -1) {
		switch (opt) {
		case 'w':
			wait = 1;
			break;
		case 'p':
			cluster = atoi(optarg);
			migrate = 1;
			config.cpu = cluster;
			break;
		case 'e':
			want_enforcement = 1;
			break;
		case 's':
			scale = atof(optarg);
			break;
		case 'l':
			loops = atoi(optarg);
			break;
		case 'm':
			mc2_param.crit = atoi(optarg);
			if (mc2_param.crit < CRIT_LEVEL_A || mc2_param.crit == NUM_CRIT_LEVELS) {
				usage("Invalid criticality level.");
			}
			res_type = PERIODIC_POLLING;
			break;
		case 'b':
			budget_ms = atof(optarg);
			break;
		case 'i':
			config.priority = atoi(optarg);
			break;
		case ':':
			usage("Argument missing.");
			break;
		case '?':
		default:
			usage("Bad argument.");
			break;
		}
	}

	if (mc2_param.crit > CRIT_LEVEL_A && config.priority != LITMUS_NO_PRIORITY)
		usage("Bad criticailty level or priority");

	if (argc - optind < 3)
		usage("Arguments missing.");

	wcet_ms   = atof(argv[optind + 0]);
	period_ms = atof(argv[optind + 1]);

	wcet   = ms2ns(wcet_ms);
	period = ms2ns(period_ms);
	budget = ms2ns(budget_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.");
	}

	duration  = atof(argv[optind + 2]);

	if (migrate) {
		ret = be_migrate_to_domain(cluster);
		if (ret < 0)
			bail_out("could not migrate to target partition or cluster.");
	}

	/* reservation config */
	config.id = gettid();
	
	config.polling_params.budget = budget;
	config.polling_params.period = period;
	config.polling_params.offset = 0;
	config.polling_params.relative_deadline = 0;
	if (config.polling_params.budget > config.polling_params.period) {
		usage("The budget must not exceed the period.");
	}
	
	/* create a reservation */
	ret = reservation_create(res_type, &config);
	if (ret < 0) {
		bail_out("failed to create reservation.");
	}
	//srand (time(NULL));
    //randInit((-rand()%65535)-2);
	randInit(-2);
	
	init_job();
	
	init_rt_task_param(&param);
	param.exec_cost = wcet;
	param.period = period;
	param.priority = priority;
	param.cls = class;
	param.release_policy = TASK_PERIODIC;
	param.budget_policy = (want_enforcement) ?
			PRECISE_ENFORCEMENT : NO_ENFORCEMENT;
	if (migrate) {
		param.cpu = gettid();
	}
	ret = set_rt_task_param(gettid(), &param);
	
	if (ret < 0)
		bail_out("could not setup rt task params");
	
	mc2_param.res_id = gettid();
	ret = set_mc2_task_param(gettid(), &mc2_param);
//printf("SET_MC2_TASK\n");
	if (ret < 0)
		bail_out("could not setup mc2 task params");

	init_litmus();
//printf("CALL\n");
	if (mc2_param.crit == CRIT_LEVEL_C)
		set_page_color(-1);
	else if (mc2_param.crit < CRIT_LEVEL_C)
		set_page_color(config.cpu);
//printf("CALL\n");

//printf("INIT_LITMUS\n");
	start = wctime();
	ret = task_mode(LITMUS_RT_TASK);
//printf("TASK_MODE\n");
	if (ret != 0)
		bail_out("could not become RT task");


	if (wait) {
//printf("BEFORE WAIT\n");
		ret = wait_for_ts_release();
		if (ret != 0)
			bail_out("wait_for_ts_release()");
		start = wctime();
	}

	while (job(wcet_ms * 0.001 * scale, start + duration)) {};

	ret = task_mode(BACKGROUND_TASK);
	if (ret != 0)
		bail_out("could not become regular task (huh?)");

	reservation_destroy(gettid(), config.cpu);
	//post_job();
	printf("%s/%d finished.\n",progname, gettid());	
	return 0;
}