mirror of
https://github.com/GreemDev/Ryujinx
synced 2025-01-03 09:29:50 +01:00
Implement blacklist functionality
- Added blacklist status to FunctionProfile - Added PPTC Info file updater from v5518 and updated old updater logic to allow multiple update passes - Added blacklist check to PPTC Cache loading - Added marking functions as blacklisted if they do not yet exist at PPTC translation time - Logger now shows how many functions were blacklisted when translating new functions to PPTC cache
This commit is contained in:
parent
8c358880cf
commit
c61e05e4f4
3 changed files with 136 additions and 14 deletions
|
@ -3,6 +3,7 @@ using ARMeilleure.CodeGen.Linking;
|
||||||
using ARMeilleure.CodeGen.Unwinding;
|
using ARMeilleure.CodeGen.Unwinding;
|
||||||
using ARMeilleure.Common;
|
using ARMeilleure.Common;
|
||||||
using ARMeilleure.Memory;
|
using ARMeilleure.Memory;
|
||||||
|
using ARMeilleure.State;
|
||||||
using Ryujinx.Common;
|
using Ryujinx.Common;
|
||||||
using Ryujinx.Common.Configuration;
|
using Ryujinx.Common.Configuration;
|
||||||
using Ryujinx.Common.Logging;
|
using Ryujinx.Common.Logging;
|
||||||
|
@ -30,7 +31,7 @@ namespace ARMeilleure.Translation.PTC
|
||||||
private const string OuterHeaderMagicString = "PTCohd\0\0";
|
private const string OuterHeaderMagicString = "PTCohd\0\0";
|
||||||
private const string InnerHeaderMagicString = "PTCihd\0\0";
|
private const string InnerHeaderMagicString = "PTCihd\0\0";
|
||||||
|
|
||||||
private const uint InternalVersion = 6997; //! To be incremented manually for each change to the ARMeilleure project.
|
private const uint InternalVersion = 7007; //! To be incremented manually for each change to the ARMeilleure project.
|
||||||
|
|
||||||
private const string ActualDir = "0";
|
private const string ActualDir = "0";
|
||||||
private const string BackupDir = "1";
|
private const string BackupDir = "1";
|
||||||
|
@ -185,6 +186,36 @@ namespace ARMeilleure.Translation.PTC
|
||||||
InitializeCarriers();
|
InitializeCarriers();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private bool ContainsBlacklistedFunctions()
|
||||||
|
{
|
||||||
|
List<ulong> blacklist = Profiler.GetBlacklistedFunctions();
|
||||||
|
bool containsBlacklistedFunctions = false;
|
||||||
|
_infosStream.Seek(0L, SeekOrigin.Begin);
|
||||||
|
bool foundBadFunction = false;
|
||||||
|
|
||||||
|
for (int index = 0; index < GetEntriesCount(); index++)
|
||||||
|
{
|
||||||
|
InfoEntry infoEntry = DeserializeStructure<InfoEntry>(_infosStream);
|
||||||
|
foreach (ulong address in blacklist)
|
||||||
|
{
|
||||||
|
if (infoEntry.Address == address)
|
||||||
|
{
|
||||||
|
containsBlacklistedFunctions = true;
|
||||||
|
Logger.Warning?.Print(LogClass.Ptc, "PPTC cache invalidated: Found blacklisted functions in PPTC cache");
|
||||||
|
foundBadFunction = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (foundBadFunction)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return containsBlacklistedFunctions;
|
||||||
|
}
|
||||||
|
|
||||||
private void PreLoad()
|
private void PreLoad()
|
||||||
{
|
{
|
||||||
string fileNameActual = $"{CachePathActual}.cache";
|
string fileNameActual = $"{CachePathActual}.cache";
|
||||||
|
@ -533,7 +564,7 @@ namespace ARMeilleure.Translation.PTC
|
||||||
|
|
||||||
public void LoadTranslations(Translator translator)
|
public void LoadTranslations(Translator translator)
|
||||||
{
|
{
|
||||||
if (AreCarriersEmpty())
|
if (AreCarriersEmpty() || ContainsBlacklistedFunctions())
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -836,10 +867,18 @@ namespace ARMeilleure.Translation.PTC
|
||||||
while (profiledFuncsToTranslate.TryDequeue(out var item))
|
while (profiledFuncsToTranslate.TryDequeue(out var item))
|
||||||
{
|
{
|
||||||
ulong address = item.address;
|
ulong address = item.address;
|
||||||
|
ExecutionMode executionMode = item.funcProfile.Mode;
|
||||||
|
bool highCq = item.funcProfile.HighCq;
|
||||||
|
|
||||||
Debug.Assert(Profiler.IsAddressInStaticCodeRange(address));
|
Debug.Assert(Profiler.IsAddressInStaticCodeRange(address));
|
||||||
|
|
||||||
TranslatedFunction func = translator.Translate(address, item.funcProfile.Mode, item.funcProfile.HighCq);
|
TranslatedFunction func = translator.Translate(address, executionMode, highCq);
|
||||||
|
|
||||||
|
if (func == null)
|
||||||
|
{
|
||||||
|
Profiler.UpdateEntry(address, executionMode, true, true);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
bool isAddressUnique = translator.Functions.TryAdd(address, func.GuestSize, func);
|
bool isAddressUnique = translator.Functions.TryAdd(address, func.GuestSize, func);
|
||||||
|
|
||||||
|
@ -886,7 +925,14 @@ namespace ARMeilleure.Translation.PTC
|
||||||
|
|
||||||
PtcStateChanged?.Invoke(PtcLoadingState.Loaded, _translateCount, _translateTotalCount);
|
PtcStateChanged?.Invoke(PtcLoadingState.Loaded, _translateCount, _translateTotalCount);
|
||||||
|
|
||||||
Logger.Info?.Print(LogClass.Ptc, $"{_translateCount} of {_translateTotalCount} functions translated | Thread count: {degreeOfParallelism} in {sw.Elapsed.TotalSeconds} s");
|
if (_translateCount == _translateTotalCount)
|
||||||
|
{
|
||||||
|
Logger.Info?.Print(LogClass.Ptc, $"{_translateCount} of {_translateTotalCount} functions translated | Thread count: {degreeOfParallelism} in {sw.Elapsed.TotalSeconds} s");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Logger.Info?.Print(LogClass.Ptc, $"{_translateCount} of {_translateTotalCount} functions translated | {_translateTotalCount - _translateCount} function{(_translateTotalCount - _translateCount != 1 ? "s" : "")} blacklisted | Thread count: {degreeOfParallelism} in {sw.Elapsed.TotalSeconds} s");
|
||||||
|
}
|
||||||
|
|
||||||
Thread preSaveThread = new(PreSave)
|
Thread preSaveThread = new(PreSave)
|
||||||
{
|
{
|
||||||
|
|
|
@ -23,10 +23,11 @@ namespace ARMeilleure.Translation.PTC
|
||||||
{
|
{
|
||||||
private const string OuterHeaderMagicString = "Pohd\0\0\0\0";
|
private const string OuterHeaderMagicString = "Pohd\0\0\0\0";
|
||||||
|
|
||||||
private const uint InternalVersion = 5518; //! Not to be incremented manually for each change to the ARMeilleure project.
|
private const uint InternalVersion = 7007; //! Not to be incremented manually for each change to the ARMeilleure project.
|
||||||
|
|
||||||
private static readonly uint[] _migrateInternalVersions = {
|
private static readonly uint[] _migrateInternalVersions = {
|
||||||
1866,
|
1866,
|
||||||
|
5518,
|
||||||
};
|
};
|
||||||
|
|
||||||
private const int SaveInterval = 30; // Seconds.
|
private const int SaveInterval = 30; // Seconds.
|
||||||
|
@ -74,20 +75,30 @@ namespace ARMeilleure.Translation.PTC
|
||||||
Enabled = false;
|
Enabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddEntry(ulong address, ExecutionMode mode, bool highCq)
|
public void AddEntry(ulong address, ExecutionMode mode, bool highCq, bool blacklist = false)
|
||||||
{
|
{
|
||||||
if (IsAddressInStaticCodeRange(address))
|
if (IsAddressInStaticCodeRange(address))
|
||||||
{
|
{
|
||||||
Debug.Assert(!highCq);
|
Debug.Assert(!highCq);
|
||||||
|
|
||||||
lock (_lock)
|
if (blacklist)
|
||||||
{
|
{
|
||||||
ProfiledFuncs.TryAdd(address, new FuncProfile(mode, highCq: false));
|
lock (_lock)
|
||||||
|
{
|
||||||
|
ProfiledFuncs[address] = new FuncProfile(mode, highCq: false, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
lock (_lock)
|
||||||
|
{
|
||||||
|
ProfiledFuncs.TryAdd(address, new FuncProfile(mode, highCq: false, false));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateEntry(ulong address, ExecutionMode mode, bool highCq)
|
public void UpdateEntry(ulong address, ExecutionMode mode, bool highCq, bool? blacklist = null)
|
||||||
{
|
{
|
||||||
if (IsAddressInStaticCodeRange(address))
|
if (IsAddressInStaticCodeRange(address))
|
||||||
{
|
{
|
||||||
|
@ -97,7 +108,7 @@ namespace ARMeilleure.Translation.PTC
|
||||||
{
|
{
|
||||||
Debug.Assert(ProfiledFuncs.ContainsKey(address));
|
Debug.Assert(ProfiledFuncs.ContainsKey(address));
|
||||||
|
|
||||||
ProfiledFuncs[address] = new FuncProfile(mode, highCq: true);
|
ProfiledFuncs[address] = new FuncProfile(mode, highCq: true, blacklist ?? ProfiledFuncs[address].Blacklist);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -113,7 +124,7 @@ namespace ARMeilleure.Translation.PTC
|
||||||
|
|
||||||
foreach (var profiledFunc in ProfiledFuncs)
|
foreach (var profiledFunc in ProfiledFuncs)
|
||||||
{
|
{
|
||||||
if (!funcs.ContainsKey(profiledFunc.Key))
|
if (!funcs.ContainsKey(profiledFunc.Key) && !profiledFunc.Value.Blacklist)
|
||||||
{
|
{
|
||||||
profiledFuncsToTranslate.Enqueue((profiledFunc.Key, profiledFunc.Value));
|
profiledFuncsToTranslate.Enqueue((profiledFunc.Key, profiledFunc.Value));
|
||||||
}
|
}
|
||||||
|
@ -128,6 +139,24 @@ namespace ARMeilleure.Translation.PTC
|
||||||
ProfiledFuncs.TrimExcess();
|
ProfiledFuncs.TrimExcess();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<ulong> GetBlacklistedFunctions()
|
||||||
|
{
|
||||||
|
List<ulong> funcs = new List<ulong>();
|
||||||
|
|
||||||
|
foreach (var profiledFunc in ProfiledFuncs)
|
||||||
|
{
|
||||||
|
if (profiledFunc.Value.Blacklist)
|
||||||
|
{
|
||||||
|
if (!funcs.Contains(profiledFunc.Key))
|
||||||
|
{
|
||||||
|
funcs.Add(profiledFunc.Key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return funcs;
|
||||||
|
}
|
||||||
|
|
||||||
public void PreLoad()
|
public void PreLoad()
|
||||||
{
|
{
|
||||||
_lastHash = default;
|
_lastHash = default;
|
||||||
|
@ -218,13 +247,18 @@ namespace ARMeilleure.Translation.PTC
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Func<ulong, FuncProfile, (ulong, FuncProfile)> migrateEntryFunc = null;
|
||||||
|
|
||||||
switch (outerHeader.InfoFileVersion)
|
switch (outerHeader.InfoFileVersion)
|
||||||
{
|
{
|
||||||
case InternalVersion:
|
case InternalVersion:
|
||||||
ProfiledFuncs = Deserialize(stream);
|
ProfiledFuncs = Deserialize(stream);
|
||||||
break;
|
break;
|
||||||
case 1866:
|
case 1866:
|
||||||
ProfiledFuncs = Deserialize(stream, (address, profile) => (address + 0x500000UL, profile));
|
migrateEntryFunc = (address, profile) => (address + 0x500000UL, profile);
|
||||||
|
goto case 5518;
|
||||||
|
case 5518:
|
||||||
|
ProfiledFuncs = DeserializeAddBlacklist(stream, migrateEntryFunc);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
Logger.Error?.Print(LogClass.Ptc, $"No migration path for {nameof(outerHeader.InfoFileVersion)} '{outerHeader.InfoFileVersion}'. Discarding cache.");
|
Logger.Error?.Print(LogClass.Ptc, $"No migration path for {nameof(outerHeader.InfoFileVersion)} '{outerHeader.InfoFileVersion}'. Discarding cache.");
|
||||||
|
@ -254,6 +288,16 @@ namespace ARMeilleure.Translation.PTC
|
||||||
return DeserializeDictionary<ulong, FuncProfile>(stream, DeserializeStructure<FuncProfile>);
|
return DeserializeDictionary<ulong, FuncProfile>(stream, DeserializeStructure<FuncProfile>);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Dictionary<ulong, FuncProfile> DeserializeAddBlacklist(Stream stream, Func<ulong, FuncProfile, (ulong, FuncProfile)> migrateEntryFunc = null)
|
||||||
|
{
|
||||||
|
if (migrateEntryFunc != null)
|
||||||
|
{
|
||||||
|
return DeserializeAndUpdateDictionary(stream, (Stream stream) => { return new FuncProfile(DeserializeStructure<FuncProfilePreBlacklist>(stream)); }, migrateEntryFunc);
|
||||||
|
}
|
||||||
|
|
||||||
|
return DeserializeDictionary<ulong, FuncProfile>(stream, (Stream stream) => { return new FuncProfile(DeserializeStructure<FuncProfilePreBlacklist>(stream)); });
|
||||||
|
}
|
||||||
|
|
||||||
private static ReadOnlySpan<byte> GetReadOnlySpan(MemoryStream memoryStream)
|
private static ReadOnlySpan<byte> GetReadOnlySpan(MemoryStream memoryStream)
|
||||||
{
|
{
|
||||||
return new(memoryStream.GetBuffer(), (int)memoryStream.Position, (int)memoryStream.Length - (int)memoryStream.Position);
|
return new(memoryStream.GetBuffer(), (int)memoryStream.Position, (int)memoryStream.Length - (int)memoryStream.Position);
|
||||||
|
@ -385,13 +429,35 @@ namespace ARMeilleure.Translation.PTC
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[StructLayout(LayoutKind.Sequential, Pack = 1/*, Size = 5*/)]
|
[StructLayout(LayoutKind.Sequential, Pack = 1/*, Size = 6*/)]
|
||||||
public struct FuncProfile
|
public struct FuncProfile
|
||||||
{
|
{
|
||||||
public ExecutionMode Mode;
|
public ExecutionMode Mode;
|
||||||
public bool HighCq;
|
public bool HighCq;
|
||||||
|
public bool Blacklist;
|
||||||
|
|
||||||
public FuncProfile(ExecutionMode mode, bool highCq)
|
public FuncProfile(ExecutionMode mode, bool highCq, bool blacklist)
|
||||||
|
{
|
||||||
|
Mode = mode;
|
||||||
|
HighCq = highCq;
|
||||||
|
Blacklist = blacklist;
|
||||||
|
}
|
||||||
|
|
||||||
|
public FuncProfile(FuncProfilePreBlacklist fp)
|
||||||
|
{
|
||||||
|
Mode = fp.Mode;
|
||||||
|
HighCq = fp.HighCq;
|
||||||
|
Blacklist = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[StructLayout(LayoutKind.Sequential, Pack = 1/*, Size = 5*/)]
|
||||||
|
public struct FuncProfilePreBlacklist
|
||||||
|
{
|
||||||
|
public ExecutionMode Mode;
|
||||||
|
public bool HighCq;
|
||||||
|
|
||||||
|
public FuncProfilePreBlacklist(ExecutionMode mode, bool highCq)
|
||||||
{
|
{
|
||||||
Mode = mode;
|
Mode = mode;
|
||||||
HighCq = highCq;
|
HighCq = highCq;
|
||||||
|
|
|
@ -249,6 +249,11 @@ namespace ARMeilleure.Translation
|
||||||
|
|
||||||
ControlFlowGraph cfg = EmitAndGetCFG(context, blocks, out Range funcRange, out Counter<uint> counter);
|
ControlFlowGraph cfg = EmitAndGetCFG(context, blocks, out Range funcRange, out Counter<uint> counter);
|
||||||
|
|
||||||
|
if (cfg == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
ulong funcSize = funcRange.End - funcRange.Start;
|
ulong funcSize = funcRange.End - funcRange.Start;
|
||||||
|
|
||||||
Logger.EndPass(PassName.Translation, cfg);
|
Logger.EndPass(PassName.Translation, cfg);
|
||||||
|
@ -407,6 +412,11 @@ namespace ARMeilleure.Translation
|
||||||
if (opCode.Instruction.Emitter != null)
|
if (opCode.Instruction.Emitter != null)
|
||||||
{
|
{
|
||||||
opCode.Instruction.Emitter(context);
|
opCode.Instruction.Emitter(context);
|
||||||
|
if (opCode.Instruction.Name == InstName.Und && blkIndex == 0)
|
||||||
|
{
|
||||||
|
range = new Range(rangeStart, rangeEnd);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue