From bcfafa4f7cdee6745626b0518c83c0106cc448c9 Mon Sep 17 00:00:00 2001 From: gralkapk Date: Mon, 27 Jun 2022 17:46:16 +0200 Subject: [PATCH 1/3] initial interface for timer queries --- .../profiling_service/Profiling_Service.cpp | 68 +++++++++++++++++++ .../profiling_service/Profiling_Service.hpp | 18 ++++- 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/frontend/services/profiling_service/Profiling_Service.cpp b/frontend/services/profiling_service/Profiling_Service.cpp index a6f5092756..6ce979ebed 100644 --- a/frontend/services/profiling_service/Profiling_Service.cpp +++ b/frontend/services/profiling_service/Profiling_Service.cpp @@ -1,5 +1,7 @@ #include "Profiling_Service.hpp" +#include "LuaCallbacksCollection.h" + namespace megamol { namespace frontend { @@ -31,6 +33,12 @@ bool Profiling_Service::init(void* configPtr) { }); } #endif + + this->_requestedResourcesNames = {"RegisterLuaCallbacks"}; + + distro_ = std::uniform_int_distribution(1); + rng_ = std::mt19937_64(42); + return true; } @@ -48,6 +56,66 @@ void Profiling_Service::updateProvidedResources() { void Profiling_Service::resetProvidedResources() { _perf_man.endFrame(); + notify_timer_queries(); +} + +void Profiling_Service::fill_lua_callbacks() { + frontend_resources::LuaCallbacksCollection callbacks; + + callbacks.add("mmCreateTimeQuery", + "(void)\n\tCreates a time query to time a specified number of frames.\n\tReturn UID of the query.", + {[&]() -> frontend_resources::LuaCallbacksCollection::LongResult { + auto uid = distro_(rng_); + auto fit = timer_map_.find(uid); + if (fit != timer_map_.end()) { + return frontend_resources::LuaCallbacksCollection::LongResult(0); + } + timer_map_[uid] = std::tuple(-1, -1, -1); + return frontend_resources::LuaCallbacksCollection::LongResult(uid); + }}); + + callbacks.add("mmStartTimeQuery", + "(long UID, int num_frames)\n\tStart the query specified by the UID. After num_frames a timestamp is " + "recorded.\n\tReturns timestamp at start of query.", + {[&](int64_t uid, int num_frames) -> frontend_resources::LuaCallbacksCollection::LongResult { + auto fit = timer_map_.find(uid); + if (fit != timer_map_.end()) { + std::get<0>(fit->second) = num_frames; + std::get<1>(fit->second) = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); + std::get<2>(fit->second) = -1; + return frontend_resources::LuaCallbacksCollection::LongResult(std::get<1>(fit->second)); + } + return frontend_resources::LuaCallbacksCollection::LongResult(-1); + }}); + + callbacks.add("mmPokeTimeQuery", + "(long UID)\n\tPokes the query specified by UID if num_frames have passed.\n\tReturns timestamp at end of " + "query.", + {[&](int64_t uid) -> frontend_resources::LuaCallbacksCollection::LongResult { + auto fit = timer_map_.find(uid); + if (fit != timer_map_.end() && std::get<0>(fit->second) == 0) { + auto val = std::get<2>(fit->second); + timer_map_.erase(fit->first); + return frontend_resources::LuaCallbacksCollection::LongResult(val); + } + return frontend_resources::LuaCallbacksCollection::LongResult(-1); + }}); + + auto& register_callbacks = + _requestedResourcesReferences[0] + .getResource>(); + + register_callbacks(callbacks); +} + +void Profiling_Service::notify_timer_queries() { + for (auto& query : timer_map_) { + if (std::get<0>(query.second) == 0) { + std::get<2>(query.second) = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); + } else if (std::get<0>(query.second) > 0) { + --std::get<0>(query.second); + } + } } } // namespace frontend } // namespace megamol diff --git a/frontend/services/profiling_service/Profiling_Service.hpp b/frontend/services/profiling_service/Profiling_Service.hpp index c63e0b02f3..4e25ccb2b0 100644 --- a/frontend/services/profiling_service/Profiling_Service.hpp +++ b/frontend/services/profiling_service/Profiling_Service.hpp @@ -8,6 +8,8 @@ #pragma once #include +#include +#include #include "AbstractFrontendService.hpp" #include "PerformanceManager.h" @@ -39,13 +41,27 @@ class Profiling_Service final : public AbstractFrontendService { const std::vector getRequestedResourceNames() const override { return _requestedResourcesNames; } - void setRequestedResources(std::vector resources) override {} + void setRequestedResources(std::vector resources) override { + _requestedResourcesReferences = resources; + } std::vector _providedResourceReferences; std::vector _requestedResourcesNames; + std::vector _requestedResourcesReferences; megamol::frontend_resources::PerformanceManager _perf_man; std::ofstream log_file; + +private: + void fill_lua_callbacks(); + + void notify_timer_queries(); + + std::unordered_map> timer_map_; + + std::mt19937_64 rng_; + + std::uniform_int_distribution distro_; }; } // namespace frontend From fc1abc995f1978f8b995db9faf518c789dda329d Mon Sep 17 00:00:00 2001 From: MegaMol-Bot Date: Mon, 27 Jun 2022 16:00:16 +0000 Subject: [PATCH 2/3] Format fix. --- frontend/services/profiling_service/Profiling_Service.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/services/profiling_service/Profiling_Service.hpp b/frontend/services/profiling_service/Profiling_Service.hpp index 4e25ccb2b0..293ee3d6dc 100644 --- a/frontend/services/profiling_service/Profiling_Service.hpp +++ b/frontend/services/profiling_service/Profiling_Service.hpp @@ -8,8 +8,8 @@ #pragma once #include -#include #include +#include #include "AbstractFrontendService.hpp" #include "PerformanceManager.h" From 360a988ed8bdf0da4f1127bdaf9fd97781ecfb91 Mon Sep 17 00:00:00 2001 From: gralkapk Date: Wed, 6 Jul 2022 14:36:37 +0200 Subject: [PATCH 3/3] wip of high resolution clock --- .../profiling_service/Profiling_Service.cpp | 43 ++++++++++++++----- .../profiling_service/Profiling_Service.hpp | 4 ++ 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/frontend/services/profiling_service/Profiling_Service.cpp b/frontend/services/profiling_service/Profiling_Service.cpp index 6ce979ebed..81dd0f1183 100644 --- a/frontend/services/profiling_service/Profiling_Service.cpp +++ b/frontend/services/profiling_service/Profiling_Service.cpp @@ -52,6 +52,7 @@ void Profiling_Service::close() { void Profiling_Service::updateProvidedResources() { _perf_man.startFrame(); + start_timer_queries(); } void Profiling_Service::resetProvidedResources() { @@ -65,7 +66,8 @@ void Profiling_Service::fill_lua_callbacks() { callbacks.add("mmCreateTimeQuery", "(void)\n\tCreates a time query to time a specified number of frames.\n\tReturn UID of the query.", {[&]() -> frontend_resources::LuaCallbacksCollection::LongResult { - auto uid = distro_(rng_); + //auto uid = distro_(rng_); + auto uid = ++timer_id_; auto fit = timer_map_.find(uid); if (fit != timer_map_.end()) { return frontend_resources::LuaCallbacksCollection::LongResult(0); @@ -74,23 +76,34 @@ void Profiling_Service::fill_lua_callbacks() { return frontend_resources::LuaCallbacksCollection::LongResult(uid); }}); - callbacks.add("mmStartTimeQuery", + callbacks.add("mmStartTimeQuery", "(long UID, int num_frames)\n\tStart the query specified by the UID. After num_frames a timestamp is " - "recorded.\n\tReturns timestamp at start of query.", - {[&](int64_t uid, int num_frames) -> frontend_resources::LuaCallbacksCollection::LongResult { + "recorded.\n\tReturns true if successful.", + {[&](int64_t uid, int num_frames) -> frontend_resources::LuaCallbacksCollection::BoolResult { auto fit = timer_map_.find(uid); if (fit != timer_map_.end()) { std::get<0>(fit->second) = num_frames; - std::get<1>(fit->second) = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); - std::get<2>(fit->second) = -1; - return frontend_resources::LuaCallbacksCollection::LongResult(std::get<1>(fit->second)); + /*std::get<1>(fit->second) = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); + std::get<2>(fit->second) = -1;*/ + return frontend_resources::LuaCallbacksCollection::BoolResult(true); } - return frontend_resources::LuaCallbacksCollection::LongResult(-1); + return frontend_resources::LuaCallbacksCollection::BoolResult(false); + }}); + + callbacks.add("mmPokeTimeQuery", + "(long UID)\n\tPokes the query specified by UID if num_frames have passed.", + {[&](int64_t uid) -> frontend_resources::LuaCallbacksCollection::BoolResult { + auto fit = timer_map_.find(uid); + if (fit != timer_map_.end() && std::get<0>(fit->second) == 0) { + /*auto val = std::get<2>(fit->second); + timer_map_.erase(fit->first);*/ + return frontend_resources::LuaCallbacksCollection::BoolResult(true); + } + return frontend_resources::LuaCallbacksCollection::BoolResult(false); }}); - callbacks.add("mmPokeTimeQuery", - "(long UID)\n\tPokes the query specified by UID if num_frames have passed.\n\tReturns timestamp at end of " - "query.", + callbacks.add("mmEndTimeQuery", + "(long UID)\n\tDestroys the query.\n\tReturns timestamp at end of query.", {[&](int64_t uid) -> frontend_resources::LuaCallbacksCollection::LongResult { auto fit = timer_map_.find(uid); if (fit != timer_map_.end() && std::get<0>(fit->second) == 0) { @@ -108,6 +121,14 @@ void Profiling_Service::fill_lua_callbacks() { register_callbacks(callbacks); } +void Profiling_Service::start_timer_queries() { + for (auto& query : timer_map_) { + if (std::get<0>(query.second) != 0 && std::get<1>(query.second) == -1) { + std::get<1>(query.second) = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); + } + } +} + void Profiling_Service::notify_timer_queries() { for (auto& query : timer_map_) { if (std::get<0>(query.second) == 0) { diff --git a/frontend/services/profiling_service/Profiling_Service.hpp b/frontend/services/profiling_service/Profiling_Service.hpp index 293ee3d6dc..27cc492032 100644 --- a/frontend/services/profiling_service/Profiling_Service.hpp +++ b/frontend/services/profiling_service/Profiling_Service.hpp @@ -55,6 +55,8 @@ class Profiling_Service final : public AbstractFrontendService { private: void fill_lua_callbacks(); + void start_timer_queries(); + void notify_timer_queries(); std::unordered_map> timer_map_; @@ -62,6 +64,8 @@ class Profiling_Service final : public AbstractFrontendService { std::mt19937_64 rng_; std::uniform_int_distribution distro_; + + int64_t timer_id_ = 0; }; } // namespace frontend