CounterCache.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System.Collections.Generic;
  2. namespace Ryujinx.Graphics.Gpu.Memory
  3. {
  4. /// <summary>
  5. /// Represents the GPU counter cache.
  6. /// </summary>
  7. class CounterCache
  8. {
  9. private struct CounterEntry
  10. {
  11. public ulong Address { get; }
  12. public CounterEntry(ulong address)
  13. {
  14. Address = address;
  15. }
  16. }
  17. private readonly List<CounterEntry> _items;
  18. /// <summary>
  19. /// Creates a new instance of the GPU counter cache.
  20. /// </summary>
  21. public CounterCache()
  22. {
  23. _items = new List<CounterEntry>();
  24. }
  25. /// <summary>
  26. /// Adds a new counter to the counter cache, or updates a existing one.
  27. /// </summary>
  28. /// <param name="gpuVa">GPU virtual address where the counter will be written in memory</param>
  29. public void AddOrUpdate(ulong gpuVa)
  30. {
  31. int index = BinarySearch(gpuVa);
  32. CounterEntry entry = new CounterEntry(gpuVa);
  33. if (index < 0)
  34. {
  35. _items.Insert(~index, entry);
  36. }
  37. else
  38. {
  39. _items[index] = entry;
  40. }
  41. }
  42. /// <summary>
  43. /// Handles removal of counters written to a memory region being unmapped.
  44. /// </summary>
  45. /// <param name="sender">Sender object</param>
  46. /// <param name="e">Event arguments</param>
  47. public void MemoryUnmappedHandler(object sender, UnmapEventArgs e) => RemoveRange(e.Address, e.Size);
  48. private void RemoveRange(ulong gpuVa, ulong size)
  49. {
  50. int index = BinarySearch(gpuVa + size - 1);
  51. if (index < 0)
  52. {
  53. index = ~index;
  54. }
  55. if (index >= _items.Count || !InRange(gpuVa, size, _items[index].Address))
  56. {
  57. return;
  58. }
  59. int count = 1;
  60. while (index > 0 && InRange(gpuVa, size, _items[index - 1].Address))
  61. {
  62. index--;
  63. count++;
  64. }
  65. _items.RemoveRange(index, count);
  66. }
  67. /// <summary>
  68. /// Checks whenever an address falls inside a given range.
  69. /// </summary>
  70. /// <param name="startVa">Range start address</param>
  71. /// <param name="size">Range size</param>
  72. /// <param name="gpuVa">Address being checked</param>
  73. /// <returns>True if the address falls inside the range, false otherwise</returns>
  74. private static bool InRange(ulong startVa, ulong size, ulong gpuVa)
  75. {
  76. return gpuVa >= startVa && gpuVa < startVa + size;
  77. }
  78. /// <summary>
  79. /// Check if any counter value was written to the specified GPU virtual memory address.
  80. /// </summary>
  81. /// <param name="gpuVa">GPU virtual address</param>
  82. /// <returns>True if any counter value was written on the specified address, false otherwise</returns>
  83. public bool Contains(ulong gpuVa)
  84. {
  85. return BinarySearch(gpuVa) >= 0;
  86. }
  87. /// <summary>
  88. /// Performs binary search of an address on the list.
  89. /// </summary>
  90. /// <param name="address">Address to search</param>
  91. /// <returns>Index of the item, or complement of the index of the nearest item with lower value</returns>
  92. private int BinarySearch(ulong address)
  93. {
  94. int left = 0;
  95. int right = _items.Count - 1;
  96. while (left <= right)
  97. {
  98. int range = right - left;
  99. int middle = left + (range >> 1);
  100. CounterEntry item = _items[middle];
  101. if (item.Address == address)
  102. {
  103. return middle;
  104. }
  105. if (address < item.Address)
  106. {
  107. right = middle - 1;
  108. }
  109. else
  110. {
  111. left = middle + 1;
  112. }
  113. }
  114. return ~left;
  115. }
  116. }
  117. }