PerformanceManager.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // Copyright (c) 2019-2020 Ryujinx
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. //
  17. using Ryujinx.Audio.Renderer.Common;
  18. using Ryujinx.Audio.Renderer.Parameter;
  19. using System;
  20. using System.Runtime.CompilerServices;
  21. namespace Ryujinx.Audio.Renderer.Server.Performance
  22. {
  23. public abstract class PerformanceManager
  24. {
  25. /// <summary>
  26. /// Get the required size for a single performance frame.
  27. /// </summary>
  28. /// <param name="parameter">The audio renderer configuration.</param>
  29. /// <param name="behaviourContext">The behaviour context.</param>
  30. /// <returns>The required size for a single performance frame.</returns>
  31. public static ulong GetRequiredBufferSizeForPerformanceMetricsPerFrame(ref AudioRendererConfiguration parameter, ref BehaviourContext behaviourContext)
  32. {
  33. uint version = behaviourContext.GetPerformanceMetricsDataFormat();
  34. if (version == 2)
  35. {
  36. return (ulong)PerformanceManagerGeneric<PerformanceFrameHeaderVersion2,
  37. PerformanceEntryVersion2,
  38. PerformanceDetailVersion2>.GetRequiredBufferSizeForPerformanceMetricsPerFrame(ref parameter);
  39. }
  40. else if (version == 1)
  41. {
  42. return (ulong)PerformanceManagerGeneric<PerformanceFrameHeaderVersion1,
  43. PerformanceEntryVersion1,
  44. PerformanceDetailVersion1>.GetRequiredBufferSizeForPerformanceMetricsPerFrame(ref parameter);
  45. }
  46. throw new NotImplementedException($"Unknown Performance metrics data format version {version}");
  47. }
  48. /// <summary>
  49. /// Copy the performance frame history to the supplied user buffer and returns the size copied.
  50. /// </summary>
  51. /// <param name="performanceOutput">The supplied user buffer to store the performance frame into.</param>
  52. /// <returns>The size copied to the supplied buffer.</returns>
  53. public abstract uint CopyHistories(Span<byte> performanceOutput);
  54. /// <summary>
  55. /// Set the target node id to profile.
  56. /// </summary>
  57. /// <param name="target">The target node id to profile.</param>
  58. public abstract void SetTargetNodeId(int target);
  59. /// <summary>
  60. /// Check if the given target node id is profiled.
  61. /// </summary>
  62. /// <param name="target">The target node id to check.</param>
  63. /// <returns>Return true, if the given target node id is profiled.</returns>
  64. public abstract bool IsTargetNodeId(int target);
  65. /// <summary>
  66. /// Get the next buffer to store a performance entry.
  67. /// </summary>
  68. /// <param name="performanceEntry">The output <see cref="PerformanceEntryAddresses"/>.</param>
  69. /// <param name="entryType">The <see cref="PerformanceEntryType"/> info.</param>
  70. /// <param name="nodeId">The node id of the entry.</param>
  71. /// <returns>Return true, if a valid <see cref="PerformanceEntryAddresses"/> was returned.</returns>
  72. public abstract bool GetNextEntry(out PerformanceEntryAddresses performanceEntry, PerformanceEntryType entryType, int nodeId);
  73. /// <summary>
  74. /// Get the next buffer to store a performance detailed entry.
  75. /// </summary>
  76. /// <param name="performanceEntry">The output <see cref="PerformanceEntryAddresses"/>.</param>
  77. /// <param name="detailType">The <see cref="PerformanceDetailType"/> info.</param>
  78. /// <param name="entryType">The <see cref="PerformanceEntryType"/> info.</param>
  79. /// <param name="nodeId">The node id of the entry.</param>
  80. /// <returns>Return true, if a valid <see cref="PerformanceEntryAddresses"/> was returned.</returns>
  81. public abstract bool GetNextEntry(out PerformanceEntryAddresses performanceEntry, PerformanceDetailType detailType, PerformanceEntryType entryType, int nodeId);
  82. /// <summary>
  83. /// Finalize the current performance frame.
  84. /// </summary>
  85. /// <param name="dspRunningBehind">Indicate if the DSP is running behind.</param>
  86. /// <param name="voiceDropCount">The count of voices that were dropped.</param>
  87. /// <param name="startRenderingTicks">The start ticks of the audio rendering.</param>
  88. public abstract void TapFrame(bool dspRunningBehind, uint voiceDropCount, ulong startRenderingTicks);
  89. /// <summary>
  90. /// Create a new <see cref="PerformanceManager"/>.
  91. /// </summary>
  92. /// <param name="performanceBuffer">The backing memory available for use by the manager.</param>
  93. /// <param name="parameter">The audio renderer configuration.</param>
  94. /// <param name="behaviourContext">The behaviour context;</param>
  95. /// <returns>A new <see cref="PerformanceManager"/>.</returns>
  96. public static PerformanceManager Create(Memory<byte> performanceBuffer, ref AudioRendererConfiguration parameter, BehaviourContext behaviourContext)
  97. {
  98. uint version = behaviourContext.GetPerformanceMetricsDataFormat();
  99. switch (version)
  100. {
  101. case 1:
  102. return new PerformanceManagerGeneric<PerformanceFrameHeaderVersion1, PerformanceEntryVersion1, PerformanceDetailVersion1>(performanceBuffer,
  103. ref parameter);
  104. case 2:
  105. return new PerformanceManagerGeneric<PerformanceFrameHeaderVersion2, PerformanceEntryVersion2, PerformanceDetailVersion2>(performanceBuffer,
  106. ref parameter);
  107. default:
  108. throw new NotImplementedException($"Unknown Performance metrics data format version {version}");
  109. }
  110. }
  111. }
  112. }