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-19 15:11:41 +02:00
|
|
|
|
|
|
|
#include "ovni.h"
|
|
|
|
#include "def.h"
|
|
|
|
|
|
|
|
/* 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
|
|
|
{
|
|
|
|
char path[PATH_MAX];
|
|
|
|
|
2021-07-19 19:05:26 +02:00
|
|
|
snprintf(path, PATH_MAX, "%s/thread.%d", rproc.dir, rthread.tid);
|
|
|
|
if((rthread.stream = fopen(path, "w")) == NULL)
|
2021-07-19 15:11:41 +02:00
|
|
|
{
|
2021-07-19 19:05:26 +02:00
|
|
|
fprintf(stderr, "fopen %s failed: %s\n", path, strerror(errno));
|
|
|
|
return -1;
|
2021-07-19 15:11:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2021-07-19 19:05:26 +02:00
|
|
|
ovni_proc_init(int loom, int proc, int ncpus)
|
2021-07-19 15:11:41 +02:00
|
|
|
{
|
|
|
|
int i;
|
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;
|
|
|
|
rproc.ncpus = ncpus;
|
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))
|
|
|
|
return -1;
|
|
|
|
|
2021-07-19 19:05:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ovni_thread_init(pid_t tid)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
assert(tid != 0);
|
|
|
|
|
|
|
|
memset(&rthread, 0, sizeof(rthread));
|
|
|
|
|
|
|
|
rthread.tid = tid;
|
|
|
|
rthread.cpu = -1;
|
|
|
|
rthread.state = ST_THREAD_INIT;
|
|
|
|
|
|
|
|
if(create_trace_stream(tid))
|
2021-07-19 15:11:41 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 */
|
|
|
|
int
|
|
|
|
ovni_clock_update()
|
|
|
|
{
|
|
|
|
struct timespec tp;
|
|
|
|
uint64_t ns = 1000LL * 1000LL * 1000LL;
|
|
|
|
uint64_t raw;
|
|
|
|
|
2021-07-19 19:05:26 +02:00
|
|
|
if(clock_gettime(rproc.clockid, &tp))
|
2021-07-19 15:11:41 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "writing %ld bytes in thread.%d\n", size, rthread.tid);
|
|
|
|
if(fwrite(buf, 1, size, rthread.stream) != size)
|
2021-07-19 15:11:41 +02:00
|
|
|
{
|
|
|
|
perror("fwrite");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-07-19 19:05:26 +02:00
|
|
|
fflush(rthread.stream);
|
2021-07-19 15:11:41 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ovni_ev(uint8_t fsm, uint8_t event, int32_t data)
|
|
|
|
{
|
2021-07-19 19:05:26 +02:00
|
|
|
struct event ev;
|
2021-07-19 15:11:41 +02:00
|
|
|
|
2021-07-19 19:05:26 +02:00
|
|
|
ev.clock = rthread.clockvalue;
|
2021-07-19 15:11:41 +02:00
|
|
|
ev.fsm = fsm;
|
|
|
|
ev.event = event;
|
|
|
|
ev.data = data;
|
|
|
|
|
|
|
|
return ovni_write((uint8_t *) &ev, sizeof(ev));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ovni_ev_worker(uint8_t fsm, uint8_t event, int32_t data)
|
|
|
|
{
|
2021-07-19 19:05:26 +02:00
|
|
|
assert(rthread.state == ST_THREAD_INIT);
|
2021-07-19 15:11:41 +02:00
|
|
|
return ovni_ev(fsm, event, data);
|
|
|
|
}
|