TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ProcessPriority.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef _PROCESSPRIO_H
19 #define _PROCESSPRIO_H
20 
21 #include "Configuration/Config.h"
22 
23 #ifdef __linux__
24 #include <sched.h>
25 #include <sys/resource.h>
26 #define PROCESS_HIGH_PRIORITY -15 // [-20, 19], default is 0
27 #endif
28 
29 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 }
104 
105 #endif
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