From 8c9e594c32b1058f3cd2c11fb048b218ee695834 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Fri, 9 Aug 2019 13:27:14 -0400 Subject: [PATCH] Use thread runtime rather than time of day for measuring module CPU. --- include/system.hpp | 2 ++ src/engine/Engine.cpp | 41 +++-------------------------------------- src/system.cpp | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 38 deletions(-) diff --git a/include/system.hpp b/include/system.hpp index 2f7ab920..c4519498 100644 --- a/include/system.hpp +++ b/include/system.hpp @@ -31,6 +31,8 @@ int getLogicalCoreCount(); void setThreadName(const std::string& name); /** Sets the current thread to be high-priority. */ void setThreadRealTime(bool realTime); +/** Returns the number of seconds the current thread has been active. */ +double getThreadTime(); /** Returns the caller's human-readable stack trace with "\n"-separated lines. */ std::string getStackTrace(); /** Opens a URL, also happens to work with PDFs and folders. diff --git a/src/engine/Engine.cpp b/src/engine/Engine.cpp index 18d3f2c3..33a11828 100644 --- a/src/engine/Engine.cpp +++ b/src/engine/Engine.cpp @@ -171,34 +171,6 @@ struct EngineWorker { }; -struct ProfilerWorker { - Engine* engine; - std::thread thread; - bool running = false; - - void start() { - assert(!running); - running = true; - thread = std::thread([&] { - run(); - }); - } - - void stop() { - running = false; - if (thread.joinable()) - thread.join(); - } - - void run() { - while (running) { - DEBUG("sample"); - std::this_thread::sleep_for(std::chrono::nanoseconds(100000000)); - } - } -}; - - struct Engine::Internal { std::vector modules; std::vector cables; @@ -229,7 +201,6 @@ struct Engine::Internal { HybridBarrier engineBarrier; HybridBarrier workerBarrier; std::atomic workerModuleIndex; - ProfilerWorker profilerWorker; }; @@ -276,12 +247,12 @@ static void Engine_stepModules(Engine* that, int threadId) { if (!module->bypass) { // Step module if (settings::cpuMeter) { - auto startTime = std::chrono::high_resolution_clock::now(); + double startTime = system::getThreadTime(); module->process(processCtx); - auto stopTime = std::chrono::high_resolution_clock::now(); - float cpuTime = std::chrono::duration(stopTime - startTime).count(); + double stopTime = system::getThreadTime(); + float cpuTime = stopTime - startTime; // Smooth CPU time const float cpuTau = 2.f /* seconds */; module->cpuTime += (cpuTime - module->cpuTime) * sampleTime / cpuTau; @@ -384,9 +355,6 @@ static void Engine_relaunchWorkers(Engine* that, int threadCount, bool realTime) Engine::Internal* internal = that->internal; if (internal->threadCount > 0) { - // Stop profiler - // internal->profilerWorker.stop(); - // Stop engine workers for (EngineWorker& worker : internal->workers) { worker.requestStop(); @@ -420,9 +388,6 @@ static void Engine_relaunchWorkers(Engine* that, int threadCount, bool realTime) worker.engine = that; worker.start(); } - - // Start profiler - // internal->profilerWorker.start(); } } diff --git a/src/system.cpp b/src/system.cpp index a6d0e715..040e1844 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -159,6 +159,20 @@ void setThreadRealTime(bool realTime) { } +double getThreadTime() { +#if defined ARCH_LIN || defined ARCH_MAC + struct timespec ts; + clockid_t cid; + pthread_getcpuclockid(pthread_self(), &cid); + clock_gettime(cid, &ts); + return ts.tv_sec + ts.tv_nsec / 1000000000.0; +#elif defined ARCH_WIN + // TODO + return 0.0; +#endif +} + + std::string getStackTrace() { int stackLen = 128; void* stack[stackLen];