18 #ifndef _PROCESSPRIO_H
19 #define _PROCESSPRIO_H
25 #include <sys/resource.h>
26 #define PROCESS_HIGH_PRIORITY -15 // [-20, 19], default is 0
36 #if defined(_WIN32) || defined(__linux__)
40 bool highPriority =
sConfigMgr->GetBoolDefault(
"ProcessPriority",
false);
42 #ifdef _WIN32 // Windows
44 HANDLE hProcess = GetCurrentProcess();
50 if (GetProcessAffinityMask(hProcess, &appAff, &sysAff))
53 ULONG_PTR currentAffinity = affinity & appAff;
56 TC_LOG_ERROR(logChannel,
"Processors marked in UseProcessors bitmask (hex) %x are not accessible. Accessible processors bitmask (hex): %x", affinity, appAff);
57 else if (SetProcessAffinityMask(hProcess, currentAffinity))
58 TC_LOG_INFO(logChannel,
"Using processors (bitmask, hex): %x", currentAffinity);
60 TC_LOG_ERROR(logChannel,
"Can't set used processors (hex): %x", currentAffinity);
66 if (SetPriorityClass(hProcess, HIGH_PRIORITY_CLASS))
67 TC_LOG_INFO(logChannel,
"Process priority class set to HIGH");
69 TC_LOG_ERROR(logChannel,
"Can't set process priority class.");
79 for (
unsigned int i = 0; i <
sizeof(affinity) * 8; ++i)
80 if (affinity & (1 << i))
83 if (sched_setaffinity(0,
sizeof(mask), &mask))
84 TC_LOG_ERROR(logChannel,
"Can't set used processors (hex): %x, error: %s", affinity, strerror(errno));
88 sched_getaffinity(0,
sizeof(mask), &mask);
89 TC_LOG_INFO(logChannel,
"Using processors (bitmask, hex): %lx", *(__cpu_mask*)(&mask));
95 if (setpriority(PRIO_PROCESS, 0, PROCESS_HIGH_PRIORITY))
96 TC_LOG_ERROR(logChannel,
"Can't set process priority class, error: %s", strerror(errno));
98 TC_LOG_INFO(logChannel,
"Process priority class set to %i", getpriority(PRIO_PROCESS, 0));
void * HANDLE
Definition: CascPort.h:146
#define sConfigMgr
Definition: Config.h:61
uint32_t uint32
Definition: Define.h:150
#define TC_LOG_INFO(filterType__,...)
Definition: Log.h:201
#define TC_LOG_ERROR(filterType__,...)
Definition: Log.h:207
void SetProcessPriority(const std::string &logChannel)
Definition: ProcessPriority.h:29