PtcProfiler.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. using ARMeilleure.State;
  2. using Humanizer;
  3. using Ryujinx.Common;
  4. using Ryujinx.Common.Logging;
  5. using Ryujinx.Common.Memory;
  6. using System;
  7. using System.Buffers.Binary;
  8. using System.Collections.Concurrent;
  9. using System.Collections.Generic;
  10. using System.Diagnostics;
  11. using System.IO;
  12. using System.IO.Compression;
  13. using System.Linq;
  14. using System.Runtime.CompilerServices;
  15. using System.Runtime.InteropServices;
  16. using System.Threading;
  17. using System.Timers;
  18. using static ARMeilleure.Translation.PTC.PtcFormatter;
  19. using Timer = System.Timers.Timer;
  20. namespace ARMeilleure.Translation.PTC
  21. {
  22. class PtcProfiler
  23. {
  24. private const string OuterHeaderMagicString = "Pohd\0\0\0\0";
  25. private const uint InternalVersion = 7007; //! Not to be incremented manually for each change to the ARMeilleure project.
  26. private static readonly uint[] _migrateInternalVersions =
  27. [
  28. 1866,
  29. 5518,
  30. ];
  31. private const int SaveInterval = 30; // Seconds.
  32. private const CompressionLevel SaveCompressionLevel = CompressionLevel.Fastest;
  33. private readonly Ptc _ptc;
  34. private readonly Timer _timer;
  35. private readonly ulong _outerHeaderMagic;
  36. private readonly ManualResetEvent _waitEvent;
  37. private readonly Lock _lock = new();
  38. private bool _disposed;
  39. private Hash128 _lastHash;
  40. public Dictionary<ulong, FuncProfile> ProfiledFuncs { get; private set; }
  41. public bool Enabled { get; private set; }
  42. public ulong StaticCodeStart { get; set; }
  43. public ulong StaticCodeSize { get; set; }
  44. public PtcProfiler(Ptc ptc)
  45. {
  46. _ptc = ptc;
  47. _timer = new Timer(SaveInterval.Seconds());
  48. _timer.Elapsed += TimerElapsed;
  49. _outerHeaderMagic = BinaryPrimitives.ReadUInt64LittleEndian(EncodingCache.UTF8NoBOM.GetBytes(OuterHeaderMagicString).AsSpan());
  50. _waitEvent = new ManualResetEvent(true);
  51. _disposed = false;
  52. ProfiledFuncs = new Dictionary<ulong, FuncProfile>();
  53. Enabled = false;
  54. }
  55. private void TimerElapsed(object _, ElapsedEventArgs __)
  56. => new Thread(PreSave) { Name = "Ptc.DiskWriter" }.Start();
  57. public void AddEntry(ulong address, ExecutionMode mode, bool highCq, bool blacklist = false)
  58. {
  59. if (IsAddressInStaticCodeRange(address))
  60. {
  61. Debug.Assert(!highCq);
  62. if (blacklist)
  63. {
  64. lock (_lock)
  65. {
  66. ProfiledFuncs[address] = new FuncProfile(mode, highCq: false, true);
  67. }
  68. }
  69. else
  70. {
  71. lock (_lock)
  72. {
  73. ProfiledFuncs.TryAdd(address, new FuncProfile(mode, highCq: false, false));
  74. }
  75. }
  76. }
  77. }
  78. public void UpdateEntry(ulong address, ExecutionMode mode, bool highCq, bool? blacklist = null)
  79. {
  80. if (IsAddressInStaticCodeRange(address))
  81. {
  82. Debug.Assert(highCq);
  83. lock (_lock)
  84. {
  85. Debug.Assert(ProfiledFuncs.ContainsKey(address));
  86. ProfiledFuncs[address] = new FuncProfile(mode, highCq: true, blacklist ?? ProfiledFuncs[address].Blacklist);
  87. }
  88. }
  89. }
  90. public bool IsAddressInStaticCodeRange(ulong address)
  91. {
  92. return address >= StaticCodeStart && address < StaticCodeStart + StaticCodeSize;
  93. }
  94. public ConcurrentQueue<(ulong address, FuncProfile funcProfile)> GetProfiledFuncsToTranslate(TranslatorCache<TranslatedFunction> funcs)
  95. {
  96. ConcurrentQueue<(ulong address, FuncProfile funcProfile)> profiledFuncsToTranslate = new();
  97. foreach (KeyValuePair<ulong, FuncProfile> profiledFunc in ProfiledFuncs)
  98. {
  99. if (!funcs.ContainsKey(profiledFunc.Key) && !profiledFunc.Value.Blacklist)
  100. {
  101. profiledFuncsToTranslate.Enqueue((profiledFunc.Key, profiledFunc.Value));
  102. }
  103. }
  104. return profiledFuncsToTranslate;
  105. }
  106. public void ClearEntries()
  107. {
  108. ProfiledFuncs.Clear();
  109. ProfiledFuncs.TrimExcess();
  110. }
  111. public List<ulong> GetBlacklistedFunctions()
  112. {
  113. List<ulong> funcs = new List<ulong>();
  114. foreach (var profiledFunc in ProfiledFuncs)
  115. {
  116. if (profiledFunc.Value.Blacklist)
  117. {
  118. if (!funcs.Contains(profiledFunc.Key))
  119. {
  120. funcs.Add(profiledFunc.Key);
  121. }
  122. }
  123. }
  124. return funcs;
  125. }
  126. public void PreLoad()
  127. {
  128. _lastHash = default;
  129. string fileNameActual = $"{_ptc.CachePathActual}.info";
  130. string fileNameBackup = $"{_ptc.CachePathBackup}.info";
  131. FileInfo fileInfoActual = new(fileNameActual);
  132. FileInfo fileInfoBackup = new(fileNameBackup);
  133. if (fileInfoActual.Exists && fileInfoActual.Length != 0L)
  134. {
  135. if (!Load(fileNameActual, false))
  136. {
  137. if (fileInfoBackup.Exists && fileInfoBackup.Length != 0L)
  138. {
  139. Load(fileNameBackup, true);
  140. }
  141. }
  142. }
  143. else if (fileInfoBackup.Exists && fileInfoBackup.Length != 0L)
  144. {
  145. Load(fileNameBackup, true);
  146. }
  147. }
  148. private bool Load(string fileName, bool isBackup)
  149. {
  150. using (FileStream compressedStream = new(fileName, FileMode.Open))
  151. using (DeflateStream deflateStream = new(compressedStream, CompressionMode.Decompress, true))
  152. {
  153. OuterHeader outerHeader = DeserializeStructure<OuterHeader>(compressedStream);
  154. if (!outerHeader.IsHeaderValid())
  155. {
  156. InvalidateCompressedStream(compressedStream);
  157. return false;
  158. }
  159. if (outerHeader.Magic != _outerHeaderMagic)
  160. {
  161. InvalidateCompressedStream(compressedStream);
  162. return false;
  163. }
  164. if (outerHeader.InfoFileVersion != InternalVersion && !_migrateInternalVersions.Contains(outerHeader.InfoFileVersion))
  165. {
  166. InvalidateCompressedStream(compressedStream);
  167. return false;
  168. }
  169. if (outerHeader.Endianness != Ptc.GetEndianness())
  170. {
  171. InvalidateCompressedStream(compressedStream);
  172. return false;
  173. }
  174. using MemoryStream stream = MemoryStreamManager.Shared.GetStream();
  175. Debug.Assert(stream.Seek(0L, SeekOrigin.Begin) == 0L && stream.Length == 0L);
  176. try
  177. {
  178. deflateStream.CopyTo(stream);
  179. }
  180. catch
  181. {
  182. InvalidateCompressedStream(compressedStream);
  183. return false;
  184. }
  185. Debug.Assert(stream.Position == stream.Length);
  186. stream.Seek(0L, SeekOrigin.Begin);
  187. Hash128 expectedHash = DeserializeStructure<Hash128>(stream);
  188. Hash128 actualHash = Hash128.ComputeHash(GetReadOnlySpan(stream));
  189. if (actualHash != expectedHash)
  190. {
  191. InvalidateCompressedStream(compressedStream);
  192. return false;
  193. }
  194. Func<ulong, FuncProfile, (ulong, FuncProfile)> migrateEntryFunc = null;
  195. switch (outerHeader.InfoFileVersion)
  196. {
  197. case InternalVersion:
  198. ProfiledFuncs = Deserialize(stream);
  199. break;
  200. case 1866:
  201. migrateEntryFunc = (address, profile) => (address + 0x500000UL, profile);
  202. goto case 5518;
  203. case 5518:
  204. ProfiledFuncs = DeserializeAddBlacklist(stream, migrateEntryFunc);
  205. break;
  206. default:
  207. Logger.Error?.Print(LogClass.Ptc, $"No migration path for {nameof(outerHeader.InfoFileVersion)} '{outerHeader.InfoFileVersion}'. Discarding cache.");
  208. InvalidateCompressedStream(compressedStream);
  209. return false;
  210. }
  211. Debug.Assert(stream.Position == stream.Length);
  212. _lastHash = actualHash;
  213. }
  214. long fileSize = new FileInfo(fileName).Length;
  215. Logger.Info?.Print(LogClass.Ptc, $"{(isBackup ? "Loaded Backup Profiling Info" : "Loaded Profiling Info")} (size: {fileSize} bytes, profiled functions: {ProfiledFuncs.Count}).");
  216. return true;
  217. }
  218. private static Dictionary<ulong, FuncProfile> Deserialize(Stream stream, Func<ulong, FuncProfile, (ulong, FuncProfile)> migrateEntryFunc = null)
  219. {
  220. if (migrateEntryFunc != null)
  221. {
  222. return DeserializeAndUpdateDictionary(stream, DeserializeStructure<FuncProfile>, migrateEntryFunc);
  223. }
  224. return DeserializeDictionary<ulong, FuncProfile>(stream, DeserializeStructure<FuncProfile>);
  225. }
  226. private static Dictionary<ulong, FuncProfile> DeserializeAddBlacklist(Stream stream, Func<ulong, FuncProfile, (ulong, FuncProfile)> migrateEntryFunc = null)
  227. {
  228. if (migrateEntryFunc != null)
  229. {
  230. return DeserializeAndUpdateDictionary(stream, (Stream stream) => { return new FuncProfile(DeserializeStructure<FuncProfilePreBlacklist>(stream)); }, migrateEntryFunc);
  231. }
  232. return DeserializeDictionary<ulong, FuncProfile>(stream, (Stream stream) => { return new FuncProfile(DeserializeStructure<FuncProfilePreBlacklist>(stream)); });
  233. }
  234. private static ReadOnlySpan<byte> GetReadOnlySpan(MemoryStream memoryStream)
  235. {
  236. return new(memoryStream.GetBuffer(), (int)memoryStream.Position, (int)memoryStream.Length - (int)memoryStream.Position);
  237. }
  238. private static void InvalidateCompressedStream(FileStream compressedStream)
  239. {
  240. compressedStream.SetLength(0L);
  241. }
  242. private void PreSave()
  243. {
  244. _waitEvent.Reset();
  245. string fileNameActual = $"{_ptc.CachePathActual}.info";
  246. string fileNameBackup = $"{_ptc.CachePathBackup}.info";
  247. FileInfo fileInfoActual = new(fileNameActual);
  248. if (fileInfoActual.Exists && fileInfoActual.Length != 0L)
  249. {
  250. File.Copy(fileNameActual, fileNameBackup, true);
  251. }
  252. Save(fileNameActual);
  253. _waitEvent.Set();
  254. }
  255. private void Save(string fileName)
  256. {
  257. int profiledFuncsCount;
  258. OuterHeader outerHeader = new()
  259. {
  260. Magic = _outerHeaderMagic,
  261. InfoFileVersion = InternalVersion,
  262. Endianness = Ptc.GetEndianness(),
  263. };
  264. outerHeader.SetHeaderHash();
  265. using (MemoryStream stream = MemoryStreamManager.Shared.GetStream())
  266. {
  267. Debug.Assert(stream.Seek(0L, SeekOrigin.Begin) == 0L && stream.Length == 0L);
  268. stream.Seek(Unsafe.SizeOf<Hash128>(), SeekOrigin.Begin);
  269. lock (_lock)
  270. {
  271. Serialize(stream, ProfiledFuncs);
  272. profiledFuncsCount = ProfiledFuncs.Count;
  273. }
  274. Debug.Assert(stream.Position == stream.Length);
  275. stream.Seek(Unsafe.SizeOf<Hash128>(), SeekOrigin.Begin);
  276. Hash128 hash = Hash128.ComputeHash(GetReadOnlySpan(stream));
  277. stream.Seek(0L, SeekOrigin.Begin);
  278. SerializeStructure(stream, hash);
  279. if (hash == _lastHash)
  280. {
  281. return;
  282. }
  283. using FileStream compressedStream = new(fileName, FileMode.OpenOrCreate);
  284. using DeflateStream deflateStream = new(compressedStream, SaveCompressionLevel, true);
  285. try
  286. {
  287. SerializeStructure(compressedStream, outerHeader);
  288. stream.WriteTo(deflateStream);
  289. _lastHash = hash;
  290. }
  291. catch
  292. {
  293. compressedStream.Position = 0L;
  294. _lastHash = default;
  295. }
  296. if (compressedStream.Position < compressedStream.Length)
  297. {
  298. compressedStream.SetLength(compressedStream.Position);
  299. }
  300. }
  301. long fileSize = new FileInfo(fileName).Length;
  302. if (fileSize != 0L)
  303. {
  304. Logger.Info?.Print(LogClass.Ptc, $"Saved Profiling Info (size: {fileSize} bytes, profiled functions: {profiledFuncsCount}).");
  305. }
  306. }
  307. private static void Serialize(Stream stream, Dictionary<ulong, FuncProfile> profiledFuncs)
  308. {
  309. SerializeDictionary(stream, profiledFuncs, SerializeStructure);
  310. }
  311. [StructLayout(LayoutKind.Sequential, Pack = 1/*, Size = 29*/)]
  312. private struct OuterHeader
  313. {
  314. public ulong Magic;
  315. public uint InfoFileVersion;
  316. public bool Endianness;
  317. public Hash128 HeaderHash;
  318. public void SetHeaderHash()
  319. {
  320. Span<OuterHeader> spanHeader = MemoryMarshal.CreateSpan(ref this, 1);
  321. HeaderHash = Hash128.ComputeHash(MemoryMarshal.AsBytes(spanHeader)[..(Unsafe.SizeOf<OuterHeader>() - Unsafe.SizeOf<Hash128>())]);
  322. }
  323. public bool IsHeaderValid()
  324. {
  325. Span<OuterHeader> spanHeader = MemoryMarshal.CreateSpan(ref this, 1);
  326. return Hash128.ComputeHash(MemoryMarshal.AsBytes(spanHeader)[..(Unsafe.SizeOf<OuterHeader>() - Unsafe.SizeOf<Hash128>())]) == HeaderHash;
  327. }
  328. }
  329. [StructLayout(LayoutKind.Sequential, Pack = 1/*, Size = 6*/)]
  330. public struct FuncProfile
  331. {
  332. public ExecutionMode Mode;
  333. public bool HighCq;
  334. public bool Blacklist;
  335. public FuncProfile(ExecutionMode mode, bool highCq, bool blacklist)
  336. {
  337. Mode = mode;
  338. HighCq = highCq;
  339. Blacklist = blacklist;
  340. }
  341. public FuncProfile(FuncProfilePreBlacklist fp)
  342. {
  343. Mode = fp.Mode;
  344. HighCq = fp.HighCq;
  345. Blacklist = false;
  346. }
  347. }
  348. [StructLayout(LayoutKind.Sequential, Pack = 1/*, Size = 5*/)]
  349. public struct FuncProfilePreBlacklist
  350. {
  351. public ExecutionMode Mode;
  352. public bool HighCq;
  353. public FuncProfilePreBlacklist(ExecutionMode mode, bool highCq)
  354. {
  355. Mode = mode;
  356. HighCq = highCq;
  357. }
  358. }
  359. public void Start()
  360. {
  361. if (_ptc.State == PtcState.Enabled ||
  362. _ptc.State == PtcState.Continuing)
  363. {
  364. Enabled = true;
  365. _timer.Enabled = true;
  366. }
  367. }
  368. public void Stop()
  369. {
  370. Enabled = false;
  371. if (!_disposed)
  372. {
  373. _timer.Enabled = false;
  374. }
  375. }
  376. public void Wait()
  377. {
  378. _waitEvent.WaitOne();
  379. }
  380. public void Dispose()
  381. {
  382. if (!_disposed)
  383. {
  384. _disposed = true;
  385. _timer.Elapsed -= TimerElapsed;
  386. _timer.Dispose();
  387. Wait();
  388. _waitEvent.Dispose();
  389. }
  390. }
  391. }
  392. }