2021-11-29 12:06:09 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021 Barcelona Supercomputing Center (BSC)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* This program is a really bad idea. It attempts to sort streams by using a
|
|
|
|
* window of the last N events in memory, so we can quickly access the event
|
|
|
|
* clocks. Then, as soon as we detect a region of potentially unsorted events,
|
|
|
|
* we go back until we find a suitable position and start injecting the events
|
|
|
|
* in order.
|
|
|
|
*
|
|
|
|
* The events inside a unsorted region must be ordered. And the number of events
|
|
|
|
* that we will look back is limited by N.
|
|
|
|
*/
|
|
|
|
|
2021-11-30 15:25:59 +01:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
2021-11-29 12:06:09 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <linux/limits.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/stat.h>
|
2021-12-15 16:50:01 +01:00
|
|
|
#include <sys/mman.h>
|
2021-11-29 12:06:09 +01:00
|
|
|
#include <stdatomic.h>
|
|
|
|
#include <dirent.h>
|
2021-11-30 15:25:59 +01:00
|
|
|
#include <unistd.h>
|
2021-12-15 15:52:41 +01:00
|
|
|
#include <ctype.h>
|
2021-11-29 12:06:09 +01:00
|
|
|
|
|
|
|
#include "ovni.h"
|
|
|
|
#include "trace.h"
|
|
|
|
#include "emu.h"
|
|
|
|
|
|
|
|
struct ring {
|
|
|
|
ssize_t head;
|
|
|
|
ssize_t tail;
|
|
|
|
ssize_t size;
|
|
|
|
struct ovni_ev **ev;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct sortplan {
|
|
|
|
/* The first and last events which need sorting */
|
|
|
|
struct ovni_ev *bad0;
|
|
|
|
|
|
|
|
/* The next event which must be not affected */
|
|
|
|
struct ovni_ev *next;
|
|
|
|
|
|
|
|
struct ring *r;
|
|
|
|
};
|
|
|
|
|
2021-11-30 15:25:59 +01:00
|
|
|
enum operation_mode { SORT, CHECK };
|
|
|
|
|
|
|
|
static char *tracedir = NULL;
|
|
|
|
static enum operation_mode operation_mode = SORT;
|
|
|
|
static const size_t max_look_back = 10000;
|
|
|
|
|
2021-11-29 12:06:09 +01:00
|
|
|
static void
|
|
|
|
ring_reset(struct ring *r)
|
|
|
|
{
|
|
|
|
r->head = r->tail = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ring_add(struct ring *r, struct ovni_ev *ev)
|
|
|
|
{
|
|
|
|
r->ev[r->tail] = ev;
|
|
|
|
r->tail++;
|
|
|
|
|
|
|
|
if(r->tail >= r->size)
|
|
|
|
r->tail = 0;
|
|
|
|
|
|
|
|
if(r->head == r->tail)
|
|
|
|
r->head = r->tail + 1;
|
|
|
|
|
|
|
|
if(r->head >= r->size)
|
|
|
|
r->head = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
find_destination(struct ring *r, uint64_t clock)
|
|
|
|
{
|
2021-11-30 15:25:59 +01:00
|
|
|
ssize_t i, start, end, nback = 0;
|
2021-11-29 12:06:09 +01:00
|
|
|
|
|
|
|
start = r->tail - 1 >= 0 ? r->tail - 1 : r->size - 1;
|
|
|
|
end = r->head - 1 >= 0 ? r->head - 1 : r->size - 1;
|
|
|
|
|
2021-12-13 19:16:34 +01:00
|
|
|
for(i=start; i != end; i = i-1 < 0 ? r->size - 1: i-1)
|
2021-11-29 12:06:09 +01:00
|
|
|
{
|
|
|
|
if(r->ev[i]->header.clock < clock)
|
2021-11-30 15:25:59 +01:00
|
|
|
{
|
2021-12-09 16:41:56 +01:00
|
|
|
dbg("found suitable position %ld events backwards\n",
|
2021-11-30 15:25:59 +01:00
|
|
|
nback);
|
2021-11-29 12:06:09 +01:00
|
|
|
return i;
|
2021-11-30 15:25:59 +01:00
|
|
|
}
|
|
|
|
nback++;
|
2021-11-29 12:06:09 +01:00
|
|
|
}
|
|
|
|
|
2021-11-30 15:25:59 +01:00
|
|
|
err("cannot find a event previous to clock %lu\n", clock);
|
|
|
|
|
2021-11-29 12:06:09 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
starts_unsorted_region(struct ovni_ev *ev)
|
|
|
|
{
|
|
|
|
return ev->header.model == 'O' &&
|
|
|
|
ev->header.category == 'U' &&
|
|
|
|
ev->header.value == '[';
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ends_unsorted_region(struct ovni_ev *ev)
|
|
|
|
{
|
|
|
|
return ev->header.model == 'O' &&
|
|
|
|
ev->header.category == 'U' &&
|
|
|
|
ev->header.value == ']';
|
|
|
|
}
|
|
|
|
|
2021-12-15 15:52:41 +01:00
|
|
|
static void
|
|
|
|
hexdump(uint8_t *buf, size_t size)
|
|
|
|
{
|
|
|
|
UNUSED(buf);
|
|
|
|
UNUSED(size);
|
|
|
|
|
|
|
|
size_t i, j;
|
|
|
|
|
|
|
|
//printf("writing %ld bytes in cpu=%d\n", size, rthread.cpu);
|
|
|
|
|
|
|
|
for(i=0; i<size; i+=16)
|
|
|
|
{
|
|
|
|
for(j=0; j<16; j++)
|
|
|
|
{
|
|
|
|
if(i+j < size)
|
|
|
|
fprintf(stderr, "%02x ", buf[i+j]);
|
|
|
|
else
|
|
|
|
fprintf(stderr, " ");
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, " | ");
|
|
|
|
|
|
|
|
for(j=0; j<16; j++)
|
|
|
|
{
|
2021-12-15 18:12:44 +01:00
|
|
|
if(i+j < size)
|
|
|
|
{
|
|
|
|
if(isprint(buf[i+j]))
|
|
|
|
fprintf(stderr, "%c", buf[i+j]);
|
|
|
|
else
|
|
|
|
fprintf(stderr, ".");
|
|
|
|
}
|
2021-12-15 15:52:41 +01:00
|
|
|
else
|
2021-12-15 18:12:44 +01:00
|
|
|
{
|
2021-12-15 15:52:41 +01:00
|
|
|
fprintf(stderr, " ");
|
2021-12-15 18:12:44 +01:00
|
|
|
}
|
2021-12-15 15:52:41 +01:00
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-29 12:06:09 +01:00
|
|
|
static void
|
|
|
|
sort_buf(uint8_t *src, uint8_t *buf, int64_t bufsize,
|
|
|
|
uint8_t *srcbad, uint8_t *srcnext)
|
|
|
|
{
|
2021-12-15 18:05:53 +01:00
|
|
|
uint8_t *p, *q;
|
|
|
|
int64_t evsize, injected = 0;
|
2021-11-29 12:06:09 +01:00
|
|
|
struct ovni_ev *ep, *eq, *ev;
|
|
|
|
|
|
|
|
p = src;
|
|
|
|
q = srcbad;
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
ep = (struct ovni_ev *) p;
|
|
|
|
eq = (struct ovni_ev *) q;
|
|
|
|
|
|
|
|
if(p < srcbad && ep->header.clock < eq->header.clock)
|
|
|
|
{
|
|
|
|
ev = ep;
|
|
|
|
evsize = ovni_ev_size(ev);
|
|
|
|
p += evsize;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ev = eq;
|
|
|
|
evsize = ovni_ev_size(ev);
|
|
|
|
q += evsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
if((uint8_t *) ev == srcnext)
|
|
|
|
break;
|
|
|
|
|
2021-12-07 19:52:48 +01:00
|
|
|
if((uint8_t *) ev > srcnext)
|
|
|
|
die("exceeded srcnext while sorting\n");
|
|
|
|
|
|
|
|
if(bufsize < evsize)
|
|
|
|
die("no space left in the sort buffer\n");
|
2021-11-29 12:06:09 +01:00
|
|
|
|
|
|
|
memcpy(buf, ev, evsize);
|
|
|
|
buf += evsize;
|
|
|
|
bufsize -= evsize;
|
2021-11-30 15:25:59 +01:00
|
|
|
injected++;
|
2021-11-29 12:06:09 +01:00
|
|
|
}
|
2021-11-30 15:25:59 +01:00
|
|
|
|
2021-12-07 19:52:48 +01:00
|
|
|
dbg("injected %ld events in the past\n", injected);
|
2021-11-29 12:06:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-12-15 18:05:53 +01:00
|
|
|
execute_sort_plan(struct sortplan *sp, size_t region)
|
2021-11-29 12:06:09 +01:00
|
|
|
{
|
|
|
|
int64_t i0, bufsize;
|
|
|
|
uint8_t *buf;
|
2021-12-15 15:00:15 +01:00
|
|
|
/* The first event in the stream that may be affected */
|
|
|
|
struct ovni_ev *first;
|
|
|
|
|
|
|
|
|
|
|
|
dbg("attempt to sort: start clock %ld\n", sp->bad0->header.clock);
|
2021-11-29 12:06:09 +01:00
|
|
|
|
|
|
|
/* Cannot sort in one pass; just fail for now */
|
|
|
|
if((i0 = find_destination(sp->r, sp->bad0->header.clock)) < 0)
|
2021-11-30 15:25:59 +01:00
|
|
|
{
|
|
|
|
err("cannot find a destination for unsorted region\n");
|
2021-11-29 12:06:09 +01:00
|
|
|
return -1;
|
2021-11-30 15:25:59 +01:00
|
|
|
}
|
2021-11-29 12:06:09 +01:00
|
|
|
|
|
|
|
/* Set the pointer to the first event */
|
2021-12-15 15:00:15 +01:00
|
|
|
first = sp->r->ev[i0];
|
2021-11-29 12:06:09 +01:00
|
|
|
|
|
|
|
/* Allocate a working buffer */
|
2021-12-15 15:00:15 +01:00
|
|
|
bufsize = ((int64_t) sp->next) - ((int64_t) first);
|
2021-11-29 12:06:09 +01:00
|
|
|
|
2021-12-07 19:52:48 +01:00
|
|
|
if(bufsize <= 0)
|
|
|
|
die("bufsize is non-positive\n");
|
2021-11-30 15:25:59 +01:00
|
|
|
|
2021-11-29 12:06:09 +01:00
|
|
|
buf = malloc(bufsize);
|
2021-11-30 15:25:59 +01:00
|
|
|
if(!buf)
|
2021-12-07 19:52:48 +01:00
|
|
|
die("malloc failed: %s\n", strerror(errno));
|
2021-11-29 12:06:09 +01:00
|
|
|
|
2021-12-15 18:05:53 +01:00
|
|
|
if(region == 0)
|
|
|
|
{
|
|
|
|
err("stream window region %ld BEFORE sort:\n", region);
|
|
|
|
hexdump((uint8_t *) first, bufsize);
|
|
|
|
}
|
|
|
|
|
2021-12-15 15:00:15 +01:00
|
|
|
sort_buf((uint8_t *) first, buf, bufsize,
|
2021-11-29 12:06:09 +01:00
|
|
|
(uint8_t *) sp->bad0, (uint8_t *) sp->next);
|
|
|
|
|
|
|
|
/* Copy the sorted events back into the stream buffer */
|
2021-12-15 15:00:15 +01:00
|
|
|
memcpy(first, buf, bufsize);
|
|
|
|
free(buf);
|
2021-11-29 12:06:09 +01:00
|
|
|
|
2021-12-15 18:05:53 +01:00
|
|
|
if(region == 0)
|
|
|
|
{
|
|
|
|
err("stream window region %ld AFTER sort:\n", region);
|
|
|
|
hexdump((uint8_t *) first, bufsize);
|
|
|
|
}
|
|
|
|
|
2021-11-29 12:06:09 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sort the events in the stream chronologically using a ring */
|
|
|
|
static int
|
|
|
|
stream_winsort(struct ovni_stream *stream, struct ring *r)
|
|
|
|
{
|
|
|
|
ssize_t i;
|
|
|
|
struct ovni_ev *ev;
|
|
|
|
struct sortplan sp = {0};
|
|
|
|
//uint64_t lastclock = 0;
|
|
|
|
char st = 'S';
|
|
|
|
|
|
|
|
ring_reset(r);
|
|
|
|
sp.r = r;
|
|
|
|
|
2021-12-15 18:05:53 +01:00
|
|
|
size_t region = 0;
|
|
|
|
|
2021-11-29 12:06:09 +01:00
|
|
|
for(i=0; stream->active; i++)
|
|
|
|
{
|
|
|
|
ovni_load_next_event(stream);
|
|
|
|
ev = stream->cur_ev;
|
|
|
|
|
|
|
|
if(st == 'S' && starts_unsorted_region(ev))
|
|
|
|
{
|
|
|
|
st = 'U';
|
|
|
|
}
|
|
|
|
else if(st == 'U')
|
|
|
|
{
|
2021-11-30 15:25:59 +01:00
|
|
|
/* Ensure that we have at least one unsorted
|
|
|
|
* event inside the section */
|
|
|
|
if(ends_unsorted_region(ev))
|
|
|
|
{
|
|
|
|
err("warning: ignoring empty sort section\n");
|
|
|
|
st = 'S';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
st = 'X';
|
|
|
|
sp.bad0 = ev;
|
|
|
|
}
|
2021-11-29 12:06:09 +01:00
|
|
|
}
|
|
|
|
else if(st == 'X')
|
|
|
|
{
|
|
|
|
if(ends_unsorted_region(ev))
|
|
|
|
{
|
|
|
|
sp.next = ev;
|
2021-12-15 15:52:41 +01:00
|
|
|
dbg("executing sort plan for stream tid=%d\n", stream->tid);
|
2021-12-15 18:05:53 +01:00
|
|
|
if(execute_sort_plan(&sp, region) < 0)
|
2021-11-30 15:25:59 +01:00
|
|
|
{
|
|
|
|
err("sort failed for stream tid=%d\n",
|
|
|
|
stream->tid);
|
|
|
|
return -1;
|
|
|
|
}
|
2021-12-15 15:00:15 +01:00
|
|
|
|
|
|
|
/* Clear markers */
|
|
|
|
sp.next = NULL;
|
|
|
|
sp.bad0 = NULL;
|
|
|
|
|
2021-12-15 18:05:53 +01:00
|
|
|
region++;
|
|
|
|
|
2021-11-29 12:06:09 +01:00
|
|
|
st = 'S';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ring_add(r, ev);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-11-30 15:25:59 +01:00
|
|
|
/* Ensures that each individual stream is sorted */
|
|
|
|
static int
|
|
|
|
stream_check(struct ovni_stream *stream)
|
|
|
|
{
|
|
|
|
ovni_load_next_event(stream);
|
|
|
|
struct ovni_ev *ev = stream->cur_ev;
|
|
|
|
uint64_t last_clock = ev->header.clock;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
for(ssize_t i=0; stream->active; i++)
|
|
|
|
{
|
|
|
|
ovni_load_next_event(stream);
|
|
|
|
ev = stream->cur_ev;
|
|
|
|
uint64_t cur_clock = ovni_ev_get_clock(ev);
|
|
|
|
|
|
|
|
if(cur_clock < last_clock)
|
|
|
|
{
|
|
|
|
err("backwards jump in time %ld -> %ld for stream tid=%d\n",
|
|
|
|
last_clock, cur_clock, stream->tid);
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
last_clock = cur_clock;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
process_trace(struct ovni_trace *trace)
|
2021-11-29 12:06:09 +01:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
struct ovni_stream *stream;
|
|
|
|
struct ring ring;
|
2021-11-30 15:25:59 +01:00
|
|
|
int ret = 0;
|
2021-11-29 12:06:09 +01:00
|
|
|
|
2021-11-30 15:25:59 +01:00
|
|
|
ring.size = max_look_back;
|
2021-11-29 12:06:09 +01:00
|
|
|
ring.ev = malloc(ring.size * sizeof(struct ovni_ev *));
|
|
|
|
|
2021-12-07 19:52:48 +01:00
|
|
|
if(ring.ev == NULL)
|
|
|
|
die("malloc failed: %s\n", strerror(errno));
|
2021-11-29 12:06:09 +01:00
|
|
|
|
|
|
|
for(i=0; i<trace->nstreams; i++)
|
|
|
|
{
|
|
|
|
stream = &trace->stream[i];
|
2021-11-30 15:25:59 +01:00
|
|
|
if(operation_mode == SORT)
|
|
|
|
{
|
2021-12-15 15:52:41 +01:00
|
|
|
dbg("sorting stream tid=%d\n", stream->tid);
|
2021-11-30 15:25:59 +01:00
|
|
|
if(stream_winsort(stream, &ring) != 0)
|
|
|
|
{
|
|
|
|
err("sort stream tid=%d failed\n", stream->tid);
|
|
|
|
/* When sorting, return at the first
|
|
|
|
* attempt */
|
|
|
|
return -1;
|
|
|
|
}
|
2021-12-15 16:50:01 +01:00
|
|
|
|
|
|
|
if(msync(stream->buf, stream->size, MS_ASYNC))
|
|
|
|
die("msync failed: %s\n", strerror(errno));
|
2021-11-30 15:25:59 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(stream_check(stream) != 0)
|
|
|
|
{
|
|
|
|
err("stream tid=%d is not sorted\n", stream->tid);
|
|
|
|
/* When checking, report all errors and
|
|
|
|
* then fail */
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
}
|
2021-11-29 12:06:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
free(ring.ev);
|
2021-11-30 15:25:59 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
UNUSED(argc);
|
|
|
|
UNUSED(argv);
|
|
|
|
|
|
|
|
err("Usage: ovnisort [-c] tracedir\n");
|
|
|
|
err("\n");
|
|
|
|
err("Sorts the events in each stream of the trace given in\n");
|
|
|
|
err("tracedir, so they are suitable for the emulator ovniemu.\n");
|
|
|
|
err("Only the events enclosed by OU[ OU] are sorted. At most a\n");
|
|
|
|
err("total of %ld events are looked back to insert the unsorted\n",
|
|
|
|
max_look_back);
|
|
|
|
err("events, so the sort procedure can fail with an error.\n");
|
|
|
|
err("\n");
|
|
|
|
err("Options:\n");
|
|
|
|
err(" -c Enable check mode: don't sort, ensure the\n");
|
|
|
|
err(" trace is already sorted.\n");
|
|
|
|
err("\n");
|
|
|
|
err(" tracedir The trace directory generated by ovni.\n");
|
|
|
|
err("\n");
|
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
parse_args(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int opt;
|
|
|
|
|
|
|
|
while((opt = getopt(argc, argv, "c")) != -1)
|
|
|
|
{
|
|
|
|
switch(opt)
|
|
|
|
{
|
|
|
|
case 'c':
|
|
|
|
operation_mode = CHECK;
|
|
|
|
break;
|
|
|
|
default: /* '?' */
|
|
|
|
usage(argc, argv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(optind >= argc)
|
|
|
|
{
|
|
|
|
err("missing tracedir\n");
|
|
|
|
usage(argc, argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
tracedir = argv[optind];
|
2021-11-29 12:06:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2021-11-30 15:25:59 +01:00
|
|
|
int ret = 0;
|
2021-11-29 12:06:09 +01:00
|
|
|
struct ovni_trace *trace;
|
|
|
|
|
2021-11-30 15:25:59 +01:00
|
|
|
parse_args(argc, argv);
|
|
|
|
|
2021-11-29 12:06:09 +01:00
|
|
|
trace = calloc(1, sizeof(struct ovni_trace));
|
|
|
|
|
|
|
|
if(trace == NULL)
|
|
|
|
{
|
|
|
|
perror("calloc");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ovni_load_trace(trace, tracedir))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if(ovni_load_streams(trace))
|
|
|
|
return 1;
|
|
|
|
|
2021-11-30 15:25:59 +01:00
|
|
|
if(process_trace(trace))
|
2021-12-09 16:41:56 +01:00
|
|
|
ret = 1;
|
2021-11-29 12:06:09 +01:00
|
|
|
|
|
|
|
ovni_free_streams(trace);
|
|
|
|
|
2021-12-15 18:29:29 +01:00
|
|
|
ovni_free_trace(trace);
|
|
|
|
|
2021-11-29 12:06:09 +01:00
|
|
|
free(trace);
|
|
|
|
|
2021-11-30 15:25:59 +01:00
|
|
|
return ret;
|
2021-11-29 12:06:09 +01:00
|
|
|
}
|