make sure throughput display gets updated even if progress doesn't move
[git/dscho.git] / progress.c
blob39d5d2c9f26e7da88846cecdc13cb440832508ca
1 /*
2 * Simple text-based progress display module for GIT
4 * Copyright (c) 2007 by Nicolas Pitre <nico@cam.org>
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 */
11 #include "git-compat-util.h"
12 #include "progress.h"
14 #define TP_IDX_MAX 8
16 struct throughput {
17 struct timeval prev_tv;
18 unsigned long count;
19 unsigned long avg_bytes;
20 unsigned long last_bytes[TP_IDX_MAX];
21 unsigned int avg_misecs;
22 unsigned int last_misecs[TP_IDX_MAX];
23 unsigned int idx;
24 char display[20];
27 struct progress {
28 const char *title;
29 int last_value;
30 unsigned total;
31 unsigned last_percent;
32 unsigned delay;
33 unsigned delayed_percent_treshold;
34 struct throughput *throughput;
37 static volatile sig_atomic_t progress_update;
39 static void progress_interval(int signum)
41 progress_update = 1;
44 static void set_progress_signal(void)
46 struct sigaction sa;
47 struct itimerval v;
49 progress_update = 0;
51 memset(&sa, 0, sizeof(sa));
52 sa.sa_handler = progress_interval;
53 sigemptyset(&sa.sa_mask);
54 sa.sa_flags = SA_RESTART;
55 sigaction(SIGALRM, &sa, NULL);
57 v.it_interval.tv_sec = 1;
58 v.it_interval.tv_usec = 0;
59 v.it_value = v.it_interval;
60 setitimer(ITIMER_REAL, &v, NULL);
63 static void clear_progress_signal(void)
65 struct itimerval v = {{0,},};
66 setitimer(ITIMER_REAL, &v, NULL);
67 signal(SIGALRM, SIG_IGN);
68 progress_update = 0;
71 static int display(struct progress *progress, unsigned n, int done)
73 char *eol, *tp;
75 if (progress->delay) {
76 if (!progress_update || --progress->delay)
77 return 0;
78 if (progress->total) {
79 unsigned percent = n * 100 / progress->total;
80 if (percent > progress->delayed_percent_treshold) {
81 /* inhibit this progress report entirely */
82 clear_progress_signal();
83 progress->delay = -1;
84 progress->total = 0;
85 return 0;
90 progress->last_value = n;
91 tp = (progress->throughput) ? progress->throughput->display : "";
92 eol = done ? ", done. \n" : " \r";
93 if (progress->total) {
94 unsigned percent = n * 100 / progress->total;
95 if (percent != progress->last_percent || progress_update) {
96 progress->last_percent = percent;
97 fprintf(stderr, "%s: %3u%% (%u/%u)%s%s",
98 progress->title, percent, n,
99 progress->total, tp, eol);
100 progress_update = 0;
101 return 1;
103 } else if (progress_update) {
104 fprintf(stderr, "%s: %u%s%s", progress->title, n, tp, eol);
105 progress_update = 0;
106 return 1;
109 return 0;
112 void display_throughput(struct progress *progress, unsigned long n)
114 struct throughput *tp;
115 struct timeval tv;
116 unsigned int misecs;
118 if (!progress)
119 return;
120 tp = progress->throughput;
122 gettimeofday(&tv, NULL);
124 if (!tp) {
125 progress->throughput = tp = calloc(1, sizeof(*tp));
126 if (tp)
127 tp->prev_tv = tv;
128 return;
131 tp->count += n;
134 * We have x = bytes and y = microsecs. We want z = KiB/s:
136 * z = (x / 1024) / (y / 1000000)
137 * z = x / y * 1000000 / 1024
138 * z = x / (y * 1024 / 1000000)
139 * z = x / y'
141 * To simplify things we'll keep track of misecs, or 1024th of a sec
142 * obtained with:
144 * y' = y * 1024 / 1000000
145 * y' = y / (1000000 / 1024)
146 * y' = y / 977
148 misecs = (tv.tv_sec - tp->prev_tv.tv_sec) * 1024;
149 misecs += (int)(tv.tv_usec - tp->prev_tv.tv_usec) / 977;
151 if (misecs > 512) {
152 tp->prev_tv = tv;
153 tp->avg_bytes += tp->count;
154 tp->avg_misecs += misecs;
155 snprintf(tp->display, sizeof(tp->display),
156 ", %lu KiB/s", tp->avg_bytes / tp->avg_misecs);
157 tp->avg_bytes -= tp->last_bytes[tp->idx];
158 tp->avg_misecs -= tp->last_misecs[tp->idx];
159 tp->last_bytes[tp->idx] = tp->count;
160 tp->last_misecs[tp->idx] = misecs;
161 tp->idx = (tp->idx + 1) % TP_IDX_MAX;
162 tp->count = 0;
164 if (progress->last_value != -1 && progress_update)
165 display(progress, progress->last_value, 0);
169 int display_progress(struct progress *progress, unsigned n)
171 return progress ? display(progress, n, 0) : 0;
174 struct progress *start_progress_delay(const char *title, unsigned total,
175 unsigned percent_treshold, unsigned delay)
177 struct progress *progress = malloc(sizeof(*progress));
178 if (!progress) {
179 /* unlikely, but here's a good fallback */
180 fprintf(stderr, "%s...\n", title);
181 return NULL;
183 progress->title = title;
184 progress->total = total;
185 progress->last_value = -1;
186 progress->last_percent = -1;
187 progress->delayed_percent_treshold = percent_treshold;
188 progress->delay = delay;
189 progress->throughput = NULL;
190 set_progress_signal();
191 return progress;
194 struct progress *start_progress(const char *title, unsigned total)
196 return start_progress_delay(title, total, 0, 0);
199 void stop_progress(struct progress **p_progress)
201 struct progress *progress = *p_progress;
202 if (!progress)
203 return;
204 *p_progress = NULL;
205 if (progress->last_value != -1) {
206 /* Force the last update */
207 progress_update = 1;
208 display(progress, progress->last_value, 1);
210 clear_progress_signal();
211 free(progress->throughput);
212 free(progress);