2021-10-26 18:42:41 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021 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 <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
#include "chan.h"
|
|
|
|
|
2021-10-21 16:15:29 +02:00
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "emu.h"
|
|
|
|
#include "prv.h"
|
2021-10-18 12:47:51 +02:00
|
|
|
#include "utlist.h"
|
2021-10-21 16:15:29 +02:00
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
static void
|
2021-11-08 17:05:22 +01:00
|
|
|
chan_init(struct ovni_chan *chan, enum chan_track track, int row, int type, FILE *prv, int64_t *clock)
|
2021-10-21 16:15:29 +02:00
|
|
|
{
|
|
|
|
chan->n = 0;
|
|
|
|
chan->type = type;
|
|
|
|
chan->enabled = 0;
|
|
|
|
chan->badst = ST_NULL;
|
|
|
|
chan->ev = -1;
|
|
|
|
chan->prv = prv;
|
|
|
|
chan->clock = clock;
|
2021-10-21 16:41:52 +02:00
|
|
|
chan->t = *clock;
|
2021-10-21 16:15:29 +02:00
|
|
|
chan->row = row;
|
|
|
|
chan->dirty = 0;
|
2021-10-21 16:41:52 +02:00
|
|
|
chan->track = track;
|
|
|
|
}
|
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
static void
|
|
|
|
mark_dirty(struct ovni_chan *chan)
|
|
|
|
{
|
|
|
|
assert(chan->dirty == 0);
|
|
|
|
|
|
|
|
dbg("adding dirty chan %d to list\n", chan->id);
|
|
|
|
chan->dirty = 1;
|
|
|
|
DL_APPEND(*chan->update_list, chan);
|
|
|
|
}
|
|
|
|
|
2021-10-21 16:41:52 +02:00
|
|
|
void
|
2021-10-18 12:47:51 +02:00
|
|
|
chan_th_init(struct ovni_ethread *th,
|
|
|
|
struct ovni_chan **update_list,
|
|
|
|
enum chan id,
|
2021-11-08 17:05:22 +01:00
|
|
|
enum chan_track track,
|
2021-10-18 12:47:51 +02:00
|
|
|
int init_st,
|
|
|
|
int enabled,
|
|
|
|
int dirty,
|
|
|
|
int row,
|
|
|
|
FILE *prv,
|
|
|
|
int64_t *clock)
|
2021-10-21 16:41:52 +02:00
|
|
|
{
|
|
|
|
struct ovni_chan *chan;
|
|
|
|
int prvth;
|
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
chan = &th->chan[id];
|
2021-11-08 17:02:39 +01:00
|
|
|
prvth = CHAN_PRV_TH(id);
|
2021-10-21 16:41:52 +02:00
|
|
|
|
|
|
|
chan_init(chan, track, row, prvth, prv, clock);
|
2021-10-18 12:47:51 +02:00
|
|
|
|
|
|
|
chan->id = id;
|
|
|
|
chan->thread = th;
|
|
|
|
chan->update_list = update_list;
|
|
|
|
chan->enabled = enabled;
|
|
|
|
chan->stack[chan->n++] = init_st;
|
|
|
|
if(dirty)
|
|
|
|
mark_dirty(chan);
|
2021-10-21 16:41:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-10-18 12:47:51 +02:00
|
|
|
chan_cpu_init(struct ovni_cpu *cpu,
|
|
|
|
struct ovni_chan **update_list,
|
|
|
|
enum chan id,
|
2021-11-08 17:05:22 +01:00
|
|
|
enum chan_track track,
|
2021-10-21 15:46:32 +02:00
|
|
|
int init_st,
|
|
|
|
int enabled,
|
|
|
|
int dirty,
|
2021-10-18 12:47:51 +02:00
|
|
|
int row,
|
|
|
|
FILE *prv,
|
|
|
|
int64_t *clock)
|
2021-10-21 16:41:52 +02:00
|
|
|
{
|
|
|
|
struct ovni_chan *chan;
|
|
|
|
int prvcpu;
|
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
chan = &cpu->chan[id];
|
2021-11-08 17:02:39 +01:00
|
|
|
prvcpu = CHAN_PRV_CPU(id);
|
2021-10-21 15:46:32 +02:00
|
|
|
|
|
|
|
chan_init(chan, track, row, prvcpu, prv, clock);
|
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
chan->id = id;
|
|
|
|
chan->cpu = cpu;
|
|
|
|
chan->update_list = update_list;
|
2021-10-21 15:46:32 +02:00
|
|
|
chan->enabled = enabled;
|
|
|
|
chan->stack[chan->n++] = init_st;
|
|
|
|
if(dirty)
|
|
|
|
mark_dirty(chan);
|
2021-10-21 16:15:29 +02:00
|
|
|
}
|
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
static void
|
|
|
|
chan_dump_update_list(struct ovni_chan *chan)
|
|
|
|
{
|
|
|
|
struct ovni_chan *c;
|
|
|
|
|
|
|
|
dbg("update list for chan %d at %p:\n",
|
|
|
|
chan->id, (void *) chan);
|
|
|
|
for(c = *chan->update_list; c; c = c->next)
|
|
|
|
{
|
|
|
|
dbg(" chan %d at %p\n", c->id, (void *) c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 16:15:29 +02:00
|
|
|
void
|
|
|
|
chan_enable(struct ovni_chan *chan, int enabled)
|
|
|
|
{
|
2021-10-21 16:41:52 +02:00
|
|
|
/* Can be dirty */
|
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
dbg("chan_enable chan=%d enabled=%d\n", chan->id, enabled);
|
2021-10-21 16:41:52 +02:00
|
|
|
|
|
|
|
if(chan->enabled == enabled)
|
|
|
|
{
|
|
|
|
err("chan already in enabled=%d\n", enabled);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2021-10-21 16:15:29 +02:00
|
|
|
chan->enabled = enabled;
|
2021-10-21 16:41:52 +02:00
|
|
|
chan->t = *chan->clock;
|
2021-10-18 12:47:51 +02:00
|
|
|
|
|
|
|
/* Only append if not dirty */
|
|
|
|
if(!chan->dirty)
|
|
|
|
{
|
|
|
|
mark_dirty(chan);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dbg("already dirty chan %d: skip update list\n",
|
|
|
|
chan->id);
|
|
|
|
chan_dump_update_list(chan);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
chan_disable(struct ovni_chan *chan)
|
|
|
|
{
|
|
|
|
chan_enable(chan, 0);
|
2021-10-21 16:15:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
chan_is_enabled(struct ovni_chan *chan)
|
|
|
|
{
|
|
|
|
return chan->enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
chan_set(struct ovni_chan *chan, int st)
|
|
|
|
{
|
2021-10-18 12:47:51 +02:00
|
|
|
/* Can be dirty */
|
|
|
|
|
|
|
|
dbg("chan_set chan %d st=%d\n", chan->id, st);
|
2021-10-21 16:15:29 +02:00
|
|
|
|
|
|
|
assert(chan->enabled);
|
|
|
|
|
|
|
|
if(chan->n == 0)
|
|
|
|
chan->n = 1;
|
|
|
|
|
2021-10-21 16:41:52 +02:00
|
|
|
chan->stack[chan->n - 1] = st;
|
2021-10-21 16:15:29 +02:00
|
|
|
chan->t = *chan->clock;
|
2021-10-18 12:47:51 +02:00
|
|
|
|
|
|
|
/* Only append if not dirty */
|
|
|
|
if(!chan->dirty)
|
|
|
|
{
|
|
|
|
mark_dirty(chan);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dbg("already dirty chan %d: skip update list\n",
|
|
|
|
chan->id);
|
|
|
|
chan_dump_update_list(chan);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
chan_enable_and_set(struct ovni_chan *chan, int st)
|
|
|
|
{
|
|
|
|
chan_enable(chan, 1);
|
|
|
|
chan_set(chan, st);
|
2021-10-21 16:15:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
chan_push(struct ovni_chan *chan, int st)
|
|
|
|
{
|
2021-10-18 12:47:51 +02:00
|
|
|
dbg("chan_push chan %d st=%d\n", chan->id, st);
|
2021-10-21 16:15:29 +02:00
|
|
|
|
|
|
|
assert(chan->enabled);
|
2021-10-18 12:47:51 +02:00
|
|
|
|
|
|
|
/* Cannot be dirty */
|
2021-10-21 16:15:29 +02:00
|
|
|
assert(chan->dirty == 0);
|
|
|
|
|
|
|
|
if(chan->n >= MAX_CHAN_STACK)
|
|
|
|
{
|
|
|
|
err("channel stack full\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
chan->stack[chan->n++] = st;
|
|
|
|
chan->t = *chan->clock;
|
2021-10-18 12:47:51 +02:00
|
|
|
|
|
|
|
mark_dirty(chan);
|
2021-10-21 16:15:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
chan_pop(struct ovni_chan *chan, int expected_st)
|
|
|
|
{
|
|
|
|
int st;
|
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
dbg("chan_pop chan %d expected_st=%d\n", chan->id, expected_st);
|
2021-10-21 16:15:29 +02:00
|
|
|
|
|
|
|
assert(chan->enabled);
|
2021-10-18 12:47:51 +02:00
|
|
|
|
|
|
|
/* Cannot be dirty */
|
2021-10-21 16:15:29 +02:00
|
|
|
assert(chan->dirty == 0);
|
|
|
|
|
|
|
|
if(chan->n <= 0)
|
|
|
|
{
|
|
|
|
err("channel empty\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
st = chan->stack[chan->n - 1];
|
|
|
|
|
|
|
|
if(expected_st >= 0 && st != expected_st)
|
|
|
|
{
|
|
|
|
err("unexpected channel state %d (expected %d)\n",
|
|
|
|
st, expected_st);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
chan->n--;
|
|
|
|
chan->t = *chan->clock;
|
2021-10-18 12:47:51 +02:00
|
|
|
|
|
|
|
mark_dirty(chan);
|
2021-10-21 16:15:29 +02:00
|
|
|
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
chan_ev(struct ovni_chan *chan, int ev)
|
|
|
|
{
|
2021-10-18 12:47:51 +02:00
|
|
|
dbg("chan_ev chan %d ev=%d\n", chan->id, ev);
|
2021-10-21 16:41:52 +02:00
|
|
|
|
2021-10-21 16:15:29 +02:00
|
|
|
assert(chan->enabled);
|
2021-10-18 12:47:51 +02:00
|
|
|
|
|
|
|
/* Cannot be dirty */
|
2021-10-21 16:15:29 +02:00
|
|
|
assert(chan->dirty == 0);
|
|
|
|
assert(ev >= 0);
|
|
|
|
|
|
|
|
chan->ev = ev;
|
|
|
|
chan->t = *chan->clock;
|
2021-10-18 12:47:51 +02:00
|
|
|
|
|
|
|
mark_dirty(chan);
|
2021-10-21 16:15:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
chan_get_st(struct ovni_chan *chan)
|
|
|
|
{
|
|
|
|
if(chan->enabled == 0)
|
|
|
|
return chan->badst;
|
|
|
|
|
2021-10-21 16:41:52 +02:00
|
|
|
if(chan->n == 0)
|
|
|
|
return 0;
|
|
|
|
|
2021-10-21 16:15:29 +02:00
|
|
|
assert(chan->n > 0);
|
|
|
|
return chan->stack[chan->n-1];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
emit_ev(struct ovni_chan *chan)
|
|
|
|
{
|
|
|
|
int new, last;
|
|
|
|
|
|
|
|
assert(chan->enabled);
|
|
|
|
assert(chan->ev != -1);
|
|
|
|
|
|
|
|
new = chan->ev;
|
|
|
|
last = chan_get_st(chan);
|
|
|
|
|
|
|
|
prv_ev(chan->prv, chan->row, chan->t-1, chan->type, new);
|
|
|
|
prv_ev(chan->prv, chan->row, chan->t, chan->type, last);
|
|
|
|
|
|
|
|
chan->ev = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
emit_st(struct ovni_chan *chan)
|
|
|
|
{
|
|
|
|
int st;
|
|
|
|
|
|
|
|
st = chan_get_st(chan);
|
|
|
|
|
|
|
|
prv_ev(chan->prv, chan->row, chan->t, chan->type, st);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Emits either the current state or punctual event in the PRV file */
|
|
|
|
void
|
|
|
|
chan_emit(struct ovni_chan *chan)
|
|
|
|
{
|
2021-10-18 12:47:51 +02:00
|
|
|
if(likely(chan->dirty == 0))
|
2021-10-21 16:15:29 +02:00
|
|
|
return;
|
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
dbg("chan_emit chan %d\n", chan->id);
|
2021-10-21 16:41:52 +02:00
|
|
|
|
2021-10-21 16:15:29 +02:00
|
|
|
/* Emit badst if disabled */
|
|
|
|
if(chan->enabled == 0)
|
|
|
|
{
|
|
|
|
/* No punctual events allowed when disabled */
|
|
|
|
assert(chan->ev == -1);
|
|
|
|
|
|
|
|
emit_st(chan);
|
|
|
|
goto shower;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, emit punctual event if any or the state */
|
|
|
|
if(chan->ev != -1)
|
|
|
|
emit_ev(chan);
|
|
|
|
else
|
|
|
|
emit_st(chan);
|
|
|
|
|
|
|
|
shower:
|
|
|
|
chan->dirty = 0;
|
|
|
|
}
|