aboutsummaryrefslogtreecommitdiffstats
path: root/lib/genalloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/genalloc.c')
0 files changed, 0 insertions, 0 deletions
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
#include <sys/time.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <string.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] task_parameters_file duration\n"
		"       rt_spin -l\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;
}

#define fms_to_ns(x)	(lt_t)(((x) * __NS_PER_MS))
/*
 * <task_id, cpu, deadline (from job release), budget, offset> .
 */
int parse_edfwm_slice(FILE *ts, int slices_no, int task_id,
		struct edf_wm_params *wm)
{
	int i, tid;
	unsigned int cpu;
	double deadline, budget, offset;

	for (i = 0; i < slices_no; i++) {

		if (fscanf(ts, "%d %u %lf %lf %lf\n", &tid, &cpu,
				&deadline, &budget, &offset) != EOF) {

			printf("slice(tid, cpu, d, e, ph) = (%d, %u, %lf, %lf, %lf)\n",
					tid, cpu, deadline, budget, offset);

			if (task_id != tid) {
				fprintf(stderr, "task_id %d != tid %d\n",
						task_id, tid);
				return -1;
			}

			wm->slices[i].deadline = fms_to_ns(deadline);
			wm->slices[i].budget = fms_to_ns(budget);
			wm->slices[i].offset = fms_to_ns(offset);
		}
		if (ferror(ts)) {
			fprintf(stderr, "Cannot read file\n");
			return -1;
		}
	}
	return 1;
}

/* Custom format to read parameters for tasks from a plain text file:
 * 	<task_id, execution_cost, period, phase, cpu, slices_number> .
 * If the task is split on multiple slices, slices_number is non 0
 * and we scan a list of slice parameters up to slices_number:
 *	<task_id, cpu, deadline (from job release), budget, offset> .
 * The offset is the start time for the slice relative to the job release.
 *
 * FIXME: this function should go in a separate file where all the parsing
 * funtions for the semi-part plugins are. It will help in later testing with
 * "tasks" programs.
 */
int parse_edfwm_ts_file(FILE *ts, struct rt_task *rt)
{
	int task_id, ret = 1;
	unsigned int cpu, sliceno;
	double fwcet, fperiod, fphase;

	ret = fscanf(ts, "%d %lf %lf %lf %d %d\n",
			&task_id, &fwcet, &fperiod, &fphase, &cpu, &sliceno);

	if (ret != EOF) {

		printf("(tid, wcet, period, ph, cpu, slices) = "
			"(%d, %.2f, %.2f, %.2f, %u, %u)\n",
			task_id, fwcet, fperiod, fphase, cpu, sliceno);

		rt->exec_cost = fms_to_ns(fwcet);
		rt->period = fms_to_ns(fperiod);
		rt->phase = fms_to_ns(fphase);
		rt->cpu = cpu;
		rt->cls = RT_CLASS_HARD;
		rt->budget_policy = PRECISE_ENFORCEMENT;
		if (sliceno > 0) {
			ret = parse_edfwm_slice(ts, sliceno, task_id,
				(struct edf_wm_params*) &rt->semi_part);
			if (ret < 0)
				goto err;
		}
	}
	if (ferror(ts))
		goto err;

	return EOF;

err:
	fprintf(stderr, "Error parsing file\n");
	return -1;
}