CounterCache.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using Ryujinx.Graphics.GAL;
  2. using System.Collections.Generic;
  3. namespace Ryujinx.Graphics.Gpu.Memory
  4. {
  5. /// <summary>
  6. /// Represents the GPU counter cache.
  7. /// </summary>
  8. class CounterCache
  9. {
  10. private readonly struct CounterEntry
  11. {
  12. public ulong Address { get; }
  13. public ICounterEvent Event { get; }
  14. public CounterEntry(ulong address, ICounterEvent evt)
  15. {
  16. Address = address;
  17. Event = evt;
  18. }
  19. }
  20. private readonly List<CounterEntry> _items;
  21. /// <summary>
  22. /// Creates a new instance of the GPU counter cache.
  23. /// </summary>
  24. public CounterCache()
  25. {
  26. _items = [];
  27. }
  28. /// <summary>
  29. /// Adds a new counter to the counter cache, or updates a existing one.
  30. /// </summary>
  31. /// <param name="gpuVa">GPU virtual address where the counter will be written in memory</param>
  32. /// <param name="evt">The new counter</param>
  33. public void AddOrUpdate(ulong gpuVa, ICounterEvent evt)
  34. {
  35. int index = BinarySearch(gpuVa);
  36. CounterEntry entry = new(gpuVa, evt);
  37. if (index < 0)
  38. {
  39. _items.Insert(~index, entry);
  40. }
  41. else
  42. {
  43. _items[index] = entry;
  44. }
  45. }
  46. /// <summary>
  47. /// Handles removal of counters written to a memory region being unmapped.
  48. /// </summary>
  49. /// <param name="sender">Sender object</param>
  50. /// <param name="e">Event arguments</param>
  51. public void MemoryUnmappedHandler(object sender, UnmapEventArgs e) => RemoveRange(e.Address, e.Size);
  52. private void RemoveRange(ulong gpuVa, ulong size)
  53. {
  54. int index = BinarySearch(gpuVa + size - 1);
  55. if (index < 0)
  56. {
  57. index = ~index;
  58. }
  59. if (index >= _items.Count || !InRange(gpuVa, size, _items[index].Address))
  60. {
  61. return;
  62. }
  63. int count = 1;
  64. while (index > 0 && InRange(gpuVa, size, _items[index - 1].Address))
  65. {
  66. index--;
  67. count++;
  68. }
  69. // Notify the removed counter events that their result should no longer be written out.
  70. for (int i = 0; i < count; i++)
  71. {
  72. ICounterEvent evt = _items[index + i].Event;
  73. if (evt != null)
  74. {
  75. evt.Invalid = true;
  76. }
  77. }
  78. _items.RemoveRange(index, count);
  79. }
  80. /// <summary>
  81. /// Checks whenever an address falls inside a given range.
  82. /// </summary>
  83. /// <param name="startVa">Range start address</param>
  84. /// <param name="size">Range size</param>
  85. /// <param name="gpuVa">Address being checked</param>
  86. /// <returns>True if the address falls inside the range, false otherwise</returns>
  87. private static bool InRange(ulong startVa, ulong size, ulong gpuVa)
  88. {
  89. return gpuVa >= startVa && gpuVa < startVa + size;
  90. }
  91. /// <summary>
  92. /// Check if any counter value was written to the specified GPU virtual memory address.
  93. /// </summary>
  94. /// <param name="gpuVa">GPU virtual address</param>
  95. /// <returns>True if any counter value was written on the specified address, false otherwise</returns>
  96. public bool Contains(ulong gpuVa)
  97. {
  98. return BinarySearch(gpuVa) >= 0;
  99. }
  100. /// <summary>
  101. /// Flush any counter value written to the specified GPU virtual memory address.
  102. /// </summary>
  103. /// <param name="gpuVa">GPU virtual address</param>
  104. /// <returns>True if any counter value was written on the specified address, false otherwise</returns>
  105. public bool FindAndFlush(ulong gpuVa)
  106. {
  107. int index = BinarySearch(gpuVa);
  108. if (index > 0)
  109. {
  110. _items[index].Event?.Flush();
  111. return true;
  112. }
  113. else
  114. {
  115. return false;
  116. }
  117. }
  118. /// <summary>
  119. /// Find any counter event that would write to the specified GPU virtual memory address.
  120. /// </summary>
  121. /// <param name="gpuVa">GPU virtual address</param>
  122. /// <returns>The counter event, or null if not present</returns>
  123. public ICounterEvent FindEvent(ulong gpuVa)
  124. {
  125. int index = BinarySearch(gpuVa);
  126. if (index > 0)
  127. {
  128. return _items[index].Event;
  129. }
  130. else
  131. {
  132. return null;
  133. }
  134. }
  135. /// <summary>
  136. /// Performs binary search of an address on the list.
  137. /// </summary>
  138. /// <param name="address">Address to search</param>
  139. /// <returns>Index of the item, or complement of the index of the nearest item with lower value</returns>
  140. private int BinarySearch(ulong address)
  141. {
  142. int left = 0;
  143. int right = _items.Count - 1;
  144. while (left <= right)
  145. {
  146. int range = right - left;
  147. int middle = left + (range >> 1);
  148. CounterEntry item = _items[middle];
  149. if (item.Address == address)
  150. {
  151. return middle;
  152. }
  153. if (address < item.Address)
  154. {
  155. right = middle - 1;
  156. }
  157. else
  158. {
  159. left = middle + 1;
  160. }
  161. }
  162. return ~left;
  163. }
  164. }
  165. }