Skip to content

Commit 83d26fa

Browse files
kbleesgitster
authored andcommitted
progress: simplify performance measurement by using getnanotime()
Calculating duration from a single uint64_t is simpler than from a struct timeval. Change throughput measurement from gettimeofday() to getnanotime(). Also calculate misec only if needed, and change integer division to integer multiplication + shift, which should be slightly faster. Signed-off-by: Karsten Blees <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 132d41e commit 83d26fa

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

progress.c

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
#include "gettext.h"
1313
#include "progress.h"
1414
#include "strbuf.h"
15+
#include "trace.h"
1516

1617
#define TP_IDX_MAX 8
1718

1819
struct throughput {
1920
off_t curr_total;
2021
off_t prev_total;
21-
struct timeval prev_tv;
22+
uint64_t prev_ns;
2223
unsigned int avg_bytes;
2324
unsigned int avg_misecs;
2425
unsigned int last_bytes[TP_IDX_MAX];
@@ -127,65 +128,65 @@ static void throughput_string(struct strbuf *buf, off_t total,
127128
void display_throughput(struct progress *progress, off_t total)
128129
{
129130
struct throughput *tp;
130-
struct timeval tv;
131-
unsigned int misecs;
131+
uint64_t now_ns;
132+
unsigned int misecs, count, rate;
133+
struct strbuf buf = STRBUF_INIT;
132134

133135
if (!progress)
134136
return;
135137
tp = progress->throughput;
136138

137-
gettimeofday(&tv, NULL);
139+
now_ns = getnanotime();
138140

139141
if (!tp) {
140142
progress->throughput = tp = calloc(1, sizeof(*tp));
141143
if (tp) {
142144
tp->prev_total = tp->curr_total = total;
143-
tp->prev_tv = tv;
145+
tp->prev_ns = now_ns;
144146
}
145147
return;
146148
}
147149
tp->curr_total = total;
148150

151+
/* only update throughput every 0.5 s */
152+
if (now_ns - tp->prev_ns <= 500000000)
153+
return;
154+
149155
/*
150-
* We have x = bytes and y = microsecs. We want z = KiB/s:
156+
* We have x = bytes and y = nanosecs. We want z = KiB/s:
151157
*
152-
* z = (x / 1024) / (y / 1000000)
153-
* z = x / y * 1000000 / 1024
154-
* z = x / (y * 1024 / 1000000)
158+
* z = (x / 1024) / (y / 1000000000)
159+
* z = x / y * 1000000000 / 1024
160+
* z = x / (y * 1024 / 1000000000)
155161
* z = x / y'
156162
*
157163
* To simplify things we'll keep track of misecs, or 1024th of a sec
158164
* obtained with:
159165
*
160-
* y' = y * 1024 / 1000000
161-
* y' = y / (1000000 / 1024)
162-
* y' = y / 977
166+
* y' = y * 1024 / 1000000000
167+
* y' = y * (2^10 / 2^42) * (2^42 / 1000000000)
168+
* y' = y / 2^32 * 4398
169+
* y' = (y * 4398) >> 32
163170
*/
164-
misecs = (tv.tv_sec - tp->prev_tv.tv_sec) * 1024;
165-
misecs += (int)(tv.tv_usec - tp->prev_tv.tv_usec) / 977;
171+
misecs = ((now_ns - tp->prev_ns) * 4398) >> 32;
166172

167-
if (misecs > 512) {
168-
struct strbuf buf = STRBUF_INIT;
169-
unsigned int count, rate;
173+
count = total - tp->prev_total;
174+
tp->prev_total = total;
175+
tp->prev_ns = now_ns;
176+
tp->avg_bytes += count;
177+
tp->avg_misecs += misecs;
178+
rate = tp->avg_bytes / tp->avg_misecs;
179+
tp->avg_bytes -= tp->last_bytes[tp->idx];
180+
tp->avg_misecs -= tp->last_misecs[tp->idx];
181+
tp->last_bytes[tp->idx] = count;
182+
tp->last_misecs[tp->idx] = misecs;
183+
tp->idx = (tp->idx + 1) % TP_IDX_MAX;
170184

171-
count = total - tp->prev_total;
172-
tp->prev_total = total;
173-
tp->prev_tv = tv;
174-
tp->avg_bytes += count;
175-
tp->avg_misecs += misecs;
176-
rate = tp->avg_bytes / tp->avg_misecs;
177-
tp->avg_bytes -= tp->last_bytes[tp->idx];
178-
tp->avg_misecs -= tp->last_misecs[tp->idx];
179-
tp->last_bytes[tp->idx] = count;
180-
tp->last_misecs[tp->idx] = misecs;
181-
tp->idx = (tp->idx + 1) % TP_IDX_MAX;
182-
183-
throughput_string(&buf, total, rate);
184-
strncpy(tp->display, buf.buf, sizeof(tp->display));
185-
strbuf_release(&buf);
186-
if (progress->last_value != -1 && progress_update)
187-
display(progress, progress->last_value, NULL);
188-
}
185+
throughput_string(&buf, total, rate);
186+
strncpy(tp->display, buf.buf, sizeof(tp->display));
187+
strbuf_release(&buf);
188+
if (progress->last_value != -1 && progress_update)
189+
display(progress, progress->last_value, NULL);
189190
}
190191

191192
int display_progress(struct progress *progress, unsigned n)

0 commit comments

Comments
 (0)