Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import clr
- import System
- from System import Environment
- from System.Diagnostics import ProcessPriorityClass
- # Returns random affinity for x cores
- def GetRandomAffinity():
- StartCores = 3 #Change the number to how many cores the service should start with
- # Get the number of cores available on the system
- cores = int(Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS")) - 1
- # Initiate a list that we're gonna use. Each core will be added to the list like [1, 2, 3, 4...]. We're gonna reverse this list later
- core_list = []
- for i in xrange(cores):
- i = i+1
- core_list.append(i)
- # Reverse the list. On a system with 8 cores, we're gonna end up with [8, 7, 6, 5, 4, 3, 2, 1].
- core_list.reverse()
- # Select 'StartCores' random cores
- random_cores = random.sample(core_list, StartCores)
- number = ""
- # Iterate through the list of cores and get binary number - if we pass a number from random_cores, we add 1 number variable. If not, we add 0.
- # Example with 8 cores with 3 and 5 as random cores: 00010100
- # When we have the binary number, we need to convert it to dec and return this. 00010100 will return 20 - and this is CPU 3 and 5.
- for i in core_list:
- if i in random_cores:
- number += "1"
- else:
- number += "0"
- dec = int(number, 2)
- return dec
- # Set temporary affinity and priority used while the game server is starting
- ThisServiceController.ServiceConfig["Service.OriginalPriority"]=ThisServiceController.ServiceConfig["Service.Priority"]
- ThisServiceController.ServiceConfig["Service.Priority"] = ProcessPriorityClass.Idle
- ThisServiceController.ServiceConfig["Service.OriginalAffinity"]=ThisServiceController.ServiceConfig["Service.Affinity"]
- ThisServiceController.ServiceConfig["Service.Affinity"] = GetRandomAffinity()
- if ThisServiceController.ServiceConfig["Service.OriginalAffinity"] == 0:
- ThisServiceController.ServiceConfig["Service.OriginalAffinity"] = ThisServiceController.GetAffinityOfAllProcessors()
Advertisement
Add Comment
Please, Sign In to add comment