Browse Source

Use thread runtime rather than time of day for measuring module CPU.

tags/v1.1.4
Andrew Belt 5 years ago
parent
commit
8c9e594c32
3 changed files with 19 additions and 38 deletions
  1. +2
    -0
      include/system.hpp
  2. +3
    -38
      src/engine/Engine.cpp
  3. +14
    -0
      src/system.cpp

+ 2
- 0
include/system.hpp View File

@@ -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.


+ 3
- 38
src/engine/Engine.cpp View File

@@ -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<Module*> modules;
std::vector<Cable*> cables;
@@ -229,7 +201,6 @@ struct Engine::Internal {
HybridBarrier engineBarrier;
HybridBarrier workerBarrier;
std::atomic<int> 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<float>(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();
}
}



+ 14
- 0
src/system.cpp View File

@@ -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];


Loading…
Cancel
Save