SmartMultiRegionHandle.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 QueryModified(Action<ulong, ulong> modifiedAction)
  38. {
  39. if (!Dirty)
  40. {
  41. return;
  42. }
  43. Dirty = false;
  44. QueryModified(_address, _size, modifiedAction);
  45. }
  46. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  47. private ulong HandlesToBytes(int handles)
  48. {
  49. return (ulong)handles * _granularity;
  50. }
  51. private void SplitHandle(int handleIndex, int splitIndex)
  52. {
  53. RegionHandle handle = _handles[handleIndex];
  54. ulong address = _address + HandlesToBytes(handleIndex);
  55. ulong size = HandlesToBytes(splitIndex - handleIndex);
  56. // First, the target handle must be removed. Its data can still be used to determine the new handles.
  57. handle.Dispose();
  58. RegionHandle splitLow = _tracking.BeginTracking(address, size);
  59. splitLow.Parent = this;
  60. _handles[handleIndex] = splitLow;
  61. RegionHandle splitHigh = _tracking.BeginTracking(address + size, handle.Size - size);
  62. splitHigh.Parent = this;
  63. _handles[splitIndex] = splitHigh;
  64. }
  65. private void CreateHandle(int startHandle, int lastHandle)
  66. {
  67. ulong startAddress = _address + HandlesToBytes(startHandle);
  68. // Scan for the first handle before us. If it's overlapping us, it must be split.
  69. for (int i = startHandle - 1; i >= 0; i--)
  70. {
  71. RegionHandle handle = _handles[i];
  72. if (handle != null)
  73. {
  74. if (handle.EndAddress > startAddress)
  75. {
  76. SplitHandle(i, startHandle);
  77. return; // The remainer of this handle should be filled in later on.
  78. }
  79. break;
  80. }
  81. }
  82. // Scan for handles after us. We should create a handle that goes up to this handle's start point, if present.
  83. for (int i = startHandle + 1; i <= lastHandle; i++)
  84. {
  85. RegionHandle handle = _handles[i];
  86. if (handle != null)
  87. {
  88. // Fill up to the found handle.
  89. handle = _tracking.BeginTracking(startAddress, HandlesToBytes(i - startHandle));
  90. handle.Parent = this;
  91. _handles[startHandle] = handle;
  92. return;
  93. }
  94. }
  95. // Can fill the whole range.
  96. _handles[startHandle] = _tracking.BeginTracking(startAddress, HandlesToBytes(1 + lastHandle - startHandle));
  97. _handles[startHandle].Parent = this;
  98. }
  99. public void QueryModified(ulong address, ulong size, Action<ulong, ulong> modifiedAction)
  100. {
  101. int startHandle = (int)((address - _address) / _granularity);
  102. int lastHandle = (int)((address + (size - 1) - _address) / _granularity);
  103. ulong rgStart = _address + (ulong)startHandle * _granularity;
  104. ulong rgSize = 0;
  105. ulong endAddress = _address + ((ulong)lastHandle + 1) * _granularity;
  106. int i = startHandle;
  107. while (i <= lastHandle)
  108. {
  109. RegionHandle handle = _handles[i];
  110. if (handle == null)
  111. {
  112. // Missing handle. A new handle must be created.
  113. CreateHandle(i, lastHandle);
  114. handle = _handles[i];
  115. }
  116. if (handle.EndAddress > endAddress)
  117. {
  118. // End address of handle is beyond the end of the search. Force a split.
  119. SplitHandle(i, lastHandle + 1);
  120. handle = _handles[i];
  121. }
  122. if (handle.Dirty)
  123. {
  124. rgSize += handle.Size;
  125. handle.Reprotect();
  126. }
  127. else
  128. {
  129. // Submit the region scanned so far as dirty
  130. if (rgSize != 0)
  131. {
  132. modifiedAction(rgStart, rgSize);
  133. rgSize = 0;
  134. }
  135. rgStart = handle.EndAddress;
  136. }
  137. i += (int)(handle.Size / _granularity);
  138. }
  139. if (rgSize != 0)
  140. {
  141. modifiedAction(rgStart, rgSize);
  142. }
  143. }
  144. public void QueryModified(ulong address, ulong size, Action<ulong, ulong> modifiedAction, int sequenceNumber)
  145. {
  146. int startHandle = (int)((address - _address) / _granularity);
  147. int lastHandle = (int)((address + (size - 1) - _address) / _granularity);
  148. ulong rgStart = _address + (ulong)startHandle * _granularity;
  149. ulong rgSize = 0;
  150. ulong endAddress = _address + ((ulong)lastHandle + 1) * _granularity;
  151. int i = startHandle;
  152. while (i <= lastHandle)
  153. {
  154. RegionHandle handle = _handles[i];
  155. if (handle == null)
  156. {
  157. // Missing handle. A new handle must be created.
  158. CreateHandle(i, lastHandle);
  159. handle = _handles[i];
  160. }
  161. if (handle.EndAddress > endAddress)
  162. {
  163. // End address of handle is beyond the end of the search. Force a split.
  164. SplitHandle(i, lastHandle + 1);
  165. handle = _handles[i];
  166. }
  167. if (handle.Dirty && sequenceNumber != handle.SequenceNumber)
  168. {
  169. rgSize += handle.Size;
  170. handle.Reprotect();
  171. }
  172. else
  173. {
  174. // Submit the region scanned so far as dirty
  175. if (rgSize != 0)
  176. {
  177. modifiedAction(rgStart, rgSize);
  178. rgSize = 0;
  179. }
  180. rgStart = handle.EndAddress;
  181. }
  182. handle.SequenceNumber = sequenceNumber;
  183. i += (int)(handle.Size / _granularity);
  184. }
  185. if (rgSize != 0)
  186. {
  187. modifiedAction(rgStart, rgSize);
  188. }
  189. }
  190. public void Dispose()
  191. {
  192. foreach (var handle in _handles)
  193. {
  194. handle?.Dispose();
  195. }
  196. }
  197. }
  198. }