DennisSkov

TCAdmin Random Core

Feb 17th, 2021
1,027
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.01 KB | None | 0 0
  1. import random
  2. import clr
  3. import System
  4. from System import Environment
  5. from System.Diagnostics import ProcessPriorityClass
  6.  
  7. # Returns random affinity for x cores
  8. def GetRandomAffinity():
  9.   StartCores = 3 #Change the number to how many cores the service should start with
  10.  
  11.   # Get the number of cores available on the system
  12.   cores = int(Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS")) - 1
  13.  
  14.   # 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
  15.   core_list = []
  16.   for i in xrange(cores):
  17.     i = i+1
  18.     core_list.append(i)
  19.    
  20.   # Reverse the list. On a system with 8 cores, we're gonna end up with [8, 7, 6, 5, 4, 3, 2, 1].
  21.   core_list.reverse()
  22.  
  23.   # Select 'StartCores' random cores
  24.   random_cores = random.sample(core_list, StartCores)
  25.  
  26.   number = ""
  27.   # 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.
  28.   # Example with 8 cores with 3 and 5 as random cores: 00010100
  29.   # 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.
  30.   for i in core_list:
  31.     if i in random_cores:
  32.       number += "1"
  33.     else:
  34.       number += "0"
  35.   dec = int(number, 2)
  36.   return dec
  37.  
  38.  
  39. # Set temporary affinity and priority used while the game server is starting
  40. ThisServiceController.ServiceConfig["Service.OriginalPriority"]=ThisServiceController.ServiceConfig["Service.Priority"]
  41. ThisServiceController.ServiceConfig["Service.Priority"] = ProcessPriorityClass.Idle
  42. ThisServiceController.ServiceConfig["Service.OriginalAffinity"]=ThisServiceController.ServiceConfig["Service.Affinity"]
  43. ThisServiceController.ServiceConfig["Service.Affinity"] = GetRandomAffinity()
  44.  
  45. if ThisServiceController.ServiceConfig["Service.OriginalAffinity"] == 0:
  46.   ThisServiceController.ServiceConfig["Service.OriginalAffinity"] = ThisServiceController.GetAffinityOfAllProcessors()
Advertisement
Add Comment
Please, Sign In to add comment