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