]> git.sesse.net Git - nageru/blob - disk_space_estimator.cpp
Embed shaders into the binary.
[nageru] / disk_space_estimator.cpp
1 #include "disk_space_estimator.h"
2
3 #include "timebase.h"
4
5 #include <memory>
6 #include <stdio.h>
7 #include <sys/stat.h>
8 #include <sys/statfs.h>
9
10 DiskSpaceEstimator::DiskSpaceEstimator(DiskSpaceEstimator::callback_t callback)
11         : callback(callback)
12 {
13 }
14
15 void DiskSpaceEstimator::report_write(const std::string &filename, uint64_t pts)
16 {
17         // Reject points that are out-of-order (happens with B-frames).
18         if (!measure_points.empty() && pts <= measure_points.back().pts) {
19                 return;
20         }
21
22         // Remove too old points.
23         while (measure_points.size() > 1 && measure_points.front().pts + window_length < pts) {
24                 measure_points.pop_front();
25         }
26
27         struct stat st;
28         if (stat(filename.c_str(), &st) == -1) {
29                 perror(filename.c_str());
30                 return;
31         }
32
33         total_size += st.st_size;
34
35         struct statfs fst;
36         if (statfs(filename.c_str(), &fst) == -1) {
37                 perror(filename.c_str());
38                 return;
39         }
40
41         off_t free_bytes = off_t(fst.f_bavail) * fst.f_frsize;
42
43         if (!measure_points.empty()) {
44                 double bytes_per_second = double(total_size - measure_points.front().size) /
45                         (pts - measure_points.front().pts) * TIMEBASE;
46                 double seconds_left = free_bytes / bytes_per_second;
47
48                 // Only report every second, since updating the UI can be expensive.
49                 if (last_pts_reported == 0 || pts - last_pts_reported >= TIMEBASE) {
50                         callback(free_bytes, seconds_left);
51                         last_pts_reported = pts;
52                 }
53         }
54
55         measure_points.push_back({ pts, total_size });
56 }
57
58 DiskSpaceEstimator *global_disk_space_estimator = nullptr;  // Created in MainWindow::MainWindow().