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

View file

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