Skip to content

Commit 06ff88e

Browse files
committed
Add support for configuring process affinity
1 parent 977faf2 commit 06ff88e

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

ConfigParser.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ public static string GetText(this IConfiguration config, params string[] section
6767
return null;
6868
}
6969

70+
public static List<long> GetLongList(this IConfiguration config, params string[] sectionKeyAlternateNames)
71+
{
72+
foreach (var sectionKeyAlternateName in sectionKeyAlternateNames)
73+
{
74+
var list = config.GetSection(sectionKeyAlternateName).GetList();
75+
if (list.Count > 0)
76+
{
77+
return list
78+
.Select(text => long.Parse(text)) //NB! if the parameter exists then it must be in a proper numeric format
79+
.ToList();
80+
}
81+
}
82+
83+
return new List<long>();
84+
}
85+
7086
public static List<string> GetListUpperOnWindows(this IConfiguration config, bool? caseSensitiveFilenames, params string[] sectionKeyAlternateNames)
7187
{
7288
if (caseSensitiveFilenames == false || (caseSensitiveFilenames == null && !IsLinux)) //assume NTFS or FAT filesystem which are usually case-insensitive

Program.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ internal static class Global
3838

3939

4040
public static bool UseIdlePriority = false;
41+
public static List<long> Affinity = new List<long>();
4142

4243
public static bool ShowErrorAlerts = true;
4344
public static bool LogInitialScan = false;
@@ -154,8 +155,9 @@ private static void Main()
154155

155156

156157
Global.UseIdlePriority = fileConfig.GetTextUpper("UseIdlePriority") == "TRUE"; //default is false
158+
Global.Affinity = fileConfig.GetLongList("Affinity");
159+
157160

158-
159161
Global.ShowErrorAlerts = fileConfig.GetTextUpper("ShowErrorAlerts") != "FALSE"; //default is true
160162
Global.LogInitialScan = fileConfig.GetTextUpper("LogInitialScan") == "TRUE"; //default is false
161163
Global.LogToFile = fileConfig.GetTextUpper("LogToFile") == "TRUE"; //default is false
@@ -245,6 +247,29 @@ private static async Task MainTask()
245247
}
246248
}
247249

250+
if (Global.Affinity.Count > 0)
251+
{
252+
try
253+
{
254+
var CurrentProcess = Process.GetCurrentProcess();
255+
256+
long affinityMask = 0;
257+
foreach (var affinityEntry in Global.Affinity)
258+
{
259+
if (affinityEntry < 0 || affinityEntry > 63)
260+
throw new ArgumentException("Affinity");
261+
262+
affinityMask |= (long)1 << (int)affinityEntry;
263+
}
264+
265+
CurrentProcess.ProcessorAffinity = new IntPtr(affinityMask);
266+
}
267+
catch (Exception)
268+
{
269+
Console.WriteLine("Unable to set affinity.");
270+
}
271+
}
272+
248273

249274
ThreadPool.SetMaxThreads(16, 16); //TODO: config
250275

appsettings.example.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"Files": {
33

44
"UseIdlePriority": false,
5+
"Affinity": [0],
6+
7+
58
"MaxFileSizeMB": 2048,
69
"RetryCountOnSrcFileOpenError": 10,
710

0 commit comments

Comments
 (0)