From 289b53f7b55a30c054e4a2fb7b19fa2cbcf27703 Mon Sep 17 00:00:00 2001 From: Andrea Bastoni Date: Wed, 22 Sep 2010 12:06:26 -0400 Subject: Add rtspin_edffm launcher function It reads the parameter for a task from a text file. The format is: - (for task parameters). - (for slices). Example: 15 2.96343109764 10 0 3 7 15 3 1.428571 1.175260 0.000000 15 7 2.857143 0.700065 1.428571 15 2 4.285714 0.504732 2.857143 15 0 5.714286 0.279637 4.285714 15 1 7.142857 0.185294 5.714286 15 4 8.571429 0.065379 7.142857 15 6 10.000000 0.053062 8.571429 --- SConstruct | 1 + bin/rtspin_edfwm.c | 319 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 320 insertions(+) create mode 100644 bin/rtspin_edfwm.c diff --git a/SConstruct b/SConstruct index 657a92f..1f4770a 100644 --- a/SConstruct +++ b/SConstruct @@ -213,6 +213,7 @@ rt.Program('rt_launch_edffm', ['bin/rt_launch_edffm.c', 'bin/common.c']) rt.Program('rtspin_npsf', ['bin/rtspin_npsf.c', 'bin/common.c']) rt.Program('npsf_add_server', ['bin/npsf_add_server.c', 'bin/common.c']) rt.Program('rt_launch_npsf', ['bin/rt_launch_npsf.c', 'bin/common.c']) +rt.Program('rtspin_edfwm', ['bin/rtspin_edfwm.c', 'bin/common.c']) rt.Program('release_ts', 'bin/release_ts.c') rtm.Program('measure_syscall', 'bin/null_call.c') diff --git a/bin/rtspin_edfwm.c b/bin/rtspin_edfwm.c new file mode 100644 index 0000000..9a78aa3 --- /dev/null +++ b/bin/rtspin_edfwm.c @@ -0,0 +1,319 @@ +#include + +#include +#include +#include +#include +#include + +#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)) +/* + * . + */ +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: + * . + * 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: + * . + * 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; +} + +#define OPTSTR "wld:v" + +int main(int argc, char** argv) +{ + int ret; + + int opt; + int wait = 0; + int test_loop = 0; + int skip_config = 0; + int verbose = 0; + double wcet_ms; + double duration, start; + + struct rt_task rt; + FILE *file; + + progname = argv[0]; + + while ((opt = getopt(argc, argv, OPTSTR)) != -1) { + switch (opt) { + case 'w': + wait = 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 ':': + 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 < 2) + usage("Arguments missing."); + + if ((file = fopen(argv[optind + 0], "r")) == NULL) { + fprintf(stderr, "Cannot open %s\n", argv[1]); + return -1; + } + duration = atof(argv[optind + 1]); + + memset(&rt, 0, sizeof(struct rt_task)); + + while (parse_edfwm_ts_file(file, &rt) != EOF) { + + if (sporadic_task_ns_semi(&rt) < 0) + bail_out("could not setup rt task params"); + } + + fclose(file); + + 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()"); + } + + wcet_ms = ((double) rt.exec_cost ) / __NS_PER_MS; + start = wctime(); + + while (start + duration > wctime()) { + job(wcet_ms * 0.0009); /* 90% wcet, in seconds */ + } + + return 0; +} -- cgit v1.2.2