make display of total transferred more accurate
The throughput display needs a delay period before accounting and displaying anything. Yet it might be called after some amount of data has already been transferred. The display of total data is therefore accounted late and therefore smaller than the reality. Let's call display_throughput() with an absolute amount of transferred data instead of a relative number, and let the throughput code find the relative amount of data by itself as needed. This way the displayed total is always exact. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nicolas Pitre committed
Nov 4, 2007 at 22:15 UTC
218558af599c01e5dec17a7399d9188a76c50203
5 files changed
+29
-28
csum-file.c
+3
-1
@@ -18,7 +18,8 @@ static void sha1flush(struct sha1file *f, unsigned int count)
18
for (;;) {
19
int ret = xwrite(f->fd, buf, count);
20
if (ret > 0) {
21
- display_throughput(f->tp, ret);
21
+ f->total += ret;
22
+ display_throughput(f->tp, f->total);
23
buf = (char *) buf + ret;
24
count -= ret;
25
if (count)
@@ -101,6 +102,7 @@ struct sha1file *sha1fd_throughput(int fd, const char *name, struct progress *tp
102
f->fd = fd;
103
f->error = 0;
104
f->offset = 0;
105
+ f->total = 0;
106
f->tp = tp;
107
f->do_crc = 0;
108
SHA1_Init(&f->ctx);
csum-file.h
+1
@@ -8,6 +8,7 @@ struct sha1file {
8
int fd, error;
9
unsigned int offset, namelen;
10
SHA_CTX ctx;
11
+ off_t total;
12
struct progress *tp;
13
char name[PATH_MAX];
14
int do_crc;
index-pack.c
+2
-2
@@ -87,9 +87,9 @@ static void *fill(int min)
87
die("early EOF");
88
die("read error on input: %s", strerror(errno));
89
}
90
- if (from_stdin)
91
- display_throughput(progress, ret);
90
input_len += ret;
91
+ if (from_stdin)
92
+ display_throughput(progress, consumed_bytes + input_len);
93
} while (input_len < min);
94
return input_buffer;
95
}
progress.c
+22
-24
@@ -14,11 +14,10 @@
14
#define TP_IDX_MAX 8
15
16
struct throughput {
17
+ off_t prev_total;
18
struct timeval prev_tv;
18
- off_t total;
19
- unsigned long count;
20
- unsigned long avg_bytes;
21
- unsigned long last_bytes[TP_IDX_MAX];
19
+ unsigned int avg_bytes;
20
+ unsigned int last_bytes[TP_IDX_MAX];
21
unsigned int avg_misecs;
22
unsigned int last_misecs[TP_IDX_MAX];
23
unsigned int idx;
@@ -110,7 +109,7 @@ static int display(struct progress *progress, unsigned n, int done)
109
return 0;
110
}
111
113
-void display_throughput(struct progress *progress, unsigned long n)
112
+void display_throughput(struct progress *progress, off_t total)
113
{
114
struct throughput *tp;
115
struct timeval tv;
@@ -124,14 +123,13 @@ void display_throughput(struct progress *progress, unsigned long n)
123
124
if (!tp) {
125
progress->throughput = tp = calloc(1, sizeof(*tp));
127
- if (tp)
126
+ if (tp) {
127
+ tp->prev_total = total;
128
tp->prev_tv = tv;
129
+ }
130
return;
131
}
132
132
- tp->total += n;
133
- tp->count += n;
134
-
133
/*
134
* We have x = bytes and y = microsecs. We want z = KiB/s:
135
*
@@ -152,37 +150,37 @@ void display_throughput(struct progress *progress, unsigned long n)
150
151
if (misecs > 512) {
152
int l = sizeof(tp->display);
153
+ unsigned int count = total - tp->prev_total;
154
+ tp->prev_total = total;
155
tp->prev_tv = tv;
156
- tp->avg_bytes += tp->count;
156
+ tp->avg_bytes += count;
157
tp->avg_misecs += misecs;
158
159
- if (tp->total > 1 << 30) {
159
+ if (total > 1 << 30) {
160
l -= snprintf(tp->display, l, ", %u.%2.2u GiB",
161
- (int)(tp->total >> 30),
162
- (int)(tp->total & ((1 << 30) - 1)) / 10737419);
163
- } else if (tp->total > 1 << 20) {
161
+ (int)(total >> 30),
162
+ (int)(total & ((1 << 30) - 1)) / 10737419);
163
+ } else if (total > 1 << 20) {
164
l -= snprintf(tp->display, l, ", %u.%2.2u MiB",
165
- (int)(tp->total >> 20),
166
- ((int)(tp->total & ((1 << 20) - 1))
165
+ (int)(total >> 20),
166
+ ((int)(total & ((1 << 20) - 1))
167
* 100) >> 20);
168
- } else if (tp->total > 1 << 10) {
168
+ } else if (total > 1 << 10) {
169
l -= snprintf(tp->display, l, ", %u.%2.2u KiB",
170
- (int)(tp->total >> 10),
171
- ((int)(tp->total & ((1 << 10) - 1))
170
+ (int)(total >> 10),
171
+ ((int)(total & ((1 << 10) - 1))
172
* 100) >> 10);
173
} else {
174
- l -= snprintf(tp->display, l, ", %u bytes",
175
- (int)tp->total);
174
+ l -= snprintf(tp->display, l, ", %u bytes", (int)total);
175
}
176
snprintf(tp->display + sizeof(tp->display) - l, l,
178
- " | %lu KiB/s", tp->avg_bytes / tp->avg_misecs);
177
+ " | %u KiB/s", tp->avg_bytes / tp->avg_misecs);
178
179
tp->avg_bytes -= tp->last_bytes[tp->idx];
180
tp->avg_misecs -= tp->last_misecs[tp->idx];
182
- tp->last_bytes[tp->idx] = tp->count;
181
+ tp->last_bytes[tp->idx] = count;
182
tp->last_misecs[tp->idx] = misecs;
183
tp->idx = (tp->idx + 1) % TP_IDX_MAX;
185
- tp->count = 0;
184
185
if (progress->last_value != -1 && progress_update)
186
display(progress, progress->last_value, 0);
progress.h
+1
-1
@@ -3,7 +3,7 @@
3
4
struct progress;
5
6
-void display_throughput(struct progress *progress, unsigned long n);
6
+void display_throughput(struct progress *progress, off_t total);
7
int display_progress(struct progress *progress, unsigned n);
8
struct progress *start_progress(const char *title, unsigned total);
9
struct progress *start_progress_delay(const char *title, unsigned total,