ovni/emu.c

1242 lines
25 KiB
C
Raw Normal View History

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-09-28 19:21:22 +02:00
#define _POSIX_C_SOURCE 200112L
#include <stdarg.h>
2021-07-28 11:56:35 +02:00
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <linux/limits.h>
#include <errno.h>
#include <sys/stat.h>
#include <stdatomic.h>
#include <dirent.h>
2021-08-03 19:56:31 +02:00
#include <unistd.h>
#include <time.h>
2021-07-28 11:56:35 +02:00
#include "ovni.h"
#include "trace.h"
2021-07-28 11:56:35 +02:00
#include "emu.h"
#include "prv.h"
2021-08-03 20:47:02 +02:00
#include "pcf.h"
2021-10-21 16:15:29 +02:00
#include "chan.h"
#include "utlist.h"
2021-07-28 11:56:35 +02:00
2021-08-03 19:56:31 +02:00
/* Obtains the corrected clock of the given event */
static int64_t
2021-08-03 19:56:31 +02:00
evclock(struct ovni_stream *stream, struct ovni_ev *ev)
{
return (int64_t) ovni_ev_get_clock(ev) + stream->clock_offset;
}
2021-07-28 11:56:35 +02:00
static void
print_ev(struct ovni_stream *stream, struct ovni_ev *ev)
2021-07-28 11:56:35 +02:00
{
int64_t clock, delta = 0;
2021-07-28 11:56:35 +02:00
int i, payloadsize;
UNUSED(delta);
2021-07-28 11:56:35 +02:00
//dbg("sizeof(*ev) = %d\n", sizeof(*ev));
//hexdump((uint8_t *) ev, sizeof(*ev));
2021-08-03 19:56:31 +02:00
clock = evclock(stream, ev);
2021-07-28 11:56:35 +02:00
delta = clock - stream->lastclock;
dbg(">>> %s.%d.%d %c %c %c % 20ld % 15ld ",
stream->loom->hostname,
stream->proc->pid,
stream->thread->tid,
ev->header.model, ev->header.category, ev->header.value, clock, delta);
2021-07-28 11:56:35 +02:00
payloadsize = ovni_payload_size(ev);
for(i=0; i<payloadsize; i++)
{
dbg("%d ", ev->payload.u8[i]);
}
dbg("\n");
}
2021-07-28 11:56:35 +02:00
static void
print_cur_ev(struct ovni_emu *emu)
{
print_ev(emu->cur_stream, emu->cur_ev);
2021-07-28 11:56:35 +02:00
}
/* Update the tracking channel if needed */
static void
cpu_update_tracking_chan(struct ovni_chan *cpu_chan, struct ovni_ethread *th)
2021-07-28 11:56:35 +02:00
{
int th_enabled, cpu_enabled, st;
struct ovni_chan *th_chan;
switch (cpu_chan->track)
{
case CHAN_TRACK_TH_RUNNING:
cpu_enabled = th->is_running;
break;
case CHAN_TRACK_TH_ACTIVE:
cpu_enabled = th->is_active;
break;
default:
dbg("ignoring thread %d chan %d with track=%d\n",
th->tid, cpu_chan->id, cpu_chan->track);
return;
}
th_chan = &th->chan[cpu_chan->id];
th_enabled = chan_is_enabled(th_chan);
/* Enable the cpu channel if needed */
if(cpu_enabled && !chan_is_enabled(cpu_chan))
chan_enable(cpu_chan, cpu_enabled);
/* Copy the state from the thread channel if needed */
if(th_enabled && cpu_enabled)
{
/* Both enabled: simply follow the same value */
st = chan_get_st(th_chan);
if(chan_get_st(cpu_chan) != st)
chan_set(cpu_chan, st);
}
else if(th_enabled && !cpu_enabled)
{
/* Only thread enabled: disable CPU */
if(chan_is_enabled(cpu_chan))
chan_disable(cpu_chan);
}
else if(!th_enabled && cpu_enabled)
{
/* Only CPU enabled: is this possible? Set to bad */
chan_set(cpu_chan, ST_BAD);
err("warning: cpu %s chan %d enabled but tracked thread %d chan disabled\n",
cpu_chan->cpu->name,
cpu_chan->id,
th->tid);
}
else
{
/* Both disabled: disable CPU channel if needed */
if(chan_is_enabled(cpu_chan))
chan_disable(cpu_chan);
}
dbg("cpu %s chan %d enabled=%d state=%d\n",
cpu_chan->cpu->name, cpu_chan->id,
chan_is_enabled(cpu_chan),
chan_get_st(cpu_chan));
2021-07-28 11:56:35 +02:00
}
void
emu_cpu_update_chan(struct ovni_cpu *cpu, struct ovni_chan *cpu_chan)
2021-07-28 11:56:35 +02:00
{
int count;
struct ovni_ethread *th;
2021-07-28 11:56:35 +02:00
/* Determine the source of tracking */
switch (cpu_chan->track)
2021-07-28 11:56:35 +02:00
{
case CHAN_TRACK_TH_RUNNING:
count = cpu->nrunning_threads;
th = cpu->th_running;
break;
case CHAN_TRACK_TH_ACTIVE:
count = cpu->nactive_threads;
th = cpu->th_active;
break;
default:
dbg("ignoring %s chan %d with track=%d\n",
cpu->name, cpu_chan->id, cpu_chan->track);
return;
2021-07-28 11:56:35 +02:00
}
/* Based on how many threads, determine the state */
if(count == 0)
{
/* The channel can be already disabled (migration of paused
* thread) so only disable it if needed. */
if(chan_is_enabled(cpu_chan))
chan_disable(cpu_chan);
}
else if(count == 1)
{
if(th == NULL)
die("emu_cpu_update_chan: tracking thread is NULL\n");
/* A unique thread found: copy the state */
dbg("cpu_update_chan: unique thread %d found, updating chan %d\n",
th->tid, cpu_chan->id);
cpu_update_tracking_chan(cpu_chan, th);
}
else
{
/* More than one thread: enable the channel and set it to a
* error value */
if(!chan_is_enabled(cpu_chan))
chan_enable(cpu_chan, 1);
if(chan_get_st(cpu_chan) != ST_TOO_MANY_TH)
chan_set(cpu_chan, ST_TOO_MANY_TH);
}
2021-07-28 11:56:35 +02:00
}
2021-10-21 16:15:29 +02:00
static void
propagate_channels(struct ovni_emu *emu)
2021-10-21 16:15:29 +02:00
{
2021-10-21 16:44:13 +02:00
struct ovni_cpu *cpu;
struct ovni_chan *cpu_chan, *th_chan, *tmp;
struct ovni_ethread *thread;
/* Only propagate thread channels to their corresponding CPU */
2021-10-21 16:15:29 +02:00
DL_FOREACH_SAFE(emu->th_chan, th_chan, tmp)
2021-10-21 16:44:13 +02:00
{
if(th_chan->thread == NULL)
die("propagate_channels: channel thread is NULL\n");
2021-10-21 16:44:13 +02:00
thread = th_chan->thread;
2021-10-11 11:12:26 +02:00
/* No CPU in the thread */
if(thread->cpu == NULL)
continue;
2021-10-21 16:44:13 +02:00
cpu = thread->cpu;
2021-10-21 16:44:13 +02:00
cpu_chan = &cpu->chan[th_chan->id];
2021-10-11 11:12:26 +02:00
dbg("propagate thread %d chan %d in cpu %s\n",
thread->tid, th_chan->id, cpu->name);
2021-10-11 11:12:26 +02:00
emu_cpu_update_chan(cpu, cpu_chan);
2021-10-21 16:44:13 +02:00
}
}
static void
emit_channels(struct ovni_emu *emu)
{
struct ovni_chan *cpu_chan, *th_chan;
dbg("emu enters emit_channels -------------------\n");
2021-10-21 16:44:13 +02:00
/* Emit only the channels that have been updated. We need a list
* of updated channels */
DL_FOREACH(emu->th_chan, th_chan)
{
dbg("emu emits th chan %d\n", th_chan->id);
chan_emit(th_chan);
}
2021-10-21 16:44:13 +02:00
DL_FOREACH(emu->cpu_chan, cpu_chan)
{
dbg("emu emits cpu chan %d\n", cpu_chan->id);
chan_emit(cpu_chan);
}
2021-10-21 16:44:13 +02:00
/* Reset the list of updated channels */
dbg("emu resets the list of dirty channels -------------------\n");
emu->th_chan = NULL;
emu->cpu_chan = NULL;
2021-10-21 16:15:29 +02:00
}
static void
hook_init(struct ovni_emu *emu)
{
hook_init_ovni(emu);
hook_init_nosv(emu);
2021-10-11 17:29:45 +02:00
hook_init_tampi(emu);
2021-10-11 12:29:12 +02:00
hook_init_openmp(emu);
2021-12-03 16:24:41 +01:00
hook_init_nodes(emu);
hook_init_kernel(emu);
2022-07-01 17:54:18 +02:00
hook_init_nanos6(emu);
}
static void
hook_end(struct ovni_emu *emu)
{
hook_end_nosv(emu);
2022-07-01 17:54:18 +02:00
hook_end_nanos6(emu);
}
2021-07-28 11:56:35 +02:00
static void
2021-07-28 19:12:20 +02:00
hook_pre(struct ovni_emu *emu)
2021-07-28 11:56:35 +02:00
{
2021-07-30 20:08:40 +02:00
switch(emu->cur_ev->header.model)
2021-07-28 11:56:35 +02:00
{
case 'O': hook_pre_ovni(emu); break;
case 'V': hook_pre_nosv(emu); break;
case 'T': hook_pre_tampi(emu); break;
case 'M': hook_pre_openmp(emu); break;
2021-12-03 16:24:41 +01:00
case 'D': hook_pre_nodes(emu); break;
case 'K': hook_pre_kernel(emu); break;
2022-07-01 17:54:18 +02:00
case '6': hook_pre_nanos6(emu); break;
2021-09-27 17:42:14 +02:00
default:
break;
2021-07-28 19:12:20 +02:00
}
}
static void
hook_post(struct ovni_emu *emu)
{
2021-07-30 20:08:40 +02:00
switch(emu->cur_ev->header.model)
2021-07-28 19:12:20 +02:00
{
2021-07-28 11:56:35 +02:00
default:
//dbg("unknown model %c\n", emu->cur_ev->model);
break;
}
}
static void
set_current(struct ovni_emu *emu, struct ovni_stream *stream)
{
emu->cur_stream = stream;
2021-07-30 20:08:40 +02:00
emu->cur_ev = stream->cur_ev;
emu->cur_loom = stream->loom;
emu->cur_proc = stream->proc;
emu->cur_thread = stream->thread;
2021-07-28 11:56:35 +02:00
}
/*
* heap_node_compare_t - comparison function, returns:
* > 0 if a > b
* < 0 if a < b
* = 0 if a == b
*
* Invert the comparison function to get a min-heap instead
*/
static inline int stream_cmp(heap_node_t *a, heap_node_t *b)
2021-07-28 11:56:35 +02:00
{
struct ovni_stream *sa, *sb;
2021-08-03 19:56:31 +02:00
sa = heap_elem(a, struct ovni_stream, hh);
sb = heap_elem(b, struct ovni_stream, hh);
2021-07-28 11:56:35 +02:00
if(sb->lastclock < sa->lastclock)
return -1;
else if(sb->lastclock > sa->lastclock)
return +1;
else
return 0;
}
2021-07-28 11:56:35 +02:00
static double
get_time(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (double)ts.tv_sec + (double)ts.tv_nsec * 1.0e-9;
}
2021-09-27 17:42:14 +02:00
static void
print_progress(struct ovni_emu *emu)
{
double progress, time_now, time_elapsed, time_total, time_left;
double speed_in, speed_out;
double cpu_written, th_written, written;
int min, sec;
2021-09-27 17:42:14 +02:00
cpu_written = (double) ftell(emu->prv_cpu);
th_written = (double) ftell(emu->prv_thread);
2021-07-28 11:56:35 +02:00
written = cpu_written + th_written;
2021-07-28 11:56:35 +02:00
progress = ((double) emu->global_offset) /
((double) emu->global_size);
2021-07-28 11:56:35 +02:00
time_now = get_time();
time_elapsed = time_now - emu->start_emulation_time;
time_total = time_elapsed / progress;
time_left = time_total - time_elapsed;
2021-07-28 11:56:35 +02:00
speed_in = (double) emu->nev_processed / time_elapsed;
speed_out = written / time_elapsed;
min = (int) (time_left / 60.0);
sec = (int) ((time_left / 60.0 - min) * 60);
err("%.1f%% done at %.0f Kev/s, out %.1f GB CPU / %.1f GB TH at %.1f MB/s (%d min %d s left)\n",
100.0 * progress,
speed_in / 1024.0,
cpu_written / (1024.0 * 1024.0 * 1024.0),
th_written / (1024.0 * 1024.0 * 1024.0),
speed_out / (1024.0 * 1024),
min, sec);
}
/* Loads the next event and sets the lastclock in the stream.
* Returns -1 if the stream has no more events. */
static int
emu_step_stream(struct ovni_emu *emu, struct ovni_stream *stream)
{
if(ovni_load_next_event(stream) < 0)
return -1;
stream->lastclock = evclock(stream, stream->cur_ev);
heap_insert(&emu->sorted_stream, &stream->hh, &stream_cmp);
return 0;
}
static int
next_event(struct ovni_emu *emu)
{
struct ovni_stream *stream;
heap_node_t *node;
static int done_first = 0;
/* Extract the next stream based on the event clock */
node = heap_pop_max(&emu->sorted_stream, stream_cmp);
2021-07-28 11:56:35 +02:00
/* No more streams */
if(node == NULL)
2021-07-28 11:56:35 +02:00
return -1;
stream = heap_elem(node, struct ovni_stream, hh);
if(stream == NULL)
die("next_event: heap_elem returned NULL\n");
2021-07-28 11:56:35 +02:00
set_current(emu, stream);
emu->global_offset += ovni_ev_size(stream->cur_ev);
//err("stream %d clock at %ld\n", stream->tid, stream->lastclock);
/* This can happen if two events are not ordered in the stream, but the
* emulator picks other events in the middle. Example:
*
* Stream A: 10 3 ...
* Stream B: 5 12
*
* emulator output:
* 5
* 10
* 3 -> warning!
* 12
* ...
* */
if(emu->lastclock > stream->lastclock)
2021-07-28 11:56:35 +02:00
{
err("warning: backwards jump in time %lu -> %lu for tid %d\n",
emu->lastclock, stream->lastclock, stream->tid);
2021-11-08 17:06:17 +01:00
if(emu->enable_linter)
abort();
2021-07-28 11:56:35 +02:00
}
emu->lastclock = stream->lastclock;
2021-07-28 11:56:35 +02:00
2021-08-03 19:56:31 +02:00
if(!done_first)
{
done_first = 1;
emu->firstclock = emu->lastclock;
}
2021-07-29 17:46:25 +02:00
2021-08-03 19:56:31 +02:00
emu->delta_time = emu->lastclock - emu->firstclock;
2021-07-29 17:46:25 +02:00
2021-07-28 11:56:35 +02:00
return 0;
}
static void
emu_load_first_events(struct ovni_emu *emu)
2021-07-28 11:56:35 +02:00
{
size_t i;
2021-07-28 11:56:35 +02:00
struct ovni_trace *trace;
struct ovni_stream *stream;
/* Prepare the stream heap */
heap_init(&emu->sorted_stream);
2021-07-28 11:56:35 +02:00
emu->lastclock = 0;
/* Load initial streams and events */
2021-07-28 11:56:35 +02:00
trace = &emu->trace;
for(i=0; i<trace->nstreams; i++)
{
stream = &trace->stream[i];
emu->global_size += stream->size;
if(emu_step_stream(emu, stream) < 0)
{
err("warning: empty stream for tid %d\n", stream->tid);
if(emu->enable_linter)
abort();
continue;
}
2021-07-28 11:56:35 +02:00
}
}
2021-07-28 11:56:35 +02:00
static void
emulate(struct ovni_emu *emu)
{
size_t i;
emu->nev_processed = 0;
err("loading first events\n");
emu_load_first_events(emu);
err("emulation starts\n");
emu->start_emulation_time = get_time();
2021-07-28 11:56:35 +02:00
hook_init(emu);
emit_channels(emu);
2021-07-28 11:56:35 +02:00
/* Then process all events */
for(i=0; next_event(emu) == 0; i++)
2021-07-28 11:56:35 +02:00
{
print_cur_ev(emu);
2021-07-28 19:12:20 +02:00
hook_pre(emu);
propagate_channels(emu);
emit_channels(emu);
2021-07-28 19:12:20 +02:00
hook_post(emu);
2021-07-28 11:56:35 +02:00
if(i >= 50000)
{
print_progress(emu);
i = 0;
}
emu->nev_processed++;
emu_step_stream(emu, emu->cur_stream);
2021-07-28 11:56:35 +02:00
}
hook_end(emu);
print_progress(emu);
2021-07-28 11:56:35 +02:00
}
struct ovni_ethread *
emu_get_thread(struct ovni_eproc *proc, int tid)
2021-07-28 11:56:35 +02:00
{
size_t i;
2021-07-28 11:56:35 +02:00
struct ovni_ethread *thread;
for(i=0; i<proc->nthreads; i++)
2021-07-28 11:56:35 +02:00
{
thread = &proc->thread[i];
if(thread->tid == tid)
return thread;
2021-07-28 11:56:35 +02:00
}
return NULL;
2021-07-28 11:56:35 +02:00
}
static void
add_new_cpu(struct ovni_emu *emu, struct ovni_loom *loom, int i, int phyid)
{
2021-10-21 16:34:04 +02:00
struct ovni_cpu *cpu;
cpu = &loom->cpu[i];
2021-11-18 18:27:28 +01:00
if(i < 0 || i >= (int) loom->ncpus)
die("CPU with index %d in loom %s is out of bounds\n",
i, loom->hostname);
2021-11-18 18:27:28 +01:00
if(cpu->state != CPU_ST_UNKNOWN)
die("new cpu %d in unexpected state in loom %s\n",
i, loom->hostname);
2021-10-21 16:34:04 +02:00
cpu->state = CPU_ST_READY;
cpu->i = i;
cpu->phyid = phyid;
cpu->gindex = emu->total_ncpus++;
2021-10-14 09:08:07 +02:00
cpu->loom = loom;
2021-10-21 16:34:04 +02:00
dbg("new cpu %d at phyid=%d\n", cpu->gindex, phyid);
}
static int
proc_load_cpus(struct ovni_emu *emu, struct ovni_loom *loom,
struct ovni_eproc *proc,
struct ovni_eproc *metadata_proc)
{
JSON_Object *meta = json_value_get_object(proc->meta);
if(meta == NULL)
die("json_value_get_object() failed\n");
JSON_Array *cpuarray = json_object_get_array(meta, "cpus");
/* This process doesn't have the cpu list */
if(cpuarray == NULL)
return -1;
if(metadata_proc)
die("duplicated metadata for proc %d and %d in loom %s\n",
metadata_proc->pid, proc->pid,
loom->hostname);
if(loom->ncpus != 0)
die("loom %s already has CPUs\n", loom->hostname);
2021-11-18 18:27:28 +01:00
loom->ncpus = json_array_get_count(cpuarray);
if(loom->ncpus == 0)
die("loom %s proc %d has metadata but no CPUs\n",
loom->hostname, proc->pid);
2021-11-18 18:27:28 +01:00
loom->cpu = calloc(loom->ncpus, sizeof(struct ovni_cpu));
if(loom->cpu == NULL)
die("calloc failed: %s\n", strerror(errno));
2021-11-18 18:27:28 +01:00
for(size_t i = 0; i < loom->ncpus; i++)
{
JSON_Object *cpu = json_array_get_object(cpuarray, i);
2021-11-18 18:27:28 +01:00
if(cpu == NULL)
die("proc_load_cpus: json_array_get_object() failed\n");
2021-11-18 18:27:28 +01:00
2021-12-09 15:55:18 +01:00
int index = (int) json_object_get_number(cpu, "index");
int phyid = (int) json_object_get_number(cpu, "phyid");
add_new_cpu(emu, loom, index, phyid);
}
2021-10-21 16:34:04 +02:00
2021-11-18 18:27:28 +01:00
/* If we reach this point, all CPUs are in the ready state */
2021-10-21 16:34:04 +02:00
/* Init the vcpu as well */
struct ovni_cpu *vcpu = &loom->vcpu;
if(vcpu->state != CPU_ST_UNKNOWN)
die("unexpected virtual CPU state in loom %s\n",
loom->hostname);
2021-10-21 16:34:04 +02:00
vcpu->state = CPU_ST_READY;
vcpu->i = -1;
vcpu->phyid = -1;
vcpu->gindex = emu->total_ncpus++;
2021-10-14 09:08:07 +02:00
vcpu->loom = loom;
2021-10-21 16:34:04 +02:00
dbg("new vcpu %d\n", vcpu->gindex);
return 0;
}
/* Obtain CPUs in the metadata files and other data */
static void
load_metadata(struct ovni_emu *emu)
{
size_t i, j;
struct ovni_loom *loom;
struct ovni_eproc *proc;
struct ovni_trace *trace;
trace = &emu->trace;
for(i=0; i<trace->nlooms; i++)
{
loom = &trace->loom[i];
2021-10-21 16:34:04 +02:00
loom->offset_ncpus = emu->total_ncpus;
struct ovni_eproc *metadata_proc = NULL;
for(j=0; j<loom->nprocs; j++)
{
proc = &loom->proc[j];
if(proc_load_cpus(emu, loom, proc, metadata_proc) < 0)
continue;
if(metadata_proc)
die("duplicated metadata found in pid %d and %d\n",
metadata_proc->pid,
proc->pid);
metadata_proc = proc;
}
/* One of the process must have the list of CPUs */
if(metadata_proc == NULL)
die("no metadata found in loom %s\n", loom->hostname);
if(loom->ncpus == 0)
die("no CPUs found in loom %s\n", loom->hostname);
}
}
static int
destroy_metadata(struct ovni_emu *emu)
{
size_t i, j;
struct ovni_loom *loom;
struct ovni_eproc *proc;
struct ovni_trace *trace;
trace = &emu->trace;
for(i=0; i<trace->nlooms; i++)
{
loom = &trace->loom[i];
for(j=0; j<loom->nprocs; j++)
{
proc = &loom->proc[j];
if(proc->meta == NULL)
die("cannot destroy metadata: is NULL\n");
json_value_free(proc->meta);
}
2021-11-18 18:27:28 +01:00
free(loom->cpu);
}
return 0;
}
2021-07-30 21:37:25 +02:00
static void
open_prvs(struct ovni_emu *emu, char *tracedir)
{
char path[PATH_MAX];
sprintf(path, "%s/%s", tracedir, "thread.prv");
emu->prv_thread = fopen(path, "w");
if(emu->prv_thread == NULL)
{
err("error opening thread PRV file %s: %s\n", path,
strerror(errno));
exit(EXIT_FAILURE);
}
sprintf(path, "%s/%s", tracedir, "cpu.prv");
emu->prv_cpu = fopen(path, "w");
if(emu->prv_cpu == NULL)
{
err("error opening cpu PRV file %s: %s\n", path,
strerror(errno));
exit(EXIT_FAILURE);
}
2021-10-21 16:34:04 +02:00
prv_header(emu->prv_thread, emu->total_nthreads);
prv_header(emu->prv_cpu, emu->total_ncpus);
}
2021-08-03 20:47:02 +02:00
static void
open_pcfs(struct ovni_emu *emu, char *tracedir)
{
char path[PATH_MAX];
sprintf(path, "%s/%s", tracedir, "thread.pcf");
2022-05-31 20:39:57 +02:00
pcf_open(&emu->pcf[CHAN_TH], path, CHAN_TH);
2021-08-03 20:47:02 +02:00
sprintf(path, "%s/%s", tracedir, "cpu.pcf");
2022-05-31 20:39:57 +02:00
pcf_open(&emu->pcf[CHAN_CPU], path, CHAN_CPU);
2021-08-03 20:47:02 +02:00
}
/* Fix the trace duration at the end */
static void
fix_prv_headers(struct ovni_emu *emu)
{
prv_fix_header(emu->prv_thread, emu->delta_time, emu->total_nthreads);
prv_fix_header(emu->prv_cpu, emu->delta_time, emu->total_ncpus);
}
static void
close_prvs(struct ovni_emu *emu)
{
fclose(emu->prv_thread);
fclose(emu->prv_cpu);
}
2021-08-03 20:47:02 +02:00
static void
close_pcfs(struct ovni_emu *emu)
{
2022-05-31 20:39:57 +02:00
pcf_close(&emu->pcf[CHAN_TH]);
pcf_close(&emu->pcf[CHAN_CPU]);
2021-08-03 20:47:02 +02:00
}
2021-08-03 19:56:31 +02:00
static void
usage(int argc, char *argv[])
{
UNUSED(argc);
UNUSED(argv);
2021-08-03 19:56:31 +02:00
err("Usage: emu [-c offsetfile] tracedir\n");
err("\n");
err("Options:\n");
err(" -c offsetfile Use the given offset file to correct\n");
err(" the clocks among nodes. It can be\n");
err(" generated by the ovnisync program\n");
err("\n");
err(" tracedir The output trace dir generated by ovni.\n");
err("\n");
err("The output PRV files are placed in the tracedir directory.\n");
exit(EXIT_FAILURE);
}
2021-07-30 21:37:25 +02:00
2021-08-03 19:56:31 +02:00
static void
parse_args(struct ovni_emu *emu, int argc, char *argv[])
2021-07-28 11:56:35 +02:00
{
2021-08-03 19:56:31 +02:00
int opt;
2021-11-08 17:06:17 +01:00
while((opt = getopt(argc, argv, "c:l")) != -1)
2021-08-03 19:56:31 +02:00
{
switch(opt)
{
case 'c':
emu->clock_offset_file = optarg;
break;
2021-11-08 17:06:17 +01:00
case 'l':
emu->enable_linter = 1;
break;
2021-08-03 19:56:31 +02:00
default: /* '?' */
usage(argc, argv);
}
}
if(optind >= argc)
{
err("missing tracedir\n");
usage(argc, argv);
}
emu->tracedir = argv[optind];
}
static void
set_clock_offsets(struct ovni_emu *emu, const char *host, size_t offset)
2021-08-03 19:56:31 +02:00
{
size_t matches = 0;
2021-08-03 19:56:31 +02:00
for(size_t i = 0; i < emu->trace.nlooms; i++)
2021-08-03 19:56:31 +02:00
{
struct ovni_loom *loom = &emu->trace.loom[i];
/* Match the hostname exactly */
if(strcmp(loom->hostname, host) != 0)
continue;
2021-08-03 19:56:31 +02:00
if(loom->clock_offset != 0)
die("loom %s already has a clock offset\n", loom->dname);
loom->clock_offset = offset;
matches++;
2021-08-03 19:56:31 +02:00
}
if(matches == 0)
die("no loom has hostname %s\n", host);
2021-08-03 19:56:31 +02:00
}
static void
load_clock_offsets(struct ovni_emu *emu)
{
FILE *f;
char buf[1024];
size_t i;
int rank, ret;
2021-09-28 12:22:56 +02:00
double offset, mean, std;
2021-08-10 10:16:41 +02:00
char host[OVNI_MAX_HOSTNAME];
2021-08-03 19:56:31 +02:00
struct ovni_loom *loom;
struct ovni_trace *trace;
struct ovni_stream *stream;
2021-08-03 19:56:31 +02:00
f = fopen(emu->clock_offset_file, "r");
if(f == NULL)
{
err("error opening clock offset file %s: %s\n",
emu->clock_offset_file,
strerror(errno));
exit(EXIT_FAILURE);
2021-08-03 19:56:31 +02:00
}
/* Ignore header line */
2021-09-28 19:21:22 +02:00
if(fgets(buf, 1024, f) == NULL)
{
perror("fgets failed");
exit(EXIT_FAILURE);
}
2021-08-03 19:56:31 +02:00
2021-09-28 12:22:56 +02:00
while(1)
2021-08-03 19:56:31 +02:00
{
2021-09-28 12:22:56 +02:00
errno = 0;
ret = fscanf(f, "%d %s %lf %lf %lf", &rank, host, &offset, &mean, &std);
if(ret == EOF)
{
if(errno != 0)
{
perror("fscanf failed");
exit(EXIT_FAILURE);
}
break;
}
if(ret != 5)
{
err("fscanf read %d instead of 5 fields in %s\n",
ret, emu->clock_offset_file);
exit(EXIT_FAILURE);
}
set_clock_offsets(emu, host, (int64_t) offset);
2021-08-03 19:56:31 +02:00
}
2021-07-28 11:56:35 +02:00
2021-08-03 19:56:31 +02:00
/* Then populate the stream offsets */
trace = &emu->trace;
for(i=0; i<trace->nstreams; i++)
2021-07-28 11:56:35 +02:00
{
2021-08-03 19:56:31 +02:00
stream = &trace->stream[i];
loom = stream->loom;
2021-08-03 19:56:31 +02:00
stream->clock_offset = loom->clock_offset;
2021-07-28 11:56:35 +02:00
}
2021-08-03 19:56:31 +02:00
fclose(f);
err("loaded clock offsets ok\n");
2021-08-03 19:56:31 +02:00
}
2021-07-28 11:56:35 +02:00
2021-08-03 20:47:02 +02:00
static void
write_row_cpu(struct ovni_emu *emu)
{
FILE *f;
size_t i;
2021-08-03 20:47:02 +02:00
char path[PATH_MAX];
struct ovni_cpu *cpu;
sprintf(path, "%s/%s", emu->tracedir, "cpu.row");
f = fopen(path, "w");
if(f == NULL)
{
perror("cannot open row file");
exit(EXIT_FAILURE);
}
fprintf(f, "LEVEL NODE SIZE 1\n");
fprintf(f, "hostname\n");
fprintf(f, "\n");
fprintf(f, "LEVEL THREAD SIZE %ld\n", emu->total_ncpus);
2021-08-03 20:47:02 +02:00
2021-10-11 11:12:26 +02:00
for(i=0; i<emu->total_ncpus; i++)
2021-08-03 20:47:02 +02:00
{
2021-10-11 11:12:26 +02:00
cpu = emu->global_cpu[i];
fprintf(f, "%s\n", cpu->name);
2021-08-03 20:47:02 +02:00
}
fclose(f);
}
2021-10-21 16:36:07 +02:00
static void
write_row_thread(struct ovni_emu *emu)
{
FILE *f;
size_t i;
2021-10-21 16:36:07 +02:00
char path[PATH_MAX];
struct ovni_ethread *th;
sprintf(path, "%s/%s", emu->tracedir, "thread.row");
f = fopen(path, "w");
if(f == NULL)
{
perror("cannot open row file");
exit(EXIT_FAILURE);
}
fprintf(f, "LEVEL NODE SIZE 1\n");
fprintf(f, "hostname\n");
fprintf(f, "\n");
fprintf(f, "LEVEL THREAD SIZE %ld\n", emu->total_nthreads);
2021-10-21 16:36:07 +02:00
for(i=0; i<emu->total_nthreads; i++)
{
th = emu->global_thread[i];
fprintf(f, "THREAD %d.%d\n", th->proc->appid, th->tid);
}
fclose(f);
}
2021-10-21 16:15:29 +02:00
static void
init_threads(struct ovni_emu *emu)
{
2021-10-21 16:34:04 +02:00
struct ovni_trace *trace;
2021-10-21 16:15:29 +02:00
struct ovni_loom *loom;
struct ovni_eproc *proc;
struct ovni_ethread *thread;
size_t i, j, k, gi;
2021-10-21 16:34:04 +02:00
emu->total_nthreads = 0;
emu->total_nprocs = 0;
2021-10-21 16:34:04 +02:00
trace = &emu->trace;
2021-10-21 16:15:29 +02:00
for(i=0; i<trace->nlooms; i++)
{
loom = &trace->loom[i];
for(j=0; j<loom->nprocs; j++)
{
proc = &loom->proc[j];
emu->total_nprocs++;
2021-10-21 16:15:29 +02:00
for(k=0; k<proc->nthreads; k++)
{
thread = &proc->thread[k];
2021-10-21 16:34:04 +02:00
emu->total_nthreads++;
2021-10-21 16:15:29 +02:00
}
}
}
2021-10-21 16:34:04 +02:00
emu->global_thread = calloc(emu->total_nthreads,
sizeof(*emu->global_thread));
if(emu->global_thread == NULL)
{
perror("calloc failed");
exit(EXIT_FAILURE);
2021-10-21 16:34:04 +02:00
}
for(gi=0, i=0; i<trace->nlooms; i++)
{
loom = &trace->loom[i];
for(j=0; j<loom->nprocs; j++)
{
proc = &loom->proc[j];
for(k=0; k<proc->nthreads; k++)
{
thread = &proc->thread[k];
emu->global_thread[gi++] = thread;
}
}
}
}
static void
init_cpus(struct ovni_emu *emu)
{
struct ovni_trace *trace;
struct ovni_loom *loom;
struct ovni_cpu *cpu;
size_t i, j;
2021-10-21 16:34:04 +02:00
trace = &emu->trace;
emu->global_cpu = calloc(emu->total_ncpus,
sizeof(*emu->global_cpu));
if(emu->global_cpu == NULL)
{
perror("calloc");
exit(EXIT_FAILURE);
2021-10-21 16:34:04 +02:00
}
for(i=0; i<trace->nlooms; i++)
2021-10-21 16:34:04 +02:00
{
loom = &trace->loom[i];
for(j=0; j<loom->ncpus; j++)
{
cpu = &loom->cpu[j];
emu->global_cpu[cpu->gindex] = cpu;
2021-10-11 11:12:26 +02:00
if(snprintf(cpu->name, MAX_CPU_NAME, "CPU %ld.%ld",
2021-10-11 11:12:26 +02:00
i, j) >= MAX_CPU_NAME)
{
err("error cpu %ld.%ld name too long\n", i, j);
exit(EXIT_FAILURE);
2021-10-11 11:12:26 +02:00
}
2021-11-29 11:02:54 +01:00
cpu->virtual = 0;
2021-10-21 16:34:04 +02:00
}
emu->global_cpu[loom->vcpu.gindex] = &loom->vcpu;
if(snprintf(loom->vcpu.name, MAX_CPU_NAME, "CPU %ld.*",
2021-10-11 11:12:26 +02:00
i) >= MAX_CPU_NAME)
{
err("error cpu %ld.* name too long\n", i);
exit(EXIT_FAILURE);
2021-10-11 11:12:26 +02:00
}
2021-11-29 11:02:54 +01:00
loom->vcpu.virtual = 1;
2021-10-21 16:34:04 +02:00
}
2021-10-21 16:15:29 +02:00
}
2022-05-31 20:39:57 +02:00
static void
create_pcf_cpus(struct ovni_emu *emu)
{
/* Only needed for the thread PCF */
struct pcf_file *pcf = &emu->pcf[CHAN_TH];
2022-07-01 17:54:18 +02:00
int prvtype = chan_to_prvtype[CHAN_OVNI_CPU];
2022-05-31 20:39:57 +02:00
struct pcf_type *type = pcf_find_type(pcf, prvtype);
2022-07-01 17:54:18 +02:00
if(type == NULL)
2022-05-31 20:39:57 +02:00
die("cannot find PCF type for CHAN_OVNI_CPU\n");
for(size_t i=0; i<emu->total_ncpus; i++)
{
int value = i + 1;
char *label = emu->global_cpu[i]->name;
pcf_add_value(type, value, label);
}
}
2021-08-03 19:56:31 +02:00
static void
emu_init(struct ovni_emu *emu, int argc, char *argv[])
{
2021-08-10 12:08:33 +02:00
memset(emu, 0, sizeof(*emu));
2021-07-28 11:56:35 +02:00
2021-08-03 19:56:31 +02:00
parse_args(emu, argc, argv);
if(ovni_load_trace(&emu->trace, emu->tracedir))
{
err("error loading ovni trace\n");
exit(EXIT_FAILURE);
}
2021-09-27 17:42:14 +02:00
2021-08-03 19:56:31 +02:00
if(ovni_load_streams(&emu->trace))
{
err("error loading streams\n");
exit(EXIT_FAILURE);
}
2021-07-28 11:56:35 +02:00
load_metadata(emu);
2021-08-03 19:56:31 +02:00
if(emu->clock_offset_file != NULL)
load_clock_offsets(emu);
2021-10-21 16:34:04 +02:00
init_threads(emu);
init_cpus(emu);
2021-08-03 19:56:31 +02:00
open_prvs(emu, emu->tracedir);
2021-08-03 20:47:02 +02:00
open_pcfs(emu, emu->tracedir);
2021-10-21 16:15:29 +02:00
2022-05-31 20:39:57 +02:00
create_pcf_cpus(emu);
emu->global_size = 0;
emu->global_offset = 0;
err("loaded %ld cpus and %ld threads\n",
2021-10-21 16:34:04 +02:00
emu->total_ncpus,
emu->total_nthreads);
2021-08-03 20:47:02 +02:00
}
static void
emu_post(struct ovni_emu *emu)
{
/* Write the PCF files */
2022-05-31 20:39:57 +02:00
pcf_write(&emu->pcf[CHAN_TH]);
pcf_write(&emu->pcf[CHAN_CPU]);
2021-08-03 20:47:02 +02:00
write_row_cpu(emu);
2021-10-21 16:36:07 +02:00
write_row_thread(emu);
2021-08-03 19:56:31 +02:00
}
2021-07-29 17:46:25 +02:00
2021-08-03 19:56:31 +02:00
static void
emu_destroy(struct ovni_emu *emu)
{
fix_prv_headers(emu);
2021-08-03 19:56:31 +02:00
close_prvs(emu);
2021-09-27 15:14:27 +02:00
close_pcfs(emu);
2021-08-03 19:56:31 +02:00
destroy_metadata(emu);
ovni_free_streams(&emu->trace);
2021-11-18 14:53:15 +01:00
ovni_free_trace(&emu->trace);
free(emu->global_cpu);
free(emu->global_thread);
2021-08-03 19:56:31 +02:00
}
2021-07-28 11:56:35 +02:00
void
edie(struct ovni_emu *emu, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
fprintf(stderr, "fatal: ");
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "fatal: while evaluating the event %c%c%c with clock=%ld in thread=%d\n",
emu->cur_ev->header.model,
emu->cur_ev->header.category,
emu->cur_ev->header.value,
emu->cur_ev->header.clock,
emu->cur_thread->tid);
abort();
}
void
eerr(struct ovni_emu *emu, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
fprintf(stderr, "fatal: ");
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "fatal: while evaluating the event %c%c%c with clock=%ld in thread=%d\n",
emu->cur_ev->header.model,
emu->cur_ev->header.category,
emu->cur_ev->header.value,
emu->cur_ev->header.clock,
emu->cur_thread->tid);
}
2021-08-03 19:56:31 +02:00
int
main(int argc, char *argv[])
{
2021-09-21 19:17:58 +02:00
struct ovni_emu *emu;
2021-09-21 19:17:58 +02:00
emu = malloc(sizeof(struct ovni_emu));
if(emu == NULL)
{
perror("malloc");
return 1;
}
2021-08-10 10:18:12 +02:00
emu_init(emu, argc, argv);
2021-08-10 10:18:12 +02:00
emulate(emu);
2021-07-28 11:56:35 +02:00
2021-08-10 10:18:12 +02:00
emu_post(emu);
2021-08-03 20:47:02 +02:00
2021-08-10 10:18:12 +02:00
emu_destroy(emu);
free(emu);
2022-08-22 16:01:38 +02:00
err("ovniemu finished\n");
2021-07-28 11:56:35 +02:00
return 0;
}