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