SmartMultiRegionHandle.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. namespace Ryujinx.Memory.Tracking
  4. {
  5. /// <summary>
  6. /// A MultiRegionHandle that attempts to segment a region's handles into the regions requested
  7. /// to avoid iterating over granular chunks for canonically large regions.
  8. /// If minimum granularity is to be expected, use MultiRegionHandle.
  9. /// </summary>
  10. public class SmartMultiRegionHandle : IMultiRegionHandle
  11. {
  12. /// <summary>
  13. /// A list of region handles starting at each granularity size increment.
  14. /// </summary>
  15. private readonly RegionHandle[] _handles;
  16. private readonly ulong _address;
  17. private readonly ulong _granularity;
  18. private readonly ulong _size;
  19. private MemoryTracking _tracking;
  20. public bool Dirty { get; private set; } = true;
  21. internal SmartMultiRegionHandle(MemoryTracking tracking, ulong address, ulong size, ulong granularity)
  22. {
  23. // For this multi-region handle, the handle list starts empty.
  24. // As regions are queried, they are added to the _handles array at their start index.
  25. // When a region being added overlaps another, the existing region is split.
  26. // A query can therefore scan multiple regions, though with no overlaps they can cover a large area.
  27. _tracking = tracking;
  28. _handles = new RegionHandle[size / granularity];
  29. _granularity = granularity;
  30. _address = address;
  31. _size = size;
  32. }
  33. public void SignalWrite()
  34. {
  35. Dirty = true;
  36. }
  37. public void ForceDirty(ulong address, ulong size)
  38. {
  39. foreach (var handle in _handles)
  40. {
  41. if (handle != null && handle.OverlapsWith(address, size))
  42. {
  43. handle.ForceDirty();
  44. }
  45. }
  46. }
  47. public void RegisterAction(RegionSignal action)
  48. {
  49. foreach (var handle in _handles)
  50. {
  51. if (handle != null)
  52. {
  53. handle?.RegisterAction((address, size) => action(handle.Address, handle.Size));
  54. }
  55. }
  56. }
  57. public void QueryModified(Action<ulong, ulong> modifiedAction)
  58. {
  59. if (!Dirty)
  60. {
  61. return;
  62. }
  63. Dirty = false;
  64. QueryModified(_address, _size, modifiedAction);
  65. }
  66. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  67. private ulong HandlesToBytes(int handles)
  68. {
  69. return (ulong)handles * _granularity;
  70. }
  71. private void SplitHandle(int handleIndex, int splitIndex)
  72. {
  73. RegionHandle handle = _handles[handleIndex];
  74. ulong address = _address + HandlesToBytes(handleIndex);
  75. ulong size = HandlesToBytes(splitIndex - handleIndex);
  76. // First, the target handle must be removed. Its data can still be used to determine the new handles.
  77. RegionSignal signal = handle.PreAction;
  78. handle.Dispose();
  79. RegionHandle splitLow = _tracking.BeginTracking(address, size);
  80. splitLow.Parent = this;
  81. if (signal != null)
  82. {
  83. splitLow.RegisterAction(signal);
  84. }
  85. _handles[handleIndex] = splitLow;
  86. RegionHandle splitHigh = _tracking.BeginTracking(address + size, handle.Size - size);
  87. splitHigh.Parent = this;
  88. if (signal != null)
  89. {
  90. splitHigh.RegisterAction(signal);
  91. }
  92. _handles[splitIndex] = splitHigh;
  93. }
  94. private void CreateHandle(int startHandle, int lastHandle)
  95. {
  96. ulong startAddress = _address + HandlesToBytes(startHandle);
  97. // Scan for the first handle before us. If it's overlapping us, it must be split.
  98. for (int i = startHandle - 1; i >= 0; i--)
  99. {
  100. RegionHandle handle = _handles[i];
  101. if (handle != null)
  102. {
  103. if (handle.EndAddress > startAddress)
  104. {
  105. SplitHandle(i, startHandle);
  106. return; // The remainer of this handle should be filled in later on.
  107. }
  108. break;
  109. }
  110. }
  111. // Scan for handles after us. We should create a handle that goes up to this handle's start point, if present.
  112. for (int i = startHandle + 1; i <= lastHandle; i++)
  113. {
  114. RegionHandle handle = _handles[i];
  115. if (handle != null)
  116. {
  117. // Fill up to the found handle.
  118. handle = _tracking.BeginTracking(startAddress, HandlesToBytes(i - startHandle));
  119. handle.Parent = this;
  120. _handles[startHandle] = handle;
  121. return;
  122. }
  123. }
  124. // Can fill the whole range.
  125. _handles[startHandle] = _tracking.BeginTracking(startAddress, HandlesToBytes(1 + lastHandle - startHandle));
  126. _handles[startHandle].Parent = this;
  127. }
  128. public void QueryModified(ulong address, ulong size, Action<ulong, ulong> modifiedAction)
  129. {
  130. int startHandle = (int)((address - _address) / _granularity);
  131. int lastHandle = (int)((address + (size - 1) - _address) / _granularity);
  132. ulong rgStart = _address + (ulong)startHandle * _granularity;
  133. ulong rgSize = 0;
  134. ulong endAddress = _address + ((ulong)lastHandle + 1) * _granularity;
  135. int i = startHandle;
  136. while (i <= lastHandle)
  137. {
  138. RegionHandle handle = _handles[i];
  139. if (handle == null)
  140. {
  141. // Missing handle. A new handle must be created.
  142. CreateHandle(i, lastHandle);
  143. handle = _handles[i];
  144. }
  145. if (handle.EndAddress > endAddress)
  146. {
  147. // End address of handle is beyond the end of the search. Force a split.
  148. SplitHandle(i, lastHandle + 1);
  149. handle = _handles[i];
  150. }
  151. if (handle.Dirty)
  152. {
  153. rgSize += handle.Size;
  154. handle.Reprotect();
  155. }
  156. else
  157. {
  158. // Submit the region scanned so far as dirty
  159. if (rgSize != 0)
  160. {
  161. modifiedAction(rgStart, rgSize);
  162. rgSize = 0;
  163. }
  164. rgStart = handle.EndAddress;
  165. }
  166. i += (int)(handle.Size / _granularity);
  167. }
  168. if (rgSize != 0)
  169. {
  170. modifiedAction(rgStart, rgSize);
  171. }
  172. }
  173. public void QueryModified(ulong address, ulong size, Action<ulong, ulong> modifiedAction, int sequenceNumber)
  174. {
  175. int startHandle = (int)((address - _address) / _granularity);
  176. int lastHandle = (int)((address + (size - 1) - _address) / _granularity);
  177. ulong rgStart = _address + (ulong)startHandle * _granularity;
  178. ulong rgSize = 0;
  179. ulong endAddress = _address + ((ulong)lastHandle + 1) * _granularity;
  180. int i = startHandle;
  181. while (i <= lastHandle)
  182. {
  183. RegionHandle handle = _handles[i];
  184. if (handle == null)
  185. {
  186. // Missing handle. A new handle must be created.
  187. CreateHandle(i, lastHandle);
  188. handle = _handles[i];
  189. }
  190. if (handle.EndAddress > endAddress)
  191. {
  192. // End address of handle is beyond the end of the search. Force a split.
  193. SplitHandle(i, lastHandle + 1);
  194. handle = _handles[i];
  195. }
  196. if (handle.Dirty && sequenceNumber != handle.SequenceNumber)
  197. {
  198. rgSize += handle.Size;
  199. handle.Reprotect();
  200. }
  201. else
  202. {
  203. // Submit the region scanned so far as dirty
  204. if (rgSize != 0)
  205. {
  206. modifiedAction(rgStart, rgSize);
  207. rgSize = 0;
  208. }
  209. rgStart = handle.EndAddress;
  210. }
  211. handle.SequenceNumber = sequenceNumber;
  212. i += (int)(handle.Size / _granularity);
  213. }
  214. if (rgSize != 0)
  215. {
  216. modifiedAction(rgStart, rgSize);
  217. }
  218. }
  219. public void Dispose()
  220. {
  221. foreach (var handle in _handles)
  222. {
  223. handle?.Dispose();
  224. }
  225. }
  226. }
  227. }