NvGpuVmmCache.cs 7.7 KB

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