2022-09-19 12:39:02 +02:00
|
|
|
/* Copyright (c) 2021 Barcelona Supercomputing Center (BSC)
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later */
|
2021-10-26 18:42:41 +02:00
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
#include "chan.h"
|
2021-07-28 11:56:35 +02:00
|
|
|
#include "emu.h"
|
2022-09-29 15:34:44 +02:00
|
|
|
#include "ovni.h"
|
2021-08-02 10:08:58 +02:00
|
|
|
#include "prv.h"
|
2021-11-18 17:05:37 +01:00
|
|
|
#include "utlist.h"
|
2021-07-28 11:56:35 +02:00
|
|
|
|
2021-10-21 16:05:01 +02:00
|
|
|
/* The emulator ovni module provides the execution model by tracking the thread
|
|
|
|
* state and which threads run in each CPU */
|
|
|
|
|
2021-10-21 16:46:42 +02:00
|
|
|
/* --------------------------- init ------------------------------- */
|
|
|
|
|
|
|
|
void
|
|
|
|
hook_init_ovni(struct ovni_emu *emu)
|
|
|
|
{
|
|
|
|
struct ovni_ethread *th;
|
|
|
|
struct ovni_cpu *cpu;
|
2021-10-18 12:47:51 +02:00
|
|
|
struct ovni_chan **uth, **ucpu;
|
|
|
|
size_t i;
|
|
|
|
int row;
|
2021-10-21 16:46:42 +02:00
|
|
|
FILE *prv_th, *prv_cpu;
|
|
|
|
int64_t *clock;
|
|
|
|
|
|
|
|
clock = &emu->delta_time;
|
|
|
|
prv_th = emu->prv_thread;
|
|
|
|
prv_cpu = emu->prv_cpu;
|
|
|
|
|
|
|
|
/* Init the ovni channels in all threads */
|
2022-09-29 15:34:44 +02:00
|
|
|
for (i = 0; i < emu->total_nthreads; i++) {
|
2021-10-21 16:46:42 +02:00
|
|
|
th = emu->global_thread[i];
|
|
|
|
row = th->gindex + 1;
|
2021-10-18 12:47:51 +02:00
|
|
|
uth = &emu->th_chan;
|
2021-10-21 16:46:42 +02:00
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
chan_th_init(th, uth, CHAN_OVNI_TID, CHAN_TRACK_TH_RUNNING, th->tid, 0, 1, row, prv_th, clock);
|
|
|
|
chan_th_init(th, uth, CHAN_OVNI_PID, CHAN_TRACK_TH_RUNNING, th->proc->pid, 0, 1, row, prv_th, clock);
|
|
|
|
chan_th_init(th, uth, CHAN_OVNI_CPU, CHAN_TRACK_NONE, -1, 0, 1, row, prv_th, clock);
|
|
|
|
chan_th_init(th, uth, CHAN_OVNI_STATE, CHAN_TRACK_NONE, TH_ST_UNKNOWN, 1, 1, row, prv_th, clock);
|
|
|
|
chan_th_init(th, uth, CHAN_OVNI_FLUSH, CHAN_TRACK_NONE, ST_NULL, 1, 1, row, prv_th, clock);
|
2021-10-21 16:46:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Init the ovni channels in all cpus */
|
2022-09-29 15:34:44 +02:00
|
|
|
for (i = 0; i < emu->total_ncpus; i++) {
|
2021-10-21 16:46:42 +02:00
|
|
|
cpu = emu->global_cpu[i];
|
|
|
|
row = cpu->gindex + 1;
|
2021-10-18 12:47:51 +02:00
|
|
|
ucpu = &emu->cpu_chan;
|
2021-10-21 16:46:42 +02:00
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
chan_cpu_init(cpu, ucpu, CHAN_OVNI_TID, CHAN_TRACK_TH_RUNNING, 0, 1, 1, row, prv_cpu, clock);
|
|
|
|
chan_cpu_init(cpu, ucpu, CHAN_OVNI_PID, CHAN_TRACK_TH_RUNNING, 0, 1, 1, row, prv_cpu, clock);
|
|
|
|
chan_cpu_init(cpu, ucpu, CHAN_OVNI_NRTHREADS, CHAN_TRACK_NONE, 0, 1, 1, row, prv_cpu, clock);
|
|
|
|
chan_cpu_init(cpu, ucpu, CHAN_OVNI_FLUSH, CHAN_TRACK_TH_RUNNING, 0, 0, 1, row, prv_cpu, clock);
|
2021-10-21 16:46:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 16:15:29 +02:00
|
|
|
/* --------------------------- pre ------------------------------- */
|
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
/* Update the tracking channel if needed */
|
2021-10-21 16:15:29 +02:00
|
|
|
static void
|
2021-10-18 12:47:51 +02:00
|
|
|
chan_tracking_update(struct ovni_chan *chan, struct ovni_ethread *th)
|
2021-10-21 16:15:29 +02:00
|
|
|
{
|
2021-10-18 12:47:51 +02:00
|
|
|
int enabled;
|
2021-10-11 11:12:26 +02:00
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
if (th == NULL)
|
2021-12-07 19:52:48 +01:00
|
|
|
die("chan_tracking_update: thread is NULL");
|
2021-10-21 16:46:42 +02:00
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
switch (chan->track) {
|
2021-10-18 12:47:51 +02:00
|
|
|
case CHAN_TRACK_TH_RUNNING:
|
|
|
|
enabled = th->is_running;
|
|
|
|
break;
|
|
|
|
case CHAN_TRACK_TH_ACTIVE:
|
|
|
|
enabled = th->is_active;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dbg("ignoring thread %d chan %d with track=%d\n",
|
|
|
|
th->tid, chan->id, chan->track);
|
|
|
|
return;
|
|
|
|
}
|
2021-10-21 16:46:42 +02:00
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
/* The channel is already in the proper state */
|
2022-09-29 15:34:44 +02:00
|
|
|
if (chan_is_enabled(chan) == enabled)
|
2021-10-18 12:47:51 +02:00
|
|
|
return;
|
2021-10-11 11:12:26 +02:00
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
dbg("thread %d changes state to %d: chan %d enabled=%d\n",
|
2022-09-29 15:34:44 +02:00
|
|
|
th->tid, th->state, chan->id, enabled);
|
2021-10-11 11:12:26 +02:00
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
chan_enable(chan, enabled);
|
2021-10-21 16:15:29 +02:00
|
|
|
}
|
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
/* Sets the state of the thread and updates the thread tracking channels */
|
2021-10-21 16:15:29 +02:00
|
|
|
static void
|
2021-11-08 17:05:22 +01:00
|
|
|
thread_set_state(struct ovni_ethread *th, enum ethread_state state)
|
2021-10-21 16:15:29 +02:00
|
|
|
{
|
2021-10-18 12:47:51 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* The state must be updated when a cpu is set */
|
2022-09-29 15:34:44 +02:00
|
|
|
if (th->cpu == NULL)
|
2021-12-07 19:52:48 +01:00
|
|
|
die("thread_set_state: thread %d doesn't have a CPU\n",
|
2022-09-29 15:34:44 +02:00
|
|
|
th->tid);
|
2021-10-18 12:47:51 +02:00
|
|
|
|
|
|
|
dbg("thread_set_state: setting thread %d state %d\n",
|
2022-09-29 15:34:44 +02:00
|
|
|
th->tid, state);
|
2021-10-21 16:15:29 +02:00
|
|
|
|
|
|
|
th->state = state;
|
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
th->is_running = (state == TH_ST_RUNNING) ? 1 : 0;
|
|
|
|
|
|
|
|
th->is_active = (state == TH_ST_RUNNING
|
2022-09-29 15:34:44 +02:00
|
|
|
|| state == TH_ST_COOLING
|
|
|
|
|| state == TH_ST_WARMING)
|
|
|
|
? 1
|
|
|
|
: 0;
|
2021-10-18 12:47:51 +02:00
|
|
|
|
2021-10-11 11:12:26 +02:00
|
|
|
chan_set(&th->chan[CHAN_OVNI_STATE], th->state);
|
2022-09-29 15:34:44 +02:00
|
|
|
|
2021-10-11 11:12:26 +02:00
|
|
|
/* Enable or disable the thread channels that track the thread state */
|
2022-09-29 15:34:44 +02:00
|
|
|
for (i = 0; i < CHAN_MAX; i++)
|
2021-10-18 12:47:51 +02:00
|
|
|
chan_tracking_update(&th->chan[i], th);
|
|
|
|
|
|
|
|
dbg("thread_set_state: done\n");
|
2021-10-21 16:15:29 +02:00
|
|
|
}
|
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
static void
|
|
|
|
cpu_update_th_stats(struct ovni_cpu *cpu)
|
2021-10-21 16:05:01 +02:00
|
|
|
{
|
2021-10-18 12:47:51 +02:00
|
|
|
struct ovni_ethread *th, *th_running = NULL, *th_active = NULL;
|
|
|
|
int active = 0, running = 0;
|
2021-10-21 16:05:01 +02:00
|
|
|
|
2021-11-18 17:05:37 +01:00
|
|
|
DL_FOREACH(cpu->thread, th)
|
2021-10-11 11:12:26 +02:00
|
|
|
{
|
2022-09-29 15:34:44 +02:00
|
|
|
if (th->state == TH_ST_RUNNING) {
|
2021-10-18 12:47:51 +02:00
|
|
|
th_running = th;
|
|
|
|
running++;
|
|
|
|
th_active = th;
|
|
|
|
active++;
|
2022-09-29 15:34:44 +02:00
|
|
|
} else if (th->state == TH_ST_COOLING || th->state == TH_ST_WARMING) {
|
2021-10-18 12:47:51 +02:00
|
|
|
th_active = th;
|
|
|
|
active++;
|
2021-10-11 11:12:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
cpu->nrunning_threads = running;
|
|
|
|
cpu->nactive_threads = active;
|
|
|
|
cpu->th_running = th_running;
|
|
|
|
cpu->th_active = th_active;
|
|
|
|
}
|
2021-10-21 16:05:01 +02:00
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
static void
|
|
|
|
update_cpu(struct ovni_cpu *cpu)
|
|
|
|
{
|
|
|
|
int i;
|
2021-10-21 16:05:01 +02:00
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
dbg("updating cpu %s\n", cpu->name);
|
2021-10-21 16:46:42 +02:00
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
/* Update the running and active threads first */
|
|
|
|
cpu_update_th_stats(cpu);
|
2021-10-21 16:46:42 +02:00
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
/* From the CPU channels we only need to manually update the number of
|
|
|
|
* threads running in the CPU */
|
2022-09-29 15:34:44 +02:00
|
|
|
if (chan_get_st(&cpu->chan[CHAN_OVNI_NRTHREADS]) != (int) cpu->nrunning_threads)
|
2021-10-19 10:13:57 +02:00
|
|
|
chan_set(&cpu->chan[CHAN_OVNI_NRTHREADS], (int) cpu->nrunning_threads);
|
2021-10-18 12:47:51 +02:00
|
|
|
|
|
|
|
/* Update all tracking channels */
|
2022-09-29 15:34:44 +02:00
|
|
|
for (i = 0; i < CHAN_MAX; i++)
|
2021-10-18 12:47:51 +02:00
|
|
|
emu_cpu_update_chan(cpu, &cpu->chan[i]);
|
|
|
|
|
|
|
|
dbg("updating cpu %s complete!\n", cpu->name);
|
2021-10-21 16:05:01 +02:00
|
|
|
}
|
|
|
|
|
2021-07-28 19:12:20 +02:00
|
|
|
struct ovni_cpu *
|
2021-08-02 21:13:03 +02:00
|
|
|
emu_get_cpu(struct ovni_loom *loom, int cpuid)
|
2021-07-28 19:12:20 +02:00
|
|
|
{
|
2022-09-29 15:34:44 +02:00
|
|
|
if (cpuid >= (int) loom->ncpus)
|
2021-12-07 19:52:48 +01:00
|
|
|
die("emu_get_cpu: CPU index out of bounds\n");
|
2021-07-28 19:12:20 +02:00
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
if (cpuid < 0)
|
2021-08-02 21:13:03 +02:00
|
|
|
return &loom->vcpu;
|
2021-07-28 19:12:20 +02:00
|
|
|
|
2021-08-02 21:13:03 +02:00
|
|
|
return &loom->cpu[cpuid];
|
2021-07-28 19:12:20 +02:00
|
|
|
}
|
|
|
|
|
2021-11-18 17:05:37 +01:00
|
|
|
static struct ovni_ethread *
|
2021-07-28 19:12:20 +02:00
|
|
|
emu_cpu_find_thread(struct ovni_cpu *cpu, struct ovni_ethread *thread)
|
|
|
|
{
|
2021-11-18 17:05:37 +01:00
|
|
|
struct ovni_ethread *p = NULL;
|
2021-07-28 19:12:20 +02:00
|
|
|
|
2021-11-18 17:05:37 +01:00
|
|
|
DL_FOREACH(cpu->thread, p)
|
|
|
|
{
|
2022-09-29 15:34:44 +02:00
|
|
|
if (p == thread)
|
2021-11-18 17:05:37 +01:00
|
|
|
return p;
|
|
|
|
}
|
2021-07-28 19:12:20 +02:00
|
|
|
|
2021-11-18 17:05:37 +01:00
|
|
|
return NULL;
|
2021-07-28 19:12:20 +02:00
|
|
|
}
|
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
/* Add the given thread to the list of threads assigned to the CPU */
|
2021-10-11 11:12:26 +02:00
|
|
|
static void
|
2021-10-18 12:47:51 +02:00
|
|
|
cpu_add_thread(struct ovni_cpu *cpu, struct ovni_ethread *thread)
|
2021-07-28 19:12:20 +02:00
|
|
|
{
|
|
|
|
/* Found, abort */
|
2022-09-29 15:34:44 +02:00
|
|
|
if (emu_cpu_find_thread(cpu, thread) != NULL) {
|
2021-10-18 12:47:51 +02:00
|
|
|
err("The thread %d is already assigned to %s\n",
|
2022-09-29 15:34:44 +02:00
|
|
|
thread->tid, cpu->name);
|
2021-07-28 19:12:20 +02:00
|
|
|
abort();
|
2021-10-18 12:47:51 +02:00
|
|
|
}
|
2021-07-28 19:12:20 +02:00
|
|
|
|
2021-11-18 17:05:37 +01:00
|
|
|
DL_APPEND(cpu->thread, thread);
|
|
|
|
cpu->nthreads++;
|
2021-10-21 16:05:01 +02:00
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
update_cpu(cpu);
|
2021-07-28 19:12:20 +02:00
|
|
|
}
|
|
|
|
|
2021-10-11 11:12:26 +02:00
|
|
|
static void
|
2021-10-18 12:47:51 +02:00
|
|
|
cpu_remove_thread(struct ovni_cpu *cpu, struct ovni_ethread *thread)
|
2021-07-28 19:12:20 +02:00
|
|
|
{
|
2021-11-18 17:05:37 +01:00
|
|
|
struct ovni_ethread *p;
|
2021-07-28 19:12:20 +02:00
|
|
|
|
2021-11-18 17:05:37 +01:00
|
|
|
p = emu_cpu_find_thread(cpu, thread);
|
2021-07-28 19:12:20 +02:00
|
|
|
|
|
|
|
/* Not found, abort */
|
2022-09-29 15:34:44 +02:00
|
|
|
if (p == NULL) {
|
2021-11-18 17:05:37 +01:00
|
|
|
err("cannot remove missing thread %d from cpu %s\n",
|
2022-09-29 15:34:44 +02:00
|
|
|
thread->tid, cpu->name);
|
2021-07-28 19:12:20 +02:00
|
|
|
abort();
|
2021-11-18 17:05:37 +01:00
|
|
|
}
|
2021-07-28 19:12:20 +02:00
|
|
|
|
2021-11-18 17:05:37 +01:00
|
|
|
DL_DELETE(cpu->thread, thread);
|
2021-07-28 19:12:20 +02:00
|
|
|
cpu->nthreads--;
|
2021-10-21 16:05:01 +02:00
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
update_cpu(cpu);
|
2021-07-28 19:12:20 +02:00
|
|
|
}
|
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
static void
|
|
|
|
cpu_migrate_thread(struct ovni_cpu *cpu,
|
2022-09-29 15:34:44 +02:00
|
|
|
struct ovni_ethread *thread,
|
|
|
|
struct ovni_cpu *newcpu)
|
2021-10-11 11:12:26 +02:00
|
|
|
{
|
2021-10-18 12:47:51 +02:00
|
|
|
cpu_remove_thread(cpu, thread);
|
|
|
|
cpu_add_thread(newcpu, thread);
|
2021-10-11 11:12:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Sets the thread assigned CPU to the given one.
|
|
|
|
* Precondition: the thread CPU must be null */
|
2021-10-18 12:47:51 +02:00
|
|
|
static void
|
2021-10-11 11:12:26 +02:00
|
|
|
thread_set_cpu(struct ovni_ethread *th, struct ovni_cpu *cpu)
|
|
|
|
{
|
2022-09-29 15:34:44 +02:00
|
|
|
if (th->cpu != NULL)
|
2021-12-07 19:52:48 +01:00
|
|
|
die("thread_set_cpu: thread %d already has a CPU\n",
|
2022-09-29 15:34:44 +02:00
|
|
|
th->tid);
|
2021-12-07 19:52:48 +01:00
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
dbg("thread_set_cpu: setting thread %d cpu to %s\n",
|
2022-09-29 15:34:44 +02:00
|
|
|
th->tid, cpu->name);
|
2021-10-11 11:12:26 +02:00
|
|
|
th->cpu = cpu;
|
|
|
|
|
|
|
|
chan_enable(&th->chan[CHAN_OVNI_CPU], 1);
|
|
|
|
chan_set(&th->chan[CHAN_OVNI_CPU], cpu->gindex + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unsets the thread assigned CPU.
|
|
|
|
* Precondition: the thread CPU must be not null */
|
2021-10-18 12:47:51 +02:00
|
|
|
static void
|
2021-10-11 11:12:26 +02:00
|
|
|
thread_unset_cpu(struct ovni_ethread *th)
|
|
|
|
{
|
2022-09-29 15:34:44 +02:00
|
|
|
if (th->cpu == NULL)
|
2021-12-07 19:52:48 +01:00
|
|
|
die("thread_unset_cpu: thread %d doesn't have a CPU\n",
|
2022-09-29 15:34:44 +02:00
|
|
|
th->tid);
|
2021-12-07 19:52:48 +01:00
|
|
|
|
2021-10-11 11:12:26 +02:00
|
|
|
th->cpu = NULL;
|
|
|
|
|
|
|
|
chan_enable(&th->chan[CHAN_OVNI_CPU], 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Migrates the thread assigned CPU to the given one.
|
|
|
|
* Precondition: the thread CPU must be not null */
|
2021-10-18 12:47:51 +02:00
|
|
|
static void
|
2021-10-11 11:12:26 +02:00
|
|
|
thread_migrate_cpu(struct ovni_ethread *th, struct ovni_cpu *cpu)
|
|
|
|
{
|
2022-09-29 15:34:44 +02:00
|
|
|
if (th->cpu == NULL)
|
2021-12-07 19:52:48 +01:00
|
|
|
die("thread_migrate_cpu: thread %d doesn't have a CPU\n",
|
2022-09-29 15:34:44 +02:00
|
|
|
th->tid);
|
2021-10-11 11:12:26 +02:00
|
|
|
th->cpu = cpu;
|
|
|
|
|
|
|
|
chan_set(&th->chan[CHAN_OVNI_CPU], cpu->gindex + 1);
|
|
|
|
}
|
|
|
|
|
2021-07-28 11:56:35 +02:00
|
|
|
static void
|
2021-10-11 11:12:26 +02:00
|
|
|
pre_thread_execute(struct ovni_emu *emu, struct ovni_ethread *th)
|
2021-07-28 11:56:35 +02:00
|
|
|
{
|
|
|
|
struct ovni_cpu *cpu;
|
|
|
|
int cpuid;
|
|
|
|
|
|
|
|
/* The thread cannot be already running */
|
2022-09-29 15:34:44 +02:00
|
|
|
if (th->state == TH_ST_RUNNING)
|
2022-09-13 14:22:44 +02:00
|
|
|
edie(emu, "pre_thread_execute: thread %d already running\n",
|
2022-09-29 15:34:44 +02:00
|
|
|
th->tid);
|
2021-07-28 11:56:35 +02:00
|
|
|
|
|
|
|
cpuid = emu->cur_ev->payload.i32[0];
|
2021-10-18 12:47:51 +02:00
|
|
|
|
2021-08-02 21:13:03 +02:00
|
|
|
cpu = emu_get_cpu(emu->cur_loom, cpuid);
|
2021-10-18 12:47:51 +02:00
|
|
|
dbg("pre_thread_execute: thread %d runs in CPU %s\n", th->tid, cpu->name);
|
2021-07-28 11:56:35 +02:00
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
/* First set the CPU in the thread */
|
2021-10-11 11:12:26 +02:00
|
|
|
thread_set_cpu(th, cpu);
|
2021-10-21 16:46:42 +02:00
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
/* Then set the thread to running state */
|
|
|
|
thread_set_state(th, TH_ST_RUNNING);
|
2021-10-21 16:46:42 +02:00
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
/* And then add the thread to the CPU, so tracking channels see the
|
|
|
|
* updated thread state */
|
|
|
|
cpu_add_thread(cpu, th);
|
2021-07-28 11:56:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-10-18 12:47:51 +02:00
|
|
|
pre_thread_end(struct ovni_ethread *th)
|
2021-07-28 11:56:35 +02:00
|
|
|
{
|
2022-09-29 15:34:44 +02:00
|
|
|
if (th->state != TH_ST_RUNNING && th->state != TH_ST_COOLING)
|
2022-09-08 12:36:22 +02:00
|
|
|
die("pre_thread_end: thread %d not running or cooling\n",
|
2022-09-29 15:34:44 +02:00
|
|
|
th->tid);
|
2021-12-07 19:52:48 +01:00
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
if (th->cpu == NULL)
|
2021-12-07 19:52:48 +01:00
|
|
|
die("pre_thread_end: thread %d doesn't have a CPU\n",
|
2022-09-29 15:34:44 +02:00
|
|
|
th->tid);
|
2021-10-21 16:46:42 +02:00
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
/* First update the thread state */
|
2021-10-21 16:46:42 +02:00
|
|
|
thread_set_state(th, TH_ST_DEAD);
|
2021-07-28 11:56:35 +02:00
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
/* Then remove it from the cpu, so channels are properly updated */
|
|
|
|
cpu_remove_thread(th->cpu, th);
|
|
|
|
|
|
|
|
thread_unset_cpu(th);
|
2021-07-28 11:56:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-10-18 12:47:51 +02:00
|
|
|
pre_thread_pause(struct ovni_ethread *th)
|
2021-07-28 11:56:35 +02:00
|
|
|
{
|
2022-09-29 15:34:44 +02:00
|
|
|
if (th->state != TH_ST_RUNNING && th->state != TH_ST_COOLING)
|
2021-12-07 19:52:48 +01:00
|
|
|
die("pre_thread_pause: thread %d not running or cooling\n",
|
2022-09-29 15:34:44 +02:00
|
|
|
th->tid);
|
2021-12-07 19:52:48 +01:00
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
if (th->cpu == NULL)
|
2021-12-07 19:52:48 +01:00
|
|
|
die("pre_thread_pause: thread %d doesn't have a CPU\n",
|
2022-09-29 15:34:44 +02:00
|
|
|
th->tid);
|
2021-07-28 11:56:35 +02:00
|
|
|
|
2021-10-11 11:12:26 +02:00
|
|
|
thread_set_state(th, TH_ST_PAUSED);
|
2021-10-18 12:47:51 +02:00
|
|
|
update_cpu(th->cpu);
|
2021-10-11 11:12:26 +02:00
|
|
|
}
|
2021-07-28 11:56:35 +02:00
|
|
|
|
2021-10-11 11:12:26 +02:00
|
|
|
static void
|
2021-10-18 12:47:51 +02:00
|
|
|
pre_thread_resume(struct ovni_ethread *th)
|
2021-10-11 11:12:26 +02:00
|
|
|
{
|
2022-09-29 15:34:44 +02:00
|
|
|
if (th->state != TH_ST_PAUSED && th->state != TH_ST_WARMING)
|
2021-12-07 19:52:48 +01:00
|
|
|
die("pre_thread_resume: thread %d not paused or warming\n",
|
2022-09-29 15:34:44 +02:00
|
|
|
th->tid);
|
2021-12-07 19:52:48 +01:00
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
if (th->cpu == NULL)
|
2021-12-07 19:52:48 +01:00
|
|
|
die("pre_thread_resume: thread %d doesn't have a CPU\n",
|
2022-09-29 15:34:44 +02:00
|
|
|
th->tid);
|
2021-10-11 11:12:26 +02:00
|
|
|
|
|
|
|
thread_set_state(th, TH_ST_RUNNING);
|
2021-10-18 12:47:51 +02:00
|
|
|
update_cpu(th->cpu);
|
2021-07-28 11:56:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-10-18 12:47:51 +02:00
|
|
|
pre_thread_cool(struct ovni_ethread *th)
|
2021-07-28 11:56:35 +02:00
|
|
|
{
|
2022-09-29 15:34:44 +02:00
|
|
|
if (th->state != TH_ST_RUNNING)
|
2021-12-07 19:52:48 +01:00
|
|
|
die("pre_thread_cool: thread %d not running\n",
|
2022-09-29 15:34:44 +02:00
|
|
|
th->tid);
|
2021-12-07 19:52:48 +01:00
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
if (th->cpu == NULL)
|
2021-12-07 19:52:48 +01:00
|
|
|
die("pre_thread_cool: thread %d doesn't have a CPU\n",
|
2022-09-29 15:34:44 +02:00
|
|
|
th->tid);
|
2021-10-11 11:12:26 +02:00
|
|
|
|
|
|
|
thread_set_state(th, TH_ST_COOLING);
|
2021-10-18 12:47:51 +02:00
|
|
|
update_cpu(th->cpu);
|
2021-10-11 11:12:26 +02:00
|
|
|
}
|
2021-07-28 11:56:35 +02:00
|
|
|
|
2021-10-11 11:12:26 +02:00
|
|
|
static void
|
2021-10-18 12:47:51 +02:00
|
|
|
pre_thread_warm(struct ovni_ethread *th)
|
2021-10-11 11:12:26 +02:00
|
|
|
{
|
2022-09-29 15:34:44 +02:00
|
|
|
if (th->state != TH_ST_PAUSED)
|
2021-12-07 19:52:48 +01:00
|
|
|
die("pre_thread_warm: thread %d not paused\n",
|
2022-09-29 15:34:44 +02:00
|
|
|
th->tid);
|
2021-12-07 19:52:48 +01:00
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
if (th->cpu == NULL)
|
2021-12-07 19:52:48 +01:00
|
|
|
die("pre_thread_warm: thread %d doesn't have a CPU\n",
|
2022-09-29 15:34:44 +02:00
|
|
|
th->tid);
|
2021-07-28 11:56:35 +02:00
|
|
|
|
2021-10-11 11:12:26 +02:00
|
|
|
thread_set_state(th, TH_ST_WARMING);
|
2021-10-18 12:47:51 +02:00
|
|
|
update_cpu(th->cpu);
|
2021-07-28 11:56:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-10-21 16:15:29 +02:00
|
|
|
pre_thread(struct ovni_emu *emu)
|
2021-07-28 11:56:35 +02:00
|
|
|
{
|
|
|
|
struct ovni_ev *ev;
|
2021-10-18 12:47:51 +02:00
|
|
|
struct ovni_ethread *th;
|
2021-07-28 11:56:35 +02:00
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
// emu_emit(emu);
|
2021-07-28 11:56:35 +02:00
|
|
|
|
2021-10-11 11:12:26 +02:00
|
|
|
th = emu->cur_thread;
|
2021-07-28 11:56:35 +02:00
|
|
|
ev = emu->cur_ev;
|
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
switch (ev->header.value) {
|
2021-10-11 11:12:26 +02:00
|
|
|
case 'C': /* create */
|
2021-07-28 11:56:35 +02:00
|
|
|
dbg("thread %d creates a new thread at cpu=%d with args=%x %x\n",
|
2022-09-29 15:34:44 +02:00
|
|
|
th->tid,
|
|
|
|
ev->payload.u32[0],
|
|
|
|
ev->payload.u32[1],
|
|
|
|
ev->payload.u32[2]);
|
2021-07-28 11:56:35 +02:00
|
|
|
|
|
|
|
break;
|
2022-09-29 15:34:44 +02:00
|
|
|
case 'x':
|
|
|
|
pre_thread_execute(emu, th);
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
pre_thread_end(th);
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
pre_thread_pause(th);
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
pre_thread_resume(th);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
pre_thread_cool(th);
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
pre_thread_warm(th);
|
|
|
|
break;
|
2021-07-28 11:56:35 +02:00
|
|
|
default:
|
2022-09-29 15:34:44 +02:00
|
|
|
err("unknown thread event value %c\n",
|
|
|
|
ev->header.value);
|
|
|
|
exit(EXIT_FAILURE);
|
2021-07-28 11:56:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-10-21 16:15:29 +02:00
|
|
|
pre_affinity_set(struct ovni_emu *emu)
|
2021-07-28 11:56:35 +02:00
|
|
|
{
|
|
|
|
int cpuid;
|
|
|
|
struct ovni_cpu *newcpu;
|
2021-10-11 11:12:26 +02:00
|
|
|
struct ovni_ethread *th;
|
2021-07-28 11:56:35 +02:00
|
|
|
|
2021-10-11 11:12:26 +02:00
|
|
|
th = emu->cur_thread;
|
2021-07-28 11:56:35 +02:00
|
|
|
cpuid = emu->cur_ev->payload.i32[0];
|
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
if (th->cpu == NULL)
|
2022-09-13 14:22:44 +02:00
|
|
|
edie(emu, "pre_affinity_set: thread %d doesn't have a CPU\n",
|
2022-09-29 15:34:44 +02:00
|
|
|
th->tid);
|
2021-12-07 19:52:48 +01:00
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
if (!th->is_active)
|
2022-09-13 14:22:44 +02:00
|
|
|
edie(emu, "pre_affinity_set: thread %d is not active\n",
|
2022-09-29 15:34:44 +02:00
|
|
|
th->tid);
|
2021-07-28 11:56:35 +02:00
|
|
|
|
|
|
|
/* Migrate current cpu to the one at cpuid */
|
2021-08-02 21:13:03 +02:00
|
|
|
newcpu = emu_get_cpu(emu->cur_loom, cpuid);
|
2021-07-28 11:56:35 +02:00
|
|
|
|
2021-10-19 10:30:47 +02:00
|
|
|
/* The CPU is already properly set, return */
|
2022-09-29 15:34:44 +02:00
|
|
|
if (th->cpu == newcpu)
|
2021-10-21 16:46:42 +02:00
|
|
|
return;
|
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
cpu_migrate_thread(th->cpu, th, newcpu);
|
2021-10-11 11:12:26 +02:00
|
|
|
thread_migrate_cpu(th, newcpu);
|
2021-10-21 16:46:42 +02:00
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
// dbg("cpu %d now runs %d\n", cpuid, th->tid);
|
2021-07-28 11:56:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-10-21 16:15:29 +02:00
|
|
|
pre_affinity_remote(struct ovni_emu *emu)
|
2021-07-28 11:56:35 +02:00
|
|
|
{
|
2021-10-18 12:47:51 +02:00
|
|
|
size_t i;
|
|
|
|
int32_t cpuid, tid;
|
2021-07-28 11:56:35 +02:00
|
|
|
struct ovni_cpu *newcpu;
|
2021-10-11 11:12:26 +02:00
|
|
|
struct ovni_ethread *remote_th;
|
2021-10-11 16:27:28 +02:00
|
|
|
struct ovni_loom *loom;
|
|
|
|
struct ovni_eproc *proc;
|
2021-07-28 11:56:35 +02:00
|
|
|
|
|
|
|
cpuid = emu->cur_ev->payload.i32[0];
|
|
|
|
tid = emu->cur_ev->payload.i32[1];
|
|
|
|
|
2021-10-11 11:12:26 +02:00
|
|
|
remote_th = emu_get_thread(emu->cur_proc, tid);
|
2021-07-28 11:56:35 +02:00
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
if (remote_th == NULL) {
|
2021-10-11 16:27:28 +02:00
|
|
|
/* Search the thread in other processes of the loom if
|
|
|
|
* not found in the current one */
|
|
|
|
loom = emu->cur_loom;
|
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
for (i = 0; i < loom->nprocs; i++) {
|
2021-10-11 16:27:28 +02:00
|
|
|
proc = &loom->proc[i];
|
|
|
|
|
|
|
|
/* Skip the current process */
|
2022-09-29 15:34:44 +02:00
|
|
|
if (proc == emu->cur_proc)
|
2021-10-11 16:27:28 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
remote_th = emu_get_thread(proc, tid);
|
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
if (remote_th)
|
2021-10-11 16:27:28 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
if (remote_th == NULL) {
|
2021-10-11 16:27:28 +02:00
|
|
|
err("thread tid %d not found: cannot set affinity remotely\n",
|
2022-09-29 15:34:44 +02:00
|
|
|
tid);
|
2021-10-11 16:27:28 +02:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
2021-08-10 12:46:45 +02:00
|
|
|
|
2021-10-11 11:12:26 +02:00
|
|
|
/* The remote_th cannot be in states dead or unknown */
|
2022-09-29 15:34:44 +02:00
|
|
|
if (remote_th->state == TH_ST_DEAD)
|
2022-09-13 14:22:44 +02:00
|
|
|
edie(emu, "pre_affinity_remote: remote thread %d in state DEAD\n",
|
2022-09-29 15:34:44 +02:00
|
|
|
remote_th->tid);
|
2021-12-07 19:52:48 +01:00
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
if (remote_th->state == TH_ST_UNKNOWN)
|
2022-09-13 14:22:44 +02:00
|
|
|
edie(emu, "pre_affinity_remote: remote thread %d in state UNKNOWN\n",
|
2022-09-29 15:34:44 +02:00
|
|
|
remote_th->tid);
|
2021-08-10 12:46:45 +02:00
|
|
|
|
|
|
|
/* It must have an assigned CPU */
|
2022-09-29 15:34:44 +02:00
|
|
|
if (remote_th->cpu == NULL)
|
2022-09-13 14:22:44 +02:00
|
|
|
edie(emu, "pre_affinity_remote: remote thread %d has no CPU\n",
|
2022-09-29 15:34:44 +02:00
|
|
|
remote_th->tid);
|
2021-07-28 11:56:35 +02:00
|
|
|
|
2021-08-10 12:46:45 +02:00
|
|
|
/* Migrate current cpu to the one at cpuid */
|
2021-08-02 21:13:03 +02:00
|
|
|
newcpu = emu_get_cpu(emu->cur_loom, cpuid);
|
2021-07-28 11:56:35 +02:00
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
cpu_migrate_thread(remote_th->cpu, remote_th, newcpu);
|
2021-10-11 11:12:26 +02:00
|
|
|
thread_migrate_cpu(remote_th, newcpu);
|
2021-07-28 11:56:35 +02:00
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
// dbg("remote_th %d switches to cpu %d by remote petition\n", tid,
|
2021-07-29 17:46:25 +02:00
|
|
|
// cpuid);
|
2021-07-28 11:56:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-10-21 16:15:29 +02:00
|
|
|
pre_affinity(struct ovni_emu *emu)
|
2021-07-28 11:56:35 +02:00
|
|
|
{
|
2022-09-29 15:34:44 +02:00
|
|
|
// emu_emit(emu);
|
|
|
|
switch (emu->cur_ev->header.value) {
|
|
|
|
case 's':
|
|
|
|
pre_affinity_set(emu);
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
pre_affinity_remote(emu);
|
|
|
|
break;
|
2021-07-28 11:56:35 +02:00
|
|
|
default:
|
|
|
|
dbg("unknown affinity event value %c\n",
|
2022-09-29 15:34:44 +02:00
|
|
|
emu->cur_ev->header.value);
|
2021-07-28 11:56:35 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
static void
|
2021-10-11 11:12:26 +02:00
|
|
|
pre_burst(struct ovni_emu *emu)
|
|
|
|
{
|
2021-10-11 12:42:37 +02:00
|
|
|
struct ovni_ethread *th;
|
2021-11-08 17:05:22 +01:00
|
|
|
int64_t dt = 0;
|
2021-10-11 12:42:37 +02:00
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
UNUSED(dt);
|
|
|
|
|
2021-10-11 12:42:37 +02:00
|
|
|
th = emu->cur_thread;
|
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
if (th->nbursts >= MAX_BURSTS) {
|
2021-10-11 12:42:37 +02:00
|
|
|
err("too many bursts: ignored\n");
|
|
|
|
return;
|
2021-10-11 11:12:26 +02:00
|
|
|
}
|
|
|
|
|
2021-10-11 12:42:37 +02:00
|
|
|
th->burst_time[th->nbursts] = emu->delta_time;
|
2022-09-29 15:34:44 +02:00
|
|
|
if (th->nbursts > 0) {
|
|
|
|
dt = th->burst_time[th->nbursts] - th->burst_time[th->nbursts - 1];
|
2021-10-11 11:12:26 +02:00
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
dbg("burst delta time %ld ns\n", dt);
|
2021-10-11 11:12:26 +02:00
|
|
|
}
|
2021-10-11 12:42:37 +02:00
|
|
|
|
|
|
|
th->nbursts++;
|
2021-10-11 11:12:26 +02:00
|
|
|
}
|
|
|
|
|
2021-10-20 18:33:54 +02:00
|
|
|
static void
|
|
|
|
pre_flush(struct ovni_emu *emu)
|
|
|
|
{
|
|
|
|
struct ovni_ethread *th;
|
|
|
|
struct ovni_chan *chan_th;
|
|
|
|
|
|
|
|
th = emu->cur_thread;
|
|
|
|
chan_th = &th->chan[CHAN_OVNI_FLUSH];
|
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
switch (emu->cur_ev->header.value) {
|
2021-10-20 18:33:54 +02:00
|
|
|
case '[':
|
|
|
|
chan_push(chan_th, ST_OVNI_FLUSHING);
|
|
|
|
break;
|
|
|
|
case ']':
|
|
|
|
chan_pop(chan_th, ST_OVNI_FLUSHING);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
err("unexpected value '%c' (expecting '[' or ']')\n",
|
2022-09-29 15:34:44 +02:00
|
|
|
emu->cur_ev->header.value);
|
2021-10-20 18:33:54 +02:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-28 11:56:35 +02:00
|
|
|
void
|
2021-07-28 19:12:20 +02:00
|
|
|
hook_pre_ovni(struct ovni_emu *emu)
|
2021-07-28 11:56:35 +02:00
|
|
|
{
|
2022-09-29 15:34:44 +02:00
|
|
|
// emu_emit(emu);
|
2021-07-28 11:56:35 +02:00
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
if (emu->cur_ev->header.model != 'O')
|
2021-10-21 16:05:01 +02:00
|
|
|
return;
|
|
|
|
|
2022-09-29 15:34:44 +02:00
|
|
|
switch (emu->cur_ev->header.category) {
|
|
|
|
case 'H':
|
|
|
|
pre_thread(emu);
|
|
|
|
break;
|
|
|
|
case 'A':
|
|
|
|
pre_affinity(emu);
|
|
|
|
break;
|
|
|
|
case 'B':
|
|
|
|
pre_burst(emu);
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
pre_flush(emu);
|
|
|
|
break;
|
2021-07-28 11:56:35 +02:00
|
|
|
default:
|
2021-10-11 15:46:49 +02:00
|
|
|
dbg("unknown ovni event category %c\n",
|
2022-09-29 15:34:44 +02:00
|
|
|
emu->cur_ev->header.category);
|
2021-07-28 11:56:35 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|