ovni/ovni.c

278 lines
4.6 KiB
C
Raw Normal View History

#define _GNU_SOURCE
#include <unistd.h>
2021-07-19 15:11:41 +02:00
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.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 <assert.h>
2021-07-22 12:35:02 +02:00
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
2021-07-19 15:11:41 +02:00
#include "ovni.h"
#include "def.h"
2021-07-22 12:35:02 +02:00
//#define ENABLE_SLOW_CHECKS
2021-07-19 15:11:41 +02:00
/* Data per process */
2021-07-19 19:05:26 +02:00
struct rproc rproc = {0};
2021-07-19 15:11:41 +02:00
/* Data per thread */
2021-07-19 19:05:26 +02:00
_Thread_local struct rthread rthread = {0};
2021-07-19 15:11:41 +02:00
static int
create_trace_dirs(char *tracedir, int loom, int proc)
{
char path[PATH_MAX];
fprintf(stderr, "create trace dirs for loom=%d, proc=%d\n",
loom, proc);
snprintf(path, PATH_MAX, "%s", tracedir);
if(mkdir(path, 0755))
{
fprintf(stderr, "mkdir %s: %s\n", path, strerror(errno));
//return -1;
}
snprintf(path, PATH_MAX, "%s/loom.%d", tracedir, loom);
if(mkdir(path, 0755))
{
fprintf(stderr, "mkdir %s: %s\n", path, strerror(errno));
//return -1;
}
2021-07-19 19:05:26 +02:00
snprintf(rproc.dir, PATH_MAX, "%s/loom.%d/proc.%d", tracedir, loom, proc);
2021-07-19 15:11:41 +02:00
2021-07-19 19:05:26 +02:00
if(mkdir(rproc.dir, 0755))
2021-07-19 15:11:41 +02:00
{
2021-07-19 19:05:26 +02:00
fprintf(stderr, "mkdir %s: %s\n", rproc.dir, strerror(errno));
2021-07-19 15:11:41 +02:00
return -1;
}
return 0;
}
static int
2021-07-19 19:05:26 +02:00
create_trace_stream()
2021-07-19 15:11:41 +02:00
{
2021-07-22 12:35:02 +02:00
int fd;
2021-07-19 15:11:41 +02:00
char path[PATH_MAX];
fprintf(stderr, "create thread stream tid=%d gettid=%d rproc.proc=%d rproc.ready=%d\n",
rthread.tid, gettid(), rproc.proc, rproc.ready);
2021-07-19 19:05:26 +02:00
snprintf(path, PATH_MAX, "%s/thread.%d", rproc.dir, rthread.tid);
2021-07-22 12:35:02 +02:00
//rthread.streamfd = open(path, O_WRONLY | O_CREAT | O_DSYNC, 0644);
rthread.streamfd = open(path, O_WRONLY | O_CREAT, 0644);
if(rthread.streamfd == -1)
2021-07-19 15:11:41 +02:00
{
2021-07-22 12:35:02 +02:00
fprintf(stderr, "open %s failed: %s\n", path, strerror(errno));
/* Shall we just return -1 ? */
abort();
2021-07-22 12:35:02 +02:00
//return -1;
2021-07-19 15:11:41 +02:00
}
return 0;
}
int
ovni_proc_init(int loom, int proc)
2021-07-19 15:11:41 +02:00
{
int i;
assert(rproc.ready == 0);
2021-07-19 19:05:26 +02:00
memset(&rproc, 0, sizeof(rproc));
2021-07-19 15:11:41 +02:00
2021-07-19 19:05:26 +02:00
rproc.loom = loom;
rproc.proc = proc;
2021-07-19 15:11:41 +02:00
/* By default we use the monotonic clock */
2021-07-19 19:05:26 +02:00
rproc.clockid = CLOCK_MONOTONIC;
2021-07-19 15:11:41 +02:00
if(create_trace_dirs(TRACEDIR, loom, proc))
abort();
rproc.ready = 1;
2021-07-19 15:11:41 +02:00
2021-07-19 19:05:26 +02:00
return 0;
}
int
ovni_thread_init(pid_t tid)
{
int i;
assert(tid != 0);
if(rthread.ready)
{
fprintf(stderr, "warning: thread tid=%d already initialized\n",
tid);
return 0;
}
assert(rthread.ready == 0);
assert(rthread.tid == 0);
assert(rthread.cpu == 0);
assert(rproc.ready == 1);
fprintf(stderr, "ovni thread init tid=%d\n", tid);
2021-07-19 19:05:26 +02:00
memset(&rthread, 0, sizeof(rthread));
rthread.tid = tid;
rthread.cpu = -666;
2021-07-22 12:35:02 +02:00
rthread.nevents = 0;
2021-07-19 19:05:26 +02:00
if(create_trace_stream(tid))
abort();
rthread.ready = 1;
2021-07-19 15:11:41 +02:00
return 0;
}
int
ovni_thread_isready()
{
return rthread.ready;
}
2021-07-19 15:11:41 +02:00
void
ovni_cpu_set(int cpu)
{
2021-07-19 19:05:26 +02:00
rthread.cpu = cpu;
2021-07-19 15:11:41 +02:00
}
/* Sets the current time so that all subsequent events have the new
* timestamp */
2021-07-22 12:35:02 +02:00
void
2021-07-19 15:11:41 +02:00
ovni_clock_update()
{
struct timespec tp;
uint64_t ns = 1000LL * 1000LL * 1000LL;
uint64_t raw;
2021-07-22 12:35:02 +02:00
int ret;
2021-07-19 15:11:41 +02:00
2021-07-22 12:35:02 +02:00
ret = clock_gettime(rproc.clockid, &tp);
#ifdef ENABLE_SLOW_CHECKS
if(ret) abort();
#endif
2021-07-19 15:11:41 +02:00
raw = tp.tv_sec * ns + tp.tv_nsec;
//raw = raw >> 6;
2021-07-19 19:05:26 +02:00
rthread.clockvalue = (uint64_t) raw;
2021-07-19 15:11:41 +02:00
}
static void
2021-07-19 19:05:26 +02:00
hexdump(uint8_t *buf, size_t size)
2021-07-19 15:11:41 +02:00
{
int i, j;
2021-07-19 19:05:26 +02:00
//printf("writing %ld bytes in cpu=%d\n", size, rthread.cpu);
2021-07-19 15:11:41 +02:00
for(i=0; i<size; i+=16)
{
for(j=0; j<16 && i+j < size; j++)
{
printf("%02x ", buf[i+j]);
}
printf("\n");
}
2021-07-19 19:05:26 +02:00
}
2021-07-19 15:11:41 +02:00
2021-07-19 19:05:26 +02:00
static int
ovni_write(uint8_t *buf, size_t size)
{
2021-07-22 12:35:02 +02:00
ssize_t written;
2021-07-22 12:35:02 +02:00
do
2021-07-19 15:11:41 +02:00
{
2021-07-22 12:35:02 +02:00
written = write(rthread.streamfd, buf, size);
if(written < 0)
{
perror("write");
return -1;
}
2021-07-19 15:11:41 +02:00
2021-07-22 12:35:02 +02:00
size -= written;
buf += written;
} while(size > 0);
2021-07-19 15:11:41 +02:00
return 0;
}
2021-07-22 12:35:02 +02:00
int
ovni_thread_flush()
{
int ret = 0;
struct event pre={0}, post={0};
assert(rthread.ready);
assert(rproc.ready);
ovni_clock_update();
pre.clock = rthread.clockvalue;
pre.fsm = 'F';
pre.event = '[';
ret = ovni_write((uint8_t *) rthread.events, rthread.nevents * sizeof(struct event));
rthread.nevents = 0;
ovni_clock_update();
post.clock = rthread.clockvalue;
post.fsm = 'F';
post.event = ']';
/* Also emit the two flush events */
memcpy(&rthread.events[rthread.nevents++], &pre, sizeof(struct event));;
memcpy(&rthread.events[rthread.nevents++], &post, sizeof(struct event));;
return ret;
}
2021-07-19 15:11:41 +02:00
static int
ovni_ev(uint8_t fsm, uint8_t event, uint16_t a, uint16_t b)
2021-07-19 15:11:41 +02:00
{
2021-07-22 12:35:02 +02:00
struct event *ev;
int ret = 0;
ev = &rthread.events[rthread.nevents++];
2021-07-19 15:11:41 +02:00
2021-07-22 12:35:02 +02:00
ev->clock = rthread.clockvalue;
ev->fsm = fsm;
ev->event = event;
ev->a = a;
ev->b = b;
2021-07-19 15:11:41 +02:00
2021-07-22 12:35:02 +02:00
/* Flush */
if(rthread.nevents >= MAX_EV)
ret = ovni_thread_flush();
return ret;
2021-07-19 15:11:41 +02:00
}
int
ovni_thread_ev(uint8_t fsm, uint8_t event, uint16_t a, uint16_t b)
2021-07-19 15:11:41 +02:00
{
assert(rthread.ready);
assert(rproc.ready);
return ovni_ev(fsm, event, a, b);
2021-07-19 15:11:41 +02:00
}
2021-07-22 12:35:02 +02:00