Commit bfce523d authored by Jeroen Vreeken's avatar Jeroen Vreeken
Browse files

Port trace_view to the new trace interface

parent fc98f4ae
......@@ -27,200 +27,118 @@
#include <math.h>
#include <time.h>
#include "tcp_connect.h"
typedef union {
float f;
uint32_t u;
} float32_t;
#include "trace/trace.h"
time_t starttime;
float step = 1.0/10.0;
int interval = 100;
#define VAL_BUFLEN (60 * 10)
#define VAL_BUFLEN (60 * 5)
FILE *fdplot = NULL;
int doplot = 0;
struct trace {
int fd;
float val_buffer[VAL_BUFLEN];
int bufpos;
float bufpos_time;
struct trace_view {
struct trace *trace;
char *tmpfilename;
FILE *fdtmp;
char *trace_name;
char *trace_unit;
time_t seconds;
int sample;
int wasnan;
struct trace_value value[VAL_BUFLEN];
int bufpos;
};
struct trace *traces;
struct trace_view *traces;
void handle_val(struct trace * trace, float val, time_t seconds, int sample)
void plot_values(struct trace_view *traces, int nr_traces)
{
int i;
int bufpos = (traces[0].bufpos+VAL_BUFLEN-1) % VAL_BUFLEN;
long long now = traces[0].value[bufpos].t.tv_sec;
long nown = traces[0].value[bufpos].t.tv_nsec;
if (sample % interval == 0) {
trace->val_buffer[trace->bufpos] = val;
trace->bufpos_time =
(double)seconds + (double)sample / (double)interval * step;
freopen(NULL, "w", trace->fdtmp);
for (i = trace->bufpos + 1; i < VAL_BUFLEN; i++) {
if (isnan(trace->val_buffer[i]))
continue;
fprintf(trace->fdtmp, "%f %e\n",
(double)(i - trace->bufpos - VAL_BUFLEN)* step +
trace->bufpos_time - starttime,
trace->val_buffer[i]);
}
for (i = 0; i <= trace->bufpos; i++) {
if (isnan(trace->val_buffer[i]))
continue;
fprintf(trace->fdtmp, "%f %e\n",
(double)(i - trace->bufpos) * step + trace->bufpos_time
- starttime,
trace->val_buffer[i]);
}
fflush(trace->fdtmp);
trace->bufpos++;
if (trace->bufpos == VAL_BUFLEN)
trace->bufpos = 0;
doplot++;
}
}
int open_trace(struct trace *trace, char *tracename)
{
union {
char name[100];
uint32_t val32[25];
} buffer;
char prevname[100];
int len = 0;
int ret;
int fdmkstemp;
int unit = 0;
float period;
memset(trace, 0, sizeof(struct trace));
memset(trace->val_buffer, 0xff, sizeof(float)*VAL_BUFLEN);
trace->fd = tcp_connect("localhost", 10000);
if (trace->fd < 0) {
return 1;
}
trace->tmpfilename = malloc(100);
sprintf(trace->tmpfilename, "/tmp/trace_view_XXXXXX");
fdmkstemp = mkstemp(trace->tmpfilename);
fprintf(fdplot, "plot ");
printf("Going to use %s temp file for '%s' data\n",
trace->tmpfilename, tracename);
trace->fdtmp = fdopen(fdmkstemp, "w");
if (trace->fdtmp == NULL) {
perror("fopen() failed");
return 1;
for(i = 0; i < nr_traces; i++) {
if (i > 0)
fprintf(fdplot, ", ");
fprintf(fdplot, "'%s' using ($1-%lld.%09ld):($2) title '%s [%s]' with lines",
traces[i].tmpfilename,
now, nown,
traces[i].trace->name,
"");
}
fprintf(fdplot, "\n");
fflush(NULL);
do {
ret = read(trace->fd, buffer.name + len, 1);
if (ret == 1) {
if ((buffer.name[len] == 0 && unit != 2) ||
(len == 4 && unit == 2)) {
if (len == 0)
break;
switch (unit) {
case 0:
strcpy(prevname, buffer.name);
unit++;
break;
case 1:
if (!strcmp(prevname, tracename)) {
trace->trace_unit =
malloc(len + 1);
if (!trace->trace_unit)
return 1;
strcpy(trace->trace_unit, buffer.name);
}
unit++;
break;
case 2: {
float32_t f32;
f32.u = ntohl(*buffer.val32);
period = f32.f;
if (period < 0.1)
interval = (0.2 / period);
else
interval = 1;
unit = 0;
break;
}
}
len = -1;
}
len++;
if (len >= 100) {
len = 0;
}
}
} while (ret == 1);
write(trace->fd, tracename, strlen(tracename) + 1);
trace->trace_name = tracename;
return 0;
return;
}
int get_val(struct trace *trace)
static void print_value(FILE *fd,
struct trace *trace, struct trace_value *value)
{
int ret;
uint32_t nin;
float fin;
float32_t f32;
ret = read(trace->fd, &nin, sizeof(uint32_t));
f32.u = ntohl(nin);
fin = f32.f;
if (trace->wasnan) {
trace->seconds = ntohl(nin);
trace->sample = 0;
trace->wasnan = 0;
} else if (isnan(fin)) {
trace->wasnan = 1;
} else {
handle_val(trace, fin, trace->seconds, trace->sample);
trace->sample++;
switch (trace->type) {
case TRACE_VALUE_TYPE_FLOAT:
fprintf(fd, "%ld.%09ld %e\n",
value->t.tv_sec, value->t.tv_nsec, value->value.f);
break;
case TRACE_VALUE_TYPE_BOOL:
fprintf(fd, "%ld.%09ld %d\n",
value->t.tv_sec, value->t.tv_nsec, value->value.b);
break;
case TRACE_VALUE_TYPE_UINT8:
fprintf(fd, "%ld.%09ld %u\n",
value->t.tv_sec, value->t.tv_nsec, value->value.u8);
break;
case TRACE_VALUE_TYPE_UINT16:
fprintf(fd, "%ld.%09ld %u\n",
value->t.tv_sec, value->t.tv_nsec, value->value.u16);
break;
case TRACE_VALUE_TYPE_UINT32:
fprintf(fd, "%ld.%09ld %u\n",
value->t.tv_sec, value->t.tv_nsec, value->value.u32);
break;
case TRACE_VALUE_TYPE_SINT8:
fprintf(fd, "%ld.%09ld %d\n",
value->t.tv_sec, value->t.tv_nsec, value->value.s8);
break;
case TRACE_VALUE_TYPE_SINT16:
fprintf(fd, "%ld.%09ld %d\n",
value->t.tv_sec, value->t.tv_nsec, value->value.s16);
break;
case TRACE_VALUE_TYPE_SINT32:
fprintf(fd, "%ld.%09ld %d\n",
value->t.tv_sec, value->t.tv_nsec, value->value.s32);
break;
}
if (ret == sizeof(float))
return 0;
else
return -1;
}
void plot_values(struct trace *traces, int nr_traces)
static void handler_value(struct trace *trace,
struct trace_value *value)
{
struct trace_view *view;
int i;
fprintf(fdplot, "plot ");
view = trace->private;
view->value[view->bufpos] = *value;
for(i = 0; i < nr_traces; i++) {
if (i > 0)
fprintf(fdplot, ", ");
fprintf(fdplot, "'%s' using 1:2 title '%s [%s]' with lines",
traces[i].tmpfilename,
traces[i].trace_name,
traces[i].trace_unit);
freopen(NULL, "w", view->fdtmp);
for (i = view->bufpos + 1; i < VAL_BUFLEN; i++) {
if (!view->value[i].t.tv_sec)
continue;
print_value(view->fdtmp, trace, &view->value[i]);
}
fprintf(fdplot, "\n");
fflush(NULL);
for (i = 0; i <= view->bufpos; i++) {
if (!view->value[i].t.tv_sec)
continue;
return;
print_value(view->fdtmp, trace, &view->value[i]);
}
fflush(view->fdtmp);
view->bufpos++;
view->bufpos %= VAL_BUFLEN;
doplot++;
}
int main(int argc, char **argv)
......@@ -229,10 +147,44 @@ int main(int argc, char **argv)
starttime = time(NULL);
traces = malloc(sizeof(struct trace) * (argc - 1));
traces = calloc(sizeof(struct trace_view), argc - 1);
for (i = 0; i < argc - 1; i++) {
printf("%s\n", argv[i + 1]);
open_trace(&traces[i], argv[i + 1]);
char *tracename = argv[i+1];
struct trace_pkt *pkt;
struct timespec t_int;
int fdmkstemp;
printf("%s\n", tracename);
traces[i].trace = trace_open("localhost", 10000);
if (!traces[i].trace) {
printf("open trace failed\n");
return 1;
}
traces[i].trace->private = &traces[i];
trace_name_set(traces[i].trace, tracename);
traces[i].tmpfilename = malloc(100);
sprintf(traces[i].tmpfilename, "/tmp/trace_view_XXXXXX");
fdmkstemp = mkstemp(traces[i].tmpfilename);
printf("Going to use %s temp file for '%s' data\n",
traces[i].tmpfilename, tracename);
traces[i].fdtmp = fdopen(fdmkstemp, "w");
if (traces[i].fdtmp == NULL) {
perror("fopen() failed");
return 1;
}
t_int.tv_sec = 0;
t_int.tv_nsec = 200*1000*1000;
pkt = trace_packet_new();
trace_packet_interval_set(pkt,
&t_int, TRACE_INTERVAL_TYPE_INTERVAL);
trace_packet_write(traces[i].trace, pkt);
trace_packet_put(pkt);
traces[i].trace->handler_value = handler_value;
}
fdplot = popen("gnuplot -noraise", "w");
......@@ -244,16 +196,23 @@ int main(int argc, char **argv)
fprintf(fdplot, "set grid\n");
do {
for (i = 0; i < argc - 1; i++) {
ret = get_val(&traces[i]);
if (ret)
break;
}
if (ret == 0) {
if (doplot == argc - 1) {
plot_values(traces, argc - 1);
doplot = 0;
}
fd_set fdrx;
int high = 0;
FD_ZERO(&fdrx);
for (i = 0; i < argc - 1; i++)
trace_fd_set(traces[i].trace, &fdrx, &high);
select(high+1, &fdrx, NULL, NULL, NULL);
for (i = 0; i < argc - 1; i++)
trace_handle(traces[i].trace, &fdrx);
if (doplot >= argc - 1) {
plot_values(traces, argc - 1);
doplot = 0;
}
} while (ret == 0);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment