NvGpuVmmCache.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using ChocolArm64.Memory;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace Ryujinx.HLE.Gpu.Memory
  5. {
  6. class NvGpuVmmCache
  7. {
  8. private const int MaxCpCount = 10000;
  9. private const int MaxCpTimeDelta = 60000;
  10. private class CachedPage
  11. {
  12. private struct Range
  13. {
  14. public long Start;
  15. public long End;
  16. public Range(long Start, long End)
  17. {
  18. this.Start = Start;
  19. this.End = End;
  20. }
  21. }
  22. private List<Range>[] Regions;
  23. public LinkedListNode<long> Node { get; set; }
  24. public int Timestamp { get; private set; }
  25. public CachedPage()
  26. {
  27. Regions = new List<Range>[(int)NvGpuBufferType.Count];
  28. for (int Index = 0; Index < Regions.Length; Index++)
  29. {
  30. Regions[Index] = new List<Range>();
  31. }
  32. }
  33. public bool AddRange(long Start, long End, NvGpuBufferType BufferType)
  34. {
  35. List<Range> BtRegions = Regions[(int)BufferType];
  36. for (int Index = 0; Index < BtRegions.Count; Index++)
  37. {
  38. Range Rg = BtRegions[Index];
  39. if (Start >= Rg.Start && End <= Rg.End)
  40. {
  41. return false;
  42. }
  43. if (Start <= Rg.End && Rg.Start <= End)
  44. {
  45. long MinStart = Math.Min(Rg.Start, Start);
  46. long MaxEnd = Math.Max(Rg.End, End);
  47. BtRegions[Index] = new Range(MinStart, MaxEnd);
  48. Timestamp = Environment.TickCount;
  49. return true;
  50. }
  51. }
  52. BtRegions.Add(new Range(Start, End));
  53. Timestamp = Environment.TickCount;
  54. return true;
  55. }
  56. public int GetTotalCount()
  57. {
  58. int Count = 0;
  59. for (int Index = 0; Index < Regions.Length; Index++)
  60. {
  61. Count += Regions[Index].Count;
  62. }
  63. return Count;
  64. }
  65. }
  66. private Dictionary<long, CachedPage> Cache;
  67. private LinkedList<long> SortedCache;
  68. private int CpCount;
  69. public NvGpuVmmCache()
  70. {
  71. Cache = new Dictionary<long, CachedPage>();
  72. SortedCache = new LinkedList<long>();
  73. }
  74. public bool IsRegionModified(AMemory Memory, NvGpuBufferType BufferType, long PA, long Size)
  75. {
  76. bool[] Modified = Memory.IsRegionModified(PA, Size);
  77. if (Modified == null)
  78. {
  79. return true;
  80. }
  81. ClearCachedPagesIfNeeded();
  82. long PageSize = Memory.GetHostPageSize();
  83. long Mask = PageSize - 1;
  84. long PAEnd = PA + Size;
  85. bool RegMod = false;
  86. int Index = 0;
  87. while (PA < PAEnd)
  88. {
  89. long Key = PA & ~Mask;
  90. long PAPgEnd = Math.Min((PA + PageSize) & ~Mask, PAEnd);
  91. bool IsCached = Cache.TryGetValue(Key, out CachedPage Cp);
  92. if (IsCached)
  93. {
  94. CpCount -= Cp.GetTotalCount();
  95. SortedCache.Remove(Cp.Node);
  96. }
  97. else
  98. {
  99. Cp = new CachedPage();
  100. Cache.Add(Key, Cp);
  101. }
  102. if (Modified[Index++] && IsCached)
  103. {
  104. Cp = new CachedPage();
  105. Cache[Key] = Cp;
  106. }
  107. Cp.Node = SortedCache.AddLast(Key);
  108. RegMod |= Cp.AddRange(PA, PAPgEnd, BufferType);
  109. CpCount += Cp.GetTotalCount();
  110. PA = PAPgEnd;
  111. }
  112. return RegMod;
  113. }
  114. private void ClearCachedPagesIfNeeded()
  115. {
  116. if (CpCount <= MaxCpCount)
  117. {
  118. return;
  119. }
  120. int Timestamp = Environment.TickCount;
  121. int TimeDelta;
  122. do
  123. {
  124. if (!TryPopOldestCachedPageKey(Timestamp, out long Key))
  125. {
  126. break;
  127. }
  128. CachedPage Cp = Cache[Key];
  129. Cache.Remove(Key);
  130. CpCount -= Cp.GetTotalCount();
  131. TimeDelta = RingDelta(Cp.Timestamp, Timestamp);
  132. }
  133. while (CpCount > (MaxCpCount >> 1) || (uint)TimeDelta > (uint)MaxCpTimeDelta);
  134. }
  135. private bool TryPopOldestCachedPageKey(int Timestamp, out long Key)
  136. {
  137. LinkedListNode<long> Node = SortedCache.First;
  138. if (Node == null)
  139. {
  140. Key = 0;
  141. return false;
  142. }
  143. SortedCache.Remove(Node);
  144. Key = Node.Value;
  145. return true;
  146. }
  147. private int RingDelta(int Old, int New)
  148. {
  149. if ((uint)New < (uint)Old)
  150. {
  151. return New + (~Old + 1);
  152. }
  153. else
  154. {
  155. return New - Old;
  156. }
  157. }
  158. }
  159. }