1 #include "disk_space_estimator.h"
5 #include <sys/statfs.h>
11 DiskSpaceEstimator::DiskSpaceEstimator(DiskSpaceEstimator::callback_t callback)
14 global_metrics.add("disk_free_bytes", &metric_disk_free_bytes, Metrics::TYPE_GAUGE);
17 void DiskSpaceEstimator::report_write(const std::string &filename, uint64_t pts)
19 if (filename != last_filename) {
20 last_filename = filename;
21 measure_points.clear();
24 // Reject points that are out-of-order (happens with B-frames).
25 if (!measure_points.empty() && pts < measure_points.back().pts) {
29 // Remove too old points.
30 while (measure_points.size() > 1 && measure_points.front().pts + window_length < pts) {
31 measure_points.pop_front();
35 if (stat(filename.c_str(), &st) == -1) {
36 perror(filename.c_str());
41 if (statfs(filename.c_str(), &fst) == -1) {
42 perror(filename.c_str());
46 off_t free_bytes = off_t(fst.f_bavail) * fst.f_frsize;
47 metric_disk_free_bytes = free_bytes;
49 if (!measure_points.empty()) {
50 double bytes_per_second = double(st.st_size - measure_points.front().size) /
51 (pts - measure_points.front().pts) * TIMEBASE;
52 double seconds_left = free_bytes / bytes_per_second;
54 // Only report every second, since updating the UI can be expensive.
55 if (last_pts_reported == 0 || pts - last_pts_reported >= TIMEBASE) {
56 callback(free_bytes, seconds_left);
57 last_pts_reported = pts;
61 measure_points.push_back({ pts, st.st_size });
64 DiskSpaceEstimator *global_disk_space_estimator = nullptr; // Created in MainWindow::MainWindow().