Add support for multiple models

This commit is contained in:
Rodrigo Arias 2023-02-01 13:07:10 +01:00 committed by Rodrigo Arias Mallo
parent 123178c340
commit 9041583207
9 changed files with 177 additions and 84 deletions

View File

@ -17,7 +17,8 @@ add_library(emu STATIC
emu.c
emu_args.c
emu_ev.c
emu_model.c
model.c
models.c
player.c
stream.c
trace.c

View File

@ -51,18 +51,20 @@ emu_init(struct emu *emu, int argc, char *argv[])
return -1;
}
// /* Register all the models */
// emu_model_register(&emu->model, &ovni_model_spec, emu);
model_init(&emu->model);
if (model_ovni.create && model_ovni.create(emu) != 0) {
err("model ovni create failed");
return -1;
}
if (model_nanos6.create && model_nanos6.create(emu) != 0) {
err("model nanos6 create failed");
/* Register all the models */
models_register(&emu->model);
if (model_probe(&emu->model, emu) != 0) {
err("model_probe failed");
return -1;
}
if (model_create(&emu->model, emu) != 0) {
err("model_create failed");
return -1;
}
return 0;
}
@ -70,14 +72,11 @@ emu_init(struct emu *emu, int argc, char *argv[])
int
emu_connect(struct emu *emu)
{
if (model_ovni.connect && model_ovni.connect(emu) != 0) {
err("model ovni connect failed");
return -1;
}
if (model_nanos6.connect && model_nanos6.connect(emu) != 0) {
err("model nanos6 connect failed");
if (model_connect(&emu->model, emu) != 0) {
err("model_connect failed");
return -1;
}
return 0;
}
@ -143,14 +142,8 @@ emu_step(struct emu *emu)
}
/* Otherwise progress */
if (emu->ev->m == 'O' && model_ovni.event(emu) != 0) {
err("ovni event failed");
panic(emu);
return -1;
}
if (emu->ev->m == '6' && model_nanos6.event(emu) != 0) {
err("nanos6 event failed");
if (model_event(&emu->model, emu, emu->ev->m) != 0) {
err("model_event failed");
panic(emu);
return -1;
}

View File

@ -9,7 +9,7 @@
#include "emu_args.h"
#include "system.h"
#include "player.h"
#include "emu_model.h"
#include "model.h"
#include "emu_ev.h"
#include "recorder.h"
@ -25,7 +25,7 @@ struct emu {
struct trace trace;
struct system system;
struct player player;
struct emu_model model;
struct model model;
struct recorder recorder;
/* Quick access */

View File

@ -1,27 +0,0 @@
/* Copyright (c) 2021-2023 Barcelona Supercomputing Center (BSC)
* SPDX-License-Identifier: GPL-3.0-or-later */
#include "emu_model.h"
#include <stdlib.h>
int
emu_model_register(struct emu_model *model, struct model_spec *spec, void *ctx)
{
int i = spec->model;
model->ctx[i] = ctx;
model->spec[i] = spec;
return 0;
}
void *
emu_model_get_context(struct emu_model *model, struct model_spec *spec, int imodel)
{
for (int i = 0; spec->depends[i]; i++) {
if (spec->depends[i] == imodel)
return model->ctx[imodel];
}
/* Not allowed */
return NULL;
}

View File

@ -1,29 +0,0 @@
/* Copyright (c) 2021-2023 Barcelona Supercomputing Center (BSC)
* SPDX-License-Identifier: GPL-3.0-or-later */
#ifndef EMU_MODEL_H
#define EMU_MODEL_H
#include "emu_hook.h"
struct model_spec {
char *name;
int model;
char *depends;
emu_hook_t *probe;
emu_hook_t *create;
emu_hook_t *connect;
emu_hook_t *event;
};
struct emu_model {
struct model_spec *spec[256];
void *ctx[256];
};
int emu_model_parse(struct emu_model *model, struct model_spec *spec, void *ctx);
int emu_model_register(struct emu_model *model, struct model_spec *spec, void *ctx);
int emu_model_register(struct emu_model *model, struct model_spec *spec, void *ctx);
void *emu_model_get_context(struct emu_model *model, struct model_spec *spec, int imodel);
#endif /* EMU_MODEL_H */

98
src/emu/model.c Normal file
View File

@ -0,0 +1,98 @@
/* Copyright (c) 2021-2023 Barcelona Supercomputing Center (BSC)
* SPDX-License-Identifier: GPL-3.0-or-later */
#include "model.h"
#include "common.h"
#include <string.h>
void
model_init(struct model *model)
{
memset(model, 0, sizeof(struct model));
}
void
model_register(struct model *model, struct model_spec *spec)
{
int i = spec->model;
model->spec[i] = spec;
model->registered[i] = 1;
}
int
model_probe(struct model *model, struct emu *emu)
{
for (int i = 0; i < MAX_MODELS; i++) {
if (!model->registered[i])
continue;
struct model_spec *spec = model->spec[i];
if (spec->probe == NULL)
continue;
if (spec->probe(emu) != 0) {
err("probe failed for model '%c'", (char) i);
return -1;
}
}
return 0;
}
int
model_create(struct model *model, struct emu *emu)
{
for (int i = 0; i < MAX_MODELS; i++) {
if (!model->registered[i])
continue;
struct model_spec *spec = model->spec[i];
if (spec->create == NULL)
continue;
if (spec->create(emu) != 0) {
err("create failed for model '%c'", (char) i);
return -1;
}
}
return 0;
}
int
model_connect(struct model *model, struct emu *emu)
{
for (int i = 0; i < MAX_MODELS; i++) {
if (!model->registered[i])
continue;
struct model_spec *spec = model->spec[i];
if (spec->connect == NULL)
continue;
if (spec->connect(emu) != 0) {
err("connect failed for model '%c'", (char) i);
return -1;
}
}
return 0;
}
int
model_event(struct model *model, struct emu *emu, int index)
{
if (!model->registered[index]) {
err("no model registered for '%c'", (char) index);
return -1;
}
struct model_spec *spec = model->spec[index];
if (spec->event == NULL)
return 0;
if (spec->event(emu) != 0) {
err("event() failed for model '%c'", (char) index);
return -1;
}
return 0;
}

34
src/emu/model.h Normal file
View File

@ -0,0 +1,34 @@
/* Copyright (c) 2021-2023 Barcelona Supercomputing Center (BSC)
* SPDX-License-Identifier: GPL-3.0-or-later */
#ifndef MODEL_H
#define MODEL_H
#include "emu_hook.h"
struct model_spec {
char *name;
int model;
char *depends;
emu_hook_t *probe;
emu_hook_t *create;
emu_hook_t *connect;
emu_hook_t *event;
};
#define MAX_MODELS 256
struct model {
struct model_spec *spec[MAX_MODELS];
int registered[MAX_MODELS];
};
void model_init(struct model *model);
void model_register(struct model *model, struct model_spec *spec);
int model_probe(struct model *model, struct emu *emu);
int model_create(struct model *model, struct emu *emu);
int model_connect(struct model *model, struct emu *emu);
int model_event(struct model *model, struct emu *emu, int index);
#endif /* MODEL_H */

23
src/emu/models.c Normal file
View File

@ -0,0 +1,23 @@
/* Copyright (c) 2023 Barcelona Supercomputing Center (BSC)
* SPDX-License-Identifier: GPL-3.0-or-later */
#include "models.h"
#include <stdlib.h>
extern struct model_spec model_ovni;
extern struct model_spec model_nanos6;
static struct model_spec *models[] = {
&model_ovni,
&model_nanos6,
NULL
};
void
models_register(struct model *model)
{
for (int i = 0; models[i] != NULL; i++) {
model_register(model, models[i]);
}
}

View File

@ -4,9 +4,9 @@
#ifndef MODELS_H
#define MODELS_H
#include "emu_model.h"
#include "model.h"
extern struct model_spec model_ovni;
extern struct model_spec model_nanos6;
void
models_register(struct model *model);
#endif /* MODELS_H */