diff --git a/emu_nosv.c b/emu_nosv.c index e1f2b1a..9d42aab 100644 --- a/emu_nosv.c +++ b/emu_nosv.c @@ -241,11 +241,14 @@ pre_task_end(struct ovni_emu *emu) } static void -pre_task_not_running(struct ovni_emu *emu) +pre_task_not_running(struct ovni_emu *emu, struct nosv_task *task) { struct ovni_ethread *th; th = emu->cur_thread; + if(task->state == TASK_ST_RUNNING) + die("task is still running\n"); + chan_set(&th->chan[CHAN_NOSV_TASKID], 0); chan_set(&th->chan[CHAN_NOSV_TYPE], 0); chan_set(&th->chan[CHAN_NOSV_APPID], 0); @@ -286,7 +289,7 @@ pre_task_running(struct ovni_emu *emu, struct nosv_task *task) static void pre_task_switch(struct ovni_emu *emu, struct nosv_task *prev_task, - struct nosv_task *next_task) + struct nosv_task *next_task, int newtask) { struct ovni_ethread *th; @@ -298,6 +301,15 @@ pre_task_switch(struct ovni_emu *emu, struct nosv_task *prev_task, if(prev_task == next_task) die("cannot switch to the same task\n"); + if(newtask && prev_task->state != TASK_ST_RUNNING) + die("previous task must not be no longer running\n"); + + if(!newtask && prev_task->state != TASK_ST_DEAD) + die("previous task must be dead\n"); + + if(next_task->state != TASK_ST_RUNNING) + die("next task must be running\n"); + if(next_task->id == 0) die("next task id cannot be 0\n"); @@ -319,13 +331,26 @@ pre_task_switch(struct ovni_emu *emu, struct nosv_task *prev_task, chan_set(&th->chan[CHAN_NOSV_TYPE], next_task->type->gid); } +static int +is_running_task(struct ovni_emu *emu, struct nosv_task **ptask) +{ + struct nosv_task *task = emu->cur_thread->task_stack; + if(task && task->state == TASK_ST_RUNNING) + { + *ptask = task; + return 1; + } + + return 0; +} + static void pre_task(struct ovni_emu *emu) { - struct nosv_task *prev_task, *next_task; - - prev_task = emu->cur_thread->task_stack; + struct nosv_task *prev_task = NULL; + int was_running_task = is_running_task(emu, &prev_task); + /* Update the emulator state, but don't modify the channels yet */ switch(emu->cur_ev->header.value) { case 'c': pre_task_create(emu); break; @@ -337,17 +362,33 @@ pre_task(struct ovni_emu *emu) abort(); } - next_task = emu->cur_thread->task_stack; + struct nosv_task *next_task = NULL; + int runs_task_now = is_running_task(emu, &next_task); - /* Unless we're creating a task, register the switch */ - if(emu->cur_ev->header.value != 'c') + /* Now that we know if the emulator was running a task before + * or if it's running one now, update the channels accordingly. */ + switch(emu->cur_ev->header.value) { - if(next_task == NULL) - pre_task_not_running(emu); - else if(prev_task == NULL) + case 'x': /* Execute: either a nested task or a new one */ + if(was_running_task) + pre_task_switch(emu, prev_task, next_task, 1); + else + pre_task_running(emu, next_task); + break; + case 'e': /* End: either a nested task or the last one */ + if(runs_task_now) + pre_task_switch(emu, prev_task, next_task, 0); + else + pre_task_not_running(emu, prev_task); + break; + case 'p': /* Pause */ + pre_task_not_running(emu, prev_task); + break; + case 'r': /* Resume */ pre_task_running(emu, next_task); - else - pre_task_switch(emu, prev_task, next_task); + break; + default: + break; } } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d9d086f..8f0fd3e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -40,3 +40,4 @@ ovni_test("mp-rank" "mp-driver.sh") ovni_test("nosv-nested-tasks" "driver.sh") ovni_test("nosv-nested-tasks-bad" "driver.sh") ovni_test("nosv-task-types" "mp-driver.sh") +ovni_test("nosv-pause" "mp-driver.sh") diff --git a/test/nosv-pause.c b/test/nosv-pause.c new file mode 100644 index 0000000..e6631c2 --- /dev/null +++ b/test/nosv-pause.c @@ -0,0 +1,211 @@ +/* + * Copyright (c) 2021-2022 Barcelona Supercomputing Center (BSC) + * + * 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 3 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, see . + */ + +#define _GNU_SOURCE + +#include "ovni.h" +#include "compat.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +static void +fail(const char *msg) +{ + fprintf(stderr, "%s\n", msg); + abort(); +} + +static inline void +emit_ev(char *mcv) +{ + struct ovni_ev ev = { 0 }; + + ovni_ev_set_mcv(&ev, mcv); + ovni_ev_set_clock(&ev, ovni_clock_now()); + ovni_ev_emit(&ev); +} + +#define INSTR_3ARG(name, mcv, ta, a, tb, b, tc, c) \ + static inline void name(ta a, tb b, tc c) \ + { \ + struct ovni_ev ev = {0}; \ + ovni_ev_set_mcv(&ev, mcv); \ + ovni_ev_set_clock(&ev, ovni_clock_now()); \ + ovni_payload_add(&ev, (uint8_t *)&a, sizeof(a)); \ + ovni_payload_add(&ev, (uint8_t *)&b, sizeof(b)); \ + ovni_payload_add(&ev, (uint8_t *)&c, sizeof(c)); \ + ovni_ev_emit(&ev); \ + } + +INSTR_3ARG(instr_thread_execute, "OHx", int32_t, cpu, int32_t, creator_tid, uint64_t, tag) + +static inline void +instr_thread_end(void) +{ + struct ovni_ev ev = {0}; + + ovni_ev_set_mcv(&ev, "OHe"); + ovni_ev_set_clock(&ev, ovni_clock_now()); + ovni_ev_emit(&ev); + + /* Flush the events to disk before killing the thread */ + ovni_flush(); +} + +static inline void +instr_start(int rank, int nranks) +{ + char hostname[HOST_NAME_MAX]; + + if(gethostname(hostname, HOST_NAME_MAX) != 0) + fail("gethostname failed"); + + ovni_proc_init(1, hostname, getpid()); + + ovni_proc_set_rank(rank, nranks); + + ovni_thread_init(gettid()); + + /* Only the rank 0 inform about all CPUs */ + if(rank == 0) + { + /* Fake nranks cpus */ + for(int i=0; i < nranks; i++) + ovni_add_cpu(i, i); + } + + int curcpu = rank; + + fprintf(stderr, "thread %d has cpu %d (ncpus=%d)\n", + gettid(), curcpu, nranks); + + instr_thread_execute(curcpu, -1, 0); +} + +static inline void +instr_end(void) +{ + instr_thread_end(); + ovni_thread_free(); + ovni_proc_fini(); +} + +static void +type_create(int32_t typeid) +{ + struct ovni_ev ev = {0}; + + ovni_ev_set_mcv(&ev, "VYc"); + ovni_ev_set_clock(&ev, ovni_clock_now()); + + char buf[256]; + char *p = buf; + + size_t nbytes = 0; + memcpy(buf, &typeid, sizeof(typeid)); + p += sizeof(typeid); + nbytes += sizeof(typeid); + sprintf(p, "testtype%d", typeid); + nbytes += strlen(p) + 1; + + ovni_ev_jumbo_emit(&ev, (uint8_t *) buf, nbytes); +} + +static void +task_begin(int32_t id, uint32_t typeid) +{ + struct ovni_ev ev = {0}; + + ovni_ev_set_mcv(&ev, "VTc"); + ovni_ev_set_clock(&ev, ovni_clock_now()); + ovni_payload_add(&ev, (uint8_t *) &id, sizeof(id)); + ovni_payload_add(&ev, (uint8_t *) &typeid, sizeof(id)); + ovni_ev_emit(&ev); + + memset(&ev, 0, sizeof(ev)); + + ovni_ev_set_mcv(&ev, "VTx"); + ovni_ev_set_clock(&ev, ovni_clock_now()); + ovni_payload_add(&ev, (uint8_t *) &id, sizeof(id)); + ovni_ev_emit(&ev); +} + +static void +task_pause(int32_t id) +{ + struct ovni_ev ev = {0}; + + ovni_ev_set_mcv(&ev, "VTp"); + ovni_ev_set_clock(&ev, ovni_clock_now()); + ovni_payload_add(&ev, (uint8_t *) &id, sizeof(id)); + ovni_ev_emit(&ev); +} + +static void +task_resume(int32_t id) +{ + struct ovni_ev ev = {0}; + + ovni_ev_set_mcv(&ev, "VTr"); + ovni_ev_set_clock(&ev, ovni_clock_now()); + ovni_payload_add(&ev, (uint8_t *) &id, sizeof(id)); + ovni_ev_emit(&ev); +} + +static void +task_end(int32_t id) +{ + struct ovni_ev ev = {0}; + + ovni_ev_set_mcv(&ev, "VTe"); + ovni_ev_set_clock(&ev, ovni_clock_now()); + ovni_payload_add(&ev, (uint8_t *) &id, sizeof(id)); + ovni_ev_emit(&ev); +} + +int +main(void) +{ + int rank = atoi(getenv("OVNI_RANK")); + int nranks = atoi(getenv("OVNI_NRANKS")); + instr_start(rank, nranks); + + int us = 500; + uint32_t typeid = 1; + + type_create(typeid); + + task_begin(1, typeid); + usleep(us); + task_pause(1); + usleep(us); + task_resume(1); + usleep(us); + task_end(1); + + instr_end(); + + return 0; +} +