From a6d18d85274e50a311f776d52413e06ce2c4828c Mon Sep 17 00:00:00 2001 From: Nicolas Pope <nwpope@utu.fi> Date: Sun, 19 Jul 2020 10:23:21 +0300 Subject: [PATCH] Improve timer efficiency --- applications/gui2/src/main.cpp | 3 ++ components/common/cpp/src/timer.cpp | 36 +++++++++++++++++-- .../rgbd-sources/include/ftl/rgbd/source.hpp | 2 +- components/streams/src/builder.cpp | 2 +- 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/applications/gui2/src/main.cpp b/applications/gui2/src/main.cpp index 7d422d98d..85d6c38d3 100644 --- a/applications/gui2/src/main.cpp +++ b/applications/gui2/src/main.cpp @@ -155,6 +155,9 @@ int main(int argc, char **argv) { Pylon::PylonAutoInitTerm autoInitTerm; #endif + // Note: This causes 100% CPU use but prevents the random frame drops. + ftl::timer::setHighPrecision(true); + { nanogui::init(); diff --git a/components/common/cpp/src/timer.cpp b/components/common/cpp/src/timer.cpp index 0121c046d..890f7dc83 100644 --- a/components/common/cpp/src/timer.cpp +++ b/components/common/cpp/src/timer.cpp @@ -89,6 +89,30 @@ static void waitTimePoint() { } } + /*while (msdelay >= 10 && sincelast != mspf) { + sleep_for(milliseconds(5)); + now = get_time(); + msdelay = mspf - (now % mspf); + }*/ + + if (msdelay >= 2 && sincelast != mspf) { + UNIQUE_LOCK(mtx, lk); + auto idle_job = jobs[kTimerIdle1].begin(); + while (idle_job != jobs[kTimerIdle1].end() && msdelay >= 2 && sincelast != mspf) { + (*idle_job).active = true; + bool doremove = !(*idle_job).job.trigger(now); + + if (doremove) { + idle_job = jobs[kTimerIdle1].erase(idle_job); + LOG(INFO) << "Timer job removed"; + } else { + (*idle_job++).active = false; + } + now = get_time(); + msdelay = mspf - (now % mspf); + } + } + if (hprec_) { // Spin loop until exact grab time // Accurate to around 4 micro seconds. @@ -194,12 +218,20 @@ static void trigger_jobs() { auto *pj = &j; - ftl::pool.push([pj,ts](int id) { + // If last job in list then do in this thread + if (active_jobs == jobs[kTimerMain].size()+1) { bool doremove = !pj->job.trigger(ts); pj->active = false; active_jobs--; if (doremove) removeJob(pj->id); - }); + } else { + ftl::pool.push([pj,ts](int id) { + bool doremove = !pj->job.trigger(ts); + pj->active = false; + active_jobs--; + if (doremove) removeJob(pj->id); + }); + } } // Final cleanup of stale jobs diff --git a/components/rgbd-sources/include/ftl/rgbd/source.hpp b/components/rgbd-sources/include/ftl/rgbd/source.hpp index 7773756f2..c7cc7d574 100644 --- a/components/rgbd-sources/include/ftl/rgbd/source.hpp +++ b/components/rgbd-sources/include/ftl/rgbd/source.hpp @@ -92,7 +92,7 @@ class Source : public ftl::Configurable, public ftl::data::DiscreteSource { BaseSourceImpl *impl_; SHARED_MUTEX mutex_; cudaStream_t stream_; - bool is_retrieving; + std::atomic_bool is_retrieving; void _swap(); }; diff --git a/components/streams/src/builder.cpp b/components/streams/src/builder.cpp index 8d2e60f8b..72dd73c5a 100644 --- a/components/streams/src/builder.cpp +++ b/components/streams/src/builder.cpp @@ -138,7 +138,7 @@ void IntervalSourceBuilder::start() { // TODO: Do in parallel... for (auto *s : srcs_) { if (!s->retrieve(fs->firstFrame())) { - LOG(WARNING) << "Frame was skipping"; + LOG(WARNING) << "Frame is being skipped: " << ts; } } -- GitLab