Make Counter a generic & use a 32-bit counter instead

This commit is contained in:
FICTURE7 2021-04-10 14:54:24 +04:00
parent 4478a32114
commit 6c28be13a8
5 changed files with 55 additions and 37 deletions

View file

@ -1,20 +1,23 @@
namespace ARMeilleure.Common using System;
namespace ARMeilleure.Common
{ {
/// <summary> /// <summary>
/// Represents an 8-bit counter. /// Represents a numeric counter.
/// </summary> /// </summary>
class Counter /// <typeparam name="T">Type of the counter</typeparam>
class Counter<T> where T : unmanaged
{ {
private readonly int _index; private readonly int _index;
private readonly EntryTable<byte> _countTable; private readonly EntryTable<T> _countTable;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Counter"/> class from the specified /// Initializes a new instance of the <see cref="Counter{T}"/> class from the specified
/// <see cref="EntryTable{byte}"/> instance and index. /// <see cref="EntryTable{T}"/> instance and index.
/// </summary> /// </summary>
/// <param name="countTable"><see cref="EntryTable{byte}"/> instance</param> /// <param name="countTable"><see cref="EntryTable{byte}"/> instance</param>
/// <param name="index">Index in the <see cref="EntryTable{TEntry}"/></param> /// <param name="index">Index in the <see cref="EntryTable{T}"/></param>
private Counter(EntryTable<byte> countTable, int index) private Counter(EntryTable<T> countTable, int index)
{ {
_countTable = countTable; _countTable = countTable;
_index = index; _index = index;
@ -23,7 +26,7 @@
/// <summary> /// <summary>
/// Gets a reference to the value of the counter. /// Gets a reference to the value of the counter.
/// </summary> /// </summary>
public ref byte Value => ref _countTable.GetValue(_index); public ref T Value => ref _countTable.GetValue(_index);
/// <summary> /// <summary>
/// Tries to create a <see cref="Counter"/> instance from the specified <see cref="EntryTable{byte}"/> instance. /// Tries to create a <see cref="Counter"/> instance from the specified <see cref="EntryTable{byte}"/> instance.
@ -31,11 +34,28 @@
/// <param name="countTable"><see cref="EntryTable{TEntry}"/> from which to create the <see cref="Counter"/></param> /// <param name="countTable"><see cref="EntryTable{TEntry}"/> from which to create the <see cref="Counter"/></param>
/// <param name="counter"><see cref="Counter"/> instance if success; otherwise <see langword="null"/></param> /// <param name="counter"><see cref="Counter"/> instance if success; otherwise <see langword="null"/></param>
/// <returns><see langword="true"/> if success; otherwise <see langword="false"/></returns> /// <returns><see langword="true"/> if success; otherwise <see langword="false"/></returns>
public static bool TryCreate(EntryTable<byte> countTable, out Counter counter) /// <exception cref="ArgumentNullException"><paramref name="countTable"/> is <see langword="null"/></exception>
/// <exception cref="ArgumentException"><typeparamref name="T"/> is unsupported</exception>
public static bool TryCreate(EntryTable<T> countTable, out Counter<T> counter)
{ {
if (countTable == null)
{
throw new ArgumentNullException(nameof(countTable));
}
if (typeof(T) != typeof(byte) && typeof(T) != typeof(sbyte) &&
typeof(T) != typeof(short) && typeof(T) != typeof(ushort) &&
typeof(T) != typeof(int) && typeof(T) != typeof(uint) &&
typeof(T) != typeof(long) && typeof(T) != typeof(ulong) &&
typeof(T) != typeof(nint) && typeof(T) != typeof(nuint) &&
typeof(T) != typeof(float) && typeof(T) != typeof(double))
{
throw new ArgumentException("Counter does not support the specified type", nameof(countTable));
}
if (countTable.TryAllocate(out int index)) if (countTable.TryAllocate(out int index))
{ {
counter = new Counter(countTable, index); counter = new Counter<T>(countTable, index);
return true; return true;
} }

View file

@ -42,7 +42,7 @@ namespace ARMeilleure.Translation
public IMemoryManager Memory { get; } public IMemoryManager Memory { get; }
public JumpTable JumpTable { get; } public JumpTable JumpTable { get; }
public EntryTable<byte> CountTable { get; } public EntryTable<uint> CountTable { get; }
public ulong EntryAddress { get; } public ulong EntryAddress { get; }
public bool HighCq { get; } public bool HighCq { get; }
@ -51,7 +51,7 @@ namespace ARMeilleure.Translation
public ArmEmitterContext( public ArmEmitterContext(
IMemoryManager memory, IMemoryManager memory,
JumpTable jumpTable, JumpTable jumpTable,
EntryTable<byte> countTable, EntryTable<uint> countTable,
ulong entryAddress, ulong entryAddress,
bool highCq, bool highCq,
Aarch32Mode mode) Aarch32Mode mode)

View file

@ -540,7 +540,8 @@ namespace ARMeilleure.Translation.PTC
} }
} }
internal static void LoadTranslations(ConcurrentDictionary<ulong, TranslatedFunction> funcs, IMemoryManager memory, JumpTable jumpTable, EntryTable<byte> countTable) internal static void LoadTranslations(ConcurrentDictionary<ulong, TranslatedFunction> funcs, IMemoryManager memory,
JumpTable jumpTable, EntryTable<uint> countTable)
{ {
if (AreCarriersEmpty()) if (AreCarriersEmpty())
{ {
@ -569,7 +570,7 @@ namespace ARMeilleure.Translation.PTC
{ {
byte[] code = ReadCode(index, infoEntry.CodeLength); byte[] code = ReadCode(index, infoEntry.CodeLength);
Counter callCounter = null; Counter<uint> callCounter = null;
if (infoEntry.RelocEntriesCount != 0) if (infoEntry.RelocEntriesCount != 0)
{ {
@ -679,7 +680,8 @@ namespace ARMeilleure.Translation.PTC
return relocEntries; return relocEntries;
} }
private static bool PatchCode(Span<byte> code, RelocEntry[] relocEntries, IntPtr pageTablePointer, JumpTable jumpTable, EntryTable<byte> countTable, out Counter callCounter) private static bool PatchCode(Span<byte> code, RelocEntry[] relocEntries, IntPtr pageTablePointer,
JumpTable jumpTable, EntryTable<uint> countTable, out Counter<uint> callCounter)
{ {
callCounter = null; callCounter = null;
@ -702,7 +704,7 @@ namespace ARMeilleure.Translation.PTC
else if (relocEntry.Index == CountTableIndex) else if (relocEntry.Index == CountTableIndex)
{ {
// If we could not allocate an entry on the count table we dip. // If we could not allocate an entry on the count table we dip.
if (!Counter.TryCreate(countTable, out Counter counter)) if (!Counter<uint>.TryCreate(countTable, out Counter<uint> counter))
{ {
return false; return false;
} }
@ -745,7 +747,8 @@ namespace ARMeilleure.Translation.PTC
return new UnwindInfo(pushEntries, prologueSize); return new UnwindInfo(pushEntries, prologueSize);
} }
private static TranslatedFunction FastTranslate(byte[] code, Counter callCounter, ulong guestSize, UnwindInfo unwindInfo, bool highCq) private static TranslatedFunction FastTranslate(byte[] code, Counter<uint> callCounter, ulong guestSize,
UnwindInfo unwindInfo, bool highCq)
{ {
CompiledFunction cFunc = new CompiledFunction(code, unwindInfo); CompiledFunction cFunc = new CompiledFunction(code, unwindInfo);
@ -794,7 +797,8 @@ namespace ARMeilleure.Translation.PTC
} }
} }
internal static void MakeAndSaveTranslations(ConcurrentDictionary<ulong, TranslatedFunction> funcs, IMemoryManager memory, JumpTable jumpTable, EntryTable<byte> countTable) internal static void MakeAndSaveTranslations(ConcurrentDictionary<ulong, TranslatedFunction> funcs, IMemoryManager memory,
JumpTable jumpTable, EntryTable<uint> countTable)
{ {
var profiledFuncsToTranslate = PtcProfiler.GetProfiledFuncsToTranslate(funcs); var profiledFuncsToTranslate = PtcProfiler.GetProfiledFuncsToTranslate(funcs);

View file

@ -8,12 +8,12 @@ namespace ARMeilleure.Translation
{ {
private readonly GuestFunction _func; // Ensure that this delegate will not be garbage collected. private readonly GuestFunction _func; // Ensure that this delegate will not be garbage collected.
public Counter CallCounter { get; } public Counter<uint> CallCounter { get; }
public ulong GuestSize { get; } public ulong GuestSize { get; }
public bool HighCq { get; } public bool HighCq { get; }
public IntPtr FuncPtr { get; } public IntPtr FuncPtr { get; }
public TranslatedFunction(GuestFunction func, Counter callCounter, ulong guestSize, bool highCq) public TranslatedFunction(GuestFunction func, Counter<uint> callCounter, ulong guestSize, bool highCq)
{ {
_func = func; _func = func;
CallCounter = callCounter; CallCounter = callCounter;

View file

@ -37,7 +37,7 @@ namespace ARMeilleure.Translation
private JumpTable _jumpTable; private JumpTable _jumpTable;
internal JumpTable JumpTable => _jumpTable; internal JumpTable JumpTable => _jumpTable;
internal EntryTable<byte> CountTable { get; } internal EntryTable<uint> CountTable { get; }
private volatile int _threadCount; private volatile int _threadCount;
@ -59,7 +59,7 @@ namespace ARMeilleure.Translation
_backgroundTranslatorEvent = new AutoResetEvent(false); _backgroundTranslatorEvent = new AutoResetEvent(false);
_backgroundTranslatorLock = new ReaderWriterLock(); _backgroundTranslatorLock = new ReaderWriterLock();
CountTable = new EntryTable<byte>(capacity: 16 * 1024 * 1024); CountTable = new EntryTable<uint>(capacity: 4 * 1024 * 1024);
JitCache.Initialize(allocator); JitCache.Initialize(allocator);
@ -239,7 +239,7 @@ namespace ARMeilleure.Translation
internal static TranslatedFunction Translate( internal static TranslatedFunction Translate(
IMemoryManager memory, IMemoryManager memory,
JumpTable jumpTable, JumpTable jumpTable,
EntryTable<byte> countTable, EntryTable<uint> countTable,
ulong address, ulong address,
ExecutionMode mode, ExecutionMode mode,
bool highCq) bool highCq)
@ -256,7 +256,7 @@ namespace ARMeilleure.Translation
Logger.StartPass(PassName.Translation); Logger.StartPass(PassName.Translation);
Counter counter = null; Counter<uint> counter = null;
if (!context.HighCq) if (!context.HighCq)
{ {
@ -415,29 +415,23 @@ namespace ARMeilleure.Translation
return context.GetControlFlowGraph(); return context.GetControlFlowGraph();
} }
internal static void EmitRejitCheck(ArmEmitterContext context, out Counter counter) internal static void EmitRejitCheck(ArmEmitterContext context, out Counter<uint> counter)
{ {
if (!Counter.TryCreate(context.CountTable, out counter)) if (!Counter<uint>.TryCreate(context.CountTable, out counter))
{ {
return; return;
} }
Operand lblRejit = Label();
Operand lblAdd = Label();
Operand lblEnd = Label(); Operand lblEnd = Label();
Operand address = Const(ref counter.Value, Ptc.CountTableIndex); Operand address = Const(ref counter.Value, Ptc.CountTableIndex);
Operand count = context.Load8(address); Operand curCount = context.Load(OperandType.I32, address);
context.BranchIf(lblAdd, count, Const(100), Comparison.LessUI); Operand count = context.Add(curCount, Const(1));
context.BranchIf(lblRejit, count, Const(100), Comparison.Equal); context.Store(address, count);
context.Branch(lblEnd); context.BranchIf(lblEnd, curCount, Const(100), Comparison.NotEqual, BasicBlockFrequency.Cold);
context.MarkLabel(lblRejit, BasicBlockFrequency.Cold);
context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.EnqueueForRejit)), Const(context.EntryAddress)); context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.EnqueueForRejit)), Const(context.EntryAddress));
context.MarkLabel(lblAdd, BasicBlockFrequency.Cold);
context.Store8(address, context.Add(count, Const(1)));
context.MarkLabel(lblEnd); context.MarkLabel(lblEnd);
} }