progress.con commit Merge branch 'rs/parse-tree-indirect' (4304395)
   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 "cache.h"
  12#include "gettext.h"
  13#include "progress.h"
  14#include "strbuf.h"
  15#include "trace.h"
  16#include "utf8.h"
  17
  18#define TP_IDX_MAX      8
  19
  20struct throughput {
  21        off_t curr_total;
  22        off_t prev_total;
  23        uint64_t prev_ns;
  24        unsigned int avg_bytes;
  25        unsigned int avg_misecs;
  26        unsigned int last_bytes[TP_IDX_MAX];
  27        unsigned int last_misecs[TP_IDX_MAX];
  28        unsigned int idx;
  29        struct strbuf display;
  30};
  31
  32struct progress {
  33        const char *title;
  34        uint64_t last_value;
  35        uint64_t total;
  36        unsigned last_percent;
  37        unsigned delay;
  38        unsigned sparse;
  39        struct throughput *throughput;
  40        uint64_t start_ns;
  41        struct strbuf counters_sb;
  42        int title_len;
  43        int split;
  44};
  45
  46static volatile sig_atomic_t progress_update;
  47
  48static void progress_interval(int signum)
  49{
  50        progress_update = 1;
  51}
  52
  53static void set_progress_signal(void)
  54{
  55        struct sigaction sa;
  56        struct itimerval v;
  57
  58        progress_update = 0;
  59
  60        memset(&sa, 0, sizeof(sa));
  61        sa.sa_handler = progress_interval;
  62        sigemptyset(&sa.sa_mask);
  63        sa.sa_flags = SA_RESTART;
  64        sigaction(SIGALRM, &sa, NULL);
  65
  66        v.it_interval.tv_sec = 1;
  67        v.it_interval.tv_usec = 0;
  68        v.it_value = v.it_interval;
  69        setitimer(ITIMER_REAL, &v, NULL);
  70}
  71
  72static void clear_progress_signal(void)
  73{
  74        struct itimerval v = {{0,},};
  75        setitimer(ITIMER_REAL, &v, NULL);
  76        signal(SIGALRM, SIG_IGN);
  77        progress_update = 0;
  78}
  79
  80static int is_foreground_fd(int fd)
  81{
  82        int tpgrp = tcgetpgrp(fd);
  83        return tpgrp < 0 || tpgrp == getpgid(0);
  84}
  85
  86static void display(struct progress *progress, uint64_t n, const char *done)
  87{
  88        const char *tp;
  89        struct strbuf *counters_sb = &progress->counters_sb;
  90        int show_update = 0;
  91
  92        if (progress->delay && (!progress_update || --progress->delay))
  93                return;
  94
  95        progress->last_value = n;
  96        tp = (progress->throughput) ? progress->throughput->display.buf : "";
  97        if (progress->total) {
  98                unsigned percent = n * 100 / progress->total;
  99                if (percent != progress->last_percent || progress_update) {
 100                        progress->last_percent = percent;
 101
 102                        strbuf_reset(counters_sb);
 103                        strbuf_addf(counters_sb,
 104                                    "%3u%% (%"PRIuMAX"/%"PRIuMAX")%s", percent,
 105                                    (uintmax_t)n, (uintmax_t)progress->total,
 106                                    tp);
 107                        show_update = 1;
 108                }
 109        } else if (progress_update) {
 110                strbuf_reset(counters_sb);
 111                strbuf_addf(counters_sb, "%"PRIuMAX"%s", (uintmax_t)n, tp);
 112                show_update = 1;
 113        }
 114
 115        if (show_update) {
 116                if (is_foreground_fd(fileno(stderr)) || done) {
 117                        const char *eol = done ? done : "\r";
 118
 119                        term_clear_line();
 120                        if (progress->split) {
 121                                fprintf(stderr, "  %s%s", counters_sb->buf,
 122                                        eol);
 123                        } else if (!done &&
 124                                   /* The "+ 2" accounts for the ": ". */
 125                                   term_columns() < progress->title_len +
 126                                                    counters_sb->len + 2) {
 127                                fprintf(stderr, "%s:\n  %s%s",
 128                                        progress->title, counters_sb->buf, eol);
 129                                progress->split = 1;
 130                        } else {
 131                                fprintf(stderr, "%s: %s%s", progress->title,
 132                                        counters_sb->buf, eol);
 133                        }
 134                        fflush(stderr);
 135                }
 136                progress_update = 0;
 137        }
 138}
 139
 140static void throughput_string(struct strbuf *buf, uint64_t total,
 141                              unsigned int rate)
 142{
 143        strbuf_reset(buf);
 144        strbuf_addstr(buf, ", ");
 145        strbuf_humanise_bytes(buf, total);
 146        strbuf_addstr(buf, " | ");
 147        strbuf_humanise_rate(buf, rate * 1024);
 148}
 149
 150void display_throughput(struct progress *progress, uint64_t total)
 151{
 152        struct throughput *tp;
 153        uint64_t now_ns;
 154        unsigned int misecs, count, rate;
 155
 156        if (!progress)
 157                return;
 158        tp = progress->throughput;
 159
 160        now_ns = getnanotime();
 161
 162        if (!tp) {
 163                progress->throughput = tp = xcalloc(1, sizeof(*tp));
 164                tp->prev_total = tp->curr_total = total;
 165                tp->prev_ns = now_ns;
 166                strbuf_init(&tp->display, 0);
 167                return;
 168        }
 169        tp->curr_total = total;
 170
 171        /* only update throughput every 0.5 s */
 172        if (now_ns - tp->prev_ns <= 500000000)
 173                return;
 174
 175        /*
 176         * We have x = bytes and y = nanosecs.  We want z = KiB/s:
 177         *
 178         *      z = (x / 1024) / (y / 1000000000)
 179         *      z = x / y * 1000000000 / 1024
 180         *      z = x / (y * 1024 / 1000000000)
 181         *      z = x / y'
 182         *
 183         * To simplify things we'll keep track of misecs, or 1024th of a sec
 184         * obtained with:
 185         *
 186         *      y' = y * 1024 / 1000000000
 187         *      y' = y * (2^10 / 2^42) * (2^42 / 1000000000)
 188         *      y' = y / 2^32 * 4398
 189         *      y' = (y * 4398) >> 32
 190         */
 191        misecs = ((now_ns - tp->prev_ns) * 4398) >> 32;
 192
 193        count = total - tp->prev_total;
 194        tp->prev_total = total;
 195        tp->prev_ns = now_ns;
 196        tp->avg_bytes += count;
 197        tp->avg_misecs += misecs;
 198        rate = tp->avg_bytes / tp->avg_misecs;
 199        tp->avg_bytes -= tp->last_bytes[tp->idx];
 200        tp->avg_misecs -= tp->last_misecs[tp->idx];
 201        tp->last_bytes[tp->idx] = count;
 202        tp->last_misecs[tp->idx] = misecs;
 203        tp->idx = (tp->idx + 1) % TP_IDX_MAX;
 204
 205        throughput_string(&tp->display, total, rate);
 206        if (progress->last_value != -1 && progress_update)
 207                display(progress, progress->last_value, NULL);
 208}
 209
 210void display_progress(struct progress *progress, uint64_t n)
 211{
 212        if (progress)
 213                display(progress, n, NULL);
 214}
 215
 216static struct progress *start_progress_delay(const char *title, uint64_t total,
 217                                             unsigned delay, unsigned sparse)
 218{
 219        struct progress *progress = xmalloc(sizeof(*progress));
 220        progress->title = title;
 221        progress->total = total;
 222        progress->last_value = -1;
 223        progress->last_percent = -1;
 224        progress->delay = delay;
 225        progress->sparse = sparse;
 226        progress->throughput = NULL;
 227        progress->start_ns = getnanotime();
 228        strbuf_init(&progress->counters_sb, 0);
 229        progress->title_len = utf8_strwidth(title);
 230        progress->split = 0;
 231        set_progress_signal();
 232        return progress;
 233}
 234
 235struct progress *start_delayed_progress(const char *title, uint64_t total)
 236{
 237        return start_progress_delay(title, total, 2, 0);
 238}
 239
 240struct progress *start_progress(const char *title, uint64_t total)
 241{
 242        return start_progress_delay(title, total, 0, 0);
 243}
 244
 245/*
 246 * Here "sparse" means that the caller might use some sampling criteria to
 247 * decide when to call display_progress() rather than calling it for every
 248 * integer value in[0 .. total).  In particular, the caller might not call
 249 * display_progress() for the last value in the range.
 250 *
 251 * When "sparse" is set, stop_progress() will automatically force the done
 252 * message to show 100%.
 253 */
 254struct progress *start_sparse_progress(const char *title, uint64_t total)
 255{
 256        return start_progress_delay(title, total, 0, 1);
 257}
 258
 259struct progress *start_delayed_sparse_progress(const char *title,
 260                                               uint64_t total)
 261{
 262        return start_progress_delay(title, total, 2, 1);
 263}
 264
 265static void finish_if_sparse(struct progress *progress)
 266{
 267        if (progress &&
 268            progress->sparse &&
 269            progress->last_value != progress->total)
 270                display_progress(progress, progress->total);
 271}
 272
 273void stop_progress(struct progress **p_progress)
 274{
 275        finish_if_sparse(*p_progress);
 276
 277        stop_progress_msg(p_progress, _("done"));
 278}
 279
 280void stop_progress_msg(struct progress **p_progress, const char *msg)
 281{
 282        struct progress *progress = *p_progress;
 283        if (!progress)
 284                return;
 285        *p_progress = NULL;
 286        if (progress->last_value != -1) {
 287                /* Force the last update */
 288                char *buf;
 289                struct throughput *tp = progress->throughput;
 290
 291                if (tp) {
 292                        uint64_t now_ns = getnanotime();
 293                        unsigned int misecs, rate;
 294                        misecs = ((now_ns - progress->start_ns) * 4398) >> 32;
 295                        rate = tp->curr_total / (misecs ? misecs : 1);
 296                        throughput_string(&tp->display, tp->curr_total, rate);
 297                }
 298                progress_update = 1;
 299                buf = xstrfmt(", %s.\n", msg);
 300                display(progress, progress->last_value, buf);
 301                free(buf);
 302        }
 303        clear_progress_signal();
 304        strbuf_release(&progress->counters_sb);
 305        if (progress->throughput)
 306                strbuf_release(&progress->throughput->display);
 307        free(progress->throughput);
 308        free(progress);
 309}