progress.con commit Start draft release notes for 1.6.0.3 (cc185a6)
   1/*
   2 * Simple text-based progress display module for GIT
   3 *
   4 * Copyright (c) 2007 by Nicolas Pitre <nico@cam.org>
   5 *
   6 * This code is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License version 2 as
   8 * published by the Free Software Foundation.
   9 */
  10
  11#include "git-compat-util.h"
  12#include "progress.h"
  13
  14#define TP_IDX_MAX      8
  15
  16struct throughput {
  17        off_t curr_total;
  18        off_t prev_total;
  19        struct timeval prev_tv;
  20        unsigned int avg_bytes;
  21        unsigned int avg_misecs;
  22        unsigned int last_bytes[TP_IDX_MAX];
  23        unsigned int last_misecs[TP_IDX_MAX];
  24        unsigned int idx;
  25        char display[32];
  26};
  27
  28struct progress {
  29        const char *title;
  30        int last_value;
  31        unsigned total;
  32        unsigned last_percent;
  33        unsigned delay;
  34        unsigned delayed_percent_treshold;
  35        struct throughput *throughput;
  36};
  37
  38static volatile sig_atomic_t progress_update;
  39
  40static void progress_interval(int signum)
  41{
  42        progress_update = 1;
  43}
  44
  45static void set_progress_signal(void)
  46{
  47        struct sigaction sa;
  48        struct itimerval v;
  49
  50        progress_update = 0;
  51
  52        memset(&sa, 0, sizeof(sa));
  53        sa.sa_handler = progress_interval;
  54        sigemptyset(&sa.sa_mask);
  55        sa.sa_flags = SA_RESTART;
  56        sigaction(SIGALRM, &sa, NULL);
  57
  58        v.it_interval.tv_sec = 1;
  59        v.it_interval.tv_usec = 0;
  60        v.it_value = v.it_interval;
  61        setitimer(ITIMER_REAL, &v, NULL);
  62}
  63
  64static void clear_progress_signal(void)
  65{
  66        struct itimerval v = {{0,},};
  67        setitimer(ITIMER_REAL, &v, NULL);
  68        signal(SIGALRM, SIG_IGN);
  69        progress_update = 0;
  70}
  71
  72static int display(struct progress *progress, unsigned n, const char *done)
  73{
  74        const char *eol, *tp;
  75
  76        if (progress->delay) {
  77                if (!progress_update || --progress->delay)
  78                        return 0;
  79                if (progress->total) {
  80                        unsigned percent = n * 100 / progress->total;
  81                        if (percent > progress->delayed_percent_treshold) {
  82                                /* inhibit this progress report entirely */
  83                                clear_progress_signal();
  84                                progress->delay = -1;
  85                                progress->total = 0;
  86                                return 0;
  87                        }
  88                }
  89        }
  90
  91        progress->last_value = n;
  92        tp = (progress->throughput) ? progress->throughput->display : "";
  93        eol = done ? done : "   \r";
  94        if (progress->total) {
  95                unsigned percent = n * 100 / progress->total;
  96                if (percent != progress->last_percent || progress_update) {
  97                        progress->last_percent = percent;
  98                        fprintf(stderr, "%s: %3u%% (%u/%u)%s%s",
  99                                progress->title, percent, n,
 100                                progress->total, tp, eol);
 101                        fflush(stderr);
 102                        progress_update = 0;
 103                        return 1;
 104                }
 105        } else if (progress_update) {
 106                fprintf(stderr, "%s: %u%s%s", progress->title, n, tp, eol);
 107                fflush(stderr);
 108                progress_update = 0;
 109                return 1;
 110        }
 111
 112        return 0;
 113}
 114
 115static void throughput_string(struct throughput *tp, off_t total,
 116                              unsigned int rate)
 117{
 118        int l = sizeof(tp->display);
 119        if (total > 1 << 30) {
 120                l -= snprintf(tp->display, l, ", %u.%2.2u GiB",
 121                              (int)(total >> 30),
 122                              (int)(total & ((1 << 30) - 1)) / 10737419);
 123        } else if (total > 1 << 20) {
 124                l -= snprintf(tp->display, l, ", %u.%2.2u MiB",
 125                              (int)(total >> 20),
 126                              ((int)(total & ((1 << 20) - 1)) * 100) >> 20);
 127        } else if (total > 1 << 10) {
 128                l -= snprintf(tp->display, l, ", %u.%2.2u KiB",
 129                              (int)(total >> 10),
 130                              ((int)(total & ((1 << 10) - 1)) * 100) >> 10);
 131        } else {
 132                l -= snprintf(tp->display, l, ", %u bytes", (int)total);
 133        }
 134        if (rate)
 135                snprintf(tp->display + sizeof(tp->display) - l, l,
 136                         " | %u KiB/s", rate);
 137}
 138
 139void display_throughput(struct progress *progress, off_t total)
 140{
 141        struct throughput *tp;
 142        struct timeval tv;
 143        unsigned int misecs;
 144
 145        if (!progress)
 146                return;
 147        tp = progress->throughput;
 148
 149        gettimeofday(&tv, NULL);
 150
 151        if (!tp) {
 152                progress->throughput = tp = calloc(1, sizeof(*tp));
 153                if (tp) {
 154                        tp->prev_total = tp->curr_total = total;
 155                        tp->prev_tv = tv;
 156                }
 157                return;
 158        }
 159        tp->curr_total = total;
 160
 161        /*
 162         * We have x = bytes and y = microsecs.  We want z = KiB/s:
 163         *
 164         *      z = (x / 1024) / (y / 1000000)
 165         *      z = x / y * 1000000 / 1024
 166         *      z = x / (y * 1024 / 1000000)
 167         *      z = x / y'
 168         *
 169         * To simplify things we'll keep track of misecs, or 1024th of a sec
 170         * obtained with:
 171         *
 172         *      y' = y * 1024 / 1000000
 173         *      y' = y / (1000000 / 1024)
 174         *      y' = y / 977
 175         */
 176        misecs = (tv.tv_sec - tp->prev_tv.tv_sec) * 1024;
 177        misecs += (int)(tv.tv_usec - tp->prev_tv.tv_usec) / 977;
 178
 179        if (misecs > 512) {
 180                unsigned int count, rate;
 181
 182                count = total - tp->prev_total;
 183                tp->prev_total = total;
 184                tp->prev_tv = tv;
 185                tp->avg_bytes += count;
 186                tp->avg_misecs += misecs;
 187                rate = tp->avg_bytes / tp->avg_misecs;
 188                tp->avg_bytes -= tp->last_bytes[tp->idx];
 189                tp->avg_misecs -= tp->last_misecs[tp->idx];
 190                tp->last_bytes[tp->idx] = count;
 191                tp->last_misecs[tp->idx] = misecs;
 192                tp->idx = (tp->idx + 1) % TP_IDX_MAX;
 193
 194                throughput_string(tp, total, rate);
 195                if (progress->last_value != -1 && progress_update)
 196                        display(progress, progress->last_value, NULL);
 197        }
 198}
 199
 200int display_progress(struct progress *progress, unsigned n)
 201{
 202        return progress ? display(progress, n, NULL) : 0;
 203}
 204
 205struct progress *start_progress_delay(const char *title, unsigned total,
 206                                       unsigned percent_treshold, unsigned delay)
 207{
 208        struct progress *progress = malloc(sizeof(*progress));
 209        if (!progress) {
 210                /* unlikely, but here's a good fallback */
 211                fprintf(stderr, "%s...\n", title);
 212                fflush(stderr);
 213                return NULL;
 214        }
 215        progress->title = title;
 216        progress->total = total;
 217        progress->last_value = -1;
 218        progress->last_percent = -1;
 219        progress->delayed_percent_treshold = percent_treshold;
 220        progress->delay = delay;
 221        progress->throughput = NULL;
 222        set_progress_signal();
 223        return progress;
 224}
 225
 226struct progress *start_progress(const char *title, unsigned total)
 227{
 228        return start_progress_delay(title, total, 0, 0);
 229}
 230
 231void stop_progress(struct progress **p_progress)
 232{
 233        stop_progress_msg(p_progress, "done");
 234}
 235
 236void stop_progress_msg(struct progress **p_progress, const char *msg)
 237{
 238        struct progress *progress = *p_progress;
 239        if (!progress)
 240                return;
 241        *p_progress = NULL;
 242        if (progress->last_value != -1) {
 243                /* Force the last update */
 244                char buf[128], *bufp;
 245                size_t len = strlen(msg) + 5;
 246                struct throughput *tp = progress->throughput;
 247
 248                bufp = (len < sizeof(buf)) ? buf : xmalloc(len + 1);
 249                if (tp) {
 250                        unsigned int rate = !tp->avg_misecs ? 0 :
 251                                        tp->avg_bytes / tp->avg_misecs;
 252                        throughput_string(tp, tp->curr_total, rate);
 253                }
 254                progress_update = 1;
 255                sprintf(bufp, ", %s.\n", msg);
 256                display(progress, progress->last_value, bufp);
 257                if (buf != bufp)
 258                        free(bufp);
 259        }
 260        clear_progress_signal();
 261        free(progress->throughput);
 262        free(progress);
 263}