Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions SS14.Watchdog/Components/ProcessManagement/ProcessManagerBasic.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Dapper;
Expand All @@ -9,6 +10,7 @@
using Microsoft.Extensions.Options;
using SS14.Watchdog.Components.DataManagement;
using SS14.Watchdog.Components.ServerManagement;
using SS14.Watchdog.Configuration;

namespace SS14.Watchdog.Components.ProcessManagement;

Expand Down Expand Up @@ -69,6 +71,14 @@ public Task<IProcessHandle> StartServer(IServerInstance instance, ProcessStartDa

if (process == null)
throw new Exception("No process was started??");

if (instance.CpuCores != null)
{
var affinity = instance.CpuCores.Aggregate(0L, (mask, core) => mask | (1L << core));
process.ProcessorAffinity = (IntPtr)affinity;
_logger.LogDebug("Set processor affinity to {Affinity} for process {Pid}",
affinity, process.Id);
}
}
catch (Exception e)
{
Expand Down
8 changes: 8 additions & 0 deletions SS14.Watchdog/Components/ServerManagement/IServerInstance.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -36,6 +37,13 @@ public interface IServerInstance
/// </summary>
string InstanceDir { get; }

/// <summary>
/// List of CPU cores to use for the server process.
/// If set, the process will be restricted to run only on the specified CPU cores.
/// Example: [0, 1] means use cores 0 and 1.
/// </summary>
IReadOnlyList<int>? CpuCores { get; }

/// <summary>
/// Server has sent a ping to the watchdog confirming that it is, in fact, still alive.
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions SS14.Watchdog/Components/ServerManagement/ServerInstance.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Security.Cryptography;
Expand Down Expand Up @@ -63,6 +64,8 @@ public sealed partial class ServerInstance : IServerInstance

private IProcessHandle? _runningServer;

public IReadOnlyList<int>? CpuCores => _instanceConfig.CpuCores;

public ServerInstance(string key,
InstanceConfiguration instanceConfig,
IConfiguration configuration,
Expand Down
8 changes: 8 additions & 0 deletions SS14.Watchdog/Configuration/InstanceConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using JetBrains.Annotations;
using Microsoft.Diagnostics.NETCore.Client;
Expand Down Expand Up @@ -47,5 +48,12 @@ public sealed class InstanceConfiguration
/// </summary>
[UsedImplicitly]
public Dictionary<string, string> EnvironmentVariables { get; set; } = new();

/// <summary>
/// List of CPU cores to use for the server process.
/// If set, the process will be restricted to run only on the specified CPU cores.
/// Example: [0, 1] means use cores 0 and 1.
/// </summary>
public List<int>? CpuCores { get; set; }
}
}