TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ProcessPriority.h File Reference
+ Include dependency graph for ProcessPriority.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void SetProcessPriority (const std::string &logChannel)
 

Function Documentation

void SetProcessPriority ( const std::string &  logChannel)
30 {
31 // Suppresses Mac OS X Warning since logChannel isn't used.
32 #if PLATFORM_APPLE
33  (void)logChannel;
34 #endif
35 
36 #if defined(_WIN32) || defined(__linux__)
37 
39  uint32 affinity = sConfigMgr->GetIntDefault("UseProcessors", 0);
40  bool highPriority = sConfigMgr->GetBoolDefault("ProcessPriority", false);
41 
42 #ifdef _WIN32 // Windows
43 
44  HANDLE hProcess = GetCurrentProcess();
45  if (affinity > 0)
46  {
47  ULONG_PTR appAff;
48  ULONG_PTR sysAff;
49 
50  if (GetProcessAffinityMask(hProcess, &appAff, &sysAff))
51  {
52  // remove non accessible processors
53  ULONG_PTR currentAffinity = affinity & appAff;
54 
55  if (!currentAffinity)
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);
59  else
60  TC_LOG_ERROR(logChannel, "Can't set used processors (hex): %x", currentAffinity);
61  }
62  }
63 
64  if (highPriority)
65  {
66  if (SetPriorityClass(hProcess, HIGH_PRIORITY_CLASS))
67  TC_LOG_INFO(logChannel, "Process priority class set to HIGH");
68  else
69  TC_LOG_ERROR(logChannel, "Can't set process priority class.");
70  }
71 
72 #else // Linux
73 
74  if (affinity > 0)
75  {
76  cpu_set_t mask;
77  CPU_ZERO(&mask);
78 
79  for (unsigned int i = 0; i < sizeof(affinity) * 8; ++i)
80  if (affinity & (1 << i))
81  CPU_SET(i, &mask);
82 
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));
85  else
86  {
87  CPU_ZERO(&mask);
88  sched_getaffinity(0, sizeof(mask), &mask);
89  TC_LOG_INFO(logChannel, "Using processors (bitmask, hex): %lx", *(__cpu_mask*)(&mask));
90  }
91  }
92 
93  if (highPriority)
94  {
95  if (setpriority(PRIO_PROCESS, 0, PROCESS_HIGH_PRIORITY))
96  TC_LOG_ERROR(logChannel, "Can't set process priority class, error: %s", strerror(errno));
97  else
98  TC_LOG_INFO(logChannel, "Process priority class set to %i", getpriority(PRIO_PROCESS, 0));
99  }
100 
101 #endif
102 #endif
103 }
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

+ Here is the caller graph for this function: