0
0
Fork 0

Fix resource limit reserve taking too long (#1391)

This commit is contained in:
gdkchan 2020-07-17 01:18:31 -03:00 committed by GitHub
parent 986be200ba
commit 46f8cef6a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 42 deletions

View file

@ -6,13 +6,13 @@ namespace Ryujinx.HLE.HOS.Kernel.Common
{
class KResourceLimit : KAutoObject
{
private const int Time10SecondsMs = 10000;
private const int DefaultTimeoutMs = 10000; // 10s
private readonly long[] _current;
private readonly long[] _limit;
private readonly long[] _available;
private readonly long[] _current2;
private readonly object _lockObj;
private readonly object _lock;
private readonly LinkedList<KThread> _waitingThreads;
@ -22,9 +22,9 @@ namespace Ryujinx.HLE.HOS.Kernel.Common
{
_current = new long[(int)LimitableResource.Count];
_limit = new long[(int)LimitableResource.Count];
_available = new long[(int)LimitableResource.Count];
_current2 = new long[(int)LimitableResource.Count];
_lockObj = new object();
_lock = new object();
_waitingThreads = new LinkedList<KThread>();
}
@ -36,7 +36,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Common
public bool Reserve(LimitableResource resource, long amount)
{
return Reserve(resource, amount, KTimeManager.ConvertMillisecondsToNanoseconds(Time10SecondsMs));
return Reserve(resource, amount, KTimeManager.ConvertMillisecondsToNanoseconds(DefaultTimeoutMs));
}
public bool Reserve(LimitableResource resource, long amount, long timeout)
@ -49,15 +49,20 @@ namespace Ryujinx.HLE.HOS.Kernel.Common
int index = GetIndex(resource);
lock (_lockObj)
lock (_lock)
{
if (_current2[index] >= _limit[index])
{
return false;
}
long newCurrent = _current[index] + amount;
while (newCurrent > _limit[index] && _available[index] + amount <= _limit[index])
while (newCurrent > _limit[index] && _current2[index] + amount <= _limit[index])
{
_waitingThreadsCount++;
KConditionVariable.Wait(KernelContext, _waitingThreads, _lockObj, timeout);
KConditionVariable.Wait(KernelContext, _waitingThreads, _lock, timeout);
_waitingThreadsCount--;
@ -72,6 +77,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Common
if (newCurrent <= _limit[index])
{
_current[index] = newCurrent;
_current2[index] += amount;
success = true;
}
@ -90,14 +96,14 @@ namespace Ryujinx.HLE.HOS.Kernel.Common
Release(resource, amount, amount);
}
public void Release(LimitableResource resource, long usedAmount, long availableAmount)
public void Release(LimitableResource resource, long amount, long amount2)
{
int index = GetIndex(resource);
lock (_lockObj)
lock (_lock)
{
_current [index] -= usedAmount;
_available[index] -= availableAmount;
_current[index] -= amount;
_current2[index] -= amount2;
if (_waitingThreadsCount > 0)
{
@ -110,7 +116,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Common
{
int index = GetIndex(resource);
lock (_lockObj)
lock (_lock)
{
return _limit[index] - _current[index];
}
@ -120,7 +126,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Common
{
int index = GetIndex(resource);
lock (_lockObj)
lock (_lock)
{
if (_current[index] <= limit)
{

View file

@ -729,22 +729,22 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
KProcess currentProcess = _context.Scheduler.GetCurrentProcess();
lock (_blocks)
{
ulong currentHeapSize = GetHeapSize();
if (currentHeapSize <= size)
{
// Expand.
ulong diffSize = size - currentHeapSize;
ulong sizeDelta = size - currentHeapSize;
lock (_blocks)
{
if (currentProcess.ResourceLimit != null && diffSize != 0 &&
!currentProcess.ResourceLimit.Reserve(LimitableResource.Memory, diffSize))
if (currentProcess.ResourceLimit != null && sizeDelta != 0 &&
!currentProcess.ResourceLimit.Reserve(LimitableResource.Memory, sizeDelta))
{
return KernelResult.ResLimitExceeded;
}
ulong pagesCount = diffSize / PageSize;
ulong pagesCount = sizeDelta / PageSize;
KMemoryRegionManager region = GetMemoryRegionManager();
@ -757,9 +757,9 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
region.FreePages(pageList);
}
if (currentProcess.ResourceLimit != null && diffSize != 0)
if (currentProcess.ResourceLimit != null && sizeDelta != 0)
{
currentProcess.ResourceLimit.Release(LimitableResource.Memory, diffSize);
currentProcess.ResourceLimit.Release(LimitableResource.Memory, sizeDelta);
}
}
@ -777,7 +777,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
return KernelResult.OutOfResource;
}
if (!IsUnmapped(_currentHeapAddr, diffSize))
if (!IsUnmapped(_currentHeapAddr, sizeDelta))
{
CleanUpForError();
@ -800,15 +800,12 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
InsertBlock(_currentHeapAddr, pagesCount, MemoryState.Heap, MemoryPermission.ReadAndWrite);
}
}
else
{
// Shrink.
ulong freeAddr = HeapRegionStart + size;
ulong diffSize = currentHeapSize - size;
ulong sizeDelta = currentHeapSize - size;
lock (_blocks)
{
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
{
return KernelResult.OutOfResource;
@ -816,7 +813,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
if (!CheckRange(
freeAddr,
diffSize,
sizeDelta,
MemoryState.Mask,
MemoryState.Heap,
MemoryPermission.Mask,
@ -831,7 +828,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
return KernelResult.InvalidMemState;
}
ulong pagesCount = diffSize / PageSize;
ulong pagesCount = sizeDelta / PageSize;
KernelResult result = MmuUnmap(freeAddr, pagesCount);
@ -840,13 +837,13 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
return result;
}
currentProcess.ResourceLimit?.Release(LimitableResource.Memory, BitUtils.AlignDown(diffSize, PageSize));
currentProcess.ResourceLimit?.Release(LimitableResource.Memory, sizeDelta);
InsertBlock(freeAddr, pagesCount, MemoryState.Unmapped);
}
}
_currentHeapAddr = HeapRegionStart + size;
}
address = HeapRegionStart;