2020-06-16 18:28:02 +00:00
|
|
|
using ARMeilleure.State;
|
2020-12-17 19:32:09 +00:00
|
|
|
using Ryujinx.Common.Logging;
|
2020-06-16 18:28:02 +00:00
|
|
|
using System;
|
2020-12-17 19:32:09 +00:00
|
|
|
using System.Collections.Concurrent;
|
2020-06-16 18:28:02 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.IO;
|
|
|
|
using System.IO.Compression;
|
2021-02-22 02:23:48 +00:00
|
|
|
using System.Runtime.InteropServices;
|
2020-06-16 18:28:02 +00:00
|
|
|
using System.Security.Cryptography;
|
|
|
|
using System.Threading;
|
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
using static ARMeilleure.Translation.PTC.PtcFormatter;
|
|
|
|
|
2020-06-16 18:28:02 +00:00
|
|
|
namespace ARMeilleure.Translation.PTC
|
|
|
|
{
|
|
|
|
public static class PtcProfiler
|
|
|
|
{
|
2020-12-17 19:32:09 +00:00
|
|
|
private const string HeaderMagic = "Phd";
|
|
|
|
|
|
|
|
private const uint InternalVersion = 1713; //! Not to be incremented manually for each change to the ARMeilleure project.
|
|
|
|
|
2020-06-16 18:28:02 +00:00
|
|
|
private const int SaveInterval = 30; // Seconds.
|
|
|
|
|
|
|
|
private const CompressionLevel SaveCompressionLevel = CompressionLevel.Fastest;
|
|
|
|
|
|
|
|
private static readonly System.Timers.Timer _timer;
|
|
|
|
|
|
|
|
private static readonly ManualResetEvent _waitEvent;
|
|
|
|
|
|
|
|
private static readonly object _lock;
|
|
|
|
|
|
|
|
private static bool _disposed;
|
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
private static byte[] _lastHash;
|
|
|
|
|
|
|
|
internal static Dictionary<ulong, FuncProfile> ProfiledFuncs { get; private set; }
|
2020-06-16 18:28:02 +00:00
|
|
|
|
|
|
|
internal static bool Enabled { get; private set; }
|
|
|
|
|
|
|
|
public static ulong StaticCodeStart { internal get; set; }
|
2020-12-17 19:32:09 +00:00
|
|
|
public static ulong StaticCodeSize { internal get; set; }
|
2020-06-16 18:28:02 +00:00
|
|
|
|
|
|
|
static PtcProfiler()
|
|
|
|
{
|
|
|
|
_timer = new System.Timers.Timer((double)SaveInterval * 1000d);
|
|
|
|
_timer.Elapsed += PreSave;
|
|
|
|
|
|
|
|
_waitEvent = new ManualResetEvent(true);
|
|
|
|
|
|
|
|
_lock = new object();
|
|
|
|
|
|
|
|
_disposed = false;
|
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
ProfiledFuncs = new Dictionary<ulong, FuncProfile>();
|
2020-06-16 18:28:02 +00:00
|
|
|
|
|
|
|
Enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static void AddEntry(ulong address, ExecutionMode mode, bool highCq)
|
|
|
|
{
|
|
|
|
if (IsAddressInStaticCodeRange(address))
|
|
|
|
{
|
2020-12-17 19:32:09 +00:00
|
|
|
Debug.Assert(!highCq);
|
|
|
|
|
2020-06-16 18:28:02 +00:00
|
|
|
lock (_lock)
|
|
|
|
{
|
2021-02-22 02:23:48 +00:00
|
|
|
ProfiledFuncs.TryAdd(address, new FuncProfile(mode, highCq: false));
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static void UpdateEntry(ulong address, ExecutionMode mode, bool highCq)
|
|
|
|
{
|
|
|
|
if (IsAddressInStaticCodeRange(address))
|
|
|
|
{
|
2020-12-17 19:32:09 +00:00
|
|
|
Debug.Assert(highCq);
|
|
|
|
|
2020-06-16 18:28:02 +00:00
|
|
|
lock (_lock)
|
|
|
|
{
|
2020-12-17 19:32:09 +00:00
|
|
|
Debug.Assert(ProfiledFuncs.ContainsKey(address));
|
2020-06-16 18:28:02 +00:00
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
ProfiledFuncs[address] = new FuncProfile(mode, highCq: true);
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static bool IsAddressInStaticCodeRange(ulong address)
|
|
|
|
{
|
2020-12-17 19:32:09 +00:00
|
|
|
return address >= StaticCodeStart && address < StaticCodeStart + StaticCodeSize;
|
|
|
|
}
|
|
|
|
|
2020-12-24 02:58:36 +00:00
|
|
|
internal static ConcurrentQueue<(ulong address, ExecutionMode mode, bool highCq)> GetProfiledFuncsToTranslate(ConcurrentDictionary<ulong, TranslatedFunction> funcs)
|
2020-12-17 19:32:09 +00:00
|
|
|
{
|
2020-12-24 02:58:36 +00:00
|
|
|
var profiledFuncsToTranslate = new ConcurrentQueue<(ulong address, ExecutionMode mode, bool highCq)>();
|
2020-12-17 19:32:09 +00:00
|
|
|
|
2020-12-24 02:58:36 +00:00
|
|
|
foreach (var profiledFunc in ProfiledFuncs)
|
2020-12-17 19:32:09 +00:00
|
|
|
{
|
2020-12-24 02:58:36 +00:00
|
|
|
ulong address = profiledFunc.Key;
|
|
|
|
|
|
|
|
if (!funcs.ContainsKey(address))
|
2020-12-17 19:32:09 +00:00
|
|
|
{
|
2021-02-22 02:23:48 +00:00
|
|
|
profiledFuncsToTranslate.Enqueue((address, profiledFunc.Value.Mode, profiledFunc.Value.HighCq));
|
2020-12-17 19:32:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return profiledFuncsToTranslate;
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal static void ClearEntries()
|
|
|
|
{
|
|
|
|
ProfiledFuncs.Clear();
|
2021-02-22 02:23:48 +00:00
|
|
|
ProfiledFuncs.TrimExcess();
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal static void PreLoad()
|
|
|
|
{
|
2021-02-22 02:23:48 +00:00
|
|
|
_lastHash = Array.Empty<byte>();
|
|
|
|
|
|
|
|
string fileNameActual = string.Concat(Ptc.CachePathActual, ".info");
|
|
|
|
string fileNameBackup = string.Concat(Ptc.CachePathBackup, ".info");
|
2020-06-16 18:28:02 +00:00
|
|
|
|
|
|
|
FileInfo fileInfoActual = new FileInfo(fileNameActual);
|
|
|
|
FileInfo fileInfoBackup = new FileInfo(fileNameBackup);
|
|
|
|
|
|
|
|
if (fileInfoActual.Exists && fileInfoActual.Length != 0L)
|
|
|
|
{
|
2020-12-17 19:32:09 +00:00
|
|
|
if (!Load(fileNameActual, false))
|
2020-06-16 18:28:02 +00:00
|
|
|
{
|
|
|
|
if (fileInfoBackup.Exists && fileInfoBackup.Length != 0L)
|
|
|
|
{
|
2020-12-17 19:32:09 +00:00
|
|
|
Load(fileNameBackup, true);
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (fileInfoBackup.Exists && fileInfoBackup.Length != 0L)
|
|
|
|
{
|
2020-12-17 19:32:09 +00:00
|
|
|
Load(fileNameBackup, true);
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
private static bool Load(string fileName, bool isBackup)
|
2020-06-16 18:28:02 +00:00
|
|
|
{
|
|
|
|
using (FileStream compressedStream = new FileStream(fileName, FileMode.Open))
|
|
|
|
using (DeflateStream deflateStream = new DeflateStream(compressedStream, CompressionMode.Decompress, true))
|
|
|
|
using (MemoryStream stream = new MemoryStream())
|
|
|
|
using (MD5 md5 = MD5.Create())
|
|
|
|
{
|
2020-11-20 01:51:59 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
deflateStream.CopyTo(stream);
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-16 18:28:02 +00:00
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
int hashSize = md5.HashSize / 8;
|
|
|
|
|
2020-06-16 18:28:02 +00:00
|
|
|
stream.Seek(0L, SeekOrigin.Begin);
|
|
|
|
|
|
|
|
byte[] currentHash = new byte[hashSize];
|
|
|
|
stream.Read(currentHash, 0, hashSize);
|
|
|
|
|
|
|
|
byte[] expectedHash = md5.ComputeHash(stream);
|
|
|
|
|
|
|
|
if (!CompareHash(currentHash, expectedHash))
|
|
|
|
{
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.Seek((long)hashSize, SeekOrigin.Begin);
|
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
Header header = ReadHeader(stream);
|
|
|
|
|
|
|
|
if (header.Magic != HeaderMagic)
|
|
|
|
{
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (header.InfoFileVersion != InternalVersion)
|
|
|
|
{
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-16 18:28:02 +00:00
|
|
|
try
|
|
|
|
{
|
2020-12-17 19:32:09 +00:00
|
|
|
ProfiledFuncs = Deserialize(stream);
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
2021-02-22 02:23:48 +00:00
|
|
|
ProfiledFuncs = new Dictionary<ulong, FuncProfile>();
|
2020-06-16 18:28:02 +00:00
|
|
|
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2021-02-22 02:23:48 +00:00
|
|
|
|
|
|
|
_lastHash = expectedHash;
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
2020-12-17 19:32:09 +00:00
|
|
|
|
|
|
|
long fileSize = new FileInfo(fileName).Length;
|
|
|
|
|
|
|
|
Logger.Info?.Print(LogClass.Ptc, $"{(isBackup ? "Loaded Backup Profiling Info" : "Loaded Profiling Info")} (size: {fileSize} bytes, profiled functions: {ProfiledFuncs.Count}).");
|
|
|
|
|
|
|
|
return true;
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static bool CompareHash(ReadOnlySpan<byte> currentHash, ReadOnlySpan<byte> expectedHash)
|
|
|
|
{
|
|
|
|
return currentHash.SequenceEqual(expectedHash);
|
|
|
|
}
|
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
private static Header ReadHeader(Stream stream)
|
2020-12-17 19:32:09 +00:00
|
|
|
{
|
|
|
|
using (BinaryReader headerReader = new BinaryReader(stream, EncodingCache.UTF8NoBOM, true))
|
|
|
|
{
|
|
|
|
Header header = new Header();
|
|
|
|
|
|
|
|
header.Magic = headerReader.ReadString();
|
|
|
|
|
|
|
|
header.InfoFileVersion = headerReader.ReadUInt32();
|
|
|
|
|
|
|
|
return header;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
private static Dictionary<ulong, FuncProfile> Deserialize(Stream stream)
|
2020-12-17 19:32:09 +00:00
|
|
|
{
|
2021-02-22 02:23:48 +00:00
|
|
|
return DeserializeDictionary<ulong, FuncProfile>(stream, (stream) => DeserializeStructure<FuncProfile>(stream));
|
2020-12-17 19:32:09 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 18:28:02 +00:00
|
|
|
private static void InvalidateCompressedStream(FileStream compressedStream)
|
|
|
|
{
|
|
|
|
compressedStream.SetLength(0L);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void PreSave(object source, System.Timers.ElapsedEventArgs e)
|
|
|
|
{
|
|
|
|
_waitEvent.Reset();
|
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
string fileNameActual = string.Concat(Ptc.CachePathActual, ".info");
|
|
|
|
string fileNameBackup = string.Concat(Ptc.CachePathBackup, ".info");
|
2020-06-16 18:28:02 +00:00
|
|
|
|
|
|
|
FileInfo fileInfoActual = new FileInfo(fileNameActual);
|
|
|
|
|
|
|
|
if (fileInfoActual.Exists && fileInfoActual.Length != 0L)
|
|
|
|
{
|
|
|
|
File.Copy(fileNameActual, fileNameBackup, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
Save(fileNameActual);
|
|
|
|
|
|
|
|
_waitEvent.Set();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void Save(string fileName)
|
|
|
|
{
|
2020-12-17 19:32:09 +00:00
|
|
|
int profiledFuncsCount;
|
|
|
|
|
2020-06-16 18:28:02 +00:00
|
|
|
using (MemoryStream stream = new MemoryStream())
|
|
|
|
using (MD5 md5 = MD5.Create())
|
|
|
|
{
|
|
|
|
int hashSize = md5.HashSize / 8;
|
|
|
|
|
|
|
|
stream.Seek((long)hashSize, SeekOrigin.Begin);
|
|
|
|
|
2020-12-17 19:32:09 +00:00
|
|
|
WriteHeader(stream);
|
|
|
|
|
2020-06-16 18:28:02 +00:00
|
|
|
lock (_lock)
|
|
|
|
{
|
2020-12-17 19:32:09 +00:00
|
|
|
Serialize(stream, ProfiledFuncs);
|
|
|
|
|
|
|
|
profiledFuncsCount = ProfiledFuncs.Count;
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stream.Seek((long)hashSize, SeekOrigin.Begin);
|
|
|
|
byte[] hash = md5.ComputeHash(stream);
|
|
|
|
|
|
|
|
stream.Seek(0L, SeekOrigin.Begin);
|
|
|
|
stream.Write(hash, 0, hashSize);
|
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
if (CompareHash(hash, _lastHash))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-16 18:28:02 +00:00
|
|
|
using (FileStream compressedStream = new FileStream(fileName, FileMode.OpenOrCreate))
|
|
|
|
using (DeflateStream deflateStream = new DeflateStream(compressedStream, SaveCompressionLevel, true))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
stream.WriteTo(deflateStream);
|
2021-02-22 02:23:48 +00:00
|
|
|
|
|
|
|
_lastHash = hash;
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
compressedStream.Position = 0L;
|
2021-02-22 02:23:48 +00:00
|
|
|
|
|
|
|
_lastHash = Array.Empty<byte>();
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (compressedStream.Position < compressedStream.Length)
|
|
|
|
{
|
|
|
|
compressedStream.SetLength(compressedStream.Position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-17 19:32:09 +00:00
|
|
|
|
|
|
|
long fileSize = new FileInfo(fileName).Length;
|
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
if (fileSize != 0L)
|
|
|
|
{
|
|
|
|
Logger.Info?.Print(LogClass.Ptc, $"Saved Profiling Info (size: {fileSize} bytes, profiled functions: {profiledFuncsCount}).");
|
|
|
|
}
|
2020-12-17 19:32:09 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
private static void WriteHeader(Stream stream)
|
2020-12-17 19:32:09 +00:00
|
|
|
{
|
|
|
|
using (BinaryWriter headerWriter = new BinaryWriter(stream, EncodingCache.UTF8NoBOM, true))
|
|
|
|
{
|
|
|
|
headerWriter.Write((string)HeaderMagic); // Header.Magic
|
|
|
|
|
|
|
|
headerWriter.Write((uint)InternalVersion); // Header.InfoFileVersion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
private static void Serialize(Stream stream, Dictionary<ulong, FuncProfile> profiledFuncs)
|
2020-12-17 19:32:09 +00:00
|
|
|
{
|
2021-02-22 02:23:48 +00:00
|
|
|
SerializeDictionary(stream, profiledFuncs, (stream, structure) => SerializeStructure(stream, structure));
|
2020-12-17 19:32:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private struct Header
|
|
|
|
{
|
|
|
|
public string Magic;
|
|
|
|
|
|
|
|
public uint InfoFileVersion;
|
2020-06-16 18:28:02 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 02:23:48 +00:00
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1/*, Size = 5*/)]
|
|
|
|
internal struct FuncProfile
|
|
|
|
{
|
|
|
|
public ExecutionMode Mode;
|
|
|
|
public bool HighCq;
|
|
|
|
|
|
|
|
public FuncProfile(ExecutionMode mode, bool highCq)
|
|
|
|
{
|
|
|
|
Mode = mode;
|
|
|
|
HighCq = highCq;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-16 18:28:02 +00:00
|
|
|
internal static void Start()
|
|
|
|
{
|
|
|
|
if (Ptc.State == PtcState.Enabled ||
|
|
|
|
Ptc.State == PtcState.Continuing)
|
|
|
|
{
|
|
|
|
Enabled = true;
|
|
|
|
|
|
|
|
_timer.Enabled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Stop()
|
|
|
|
{
|
|
|
|
Enabled = false;
|
|
|
|
|
|
|
|
if (!_disposed)
|
|
|
|
{
|
|
|
|
_timer.Enabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static void Wait()
|
|
|
|
{
|
|
|
|
_waitEvent.WaitOne();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Dispose()
|
|
|
|
{
|
|
|
|
if (!_disposed)
|
|
|
|
{
|
|
|
|
_disposed = true;
|
|
|
|
|
|
|
|
_timer.Elapsed -= PreSave;
|
|
|
|
_timer.Dispose();
|
|
|
|
|
|
|
|
Wait();
|
|
|
|
_waitEvent.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-20 01:51:59 +00:00
|
|
|
}
|