From 17994aabfaeff4ea2b80c6be8c379fc41e868b70 Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Fri, 8 Nov 2013 15:35:35 -0800 Subject: [PATCH] Add support for parsing a fips data file. The data file format is still in flux, so this code is expected to change as fips does. --- acre-x.c | 97 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 63 insertions(+), 34 deletions(-) diff --git a/acre-x.c b/acre-x.c index c375c36..bf70dd5 100644 --- a/acre-x.c +++ b/acre-x.c @@ -17,7 +17,11 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ +#include #include +#include +#include +#include #include #include @@ -25,50 +29,66 @@ #include "acre.h" #include "math.h" +#define STRNCMP_LITERAL(var, literal) \ + strncmp ((var), (literal), sizeof (literal) - 1) + static acre_t * -load_chart (void) +load_fips_data (const char *filename) { + FILE *file; acre_t *acre; - acre_data_t *data0, *data1, *data2; - int i; + acre_data_t *frame_time; + char *line = NULL, *s; + size_t line_size; + ssize_t bytes; + + file = fopen (filename, "r"); + if (file == NULL) { + fprintf (stderr, "Failed to open %s: %s\n", + filename, strerror (errno)); + return NULL; + } acre = acre_create (); - acre_set_x_axis_label (acre, "X axis"); - acre_set_y_axis_label (acre, "Y axis"); - data0 = acre_data_create (); - data1 = acre_data_create (); - data2 = acre_data_create (); + acre_set_title (acre, filename); + acre_set_x_axis_label (acre, "Frame #"); + acre_set_y_axis_label (acre, "Frame time (ms)"); - acre_data_set_name (data0, "Data 0"); - acre_data_set_name (data1, "Data 1"); - acre_data_set_name (data2, "Data 2"); + frame_time = acre_data_create (); - for (i = 0; i <= 100; i++) { - acre_data_add_point_2d (data0, i, 0 - (i/3.0)*(i/3.0)); - } + while (1) { + int scanned; - for (i = 0; i < 100; i++) { - double t = 1.0 - (i / 100.0); - acre_data_add_point_2d (data1, i, -1000 * (1.0 - t*t*t)); - } - - for (i = 0; i <= 1000; i++) { - double t, x, y; - t = i/10.0 - 50; - x = t + 50; - if (t == 0.0) - y = -200; - else - y = -1200 + 1000 * sin(t) / t; - acre_data_add_point_2d (data2, x, y); + bytes = getline (&line, &line_size, file); + if (bytes == -1) + break; + + s = line; + + if (STRNCMP_LITERAL (s, "frame-time: ") == 0) { + unsigned frame; + int64_t frame_time_ns; + + s += strlen("frame-time: "); + + scanned = sscanf (s, "%d %" SCNu64, + &frame, &frame_time_ns); + if (scanned != 2) { + fprintf (stderr, "Warning: Failed to parse line: %s\n", line); + continue; + } + + acre_data_add_point_2d (frame_time, frame, + frame_time_ns / 1e6); + } else { + /* Ignoring all other lines. */ + } } - acre_add_data (acre, data0); - acre_add_data (acre, data1); - acre_add_data (acre, data2); + free (line); - acre_set_title (acre, "All the data"); + acre_add_data (acre, frame_time); return acre; } @@ -180,7 +200,7 @@ handle_events(Display *dpy, Window window, Visual *visual, } int -main (void) +main (int argc, char *argv[]) { Display *dpy; Window window, root; @@ -194,7 +214,16 @@ main (void) int width = 800; int height = 600; - acre = load_chart (); + if (argc < 2) { + fprintf (stderr, "Usage: acre-x data-file\n"); + fprintf (stderr, "Where the data file is the output from fips (with frame-timing feature.\n"); + exit (1); + } + + acre = load_fips_data (argv[1]); + + if (acre == NULL) + return 1; dpy = XOpenDisplay (NULL); -- 2.43.0