2020-06-16 20:28:02 +02:00
|
|
|
using ARMeilleure.State;
|
2020-12-17 20:32:09 +01:00
|
|
|
using Ryujinx.Common.Logging;
|
2020-06-16 20:28:02 +02:00
|
|
|
using System;
|
2020-12-17 20:32:09 +01:00
|
|
|
using System.Collections.Concurrent;
|
2020-06-16 20:28:02 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.IO;
|
|
|
|
using System.IO.Compression;
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
namespace ARMeilleure.Translation.PTC
|
|
|
|
{
|
|
|
|
public static class PtcProfiler
|
|
|
|
{
|
2020-12-17 20:32:09 +01: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 20:28:02 +02: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-01-27 06:21:37 +01:00
|
|
|
private static byte[] _lastHash;
|
|
|
|
|
2020-12-17 20:32:09 +01:00
|
|
|
internal static Dictionary<ulong, (ExecutionMode mode, bool highCq)> ProfiledFuncs { get; private set; }
|
2020-06-16 20:28:02 +02:00
|
|
|
|
|
|
|
internal static bool Enabled { get; private set; }
|
|
|
|
|
|
|
|
public static ulong StaticCodeStart { internal get; set; }
|
2020-12-17 20:32:09 +01:00
|
|
|
public static ulong StaticCodeSize { internal get; set; }
|
2020-06-16 20:28:02 +02:00
|
|
|
|
|
|
|
static PtcProfiler()
|
|
|
|
{
|
|
|
|
_timer = new System.Timers.Timer((double)SaveInterval * 1000d);
|
|
|
|
_timer.Elapsed += PreSave;
|
|
|
|
|
|
|
|
_waitEvent = new ManualResetEvent(true);
|
|
|
|
|
|
|
|
_lock = new object();
|
|
|
|
|
|
|
|
_disposed = false;
|
|
|
|
|
|
|
|
ProfiledFuncs = new Dictionary<ulong, (ExecutionMode, bool)>();
|
|
|
|
|
|
|
|
Enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static void AddEntry(ulong address, ExecutionMode mode, bool highCq)
|
|
|
|
{
|
|
|
|
if (IsAddressInStaticCodeRange(address))
|
|
|
|
{
|
2020-12-17 20:32:09 +01:00
|
|
|
Debug.Assert(!highCq);
|
|
|
|
|
2020-06-16 20:28:02 +02:00
|
|
|
lock (_lock)
|
|
|
|
{
|
2020-12-17 20:32:09 +01:00
|
|
|
ProfiledFuncs.TryAdd(address, (mode, highCq: false));
|
2020-06-16 20:28:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static void UpdateEntry(ulong address, ExecutionMode mode, bool highCq)
|
|
|
|
{
|
|
|
|
if (IsAddressInStaticCodeRange(address))
|
|
|
|
{
|
2020-12-17 20:32:09 +01:00
|
|
|
Debug.Assert(highCq);
|
|
|
|
|
2020-06-16 20:28:02 +02:00
|
|
|
lock (_lock)
|
|
|
|
{
|
2020-12-17 20:32:09 +01:00
|
|
|
Debug.Assert(ProfiledFuncs.ContainsKey(address));
|
2020-06-16 20:28:02 +02:00
|
|
|
|
2020-12-17 20:32:09 +01:00
|
|
|
ProfiledFuncs[address] = (mode, highCq: true);
|
2020-06-16 20:28:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static bool IsAddressInStaticCodeRange(ulong address)
|
|
|
|
{
|
2020-12-17 20:32:09 +01:00
|
|
|
return address >= StaticCodeStart && address < StaticCodeStart + StaticCodeSize;
|
|
|
|
}
|
|
|
|
|
2020-12-24 03:58:36 +01:00
|
|
|
internal static ConcurrentQueue<(ulong address, ExecutionMode mode, bool highCq)> GetProfiledFuncsToTranslate(ConcurrentDictionary<ulong, TranslatedFunction> funcs)
|
2020-12-17 20:32:09 +01:00
|
|
|
{
|
2020-12-24 03:58:36 +01:00
|
|
|
var profiledFuncsToTranslate = new ConcurrentQueue<(ulong address, ExecutionMode mode, bool highCq)>();
|
2020-12-17 20:32:09 +01:00
|
|
|
|
2020-12-24 03:58:36 +01:00
|
|
|
foreach (var profiledFunc in ProfiledFuncs)
|
2020-12-17 20:32:09 +01:00
|
|
|
{
|
2020-12-24 03:58:36 +01:00
|
|
|
ulong address = profiledFunc.Key;
|
|
|
|
|
|
|
|
if (!funcs.ContainsKey(address))
|
2020-12-17 20:32:09 +01:00
|
|
|
{
|
2020-12-24 03:58:36 +01:00
|
|
|
profiledFuncsToTranslate.Enqueue((address, profiledFunc.Value.mode, profiledFunc.Value.highCq));
|
2020-12-17 20:32:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return profiledFuncsToTranslate;
|
2020-06-16 20:28:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
internal static void ClearEntries()
|
|
|
|
{
|
|
|
|
ProfiledFuncs.Clear();
|
2021-01-27 06:21:37 +01:00
|
|
|
ProfiledFuncs.TrimExcess();
|
2020-06-16 20:28:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
internal static void PreLoad()
|
|
|
|
{
|
2021-01-27 06:21:37 +01:00
|
|
|
_lastHash = Array.Empty<byte>();
|
|
|
|
|
2021-02-08 03:46:09 +01:00
|
|
|
string fileNameActual = string.Concat(Ptc.CachePathActual, ".info");
|
|
|
|
string fileNameBackup = string.Concat(Ptc.CachePathBackup, ".info");
|
2020-06-16 20:28:02 +02:00
|
|
|
|
|
|
|
FileInfo fileInfoActual = new FileInfo(fileNameActual);
|
|
|
|
FileInfo fileInfoBackup = new FileInfo(fileNameBackup);
|
|
|
|
|
|
|
|
if (fileInfoActual.Exists && fileInfoActual.Length != 0L)
|
|
|
|
{
|
2020-12-17 20:32:09 +01:00
|
|
|
if (!Load(fileNameActual, false))
|
2020-06-16 20:28:02 +02:00
|
|
|
{
|
|
|
|
if (fileInfoBackup.Exists && fileInfoBackup.Length != 0L)
|
|
|
|
{
|
2020-12-17 20:32:09 +01:00
|
|
|
Load(fileNameBackup, true);
|
2020-06-16 20:28:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (fileInfoBackup.Exists && fileInfoBackup.Length != 0L)
|
|
|
|
{
|
2020-12-17 20:32:09 +01:00
|
|
|
Load(fileNameBackup, true);
|
2020-06-16 20:28:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 20:32:09 +01:00
|
|
|
private static bool Load(string fileName, bool isBackup)
|
2020-06-16 20:28:02 +02: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 02:51:59 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
deflateStream.CopyTo(stream);
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-16 20:28:02 +02:00
|
|
|
|
2021-01-27 06:21:37 +01:00
|
|
|
int hashSize = md5.HashSize / 8;
|
|
|
|
|
2020-06-16 20:28:02 +02: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 20:32:09 +01:00
|
|
|
Header header = ReadHeader(stream);
|
|
|
|
|
|
|
|
if (header.Magic != HeaderMagic)
|
|
|
|
{
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (header.InfoFileVersion != InternalVersion)
|
|
|
|
{
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-16 20:28:02 +02:00
|
|
|
try
|
|
|
|
{
|
2020-12-17 20:32:09 +01:00
|
|
|
ProfiledFuncs = Deserialize(stream);
|
2020-06-16 20:28:02 +02:00
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
ProfiledFuncs = new Dictionary<ulong, (ExecutionMode, bool)>();
|
|
|
|
|
|
|
|
InvalidateCompressedStream(compressedStream);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2021-01-27 06:21:37 +01:00
|
|
|
|
|
|
|
_lastHash = expectedHash;
|
2020-06-16 20:28:02 +02:00
|
|
|
}
|
2020-12-17 20:32:09 +01: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 20:28:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static bool CompareHash(ReadOnlySpan<byte> currentHash, ReadOnlySpan<byte> expectedHash)
|
|
|
|
{
|
|
|
|
return currentHash.SequenceEqual(expectedHash);
|
|
|
|
}
|
|
|
|
|
2021-01-27 06:21:37 +01:00
|
|
|
private static Header ReadHeader(Stream stream)
|
2020-12-17 20:32:09 +01: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-01-27 06:21:37 +01:00
|
|
|
private static Dictionary<ulong, (ExecutionMode, bool)> Deserialize(Stream stream)
|
2020-12-17 20:32:09 +01:00
|
|
|
{
|
|
|
|
using (BinaryReader reader = new BinaryReader(stream, EncodingCache.UTF8NoBOM, true))
|
|
|
|
{
|
|
|
|
var profiledFuncs = new Dictionary<ulong, (ExecutionMode, bool)>();
|
|
|
|
|
|
|
|
int profiledFuncsCount = reader.ReadInt32();
|
|
|
|
|
|
|
|
for (int i = 0; i < profiledFuncsCount; i++)
|
|
|
|
{
|
|
|
|
ulong address = reader.ReadUInt64();
|
|
|
|
|
|
|
|
ExecutionMode mode = (ExecutionMode)reader.ReadInt32();
|
|
|
|
bool highCq = reader.ReadBoolean();
|
|
|
|
|
|
|
|
profiledFuncs.Add(address, (mode, highCq));
|
|
|
|
}
|
|
|
|
|
|
|
|
return profiledFuncs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-16 20:28:02 +02:00
|
|
|
private static void InvalidateCompressedStream(FileStream compressedStream)
|
|
|
|
{
|
|
|
|
compressedStream.SetLength(0L);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void PreSave(object source, System.Timers.ElapsedEventArgs e)
|
|
|
|
{
|
|
|
|
_waitEvent.Reset();
|
|
|
|
|
2021-02-08 03:46:09 +01:00
|
|
|
string fileNameActual = string.Concat(Ptc.CachePathActual, ".info");
|
|
|
|
string fileNameBackup = string.Concat(Ptc.CachePathBackup, ".info");
|
2020-06-16 20:28:02 +02: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 20:32:09 +01:00
|
|
|
int profiledFuncsCount;
|
|
|
|
|
2020-06-16 20:28:02 +02: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 20:32:09 +01:00
|
|
|
WriteHeader(stream);
|
|
|
|
|
2020-06-16 20:28:02 +02:00
|
|
|
lock (_lock)
|
|
|
|
{
|
2020-12-17 20:32:09 +01:00
|
|
|
Serialize(stream, ProfiledFuncs);
|
|
|
|
|
|
|
|
profiledFuncsCount = ProfiledFuncs.Count;
|
2020-06-16 20:28:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
stream.Seek((long)hashSize, SeekOrigin.Begin);
|
|
|
|
byte[] hash = md5.ComputeHash(stream);
|
|
|
|
|
|
|
|
stream.Seek(0L, SeekOrigin.Begin);
|
|
|
|
stream.Write(hash, 0, hashSize);
|
|
|
|
|
2021-01-27 06:21:37 +01:00
|
|
|
if (CompareHash(hash, _lastHash))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-16 20:28:02 +02:00
|
|
|
using (FileStream compressedStream = new FileStream(fileName, FileMode.OpenOrCreate))
|
|
|
|
using (DeflateStream deflateStream = new DeflateStream(compressedStream, SaveCompressionLevel, true))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
stream.WriteTo(deflateStream);
|
2021-01-27 06:21:37 +01:00
|
|
|
|
|
|
|
_lastHash = hash;
|
2020-06-16 20:28:02 +02:00
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
compressedStream.Position = 0L;
|
2021-01-27 06:21:37 +01:00
|
|
|
|
|
|
|
_lastHash = Array.Empty<byte>();
|
2020-06-16 20:28:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (compressedStream.Position < compressedStream.Length)
|
|
|
|
{
|
|
|
|
compressedStream.SetLength(compressedStream.Position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-17 20:32:09 +01:00
|
|
|
|
|
|
|
long fileSize = new FileInfo(fileName).Length;
|
|
|
|
|
2021-01-27 06:21:37 +01:00
|
|
|
if (fileSize != 0L)
|
|
|
|
{
|
|
|
|
Logger.Info?.Print(LogClass.Ptc, $"Saved Profiling Info (size: {fileSize} bytes, profiled functions: {profiledFuncsCount}).");
|
|
|
|
}
|
2020-12-17 20:32:09 +01:00
|
|
|
}
|
|
|
|
|
2021-01-27 06:21:37 +01:00
|
|
|
private static void WriteHeader(Stream stream)
|
2020-12-17 20:32:09 +01:00
|
|
|
{
|
|
|
|
using (BinaryWriter headerWriter = new BinaryWriter(stream, EncodingCache.UTF8NoBOM, true))
|
|
|
|
{
|
|
|
|
headerWriter.Write((string)HeaderMagic); // Header.Magic
|
|
|
|
|
|
|
|
headerWriter.Write((uint)InternalVersion); // Header.InfoFileVersion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-27 06:21:37 +01:00
|
|
|
private static void Serialize(Stream stream, Dictionary<ulong, (ExecutionMode mode, bool highCq)> profiledFuncs)
|
2020-12-17 20:32:09 +01:00
|
|
|
{
|
|
|
|
using (BinaryWriter writer = new BinaryWriter(stream, EncodingCache.UTF8NoBOM, true))
|
|
|
|
{
|
|
|
|
writer.Write((int)profiledFuncs.Count);
|
|
|
|
|
|
|
|
foreach (var kv in profiledFuncs)
|
|
|
|
{
|
|
|
|
writer.Write((ulong)kv.Key); // address
|
|
|
|
|
|
|
|
writer.Write((int)kv.Value.mode);
|
|
|
|
writer.Write((bool)kv.Value.highCq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private struct Header
|
|
|
|
{
|
|
|
|
public string Magic;
|
|
|
|
|
|
|
|
public uint InfoFileVersion;
|
2020-06-16 20:28:02 +02: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 02:51:59 +01:00
|
|
|
}
|