Better max estimate for line graphs
[blktrace.git] / iowatcher / plot.c
index d59c5c75c8795621c1f238bf5f180f1c0a828e30..372406b47569c42f8e0e7f2e7ace07eb1c7d6a48 100644 (file)
@@ -12,7 +12,7 @@
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  *
  *  Parts of this file were imported from Jens Axboe's blktrace sources (also GPL)
  */
@@ -69,7 +69,67 @@ static char line[1024];
 static int final_height = 0;
 static int final_width = 0;
 
-struct graph_line_data *alloc_line_data(int seconds, int stop_seconds)
+static char *colors[] = {
+       "blue", "darkgreen",
+       "red",
+       "darkviolet",
+       "orange",
+       "aqua",
+       "brown", "#00FF00",
+       "yellow", "coral",
+       "black", "darkred",
+       "fuchsia", "crimson",
+       NULL };
+
+extern unsigned int longest_proc_name;
+
+char *pick_color(void)
+{
+       static int color_index;
+       char *ret = colors[color_index];
+
+       if (!ret) {
+               color_index = 0;
+               ret = colors[color_index];
+       }
+       color_index++;
+       return ret;
+}
+
+char *pick_fio_color(void)
+{
+       static int fio_color_index;
+       char *ret = colors[fio_color_index];
+
+       if (!ret) {
+               fio_color_index = 0;
+               ret = colors[fio_color_index];
+       }
+       fio_color_index += 2;
+       return ret;
+}
+
+static int cpu_color_index;
+
+char *pick_cpu_color(void)
+{
+       char *ret = colors[cpu_color_index];
+       if (!ret) {
+               cpu_color_index = 0;
+               ret = colors[cpu_color_index];
+       }
+       cpu_color_index++;
+       return ret;
+}
+
+void reset_cpu_color(void)
+{
+       cpu_color_index = 0;
+}
+
+struct graph_line_data *alloc_line_data(unsigned int min_seconds,
+                                       unsigned int max_seconds,
+                                       unsigned int stop_seconds)
 {
        int size = sizeof(struct graph_line_data) + (stop_seconds + 1) * sizeof(struct graph_line_pair);
        struct graph_line_data *gld;
@@ -79,18 +139,17 @@ struct graph_line_data *alloc_line_data(int seconds, int stop_seconds)
                fprintf(stderr, "Unable to allocate memory for graph data\n");
                exit(1);
        }
-       gld->seconds = seconds;
+       gld->min_seconds = min_seconds;
+       gld->max_seconds = max_seconds;
        gld->stop_seconds = stop_seconds;
        return gld;
 }
 
-void free_line_data(struct graph_line_data *gld)
-{
-       free(gld->label);
-       free(gld);
-}
-
-struct graph_dot_data *alloc_dot_data(int seconds, u64 max_offset, int stop_seconds)
+struct graph_dot_data *alloc_dot_data(unsigned int min_seconds,
+                                     unsigned int max_seconds,
+                                     u64 min_offset, u64 max_offset,
+                                     unsigned int stop_seconds,
+                                     char *color, char *label)
 {
        int size;
        int arr_size;
@@ -104,31 +163,33 @@ struct graph_dot_data *alloc_dot_data(int seconds, u64 max_offset, int stop_seco
        arr_size = (rows + 1) * cols;
 
        /* the number of bytes */
-       arr_size /= 8;
+       arr_size = (arr_size + 7) / 8;
 
        gdd = calloc(1, size + arr_size);
        if (!gdd) {
                fprintf(stderr, "Unable to allocate memory for graph data\n");
                exit(1);
        }
-       gdd->seconds = seconds;
+       gdd->min_seconds = min_seconds;
+       gdd->max_seconds = max_seconds;
        gdd->stop_seconds = stop_seconds;
        gdd->rows = rows;
        gdd->cols = cols;
+       gdd->min_offset = min_offset;
        gdd->max_offset = max_offset;
-       return gdd;
-}
+       gdd->color = color;
+       gdd->label = label;
 
-void free_dot_data(struct graph_dot_data *gdd)
-{
-       free(gdd);
+       if (strlen(label) > longest_proc_name)
+               longest_proc_name = strlen(label);
+
+       return gdd;
 }
 
 void set_gdd_bit(struct graph_dot_data *gdd, u64 offset, double bytes, double time)
 {
-       double bytes_per_row = (double)gdd->max_offset / gdd->rows;
-
-       double secs_per_col = (double)gdd->seconds / gdd->cols;
+       double bytes_per_row = (double)(gdd->max_offset - gdd->min_offset + 1) / gdd->rows;
+       double secs_per_col = (double)(gdd->max_seconds - gdd->min_seconds) / gdd->cols;
        double col;
        double row;
        int col_int;
@@ -138,14 +199,15 @@ void set_gdd_bit(struct graph_dot_data *gdd, u64 offset, double bytes, double ti
        int bit_mod;
        double mod = bytes_per_row;
 
-       if (offset > gdd->max_offset)
+       if (offset > gdd->max_offset || offset < gdd->min_offset)
                return;
-
-       gdd->total_ios++;
        time = time / 1000000000.0;
-       while (bytes > 0) {
-               row = (double)offset / bytes_per_row;
-               col = time / secs_per_col;
+       if (time < gdd->min_seconds || time > gdd->max_seconds)
+               return;
+       gdd->total_ios++;
+       while (bytes > 0 && offset <= gdd->max_offset) {
+               row = (double)(offset - gdd->min_offset) / bytes_per_row;
+               col = (time - gdd->min_seconds) / secs_per_col;
 
                col_int = floor(col);
                row_int = floor(row);
@@ -159,31 +221,6 @@ void set_gdd_bit(struct graph_dot_data *gdd, u64 offset, double bytes, double ti
        }
 }
 
-void print_gdd(struct graph_dot_data *gdd)
-{
-       int col = 0;
-       int row = 0;
-       int arr_index;
-       u64 val;
-       int bit_index;
-       int bit_mod;
-
-       for (row = gdd->rows - 1; row >= 0; row--) {
-               for (col = 0; col < gdd->cols; col++) {
-                       bit_index = row * gdd->cols + col;
-                       arr_index = bit_index / sizeof(unsigned long);
-                       bit_mod = bit_index % sizeof(unsigned long);
-
-                       val = gdd->data[arr_index];
-                       if (val & (1 << bit_mod))
-                               printf("*");
-                       else
-                               printf(" ");
-               }
-               printf("\n");
-       }
-}
-
 static double rolling_avg(struct graph_line_pair *data, int index, int distance)
 {
        double sum = 0;
@@ -212,6 +249,20 @@ static double rolling_avg(struct graph_line_pair *data, int index, int distance)
        return sum / distance;
 }
 
+static void write_check(int fd, char *buf, size_t size)
+{
+       ssize_t ret;
+
+       ret = write(fd, buf, size);
+       if (ret != (ssize_t)size) {
+               if (ret < 0)
+                       perror("write failed");
+               else
+                       fprintf(stderr, "error: short write\n");
+               exit(1);
+       }
+}
+
 void write_svg_header(int fd)
 {
        char *spaces = "                                                    \n";
@@ -238,42 +289,17 @@ void write_svg_header(int fd)
        final_width = 0;
        final_height = 0;
 
-       write(fd, header, strlen(header));
+       write_check(fd, header, strlen(header));
        /* write a bunch of spaces so we can stuff in the width and height later */
-       write(fd, spaces, strlen(spaces));
-
-       write(fd, defs_start, strlen(defs_start));
-       write(fd, filter1, strlen(filter1));
-       write(fd, filter2, strlen(filter2));
-       write(fd, filter3, strlen(filter3));
-       write(fd, defs_close, strlen(defs_close));
-}
-
-void write_drop_shadow(struct plot *plot)
-{
-       snprintf(line, line_len, "<rect x=\"0\" y=\"%d\" width=\"%d\" height=\"%d\" fill=\"white\"/>\n",
-                plot->start_y_offset, plot->total_width, 45);
-       write(plot->fd, line, strlen(line));
-
-       snprintf(line, line_len, "<path d=\"M %d %d h %d v %d h %d t %d %d V %d H %d Z\" "
-                "fill=\"white\" filter=\"url(#shadow)\"/>",
-               0, plot->start_y_offset,
-               plot->total_width - graph_left_pad / 2,
-               -plot->total_height, 24, 1, 1,
-               plot->start_y_offset + 10, 0);
-       write(plot->fd, line, strlen(line));
-
-       snprintf(line, line_len, "<path d=\"M %d %d H %d V %d h %d V %d H %d Z\" "
-                "fill=\"white\"/>",
-               0, plot->start_y_offset - 15, /* start */
-               plot->total_width - graph_left_pad / 2 - 10, /* hline over */
-               plot->start_y_offset - plot->total_height, /* vline up */
-               15, /*hline over */
-               plot->start_y_offset, /* vline back down */
-               0);
-       write(plot->fd, line, strlen(line));
-
-       plot->start_y_offset += 45;
+       write_check(fd, spaces, strlen(spaces));
+       write_check(fd, spaces, strlen(spaces));
+       write_check(fd, spaces, strlen(spaces));
+
+       write_check(fd, defs_start, strlen(defs_start));
+       write_check(fd, filter1, strlen(filter1));
+       write_check(fd, filter2, strlen(filter2));
+       write_check(fd, filter3, strlen(filter3));
+       write_check(fd, defs_close, strlen(defs_close));
 }
 
 /* svg y offset for the traditional 0,0 (bottom left corner) of the plot */
@@ -310,7 +336,6 @@ static int axis_x_off(int x)
        return (int)axis_x_off_double(x);
 }
 
-
 /*
  * this draws a backing rectangle for the plot and it
  * also creates a new svg element so our offsets can
@@ -318,52 +343,54 @@ static int axis_x_off(int x)
  */
 void setup_axis(struct plot *plot)
 {
-       int ret;
        int len;
        int fd = plot->fd;
        int bump_height = tick_font_size * 3 + axis_label_font_size;
+       int local_legend_width = legend_width;
 
-       plot->total_width = axis_x_off(graph_width) + graph_left_pad / 2 + legend_width;
+       if (plot->no_legend)
+               local_legend_width = 0;
+
+       plot->total_width = axis_x_off(graph_width) + graph_left_pad / 2 + local_legend_width;
        plot->total_height = axis_y() + tick_label_pad + tick_font_size;
 
        if (plot->add_xlabel)
                plot->total_height += bump_height;
 
        /* backing rect */
-       snprintf(line, line_len, "<rect x=\"0\" y=\"%d\" width=\"%d\" "
+       snprintf(line, line_len, "<rect x=\"%d\" y=\"%d\" width=\"%d\" "
                 "height=\"%d\" fill=\"white\" stroke=\"none\"/>",
+                plot->start_x_offset,
                plot->start_y_offset, plot->total_width + 40,
                plot->total_height + 20);
        len = strlen(line);
-       write(fd, line, len);
+       write_check(fd, line, len);
 
-       snprintf(line, line_len, "<rect x=\"15\" y=\"%d\" width=\"%d\" "
+       snprintf(line, line_len, "<rect x=\"%d\" y=\"%d\" width=\"%d\" "
                 "filter=\"url(#shadow)\" "
                 "height=\"%d\" fill=\"white\" stroke=\"none\"/>",
+                plot->start_x_offset + 15,
                plot->start_y_offset, plot->total_width, plot->total_height);
        len = strlen(line);
-       write(fd, line, len);
+       write_check(fd, line, len);
        plot->total_height += 20;
+       plot->total_width += 20;
 
        if (plot->total_height + plot->start_y_offset > final_height)
                final_height = plot->total_height + plot->start_y_offset;
-       if (plot->total_width + 40 > final_width)
-               final_width = plot->total_width + 40;
+       if (plot->start_x_offset + plot->total_width + 40 > final_width)
+               final_width = plot->start_x_offset + plot->total_width + 40;
 
        /* create an svg object for all our coords to be relative against */
        snprintf(line, line_len, "<svg x=\"%d\" y=\"%d\">\n", plot->start_x_offset, plot->start_y_offset);
-       write(fd, line, strlen(line));
+       write_check(fd, line, strlen(line));
 
        snprintf(line, 1024, "<path d=\"M%d %d h %d V %d H %d Z\" stroke=\"black\" stroke-width=\"2\" fill=\"none\"/>\n",
                 axis_x(), axis_y(),
                 graph_width + graph_inner_x_margin * 2, axis_y_off(graph_height) - graph_inner_y_margin,
                 axis_x());
        len = strlen(line);
-       ret = write(fd, line, len);
-       if (ret != len) {
-               fprintf(stderr, "failed to write svg axis\n");
-               exit(1);
-       }
+       write_check(fd, line, len);
 }
 
 /*
@@ -377,36 +404,41 @@ void setup_axis_spindle(struct plot *plot)
        int fd = plot->fd;
        int bump_height = tick_font_size * 3 + axis_label_font_size;
 
-       plot->total_width = axis_x_off(graph_width) + graph_left_pad / 2 + legend_width;
+       legend_x_off = -60;
+
+       plot->total_width = axis_x_off(graph_width) + legend_width;
        plot->total_height = axis_y() + tick_label_pad + tick_font_size;
 
        if (plot->add_xlabel)
                plot->total_height += bump_height;
 
        /* backing rect */
-       snprintf(line, line_len, "<rect x=\"0\" y=\"%d\" width=\"%d\" "
+       snprintf(line, line_len, "<rect x=\"%d\" y=\"%d\" width=\"%d\" "
                 "height=\"%d\" fill=\"white\" stroke=\"none\"/>",
-               plot->start_y_offset, plot->total_width + 40,
+                plot->start_x_offset,
+               plot->start_y_offset, plot->total_width + 10,
                plot->total_height + 20);
        len = strlen(line);
-       write(fd, line, len);
+       write_check(fd, line, len);
 
-       snprintf(line, line_len, "<rect x=\"15\" y=\"%d\" width=\"%d\" "
+       snprintf(line, line_len, "<rect x=\"%d\" y=\"%d\" width=\"%d\" "
                 "filter=\"url(#shadow)\" "
                 "height=\"%d\" fill=\"white\" stroke=\"none\"/>",
-               plot->start_y_offset, plot->total_width, plot->total_height);
+                plot->start_x_offset + 15,
+               plot->start_y_offset, plot->total_width - 30,
+               plot->total_height);
        len = strlen(line);
-       write(fd, line, len);
+       write_check(fd, line, len);
        plot->total_height += 20;
 
        if (plot->total_height + plot->start_y_offset > final_height)
                final_height = plot->total_height + plot->start_y_offset;
-       if (plot->total_width + 40 > final_width)
-               final_width = plot->total_width + 40;
+       if (plot->start_x_offset + plot->total_width + 40 > final_width)
+               final_width = plot->start_x_offset + plot->total_width + 40;
 
        /* create an svg object for all our coords to be relative against */
        snprintf(line, line_len, "<svg x=\"%d\" y=\"%d\">\n", plot->start_x_offset, plot->start_y_offset);
-       write(fd, line, strlen(line));
+       write_check(fd, line, strlen(line));
 
 }
 
@@ -428,7 +460,7 @@ void set_plot_title(struct plot *plot, char *title)
        snprintf(line, line_len, "<rect x=\"0\" y=\"%d\" width=\"%d\" height=\"%d\" fill=\"white\" stroke=\"none\"/>",
                plot->start_y_offset, plot->total_width + 40, plot_title_height + 20);
        len = strlen(line);
-       write(fd, line, len);
+       write_check(fd, line, len);
 
        snprintf(line, line_len, "<text x=\"%d\" y=\"%d\" font-family=\"%s\" font-size=\"%d\" "
                 "font-weight=\"bold\" fill=\"black\" style=\"text-anchor: %s\">%s</text>\n",
@@ -437,7 +469,29 @@ void set_plot_title(struct plot *plot, char *title)
                font_family, plot_title_font_size, "middle", title);
        plot->start_y_offset += plot_title_height;
        len = strlen(line);
-       write(fd, line, len);
+       write_check(fd, line, len);
+}
+
+#define TICK_MINI_STEPS 3
+
+static double find_step(double first, double last, int num_ticks)
+{
+       int mini_step[TICK_MINI_STEPS] = { 1, 2, 5 };
+       int cur_mini_step = 0;
+       double step = (last - first) / num_ticks;
+       double log10 = log(10);
+
+       /* Round to power of 10 */
+       step = exp(floor(log(step) / log10) * log10);
+       /* Scale down step to provide enough ticks */
+       while (cur_mini_step < TICK_MINI_STEPS
+              && (last - first) / (step * mini_step[cur_mini_step]) > num_ticks)
+               cur_mini_step++;
+
+       if (cur_mini_step > 0)
+               step *= mini_step[cur_mini_step - 1];
+
+       return step;
 }
 
 /*
@@ -446,8 +500,8 @@ void set_plot_title(struct plot *plot, char *title)
  */
 void set_xticks(struct plot *plot, int num_ticks, int first, int last)
 {
-       int pixels_per_tick = graph_width / num_ticks;
-       int step = (last - first) / num_ticks;
+       int pixels_per_tick;
+       double step;
        int i;
        int tick_y = axis_y_off(graph_tick_len) + graph_inner_y_margin;
        int tick_x = axis_x();
@@ -458,32 +512,53 @@ void set_xticks(struct plot *plot, int num_ticks, int first, int last)
        char *middle = "middle";
        char *start = "start";
 
+       step = find_step(first, last, num_ticks);
+       /*
+        * We don't want last two ticks to be too close together so subtract
+        * 20% of the step from the interval
+        */
+       num_ticks = (double)(last - first - step) / step + 1;
+       pixels_per_tick = graph_width * step / (double)(last - first);
+
        for (i = 0; i < num_ticks; i++) {
                char *anchor;
                if (i != 0) {
                        snprintf(line, line_len, "<rect x=\"%d\" y=\"%d\" width=\"2\" height=\"%d\" style=\"stroke:none;fill:black;\"/>\n",
                                tick_x, tick_y, graph_tick_len);
-                       write(plot->fd, line, strlen(line));
+                       write_check(plot->fd, line, strlen(line));
                        anchor = middle;
                } else {
                        anchor = start;
                }
 
                if (!tick_only) {
-                       snprintf(line, line_len, "<text x=\"%d\" y=\"%d\" font-family=\"%s\" font-size=\"%d\" "
-                               "fill=\"black\" style=\"text-anchor: %s\">%d</text>\n",
-                               tick_x, text_y, font_family, tick_font_size, anchor, step * i);
-                       write(plot->fd, line, strlen(line));
+                       if (step >= 1)
+                               snprintf(line, line_len, "<text x=\"%d\" y=\"%d\" font-family=\"%s\" font-size=\"%d\" "
+                                       "fill=\"black\" style=\"text-anchor: %s\">%d</text>\n",
+                                       tick_x, text_y, font_family, tick_font_size, anchor,
+                                       (int)(first + step * i));
+                       else
+                               snprintf(line, line_len, "<text x=\"%d\" y=\"%d\" font-family=\"%s\" font-size=\"%d\" "
+                                       "fill=\"black\" style=\"text-anchor: %s\">%.2f</text>\n",
+                                       tick_x, text_y, font_family, tick_font_size, anchor,
+                                       first + step * i);
+                       write_check(plot->fd, line, strlen(line));
                }
                tick_x += pixels_per_tick;
        }
 
        if (!tick_only) {
-               snprintf(line, line_len, "<text x=\"%d\" y=\"%d\" font-family=\"%s\" font-size=\"%d\" "
-                       "fill=\"black\" style=\"text-anchor: middle\">%d</text>\n",
-                       axis_x_off(graph_width - 2),
-                       text_y, font_family, tick_font_size, last);
-               write(plot->fd, line, strlen(line));
+               if (step >= 1)
+                       snprintf(line, line_len, "<text x=\"%d\" y=\"%d\" font-family=\"%s\" font-size=\"%d\" "
+                               "fill=\"black\" style=\"text-anchor: middle\">%d</text>\n",
+                               axis_x_off(graph_width - 2),
+                               text_y, font_family, tick_font_size, last);
+               else
+                       snprintf(line, line_len, "<text x=\"%d\" y=\"%d\" font-family=\"%s\" font-size=\"%d\" "
+                               "fill=\"black\" style=\"text-anchor: middle\">%.2f</text>\n",
+                               axis_x_off(graph_width - 2),
+                               text_y, font_family, tick_font_size, (double)last);
+               write_check(plot->fd, line, strlen(line));
        }
 }
 
@@ -502,7 +577,7 @@ void set_ylabel(struct plot *plot, char *label)
                 (int)axis_y_off(graph_height / 2),
                 axis_label_font_size, "middle", label);
        len = strlen(line);
-       write(fd, line, len);
+       write_check(fd, line, len);
 }
 
 void set_xlabel(struct plot *plot, char *label)
@@ -517,7 +592,7 @@ void set_xlabel(struct plot *plot, char *label)
                 font_family,
                 axis_label_font_size, "middle", label);
        len = strlen(line);
-       write(fd, line, len);
+       write_check(fd, line, len);
 
 }
 
@@ -541,21 +616,21 @@ void set_yticks(struct plot *plot, int num_ticks, int first, int last, char *uni
                                 "style=\"stroke:lightgray;stroke-width:2;stroke-dasharray:9,12;\"/>\n",
                                tick_x, axis_y_off(tick_y),
                                axis_x_off(graph_width), axis_y_off(tick_y));
-                       write(plot->fd, line, strlen(line));
+                       write_check(plot->fd, line, strlen(line));
                }
 
                snprintf(line, line_len, "<text x=\"%d\" y=\"%d\" font-family=\"%s\" font-size=\"%d\" "
                         "fill=\"black\" style=\"text-anchor: %s\">%d%s</text>\n",
                        text_x,
                        axis_y_off(tick_y - tick_font_size / 2),
-                       font_family, tick_font_size, anchor, step * i, units);
-               write(plot->fd, line, strlen(line));
+                       font_family, tick_font_size, anchor, first + step * i, units);
+               write_check(plot->fd, line, strlen(line));
                tick_y += pixels_per_tick;
        }
        snprintf(line, line_len, "<text x=\"%d\" y=\"%d\" font-family=\"%s\" font-size=\"%d\" "
                 "fill=\"black\" style=\"text-anchor: %s\">%d%s</text>\n",
                 text_x, axis_y_off(graph_height), font_family, tick_font_size, anchor, last, units);
-       write(plot->fd, line, strlen(line));
+       write_check(plot->fd, line, strlen(line));
 }
 
 void set_plot_label(struct plot *plot, char *label)
@@ -569,21 +644,23 @@ void set_plot_label(struct plot *plot, char *label)
                 plot_label_height / 2,
                font_family, plot_label_font_size, "middle", label);
        len = strlen(line);
-       write(fd, line, len);
+       write_check(fd, line, len);
 }
 
 static void close_svg(int fd)
 {
        char *close_line = "</svg>\n";
 
-       write(fd, close_line, strlen(close_line));
+       write_check(fd, close_line, strlen(close_line));
 }
 
 int close_plot(struct plot *plot)
 {
        close_svg(plot->fd);
-       plot->start_y_offset += plot->total_height;
-       plot->add_xlabel = 0;
+       if (plot->direction == PLOT_DOWN)
+               plot->start_y_offset += plot->total_height;
+       else if (plot->direction == PLOT_ACROSS)
+               plot->start_x_offset += plot->total_width;
        return 0;
 }
 
@@ -612,7 +689,10 @@ int close_plot_file(struct plot *plot)
        snprintf(line, line_len, "<svg  xmlns=\"http://www.w3.org/2000/svg\" "
                 "width=\"%d\" height=\"%d\">\n",
                 final_width, final_height);
-       write(plot->fd, line, strlen(line));
+       write_check(plot->fd, line, strlen(line));
+       snprintf(line, line_len, "<rect x=\"0\" y=\"0\" width=\"%d\" "
+                "height=\"%d\" fill=\"white\"/>\n", final_width, final_height);
+       write_check(plot->fd, line, strlen(line));
        close(plot->fd);
        plot->fd = 0;
        return 0;
@@ -679,16 +759,39 @@ void scale_line_graph_time(u64 *max, char **units)
        *max /= div;
 }
 
+static int rolling_span(struct graph_line_data *gld)
+{
+       if (rolling_avg_secs)
+               return rolling_avg_secs;
+       return (gld->stop_seconds - gld->min_seconds) / 25;
+}
+
+
+double line_graph_roll_avg_max(struct graph_line_data *gld)
+{
+       unsigned int i;
+       int rolling;
+       double avg, max = 0;
+
+       rolling = rolling_span(gld);
+       for (i = gld->min_seconds; i < gld->stop_seconds; i++) {
+               avg = rolling_avg(gld->data, i, rolling);
+               if (avg > max)
+                       max = avg;
+       }
+       return max;
+}
+
 int svg_line_graph(struct plot *plot, struct graph_line_data *gld, char *color, int thresh1, int thresh2)
 {
-       int i;
+       unsigned int i;
        double val;
        double avg;
        int rolling;
        int fd = plot->fd;
        char *start = "<path d=\"";
        double yscale = ((double)gld->max) / graph_height;
-       double xscale = (double)(gld->seconds - 1) / graph_width;
+       double xscale = (double)(gld->max_seconds - gld->min_seconds - 1) / graph_width;
        char c = 'M';
        double x;
        int printed_header = 0;
@@ -696,12 +799,10 @@ int svg_line_graph(struct plot *plot, struct graph_line_data *gld, char *color,
 
        if (thresh1 && thresh2)
                rolling = 0;
-       else if (rolling_avg_secs)
-               rolling = rolling_avg_secs;
        else
-               rolling = gld->stop_seconds / 25;
+               rolling = rolling_span(gld);
 
-       for (i = 0; i < gld->stop_seconds; i++) {
+       for (i = gld->min_seconds; i < gld->stop_seconds; i++) {
                avg = rolling_avg(gld->data, i, rolling);
                if (yscale == 0)
                        val = 0;
@@ -713,23 +814,22 @@ int svg_line_graph(struct plot *plot, struct graph_line_data *gld, char *color,
                if (val < 0)
                        val = 0;
 
-               x = (double)i / xscale;
+               x = (double)(i - gld->min_seconds) / xscale;
                if (!thresh1 && !thresh2) {
-
                        if (!printed_header) {
-                               write(fd, start, strlen(start));
+                               write_check(fd, start, strlen(start));
                                printed_header = 1;
                        }
 
                        /* in full line mode, everything in the graph is connected */
                        snprintf(line, line_len, "%c %d %d ", c, axis_x_off(x), axis_y_off(val));
                        c = 'L';
-                       write(fd, line, strlen(line));
+                       write_check(fd, line, strlen(line));
                        printed_lines = 1;
                } else if (avg > thresh1 || avg > thresh2) {
                        int len = 10;
                        if (!printed_header) {
-                               write(fd, start, strlen(start));
+                               write_check(fd, start, strlen(start));
                                printed_header = 1;
                        }
 
@@ -743,15 +843,17 @@ int svg_line_graph(struct plot *plot, struct graph_line_data *gld, char *color,
                         */
                        snprintf(line, line_len, "M %d %d h %d ", axis_x_off(x),
                                 axis_y_off(val), len);
-                       write(fd, line, strlen(line));
+                       write_check(fd, line, strlen(line));
                        printed_lines = 1;
                }
 
        }
        if (printed_lines) {
                snprintf(line, line_len, "\" fill=\"none\" stroke=\"%s\" stroke-width=\"2\"/>\n", color);
-               write(fd, line, strlen(line));
+               write_check(fd, line, strlen(line));
        }
+       if (plot->timeline)
+               svg_write_time_line(plot, plot->timeline);
 
        return 0;
 }
@@ -762,31 +864,31 @@ void svg_write_time_line(struct plot *plot, int col)
                                 "style=\"stroke:black;stroke-width:2;\"/>\n",
                                 axis_x_off(col), axis_y_off(0),
                                 axis_x_off(col), axis_y_off(graph_height));
-       write(plot->fd, line, strlen(line));
+       write_check(plot->fd, line, strlen(line));
 }
 
-static int svg_add_io(int fd, double row, double col, double width, double height, char *color)
+static void svg_add_io(int fd, double row, double col, double width, double height, char *color)
 {
        float rx = 0;
 
        snprintf(line, line_len, "<rect x=\"%.2f\" y=\"%.2f\" width=\"%.1f\" height=\"%.1f\" "
                 "rx=\"%.2f\" style=\"stroke:none;fill:%s;stroke-width:0\"/>\n",
                 axis_x_off_double(col), axis_y_off_double(row), width, height, rx, color);
-       return write(fd, line, strlen(line));
+       write_check(fd, line, strlen(line));
 }
 
-int svg_io_graph_movie_array(struct plot *plot, struct plot_history *ph)
+int svg_io_graph_movie_array(struct plot *plot, struct pid_plot_history *pph)
 {
        double cell_index;
        double movie_row;
        double movie_col;
        int i;
 
-       for (i = 0; i < ph->num_used; i++) {
-               cell_index = ph->history[i];
+       for (i = 0; i < pph->num_used; i++) {
+               cell_index = pph->history[i];
                movie_row = floor(cell_index / graph_width);
                movie_col = cell_index - movie_row * graph_width;
-               svg_add_io(plot->fd, movie_row, movie_col, 4, 4, ph->color);
+               svg_add_io(plot->fd, movie_row, movie_col, 4, 4, pph->color);
        }
        return 0;
 }
@@ -798,7 +900,7 @@ void rewind_spindle_steps(int num)
        spindle_steps -= num * 0.01;
 }
 
-int svg_io_graph_movie_array_spindle(struct plot *plot, struct plot_history *ph)
+int svg_io_graph_movie_array_spindle(struct plot *plot, struct pid_plot_history *pph)
 {
        double cell_index;
        int i;
@@ -828,21 +930,21 @@ int svg_io_graph_movie_array_spindle(struct plot *plot, struct plot_history *ph)
                 "<circle cx=\"%.2f\" cy=\"%.2f\" "
                 "stroke=\"black\" stroke-width=\"6\" "
                 "r=\"%.2f\" fill=\"none\"/>\n",
-                -spindle_steps * 1.2, center_x, center_y, center_x, center_y, graph_width_extra / 2);
-       write(plot->fd, line, strlen(line));
+                spindle_steps * 1.2, center_x, center_y, center_x, center_y, graph_width_extra / 2);
+       write_check(plot->fd, line, strlen(line));
        snprintf(line, line_len, "<circle cx=\"%.2f\" cy=\"%.2f\" "
                "stroke=\"none\" fill=\"red\" r=\"%.2f\"/>\n</g>\n",
                axis_x_off_double(graph_width_extra), center_y, 4.5);
-       write(plot->fd, line, strlen(line));
+       write_check(plot->fd, line, strlen(line));
        spindle_steps += 0.01;
 
        radius = floor(radius / 2);
        num_circles = radius / 4 - 3;
-       cells_per_circle = ph->history_max / num_circles;
+       cells_per_circle = pph->history_max / num_circles;
        degrees_per_cell = 360 / cells_per_circle;
 
-       for (i = 0; i < ph->num_used; i++) {
-               cell_index = ph->history[i];
+       for (i = 0; i < pph->num_used; i++) {
+               cell_index = pph->history[i];
                circle_num = floor(cell_index / cells_per_circle);
                rot = cell_index - circle_num * cells_per_circle;
                circle_num = num_circles - circle_num;
@@ -853,44 +955,44 @@ int svg_io_graph_movie_array_spindle(struct plot *plot, struct plot_history *ph)
                snprintf(line, line_len, "<path transform=\"rotate(%.4f, %.2f, %.2f)\" "
                         "d=\"M %.2f %.2f a %.2f %.2f 0 0 1 0 5\" "
                         "stroke=\"%s\" stroke-width=\"4\"/>\n",
-                        rot, center_x, center_y,
+                        -rot, center_x, center_y,
                         axis_x_off_double(graph_width_extra / 2 + radius) + 8, center_y,
-                        radius, radius, ph->color);
+                        radius, radius, pph->color);
 
-               write(plot->fd, line, strlen(line));
+               write_check(plot->fd, line, strlen(line));
        }
        return 0;
 }
 
-static int add_plot_history(struct plot_history *ph, double val)
+static int add_plot_history(struct pid_plot_history *pph, double val)
 {
-       if (ph->num_used == ph->history_len) {
-               ph->history = realloc(ph->history,
-                                     (ph->history_len + 4096) * sizeof(double));
-               if (!ph->history) {
+       if (pph->num_used == pph->history_len) {
+               pph->history_len += 4096;
+               pph->history = realloc(pph->history,
+                                      pph->history_len * sizeof(double));
+               if (!pph->history) {
                        perror("Unable to allocate memory");
                        exit(1);
                }
-               ph->history_len += 4096;
        }
-       ph->history[ph->num_used++] = val;
+       pph->history[pph->num_used++] = val;
        return 0;
 }
 
-int svg_io_graph_movie(struct graph_dot_data *gdd, struct plot_history *ph, int col)
+int svg_io_graph_movie(struct graph_dot_data *gdd, struct pid_plot_history *pph, int col)
 {
        int row = 0;
        int arr_index;
        unsigned char val;
        int bit_index;
        int bit_mod;
-       double blocks_per_row = gdd->max_offset / gdd->rows;
-       double movie_blocks_per_cell = gdd->max_offset / (graph_width * graph_height);
+       double blocks_per_row = (gdd->max_offset - gdd->min_offset + 1) / gdd->rows;
+       double movie_blocks_per_cell = (gdd->max_offset - gdd->min_offset + 1) / (graph_width * graph_height);
        double cell_index;
        int margin_orig = graph_inner_y_margin;
 
        graph_inner_y_margin += 5;
-       ph->history_max = gdd->max_offset / movie_blocks_per_cell;
+       pph->history_max = (gdd->max_offset - gdd->min_offset + 1) / movie_blocks_per_cell;
 
        for (row = gdd->rows - 1; row >= 0; row--) {
                bit_index = row * gdd->cols + col;
@@ -907,14 +1009,14 @@ int svg_io_graph_movie(struct graph_dot_data *gdd, struct plot_history *ph, int
                        /* a cell number in the graph */
                        cell_index /= movie_blocks_per_cell;
 
-                       add_plot_history(ph, cell_index);
+                       add_plot_history(pph, cell_index);
                }
        }
        graph_inner_y_margin = margin_orig;
        return 0;
 }
 
-int svg_io_graph(struct plot *plot, struct graph_dot_data *gdd, char *color)
+int svg_io_graph(struct plot *plot, struct graph_dot_data *gdd)
 {
        int fd = plot->fd;;
        int col = 0;
@@ -934,7 +1036,7 @@ int svg_io_graph(struct plot *plot, struct graph_dot_data *gdd, char *color)
                                continue;
                        val = gdd->data[arr_index];
                        if (val & (1 << bit_mod))
-                               svg_add_io(fd, floor(row / io_graph_scale), col, 1.5, 1.5, color);
+                               svg_add_io(fd, floor(row / io_graph_scale), col, 1.5, 1.5, gdd->color);
                }
        }
        return 0;
@@ -974,9 +1076,9 @@ void svg_write_legend(struct plot *plot)
                 legend_width,
                 plot->legend_index * legend_font_size + legend_font_size / 2 + 12);
 
-       write(plot->fd, line, strlen(line));
+       write_check(plot->fd, line, strlen(line));
        for (i = 0; i < plot->legend_index; i++) {
-               write(plot->fd, plot->legend_lines[i],
+               write_check(plot->fd, plot->legend_lines[i],
                      strlen(plot->legend_lines[i]));
                free(plot->legend_lines[i]);
        }