X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=futatabi%2Fframe_on_disk.cpp;h=971cafd5d9c7d8b89ee5a208fa6c91a2539803e7;hb=9ffd4f03f314cc6e0254449593def95c9bc203d6;hp=b496b3d4188ebda04e401089cd7c03e9b59c22c3;hpb=9b7d691b4cc5db7dbfc18c82e86c1207fcac4722;p=nageru diff --git a/futatabi/frame_on_disk.cpp b/futatabi/frame_on_disk.cpp index b496b3d..971cafd 100644 --- a/futatabi/frame_on_disk.cpp +++ b/futatabi/frame_on_disk.cpp @@ -1,22 +1,60 @@ +#include "frame_on_disk.h" + +#include "shared/metrics.h" + +#include +#include #include +#include #include -#include "frame_on_disk.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 metric_frame_opened_files{ 0 }; +atomic metric_frame_closed_files{ 0 }; +atomic metric_frame_read_bytes{ 0 }; +atomic 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 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; @@ -28,13 +66,14 @@ string FrameReader::read_frame(FrameOnDisk frame) fd = open(filename.c_str(), O_RDONLY); if (fd == -1) { perror(filename.c_str()); - exit(1); + abort(); } // We want readahead. (Ignore errors.) posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL); last_filename_idx = frame.filename_idx; + ++metric_frame_opened_files; } string str; @@ -44,10 +83,17 @@ string FrameReader::read_frame(FrameOnDisk frame) int ret = pread(fd, &str[offset], frame.size - offset, frame.offset + offset); if (ret <= 0) { perror("pread"); - exit(1); + abort(); } offset += ret; } + + steady_clock::time_point stop = steady_clock::now(); + metric_frame_read_time_seconds.count_event(duration(stop - start).count()); + + metric_frame_read_bytes += frame.size; + ++metric_frame_read_frames; + return str; }