progress.con commit l10n: localizable upload progress messages (8f354a1)
   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        int last_count_len = counters_sb->len;
  92
  93        if (progress->delay && (!progress_update || --progress->delay))
  94                return;
  95
  96        progress->last_value = n;
  97        tp = (progress->throughput) ? progress->throughput->display.buf : "";
  98        if (progress->total) {
  99                unsigned percent = n * 100 / progress->total;
 100                if (percent != progress->last_percent || progress_update) {
 101                        progress->last_percent = percent;
 102
 103                        strbuf_reset(counters_sb);
 104                        strbuf_addf(counters_sb,
 105                                    "%3u%% (%"PRIuMAX"/%"PRIuMAX")%s", percent,
 106                                    (uintmax_t)n, (uintmax_t)progress->total,
 107                                    tp);
 108                        show_update = 1;
 109                }
 110        } else if (progress_update) {
 111                strbuf_reset(counters_sb);
 112                strbuf_addf(counters_sb, "%"PRIuMAX"%s", (uintmax_t)n, tp);
 113                show_update = 1;
 114        }
 115
 116        if (show_update) {
 117                if (is_foreground_fd(fileno(stderr)) || done) {
 118                        const char *eol = done ? done : "\r";
 119                        size_t clear_len = counters_sb->len < last_count_len ?
 120                                        last_count_len - counters_sb->len + 1 :
 121                                        0;
 122                        size_t progress_line_len = progress->title_len +
 123                                                counters_sb->len + 2;
 124                        int cols = term_columns();
 125
 126                        if (progress->split) {
 127                                fprintf(stderr, "  %s%*s", counters_sb->buf,
 128                                        (int) clear_len, eol);
 129                        } else if (!done && cols < progress_line_len) {
 130                                clear_len = progress->title_len + 1 < cols ?
 131                                            cols - progress->title_len - 1 : 0;
 132                                fprintf(stderr, "%s:%*s\n  %s%s",
 133                                        progress->title, (int) clear_len, "",
 134                                        counters_sb->buf, eol);
 135                                progress->split = 1;
 136                        } else {
 137                                fprintf(stderr, "%s: %s%*s", progress->title,
 138                                        counters_sb->buf, (int) clear_len, eol);
 139                        }
 140                        fflush(stderr);
 141                }
 142                progress_update = 0;
 143        }
 144}
 145
 146static void throughput_string(struct strbuf *buf, uint64_t total,
 147                              unsigned int rate)
 148{
 149        strbuf_reset(buf);
 150        strbuf_addstr(buf, ", ");
 151        strbuf_humanise_bytes(buf, total);
 152        strbuf_addstr(buf, " | ");
 153        strbuf_humanise_rate(buf, rate * 1024);
 154}
 155
 156void display_throughput(struct progress *progress, uint64_t total)
 157{
 158        struct throughput *tp;
 159        uint64_t now_ns;
 160        unsigned int misecs, count, rate;
 161
 162        if (!progress)
 163                return;
 164        tp = progress->throughput;
 165
 166        now_ns = getnanotime();
 167
 168        if (!tp) {
 169                progress->throughput = tp = xcalloc(1, sizeof(*tp));
 170                tp->prev_total = tp->curr_total = total;
 171                tp->prev_ns = now_ns;
 172                strbuf_init(&tp->display, 0);
 173                return;
 174        }
 175        tp->curr_total = total;
 176
 177        /* only update throughput every 0.5 s */
 178        if (now_ns - tp->prev_ns <= 500000000)
 179                return;
 180
 181        /*
 182         * We have x = bytes and y = nanosecs.  We want z = KiB/s:
 183         *
 184         *      z = (x / 1024) / (y / 1000000000)
 185         *      z = x / y * 1000000000 / 1024
 186         *      z = x / (y * 1024 / 1000000000)
 187         *      z = x / y'
 188         *
 189         * To simplify things we'll keep track of misecs, or 1024th of a sec
 190         * obtained with:
 191         *
 192         *      y' = y * 1024 / 1000000000
 193         *      y' = y * (2^10 / 2^42) * (2^42 / 1000000000)
 194         *      y' = y / 2^32 * 4398
 195         *      y' = (y * 4398) >> 32
 196         */
 197        misecs = ((now_ns - tp->prev_ns) * 4398) >> 32;
 198
 199        count = total - tp->prev_total;
 200        tp->prev_total = total;
 201        tp->prev_ns = now_ns;
 202        tp->avg_bytes += count;
 203        tp->avg_misecs += misecs;
 204        rate = tp->avg_bytes / tp->avg_misecs;
 205        tp->avg_bytes -= tp->last_bytes[tp->idx];
 206        tp->avg_misecs -= tp->last_misecs[tp->idx];
 207        tp->last_bytes[tp->idx] = count;
 208        tp->last_misecs[tp->idx] = misecs;
 209        tp->idx = (tp->idx + 1) % TP_IDX_MAX;
 210
 211        throughput_string(&tp->display, total, rate);
 212        if (progress->last_value != -1 && progress_update)
 213                display(progress, progress->last_value, NULL);
 214}
 215
 216void display_progress(struct progress *progress, uint64_t n)
 217{
 218        if (progress)
 219                display(progress, n, NULL);
 220}
 221
 222static struct progress *start_progress_delay(const char *title, uint64_t total,
 223                                             unsigned delay, unsigned sparse)
 224{
 225        struct progress *progress = xmalloc(sizeof(*progress));
 226        progress->title = title;
 227        progress->total = total;
 228        progress->last_value = -1;
 229        progress->last_percent = -1;
 230        progress->delay = delay;
 231        progress->sparse = sparse;
 232        progress->throughput = NULL;
 233        progress->start_ns = getnanotime();
 234        strbuf_init(&progress->counters_sb, 0);
 235        progress->title_len = utf8_strwidth(title);
 236        progress->split = 0;
 237        set_progress_signal();
 238        return progress;
 239}
 240
 241struct progress *start_delayed_progress(const char *title, uint64_t total)
 242{
 243        return start_progress_delay(title, total, 2, 0);
 244}
 245
 246struct progress *start_progress(const char *title, uint64_t total)
 247{
 248        return start_progress_delay(title, total, 0, 0);
 249}
 250
 251/*
 252 * Here "sparse" means that the caller might use some sampling criteria to
 253 * decide when to call display_progress() rather than calling it for every
 254 * integer value in[0 .. total).  In particular, the caller might not call
 255 * display_progress() for the last value in the range.
 256 *
 257 * When "sparse" is set, stop_progress() will automatically force the done
 258 * message to show 100%.
 259 */
 260struct progress *start_sparse_progress(const char *title, uint64_t total)
 261{
 262        return start_progress_delay(title, total, 0, 1);
 263}
 264
 265struct progress *start_delayed_sparse_progress(const char *title,
 266                                               uint64_t total)
 267{
 268        return start_progress_delay(title, total, 2, 1);
 269}
 270
 271static void finish_if_sparse(struct progress *progress)
 272{
 273        if (progress &&
 274            progress->sparse &&
 275            progress->last_value != progress->total)
 276                display_progress(progress, progress->total);
 277}
 278
 279void stop_progress(struct progress **p_progress)
 280{
 281        finish_if_sparse(*p_progress);
 282
 283        stop_progress_msg(p_progress, _("done"));
 284}
 285
 286void stop_progress_msg(struct progress **p_progress, const char *msg)
 287{
 288        struct progress *progress = *p_progress;
 289        if (!progress)
 290                return;
 291        *p_progress = NULL;
 292        if (progress->last_value != -1) {
 293                /* Force the last update */
 294                char *buf;
 295                struct throughput *tp = progress->throughput;
 296
 297                if (tp) {
 298                        uint64_t now_ns = getnanotime();
 299                        unsigned int misecs, rate;
 300                        misecs = ((now_ns - progress->start_ns) * 4398) >> 32;
 301                        rate = tp->curr_total / (misecs ? misecs : 1);
 302                        throughput_string(&tp->display, tp->curr_total, rate);
 303                }
 304                progress_update = 1;
 305                buf = xstrfmt(", %s.\n", msg);
 306                display(progress, progress->last_value, buf);
 307                free(buf);
 308        }
 309        clear_progress_signal();
 310        strbuf_release(&progress->counters_sb);
 311        if (progress->throughput)
 312                strbuf_release(&progress->throughput->display);
 313        free(progress->throughput);
 314        free(progress);
 315}