From 89791ba68dba70999643c5d876e9329b385c6e8a Mon Sep 17 00:00:00 2001 From: FICTURE7 Date: Mon, 19 Apr 2021 01:43:53 +0400 Subject: Add inlined on translation call counting (#2190) * Add EntryTable * Add on translation call counting * Add Counter * Add PPTC support * Make Counter a generic & use a 32-bit counter instead * Return false on overflow * Set PPTC version * Print more information about the rejit queue * Make Counter disposable * Remove Block.TailCall since it is not used anymore * Apply suggestions from code review Address gdkchan's feedback Co-authored-by: gdkchan * Fix more stale docs * Remove rejit requests queue logging * Make Counter finalizable Most certainly quite an odd use case. * Make EntryTable.TryAllocate set entry to default * Re-trigger CI * Dispose Counters before they hit the finalizer queue * Re-trigger CI Just for good measure... * Make EntryTable expandable * EntryTable is now expandable instead of being a fixed slab. * Remove EntryTable.TryAllocate * Remove Counter.TryCreate Address LDj3SNuD's feedback * Apply suggestions from code review Address LDj3SNuD's feedback Co-authored-by: LDj3SNuD <35856442+LDj3SNuD@users.noreply.github.com> * Remove useless return * POH approach, but the sequel * Revert "POH approach, but the sequel" This reverts commit 5f5abaa24735726ff2db367dc74f98055d4f4cba. The sequel got shelved * Add extra documentation Co-authored-by: gdkchan Co-authored-by: LDj3SNuD <35856442+LDj3SNuD@users.noreply.github.com> --- ARMeilleure/Translation/PTC/Ptc.cs | 51 ++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 10 deletions(-) (limited to 'ARMeilleure/Translation/PTC') diff --git a/ARMeilleure/Translation/PTC/Ptc.cs b/ARMeilleure/Translation/PTC/Ptc.cs index 32e0e7e8..55a0f4d0 100644 --- a/ARMeilleure/Translation/PTC/Ptc.cs +++ b/ARMeilleure/Translation/PTC/Ptc.cs @@ -1,6 +1,7 @@ using ARMeilleure.CodeGen; using ARMeilleure.CodeGen.Unwinding; using ARMeilleure.CodeGen.X86; +using ARMeilleure.Common; using ARMeilleure.Memory; using ARMeilleure.Translation.Cache; using Ryujinx.Common; @@ -27,7 +28,7 @@ namespace ARMeilleure.Translation.PTC private const string OuterHeaderMagicString = "PTCohd\0\0"; private const string InnerHeaderMagicString = "PTCihd\0\0"; - private const uint InternalVersion = 2169; //! To be incremented manually for each change to the ARMeilleure project. + private const uint InternalVersion = 2190; //! To be incremented manually for each change to the ARMeilleure project. private const string ActualDir = "0"; private const string BackupDir = "1"; @@ -38,6 +39,7 @@ namespace ARMeilleure.Translation.PTC internal const int PageTablePointerIndex = -1; // Must be a negative value. internal const int JumpPointerIndex = -2; // Must be a negative value. internal const int DynamicPointerIndex = -3; // Must be a negative value. + internal const int CountTableIndex = -4; // Must be a negative value. private const byte FillingByte = 0x00; private const CompressionLevel SaveCompressionLevel = CompressionLevel.Fastest; @@ -538,7 +540,11 @@ namespace ARMeilleure.Translation.PTC } } - internal static void LoadTranslations(ConcurrentDictionary funcs, IMemoryManager memory, JumpTable jumpTable) + internal static void LoadTranslations( + ConcurrentDictionary funcs, + IMemoryManager memory, + JumpTable jumpTable, + EntryTable countTable) { if (AreCarriersEmpty()) { @@ -567,16 +573,18 @@ namespace ARMeilleure.Translation.PTC { byte[] code = ReadCode(index, infoEntry.CodeLength); + Counter callCounter = null; + if (infoEntry.RelocEntriesCount != 0) { RelocEntry[] relocEntries = GetRelocEntries(relocsReader, infoEntry.RelocEntriesCount); - PatchCode(code.AsSpan(), relocEntries, memory.PageTablePointer, jumpTable); + PatchCode(code, relocEntries, memory.PageTablePointer, jumpTable, countTable, out callCounter); } UnwindInfo unwindInfo = ReadUnwindInfo(unwindInfosReader); - TranslatedFunction func = FastTranslate(code, infoEntry.GuestSize, unwindInfo, infoEntry.HighCq); + TranslatedFunction func = FastTranslate(code, callCounter, infoEntry.GuestSize, unwindInfo, infoEntry.HighCq); bool isAddressUnique = funcs.TryAdd(infoEntry.Address, func); @@ -670,8 +678,16 @@ namespace ARMeilleure.Translation.PTC return relocEntries; } - private static void PatchCode(Span code, RelocEntry[] relocEntries, IntPtr pageTablePointer, JumpTable jumpTable) + private static void PatchCode( + Span code, + RelocEntry[] relocEntries, + IntPtr pageTablePointer, + JumpTable jumpTable, + EntryTable countTable, + out Counter callCounter) { + callCounter = null; + foreach (RelocEntry relocEntry in relocEntries) { ulong imm; @@ -688,6 +704,12 @@ namespace ARMeilleure.Translation.PTC { imm = (ulong)jumpTable.DynamicPointer.ToInt64(); } + else if (relocEntry.Index == CountTableIndex) + { + callCounter = new Counter(countTable); + + unsafe { imm = (ulong)Unsafe.AsPointer(ref callCounter.Value); } + } else if (Delegates.TryGetDelegateFuncPtrByIndex(relocEntry.Index, out IntPtr funcPtr)) { imm = (ulong)funcPtr.ToInt64(); @@ -722,7 +744,12 @@ namespace ARMeilleure.Translation.PTC return new UnwindInfo(pushEntries, prologueSize); } - private static TranslatedFunction FastTranslate(byte[] code, ulong guestSize, UnwindInfo unwindInfo, bool highCq) + private static TranslatedFunction FastTranslate( + byte[] code, + Counter callCounter, + ulong guestSize, + UnwindInfo unwindInfo, + bool highCq) { CompiledFunction cFunc = new CompiledFunction(code, unwindInfo); @@ -730,7 +757,7 @@ namespace ARMeilleure.Translation.PTC GuestFunction gFunc = Marshal.GetDelegateForFunctionPointer(codePtr); - TranslatedFunction tFunc = new TranslatedFunction(gFunc, guestSize, highCq); + TranslatedFunction tFunc = new TranslatedFunction(gFunc, callCounter, guestSize, highCq); return tFunc; } @@ -771,7 +798,11 @@ namespace ARMeilleure.Translation.PTC } } - internal static void MakeAndSaveTranslations(ConcurrentDictionary funcs, IMemoryManager memory, JumpTable jumpTable) + internal static void MakeAndSaveTranslations( + ConcurrentDictionary funcs, + IMemoryManager memory, + JumpTable jumpTable, + EntryTable countTable) { var profiledFuncsToTranslate = PtcProfiler.GetProfiledFuncsToTranslate(funcs); @@ -813,7 +844,7 @@ namespace ARMeilleure.Translation.PTC Debug.Assert(PtcProfiler.IsAddressInStaticCodeRange(address)); - TranslatedFunction func = Translator.Translate(memory, jumpTable, address, item.mode, item.highCq); + TranslatedFunction func = Translator.Translate(memory, jumpTable, countTable, address, item.mode, item.highCq); bool isAddressUnique = funcs.TryAdd(address, func); @@ -1058,4 +1089,4 @@ namespace ARMeilleure.Translation.PTC } } } -} \ No newline at end of file +} -- cgit v1.2.3