NvGpuVmmCache.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. using ChocolArm64.Memory;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace Ryujinx.Graphics.Memory
  5. {
  6. class NvGpuVmmCache
  7. {
  8. private const long RamSize = 4L * 1024 * 1024 * 1024;
  9. private const int MaxCpCount = 10000;
  10. private const int MaxCpTimeDelta = 60000;
  11. private class CachedPage
  12. {
  13. private struct Range
  14. {
  15. public long Start;
  16. public long End;
  17. public Range(long Start, long End)
  18. {
  19. this.Start = Start;
  20. this.End = End;
  21. }
  22. }
  23. private List<Range>[] Regions;
  24. private HashSet<long> ResidencyKeys;
  25. public LinkedListNode<long> Node { get; set; }
  26. public int Timestamp { get; private set; }
  27. public CachedPage()
  28. {
  29. Regions = new List<Range>[(int)NvGpuBufferType.Count];
  30. for (int Index = 0; Index < Regions.Length; Index++)
  31. {
  32. Regions[Index] = new List<Range>();
  33. }
  34. ResidencyKeys = new HashSet<long>();
  35. }
  36. public void AddResidency(long Key)
  37. {
  38. ResidencyKeys.Add(Key);
  39. }
  40. public void RemoveResidency(HashSet<long>[] Residency, long PageSize)
  41. {
  42. for (int i = 0; i < (int)NvGpuBufferType.Count; i++)
  43. {
  44. foreach (Range Region in Regions[i])
  45. {
  46. foreach (long Key in ResidencyKeys)
  47. {
  48. Residency[Region.Start / PageSize].Remove(Key);
  49. }
  50. }
  51. }
  52. }
  53. public bool AddRange(long Start, long End, NvGpuBufferType BufferType)
  54. {
  55. List<Range> BtRegions = Regions[(int)BufferType];
  56. for (int Index = 0; Index < BtRegions.Count; Index++)
  57. {
  58. Range Rg = BtRegions[Index];
  59. if (Start >= Rg.Start && End <= Rg.End)
  60. {
  61. return false;
  62. }
  63. if (Start <= Rg.End && Rg.Start <= End)
  64. {
  65. long MinStart = Math.Min(Rg.Start, Start);
  66. long MaxEnd = Math.Max(Rg.End, End);
  67. BtRegions[Index] = new Range(MinStart, MaxEnd);
  68. Timestamp = Environment.TickCount;
  69. return true;
  70. }
  71. }
  72. BtRegions.Add(new Range(Start, End));
  73. Timestamp = Environment.TickCount;
  74. return true;
  75. }
  76. public int GetTotalCount()
  77. {
  78. int Count = 0;
  79. for (int Index = 0; Index < Regions.Length; Index++)
  80. {
  81. Count += Regions[Index].Count;
  82. }
  83. return Count;
  84. }
  85. }
  86. private Dictionary<long, CachedPage> Cache;
  87. private LinkedList<long> SortedCache;
  88. private HashSet<long>[] Residency;
  89. private long ResidencyPageSize;
  90. private int CpCount;
  91. public NvGpuVmmCache()
  92. {
  93. Cache = new Dictionary<long, CachedPage>();
  94. SortedCache = new LinkedList<long>();
  95. }
  96. public bool IsRegionModified(AMemory Memory, NvGpuBufferType BufferType, long PA, long Size)
  97. {
  98. (bool[] Modified, long ModifiedCount) = Memory.IsRegionModified(PA, Size);
  99. PA = Memory.GetPhysicalAddress(PA);
  100. ClearCachedPagesIfNeeded();
  101. long PageSize = AMemory.PageSize;
  102. EnsureResidencyInitialized(PageSize);
  103. bool HasResidents = AddResidency(PA, Size);
  104. if (!HasResidents && ModifiedCount == 0)
  105. {
  106. return false;
  107. }
  108. long Mask = PageSize - 1;
  109. long ResidencyKey = PA;
  110. long PAEnd = PA + Size;
  111. bool RegMod = false;
  112. int Index = 0;
  113. while (PA < PAEnd)
  114. {
  115. long Key = PA & ~AMemory.PageMask;
  116. long PAPgEnd = Math.Min((PA + AMemory.PageSize) & ~AMemory.PageMask, PAEnd);
  117. bool IsCached = Cache.TryGetValue(Key, out CachedPage Cp);
  118. if (IsCached)
  119. {
  120. CpCount -= Cp.GetTotalCount();
  121. SortedCache.Remove(Cp.Node);
  122. }
  123. else
  124. {
  125. Cp = new CachedPage();
  126. Cache.Add(Key, Cp);
  127. }
  128. if (Modified[Index++] && IsCached)
  129. {
  130. Cp = new CachedPage();
  131. Cache[Key] = Cp;
  132. }
  133. Cp.AddResidency(ResidencyKey);
  134. Cp.Node = SortedCache.AddLast(Key);
  135. RegMod |= Cp.AddRange(PA, PAPgEnd, BufferType);
  136. CpCount += Cp.GetTotalCount();
  137. PA = PAPgEnd;
  138. }
  139. return RegMod;
  140. }
  141. private bool AddResidency(long PA, long Size)
  142. {
  143. long PageSize = ResidencyPageSize;
  144. long Mask = PageSize - 1;
  145. long Key = PA;
  146. bool ResidentFound = false;
  147. for (long Cursor = PA & ~Mask; Cursor < ((PA + Size + PageSize - 1) & ~Mask); Cursor += PageSize)
  148. {
  149. long PageIndex = Cursor / PageSize;
  150. Residency[PageIndex].Add(Key);
  151. if (Residency[PageIndex].Count > 1)
  152. {
  153. ResidentFound = true;
  154. }
  155. }
  156. return ResidentFound;
  157. }
  158. private void EnsureResidencyInitialized(long PageSize)
  159. {
  160. if (Residency == null)
  161. {
  162. Residency = new HashSet<long>[RamSize / PageSize];
  163. for (int i = 0; i < Residency.Length; i++)
  164. {
  165. Residency[i] = new HashSet<long>();
  166. }
  167. ResidencyPageSize = PageSize;
  168. }
  169. else
  170. {
  171. if (ResidencyPageSize != PageSize)
  172. {
  173. throw new InvalidOperationException("Tried to change residency page size");
  174. }
  175. }
  176. }
  177. private void ClearCachedPagesIfNeeded()
  178. {
  179. if (CpCount <= MaxCpCount)
  180. {
  181. return;
  182. }
  183. int Timestamp = Environment.TickCount;
  184. int TimeDelta;
  185. do
  186. {
  187. if (!TryPopOldestCachedPageKey(Timestamp, out long Key))
  188. {
  189. break;
  190. }
  191. CachedPage Cp = Cache[Key];
  192. Cp.RemoveResidency(Residency, ResidencyPageSize);
  193. Cache.Remove(Key);
  194. CpCount -= Cp.GetTotalCount();
  195. TimeDelta = RingDelta(Cp.Timestamp, Timestamp);
  196. }
  197. while (CpCount > (MaxCpCount >> 1) || (uint)TimeDelta > (uint)MaxCpTimeDelta);
  198. }
  199. private bool TryPopOldestCachedPageKey(int Timestamp, out long Key)
  200. {
  201. LinkedListNode<long> Node = SortedCache.First;
  202. if (Node == null)
  203. {
  204. Key = 0;
  205. return false;
  206. }
  207. SortedCache.Remove(Node);
  208. Key = Node.Value;
  209. return true;
  210. }
  211. private int RingDelta(int Old, int New)
  212. {
  213. if ((uint)New < (uint)Old)
  214. {
  215. return New + (~Old + 1);
  216. }
  217. else
  218. {
  219. return New - Old;
  220. }
  221. }
  222. }
  223. }