-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
138 lines (115 loc) · 3.46 KB
/
Program.cs
File metadata and controls
138 lines (115 loc) · 3.46 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System.Diagnostics;
#if WINDOWS
using System.Management;
#endif
if (args.Length < 1)
{
Console.Error.WriteLine("Usage: ThreadAffinityProgram <process>");
return;
}
string processName = Path.GetFileNameWithoutExtension(args[0]);
// build affinity mask, if we change disableCpuCount we can disable more processors
// defaults to disabling cpu 0 and 1..
nint affinity = BuildAffinityMask(disableCpuCount: 2);
LogStartup(processName, affinity);
ApplyAffinityToExistingProcesses(processName, affinity);
#if WINDOWS
Console.WriteLine("Using Windows WMI process watcher");
StartWindowsWatcher(processName, affinity);
#else
Console.WriteLine("Using cross-platform polling loop");
StartPollingLoop(processName, affinity);
#endif
Thread.Sleep(Timeout.Infinite);
return;
static nint BuildAffinityMask(int disableCpuCount)
{
int cpuCount = Environment.ProcessorCount;
return ((1 << cpuCount) - 1) & ~((1 << disableCpuCount) - 1);
}
static void LogStartup(string processName, nint affinity)
{
const int alignment = -13;
Console.WriteLine($"{"Processors", alignment}: {Environment.ProcessorCount}");
Console.WriteLine($"{"Affinity mask", alignment}: {affinity}");
Console.WriteLine($"{"Target", alignment}: {processName}");
}
static void ApplyAffinityToExistingProcesses(string name, nint affinity)
{
foreach (var process in Process.GetProcessesByName(name))
{
TrySetAffinity(process, affinity);
}
}
static void StartPollingLoop(string name, nint affinity)
{
var thread = new Thread(() =>
{
while (true)
{
foreach (var process in Process.GetProcessesByName(name))
{
TrySetAffinity(process, affinity);
}
Thread.Sleep(1000);
}
})
{
IsBackground = true,
Name = "AffinityPollingLoop",
};
thread.Start();
}
static void TrySetAffinity(Process process, nint affinity)
{
try
{
if (process.HasExited)
return;
if (process.ProcessorAffinity != affinity)
{
process.ProcessorAffinity = affinity;
Console.WriteLine(
$"[{DateTime.Now:HH:mm:ss}] Set affinity for {process.ProcessName} (PID {process.Id})"
);
}
else
{
Console.WriteLine(
$"[{DateTime.Now:HH:mm:ss}] Affinity already set for {process.ProcessName} (PID {process.Id})"
);
}
}
catch (Exception ex)
{
Console.Error.WriteLine($"PID {process.Id}: {ex.Message}");
}
}
#if WINDOWS
static void StartWindowsWatcher(string processName, nint affinity)
{
processName = EnsureExeSuffix(processName);
string query = $"SELECT * FROM Win32_ProcessStartTrace WHERE ProcessName = '{processName}'";
var watcher = new ManagementEventWatcher(new WqlEventQuery(query));
watcher.EventArrived += (_, e) =>
{
try
{
uint pid = (uint)e.NewEvent["ProcessID"];
var process = Process.GetProcessById((int)pid);
// Avoid early-start race conditions
Thread.Sleep(200);
TrySetAffinity(process, affinity);
}
catch (Exception ex)
{
Console.Error.WriteLine($"WMI event error: {ex.Message}");
}
};
watcher.Start();
}
static string EnsureExeSuffix(string processName) =>
processName.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)
? processName
: processName + ".exe";
#endif