Browse Source

Set realtime thread priority for engine and engine worker threads.

tags/v1.0.0
Andrew Belt 6 years ago
parent
commit
e3c648a7f5
3 changed files with 27 additions and 4 deletions
  1. +1
    -0
      include/system.hpp
  2. +4
    -3
      src/engine/Engine.cpp
  3. +22
    -1
      src/system.cpp

+ 1
- 0
include/system.hpp View File

@@ -16,6 +16,7 @@ void createDirectory(const std::string &path);
/** Currently this lies and returns the number of logical cores instead. */ /** Currently this lies and returns the number of logical cores instead. */
int getPhysicalCoreCount(); int getPhysicalCoreCount();
void setThreadName(const std::string &name); void setThreadName(const std::string &name);
void setThreadRealTime();


/** Opens a URL, also happens to work with PDFs and folders. /** Opens a URL, also happens to work with PDFs and folders.
Shell injection is possible, so make sure the URL is trusted or hard coded. Shell injection is possible, so make sure the URL is trusted or hard coded.


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

@@ -101,6 +101,7 @@ struct EngineWorker {
void start() { void start() {
thread = std::thread([&] { thread = std::thread([&] {
system::setThreadName("Engine worker"); system::setThreadName("Engine worker");
system::setThreadRealTime();
run(); run();
}); });
} }
@@ -298,10 +299,10 @@ static void Engine_step(Engine *engine) {
} }


static void Engine_run(Engine *engine) { static void Engine_run(Engine *engine) {
#if defined ARCH_LIN
// Name thread
// Set up thread
system::setThreadName("Engine"); system::setThreadName("Engine");
#endif
system::setThreadRealTime();

// Set CPU to flush-to-zero (FTZ) and denormals-are-zero (DAZ) mode // Set CPU to flush-to-zero (FTZ) and denormals-are-zero (DAZ) mode
// https://software.intel.com/en-us/node/682949 // https://software.intel.com/en-us/node/682949
_MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON); _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);


+ 22
- 1
src/system.cpp View File

@@ -3,9 +3,15 @@
#include <dirent.h> #include <dirent.h>
#include <sys/stat.h> #include <sys/stat.h>


#if defined ARCH_LIN
#include <pthread.h>
#include <sched.h>
#endif

#if defined ARCH_WIN #if defined ARCH_WIN
#include <windows.h> #include <windows.h>
#include <shellapi.h> #include <shellapi.h>
#include <processthreadsapi.h>
#endif #endif




@@ -84,8 +90,23 @@ int getPhysicalCoreCount() {
} }


void setThreadName(const std::string &name) { void setThreadName(const std::string &name) {
#if defined ARCH_LIN
#if defined ARCH_LIN || defined ARCH_MAC
pthread_setname_np(pthread_self(), name.c_str()); pthread_setname_np(pthread_self(), name.c_str());
#elif defined ARCH_WIN
SetThreadDescription(GetCurrentThread(), name.c_str());
#endif
}

void setThreadRealTime() {
#if defined ARCH_LIN || defined ARCH_MAC
int policy = SCHED_RR;
struct sched_param param;
param.sched_priority = sched_get_priority_max(policy);
pthread_setschedparam(pthread_self(), policy, &param);
#elif defined ARCH_WIN
// Set entire process as realtime
SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
#endif #endif
} }




Loading…
Cancel
Save