]> git.sesse.net Git - nageru/commitdiff
Add metrics for reading frames from disk.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 24 Dec 2018 10:07:19 +0000 (11:07 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 24 Dec 2018 10:07:19 +0000 (11:07 +0100)
futatabi/frame_on_disk.cpp
futatabi/frame_on_disk.h

index b496b3d4188ebda04e401089cd7c03e9b59c22c3..34fcd38ef768394edb59d4e08ea9796fd2451de1 100644 (file)
@@ -1,22 +1,60 @@
 #include <fcntl.h>
 #include <unistd.h>
 
+#include <atomic>
+#include <chrono>
+#include <mutex>
+
 #include "frame_on_disk.h"
+#include "shared/metrics.h"
 
 using namespace std;
+using namespace std::chrono;
+
+namespace {
+
+// There can be multiple FrameReader classes, so make all the metrics static.
+once_flag frame_metrics_inited;
+
+atomic<int64_t> metric_frame_opened_files{0};
+atomic<int64_t> metric_frame_closed_files{0};
+atomic<int64_t> metric_frame_read_bytes{0};
+atomic<int64_t> metric_frame_read_frames{0};
+
+Summary metric_frame_read_time_seconds;
+
+}  // namespace
+
+FrameReader::FrameReader()
+{
+       call_once(frame_metrics_inited, []{
+               global_metrics.add("frame_opened_files", &metric_frame_opened_files);
+               global_metrics.add("frame_closed_files", &metric_frame_closed_files);
+               global_metrics.add("frame_read_bytes", &metric_frame_read_bytes);
+               global_metrics.add("frame_read_frames", &metric_frame_read_frames);
+
+               vector<double> quantiles{0.01, 0.1, 0.25, 0.5, 0.75, 0.9, 0.99};
+               metric_frame_read_time_seconds.init(quantiles, 60.0);
+               global_metrics.add("frame_read_time_seconds", &metric_frame_read_time_seconds);
+       });
+}
 
 FrameReader::~FrameReader()
 {
        if (fd != -1) {
                close(fd);
+               ++metric_frame_closed_files;
        }
 }
 
 string FrameReader::read_frame(FrameOnDisk frame)
 {
+       steady_clock::time_point start = steady_clock::now();
+
        if (int(frame.filename_idx) != last_filename_idx) {
                if (fd != -1) {
                        close(fd);  // Ignore errors.
+                       ++metric_frame_closed_files;
                }
 
                string filename;
@@ -35,6 +73,7 @@ string FrameReader::read_frame(FrameOnDisk frame)
                posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL);
 
                last_filename_idx = frame.filename_idx;
+               ++metric_frame_opened_files;
        }
 
        string str;
@@ -49,5 +88,12 @@ string FrameReader::read_frame(FrameOnDisk frame)
 
                offset += ret;
        }
+
+       steady_clock::time_point stop = steady_clock::now();
+       metric_frame_read_time_seconds.count_event(duration<double>(stop - start).count());
+
+       metric_frame_read_bytes += frame.size;
+       ++metric_frame_read_frames;
+
        return str;
 }
index 47fcb32d349d1b132835500374cb12d14dd98235..2ac86fca697291f2d7d22ff2c92985aef79a618d 100644 (file)
@@ -27,6 +27,7 @@ extern std::vector<std::string> frame_filenames;  // Under frame_mu.
 // for a single .frames file.)
 class FrameReader {
 public:
+       FrameReader();
        ~FrameReader();
        std::string read_frame(FrameOnDisk frame);