1
0
Fork 0
mirror of https://github.com/Ryujinx/Ryujinx.git synced 2024-10-01 12:30:00 +02:00

nvservice: add a lock around NvHostEvent and remove release fence on SFv2 (#1197)

* nvservice: add a lock to NvHostEvent

* Disable surface flinger release fence and readd infinite timeout

* FenceAction: Add a timeout of 1 seconds as this shouldn't wait forever anyuway

* surfaceflinger: remove leftovers from the release fence

* Don't allow infinite timeout on syncpoint while printing all timeout for better debugging
This commit is contained in:
Thog 2020-05-02 22:47:06 +02:00 committed by GitHub
parent 0a3b75ae2b
commit 764891e670
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 237 additions and 211 deletions

View file

@ -112,14 +112,10 @@ namespace Ryujinx.Graphics.Gpu.Synchronization
throw new ArgumentOutOfRangeException(nameof(id)); throw new ArgumentOutOfRangeException(nameof(id));
} }
bool warnAboutTimeout = false;
// TODO: Remove this when GPU channel scheduling will be implemented. // TODO: Remove this when GPU channel scheduling will be implemented.
if (timeout == Timeout.InfiniteTimeSpan) if (timeout == Timeout.InfiniteTimeSpan)
{ {
timeout = TimeSpan.FromSeconds(1); timeout = TimeSpan.FromSeconds(1);
warnAboutTimeout = true;
} }
using (ManualResetEvent waitEvent = new ManualResetEvent(false)) using (ManualResetEvent waitEvent = new ManualResetEvent(false))
@ -135,10 +131,7 @@ namespace Ryujinx.Graphics.Gpu.Synchronization
if (!signaled && info != null) if (!signaled && info != null)
{ {
if (warnAboutTimeout) Logger.PrintError(LogClass.Gpu, $"Wait on syncpoint {id} for threshold {threshold} took more than {timeout.TotalMilliseconds}ms, resuming execution...");
{
Logger.PrintError(LogClass.Gpu, $"Wait on syncpoint {id} for threshold {threshold} took more than {timeout.TotalMilliseconds}ms, resuming execution...");
}
_syncpoints[id].UnregisterCallback(info); _syncpoints[id].UnregisterCallback(info);
} }

View file

@ -95,26 +95,29 @@ namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl
private KEvent QueryEvent(uint eventId) private KEvent QueryEvent(uint eventId)
{ {
uint eventSlot; lock (_events)
uint syncpointId;
if ((eventId >> 28) == 1)
{ {
eventSlot = eventId & 0xFFFF; uint eventSlot;
syncpointId = (eventId >> 16) & 0xFFF; uint syncpointId;
}
else
{
eventSlot = eventId & 0xFF;
syncpointId = eventId >> 4;
}
if (eventSlot >= EventsCount || _events[eventSlot] == null || _events[eventSlot].Fence.Id != syncpointId) if ((eventId >> 28) == 1)
{ {
return null; eventSlot = eventId & 0xFFFF;
} syncpointId = (eventId >> 16) & 0xFFF;
}
else
{
eventSlot = eventId & 0xFF;
syncpointId = eventId >> 4;
}
return _events[eventSlot].Event; if (eventSlot >= EventsCount || _events[eventSlot] == null || _events[eventSlot].Fence.Id != syncpointId)
{
return null;
}
return _events[eventSlot].Event;
}
} }
public override NvInternalResult QueryEvent(out int eventHandle, uint eventId) public override NvInternalResult QueryEvent(out int eventHandle, uint eventId)
@ -226,61 +229,71 @@ namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl
private NvInternalResult EventRegister(ref uint userEventId) private NvInternalResult EventRegister(ref uint userEventId)
{ {
NvInternalResult result = EventUnregister(ref userEventId); lock (_events)
if (result == NvInternalResult.Success)
{ {
_events[userEventId] = new NvHostEvent(_device.System.HostSyncpoint, userEventId, _device.System); NvInternalResult result = EventUnregister(ref userEventId);
if (result == NvInternalResult.Success)
{
_events[userEventId] = new NvHostEvent(_device.System.HostSyncpoint, userEventId, _device.System);
}
return result;
} }
return result;
} }
private NvInternalResult EventUnregister(ref uint userEventId) private NvInternalResult EventUnregister(ref uint userEventId)
{ {
if (userEventId >= EventsCount) lock (_events)
{ {
return NvInternalResult.InvalidInput; if (userEventId >= EventsCount)
{
return NvInternalResult.InvalidInput;
}
NvHostEvent hostEvent = _events[userEventId];
if (hostEvent == null)
{
return NvInternalResult.Success;
}
if (hostEvent.State == NvHostEventState.Available ||
hostEvent.State == NvHostEventState.Cancelled ||
hostEvent.State == NvHostEventState.Signaled)
{
_events[userEventId].Dispose();
_events[userEventId] = null;
return NvInternalResult.Success;
}
return NvInternalResult.Busy;
} }
NvHostEvent hostEvent = _events[userEventId];
if (hostEvent == null)
{
return NvInternalResult.Success;
}
if (hostEvent.State == NvHostEventState.Available ||
hostEvent.State == NvHostEventState.Cancelled ||
hostEvent.State == NvHostEventState.Signaled)
{
_events[userEventId].Dispose();
_events[userEventId] = null;
return NvInternalResult.Success;
}
return NvInternalResult.Busy;
} }
private NvInternalResult EventKill(ref ulong eventMask) private NvInternalResult EventKill(ref ulong eventMask)
{ {
NvInternalResult result = NvInternalResult.Success; lock (_events)
for (uint eventId = 0; eventId < EventsCount; eventId++)
{ {
if ((eventMask & (1UL << (int)eventId)) != 0) NvInternalResult result = NvInternalResult.Success;
{
NvInternalResult tmp = EventUnregister(ref eventId);
if (tmp != NvInternalResult.Success) for (uint eventId = 0; eventId < EventsCount; eventId++)
{
if ((eventMask & (1UL << (int)eventId)) != 0)
{ {
result = tmp; NvInternalResult tmp = EventUnregister(ref eventId);
if (tmp != NvInternalResult.Success)
{
result = tmp;
}
} }
} }
}
return result; return result;
}
} }
private NvInternalResult EventSignal(ref uint userEventId) private NvInternalResult EventSignal(ref uint userEventId)
@ -292,27 +305,34 @@ namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl
return NvInternalResult.InvalidInput; return NvInternalResult.InvalidInput;
} }
NvHostEvent hostEvent = _events[eventId]; lock (_events)
if (hostEvent == null)
{ {
return NvInternalResult.InvalidInput; NvHostEvent hostEvent = _events[eventId];
if (hostEvent == null)
{
return NvInternalResult.InvalidInput;
}
lock (hostEvent.Lock)
{
NvHostEventState oldState = hostEvent.State;
if (oldState == NvHostEventState.Waiting)
{
hostEvent.State = NvHostEventState.Cancelling;
hostEvent.Cancel(_device.Gpu);
}
hostEvent.State = NvHostEventState.Cancelled;
_device.System.HostSyncpoint.UpdateMin(hostEvent.Fence.Id);
return NvInternalResult.Success;
}
} }
NvHostEventState oldState = hostEvent.State;
if (oldState == NvHostEventState.Waiting)
{
hostEvent.State = NvHostEventState.Cancelling;
hostEvent.Cancel(_device.Gpu);
}
hostEvent.State = NvHostEventState.Cancelled;
_device.System.HostSyncpoint.UpdateMin(hostEvent.Fence.Id);
return NvInternalResult.Success;
} }
private NvInternalResult SyncptReadMinOrMax(ref NvFence arguments, bool max) private NvInternalResult SyncptReadMinOrMax(ref NvFence arguments, bool max)
@ -379,67 +399,81 @@ namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl
uint eventIndex; uint eventIndex;
if (isWaitEventAsyncCmd) lock (_events)
{ {
eventIndex = value; if (isWaitEventAsyncCmd)
if (eventIndex >= EventsCount)
{ {
return NvInternalResult.InvalidInput; eventIndex = value;
}
hostEvent = _events[eventIndex]; if (eventIndex >= EventsCount)
}
else
{
hostEvent = GetFreeEvent(fence.Id, out eventIndex);
}
if (hostEvent != null &&
(hostEvent.State == NvHostEventState.Available ||
hostEvent.State == NvHostEventState.Signaled ||
hostEvent.State == NvHostEventState.Cancelled))
{
bool timedOut = hostEvent.Wait(_device.Gpu, fence);
if (timedOut)
{
if (isWaitEventCmd)
{ {
value = ((fence.Id & 0xfff) << 16) | 0x10000000; return NvInternalResult.InvalidInput;
}
else
{
value = fence.Id << 4;
} }
value |= eventIndex; hostEvent = _events[eventIndex];
result = NvInternalResult.TryAgain;
} }
else else
{ {
value = fence.Value; hostEvent = GetFreeEventLocked(fence.Id, out eventIndex);
return NvInternalResult.Success;
} }
}
else
{
Logger.PrintError(LogClass.ServiceNv, $"Invalid Event at index {eventIndex} (isWaitEventAsyncCmd: {isWaitEventAsyncCmd}, isWaitEventCmd: {isWaitEventCmd})");
if (hostEvent != null) if (hostEvent != null)
{ {
Logger.PrintError(LogClass.ServiceNv, hostEvent.DumpState(_device.Gpu)); lock (hostEvent.Lock)
} {
if (hostEvent.State == NvHostEventState.Available ||
hostEvent.State == NvHostEventState.Signaled ||
hostEvent.State == NvHostEventState.Cancelled)
{
bool timedOut = hostEvent.Wait(_device.Gpu, fence);
result = NvInternalResult.InvalidInput; if (timedOut)
{
if (isWaitEventCmd)
{
value = ((fence.Id & 0xfff) << 16) | 0x10000000;
}
else
{
value = fence.Id << 4;
}
value |= eventIndex;
result = NvInternalResult.TryAgain;
}
else
{
value = fence.Value;
return NvInternalResult.Success;
}
}
else
{
Logger.PrintError(LogClass.ServiceNv, $"Invalid Event at index {eventIndex} (isWaitEventAsyncCmd: {isWaitEventAsyncCmd}, isWaitEventCmd: {isWaitEventCmd})");
if (hostEvent != null)
{
Logger.PrintError(LogClass.ServiceNv, hostEvent.DumpState(_device.Gpu));
}
result = NvInternalResult.InvalidInput;
}
}
}
else
{
Logger.PrintError(LogClass.ServiceNv, $"Invalid Event at index {eventIndex} (isWaitEventAsyncCmd: {isWaitEventAsyncCmd}, isWaitEventCmd: {isWaitEventCmd})");
result = NvInternalResult.InvalidInput;
}
} }
return result; return result;
} }
public NvHostEvent GetFreeEvent(uint id, out uint eventIndex) private NvHostEvent GetFreeEventLocked(uint id, out uint eventIndex)
{ {
eventIndex = EventsCount; eventIndex = EventsCount;
@ -490,38 +524,44 @@ namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl
{ {
Logger.PrintWarning(LogClass.ServiceNv, "Closing channel"); Logger.PrintWarning(LogClass.ServiceNv, "Closing channel");
// If the device file need to be closed, cancel all user events and dispose events. lock (_events)
for (int i = 0; i < _events.Length; i++)
{ {
NvHostEvent evnt = _events[i]; // If the device file need to be closed, cancel all user events and dispose events.
for (int i = 0; i < _events.Length; i++)
if (evnt != null)
{ {
if (evnt.State == NvHostEventState.Waiting) NvHostEvent evnt = _events[i];
{
evnt.State = NvHostEventState.Cancelling;
evnt.Cancel(_device.Gpu); if (evnt != null)
}
else if (evnt.State == NvHostEventState.Signaling)
{ {
// Wait at max 9ms if the guest app is trying to signal the event while closing it.. lock (evnt.Lock)
int retryCount = 0;
do
{ {
if (retryCount++ > 9) if (evnt.State == NvHostEventState.Waiting)
{ {
break; evnt.State = NvHostEventState.Cancelling;
evnt.Cancel(_device.Gpu);
}
else if (evnt.State == NvHostEventState.Signaling)
{
// Wait at max 9ms if the guest app is trying to signal the event while closing it..
int retryCount = 0;
do
{
if (retryCount++ > 9)
{
break;
}
// TODO: This should be handled by the kernel (reschedule the current thread ect), waiting for Kernel decoupling work.
Thread.Sleep(1);
} while (evnt.State != NvHostEventState.Signaled);
} }
// TODO: This should be handled by the kernel (reschedule the current thread ect), waiting for Kernel decoupling work. evnt.Dispose();
Thread.Sleep(1);
} while (evnt.State != NvHostEventState.Signaled); _events[i] = null;
}
} }
evnt.Dispose();
_events[i] = null;
} }
} }
} }

View file

@ -21,6 +21,8 @@ namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl
private NvFence _previousFailingFence; private NvFence _previousFailingFence;
private uint _failingCount; private uint _failingCount;
public object Lock = new object();
/// <summary> /// <summary>
/// Max failing count until waiting on CPU. /// Max failing count until waiting on CPU.
/// FIXME: This seems enough for most of the cases, reduce if needed. /// FIXME: This seems enough for most of the cases, reduce if needed.
@ -49,83 +51,88 @@ namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl
_failingCount = 0; _failingCount = 0;
} }
public void Reset()
{
Fence.Id = 0;
Fence.Value = 0;
State = NvHostEventState.Available;
}
private void Signal() private void Signal()
{ {
NvHostEventState oldState = State; lock (Lock)
State = NvHostEventState.Signaling;
if (oldState == NvHostEventState.Waiting)
{ {
Event.WritableEvent.Signal(); NvHostEventState oldState = State;
}
State = NvHostEventState.Signaled; State = NvHostEventState.Signaling;
if (oldState == NvHostEventState.Waiting)
{
Event.WritableEvent.Signal();
}
State = NvHostEventState.Signaled;
}
} }
private void GpuSignaled() private void GpuSignaled()
{ {
ResetFailingState(); lock (Lock)
{
ResetFailingState();
Signal(); Signal();
}
} }
public void Cancel(GpuContext gpuContext) public void Cancel(GpuContext gpuContext)
{ {
if (_waiterInformation != null) lock (Lock)
{ {
gpuContext.Synchronization.UnregisterCallback(Fence.Id, _waiterInformation); if (_waiterInformation != null)
if (_previousFailingFence.Id == Fence.Id && _previousFailingFence.Value == Fence.Value)
{ {
_failingCount++; gpuContext.Synchronization.UnregisterCallback(Fence.Id, _waiterInformation);
}
else
{
_failingCount = 1;
_previousFailingFence = Fence; if (_previousFailingFence.Id == Fence.Id && _previousFailingFence.Value == Fence.Value)
{
_failingCount++;
}
else
{
_failingCount = 1;
_previousFailingFence = Fence;
}
Signal();
} }
Signal(); Event.WritableEvent.Clear();
} }
Event.WritableEvent.Clear();
} }
public bool Wait(GpuContext gpuContext, NvFence fence) public bool Wait(GpuContext gpuContext, NvFence fence)
{ {
Fence = fence; lock (Lock)
State = NvHostEventState.Waiting;
// NOTE: nvservices code should always wait on the GPU side.
// If we do this, we may get an abort or undefined behaviour when the GPU processing thread is blocked for a long period (for example, during shader compilation).
// The reason for this is that the NVN code will try to wait until giving up.
// This is done by trying to wait and signal multiple times until aborting after you are past the timeout.
// As such, if it fails too many time, we enforce a wait on the CPU side indefinitely.
// This allows to keep GPU and CPU in sync when we are slow.
if (_failingCount == FailingCountMax)
{ {
Logger.PrintWarning(LogClass.ServiceNv, "GPU processing thread is too slow, waiting on CPU..."); Fence = fence;
State = NvHostEventState.Waiting;
bool timedOut = Fence.Wait(gpuContext, Timeout.InfiniteTimeSpan); // NOTE: nvservices code should always wait on the GPU side.
// If we do this, we may get an abort or undefined behaviour when the GPU processing thread is blocked for a long period (for example, during shader compilation).
// The reason for this is that the NVN code will try to wait until giving up.
// This is done by trying to wait and signal multiple times until aborting after you are past the timeout.
// As such, if it fails too many time, we enforce a wait on the CPU side indefinitely.
// This allows to keep GPU and CPU in sync when we are slow.
if (_failingCount == FailingCountMax)
{
Logger.PrintWarning(LogClass.ServiceNv, "GPU processing thread is too slow, waiting on CPU...");
GpuSignaled(); bool timedOut = Fence.Wait(gpuContext, Timeout.InfiniteTimeSpan);
return timedOut; GpuSignaled();
}
else
{
_waiterInformation = gpuContext.Synchronization.RegisterCallbackOnSyncpoint(Fence.Id, Fence.Value, GpuSignaled);
return true; return timedOut;
}
else
{
_waiterInformation = gpuContext.Synchronization.RegisterCallbackOnSyncpoint(Fence.Id, Fence.Value, GpuSignaled);
return true;
}
} }
} }

View file

@ -26,8 +26,6 @@ namespace Ryujinx.HLE.HOS.Services.SurfaceFlinger
private Stopwatch _chrono; private Stopwatch _chrono;
private AndroidFence _vblankFence;
private long _ticks; private long _ticks;
private long _ticksPerFrame; private long _ticksPerFrame;
@ -49,7 +47,6 @@ namespace Ryujinx.HLE.HOS.Services.SurfaceFlinger
{ {
public Layer Layer; public Layer Layer;
public BufferItem Item; public BufferItem Item;
public AndroidFence Fence;
} }
public SurfaceFlinger(Switch device) public SurfaceFlinger(Switch device)
@ -69,13 +66,6 @@ namespace Ryujinx.HLE.HOS.Services.SurfaceFlinger
UpdateSwapInterval(1); UpdateSwapInterval(1);
_vblankFence = AndroidFence.NoFence;
_vblankFence.AddFence(new NvFence
{
Id = NvHostSyncpt.VBlank0SyncpointId,
Value = 0
});
_composerThread.Start(); _composerThread.Start();
} }
@ -222,8 +212,6 @@ namespace Ryujinx.HLE.HOS.Services.SurfaceFlinger
{ {
lock (Lock) lock (Lock)
{ {
_vblankFence.NvFences[0].Increment(_device.Gpu);
// TODO: support multilayers (& multidisplay ?) // TODO: support multilayers (& multidisplay ?)
if (_layers.Count == 0) if (_layers.Count == 0)
{ {
@ -298,14 +286,10 @@ namespace Ryujinx.HLE.HOS.Services.SurfaceFlinger
flipX, flipX,
flipY); flipY);
// Enforce that dequeueBuffer wait for the next vblank
_vblankFence.NvFences[0].Value++;
TextureCallbackInformation textureCallbackInformation = new TextureCallbackInformation TextureCallbackInformation textureCallbackInformation = new TextureCallbackInformation
{ {
Layer = layer, Layer = layer,
Item = item, Item = item,
Fence = _vblankFence
}; };
_device.Gpu.Window.EnqueueFrameThreadSafe( _device.Gpu.Window.EnqueueFrameThreadSafe(
@ -330,7 +314,9 @@ namespace Ryujinx.HLE.HOS.Services.SurfaceFlinger
private void ReleaseBuffer(TextureCallbackInformation information) private void ReleaseBuffer(TextureCallbackInformation information)
{ {
information.Layer.Consumer.ReleaseBuffer(information.Item, ref information.Fence); AndroidFence fence = AndroidFence.NoFence;
information.Layer.Consumer.ReleaseBuffer(information.Item, ref fence);
} }
private void AcquireBuffer(GpuContext ignored, object obj) private void AcquireBuffer(GpuContext ignored, object obj)