/* * Copyright (c) 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 . */ #include "uthash.h" #include "utlist.h" #include "ovni.h" #include "emu.h" #include "emu_task.h" #include "prv.h" #include "chan.h" void hook_init_nanos6(struct ovni_emu *emu) { struct ovni_ethread *th; struct ovni_cpu *cpu; struct ovni_chan **uth, **ucpu; int row; FILE *prv_th, *prv_cpu; int64_t *clock; clock = &emu->delta_time; prv_th = emu->prv_thread; prv_cpu = emu->prv_cpu; /* Init the channels in all threads */ for(size_t i=0; itotal_nthreads; i++) { th = emu->global_thread[i]; row = th->gindex + 1; uth = &emu->th_chan; chan_th_init(th, uth, CHAN_NANOS6_TASKID, CHAN_TRACK_TH_RUNNING, 0, 0, 1, row, prv_th, clock); chan_th_init(th, uth, CHAN_NANOS6_TYPE, CHAN_TRACK_TH_RUNNING, 0, 0, 1, row, prv_th, clock); chan_th_init(th, uth, CHAN_NANOS6_SUBSYSTEM, CHAN_TRACK_TH_ACTIVE, 0, 0, 1, row, prv_th, clock); chan_th_init(th, uth, CHAN_NANOS6_RANK, CHAN_TRACK_TH_RUNNING, 0, 0, 1, row, prv_th, clock); chan_th_init(th, uth, CHAN_NANOS6_THREAD, CHAN_TRACK_NONE, 0, 1, 1, row, prv_th, clock); } /* Init the Nanos6 channels in all cpus */ for(size_t i=0; itotal_ncpus; i++) { cpu = emu->global_cpu[i]; row = cpu->gindex + 1; ucpu = &emu->cpu_chan; chan_cpu_init(cpu, ucpu, CHAN_NANOS6_TASKID, CHAN_TRACK_TH_RUNNING, 0, 0, 1, row, prv_cpu, clock); chan_cpu_init(cpu, ucpu, CHAN_NANOS6_TYPE, CHAN_TRACK_TH_RUNNING, 0, 0, 1, row, prv_cpu, clock); chan_cpu_init(cpu, ucpu, CHAN_NANOS6_SUBSYSTEM, CHAN_TRACK_TH_RUNNING, 0, 0, 1, row, prv_cpu, clock); chan_cpu_init(cpu, ucpu, CHAN_NANOS6_RANK, CHAN_TRACK_TH_RUNNING, 0, 0, 1, row, prv_cpu, clock); chan_cpu_init(cpu, ucpu, CHAN_NANOS6_THREAD, CHAN_TRACK_TH_RUNNING, 0, 0, 1, row, prv_cpu, clock); } /* Init task stack */ for(size_t i=0; itotal_nthreads; i++) { th = emu->global_thread[i]; th->nanos6_task_stack.thread = th; } } /* --------------------------- pre ------------------------------- */ static void chan_task_stopped(struct ovni_emu *emu, char tr) { struct ovni_ethread *th; th = emu->cur_thread; chan_set(&th->chan[CHAN_NANOS6_TASKID], 0); chan_set(&th->chan[CHAN_NANOS6_TYPE], 0); if(emu->cur_loom->rank_enabled) chan_set(&th->chan[CHAN_NANOS6_RANK], 0); /* Only exit the task body when finishing */ if(tr == 'e') chan_pop(&th->chan[CHAN_NANOS6_SUBSYSTEM], ST_NANOS6_TASK_BODY); } static void chan_task_running(struct ovni_emu *emu, struct task *task, char tr) { struct ovni_ethread *th; struct ovni_eproc *proc; th = emu->cur_thread; proc = emu->cur_proc; if(task->id == 0) die("task id cannot be 0\n"); if(task->type->gid == 0) die("task type gid cannot be 0\n"); if(proc->appid <= 0) die("app id must be positive\n"); chan_set(&th->chan[CHAN_NANOS6_TASKID], task->id); chan_set(&th->chan[CHAN_NANOS6_TYPE], task->type->gid); if(emu->cur_loom->rank_enabled) chan_set(&th->chan[CHAN_NANOS6_RANK], proc->rank + 1); /* Only enter the body of the task when we begin the execution */ if(tr == 'x') chan_push(&th->chan[CHAN_NANOS6_SUBSYSTEM], ST_NANOS6_TASK_BODY); } static void chan_task_switch(struct ovni_emu *emu, struct task *prev, struct task *next) { struct ovni_ethread *th = emu->cur_thread; if(!prev || !next) die("cannot switch to or from a NULL task\n"); if(prev == next) die("cannot switch to the same task\n"); if(next->id == 0) die("next task id cannot be 0\n"); if(next->type->gid == 0) die("next task type id cannot be 0\n"); if(prev->thread != next->thread) die("cannot switch to a task of another thread\n"); /* No need to change the rank as we will switch to tasks from * same thread */ chan_set(&th->chan[CHAN_NANOS6_TASKID], next->id); /* FIXME: We should emit a PRV event even if we are switching to * the same type event, to mark the end of the current task. For * now we only emit a new type if we switch to a type with a * different gid. */ if(prev->type->gid != next->type->gid) chan_set(&th->chan[CHAN_NANOS6_TYPE], next->type->gid); } static void update_task_state(struct ovni_emu *emu) { if(ovni_payload_size(emu->cur_ev) < 4) die("missing task id in payload\n"); uint32_t task_id = emu->cur_ev->payload.u32[0]; struct ovni_ethread *th = emu->cur_thread; struct ovni_eproc *proc = emu->cur_proc; struct task_info *info = &proc->nanos6_task_info; struct task_stack *stack = &th->nanos6_task_stack; struct task *task = task_find(info->tasks, task_id); if(task == NULL) die("cannot find task with id %u\n", task_id); switch(emu->cur_ev->header.value) { case 'x': task_execute(stack, task); break; case 'e': task_end(stack, task); break; case 'p': task_pause(stack, task); break; case 'r': task_resume(stack, task); break; default: die("unexpected Nanos6 task event value %c\n", emu->cur_ev->header.value); } } static char expand_transition_value(struct ovni_emu *emu, int was_running, int runs_now) { char tr = emu->cur_ev->header.value; /* Ensure we don't clobber the value */ if(tr == 'X' || tr == 'E') die("unexpected event value %c\n", tr); /* Modify the event value to detect nested transitions */ if(tr == 'x' && was_running) tr = 'X'; /* Execute a new nested task */ else if(tr == 'e' && runs_now) tr = 'E'; /* End a nested task */ return tr; } static void update_task_channels(struct ovni_emu *emu, char tr, struct task *prev, struct task *next) { switch(tr) { case 'x': case 'r': chan_task_running(emu, next, tr); break; case 'e': case 'p': chan_task_stopped(emu, tr); break; /* Additional nested transitions */ case 'X': case 'E': chan_task_switch(emu, prev, next); break; default: die("unexpected transition value %c\n", tr); } } static void update_task(struct ovni_emu *emu) { struct ovni_ethread *th = emu->cur_thread; struct task_stack *stack = &th->nanos6_task_stack; struct task *prev = task_get_running(stack); /* Update the emulator state, but don't modify the channels */ update_task_state(emu); struct task *next = task_get_running(stack); int was_running = (prev != NULL); int runs_now = (next != NULL); char tr = expand_transition_value(emu, was_running, runs_now); /* Update the channels now */ update_task_channels(emu, tr, prev, next); } static void create_task(struct ovni_emu *emu) { if(ovni_payload_size(emu->cur_ev) != 8) die("cannot create task: unexpected payload size\n"); uint32_t task_id = emu->cur_ev->payload.u32[0]; uint32_t type_id = emu->cur_ev->payload.u32[1]; struct task_info *info = &emu->cur_proc->nanos6_task_info; task_create(info, type_id, task_id); } static void pre_task(struct ovni_emu *emu) { struct ovni_ethread *th; struct ovni_chan *chan_th; th = emu->cur_thread; chan_th = &th->chan[CHAN_NANOS6_SUBSYSTEM]; switch(emu->cur_ev->header.value) { /* We use the 'c' event to create the task and switch * the subsystem all in one step because the timing here * is critical. */ case 'c': chan_push(chan_th, ST_NANOS6_TASK_CREATING); create_task(emu); break; case 'C': chan_pop(chan_th, ST_NANOS6_TASK_CREATING); break; case 'x': case 'e': case 'r': case 'p': /* Wet floor */ update_task(emu); break; default: die("unexpected Nanos6 task event value %c\n", emu->cur_ev->header.value); } } static void pre_type(struct ovni_emu *emu) { if(emu->cur_ev->header.value != 'c') die("unexpected event value %c\n", emu->cur_ev->header.value); if((emu->cur_ev->header.flags & OVNI_EV_JUMBO) == 0) die("expecting a jumbo event\n"); uint8_t *data = &emu->cur_ev->payload.jumbo.data[0]; uint32_t typeid = *(uint32_t *) data; data += 4; const char *label = (const char *) data; struct ovni_eproc *proc = emu->cur_proc; task_type_create(&proc->nanos6_task_info, typeid, label); } static void pre_deps(struct ovni_emu *emu) { struct ovni_ethread *th; struct ovni_chan *chan_th; th = emu->cur_thread; chan_th = &th->chan[CHAN_NANOS6_SUBSYSTEM]; switch(emu->cur_ev->header.value) { case 'r': chan_push(chan_th, ST_NANOS6_DEP_REG); break; case 'R': chan_pop (chan_th, ST_NANOS6_DEP_REG); break; case 'u': chan_push(chan_th, ST_NANOS6_DEP_UNREG); break; case 'U': chan_pop (chan_th, ST_NANOS6_DEP_UNREG); break; default: break; } } static void pre_blocking(struct ovni_emu *emu) { struct ovni_ethread *th; struct ovni_chan *chan_th; th = emu->cur_thread; chan_th = &th->chan[CHAN_NANOS6_SUBSYSTEM]; switch(emu->cur_ev->header.value) { case 'b': chan_push(chan_th, ST_NANOS6_BLK_BLOCKING); break; case 'B': chan_pop (chan_th, ST_NANOS6_BLK_BLOCKING); break; case 'u': chan_push(chan_th, ST_NANOS6_BLK_UNBLOCKING); break; case 'U': chan_pop (chan_th, ST_NANOS6_BLK_UNBLOCKING); break; case 'w': chan_push(chan_th, ST_NANOS6_BLK_TASKWAIT); break; case 'W': chan_pop (chan_th, ST_NANOS6_BLK_TASKWAIT); break; case 'f': chan_push(chan_th, ST_NANOS6_BLK_WAITFOR); break; case 'F': chan_pop (chan_th, ST_NANOS6_BLK_WAITFOR); break; default: break; } } static void pre_worker(struct ovni_emu *emu) { struct ovni_ethread *th; struct ovni_chan *chan_th; th = emu->cur_thread; chan_th = &th->chan[CHAN_NANOS6_SUBSYSTEM]; switch(emu->cur_ev->header.value) { case '[': chan_push(chan_th, ST_NANOS6_WORKER_LOOP); break; case ']': chan_pop (chan_th, ST_NANOS6_WORKER_LOOP); break; case 't': chan_push(chan_th, ST_NANOS6_HANDLING_TASK); break; case 'T': chan_pop (chan_th, ST_NANOS6_HANDLING_TASK); break; default: break; } } static void pre_sched(struct ovni_emu *emu) { struct ovni_ethread *th; struct ovni_chan *chan_th; th = emu->cur_thread; chan_th = &th->chan[CHAN_NANOS6_SUBSYSTEM]; switch(emu->cur_ev->header.value) { case '[': chan_push(chan_th, ST_NANOS6_SCHED_SERVING); break; case ']': chan_pop (chan_th, ST_NANOS6_SCHED_SERVING); break; case 'a': chan_push(chan_th, ST_NANOS6_SCHED_ADDING); break; case 'A': chan_pop (chan_th, ST_NANOS6_SCHED_ADDING); break; case '@': chan_ev (chan_th, EV_NANOS6_SCHED_SELF); break; case 'r': chan_ev (chan_th, EV_NANOS6_SCHED_RECV); break; case 's': chan_ev (chan_th, EV_NANOS6_SCHED_SEND); break; default: die("unknown Nanos6 scheduler event %c\n", emu->cur_ev->header.value); } } static void pre_thread(struct ovni_emu *emu) { struct ovni_ethread *th; struct ovni_chan *chan_th; th = emu->cur_thread; chan_th = &th->chan[CHAN_NANOS6_THREAD]; switch(emu->cur_ev->header.value) { case 'e': chan_push(chan_th, ST_NANOS6_TH_EXTERNAL); break; case 'E': chan_pop (chan_th, ST_NANOS6_TH_EXTERNAL); break; case 'w': chan_push(chan_th, ST_NANOS6_TH_WORKER); break; case 'W': chan_pop (chan_th, ST_NANOS6_TH_WORKER); break; case 'l': chan_push(chan_th, ST_NANOS6_TH_LEADER); break; case 'L': chan_pop (chan_th, ST_NANOS6_TH_LEADER); break; case 'm': chan_push(chan_th, ST_NANOS6_TH_MAIN); break; case 'M': chan_pop (chan_th, ST_NANOS6_TH_MAIN); break; default: break; } } static void pre_ss(struct ovni_emu *emu, int st) { struct ovni_ethread *th; struct ovni_chan *chan_th; th = emu->cur_thread; chan_th = &th->chan[CHAN_NANOS6_SUBSYSTEM]; dbg("pre_ss chan id %d st=%d\n", chan_th->id, st); switch(emu->cur_ev->header.value) { case '[': chan_push(chan_th, st); break; case ']': chan_pop(chan_th, st); break; default: die("unexpected value '%c' (expecting '[' or ']')\n", emu->cur_ev->header.value); } } static void check_affinity(struct ovni_emu *emu) { struct ovni_ethread *th = emu->cur_thread; struct ovni_cpu *cpu = th->cpu; if(!cpu || cpu->virtual) return; if(cpu->nrunning_threads > 1) { err("cpu %s has more than one thread running\n", cpu->name); if(emu->enable_linter) abort(); } } void hook_pre_nanos6(struct ovni_emu *emu) { if(emu->cur_ev->header.model != '6') die("hook_pre_nanos6: unexpected event with model %c\n", emu->cur_ev->header.model); if(!emu->cur_thread->is_active) die("hook_pre_nanos6: current thread %d not active\n", emu->cur_thread->tid); switch(emu->cur_ev->header.category) { case 'T': pre_task(emu); break; case 'Y': pre_type(emu); break; case 'S': pre_sched(emu); break; case 'U': pre_ss(emu, ST_NANOS6_TASK_SUBMIT); break; case 'F': pre_ss(emu, ST_NANOS6_TASK_SPAWNING); break; case 'H': pre_thread(emu); break; case 'D': pre_deps(emu); break; case 'B': pre_blocking(emu); break; case 'W': pre_worker(emu); break; default: die("unknown Nanos6 event category %c\n", emu->cur_ev->header.category); } check_affinity(emu); } static void end_lint(struct ovni_emu *emu) { /* Ensure we run out of subsystem states */ for(size_t i = 0; i < emu->total_nthreads; i++) { struct ovni_ethread *th = emu->global_thread[i]; struct ovni_chan *ch = &th->chan[CHAN_NANOS6_SUBSYSTEM]; if(ch->n != 1) { int top = ch->stack[ch->n - 1]; struct pcf_value_label *pv; char *name = "(unknown)"; for(pv = &nanos6_ss_values[0]; pv->label; pv++) { if(pv->value == top) { name = pv->label; break; } } die("thread %d ended with %d extra stacked nanos6 subsystems, top=\"%s\"\n", th->tid, ch->n - 1, name); } } } void hook_end_nanos6(struct ovni_emu *emu) { /* Emit types for all channel types and processes */ for(enum chan_type ct = 0; ct < CHAN_MAXTYPE; ct++) { struct pcf_file *pcf = &emu->pcf[ct]; int typeid = chan_to_prvtype[CHAN_NANOS6_TYPE]; struct pcf_type *pcftype = pcf_find_type(pcf, typeid); for(size_t i = 0; i < emu->trace.nlooms; i++) { struct ovni_loom *loom = &emu->trace.loom[i]; for(size_t j = 0; j < loom->nprocs; j++) { struct ovni_eproc *proc = &loom->proc[j]; task_create_pcf_types(pcftype, proc->nanos6_task_info.types); } } } /* When running in linter mode perform additional checks */ if(emu->enable_linter) end_lint(emu); }