diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 35cb319..780f1d1 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -16,6 +16,7 @@ jobs: - batch - pool - syscall + - cosched steps: - uses: actions/checkout@v2 diff --git a/apps.c b/apps.c index c889fd5..212fa83 100644 --- a/apps.c +++ b/apps.c @@ -5,6 +5,7 @@ #include #include +#include "sched.h" #include "usyscall.h" #include "pool.h" @@ -15,6 +16,8 @@ static int g_retcode; X(retcode) \ X(pooltest) \ X(syscalltest) \ + X(coapp) \ + X(cosched) \ #define DECLARE(X) static int X(int, char *[]); APPS_X(DECLARE) @@ -52,6 +55,51 @@ static int retcode(int argc, char *argv[]) { return 0; } +struct coapp_ctx { + int cnt; +} ctxarray[16]; +struct pool ctxpool = POOL_INITIALIZER_ARRAY(ctxarray); + +static void coapp_task(void *_ctx) { + struct coapp_ctx *ctx = _ctx; + + printf("%16s id %d cnt %d\n", __func__, 1 + ctx - ctxarray, ctx->cnt); + + if (0 < ctx->cnt) { + sched_cont(coapp_task, ctx, 2); + } + + --ctx->cnt; +} + +static void coapp_rt(void *_ctx) { + struct coapp_ctx *ctx = _ctx; + + printf("%16s id %d cnt %d\n", __func__, 1 + ctx - ctxarray, ctx->cnt); + + sched_time_elapsed(1); + + if (0 < ctx->cnt) { + sched_cont(coapp_rt, ctx, 0); + } + + --ctx->cnt; +} + +static int coapp(int argc, char* argv[]) { + int entry_id = atoi(argv[1]) - 1; + + struct coapp_ctx *ctx = pool_alloc(&ctxpool); + ctx->cnt = atoi(argv[2]); + + void (*entries[])(void*) = { coapp_task, coapp_rt }; + sched_new(entries[entry_id], ctx, atoi(argv[3]), atoi(argv[4])); +} + +static int cosched(int argc, char* argv[]) { + sched_run(atoi(argv[1])); +} + static int exec(int argc, char *argv[]) { const struct app *app = NULL; for (int i = 0; i < ARRAY_SIZE(app_list); ++i) { @@ -102,12 +150,12 @@ int shell(int argc, char *argv[]) { char *stcmd; char *cmd = strtok_r(line, comsep, &stcmd); while (cmd) { - const char *argsep = " "; + const char *argsep = " \t"; char *starg; char *arg = strtok_r(cmd, argsep, &starg); char *argv[256]; int argc = 0; - while (arg) { + while (arg && arg[0] != '#') { argv[argc++] = arg; arg = strtok_r(NULL, argsep, &starg); } diff --git a/sched.c b/sched.c new file mode 100644 index 0000000..59ff079 --- /dev/null +++ b/sched.c @@ -0,0 +1,29 @@ +#include +#include +#include + +#include "sched.h" +#include "pool.h" + +static int time; + +void sched_new(void (*entrypoint)(void *aspace), + void *aspace, + int priority, + int deadline) { + +} + +void sched_cont(void (*entrypoint)(void *aspace), + void *aspace, + int timeout) { + +} + +void sched_time_elapsed(unsigned amount) { + time += amount; + +} + +void sched_run(enum policy policy) { +} diff --git a/sched.h b/sched.h new file mode 100644 index 0000000..462dbec --- /dev/null +++ b/sched.h @@ -0,0 +1,33 @@ +#pragma once + +enum policy { + // first-in, first-out; run tasks in order of their arrival + POLICY_FIFO, + + // highest priority task (highest priority value) should be executed + // first. Use round-robin for processes with same priority + // (task from 1st process, from 2nd, ... Nth, 1st, 2nd, ...) + POLICY_PRIO, + + // consider deadline, execute process with Earliest Deadline First. + // Fallback to priority policy if deadlines are equal + POLICY_DEADLINE, +}; + +// Add new task +extern void sched_new(void (*entrypoint)(void *ctx), // entrypoint function + void *ctx, // context of the process + int priority, // priority, [0 - 10], bigger for more priority + int deadline); // absolute time till the task should be completed, <=0 for no deadline + +// Continue process from function after some amount of time +extern void sched_cont(void (*entrypoint)(void *aspace), // entrypoint function + void *aspace,// addresses the process can access + int timeout); // when the continuation became runnable + +// Notify scheduler that some amount of time passed +extern void sched_time_elapsed(unsigned amount); + +// Scheduler loop, start executing tasks until all of them finish +extern void sched_run(enum policy policy); + diff --git a/test/cosched/1.in b/test/cosched/1.in new file mode 100644 index 0000000..db92bf7 --- /dev/null +++ b/test/cosched/1.in @@ -0,0 +1,5 @@ +# entryid cnt prio deadline +coapp 2 10 0 0 +coapp 2 10 0 0 + +cosched 0 diff --git a/test/cosched/1.out b/test/cosched/1.out new file mode 100644 index 0000000..2a2a491 --- /dev/null +++ b/test/cosched/1.out @@ -0,0 +1,22 @@ + coapp_rt id 1 cnt 10 + coapp_rt id 2 cnt 10 + coapp_rt id 1 cnt 9 + coapp_rt id 2 cnt 9 + coapp_rt id 1 cnt 8 + coapp_rt id 2 cnt 8 + coapp_rt id 1 cnt 7 + coapp_rt id 2 cnt 7 + coapp_rt id 1 cnt 6 + coapp_rt id 2 cnt 6 + coapp_rt id 1 cnt 5 + coapp_rt id 2 cnt 5 + coapp_rt id 1 cnt 4 + coapp_rt id 2 cnt 4 + coapp_rt id 1 cnt 3 + coapp_rt id 2 cnt 3 + coapp_rt id 1 cnt 2 + coapp_rt id 2 cnt 2 + coapp_rt id 1 cnt 1 + coapp_rt id 2 cnt 1 + coapp_rt id 1 cnt 0 + coapp_rt id 2 cnt 0 diff --git a/test/cosched/2.in b/test/cosched/2.in new file mode 100644 index 0000000..0e021e7 --- /dev/null +++ b/test/cosched/2.in @@ -0,0 +1,6 @@ +# entryid cnt prio deadline +coapp 2 2 2 0 +coapp 2 2 0 0 +coapp 2 2 0 0 + +cosched 1 diff --git a/test/cosched/2.out b/test/cosched/2.out new file mode 100644 index 0000000..47a058f --- /dev/null +++ b/test/cosched/2.out @@ -0,0 +1,9 @@ + coapp_rt id 1 cnt 2 + coapp_rt id 1 cnt 1 + coapp_rt id 1 cnt 0 + coapp_rt id 2 cnt 2 + coapp_rt id 3 cnt 2 + coapp_rt id 2 cnt 1 + coapp_rt id 3 cnt 1 + coapp_rt id 2 cnt 0 + coapp_rt id 3 cnt 0 diff --git a/test/cosched/3.in b/test/cosched/3.in new file mode 100644 index 0000000..2bf55b7 --- /dev/null +++ b/test/cosched/3.in @@ -0,0 +1,6 @@ +# entryid cnt prio deadline +coapp 2 2 0 -1 +coapp 2 2 0 2 +coapp 2 2 0 -1 + +cosched 2 diff --git a/test/cosched/3.out b/test/cosched/3.out new file mode 100644 index 0000000..698f213 --- /dev/null +++ b/test/cosched/3.out @@ -0,0 +1,9 @@ + coapp_rt id 2 cnt 2 + coapp_rt id 2 cnt 1 + coapp_rt id 2 cnt 0 + coapp_rt id 1 cnt 2 + coapp_rt id 3 cnt 2 + coapp_rt id 1 cnt 1 + coapp_rt id 3 cnt 1 + coapp_rt id 1 cnt 0 + coapp_rt id 3 cnt 0 diff --git a/test/cosched/4.in b/test/cosched/4.in new file mode 100644 index 0000000..2b984b5 --- /dev/null +++ b/test/cosched/4.in @@ -0,0 +1,5 @@ +# entryid cnt prio deadline +coapp 1 4 2 0 +coapp 2 16 0 0 + +cosched 1 diff --git a/test/cosched/4.out b/test/cosched/4.out new file mode 100644 index 0000000..b6b56cc --- /dev/null +++ b/test/cosched/4.out @@ -0,0 +1,22 @@ + coapp_task id 1 cnt 4 + coapp_rt id 2 cnt 16 + coapp_rt id 2 cnt 15 + coapp_task id 1 cnt 3 + coapp_rt id 2 cnt 14 + coapp_rt id 2 cnt 13 + coapp_task id 1 cnt 2 + coapp_rt id 2 cnt 12 + coapp_rt id 2 cnt 11 + coapp_task id 1 cnt 1 + coapp_rt id 2 cnt 10 + coapp_rt id 2 cnt 9 + coapp_task id 1 cnt 0 + coapp_rt id 2 cnt 8 + coapp_rt id 2 cnt 7 + coapp_rt id 2 cnt 6 + coapp_rt id 2 cnt 5 + coapp_rt id 2 cnt 4 + coapp_rt id 2 cnt 3 + coapp_rt id 2 cnt 2 + coapp_rt id 2 cnt 1 + coapp_rt id 2 cnt 0 diff --git a/test/cosched/run-test.sh b/test/cosched/run-test.sh new file mode 100755 index 0000000..145f39a --- /dev/null +++ b/test/cosched/run-test.sh @@ -0,0 +1,2 @@ + +map_inputs checkdiff