TrinityCore
ProcessPriority.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
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#include "ProcessPriority.h"
19#include "Log.h"
20
21#ifdef _WIN32 // Windows
22#include <Windows.h>
23#elif defined(__linux__)
24#include <cstring>
25#include <sched.h>
26#include <sys/resource.h>
27#define PROCESS_HIGH_PRIORITY -15 // [-20, 19], default is 0
28#endif
29
30void SetProcessPriority(std::string const& logChannel, uint32 affinity, bool highPriority)
31{
33#ifdef _WIN32 // Windows
34
35 HANDLE hProcess = GetCurrentProcess();
36 if (affinity > 0)
37 {
38 ULONG_PTR appAff;
39 ULONG_PTR sysAff;
40
41 if (GetProcessAffinityMask(hProcess, &appAff, &sysAff))
42 {
43 // remove non accessible processors
44 ULONG_PTR currentAffinity = affinity & appAff;
45
46 if (!currentAffinity)
47 TC_LOG_ERROR(logChannel, "Processors marked in UseProcessors bitmask (hex) {:x} are not accessible. Accessible processors bitmask (hex): {:x}", affinity, appAff);
48 else if (SetProcessAffinityMask(hProcess, currentAffinity))
49 TC_LOG_INFO(logChannel, "Using processors (bitmask, hex): {:x}", currentAffinity);
50 else
51 TC_LOG_ERROR(logChannel, "Can't set used processors (hex): {:x}", currentAffinity);
52 }
53 }
54
55 if (highPriority)
56 {
57 if (SetPriorityClass(hProcess, HIGH_PRIORITY_CLASS))
58 TC_LOG_INFO(logChannel, "Process priority class set to HIGH");
59 else
60 TC_LOG_ERROR(logChannel, "Can't set process priority class.");
61 }
62
63#elif defined(__linux__) // Linux
64
65 if (affinity > 0)
66 {
67 cpu_set_t mask;
68 CPU_ZERO(&mask);
69
70 for (unsigned int i = 0; i < sizeof(affinity) * 8; ++i)
71 if (affinity & (1 << i))
72 CPU_SET(i, &mask);
73
74 if (sched_setaffinity(0, sizeof(mask), &mask))
75 TC_LOG_ERROR(logChannel, "Can't set used processors (hex): {:x}, error: {}", affinity, strerror(errno));
76 else
77 {
78 CPU_ZERO(&mask);
79 sched_getaffinity(0, sizeof(mask), &mask);
80 TC_LOG_INFO(logChannel, "Using processors (bitmask, hex): {:x}", *(__cpu_mask*)(&mask));
81 }
82 }
83
84 if (highPriority)
85 {
86 if (setpriority(PRIO_PROCESS, 0, PROCESS_HIGH_PRIORITY))
87 TC_LOG_ERROR(logChannel, "Can't set process priority class, error: {}", strerror(errno));
88 else
89 TC_LOG_INFO(logChannel, "Process priority class set to {}", getpriority(PRIO_PROCESS, 0));
90 }
91
92#else
93 // Suppresses unused argument warning for all other platforms
94 (void)logChannel;
95 (void)affinity;
96 (void)highPriority;
97#endif
98}
uint32_t uint32
Definition: Define.h:142
#define TC_LOG_ERROR(filterType__,...)
Definition: Log.h:165
#define TC_LOG_INFO(filterType__,...)
Definition: Log.h:159
void SetProcessPriority(std::string const &logChannel, uint32 affinity, bool highPriority)