summaryrefslogtreecommitdiff
path: root/progress.c
diff options
context:
space:
mode:
Diffstat (limited to 'progress.c')
-rw-r--r--progress.c36
1 files changed, 32 insertions, 4 deletions
diff --git a/progress.c b/progress.c
index a2e8cf64a8..0063559aab 100644
--- a/progress.c
+++ b/progress.c
@@ -45,6 +45,19 @@ struct progress {
static volatile sig_atomic_t progress_update;
+/*
+ * These are only intended for testing the progress output, i.e. exclusively
+ * for 'test-tool progress'.
+ */
+int progress_testing;
+uint64_t progress_test_ns = 0;
+void progress_test_force_update(void); /* To silence -Wmissing-prototypes */
+void progress_test_force_update(void)
+{
+ progress_update = 1;
+}
+
+
static void progress_interval(int signum)
{
progress_update = 1;
@@ -55,6 +68,9 @@ static void set_progress_signal(void)
struct sigaction sa;
struct itimerval v;
+ if (progress_testing)
+ return;
+
progress_update = 0;
memset(&sa, 0, sizeof(sa));
@@ -72,6 +88,10 @@ static void set_progress_signal(void)
static void clear_progress_signal(void)
{
struct itimerval v = {{0,},};
+
+ if (progress_testing)
+ return;
+
setitimer(ITIMER_REAL, &v, NULL);
signal(SIGALRM, SIG_IGN);
progress_update = 0;
@@ -119,6 +139,7 @@ static void display(struct progress *progress, uint64_t n, const char *done)
size_t clear_len = counters_sb->len < last_count_len ?
last_count_len - counters_sb->len + 1 :
0;
+ /* The "+ 2" accounts for the ": ". */
size_t progress_line_len = progress->title_len +
counters_sb->len + 2;
int cols = term_columns();
@@ -150,8 +171,15 @@ static void throughput_string(struct strbuf *buf, uint64_t total,
strbuf_addstr(buf, ", ");
strbuf_humanise_bytes(buf, total);
strbuf_addstr(buf, " | ");
- strbuf_humanise_bytes(buf, rate * 1024);
- strbuf_addstr(buf, "/s");
+ strbuf_humanise_rate(buf, rate * 1024);
+}
+
+static uint64_t progress_getnanotime(struct progress *progress)
+{
+ if (progress_testing)
+ return progress->start_ns + progress_test_ns;
+ else
+ return getnanotime();
}
void display_throughput(struct progress *progress, uint64_t total)
@@ -164,7 +192,7 @@ void display_throughput(struct progress *progress, uint64_t total)
return;
tp = progress->throughput;
- now_ns = getnanotime();
+ now_ns = progress_getnanotime(progress);
if (!tp) {
progress->throughput = tp = xcalloc(1, sizeof(*tp));
@@ -296,7 +324,7 @@ void stop_progress_msg(struct progress **p_progress, const char *msg)
struct throughput *tp = progress->throughput;
if (tp) {
- uint64_t now_ns = getnanotime();
+ uint64_t now_ns = progress_getnanotime(progress);
unsigned int misecs, rate;
misecs = ((now_ns - progress->start_ns) * 4398) >> 32;
rate = tp->curr_total / (misecs ? misecs : 1);