2021-10-26 18:42:41 +02:00
|
|
|
/*
|
|
|
|
* MIT License
|
|
|
|
*
|
|
|
|
* Copyright (c) 2021 Barcelona Supercomputing Center (BSC)
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2021-07-21 18:27:58 +02:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
2021-07-19 15:11:41 +02:00
|
|
|
#include <errno.h>
|
2021-12-07 17:26:08 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
2021-07-30 20:08:40 +02:00
|
|
|
#include <sys/mman.h>
|
2021-07-22 12:35:02 +02:00
|
|
|
#include <sys/stat.h>
|
2021-12-07 17:26:08 +01:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2021-07-19 15:11:41 +02:00
|
|
|
|
|
|
|
#include "ovni.h"
|
2021-11-18 11:55:28 +01:00
|
|
|
#include "common.h"
|
2021-08-03 17:48:37 +02:00
|
|
|
#include "parson.h"
|
2021-07-24 10:53:41 +02:00
|
|
|
|
2021-09-28 19:21:22 +02:00
|
|
|
#ifndef gettid
|
|
|
|
# include <sys/syscall.h>
|
|
|
|
# define gettid() ((pid_t)syscall(SYS_gettid))
|
|
|
|
#endif
|
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
#pragma GCC poison assert
|
2021-07-24 10:53:41 +02:00
|
|
|
|
2021-07-19 15:11:41 +02:00
|
|
|
/* Data per process */
|
2021-07-24 10:53:41 +02:00
|
|
|
struct ovni_rproc rproc = {0};
|
2021-07-19 15:11:41 +02:00
|
|
|
|
|
|
|
/* Data per thread */
|
2021-07-24 10:53:41 +02:00
|
|
|
_Thread_local struct ovni_rthread rthread = {0};
|
2021-07-19 15:11:41 +02:00
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
static void
|
|
|
|
create_trace_dirs(char *tracedir, const char *loom, int pid)
|
2021-07-19 15:11:41 +02:00
|
|
|
{
|
|
|
|
char path[PATH_MAX];
|
|
|
|
|
|
|
|
snprintf(path, PATH_MAX, "%s", tracedir);
|
|
|
|
|
2021-08-02 20:03:20 +02:00
|
|
|
/* May fail if another loom created the directory already */
|
|
|
|
mkdir(path, 0755);
|
2021-07-19 15:11:41 +02:00
|
|
|
|
2021-08-02 20:03:20 +02:00
|
|
|
snprintf(path, PATH_MAX, "%s/loom.%s", tracedir, loom);
|
2021-07-19 15:11:41 +02:00
|
|
|
|
2021-08-02 20:03:20 +02:00
|
|
|
/* Also may fail */
|
|
|
|
mkdir(path, 0755);
|
2021-07-19 15:11:41 +02:00
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
snprintf(rproc.dir, PATH_MAX, "%s/loom.%s/proc.%d", tracedir, loom, pid);
|
2021-07-19 15:11:41 +02:00
|
|
|
|
2021-08-02 20:03:20 +02:00
|
|
|
/* But this one shall not fail */
|
2021-07-19 19:05:26 +02:00
|
|
|
if(mkdir(rproc.dir, 0755))
|
2021-12-07 17:26:08 +01:00
|
|
|
die("mkdir %s failed: %s\n", rproc.dir, strerror(errno));
|
2021-07-19 15:11:41 +02:00
|
|
|
}
|
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
static void
|
2021-10-18 12:47:51 +02:00
|
|
|
create_trace_stream(void)
|
2021-07-19 15:11:41 +02:00
|
|
|
{
|
|
|
|
char path[PATH_MAX];
|
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
int written = snprintf(path, PATH_MAX, "%s/thread.%d",
|
|
|
|
rproc.dir, rthread.tid);
|
|
|
|
|
|
|
|
if(written >= PATH_MAX)
|
|
|
|
die("thread trace path too long: %s/thread.%d\n",
|
|
|
|
rproc.dir, rthread.tid);
|
2021-07-22 12:35:02 +02:00
|
|
|
|
|
|
|
rthread.streamfd = open(path, O_WRONLY | O_CREAT, 0644);
|
|
|
|
|
|
|
|
if(rthread.streamfd == -1)
|
2021-12-07 17:26:08 +01:00
|
|
|
die("open %s failed: %s\n", path, strerror(errno));
|
2021-07-19 15:11:41 +02:00
|
|
|
}
|
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
static void
|
2021-08-03 17:48:37 +02:00
|
|
|
proc_metadata_init(struct ovni_rproc *proc)
|
|
|
|
{
|
|
|
|
proc->meta = json_value_init_object();
|
|
|
|
|
|
|
|
if(proc->meta == NULL)
|
2021-12-07 17:26:08 +01:00
|
|
|
die("failed to create metadata JSON object\n");
|
2021-08-03 17:48:37 +02:00
|
|
|
}
|
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
static void
|
2021-08-03 17:48:37 +02:00
|
|
|
proc_metadata_store(struct ovni_rproc *proc)
|
|
|
|
{
|
|
|
|
char path[PATH_MAX];
|
2021-12-07 17:26:08 +01:00
|
|
|
|
|
|
|
if(proc->meta == NULL)
|
|
|
|
die("process metadata not initialized\n");
|
2021-08-03 17:48:37 +02:00
|
|
|
|
2021-10-21 16:05:31 +02:00
|
|
|
if(snprintf(path, PATH_MAX, "%s/metadata.json", proc->dir) >= PATH_MAX)
|
2021-12-07 17:26:08 +01:00
|
|
|
die("metadata path too long: %s/metadata.json\n", proc->dir);
|
2021-08-03 17:48:37 +02:00
|
|
|
|
|
|
|
if(json_serialize_to_file_pretty(proc->meta, path) != JSONSuccess)
|
2021-12-07 17:26:08 +01:00
|
|
|
die("failed to write process metadata\n");
|
2021-08-03 17:48:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ovni_add_cpu(int index, int phyid)
|
|
|
|
{
|
2021-12-07 17:26:08 +01:00
|
|
|
if(index < 0)
|
|
|
|
die("ovni_add_cpu: cannot use negative index %d\n", index);
|
|
|
|
|
|
|
|
if(phyid < 0)
|
|
|
|
die("ovni_add_cpu: cannot use negative CPU id %d\n", phyid);
|
2021-08-03 17:48:37 +02:00
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
if(!rproc.ready)
|
|
|
|
die("ovni_add_cpu: process not yet initialized\n");
|
2021-08-03 17:48:37 +02:00
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
if(rproc.meta == NULL)
|
|
|
|
die("ovni_add_cpu: metadata not initialized\n");
|
2021-08-03 17:48:37 +02:00
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
JSON_Object *meta = json_value_get_object(rproc.meta);
|
|
|
|
|
|
|
|
if(meta == NULL)
|
|
|
|
die("ovni_add_cpu: json_value_get_object() failed\n");
|
|
|
|
|
|
|
|
int first_time = 0;
|
2021-08-03 17:48:37 +02:00
|
|
|
|
|
|
|
/* Find the CPU array and create it if needed */
|
2021-12-07 17:26:08 +01:00
|
|
|
JSON_Array *cpuarray = json_object_dotget_array(meta, "cpus");
|
2021-08-03 17:48:37 +02:00
|
|
|
|
|
|
|
if(cpuarray == NULL)
|
|
|
|
{
|
2021-12-07 17:26:08 +01:00
|
|
|
JSON_Value *value = json_value_init_array();
|
|
|
|
if(value == NULL)
|
|
|
|
die("ovni_add_cpu: json_value_init_array() failed\n");
|
2021-08-03 17:48:37 +02:00
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
cpuarray = json_array(value);
|
|
|
|
if(cpuarray == NULL)
|
|
|
|
die("ovni_add_cpu: json_array() failed\n");
|
2021-08-03 17:48:37 +02:00
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
first_time = 1;
|
|
|
|
}
|
2021-08-03 17:48:37 +02:00
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
JSON_Value *valcpu = json_value_init_object();
|
|
|
|
if(valcpu == NULL)
|
|
|
|
die("ovni_add_cpu: json_value_init_object() failed\n");
|
2021-08-03 17:48:37 +02:00
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
JSON_Object *cpu = json_object(valcpu);
|
|
|
|
if(cpu == NULL)
|
|
|
|
die("ovni_add_cpu: json_object() failed\n");
|
2021-08-03 17:48:37 +02:00
|
|
|
|
|
|
|
if(json_object_set_number(cpu, "index", index) != 0)
|
2021-12-07 17:26:08 +01:00
|
|
|
die("ovni_add_cpu: json_object_set_number() failed\n");
|
2021-08-03 17:48:37 +02:00
|
|
|
|
|
|
|
if(json_object_set_number(cpu, "phyid", phyid) != 0)
|
2021-12-07 17:26:08 +01:00
|
|
|
die("ovni_add_cpu: json_object_set_number() failed\n");
|
2021-08-03 17:48:37 +02:00
|
|
|
|
|
|
|
if(json_array_append_value(cpuarray, valcpu) != 0)
|
2021-12-07 17:26:08 +01:00
|
|
|
die("ovni_add_cpu: json_array_append_value() failed\n");
|
2021-08-03 17:48:37 +02:00
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
if(first_time)
|
|
|
|
{
|
|
|
|
JSON_Value *value = json_array_get_wrapping_value(cpuarray);
|
|
|
|
if(value == NULL)
|
|
|
|
die("ovni_add_cpu: json_array_get_wrapping_value() failed\n");
|
2021-08-03 17:48:37 +02:00
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
if(json_object_set_value(meta, "cpus", value) != 0)
|
|
|
|
die("ovni_add_cpu: json_object_set_value failed\n");
|
|
|
|
}
|
2021-08-03 17:48:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
proc_set_app(int appid)
|
|
|
|
{
|
2021-12-07 17:26:08 +01:00
|
|
|
JSON_Object *meta = json_value_get_object(rproc.meta);
|
2021-08-03 17:48:37 +02:00
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
if(meta == NULL)
|
|
|
|
die("json_value_get_object failed\n");
|
2021-08-03 17:48:37 +02:00
|
|
|
|
|
|
|
if(json_object_set_number(meta, "app_id", appid) != 0)
|
2021-12-07 17:26:08 +01:00
|
|
|
die("json_object_set_number for app_id failed\n");
|
2021-08-03 17:48:37 +02:00
|
|
|
}
|
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
void
|
|
|
|
ovni_proc_init(int app, const char *loom, int pid)
|
2021-07-19 15:11:41 +02:00
|
|
|
{
|
2021-12-07 17:26:08 +01:00
|
|
|
if(rproc.ready)
|
|
|
|
die("ovni_proc_init: pid %d already initialized\n", pid);
|
2021-07-21 18:27:58 +02:00
|
|
|
|
2021-07-19 19:05:26 +02:00
|
|
|
memset(&rproc, 0, sizeof(rproc));
|
2021-07-19 15:11:41 +02:00
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
if(strlen(loom) >= OVNI_MAX_HOSTNAME)
|
|
|
|
die("ovni_proc_init: loom name too long: %s\n", loom);
|
|
|
|
|
2021-08-02 20:03:20 +02:00
|
|
|
strcpy(rproc.loom, loom);
|
2021-12-07 17:26:08 +01:00
|
|
|
rproc.pid = pid;
|
2021-08-03 17:48:37 +02:00
|
|
|
rproc.app = app;
|
2021-07-19 19:05:26 +02:00
|
|
|
rproc.clockid = CLOCK_MONOTONIC;
|
2021-07-19 15:11:41 +02:00
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
create_trace_dirs(OVNI_TRACEDIR, loom, pid);
|
2021-07-21 18:27:58 +02:00
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
proc_metadata_init(&rproc);
|
2021-08-03 17:48:37 +02:00
|
|
|
|
2021-07-21 18:27:58 +02:00
|
|
|
rproc.ready = 1;
|
2021-07-19 15:11:41 +02:00
|
|
|
|
2021-08-03 17:48:37 +02:00
|
|
|
proc_set_app(app);
|
|
|
|
}
|
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
void
|
2021-10-18 12:47:51 +02:00
|
|
|
ovni_proc_fini(void)
|
2021-08-03 17:48:37 +02:00
|
|
|
{
|
2021-12-07 17:26:08 +01:00
|
|
|
if(!rproc.ready)
|
|
|
|
die("ovni_proc_fini: process not initialized\n");
|
2021-08-03 17:48:37 +02:00
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
proc_metadata_store(&rproc);
|
2021-07-19 19:05:26 +02:00
|
|
|
}
|
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
void
|
2021-07-19 19:05:26 +02:00
|
|
|
ovni_thread_init(pid_t tid)
|
|
|
|
{
|
2021-07-21 18:27:58 +02:00
|
|
|
if(rthread.ready)
|
|
|
|
{
|
2021-12-07 17:26:08 +01:00
|
|
|
err("warning: thread %d already initialized, ignored\n", tid);
|
|
|
|
return;
|
2021-07-21 18:27:58 +02:00
|
|
|
}
|
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
if(tid == 0)
|
|
|
|
die("ovni_thread_init: cannot use tid=%d\n", tid);
|
|
|
|
|
|
|
|
if(!rproc.ready)
|
|
|
|
die("ovni_thread_init: process not yet initialized\n");
|
2021-07-21 18:27:58 +02:00
|
|
|
|
2021-07-19 19:05:26 +02:00
|
|
|
memset(&rthread, 0, sizeof(rthread));
|
|
|
|
|
|
|
|
rthread.tid = tid;
|
2021-07-24 10:53:41 +02:00
|
|
|
rthread.evlen = 0;
|
|
|
|
rthread.evbuf = malloc(OVNI_MAX_EV_BUF);
|
2021-12-07 17:26:08 +01:00
|
|
|
|
2021-07-24 10:53:41 +02:00
|
|
|
if(rthread.evbuf == NULL)
|
2021-12-07 17:26:08 +01:00
|
|
|
die("ovni_thread_init: malloc failed: %s", strerror(errno));
|
2021-07-19 19:05:26 +02:00
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
create_trace_stream();
|
2021-07-21 18:27:58 +02:00
|
|
|
|
|
|
|
rthread.ready = 1;
|
2021-07-19 15:11:41 +02:00
|
|
|
}
|
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
void
|
|
|
|
ovni_thread_free(void)
|
2021-07-24 10:53:41 +02:00
|
|
|
{
|
2021-12-07 17:26:08 +01:00
|
|
|
if(!rthread.ready)
|
|
|
|
die("ovni_thread_free: thread not initialized\n");
|
|
|
|
|
2021-07-24 10:53:41 +02:00
|
|
|
free(rthread.evbuf);
|
|
|
|
}
|
|
|
|
|
2021-07-21 18:27:58 +02:00
|
|
|
int
|
2021-10-18 12:47:51 +02:00
|
|
|
ovni_thread_isready(void)
|
2021-07-21 18:27:58 +02:00
|
|
|
{
|
|
|
|
return rthread.ready;
|
|
|
|
}
|
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
#ifdef USE_TSC
|
2021-07-24 10:53:41 +02:00
|
|
|
static inline
|
2021-12-07 17:26:08 +01:00
|
|
|
uint64_t clock_tsc_now(void)
|
2021-07-24 10:53:41 +02:00
|
|
|
{
|
|
|
|
uint32_t lo, hi;
|
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
/* RDTSC copies contents of 64-bit TSC into EDX:EAX */
|
2021-09-28 19:21:22 +02:00
|
|
|
__asm__ volatile("rdtsc" : "=a" (lo), "=d" (hi));
|
2021-07-24 10:53:41 +02:00
|
|
|
return (uint64_t) hi << 32 | lo;
|
|
|
|
}
|
2021-11-08 17:05:22 +01:00
|
|
|
#endif
|
2021-07-24 10:53:41 +02:00
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
static uint64_t
|
|
|
|
clock_monotonic_now(void)
|
2021-07-19 15:11:41 +02:00
|
|
|
{
|
2021-12-07 17:26:08 +01:00
|
|
|
uint64_t ns = 1000ULL * 1000ULL * 1000ULL;
|
2021-07-19 15:11:41 +02:00
|
|
|
struct timespec tp;
|
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
if(clock_gettime(rproc.clockid, &tp))
|
|
|
|
die("clock_gettime() failed: %s\n", strerror(errno));
|
2021-07-19 15:11:41 +02:00
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
return tp.tv_sec * ns + tp.tv_nsec;
|
2021-07-24 10:53:41 +02:00
|
|
|
}
|
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
uint64_t
|
|
|
|
ovni_clock_now(void)
|
2021-07-19 19:05:26 +02:00
|
|
|
{
|
2021-12-07 17:26:08 +01:00
|
|
|
#ifdef USE_TSC
|
|
|
|
return clock_tsc_now();
|
|
|
|
#else
|
|
|
|
return clock_monotonic_now();
|
|
|
|
#endif
|
|
|
|
}
|
2021-07-21 18:27:58 +02:00
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
static void
|
|
|
|
write_evbuf(uint8_t *buf, size_t size)
|
|
|
|
{
|
2021-07-22 12:35:02 +02:00
|
|
|
do
|
2021-07-19 15:11:41 +02:00
|
|
|
{
|
2021-12-07 17:26:08 +01:00
|
|
|
ssize_t written = write(rthread.streamfd, buf, size);
|
2021-07-22 12:35:02 +02:00
|
|
|
|
|
|
|
if(written < 0)
|
2021-12-07 17:26:08 +01:00
|
|
|
die("failed to write buffer to disk: %s\n", strerror(errno));
|
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
|
|
|
}
|
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
static void
|
2021-10-18 12:47:51 +02:00
|
|
|
flush_evbuf(void)
|
2021-07-24 10:53:41 +02:00
|
|
|
{
|
2021-12-07 17:26:08 +01:00
|
|
|
write_evbuf(rthread.evbuf, rthread.evlen);
|
2021-07-24 10:53:41 +02:00
|
|
|
|
|
|
|
rthread.evlen = 0;
|
|
|
|
}
|
|
|
|
|
2021-11-16 14:35:39 +01:00
|
|
|
void
|
|
|
|
ovni_ev_set_clock(struct ovni_ev *ev, uint64_t clock)
|
2021-07-24 10:53:41 +02:00
|
|
|
{
|
2021-11-16 14:35:39 +01:00
|
|
|
ev->header.clock = clock;
|
2021-07-24 10:53:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
2021-11-19 17:09:39 +01:00
|
|
|
ovni_ev_get_clock(const struct ovni_ev *ev)
|
2021-07-24 10:53:41 +02:00
|
|
|
{
|
2021-08-11 11:50:07 +02:00
|
|
|
return ev->header.clock;
|
2021-07-24 10:53:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-11-19 17:09:39 +01:00
|
|
|
ovni_ev_set_mcv(struct ovni_ev *ev, const char *mcv)
|
2021-07-24 10:53:41 +02:00
|
|
|
{
|
2021-07-30 20:08:40 +02:00
|
|
|
ev->header.model = mcv[0];
|
2021-10-11 15:46:49 +02:00
|
|
|
ev->header.category = mcv[1];
|
2021-07-30 20:08:40 +02:00
|
|
|
ev->header.value = mcv[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2021-11-19 17:09:39 +01:00
|
|
|
get_jumbo_payload_size(const struct ovni_ev *ev)
|
2021-07-30 20:08:40 +02:00
|
|
|
{
|
|
|
|
return sizeof(ev->payload.jumbo.size) + ev->payload.jumbo.size;
|
2021-07-24 10:53:41 +02:00
|
|
|
}
|
|
|
|
|
2021-07-22 12:35:02 +02:00
|
|
|
int
|
2021-11-19 17:09:39 +01:00
|
|
|
ovni_payload_size(const struct ovni_ev *ev)
|
2021-07-24 10:53:41 +02:00
|
|
|
{
|
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;
|
2021-07-24 10:53:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-11-19 17:09:39 +01:00
|
|
|
ovni_payload_add(struct ovni_ev *ev, const uint8_t *buf, int size)
|
2021-07-24 10:53:41 +02:00
|
|
|
{
|
2021-12-07 17:26:08 +01:00
|
|
|
if(ev->header.flags & OVNI_EV_JUMBO)
|
|
|
|
die("ovni_payload_add: event is marked as jumbo\n");
|
2021-07-28 11:56:35 +02:00
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
if(size < 2)
|
|
|
|
die("ovni_payload_add: payload size %d too small\n", size);
|
2021-07-30 20:08:40 +02:00
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
size_t payload_size = ovni_payload_size(ev);
|
2021-07-28 11:56:35 +02:00
|
|
|
|
2021-07-24 10:53:41 +02:00
|
|
|
/* Ensure we have room */
|
2021-12-07 17:26:08 +01:00
|
|
|
if(payload_size + size > sizeof(ev->payload))
|
|
|
|
die("ovni_payload_add: no space left for %d bytes\n", size);
|
2021-07-28 11:56:35 +02:00
|
|
|
|
|
|
|
memcpy(&ev->payload.u8[payload_size], buf, size);
|
|
|
|
payload_size += size;
|
2021-07-24 10:53:41 +02:00
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
ev->header.flags = (ev->header.flags & 0xf0) | ((payload_size-1) & 0x0f);
|
2021-07-24 10:53:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2021-11-19 17:09:39 +01:00
|
|
|
ovni_ev_size(const struct ovni_ev *ev)
|
2021-07-24 10:53:41 +02:00
|
|
|
{
|
2021-07-30 20:08:40 +02:00
|
|
|
return sizeof(ev->header) + ovni_payload_size(ev);
|
2021-07-24 10:53:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-07-30 20:08:40 +02:00
|
|
|
ovni_ev_add(struct ovni_ev *ev);
|
2021-07-24 10:53:41 +02:00
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
void
|
2021-10-18 12:47:51 +02:00
|
|
|
ovni_flush(void)
|
2021-07-22 12:35:02 +02:00
|
|
|
{
|
2021-07-24 10:53:41 +02:00
|
|
|
struct ovni_ev pre={0}, post={0};
|
2021-07-22 12:35:02 +02:00
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
if(!rthread.ready)
|
|
|
|
die("ovni_flush: thread is not initialized\n");
|
|
|
|
|
|
|
|
if(!rproc.ready)
|
|
|
|
die("ovni_flush: process is not initialized\n");
|
2021-07-22 12:35:02 +02:00
|
|
|
|
2021-11-16 14:35:39 +01:00
|
|
|
ovni_ev_set_clock(&pre, ovni_clock_now());
|
2021-07-24 10:53:41 +02:00
|
|
|
ovni_ev_set_mcv(&pre, "OF[");
|
2021-07-22 12:35:02 +02:00
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
flush_evbuf();
|
2021-07-22 12:35:02 +02:00
|
|
|
|
2021-11-16 14:35:39 +01:00
|
|
|
ovni_ev_set_clock(&post, ovni_clock_now());
|
2021-07-24 10:53:41 +02:00
|
|
|
ovni_ev_set_mcv(&post, "OF]");
|
2021-07-22 12:35:02 +02:00
|
|
|
|
2021-07-24 10:53:41 +02:00
|
|
|
/* Add the two flush events */
|
|
|
|
ovni_ev_add(&pre);
|
|
|
|
ovni_ev_add(&post);
|
2021-07-22 12:35:02 +02:00
|
|
|
}
|
|
|
|
|
2021-11-03 11:08:29 +01:00
|
|
|
static void
|
|
|
|
add_flush_events(uint64_t t0, uint64_t t1)
|
|
|
|
{
|
|
|
|
struct ovni_ev pre={0}, post={0};
|
|
|
|
|
|
|
|
pre.header.clock = t0;
|
|
|
|
ovni_ev_set_mcv(&pre, "OF[");
|
|
|
|
|
|
|
|
post.header.clock = t1;
|
|
|
|
ovni_ev_set_mcv(&post, "OF]");
|
|
|
|
|
|
|
|
/* Add the two flush events */
|
|
|
|
ovni_ev_add(&pre);
|
|
|
|
ovni_ev_add(&post);
|
|
|
|
}
|
|
|
|
|
2021-10-18 12:47:51 +02:00
|
|
|
static void
|
2021-11-19 17:09:39 +01:00
|
|
|
ovni_ev_add_jumbo(struct ovni_ev *ev, const uint8_t *buf, uint32_t bufsize)
|
2021-07-30 20:08:40 +02:00
|
|
|
{
|
2021-11-03 11:08:29 +01:00
|
|
|
int flushed = 0;
|
|
|
|
uint64_t t0, t1;
|
2021-07-30 20:08:40 +02:00
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
if(ovni_payload_size(ev) != 0)
|
|
|
|
die("ovni_ev_add_jumbo: the event payload must be empty\n");
|
2021-07-30 20:08:40 +02:00
|
|
|
|
|
|
|
ovni_payload_add(ev, (uint8_t *) &bufsize, sizeof(bufsize));
|
2021-12-07 17:26:08 +01:00
|
|
|
size_t evsize = ovni_ev_size(ev);
|
|
|
|
|
|
|
|
size_t totalsize = evsize + bufsize;
|
2021-07-30 20:08:40 +02:00
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
if(totalsize >= OVNI_MAX_EV_BUF)
|
|
|
|
die("ovni_ev_add_jumbo: event too large\n");
|
2021-07-30 20:08:40 +02:00
|
|
|
|
|
|
|
/* Check if the event fits or flush first otherwise */
|
|
|
|
if(rthread.evlen + totalsize >= OVNI_MAX_EV_BUF)
|
2021-11-03 11:08:29 +01:00
|
|
|
{
|
|
|
|
/* Measure the flush times */
|
2021-11-16 14:35:39 +01:00
|
|
|
t0 = ovni_clock_now();
|
2021-11-03 11:08:29 +01:00
|
|
|
flush_evbuf();
|
2021-11-16 14:35:39 +01:00
|
|
|
t1 = ovni_clock_now();
|
2021-11-03 11:08:29 +01:00
|
|
|
flushed = 1;
|
|
|
|
}
|
2021-07-30 20:08:40 +02:00
|
|
|
|
2021-12-09 16:42:12 +01:00
|
|
|
/* Set the jumbo flag here, so we capture the previous evsize
|
2021-07-30 20:08:40 +02:00
|
|
|
* 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;
|
|
|
|
|
2021-11-03 11:08:29 +01:00
|
|
|
if(flushed)
|
|
|
|
{
|
|
|
|
/* Emit the flush events *after* the user event */
|
|
|
|
add_flush_events(t0, t1);
|
|
|
|
}
|
|
|
|
}
|
2021-07-30 20:08:40 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
ovni_ev_add(struct ovni_ev *ev)
|
|
|
|
{
|
2021-12-07 17:26:08 +01:00
|
|
|
int flushed = 0;
|
2021-11-03 11:08:29 +01:00
|
|
|
uint64_t t0, t1;
|
2021-07-30 20:08:40 +02:00
|
|
|
|
2021-12-07 17:26:08 +01:00
|
|
|
int size = ovni_ev_size(ev);
|
2021-07-30 20:08:40 +02:00
|
|
|
|
|
|
|
/* Check if the event fits or flush first otherwise */
|
|
|
|
if(rthread.evlen + size >= OVNI_MAX_EV_BUF)
|
2021-11-03 11:08:29 +01:00
|
|
|
{
|
|
|
|
/* Measure the flush times */
|
2021-11-16 14:35:39 +01:00
|
|
|
t0 = ovni_clock_now();
|
2021-11-03 11:08:29 +01:00
|
|
|
flush_evbuf();
|
2021-11-16 14:35:39 +01:00
|
|
|
t1 = ovni_clock_now();
|
2021-11-03 11:08:29 +01:00
|
|
|
flushed = 1;
|
|
|
|
}
|
2021-07-30 20:08:40 +02:00
|
|
|
|
|
|
|
memcpy(&rthread.evbuf[rthread.evlen], ev, size);
|
|
|
|
rthread.evlen += size;
|
2021-11-03 11:08:29 +01:00
|
|
|
|
|
|
|
if(flushed)
|
|
|
|
{
|
|
|
|
/* Emit the flush events *after* the user event */
|
|
|
|
add_flush_events(t0, t1);
|
|
|
|
}
|
2021-07-30 20:08:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-11-19 17:09:39 +01:00
|
|
|
ovni_ev_jumbo_emit(struct ovni_ev *ev, const uint8_t *buf, uint32_t bufsize)
|
2021-07-30 20:08:40 +02:00
|
|
|
{
|
|
|
|
ovni_ev_add_jumbo(ev, buf, bufsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-10-29 17:04:42 +02:00
|
|
|
ovni_ev_emit(struct ovni_ev *ev)
|
2021-07-30 20:08:40 +02:00
|
|
|
{
|
|
|
|
ovni_ev_add(ev);
|
|
|
|
}
|