using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
namespace XFE.SeAgent.Plugin.Performance
{
// Optional locality tuning for CPUs with unequal L3 groups. Never assumes CPU numbering.
internal sealed class CacheCoreAffinity : IDisposable
{
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool GetLogicalProcessorInformation(IntPtr buffer, ref uint length);
[DllImport("kernel32.dll")]
private static extern ushort GetActiveProcessorGroupCount();
[DllImport("kernel32.dll")]
private static extern uint GetCurrentThreadId();
[DllImport("kernel32.dll",SetLastError=true)]
private static extern IntPtr OpenThread(uint access,bool inherit,uint id);
[DllImport("kernel32.dll",SetLastError=true)]
private static extern UIntPtr SetThreadAffinityMask(IntPtr thread,UIntPtr mask);
[DllImport("kernel32.dll",SetLastError=true)]
private static extern bool GetThreadGroupAffinity(IntPtr thread,out GroupAffinity affinity);
[DllImport("kernel32.dll")]
private static extern bool CloseHandle(IntPtr handle);
[StructLayout(LayoutKind.Sequential)]
private struct GroupAffinity { public UIntPtr Mask; public ushort Group,Reserved0,Reserved1,Reserved2; }
private readonly ulong original = ReadMask();
private ulong applied;
private IntPtr mainThread;
private ulong mainOriginal,mainApplied;
private uint mainId;
private sealed class ThreadMask { public IntPtr Handle; public ulong Original,Applied; }
private readonly Dictionary<uint,ThreadMask> renderThreads=new Dictionary<uint,ThreadMask>();
private volatile bool pinRender;
private int affinityGeneration;
[ThreadStatic] private static CacheCoreAffinity lastOwner;
[ThreadStatic] private static int lastGeneration;
public bool Enabled { get; private set; }
public bool MainThreadOnly { get; private set; }
public bool PinRenderWorkers => pinRender;
public int PinnedRenderThreads { get { lock(renderThreads) return renderThreads.Count; } }
public string ThreadError { get; private set; }
public uint SelectedCacheBytes { get; private set; }
public string CurrentMask => ReadMask().ToString("X");
private struct Cache { public ulong Mask; public uint Bytes; }
private static ulong ReadMask()
{
using (var process = Process.GetCurrentProcess()) return unchecked((ulong)process.ProcessorAffinity.ToInt64());
}
private static void WriteMask(ulong mask)
{
using (var process = Process.GetCurrentProcess()) process.ProcessorAffinity = new IntPtr(unchecked((long)mask));
}
public void MarkRenderThread()
{
if(!pinRender) return;
int generation=Volatile.Read(ref affinityGeneration);
if(ReferenceEquals(lastOwner,this) && lastGeneration==generation) return;
lock(renderThreads) {
if(!pinRender) return;
uint id=GetCurrentThreadId();
if(id!=mainId && !renderThreads.ContainsKey(id)) {
var handle=OpenThread(0x60,false,id);
if(handle==IntPtr.Zero) {ThreadError="Open render thread: "+Marshal.GetLastWin32Error();return;}
ulong prior=SetThreadAffinityMask(handle,new UIntPtr(mainApplied)).ToUInt64();
if(prior==0) {ThreadError="Pin render thread: "+Marshal.GetLastWin32Error();CloseHandle(handle);return;}
renderThreads.Add(id,new ThreadMask {Handle=handle,Original=prior,Applied=mainApplied});
}
lastOwner=this;lastGeneration=generation;
}
}
public void Set(bool enabled,bool mainThreadOnly=false,bool renderWorkers=false)
{
renderWorkers=enabled && mainThreadOnly && renderWorkers;
if (enabled == Enabled && (!enabled || mainThreadOnly == MainThreadOnly && renderWorkers==pinRender)) return;
lock(renderThreads) {
pinRender=false;Interlocked.Increment(ref affinityGeneration);
foreach(var saved in renderThreads.Values) {
if(GetThreadGroupAffinity(saved.Handle,out var current) && current.Mask.ToUInt64()==saved.Applied)
SetThreadAffinityMask(saved.Handle,new UIntPtr(saved.Original));
CloseHandle(saved.Handle);
}
renderThreads.Clear();ThreadError=null;
}
if(mainThread!=IntPtr.Zero) {
if(GetThreadGroupAffinity(mainThread,out var affinity) && affinity.Mask.ToUInt64()==mainApplied)
SetThreadAffinityMask(mainThread,new UIntPtr(mainOriginal));
CloseHandle(mainThread);mainThread=IntPtr.Zero;
}
if(Enabled && applied!=original && ReadMask()==applied) WriteMask(original);
Enabled=false;MainThreadOnly=false;
if (!enabled)
{
// Restoration above respects externally changed process/thread masks.
Enabled = false; SelectedCacheBytes = 0; return;
}
if (IntPtr.Size != 8 || GetActiveProcessorGroupCount() != 1)
throw new NotSupportedException("Cache affinity requires x64 and a single Windows processor group.");
uint length = 0;
GetLogicalProcessorInformation(IntPtr.Zero, ref length);
if (length == 0 || length % 32 != 0 || length > 1048576)
throw new InvalidOperationException("Unsupported CPU topology record size.");
IntPtr buffer = Marshal.AllocHGlobal((int)length);
var caches = new List<Cache>();
try
{
if (!GetLogicalProcessorInformation(buffer, ref length)) throw new Win32Exception(Marshal.GetLastWin32Error());
// SYSTEM_LOGICAL_PROCESSOR_INFORMATION: x64 mask at 0, relationship at 8,
// 16-byte union at 16; CACHE_DESCRIPTOR level at 16 and size at 20.
for (int offset = 0; offset < length; offset += 32)
{
var record = IntPtr.Add(buffer, offset);
if (Marshal.ReadInt32(record, 8) != 2 || Marshal.ReadByte(record, 16) != 3) continue;
ulong mask = unchecked((ulong)Marshal.ReadInt64(record)) & original;
if (mask != 0) caches.Add(new Cache { Mask = mask, Bytes = unchecked((uint)Marshal.ReadInt32(record, 20)) });
}
}
finally { Marshal.FreeHGlobal(buffer); }
caches.Sort((a, b) => b.Bytes.CompareTo(a.Bytes));
if (caches.Count < 2 || caches[0].Bytes <= caches[1].Bytes)
throw new NotSupportedException("No uniquely larger L3 cache group is available; keep normal CPU scheduling.");
if(mainThreadOnly) {
// Retain cache locality for the simulation thread while allowing
// existing render/Havok/jobs to run on the other CPU cores too.
// This method is invoked on the game thread by the command queue.
mainId=GetCurrentThreadId();mainThread=OpenThread(0x60,false,mainId);
if(mainThread==IntPtr.Zero) throw new Win32Exception(Marshal.GetLastWin32Error());
mainApplied=caches[0].Mask;
mainOriginal=SetThreadAffinityMask(mainThread,new UIntPtr(mainApplied)).ToUInt64();
if(mainOriginal==0) {int error=Marshal.GetLastWin32Error();CloseHandle(mainThread);mainThread=IntPtr.Zero;throw new Win32Exception(error);}
applied=ReadMask();MainThreadOnly=true;pinRender=renderWorkers;
} else {WriteMask(caches[0].Mask);applied=caches[0].Mask;}
SelectedCacheBytes = caches[0].Bytes; Enabled = true;
}
public void Dispose() { Set(false); }
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
namespace XFE.SeAgent.Plugin.Performance
{
// Optional locality tuning for CPUs with unequal L3 groups. Never assumes CPU numbering.
internal sealed class CacheCoreAffinity : IDisposable
{
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool GetLogicalProcessorInformation(IntPtr buffer, ref uint length);
[DllImport("kernel32.dll")]
private static extern ushort GetActiveProcessorGroupCount();
[DllImport("kernel32.dll")]
private static extern uint GetCurrentThreadId();
[DllImport("kernel32.dll",SetLastError=true)]
private static extern IntPtr OpenThread(uint access,bool inherit,uint id);
[DllImport("kernel32.dll",SetLastError=true)]
private static extern UIntPtr SetThreadAffinityMask(IntPtr thread,UIntPtr mask);
[DllImport("kernel32.dll",SetLastError=true)]
private static extern bool GetThreadGroupAffinity(IntPtr thread,out GroupAffinity affinity);
[DllImport("kernel32.dll")]
private static extern bool CloseHandle(IntPtr handle);
[StructLayout(LayoutKind.Sequential)]
private struct GroupAffinity { public UIntPtr Mask; public ushort Group,Reserved0,Reserved1,Reserved2; }
private readonly ulong original = ReadMask();
private ulong applied;
private IntPtr mainThread;
private ulong mainOriginal,mainApplied;
private uint mainId;
private sealed class ThreadMask { public IntPtr Handle; public ulong Original,Applied; }
private readonly Dictionary<uint,ThreadMask> renderThreads=new Dictionary<uint,ThreadMask>();
private volatile bool pinRender;
private int affinityGeneration;
[ThreadStatic] private static CacheCoreAffinity lastOwner;
[ThreadStatic] private static int lastGeneration;
public bool Enabled { get; private set; }
public bool MainThreadOnly { get; private set; }
public bool PinRenderWorkers => pinRender;
public int PinnedRenderThreads { get { lock(renderThreads) return renderThreads.Count; } }
public string ThreadError { get; private set; }
public uint SelectedCacheBytes { get; private set; }
public string CurrentMask => ReadMask().ToString("X");
private struct Cache { public ulong Mask; public uint Bytes; }
private static ulong ReadMask()
{
using (var process = Process.GetCurrentProcess()) return unchecked((ulong)process.ProcessorAffinity.ToInt64());
}
private static void WriteMask(ulong mask)
{
using (var process = Process.GetCurrentProcess()) process.ProcessorAffinity = new IntPtr(unchecked((long)mask));
}
public void MarkRenderThread()
{
if(!pinRender) return;
int generation=Volatile.Read(ref affinityGeneration);
if(ReferenceEquals(lastOwner,this) && lastGeneration==generation) return;
lock(renderThreads) {
if(!pinRender) return;
uint id=GetCurrentThreadId();
if(id!=mainId && !renderThreads.ContainsKey(id)) {
var handle=OpenThread(0x60,false,id);
if(handle==IntPtr.Zero) {ThreadError="Open render thread: "+Marshal.GetLastWin32Error();return;}
ulong prior=SetThreadAffinityMask(handle,new UIntPtr(mainApplied)).ToUInt64();
if(prior==0) {ThreadError="Pin render thread: "+Marshal.GetLastWin32Error();CloseHandle(handle);return;}
renderThreads.Add(id,new ThreadMask {Handle=handle,Original=prior,Applied=mainApplied});
}
lastOwner=this;lastGeneration=generation;
}
}
public void Set(bool enabled,bool mainThreadOnly=false,bool renderWorkers=false)
{
renderWorkers=enabled && mainThreadOnly && renderWorkers;
if (enabled == Enabled && (!enabled || mainThreadOnly == MainThreadOnly && renderWorkers==pinRender)) return;
lock(renderThreads) {
pinRender=false;Interlocked.Increment(ref affinityGeneration);
foreach(var saved in renderThreads.Values) {
if(GetThreadGroupAffinity(saved.Handle,out var current) && current.Mask.ToUInt64()==saved.Applied)
SetThreadAffinityMask(saved.Handle,new UIntPtr(saved.Original));
CloseHandle(saved.Handle);
}
renderThreads.Clear();ThreadError=null;
}
if(mainThread!=IntPtr.Zero) {
if(GetThreadGroupAffinity(mainThread,out var affinity) && affinity.Mask.ToUInt64()==mainApplied)
SetThreadAffinityMask(mainThread,new UIntPtr(mainOriginal));
CloseHandle(mainThread);mainThread=IntPtr.Zero;
}
if(Enabled && applied!=original && ReadMask()==applied) WriteMask(original);
Enabled=false;MainThreadOnly=false;
if (!enabled)
{
// Restoration above respects externally changed process/thread masks.
Enabled = false; SelectedCacheBytes = 0; return;
}
if (IntPtr.Size != 8 || GetActiveProcessorGroupCount() != 1)
throw new NotSupportedException("Cache affinity requires x64 and a single Windows processor group.");
uint length = 0;
GetLogicalProcessorInformation(IntPtr.Zero, ref length);
if (length == 0 || length % 32 != 0 || length > 1048576)
throw new InvalidOperationException("Unsupported CPU topology record size.");
IntPtr buffer = Marshal.AllocHGlobal((int)length);
var caches = new List<Cache>();
try
{
if (!GetLogicalProcessorInformation(buffer, ref length)) throw new Win32Exception(Marshal.GetLastWin32Error());
// SYSTEM_LOGICAL_PROCESSOR_INFORMATION: x64 mask at 0, relationship at 8,
// 16-byte union at 16; CACHE_DESCRIPTOR level at 16 and size at 20.
for (int offset = 0; offset < length; offset += 32)
{
var record = IntPtr.Add(buffer, offset);
if (Marshal.ReadInt32(record, 8) != 2 || Marshal.ReadByte(record, 16) != 3) continue;
ulong mask = unchecked((ulong)Marshal.ReadInt64(record)) & original;
if (mask != 0) caches.Add(new Cache { Mask = mask, Bytes = unchecked((uint)Marshal.ReadInt32(record, 20)) });
}
}
finally { Marshal.FreeHGlobal(buffer); }
caches.Sort((a, b) => b.Bytes.CompareTo(a.Bytes));
if (caches.Count < 2 || caches[0].Bytes <= caches[1].Bytes)
throw new NotSupportedException("No uniquely larger L3 cache group is available; keep normal CPU scheduling.");
if(mainThreadOnly) {
// Retain cache locality for the simulation thread while allowing
// existing render/Havok/jobs to run on the other CPU cores too.
// This method is invoked on the game thread by the command queue.
mainId=GetCurrentThreadId();mainThread=OpenThread(0x60,false,mainId);
if(mainThread==IntPtr.Zero) throw new Win32Exception(Marshal.GetLastWin32Error());
mainApplied=caches[0].Mask;
mainOriginal=SetThreadAffinityMask(mainThread,new UIntPtr(mainApplied)).ToUInt64();
if(mainOriginal==0) {int error=Marshal.GetLastWin32Error();CloseHandle(mainThread);mainThread=IntPtr.Zero;throw new Win32Exception(error);}
applied=ReadMask();MainThreadOnly=true;pinRender=renderWorkers;
} else {WriteMask(caches[0].Mask);applied=caches[0].Mask;}
SelectedCacheBytes = caches[0].Bytes; Enabled = true;
}
public void Dispose() { Set(false); }
}
}