Make "git clone" pack-fetching download statistics better
Average it out over a few events to make the numbers stable, and fix the silly usec->binary-ms conversion. Yeah, yeah, it's arguably eye-candy to keep the user calm, but let's do that right. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Linus Torvalds committed
Feb 11, 2006 at 10:43 UTC
c548cf4ee0737a321ffe94f6a97c65baf87281be
1 file changed
+41
-3
fetch-clone.c
+41
-3
@@ -130,12 +130,35 @@ int receive_unpack_pack(int fd[2], const char *me, int quiet)
130
die("git-unpack-objects died of unnatural causes %d", status);
131
}
132
133
+/*
134
+ * We average out the download speed over this many "events", where
135
+ * an event is a minimum of about half a second. That way, we get
136
+ * a reasonably stable number.
137
+ */
138
+#define NR_AVERAGE (4)
139
+
140
+/*
141
+ * A "binary msec" is a power-of-two-msec, aka 1/1024th of a second.
142
+ * Keeing the time in that format means that "bytes / msecs" means
143
+ * is the same as kB/s (modulo rounding).
144
+ *
145
+ * 1000512 is a magic number (usecs in a second, rounded up by half
146
+ * of 1024, to make "rounding" come out right ;)
147
+ */
148
+#define usec_to_binarymsec(x) ((int)(x) / (1000512 >> 10))
149
+
150
int receive_keep_pack(int fd[2], const char *me, int quiet)
151
{
152
char tmpfile[PATH_MAX];
153
int ofd, ifd;
154
unsigned long total;
155
static struct timeval prev_tv;
156
+ struct average {
157
+ unsigned long bytes;
158
+ unsigned long time;
159
+ } download[NR_AVERAGE] = { {0, 0}, };
160
+ unsigned long avg_bytes, avg_time;
161
+ int idx = 0;
162
163
ifd = fd[0];
164
snprintf(tmpfile, sizeof(tmpfile),
@@ -146,6 +169,8 @@ int receive_keep_pack(int fd[2], const char *me, int quiet)
169
170
gettimeofday(&prev_tv, NULL);
171
total = 0;
172
+ avg_bytes = 0;
173
+ avg_time = 0;
174
while (1) {
175
char buf[8192];
176
ssize_t sz, wsz, pos;
@@ -181,14 +206,27 @@ int receive_keep_pack(int fd[2], const char *me, int quiet)
206
gettimeofday(&tv, NULL);
207
msecs = tv.tv_sec - prev_tv.tv_sec;
208
msecs <<= 10;
184
- msecs += (int)(tv.tv_usec - prev_tv.tv_usec) >> 10;
209
+ msecs += usec_to_binarymsec(tv.tv_usec - prev_tv.tv_usec);
210
+
211
if (msecs > 500) {
212
prev_tv = tv;
213
last = total;
188
- fprintf(stderr, "%4lu.%03luMB (%lu kB/s) \r",
214
+
215
+ /* Update averages ..*/
216
+ avg_bytes += diff;
217
+ avg_time += msecs;
218
+ avg_bytes -= download[idx].bytes;
219
+ avg_time -= download[idx].time;
220
+ download[idx].bytes = diff;
221
+ download[idx].time = msecs;
222
+ idx++;
223
+ if (idx >= NR_AVERAGE)
224
+ idx = 0;
225
+
226
+ fprintf(stderr, "%4lu.%03luMB (%lu kB/s) \r",
227
total >> 20,
228
1000*((total >> 10) & 1023)>>10,
191
- diff / msecs );
229
+ avg_bytes / avg_time );
230
}
231
}
232
}