Implement a method to get Physical Core Count and resolve pending TODO

This commit is contained in:
sunshineinabox 2023-07-05 23:30:49 -07:00
parent 1c7a90ef35
commit 0f255d9ada
2 changed files with 52 additions and 4 deletions

View file

@ -10,6 +10,7 @@ using ARMeilleure.State;
using ARMeilleure.Translation.Cache;
using ARMeilleure.Translation.PTC;
using Ryujinx.Common;
using Ryujinx.Common.SystemInfo;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
@ -103,6 +104,7 @@ namespace ARMeilleure.Translation
public void Execute(State.ExecutionContext context, ulong address)
{
int physicalCoreCount = SystemInfo.GetPhysicalCoreCount();
if (Interlocked.Increment(ref _threadCount) == 1)
{
IsReadyForTranslation.WaitOne();
@ -122,10 +124,7 @@ namespace ARMeilleure.Translation
// etc). All threads are normal priority except from the last, which just fills as much of the last core
// as the os lets it with a low priority. If we only have one rejit thread, it should be normal priority
// as highCq code is performance critical.
//
// TODO: Use physical cores rather than logical. This only really makes sense for processors with
// hyperthreading. Requires OS specific code.
int unboundedThreadCount = Math.Max(1, (Environment.ProcessorCount - 6) / 3);
int unboundedThreadCount = Math.Max(1, (physicalCoreCount - 6) / 3);
int threadCount = Math.Min(4, unboundedThreadCount);
Thread[] backgroundTranslationThreads = new Thread[threadCount];

View file

@ -1,5 +1,8 @@
using Ryujinx.Common.Logging;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics.X86;
using System.Text;
@ -74,5 +77,51 @@ namespace Ryujinx.Common.SystemInfo
return string.IsNullOrEmpty(name) ? null : name;
}
public static int GetPhysicalCoreCount()
{
int coreCount = Environment.ProcessorCount;
try
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
coreCount = 0;
foreach (var item in new System.Management.ManagementObjectSearcher("Select NumberOfCores from Win32_Processor").Get())
{
coreCount += int.Parse(item["NumberOfCores"].ToString());
}
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
coreCount = File.ReadLines("/proc/cpuinfo")
.Count(line => line.Contains("cpu cores"));
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "sysctl",
Arguments = "-n hw.physicalcpu",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
process.Start();
coreCount = int.Parse(process.StandardOutput.ReadToEnd());
process.WaitForExit();
}
}
catch (Exception ex)
{
Console.WriteLine("An error occurred while trying to get the physical core count: " + ex.Message);
}
return coreCount;
}
}
}