ovni/dump.c

326 lines
5.2 KiB
C
Raw Normal View History

2021-07-19 15:11:41 +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>
2021-07-19 19:05:26 +02:00
#include <dirent.h>
2021-07-19 15:11:41 +02:00
#include "def.h"
2021-07-19 19:05:26 +02:00
int load_proc(struct eproc *proc, char *procdir)
2021-07-19 15:11:41 +02:00
{
2021-07-19 19:05:26 +02:00
struct dirent *dirent;
DIR *dir;
2021-07-19 15:11:41 +02:00
char path[PATH_MAX];
2021-07-19 19:05:26 +02:00
char *p;
struct ethread *thread;
2021-07-19 15:11:41 +02:00
2021-07-19 19:05:26 +02:00
if((dir = opendir(procdir)) == NULL)
2021-07-19 15:11:41 +02:00
{
2021-07-19 19:05:26 +02:00
fprintf(stderr, "opendir %s failed: %s\n",
procdir, strerror(errno));
return -1;
}
2021-07-19 15:11:41 +02:00
2021-07-19 19:05:26 +02:00
while((dirent = readdir(dir)) != NULL)
{
if(dirent->d_name[0] != 't')
continue;
p = strchr(dirent->d_name, '.');
if(p == NULL)
continue;
p++;
if(*p == '\0')
{
fprintf(stderr, "bad thread stream file: %s\n",
dirent->d_name);
return -1;
}
2021-07-19 15:11:41 +02:00
2021-07-19 19:05:26 +02:00
thread = &proc->thread[proc->nthreads++];
thread->tid = atoi(p);
sprintf(path, "%s/%s", procdir, dirent->d_name);
thread->f = fopen(path, "r");
if(thread->f == NULL)
{
fprintf(stderr, "fopen %s failed: %s\n",
path, strerror(errno));
return -1;
}
2021-07-19 15:11:41 +02:00
}
2021-07-19 19:05:26 +02:00
closedir(dir);
2021-07-19 15:11:41 +02:00
return 0;
}
2021-07-19 19:05:26 +02:00
int load_loom(struct loom *loom, char *loomdir)
2021-07-19 15:11:41 +02:00
{
int proc;
char path[PATH_MAX];
struct stat st;
for(proc=0; proc<MAX_PROC; proc++)
{
sprintf(path, "%s/proc.%d", loomdir, proc);
if(stat(path, &st) != 0)
{
2021-07-19 19:05:26 +02:00
/* No more proc.N directories */
2021-07-19 15:11:41 +02:00
if(errno == ENOENT)
break;
fprintf(stderr, "cannot stat %s: %s\n", path,
strerror(errno));
return -1;
}
if(!S_ISDIR(st.st_mode))
{
fprintf(stderr, "not a dir %s\n", path);
return -1;
}
if(load_proc(&loom->proc[proc], path))
return -1;
}
loom->nprocs = proc;
return 0;
}
2021-07-19 19:05:26 +02:00
int load_trace(struct trace *trace, char *tracedir)
2021-07-19 15:11:41 +02:00
{
int loom, nlooms;
char path[PATH_MAX];
/* TODO: For now only one loom */
nlooms = 1;
loom = 0;
sprintf(path, "%s/loom.%d", tracedir, loom);
if(load_loom(&trace->loom[loom], path))
return -1;
trace->nlooms = nlooms;
return 0;
}
/* Populates the streams in a single array */
2021-07-19 19:05:26 +02:00
int load_streams(struct trace *trace)
2021-07-19 15:11:41 +02:00
{
int i, j, k, s;
2021-07-19 19:05:26 +02:00
struct loom *loom;
struct eproc *proc;
struct ethread *thread;
2021-07-19 19:05:26 +02:00
struct stream *stream;
2021-07-19 15:11:41 +02:00
trace->nstreams = 0;
for(i=0; i<trace->nlooms; i++)
{
loom = &trace->loom[i];
for(j=0; j<loom->nprocs; j++)
{
proc = &loom->proc[j];
2021-07-19 19:05:26 +02:00
for(k=0; k<proc->nthreads; k++)
2021-07-19 15:11:41 +02:00
{
trace->nstreams++;
}
}
}
2021-07-19 19:05:26 +02:00
trace->stream = calloc(trace->nstreams, sizeof(struct stream));
2021-07-19 15:11:41 +02:00
if(trace->stream == NULL)
{
perror("calloc");
return -1;
}
fprintf(stderr, "loaded %d streams\n", trace->nstreams);
for(s=0, i=0; i<trace->nlooms; i++)
{
loom = &trace->loom[i];
for(j=0; j<loom->nprocs; j++)
{
proc = &loom->proc[j];
2021-07-19 19:05:26 +02:00
for(k=0; k<proc->nthreads; k++)
2021-07-19 15:11:41 +02:00
{
thread = &proc->thread[k];
2021-07-19 15:11:41 +02:00
stream = &trace->stream[s++];
stream->f = thread->f;
stream->tid = thread->tid;
stream->proc = j;
stream->loom = i;
2021-07-19 15:11:41 +02:00
stream->active = 1;
stream->lastclock = 0;
2021-07-19 15:11:41 +02:00
}
}
}
return 0;
}
2021-07-19 19:05:26 +02:00
int load_first_event(struct trace *trace)
2021-07-19 15:11:41 +02:00
{
int i;
2021-07-19 19:05:26 +02:00
struct stream *stream;
struct event *ev;
2021-07-19 15:11:41 +02:00
for(i=0; i<trace->nstreams; i++)
{
stream = &trace->stream[i];
ev = &stream->last;
if(fread(ev, sizeof(*ev), 1, stream->f) != 1)
{
fprintf(stderr, "failed to read and event\n");
return -1;
}
fprintf(stderr, "ev clock %u\n", ev->clock);
}
return 0;
}
void emit(struct stream *stream, struct event *ev)
2021-07-19 15:11:41 +02:00
{
int64_t delta;
2021-07-19 15:11:41 +02:00
delta = ev->clock - stream->lastclock;
2021-07-19 15:11:41 +02:00
2021-07-22 12:35:38 +02:00
printf("%d.%d.%d %c %c % 6u % 6u % 20lu % 15ld\n",
stream->loom, stream->proc, stream->tid,
ev->fsm, ev->event, ev->a, ev->b, ev->clock, delta);
2021-07-19 15:11:41 +02:00
stream->lastclock = ev->clock;
2021-07-19 15:11:41 +02:00
}
2021-07-19 19:05:26 +02:00
void load_next_event(struct stream *stream)
2021-07-19 15:11:41 +02:00
{
int i;
size_t n;
2021-07-19 19:05:26 +02:00
struct event *ev;
2021-07-19 15:11:41 +02:00
if(!stream->active)
return;
ev = &stream->last;
if((n = fread(ev, sizeof(*ev), 1, stream->f)) != 1)
{
//fprintf(stderr, "failed to read an event, n=%ld\n", n);
stream->active = 0;
return;
}
stream->active = 1;
}
2021-07-19 19:05:26 +02:00
void dump_events(struct trace *trace)
2021-07-19 15:11:41 +02:00
{
int i, f;
uint64_t minclock, lastclock;
2021-07-19 19:05:26 +02:00
struct event *ev;
struct stream *stream;
2021-07-19 15:11:41 +02:00
/* Load events */
for(i=0; i<trace->nstreams; i++)
{
stream = &trace->stream[i];
load_next_event(stream);
}
lastclock = 0;
while(1)
{
f = -1;
minclock = 0;
/* Select next event based on the clock */
for(i=0; i<trace->nstreams; i++)
{
stream = &trace->stream[i];
if(!stream->active)
continue;
ev = &stream->last;
if(f < 0 || ev->clock < minclock)
{
f = i;
minclock = ev->clock;
}
}
//fprintf(stderr, "f=%d minclock=%u\n", f, minclock);
if(f < 0)
break;
stream = &trace->stream[f];
2021-07-22 12:35:38 +02:00
if(lastclock > stream->last.clock)
2021-07-19 15:11:41 +02:00
{
2021-07-22 12:35:38 +02:00
fprintf(stdout, "warning: backwards jump in time %lu -> %lu\n",
lastclock, stream->last.clock);
2021-07-19 15:11:41 +02:00
}
/* Emit current event */
emit(stream, &stream->last);
2021-07-19 15:11:41 +02:00
lastclock = stream->last.clock;
/* Read the next one */
load_next_event(stream);
/* Unset the index */
f = -1;
minclock = 0;
}
}
2021-07-19 19:05:26 +02:00
void free_streams(struct trace *trace)
2021-07-19 15:11:41 +02:00
{
free(trace->stream);
}
int main(int argc, char *argv[])
{
char *tracedir;
2021-07-19 19:05:26 +02:00
struct trace trace;
2021-07-19 15:11:41 +02:00
if(argc != 2)
{
fprintf(stderr, "missing tracedir\n");
exit(EXIT_FAILURE);
}
tracedir = argv[1];
if(load_trace(&trace, tracedir))
return 1;
if(load_streams(&trace))
return 1;
dump_events(&trace);
free_streams(&trace);
return 0;
}