Skip to content

Commit

Permalink
Memory Read/Write Tracking using Region Handles (#1272)
Browse files Browse the repository at this point in the history
* WIP Range Tracking

- Texture invalidation seems to have large problems
- Buffer/Pool invalidation may have problems
- Mirror memory tracking puts an additional `add` in compiled code, we likely just want to make HLE access slower if this is the final solution.
- Native project is in the messiest possible location.
- [HACK] JIT memory access always uses native "fast" path
- [HACK] Trying some things with texture invalidation and views.

It works :)

Still a few hacks, messy things, slow things

More work in progress stuff (also move to memory project)

Quite a bit faster now.
- Unmapping GPU VA and CPU VA will now correctly update write tracking regions, and invalidate textures for the former.
- The Virtual range list is now non-overlapping like the physical one.
- Fixed some bugs where regions could leak.
- Introduced a weird bug that I still need to track down (consistent invalid buffer in MK8 ribbon road)

Move some stuff.

I think we'll eventually just put the dll and so for this in a nuget package.

Fix rebase.

[WIP] MultiRegionHandle variable size ranges

- Avoid reprotecting regions that change often (needs some tweaking)
- There's still a bug in buffers, somehow.
- Might want different api for minimum granularity

Fix rebase issue

Commit everything needed for software only tracking.

Remove native components.

Remove more native stuff.

Cleanup

Use a separate window for the background context, update opentk. (fixes linux)

Some experimental changes

Should get things working up to scratch - still need to try some things with flush/modification and res scale.

Include address with the region action.

Initial work to make range tracking work

Still a ton of bugs

Fix some issues with the new stuff.

* Fix texture flush instability

There's still some weird behaviour, but it's much improved without this. (textures with cpu modified data were flushing over it)

* Find the destination texture for Buffer->Texture full copy

Greatly improves performance for nvdec videos (with range tracking)

* Further improve texture tracking

* Disable Memory Tracking for view parents

This is a temporary approach to better match behaviour on master (where invalidations would be soaked up by views, rather than trigger twice)

The assumption is that when views are created to a texture, they will cover all of its data anyways. Of course, this can easily be improved in future.

* Introduce some tracking tests.

WIP

* Complete base tests.

* Add more tests for multiregion, fix existing test.

* Cleanup Part 1

* Remove unnecessary code from memory tracking

* Fix some inconsistencies with 3D texture rule.

* Add dispose tests.

* Use a background thread for the background context.

Rather than setting and unsetting a context as current, doing the work on a dedicated thread with signals seems to be a bit faster.

Also nerf the multithreading test a bit.

* Copy to texture with matching alignment

This extends the copy to work for some videos with unusual size, such as tutorial videos in SMO. It will only occur if the destination texture already exists at XCount size.

* Track reads for buffer copies. Synchronize new buffers before copying overlaps.

* Remove old texture flushing mechanisms.

Range tracking all the way, baby.

* Wake the background thread when disposing.

Avoids a deadlock when games are closed.

* Address Feedback 1

* Separate TextureCopy instance for background thread

Also `BackgroundContextWorker.InBackground` for a more sensible idenfifier for if we're in a background thread.

* Add missing XML docs.

* Address Feedback

* Maybe I should start drinking coffee.

* Some more feedback.

* Remove flush warning, Refocus window after making background context
  • Loading branch information
riperiperi authored Oct 16, 2020
1 parent f02791b commit b4d8d89
Show file tree
Hide file tree
Showing 50 changed files with 2,963 additions and 431 deletions.
23 changes: 11 additions & 12 deletions ARMeilleure/Instructions/InstEmitMemoryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,23 +391,22 @@ public static Operand EmitPtPointerLoad(ArmEmitterContext context, Operand addre

if (lblSlowPath != null)
{
context.BranchIf(lblSlowPath, pte, Const(0L), Comparison.LessOrEqual);
ulong protection = (write ? 3UL : 1UL) << 48;
context.BranchIfTrue(lblSlowPath, context.BitwiseAnd(pte, Const(protection)));
}
else
{
// When no label is provided to jump to a slow path if the address is invalid,
// we do the validation ourselves, and throw if needed.
if (write)
{
Operand lblNotWatched = Label();

// Is the page currently being monitored for modifications? If so we need to call MarkRegionAsModified.
context.BranchIf(lblNotWatched, pte, Const(0L), Comparison.GreaterOrEqual, BasicBlockFrequency.Cold);
Operand lblNotWatched = Label();

// Mark the region as modified. Size here doesn't matter as address is assumed to be size aligned here.
context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.MarkRegionAsModified)), address, Const(1UL));
context.MarkLabel(lblNotWatched);
}
// Is the page currently being tracked for read/write? If so we need to call MarkRegionAsModified.
context.BranchIf(lblNotWatched, pte, Const(0L), Comparison.GreaterOrEqual, BasicBlockFrequency.Cold);

// Mark the region as modified. Size here doesn't matter as address is assumed to be size aligned here.
context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.SignalMemoryTracking)), address, Const(1UL), Const(write ? 1 : 0));
context.MarkLabel(lblNotWatched);

Operand lblNonNull = Label();

Expand All @@ -417,10 +416,10 @@ public static Operand EmitPtPointerLoad(ArmEmitterContext context, Operand addre
// The call is not expected to return (it should throw).
context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.ThrowInvalidMemoryAccess)), address);
context.MarkLabel(lblNonNull);

pte = context.BitwiseAnd(pte, Const(0xffffffffffffUL));
}

pte = context.BitwiseAnd(pte, Const(0xffffffffffffUL)); // Ignore any software protection bits. (they are still used by c# memory access)

Operand pageOffset = context.BitwiseAnd(address, Const(address.Type, PageMask));

if (pageOffset.Type == OperandType.I32)
Expand Down
14 changes: 7 additions & 7 deletions ARMeilleure/Instructions/NativeInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,27 +163,27 @@ public static void SetTpidrEl032(uint value)
#region "Read"
public static byte ReadByte(ulong address)
{
return GetMemoryManager().Read<byte>(address);
return GetMemoryManager().ReadTracked<byte>(address);
}

public static ushort ReadUInt16(ulong address)
{
return GetMemoryManager().Read<ushort>(address);
return GetMemoryManager().ReadTracked<ushort>(address);
}

public static uint ReadUInt32(ulong address)
{
return GetMemoryManager().Read<uint>(address);
return GetMemoryManager().ReadTracked<uint>(address);
}

public static ulong ReadUInt64(ulong address)
{
return GetMemoryManager().Read<ulong>(address);
return GetMemoryManager().ReadTracked<ulong>(address);
}

public static V128 ReadVector128(ulong address)
{
return GetMemoryManager().Read<V128>(address);
return GetMemoryManager().ReadTracked<V128>(address);
}
#endregion

Expand Down Expand Up @@ -214,9 +214,9 @@ public static void WriteVector128(ulong address, V128 value)
}
#endregion

public static void MarkRegionAsModified(ulong address, ulong size)
public static void SignalMemoryTracking(ulong address, ulong size, bool write)
{
GetMemoryManager().MarkRegionAsModified(address, size);
GetMemoryManager().SignalMemoryTracking(address, size, write);
}

public static void ThrowInvalidMemoryAccess(ulong address)
Expand Down
3 changes: 2 additions & 1 deletion ARMeilleure/Memory/IMemoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ public interface IMemoryManager
IntPtr PageTablePointer { get; }

T Read<T>(ulong va) where T : unmanaged;
T ReadTracked<T>(ulong va) where T : unmanaged;
void Write<T>(ulong va, T value) where T : unmanaged;

ref T GetRef<T>(ulong va) where T : unmanaged;

bool IsMapped(ulong va);

void MarkRegionAsModified(ulong va, ulong size);
void SignalMemoryTracking(ulong va, ulong size, bool write);
}
}
2 changes: 1 addition & 1 deletion ARMeilleure/Translation/Delegates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ static Delegates()
SetDelegateInfo(typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetTpidr32))); // A32 only.
SetDelegateInfo(typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetTpidrEl0)));
SetDelegateInfo(typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetTpidrEl032))); // A32 only.
SetDelegateInfo(typeof(NativeInterface).GetMethod(nameof(NativeInterface.MarkRegionAsModified)));
SetDelegateInfo(typeof(NativeInterface).GetMethod(nameof(NativeInterface.ReadByte)));
SetDelegateInfo(typeof(NativeInterface).GetMethod(nameof(NativeInterface.ReadUInt16)));
SetDelegateInfo(typeof(NativeInterface).GetMethod(nameof(NativeInterface.ReadUInt32)));
Expand All @@ -129,6 +128,7 @@ static Delegates()
SetDelegateInfo(typeof(NativeInterface).GetMethod(nameof(NativeInterface.SetFpsrQc))); // A32 only.
SetDelegateInfo(typeof(NativeInterface).GetMethod(nameof(NativeInterface.SetTpidrEl0)));
SetDelegateInfo(typeof(NativeInterface).GetMethod(nameof(NativeInterface.SetTpidrEl032))); // A32 only.
SetDelegateInfo(typeof(NativeInterface).GetMethod(nameof(NativeInterface.SignalMemoryTracking)));
SetDelegateInfo(typeof(NativeInterface).GetMethod(nameof(NativeInterface.SupervisorCall)));
SetDelegateInfo(typeof(NativeInterface).GetMethod(nameof(NativeInterface.ThrowInvalidMemoryAccess)));
SetDelegateInfo(typeof(NativeInterface).GetMethod(nameof(NativeInterface.Undefined)));
Expand Down
2 changes: 1 addition & 1 deletion ARMeilleure/Translation/PTC/Ptc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static class Ptc
{
private const string HeaderMagic = "PTChd";

private const int InternalVersion = 1577; //! To be incremented manually for each change to the ARMeilleure project.
private const int InternalVersion = 1273; //! To be incremented manually for each change to the ARMeilleure project.

private const string ActualDir = "0";
private const string BackupDir = "1";
Expand Down
2 changes: 1 addition & 1 deletion Ryujinx.Audio/Ryujinx.Audio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="OpenTK.NetStandard" Version="1.0.5.12" />
<PackageReference Include="OpenTK.NetStandard" Version="1.0.5.22" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit b4d8d89

Please sign in to comment.