-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
110 lines (96 loc) · 4.29 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace SetRandomIdealProcessor
{
class Program
{
[DllImport("kernel32.dll")]
static extern IntPtr OpenThread(ThreadAccess dwDesiredAccess, bool bInheritHandle, uint dwThreadId);
[DllImport("kernel32.dll")]
static extern bool SetThreadIdealProcessor(IntPtr hThread, int dwIdealProcessor);
[DllImport("kernel32.dll")]
static extern bool SetThreadAffinityMask(IntPtr hThread, IntPtr dwThreadAffinityMask);
[DllImport("kernel32.dll")]
static extern bool GetThreadIdealProcessorEx(IntPtr hThread, ref PROCESSOR_NUMBER lpIdealProcessor);
[DllImport("kernel32.dll")]
static extern bool CloseHandle(IntPtr hObject);
[StructLayout(LayoutKind.Sequential)]
public struct PROCESSOR_NUMBER
{
public ushort Group;
public byte Number;
public byte Reserved;
}
[Flags]
public enum ThreadAccess : int
{
TERMINATE = (0x0001),
SUSPEND_RESUME = (0x0002),
GET_CONTEXT = (0x0008),
SET_CONTEXT = (0x0010),
SET_INFORMATION = (0x0020),
QUERY_INFORMATION = (0x0040),
SET_THREAD_TOKEN = (0x0080),
IMPERSONATE = (0x0100),
DIRECT_IMPERSONATION = (0x0200),
THREAD_ALL_ACCESS = (0x1F03FF)
}
static void Main(string[] args)
{
if (args.Length != 2)
{
Console.WriteLine("Usage: ThreadRebalance <PID or Process Name> <interval>");
return;
}
int pid;
if (!int.TryParse(args[0], out pid))
{
Process[] processes = Process.GetProcessesByName(args[0]);
if (processes.Length == 0)
{
Console.WriteLine("Process not found");
return;
}
pid = processes[0].Id;
}
int interval = int.Parse(args[1]);
Process process = Process.GetProcessById(pid);
Random random = new Random();
while (true)
{
foreach (ProcessThread thread in process.Threads)
{
IntPtr hThread = OpenThread(ThreadAccess.THREAD_ALL_ACCESS, false, (uint)thread.Id);
PROCESSOR_NUMBER processorNumber = new PROCESSOR_NUMBER();
if (GetThreadIdealProcessorEx(hThread, ref processorNumber))
{
Console.WriteLine("Thread {0} ideal processor is {1}", thread.Id, processorNumber.Number);
// Set a random ideal processor, unless the current one is 11 (max value)
int idealProcessor = processorNumber.Number;
{
int newIdealProcessor;
do
{
newIdealProcessor = random.Next(0, Environment.ProcessorCount);
} while (newIdealProcessor == idealProcessor);
Console.WriteLine("Setting thread {0} ideal processor to {1}", thread.Id, newIdealProcessor);
SetThreadIdealProcessor(hThread, newIdealProcessor);
idealProcessor = newIdealProcessor;
}
// Set the affinity mask to the ideal processor and the next available processor
int nextProcessor = idealProcessor < Environment.ProcessorCount - 1 ? idealProcessor + 1 : random.Next(Environment.ProcessorCount);
IntPtr affinityMask = (IntPtr)((1 << idealProcessor) | (1 << nextProcessor));
SetThreadAffinityMask(hThread, affinityMask);
}
else
{
Console.WriteLine("Thread {0} ideal processor is unknown", thread.Id);
}
CloseHandle(hThread);
}
System.Threading.Thread.Sleep(interval * 1000);
}
}
}
}