| 191 | } |
| 192 | |
| 193 | void display_throughput(struct progress *progress, uint64_t total) |
| 194 | { |
| 195 | struct throughput *tp; |
| 196 | uint64_t now_ns; |
| 197 | unsigned int misecs, count, rate; |
| 198 | |
| 199 | if (!progress) |
| 200 | return; |
| 201 | tp = progress->throughput; |
| 202 | |
| 203 | now_ns = progress_getnanotime(progress); |
| 204 | |
| 205 | if (!tp) { |
| 206 | progress->throughput = CALLOC_ARRAY(tp, 1); |
| 207 | tp->prev_total = tp->curr_total = total; |
| 208 | tp->prev_ns = now_ns; |
| 209 | strbuf_init(&tp->display, 0); |
| 210 | return; |
| 211 | } |
| 212 | tp->curr_total = total; |
| 213 | |
| 214 | /* only update throughput every 0.5 s */ |
| 215 | if (now_ns - tp->prev_ns <= 500000000) |
| 216 | return; |
| 217 | |
| 218 | /* |
| 219 | * We have x = bytes and y = nanosecs. We want z = KiB/s: |
| 220 | * |
| 221 | * z = (x / 1024) / (y / 1000000000) |
| 222 | * z = x / y * 1000000000 / 1024 |
| 223 | * z = x / (y * 1024 / 1000000000) |
| 224 | * z = x / y' |
| 225 | * |
| 226 | * To simplify things we'll keep track of misecs, or 1024th of a sec |
| 227 | * obtained with: |
| 228 | * |
| 229 | * y' = y * 1024 / 1000000000 |
| 230 | * y' = y * (2^10 / 2^42) * (2^42 / 1000000000) |
| 231 | * y' = y / 2^32 * 4398 |
| 232 | * y' = (y * 4398) >> 32 |
| 233 | */ |
| 234 | misecs = ((now_ns - tp->prev_ns) * 4398) >> 32; |
| 235 | |
| 236 | count = total - tp->prev_total; |
| 237 | tp->prev_total = total; |
| 238 | tp->prev_ns = now_ns; |
| 239 | tp->avg_bytes += count; |
| 240 | tp->avg_misecs += misecs; |
| 241 | rate = tp->avg_bytes / tp->avg_misecs; |
| 242 | tp->avg_bytes -= tp->last_bytes[tp->idx]; |
| 243 | tp->avg_misecs -= tp->last_misecs[tp->idx]; |
| 244 | tp->last_bytes[tp->idx] = count; |
| 245 | tp->last_misecs[tp->idx] = misecs; |
| 246 | tp->idx = (tp->idx + 1) % TP_IDX_MAX; |
| 247 | |
| 248 | throughput_string(&tp->display, total, rate); |
| 249 | if (progress->last_value != -1 && progress_update) |
| 250 | display(progress, progress->last_value, NULL); |