ovni/ovni.c

757 lines
13 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>
2021-07-30 20:08:40 +02:00
#include <sys/mman.h>
2021-07-19 15:11:41 +02:00
#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>
#include <dirent.h>
2021-07-19 15:11:41 +02:00
#include "ovni.h"
2021-07-28 11:56:35 +02:00
#include "ovni_trace.h"
#define ENABLE_DEBUG
#ifdef ENABLE_DEBUG
#define dbg(...) fprintf(stderr, __VA_ARGS__);
#else
#define dbg(...)
#endif
#define err(...) fprintf(stderr, __VA_ARGS__);
2021-07-19 15:11:41 +02:00
2021-07-22 12:35:02 +02:00
//#define ENABLE_SLOW_CHECKS
//#define USE_RDTSC
2021-07-19 15:11:41 +02:00
/* Data per process */
struct ovni_rproc rproc = {0};
2021-07-19 15:11:41 +02:00
/* Data per thread */
_Thread_local struct ovni_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(OVNI_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;
rthread.evlen = 0;
rthread.evbuf = malloc(OVNI_MAX_EV_BUF);
if(rthread.evbuf == NULL)
{
perror("malloc");
abort();
}
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_free()
{
assert(rthread.ready);
free(rthread.evbuf);
}
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
}
static inline
uint64_t rdtsc(void)
{
uint32_t lo, hi;
// RDTSC copies contents of 64-bit TSC into EDX:EAX
asm volatile("rdtsc" : "=a" (lo), "=d" (hi));
return (uint64_t) hi << 32 | lo;
}
uint64_t
ovni_get_clock()
2021-07-19 15:11:41 +02:00
{
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
#ifdef USE_RDTSC
raw = rdtsc();
#else
2021-07-22 12:35:02 +02:00
ret = clock_gettime(rproc.clockid, &tp);
#ifdef ENABLE_SLOW_CHECKS
if(ret) abort();
#endif /* ENABLE_SLOW_CHECKS */
2021-07-19 15:11:41 +02:00
raw = tp.tv_sec * ns + tp.tv_nsec;
2021-07-19 19:05:26 +02:00
rthread.clockvalue = (uint64_t) raw;
#endif /* USE_RDTSC */
return raw;
}
/* Sets the current time so that all subsequent events have the new
* timestamp */
void
ovni_clock_update()
{
rthread.clockvalue = ovni_get_clock();
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++)
{
2021-07-28 11:56:35 +02:00
dbg("%02x ", buf[i+j]);
2021-07-19 15:11:41 +02:00
}
2021-07-28 11:56:35 +02:00
dbg("\n");
2021-07-19 15:11:41 +02:00
}
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;
}
static int
flush_evbuf()
{
int ret;
assert(rthread.ready);
assert(rproc.ready);
ret = ovni_write(rthread.evbuf, rthread.evlen);
rthread.evlen = 0;
return ret;
}
static void
ovni_ev_set_clock(struct ovni_ev *ev)
{
2021-07-30 20:08:40 +02:00
ev->header.clock_lo = (uint32_t) (rthread.clockvalue & 0xffffffff);
ev->header.clock_hi = (uint16_t) ((rthread.clockvalue >> 32) & 0xffff);
}
uint64_t
ovni_ev_get_clock(struct ovni_ev *ev)
{
uint64_t clock;
2021-07-30 20:08:40 +02:00
clock = ((uint64_t) ev->header.clock_hi) << 32 | ((uint64_t) ev->header.clock_lo);
return clock;
}
void
ovni_ev_set_mcv(struct ovni_ev *ev, char *mcv)
{
2021-07-30 20:08:40 +02:00
ev->header.model = mcv[0];
ev->header.class = mcv[1];
ev->header.value = mcv[2];
}
static size_t
get_jumbo_payload_size(struct ovni_ev *ev)
{
return sizeof(ev->payload.jumbo.size) + ev->payload.jumbo.size;
}
2021-07-22 12:35:02 +02:00
int
ovni_payload_size(struct ovni_ev *ev)
{
2021-07-28 11:56:35 +02:00
int size;
2021-07-30 20:08:40 +02:00
if(ev->header.flags & OVNI_EV_JUMBO)
return get_jumbo_payload_size(ev);
size = ev->header.flags & 0x0f;
2021-07-28 11:56:35 +02:00
if(size == 0)
return 0;
/* The minimum size is 2 bytes, so we can encode a length of 16
* bytes using 4 bits (0x0f) */
size++;
return size;
}
void
ovni_payload_add(struct ovni_ev *ev, uint8_t *buf, int size)
{
2021-07-28 11:56:35 +02:00
int payload_size;
2021-07-30 20:08:40 +02:00
assert((ev->header.flags & OVNI_EV_JUMBO) == 0);
assert(size >= 2);
2021-07-28 11:56:35 +02:00
payload_size = ovni_payload_size(ev);
/* Ensure we have room */
2021-07-28 11:56:35 +02:00
assert(payload_size + size <= sizeof(ev->payload));
memcpy(&ev->payload.u8[payload_size], buf, size);
payload_size += size;
2021-07-30 20:08:40 +02:00
ev->header.flags = ev->header.flags & 0xf0 | (payload_size-1) & 0x0f;
}
int
ovni_ev_size(struct ovni_ev *ev)
{
2021-07-30 20:08:40 +02:00
return sizeof(ev->header) + ovni_payload_size(ev);
}
static void
2021-07-30 20:08:40 +02:00
ovni_ev_add(struct ovni_ev *ev);
int
ovni_flush()
2021-07-22 12:35:02 +02:00
{
int ret = 0;
struct ovni_ev pre={0}, post={0};
2021-07-22 12:35:02 +02:00
assert(rthread.ready);
assert(rproc.ready);
ovni_clock_update();
ovni_ev_set_clock(&pre);
ovni_ev_set_mcv(&pre, "OF[");
2021-07-22 12:35:02 +02:00
ret = flush_evbuf();
2021-07-22 12:35:02 +02:00
ovni_clock_update();
ovni_ev_set_clock(&post);
ovni_ev_set_mcv(&post, "OF]");
2021-07-22 12:35:02 +02:00
/* Add the two flush events */
ovni_ev_add(&pre);
ovni_ev_add(&post);
2021-07-22 12:35:02 +02:00
return ret;
}
2021-07-30 20:08:40 +02:00
void
ovni_ev_add_jumbo(struct ovni_ev *ev, uint8_t *buf, uint32_t bufsize)
{
size_t evsize, totalsize;
assert(ovni_payload_size(ev) == 0);
ovni_payload_add(ev, (uint8_t *) &bufsize, sizeof(bufsize));
evsize = ovni_ev_size(ev);
totalsize = evsize + bufsize;
/* Check if the event fits or flush first otherwise */
if(rthread.evlen + totalsize >= OVNI_MAX_EV_BUF)
ovni_flush();
/* Se the jumbo flag here, so we capture the previous evsize
* properly, ignoring the jumbo buffer */
ev->header.flags |= OVNI_EV_JUMBO;
memcpy(&rthread.evbuf[rthread.evlen], ev, evsize);
rthread.evlen += evsize;
memcpy(&rthread.evbuf[rthread.evlen], buf, bufsize);
rthread.evlen += bufsize;
assert(rthread.evlen < OVNI_MAX_EV_BUF);
}
static void
ovni_ev_add(struct ovni_ev *ev)
{
int size;
size = ovni_ev_size(ev);
/* Check if the event fits or flush first otherwise */
if(rthread.evlen + size >= OVNI_MAX_EV_BUF)
ovni_flush();
memcpy(&rthread.evbuf[rthread.evlen], ev, size);
rthread.evlen += size;
}
void
ovni_ev_jumbo(struct ovni_ev *ev, uint8_t *buf, uint32_t bufsize)
{
ovni_ev_set_clock(ev);
ovni_ev_add_jumbo(ev, buf, bufsize);
}
void
ovni_ev(struct ovni_ev *ev)
{
ovni_ev_set_clock(ev);
ovni_ev_add(ev);
}
2021-07-30 21:37:25 +02:00
static int
find_dir_prefix(struct dirent *dirent, const char *prefix, int *num)
{
const char *p;
p = dirent->d_name;
/* Check the prefix */
if(strncmp(p, prefix, strlen(prefix)) != 0)
return -1;
p += strlen(prefix);
/* Find the dot */
if(*p != '.')
return -1;
p++;
/* Convert the end to a number */
*num = atoi(p);
return 0;
}
2021-07-30 20:08:40 +02:00
static int
2021-08-02 10:08:58 +02:00
load_thread(struct ovni_ethread *thread, struct ovni_eproc *proc, int index, int tid, char *filepath)
2021-07-30 20:08:40 +02:00
{
2021-08-02 10:08:58 +02:00
static int total_threads = 0;
2021-07-30 20:08:40 +02:00
thread->tid = tid;
2021-08-02 10:08:58 +02:00
thread->index = index;
thread->gindex = total_threads++;
thread->state = TH_ST_UNKNOWN;
thread->proc = proc;
2021-07-30 20:08:40 +02:00
thread->stream_fd = open(filepath, O_RDONLY);
if(thread->stream_fd == -1)
{
perror("open");
return -1;
}
return 0;
}
2021-07-19 15:11:41 +02:00
static int
2021-08-02 10:08:58 +02:00
load_proc(struct ovni_eproc *proc, int index, int pid, char *procdir)
2021-07-19 15:11:41 +02:00
{
2021-08-02 10:08:58 +02:00
static int total_procs = 0;
struct dirent *dirent;
DIR *dir;
char path[PATH_MAX];
struct ovni_ethread *thread;
2021-07-30 21:37:25 +02:00
int tid;
proc->pid = pid;
2021-08-02 10:08:58 +02:00
proc->index = index;
proc->gindex = total_procs++;
if((dir = opendir(procdir)) == NULL)
{
fprintf(stderr, "opendir %s failed: %s\n",
procdir, strerror(errno));
return -1;
}
2021-07-22 12:35:02 +02:00
while((dirent = readdir(dir)) != NULL)
{
2021-07-30 21:37:25 +02:00
if(find_dir_prefix(dirent, "thread", &tid) != 0)
{
2021-07-30 21:37:25 +02:00
err("warning: ignoring bogus directory entry %s\n",
dirent->d_name);
2021-07-30 21:37:25 +02:00
continue;
}
2021-07-19 15:11:41 +02:00
2021-07-30 20:08:40 +02:00
sprintf(path, "%s/%s", procdir, dirent->d_name);
2021-07-19 15:11:41 +02:00
2021-07-30 21:37:25 +02:00
if(proc->nthreads >= OVNI_MAX_THR)
{
err("too many thread streams for process %d\n", pid);
abort();
}
2021-08-02 10:08:58 +02:00
thread = &proc->thread[proc->nthreads];
2021-08-02 10:08:58 +02:00
if(load_thread(thread, proc, proc->nthreads, tid, path) != 0)
return -1;
2021-08-02 10:08:58 +02:00
proc->nthreads++;
}
closedir(dir);
return 0;
}
static int
2021-07-30 21:37:25 +02:00
load_loom(struct ovni_loom *loom, int loomid, char *loomdir)
{
2021-07-30 21:37:25 +02:00
int pid;
char path[PATH_MAX];
struct stat st;
2021-07-30 21:37:25 +02:00
DIR *dir;
struct dirent *dirent;
struct ovni_eproc *proc;
2021-07-30 21:37:25 +02:00
if((dir = opendir(loomdir)) == NULL)
{
2021-07-30 21:37:25 +02:00
fprintf(stderr, "opendir %s failed: %s\n",
loomdir, strerror(errno));
return -1;
}
2021-07-30 21:37:25 +02:00
while((dirent = readdir(dir)) != NULL)
{
if(find_dir_prefix(dirent, "proc", &pid) != 0)
{
2021-07-30 21:37:25 +02:00
err("warning: ignoring bogus directory entry %s\n",
dirent->d_name);
continue;
}
2021-07-30 21:37:25 +02:00
sprintf(path, "%s/%s", loomdir, dirent->d_name);
if(loom->nprocs >= OVNI_MAX_PROC)
{
2021-07-30 21:37:25 +02:00
err("too many process streams for loom %d\n",
loomid);
abort();
}
2021-08-02 10:08:58 +02:00
proc = &loom->proc[loom->nprocs];
2021-07-30 21:37:25 +02:00
2021-08-02 10:08:58 +02:00
if(load_proc(proc, loom->nprocs, pid, path) != 0)
return -1;
2021-08-02 10:08:58 +02:00
loom->nprocs++;
}
2021-07-30 21:37:25 +02:00
closedir(dir);
return 0;
2021-07-19 15:11:41 +02:00
}
int
ovni_load_trace(struct ovni_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);
2021-07-30 21:37:25 +02:00
if(load_loom(&trace->loom[loom], loom, path))
return -1;
trace->nlooms = nlooms;
return 0;
2021-07-19 15:11:41 +02:00
}
2021-07-22 12:35:02 +02:00
2021-07-30 20:08:40 +02:00
static int
load_stream_buf(struct ovni_stream *stream, struct ovni_ethread *thread)
{
struct stat st;
if(fstat(thread->stream_fd, &st) < 0)
{
perror("fstat");
return -1;
}
stream->size = st.st_size;
stream->buf = mmap(NULL, stream->size, PROT_READ, MAP_SHARED,
thread->stream_fd, 0);
if(stream->buf == MAP_FAILED)
{
perror("mmap");
return -1;
}
return 0;
}
/* Populates the streams in a single array */
int
ovni_load_streams(struct ovni_trace *trace)
{
int i, j, k, s;
struct ovni_loom *loom;
struct ovni_eproc *proc;
struct ovni_ethread *thread;
struct ovni_stream *stream;
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];
for(k=0; k<proc->nthreads; k++)
{
trace->nstreams++;
}
}
}
trace->stream = calloc(trace->nstreams, sizeof(struct ovni_stream));
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];
for(k=0; k<proc->nthreads; k++)
{
thread = &proc->thread[k];
stream = &trace->stream[s++];
2021-07-30 20:08:40 +02:00
if(load_stream_buf(stream, thread) != 0)
{
err("load_stream_buf failed\n");
return -1;
}
stream->tid = thread->tid;
stream->thread = k;
stream->proc = j;
stream->loom = i;
stream->active = 1;
stream->lastclock = 0;
2021-07-30 20:08:40 +02:00
stream->offset = 0;
stream->cur_ev = NULL;
}
}
}
return 0;
}
void
ovni_free_streams(struct ovni_trace *trace)
{
free(trace->stream);
}
2021-07-30 20:08:40 +02:00
int
ovni_load_next_event(struct ovni_stream *stream)
{
int i;
size_t n, size;
uint8_t flags;
2021-07-30 20:08:40 +02:00
if(stream->active == 0)
{
2021-07-30 20:08:40 +02:00
dbg("stream is inactive, cannot load more events\n");
return -1;
}
2021-07-30 20:08:40 +02:00
if(stream->cur_ev == NULL)
{
stream->cur_ev = (struct ovni_ev *) stream->buf;
stream->offset = 0;
dbg("first event\n");
goto out;
}
2021-07-30 20:08:40 +02:00
//printf("advancing offset %ld bytes\n", ovni_ev_size(stream->cur_ev));
stream->offset += ovni_ev_size(stream->cur_ev);
2021-07-30 20:08:40 +02:00
/* We have reached the end */
if(stream->offset == stream->size)
{
stream->active = 0;
2021-07-30 20:08:40 +02:00
dbg("stream runs out of events\n");
return -1;
}
2021-07-30 20:08:40 +02:00
/* It cannot overflow, otherwise we are reading garbage */
assert(stream->offset < stream->size);
stream->cur_ev = (struct ovni_ev *) &stream->buf[stream->offset];
out:
//dbg("---------\n");
//dbg("ev size = %d\n", ovni_ev_size(stream->cur_ev));
//dbg("ev flags = %02x\n", stream->cur_ev->header.flags);
//dbg("loaded next event:\n");
2021-07-30 20:08:40 +02:00
//hexdump((uint8_t *) stream->cur_ev, ovni_ev_size(stream->cur_ev));
//dbg("---------\n");
2021-07-30 20:08:40 +02:00
return 0;
}