CounterCache.cs 5.7 KB

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