MultiRegionHandle.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Numerics;
  4. using System.Runtime.CompilerServices;
  5. using System.Threading;
  6. namespace Ryujinx.Memory.Tracking
  7. {
  8. /// <summary>
  9. /// A region handle that tracks a large region using many smaller handles, to provide
  10. /// granular tracking that can be used to track partial updates. Backed by a bitmap
  11. /// to improve performance when scanning large regions.
  12. /// </summary>
  13. public class MultiRegionHandle : IMultiRegionHandle
  14. {
  15. /// <summary>
  16. /// A list of region handles for each granularity sized chunk of the whole region.
  17. /// </summary>
  18. private readonly RegionHandle[] _handles;
  19. private readonly ulong Address;
  20. private readonly ulong Granularity;
  21. private readonly ulong Size;
  22. private ConcurrentBitmap _dirtyBitmap;
  23. private int _sequenceNumber;
  24. private BitMap _sequenceNumberBitmap;
  25. private BitMap _dirtyCheckedBitmap;
  26. private int _uncheckedHandles;
  27. public bool Dirty { get; private set; } = true;
  28. internal MultiRegionHandle(
  29. MemoryTracking tracking,
  30. ulong address,
  31. ulong size,
  32. IEnumerable<IRegionHandle> handles,
  33. ulong granularity,
  34. int id)
  35. {
  36. _handles = new RegionHandle[(size + granularity - 1) / granularity];
  37. Granularity = granularity;
  38. _dirtyBitmap = new ConcurrentBitmap(_handles.Length, true);
  39. _sequenceNumberBitmap = new BitMap(_handles.Length);
  40. _dirtyCheckedBitmap = new BitMap(_handles.Length);
  41. int i = 0;
  42. if (handles != null)
  43. {
  44. // Inherit from the handles we were given. Any gaps must be filled with new handles,
  45. // and old handles larger than our granularity must copy their state onto new granular handles and dispose.
  46. // It is assumed that the provided handles do not overlap, in order, are on page boundaries,
  47. // and don't extend past the requested range.
  48. foreach (RegionHandle handle in handles)
  49. {
  50. int startIndex = (int)((handle.RealAddress - address) / granularity);
  51. // Fill any gap left before this handle.
  52. while (i < startIndex)
  53. {
  54. RegionHandle fillHandle = tracking.BeginTrackingBitmap(address + (ulong)i * granularity, granularity, _dirtyBitmap, i, id);
  55. fillHandle.Parent = this;
  56. _handles[i++] = fillHandle;
  57. }
  58. lock (tracking.TrackingLock)
  59. {
  60. if (handle is RegionHandle bitHandle && handle.Size == granularity)
  61. {
  62. handle.Parent = this;
  63. bitHandle.ReplaceBitmap(_dirtyBitmap, i);
  64. _handles[i++] = bitHandle;
  65. }
  66. else
  67. {
  68. int endIndex = (int)((handle.RealEndAddress - address) / granularity);
  69. while (i < endIndex)
  70. {
  71. RegionHandle splitHandle = tracking.BeginTrackingBitmap(address + (ulong)i * granularity, granularity, _dirtyBitmap, i, id);
  72. splitHandle.Parent = this;
  73. splitHandle.Reprotect(handle.Dirty);
  74. RegionSignal signal = handle.PreAction;
  75. if (signal != null)
  76. {
  77. splitHandle.RegisterAction(signal);
  78. }
  79. _handles[i++] = splitHandle;
  80. }
  81. handle.Dispose();
  82. }
  83. }
  84. }
  85. }
  86. // Fill any remaining space with new handles.
  87. while (i < _handles.Length)
  88. {
  89. RegionHandle handle = tracking.BeginTrackingBitmap(address + (ulong)i * granularity, granularity, _dirtyBitmap, i, id);
  90. handle.Parent = this;
  91. _handles[i++] = handle;
  92. }
  93. _uncheckedHandles = _handles.Length;
  94. Address = address;
  95. Size = size;
  96. }
  97. public void SignalWrite()
  98. {
  99. Dirty = true;
  100. }
  101. public IEnumerable<RegionHandle> GetHandles()
  102. {
  103. return _handles;
  104. }
  105. public void ForceDirty(ulong address, ulong size)
  106. {
  107. Dirty = true;
  108. int startHandle = (int)((address - Address) / Granularity);
  109. int lastHandle = (int)((address + (size - 1) - Address) / Granularity);
  110. for (int i = startHandle; i <= lastHandle; i++)
  111. {
  112. if (_sequenceNumberBitmap.Clear(i))
  113. {
  114. _uncheckedHandles++;
  115. }
  116. _handles[i].ForceDirty();
  117. }
  118. }
  119. public void QueryModified(Action<ulong, ulong> modifiedAction)
  120. {
  121. if (!Dirty)
  122. {
  123. return;
  124. }
  125. Dirty = false;
  126. QueryModified(Address, Size, modifiedAction);
  127. }
  128. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  129. private void ParseDirtyBits(long dirtyBits, ref int baseBit, ref int prevHandle, ref ulong rgStart, ref ulong rgSize, Action<ulong, ulong> modifiedAction)
  130. {
  131. while (dirtyBits != 0)
  132. {
  133. int bit = BitOperations.TrailingZeroCount(dirtyBits);
  134. dirtyBits &= ~(1L << bit);
  135. int handleIndex = baseBit + bit;
  136. RegionHandle handle = _handles[handleIndex];
  137. if (handleIndex != prevHandle + 1)
  138. {
  139. // Submit handles scanned until the gap as dirty
  140. if (rgSize != 0)
  141. {
  142. modifiedAction(rgStart, rgSize);
  143. rgSize = 0;
  144. }
  145. rgStart = handle.RealAddress;
  146. }
  147. if (handle.Dirty)
  148. {
  149. rgSize += handle.RealSize;
  150. handle.Reprotect();
  151. }
  152. prevHandle = handleIndex;
  153. }
  154. baseBit += ConcurrentBitmap.IntSize;
  155. }
  156. public void QueryModified(ulong address, ulong size, Action<ulong, ulong> modifiedAction)
  157. {
  158. int startHandle = (int)((address - Address) / Granularity);
  159. int lastHandle = (int)((address + (size - 1) - Address) / Granularity);
  160. ulong rgStart = Address + (ulong)startHandle * Granularity;
  161. if (startHandle == lastHandle)
  162. {
  163. RegionHandle handle = _handles[startHandle];
  164. if (handle.Dirty)
  165. {
  166. handle.Reprotect();
  167. modifiedAction(rgStart, handle.RealSize);
  168. }
  169. return;
  170. }
  171. ulong rgSize = 0;
  172. long[] masks = _dirtyBitmap.Masks;
  173. int startIndex = startHandle >> ConcurrentBitmap.IntShift;
  174. int startBit = startHandle & ConcurrentBitmap.IntMask;
  175. long startMask = -1L << startBit;
  176. int endIndex = lastHandle >> ConcurrentBitmap.IntShift;
  177. int endBit = lastHandle & ConcurrentBitmap.IntMask;
  178. long endMask = (long)(ulong.MaxValue >> (ConcurrentBitmap.IntMask - endBit));
  179. long startValue = Volatile.Read(ref masks[startIndex]);
  180. int baseBit = startIndex << ConcurrentBitmap.IntShift;
  181. int prevHandle = startHandle - 1;
  182. if (startIndex == endIndex)
  183. {
  184. ParseDirtyBits(startValue & startMask & endMask, ref baseBit, ref prevHandle, ref rgStart, ref rgSize, modifiedAction);
  185. }
  186. else
  187. {
  188. ParseDirtyBits(startValue & startMask, ref baseBit, ref prevHandle, ref rgStart, ref rgSize, modifiedAction);
  189. for (int i = startIndex + 1; i < endIndex; i++)
  190. {
  191. ParseDirtyBits(Volatile.Read(ref masks[i]), ref baseBit, ref prevHandle, ref rgStart, ref rgSize, modifiedAction);
  192. }
  193. long endValue = Volatile.Read(ref masks[endIndex]);
  194. ParseDirtyBits(endValue & endMask, ref baseBit, ref prevHandle, ref rgStart, ref rgSize, modifiedAction);
  195. }
  196. if (rgSize != 0)
  197. {
  198. modifiedAction(rgStart, rgSize);
  199. }
  200. }
  201. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  202. private void ParseDirtyBits(long dirtyBits, long mask, int index, long[] seqMasks, long[] checkMasks, ref int baseBit, ref int prevHandle, ref ulong rgStart, ref ulong rgSize, Action<ulong, ulong> modifiedAction)
  203. {
  204. long seqMask = mask & ~seqMasks[index];
  205. long checkMask = (~dirtyBits) & seqMask;
  206. dirtyBits &= seqMask;
  207. while (dirtyBits != 0)
  208. {
  209. int bit = BitOperations.TrailingZeroCount(dirtyBits);
  210. long bitValue = 1L << bit;
  211. dirtyBits &= ~bitValue;
  212. int handleIndex = baseBit + bit;
  213. RegionHandle handle = _handles[handleIndex];
  214. if (handleIndex != prevHandle + 1)
  215. {
  216. // Submit handles scanned until the gap as dirty
  217. if (rgSize != 0)
  218. {
  219. modifiedAction(rgStart, rgSize);
  220. rgSize = 0;
  221. }
  222. rgStart = handle.RealAddress;
  223. }
  224. rgSize += handle.RealSize;
  225. handle.Reprotect(false, (checkMasks[index] & bitValue) == 0);
  226. checkMasks[index] &= ~bitValue;
  227. prevHandle = handleIndex;
  228. }
  229. checkMasks[index] |= checkMask;
  230. seqMasks[index] |= mask;
  231. _uncheckedHandles -= BitOperations.PopCount((ulong)seqMask);
  232. baseBit += ConcurrentBitmap.IntSize;
  233. }
  234. public void QueryModified(ulong address, ulong size, Action<ulong, ulong> modifiedAction, int sequenceNumber)
  235. {
  236. int startHandle = (int)((address - Address) / Granularity);
  237. int lastHandle = (int)((address + (size - 1) - Address) / Granularity);
  238. ulong rgStart = Address + (ulong)startHandle * Granularity;
  239. if (sequenceNumber != _sequenceNumber)
  240. {
  241. if (_uncheckedHandles != _handles.Length)
  242. {
  243. _sequenceNumberBitmap.Clear();
  244. _uncheckedHandles = _handles.Length;
  245. }
  246. _sequenceNumber = sequenceNumber;
  247. }
  248. if (startHandle == lastHandle)
  249. {
  250. var handle = _handles[startHandle];
  251. if (_sequenceNumberBitmap.Set(startHandle))
  252. {
  253. _uncheckedHandles--;
  254. if (handle.DirtyOrVolatile())
  255. {
  256. handle.Reprotect();
  257. modifiedAction(rgStart, handle.RealSize);
  258. }
  259. }
  260. return;
  261. }
  262. if (_uncheckedHandles == 0)
  263. {
  264. return;
  265. }
  266. ulong rgSize = 0;
  267. long[] seqMasks = _sequenceNumberBitmap.Masks;
  268. long[] checkedMasks = _dirtyCheckedBitmap.Masks;
  269. long[] masks = _dirtyBitmap.Masks;
  270. int startIndex = startHandle >> ConcurrentBitmap.IntShift;
  271. int startBit = startHandle & ConcurrentBitmap.IntMask;
  272. long startMask = -1L << startBit;
  273. int endIndex = lastHandle >> ConcurrentBitmap.IntShift;
  274. int endBit = lastHandle & ConcurrentBitmap.IntMask;
  275. long endMask = (long)(ulong.MaxValue >> (ConcurrentBitmap.IntMask - endBit));
  276. long startValue = Volatile.Read(ref masks[startIndex]);
  277. int baseBit = startIndex << ConcurrentBitmap.IntShift;
  278. int prevHandle = startHandle - 1;
  279. if (startIndex == endIndex)
  280. {
  281. ParseDirtyBits(startValue, startMask & endMask, startIndex, seqMasks, checkedMasks, ref baseBit, ref prevHandle, ref rgStart, ref rgSize, modifiedAction);
  282. }
  283. else
  284. {
  285. ParseDirtyBits(startValue, startMask, startIndex, seqMasks, checkedMasks, ref baseBit, ref prevHandle, ref rgStart, ref rgSize, modifiedAction);
  286. for (int i = startIndex + 1; i < endIndex; i++)
  287. {
  288. ParseDirtyBits(Volatile.Read(ref masks[i]), -1L, i, seqMasks, checkedMasks, ref baseBit, ref prevHandle, ref rgStart, ref rgSize, modifiedAction);
  289. }
  290. long endValue = Volatile.Read(ref masks[endIndex]);
  291. ParseDirtyBits(endValue, endMask, endIndex, seqMasks, checkedMasks, ref baseBit, ref prevHandle, ref rgStart, ref rgSize, modifiedAction);
  292. }
  293. if (rgSize != 0)
  294. {
  295. modifiedAction(rgStart, rgSize);
  296. }
  297. }
  298. public void RegisterAction(ulong address, ulong size, RegionSignal action)
  299. {
  300. int startHandle = (int)((address - Address) / Granularity);
  301. int lastHandle = (int)((address + (size - 1) - Address) / Granularity);
  302. for (int i = startHandle; i <= lastHandle; i++)
  303. {
  304. _handles[i].RegisterAction(action);
  305. }
  306. }
  307. public void RegisterPreciseAction(ulong address, ulong size, PreciseRegionSignal action)
  308. {
  309. int startHandle = (int)((address - Address) / Granularity);
  310. int lastHandle = (int)((address + (size - 1) - Address) / Granularity);
  311. for (int i = startHandle; i <= lastHandle; i++)
  312. {
  313. _handles[i].RegisterPreciseAction(action);
  314. }
  315. }
  316. public void Dispose()
  317. {
  318. foreach (var handle in _handles)
  319. {
  320. handle.Dispose();
  321. }
  322. }
  323. }
  324. }