X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=shared%2Fdisk_space_estimator.cpp;fp=nageru%2Fdisk_space_estimator.cpp;h=da55ee1e46b5018a0f8e381752bf661e55c11cc1;hb=827606868bf9f1dd16208882f0e3ca424b3e9e0a;hp=42fdfc24a04647d36b79dbc848d2f65000e4c3eb;hpb=c290898058c364494fa74398c70566f3bff1ae4c;p=nageru diff --git a/nageru/disk_space_estimator.cpp b/shared/disk_space_estimator.cpp similarity index 68% rename from nageru/disk_space_estimator.cpp rename to shared/disk_space_estimator.cpp index 42fdfc2..da55ee1 100644 --- a/nageru/disk_space_estimator.cpp +++ b/shared/disk_space_estimator.cpp @@ -1,28 +1,47 @@ -#include "disk_space_estimator.h" +#include "shared/disk_space_estimator.h" +#include #include #include #include -#include #include "shared/metrics.h" #include "shared/timebase.h" +using namespace std; + DiskSpaceEstimator::DiskSpaceEstimator(DiskSpaceEstimator::callback_t callback) : callback(callback) { global_metrics.add("disk_free_bytes", &metric_disk_free_bytes, Metrics::TYPE_GAUGE); } -void DiskSpaceEstimator::report_write(const std::string &filename, uint64_t pts) +void DiskSpaceEstimator::report_write(const string &filename, off_t bytes, uint64_t pts) +{ + total_size += bytes; + report_write_internal(filename, total_size, pts); +} + +void DiskSpaceEstimator::report_append(const string &filename, uint64_t pts) { if (filename != last_filename) { last_filename = filename; measure_points.clear(); } + struct stat st; + if (stat(filename.c_str(), &st) == -1) { + perror(filename.c_str()); + return; + } + + report_write_internal(filename, st.st_size, pts); +} + +void DiskSpaceEstimator::report_write_internal(const string &filename, off_t file_size, uint64_t pts) +{ // Reject points that are out-of-order (happens with B-frames). - if (!measure_points.empty() && pts < measure_points.back().pts) { + if (!measure_points.empty() && pts <= measure_points.back().pts) { return; } @@ -31,12 +50,6 @@ void DiskSpaceEstimator::report_write(const std::string &filename, uint64_t pts) measure_points.pop_front(); } - struct stat st; - if (stat(filename.c_str(), &st) == -1) { - perror(filename.c_str()); - return; - } - struct statfs fst; if (statfs(filename.c_str(), &fst) == -1) { perror(filename.c_str()); @@ -47,7 +60,7 @@ void DiskSpaceEstimator::report_write(const std::string &filename, uint64_t pts) metric_disk_free_bytes = free_bytes; if (!measure_points.empty()) { - double bytes_per_second = double(st.st_size - measure_points.front().size) / + double bytes_per_second = double(file_size - measure_points.front().size) / (pts - measure_points.front().pts) * TIMEBASE; double seconds_left = free_bytes / bytes_per_second; @@ -58,7 +71,7 @@ void DiskSpaceEstimator::report_write(const std::string &filename, uint64_t pts) } } - measure_points.push_back({ pts, st.st_size }); + measure_points.push_back({ pts, file_size }); } DiskSpaceEstimator *global_disk_space_estimator = nullptr; // Created in MainWindow::MainWindow().