summaryrefslogtreecommitdiff
path: root/src/rt-app_args.c
blob: fc00180c6921f1e86e77f4cc24d82395d26aae55 (plain)
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
/* 
This file is part of rt-app - https://launchpad.net/rt-app
Copyright (C) 2010  Giacomo Bagnoli <g.bagnoli@asidev.com>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/ 

#include "rt-app_args.h"

void
usage (const char* msg, int ex_code)
{
#ifdef JSON
	printf("usage:\n"
	       "rt-app <taskset.json>\nOR\n");
#endif	       
	printf("rt-app [options] -t <period>:<exec>[:policy"
		"[:CPU affinity[:prio[:deadline]]]] -t ...\n\n");
	printf("-h, --help\t:\tshow this help\n");
	printf("-f, --fifo\t:\tset default policy for threads to SCHED_FIFO\n");
	printf("-r, --rr\t:\tset default policy fior threads to SCHED_RR\n");
	printf("-s, --spacing\t:\tmsec to wait beetween thread starts\n");
	printf("-l, --logdir\t:\tsave logs to different directory\n");
	printf("-b, --baselog\t:\tbasename for logs (implies -l . if not set)\n");
	printf("-G, --gnuplot\t:\tgenerate gnuplot script (needs -l)\n");
	printf("-D, --duration\t:\ttime (in seconds) before stopping threads\n");
	printf("-K, --no-mlock\t:\tDo not lock pages in memory\n");
	printf("-K, --no-mlock\t:\tDo not lock pages in memory\n");
	printf("-T, --ftrace\t:\tenable ftrace prints\n");
	
#ifdef AQUOSA
	printf("-q, --qos\t:\tcreate AQuoSA reservation\n");
	printf("-g, --frag\t:\tfragment for the reservation\n\n");
#else
#endif
	printf("\nPOLICY: f=SCHED_FIFO, r=SCHED_RR, o=SCHED_OTHER");
#ifdef DLSCHED
	printf(", d=SCHED_DEADLINE");
#endif
#ifdef AQUOSA
	printf(", q=AQuoSA\n");
	printf("when using AQuoSA scheduling, priority is used as"
		" percent increment \nfor budget over exec time\n");
#else
	printf("\n");
#endif
	printf("AFFINITY: comma-separated cpu index (starting from 0)\n");
	printf("\ti.e. 0,2,3 for first, third and fourth CPU\n");

	if (msg != NULL)
		printf("\n%s\n", msg);
	exit(ex_code);
}


void
parse_thread_args(char *arg, int idx, thread_data_t *tdata, policy_t def_policy)
{
	char *str = strdup(arg);
	char *token;
	long period, exec, dline;
	char tmp[256];
	int i = 0;
	int cpu;
	dline = 0;

	token = strtok(str, ":");
	tdata->name = malloc(sizeof(char) * 5);
	tdata->ind = idx;
	/* default name for command line threads */
	snprintf(tdata->name, 1, "t%d", tdata->ind); 
	tdata->sched_prio = DEFAULT_THREAD_PRIORITY;
	tdata->sched_policy = def_policy;
	tdata->cpuset = NULL;
	tdata->cpuset_str = NULL;

	while ( token != NULL)
	{
		switch(i) {
		case 0:
			period = strtol(token, NULL, 10);
			if (period <= 0 )
				usage("Cannot set negative period.", 
				      EXIT_INV_COMMANDLINE);
			tdata->period = usec_to_timespec(period);
			i++;
			break;

		case 1:
			exec = strtol(token,NULL, 10);
			//TODO: add support for max_et somehow
			if (exec > period)
				usage("Exec time cannot be greater than"
				      " period.", EXIT_INV_COMMANDLINE);
			if (exec <= 0 )
				usage("Cannot set negative exec time",
				      EXIT_INV_COMMANDLINE);
			tdata->min_et = usec_to_timespec(exec);
			tdata->max_et = usec_to_timespec(exec);
			i++;
			break;

		case 2:
#ifdef AQUOSA
			if (strcmp(token,"q") == 0)
				tdata->sched_policy = aquosa;
			else 
#endif
#ifdef DLSCHED
			if (strcmp(token,"d") == 0)
				tdata->sched_policy = deadline;
			else
#endif
			if (strcmp(token,"f") == 0)
				tdata->sched_policy = fifo;
			else if (strcmp(token,"r") == 0)
				tdata->sched_policy = rr ;
			else if (strcmp(token,"o") == 0)
				tdata->sched_policy = other;
			else {
				snprintf(tmp, 256, 
					"Invalid scheduling policy %s in %s",
					token, arg);
				usage(tmp, EXIT_INV_COMMANDLINE);
			}
			policy_to_string(tdata->sched_policy, 
					 tdata->sched_policy_descr);	

			i++;
			break;
		case 3:
			if (strcmp(token, "-") == 0)
				tdata->cpuset = NULL;
			else {
				tdata->cpuset = malloc (sizeof(cpu_set_t));
				tdata->cpuset_str = strdup(token);
			}
			i++;
			break;
		case 4:
			tdata->sched_prio = strtol(token, NULL, 10);
			// do not check, will fail in pthread_setschedparam
			i++;
			break;
		case 5:
			dline = strtol(token, NULL, 10);
			if (dline < exec)
				usage("Deadline cannot be less than "
				      "execution time", EXIT_INV_COMMANDLINE);
			if (dline > period)
				usage("Deadline cannot be greater than "
				      "period", EXIT_INV_COMMANDLINE);
			if (dline <= 0 )
				usage("Cannot set negative deadline",
				      EXIT_INV_COMMANDLINE);
			tdata->deadline = usec_to_timespec(dline);
			i++;
			break;
		}
		token = strtok(NULL, ":");
	}
	if ( i < 2 ) {
		printf("Period and exec time are mandatory\n");
		exit(EXIT_INV_COMMANDLINE);
	}

	if (dline == 0)
		tdata->deadline = tdata->period;
	
	/* set cpu affinity mask */
	if (tdata->cpuset_str)
	{
		snprintf(tmp, 256, "%s", tdata->cpuset_str);
		token = strtok(tmp, ",");
		while (token != NULL && i < 1000) {
			cpu = strtol(token, NULL, 10);
			CPU_SET(cpu, tdata->cpuset);
			strtok(NULL, ",");
			i++;
		}
	} else 
		tdata->cpuset_str = strdup("-");
	
	free(str);
}

static void
parse_command_line_options(int argc, char **argv, rtapp_options_t *opts)
{
	char tmp[PATH_LENGTH];
	char ch;
	int longopt_idx;
	int i;

	struct stat dirstat;

	/* set defaults */
	opts->spacing = 0;
	opts->gnuplot = 0;
	opts->lock_pages = 1;
	opts->duration = -1;
	opts->logbasename = strdup("rt-app");
	opts->logdir = NULL;
	opts->nthreads = 0;
	opts->ftrace = 0;
	opts->policy = other;
	opts->threads_data = malloc(sizeof(thread_data_t));
#ifdef AQUOSA
	opts->fragment = 1;
#endif
	static struct option long_options[] = {
	                   {"help", 0, 0, 'h'},
			   {"fifo", 0, 0, 'f'},
			   {"rr", 0, 0, 'r'},
			   {"thread", 1, 0, 't'},
			   {"spacing", 1, 0, 's'},
			   {"logdir", 1, 0, 'l'},
	                   {"baselog", 1, 0, 'b'},
			   {"gnuplot", 1, 0, 'G'},
			   {"duration", 1, 0, 'D'},
			   {"ftrace", 0, 0, 'T'},
#ifdef AQUOSA
			   {"qos", 0, 0, 'q'},
			   {"frag",1, 0, 'g'},
#endif
	                   {0, 0, 0, 0}
	               };
#ifdef AQUOSA
	while (( ch = getopt_long(argc,argv,"D:GKhfrb:s:l:qg:t:T", 
				  long_options, &longopt_idx)) != -1)
#else
	while (( ch = getopt_long(argc,argv,"D:GKhfrb:s:l:t:T", 
				  long_options, &longopt_idx)) != -1)
#endif
	{
		switch (ch)
		{
			case 'h':
				usage(NULL, EXIT_SUCCESS);
				break;
			case 'f':
				if (opts->policy != other)
					usage("Cannot set multiple policies",
					      EXIT_INV_COMMANDLINE);
				opts->policy = fifo;
				break;
			case 'r':
				if (opts->policy != other)
					usage("Cannot set multiple policies",
					      EXIT_INV_COMMANDLINE);
				opts->policy = rr;
				break;
			case 'b':
				if (!opts->logdir)	
					opts->logdir = strdup(".");
				opts->logbasename = strdup(optarg);
				break;
			case 's':
				opts->spacing  = strtol(optarg, NULL, 0);
				if (opts->spacing < 0)
					usage("Cannot set negative spacing",
					      EXIT_INV_COMMANDLINE);
				break;
			case 'l':
				opts->logdir = strdup(optarg);	
				lstat(opts->logdir, &dirstat);
				if (! S_ISDIR(dirstat.st_mode))
					usage("Cannot stat log directory",
					      EXIT_INV_COMMANDLINE);
				break;
			case 't':
				if (opts->nthreads > 0)
				{
					opts->threads_data = realloc(
						opts->threads_data, 
						(opts->nthreads+1) * \
							sizeof(thread_data_t));
				}
				parse_thread_args(optarg, opts->nthreads,
					&opts->threads_data[opts->nthreads],
					opts->policy);
				opts->nthreads++;
				break;
			case 'G':
				opts->gnuplot = 1;
				break;
			case 'D':
				opts->duration = strtol(optarg, NULL, 10);
				if (opts->duration < 0)
					usage("Cannot set negative duration",
					      EXIT_INV_COMMANDLINE);
				break;
			case 'K':
				opts->lock_pages = 0;
				break;
			case 'T':
				opts->ftrace = 1;
				break;
#ifdef AQUOSA				
			case 'q':
				if (opts->policy != other)
					usage("Cannot set multiple policies",
					      EXIT_INV_COMMANDLINE);
				opts->policy = aquosa;
				break;
			case 'g':
				opts->fragment = strtol(optarg, NULL, 10);
				if (opts->fragment < 1 || opts->fragment > 16)
					usage("Fragment divisor must be between"
					      "1 and 16", EXIT_INV_COMMANDLINE);
				break;
#endif
			default:
				log_error("Invalid option %c", ch);
				usage(NULL, EXIT_INV_COMMANDLINE);

		}

	}
	if ( opts->nthreads < 1)
		usage("You have to set parameters for at least one thread",
		      EXIT_INV_COMMANDLINE);
	
}

void
parse_command_line(int argc, char **argv, rtapp_options_t *opts)
{
#ifdef JSON
	if (argc < 2)
		usage(NULL, EXIT_SUCCESS);
	struct stat config_file_stat;
	if (stat(argv[1], &config_file_stat) == 0) {
		parse_config(argv[1], opts);
		return;
	}
	else if (strcmp(argv[1], "-") == 0) {
		parse_config_stdin(opts);
		return;
	} 
#endif
	parse_command_line_options(argc, argv, opts);
	opts->resources = NULL;
	opts->nresources = 0;
}