aboutsummaryrefslogtreecommitdiff
path: root/src/common/Threading/ProcessPriority.h
diff options
context:
space:
mode:
authorStormBytePP <stormbyte@gmail.com>2015-08-19 19:02:10 +0200
committerStormBytePP <stormbyte@gmail.com>2015-08-21 17:52:42 +0200
commit1d2aafd39bcb79a67357d198ce9b2345642fdd39 (patch)
treec32cf1c3717625c60da59c82ba5a4fca2530119a /src/common/Threading/ProcessPriority.h
parent172293acee1607727ebd8070ab3e1390590d02a8 (diff)
Core/Build: Merge common library and move database out of shared
Diffstat (limited to 'src/common/Threading/ProcessPriority.h')
-rw-r--r--src/common/Threading/ProcessPriority.h105
1 files changed, 105 insertions, 0 deletions
diff --git a/src/common/Threading/ProcessPriority.h b/src/common/Threading/ProcessPriority.h
new file mode 100644
index 00000000000..9f70ab0ac03
--- /dev/null
+++ b/src/common/Threading/ProcessPriority.h
@@ -0,0 +1,105 @@
+/*
+ * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _PROCESSPRIO_H
+#define _PROCESSPRIO_H
+
+#include "Configuration/Config.h"
+
+#ifdef __linux__
+#include <sched.h>
+#include <sys/resource.h>
+#define PROCESS_HIGH_PRIORITY -15 // [-20, 19], default is 0
+#endif
+
+void SetProcessPriority(const std::string& logChannel)
+{
+// Suppresses Mac OS X Warning since logChannel isn't used.
+#if PLATFORM_APPLE
+ (void)logChannel;
+#endif
+
+#if defined(_WIN32) || defined(__linux__)
+
+ ///- Handle affinity for multiple processors and process priority
+ uint32 affinity = sConfigMgr->GetIntDefault("UseProcessors", 0);
+ bool highPriority = sConfigMgr->GetBoolDefault("ProcessPriority", false);
+
+#ifdef _WIN32 // Windows
+
+ HANDLE hProcess = GetCurrentProcess();
+ if (affinity > 0)
+ {
+ ULONG_PTR appAff;
+ ULONG_PTR sysAff;
+
+ if (GetProcessAffinityMask(hProcess, &appAff, &sysAff))
+ {
+ // remove non accessible processors
+ ULONG_PTR currentAffinity = affinity & appAff;
+
+ if (!currentAffinity)
+ TC_LOG_ERROR(logChannel, "Processors marked in UseProcessors bitmask (hex) %x are not accessible. Accessible processors bitmask (hex): %x", affinity, appAff);
+ else if (SetProcessAffinityMask(hProcess, currentAffinity))
+ TC_LOG_INFO(logChannel, "Using processors (bitmask, hex): %x", currentAffinity);
+ else
+ TC_LOG_ERROR(logChannel, "Can't set used processors (hex): %x", currentAffinity);
+ }
+ }
+
+ if (highPriority)
+ {
+ if (SetPriorityClass(hProcess, HIGH_PRIORITY_CLASS))
+ TC_LOG_INFO(logChannel, "Process priority class set to HIGH");
+ else
+ TC_LOG_ERROR(logChannel, "Can't set process priority class.");
+ }
+
+#else // Linux
+
+ if (affinity > 0)
+ {
+ cpu_set_t mask;
+ CPU_ZERO(&mask);
+
+ for (unsigned int i = 0; i < sizeof(affinity) * 8; ++i)
+ if (affinity & (1 << i))
+ CPU_SET(i, &mask);
+
+ if (sched_setaffinity(0, sizeof(mask), &mask))
+ TC_LOG_ERROR(logChannel, "Can't set used processors (hex): %x, error: %s", affinity, strerror(errno));
+ else
+ {
+ CPU_ZERO(&mask);
+ sched_getaffinity(0, sizeof(mask), &mask);
+ TC_LOG_INFO(logChannel, "Using processors (bitmask, hex): %lx", *(__cpu_mask*)(&mask));
+ }
+ }
+
+ if (highPriority)
+ {
+ if (setpriority(PRIO_PROCESS, 0, PROCESS_HIGH_PRIORITY))
+ TC_LOG_ERROR(logChannel, "Can't set process priority class, error: %s", strerror(errno));
+ else
+ TC_LOG_INFO(logChannel, "Process priority class set to %i", getpriority(PRIO_PROCESS, 0));
+ }
+
+#endif
+#endif
+}
+
+#endif