-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress.cpp
More file actions
72 lines (56 loc) · 1.68 KB
/
progress.cpp
File metadata and controls
72 lines (56 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
struct Progress {
enum Units: u8 {
NONE, BYTES
};
s64 total = 0;
u8 unit = NONE;
s64 cur = 0;
s64 cur_last = 0;
u64 t_start = 0;
u64 t_last = 0;
u64 interval = 5000000000;
bool initialized = false;
};
bool progress_update(Progress* progress, s64 cur) {
u64 t;
if (not progress->initialized) {
progress->initialized = true;
if (not global_os.initialized) os_init();
t = os_now();
progress->t_start = t;
progress->t_last = t;
} else {
t = os_now();
}
if (progress->t_last + progress->interval > t) return false;
if (progress->unit == Progress::NONE) {
format_print("%d", cur);
if (progress->total) format_print("/%d", progress->total);
} else if (progress->unit == Progress::BYTES) {
format_print("%B", cur);
if (progress->total) format_print(" / %B", progress->total);
} else assert(false);
if (t != progress->t_last) {
double speed = (cur - progress->cur_last) * 1e9 / (t - progress->t_last);
double speed_avg = cur * 1e9 / (t - progress->t_start);
format_print(" ");
if (progress->unit == Progress::NONE) {
format_print("%.2f it/s avg %.2f it/s", speed, speed_avg);
} else if (progress->unit == Progress::BYTES) {
format_print("%B/s avg %B/s", (s64)speed, (s64)speed_avg);
}
if (progress->total and speed) {
s64 eta = (progress->total - cur) * 1e9 / speed;
format_print(" eta %T", eta);
}
}
format_print("\n");
progress->cur_last = cur;
progress->t_last = t;
progress->interval *= 1.1;
return true;
}
void progress_update_add(Progress* progress, s64 cur_add) {
progress->cur += cur_add;
progress_update(progress, progress->cur);
}