Browse Source

Implemented event timer.

jcsyshc 1 year ago
parent
commit
2ef6fb88a9

+ 1 - 0
CMakeLists.txt

@@ -5,6 +5,7 @@ set(CMAKE_CXX_STANDARD 20)
 
 add_executable(${PROJECT_NAME} src/main.cpp
         src/impl/main_impl.cpp
+        src/core/impl/event_timer.cpp
         src/core/impl/memory_pool.cpp
         src/core/impl/object_manager.cpp
         src/module/impl/image_viewer.cpp

+ 38 - 0
src/core/event_timer.h

@@ -0,0 +1,38 @@
+#ifndef DEPTHGUIDE_EVENT_TIMER_H
+#define DEPTHGUIDE_EVENT_TIMER_H
+
+#include "utility.hpp"
+
+#include <memory>
+
+class event_timer {
+public:
+
+    struct create_config {
+        uint win_size = 5;
+    };
+
+    event_timer();
+
+    explicit event_timer(create_config conf);
+
+    ~event_timer();
+
+    void record(timestamp_type ts = current_timestamp());
+
+    struct stat_info {
+        float frequency; // in Hz
+        float interval; // in ms
+    };
+
+    stat_info query() const;
+
+    timestamp_type last_ts() const;
+
+private:
+    struct impl;
+    std::unique_ptr<impl> pimpl;
+};
+
+
+#endif //DEPTHGUIDE_EVENT_TIMER_H

+ 43 - 0
src/core/impl/event_timer.cpp

@@ -0,0 +1,43 @@
+#include "event_timer_impl.h"
+
+event_timer::impl::impl(create_config conf)
+        : interval_acc(bt::rolling_window::window_size = conf.win_size) {
+}
+
+void event_timer::impl::record(timestamp_type ts) {
+    assert(ts > last_ts);
+    if (last_ts != 0) {
+        auto interval = (ts - last_ts) / 1e3f;
+        interval_acc(interval);
+    }
+    last_ts = ts;
+}
+
+event_timer::stat_info event_timer::impl::query() const {
+    auto ret = stat_info();
+    ret.interval = ba::rolling_mean(interval_acc);
+    ret.frequency = 1e3f / ret.interval;
+    return ret;
+}
+
+event_timer::event_timer()
+        : event_timer(create_config{}) {
+}
+
+event_timer::event_timer(create_config conf)
+        : pimpl(std::make_unique<impl>(conf)) {
+}
+
+event_timer::~event_timer() = default;
+
+void event_timer::record(timestamp_type ts) {
+    pimpl->record(ts);
+}
+
+event_timer::stat_info event_timer::query() const {
+    return pimpl->query();
+}
+
+timestamp_type event_timer::last_ts() const {
+    return pimpl->last_ts;
+}

+ 29 - 0
src/core/impl/event_timer_impl.h

@@ -0,0 +1,29 @@
+#ifndef DEPTHGUIDE_EVENT_TIMER_IMPL_H
+#define DEPTHGUIDE_EVENT_TIMER_IMPL_H
+
+#include "core/event_timer.h"
+
+#include <boost/accumulators/accumulators.hpp>
+#include <boost/accumulators/statistics.hpp>
+#include <boost/accumulators/statistics/rolling_mean.hpp>
+
+namespace ba = boost::accumulators;
+namespace bt = ba::tag;
+
+struct event_timer::impl {
+
+    timestamp_type last_ts = 0;
+
+    using interval_acc_type = ba::accumulator_set<
+            float, ba::stats<bt::lazy_rolling_mean>>;
+    interval_acc_type interval_acc; // in ms
+
+    explicit impl(create_config conf);
+
+    void record(timestamp_type ts);
+
+    stat_info query() const;
+
+};
+
+#endif //DEPTHGUIDE_EVENT_TIMER_IMPL_H

+ 6 - 12
src/core/impl/object_manager.cpp

@@ -27,15 +27,16 @@ object_manager::impl::query_info(name_type obj_name) {
     auto &st = iter->second;
     return obj_info{
             .type = st.type, .pl_ptr = st.ptr,
-            .sig = &st.sig, .last_save_ts = st.last_save_ts};
+            .sig = &st.sig, .last_save_ts = st.stats_timer.last_ts()};
 }
 
 object_manager::obj_stats
 object_manager::impl::query_obj_stats(name_type obj_name) {
     auto obj_st = query_st(obj_name);
-    auto ret = obj_stats();
-    ret.save_interval = ba::rolling_mean(obj_st->interval_acc) * 1e3f; // ms
-    ret.save_frequency = 1e3f / ret.save_interval; // Hz
+    auto info = obj_st->stats_timer.query();
+    auto ret = obj_stats{
+            .save_interval = info.interval,
+            .save_frequency = info.frequency};
     return ret;
 }
 
@@ -57,14 +58,7 @@ void object_manager::impl::notify_signal(name_type obj_name) {
         });
         obj_st->is_pending = true;
     }
-
-    // stats
-    if (obj_st->last_save_ts != 0) {
-        assert(cur_ts > obj_st->last_save_ts);
-        auto interval = (cur_ts - obj_st->last_save_ts) / 1e6f;
-        obj_st->interval_acc(interval);
-    }
-    obj_st->last_save_ts = cur_ts;
+    obj_st->stats_timer.record(cur_ts);
 }
 
 io_context *object_manager::impl::switch_ctx() const {

+ 2 - 13
src/core/impl/object_manager_impl.h

@@ -2,17 +2,12 @@
 #define DEPTHGUIDE_OBJECT_MANAGER_IMPL_H
 
 #include "core/object_manager.h"
-
-#include <boost/accumulators/accumulators.hpp>
-#include <boost/accumulators/statistics.hpp>
-#include <boost/accumulators/statistics/rolling_mean.hpp>
+#include "core/event_timer.h"
 
 #include <list>
 #include <thread>
 #include <unordered_map>
 
-namespace ba = boost::accumulators;
-
 struct object_manager::impl {
 
     struct obj_st_type { // object store type
@@ -23,13 +18,7 @@ struct object_manager::impl {
         obj_sig_type sig;
 
         // statistical information
-        timestamp_type last_save_ts = 0;
-
-        static constexpr auto acc_window_size = 5;
-        using interval_acc_type = ba::accumulator_set<
-                float, ba::stats<ba::tag::lazy_rolling_mean>>;
-        interval_acc_type interval_acc = interval_acc_type(
-                ba::tag::rolling_window::window_size = acc_window_size);
+        event_timer stats_timer;
     };
 
     using obj_pool_type = std::unordered_map<name_type, obj_st_type>;

+ 7 - 0
src/impl/main_impl.cpp

@@ -1,5 +1,6 @@
 #include "main_impl.h"
 #include "device/orb_camera_ui.h"
+#include "core/event_timer.h"
 #include "core/image_utility.hpp"
 #include "module/image_streamer.h"
 #include "module/image_viewer.h"
@@ -31,6 +32,7 @@ smart_cuda_stream *default_cuda_stream = nullptr;
 io_context *main_ctx;
 object_manager *main_ob;
 
+event_timer perf_timer; // performance timer
 std::unique_ptr<steady_timer> ui_timer;
 std::chrono::milliseconds ui_interval;
 
@@ -208,6 +210,10 @@ void show_ui() {
                 }
                 ImGui::TreePop();
             }
+            if (ImGui::TreeNode("Performance")) {
+                ImGui::Text("UI Refresh Rate: %.2fms", perf_timer.query().interval);
+                ImGui::TreePop();
+            }
         }
 
         ImGui::PopItemWidth();
@@ -229,6 +235,7 @@ void show_ui() {
 
     ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
     glfwSwapBuffers(window);
+    perf_timer.record();
 }
 
 void cleanup() {