Ingen beskrivning
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProfilerCounter.cs 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Diagnostics;
  3. using System.Diagnostics.Contracts;
  4. using System.Runtime.CompilerServices;
  5. using System.Runtime.InteropServices;
  6. using Unity.Collections.LowLevel.Unsafe;
  7. using Unity.Profiling.LowLevel;
  8. using Unity.Profiling.LowLevel.Unsafe;
  9. namespace Unity.Profiling
  10. {
  11. /// <summary>
  12. /// Reports a value of an integer or floating point type to the Unity Profiler.
  13. /// </summary>
  14. /// <typeparam name="T">int, uint, long, ulong, float or double type.</typeparam>
  15. #if ENABLE_PROFILER
  16. [StructLayout(LayoutKind.Sequential)]
  17. #else
  18. [StructLayout(LayoutKind.Sequential, Size = 0)]
  19. #endif
  20. public readonly struct ProfilerCounter<T> where T : unmanaged
  21. {
  22. #if ENABLE_PROFILER
  23. [NativeDisableUnsafePtrRestriction]
  24. [NonSerialized]
  25. readonly IntPtr m_Ptr;
  26. [NonSerialized]
  27. readonly byte m_Type;
  28. #endif
  29. /// <summary>
  30. /// Constructs a **ProfilerCounter** that is reported to the Unity Profiler whenever you call Sample().
  31. /// </summary>
  32. /// <param name="category">Profiler category.</param>
  33. /// <param name="name">Name of ProfilerCounter.</param>
  34. /// <param name="dataUnit">Value unit type.</param>
  35. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  36. public ProfilerCounter(ProfilerCategory category, string name, ProfilerMarkerDataUnit dataUnit)
  37. {
  38. #if ENABLE_PROFILER
  39. m_Type = ProfilerUtility.GetProfilerMarkerDataType<T>();
  40. m_Ptr = ProfilerUnsafeUtility.CreateMarker(name, category, MarkerFlags.Counter, 1);
  41. ProfilerUnsafeUtility.SetMarkerMetadata(m_Ptr, 0, null, m_Type, (byte)dataUnit);
  42. #endif
  43. }
  44. /// <summary>
  45. /// Sends the value to Unity Profiler immediately.
  46. /// </summary>
  47. /// <remarks>Does nothing in Release Players.</remarks>
  48. /// <param name="value">The value to send to the profiler.</param>
  49. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  50. [Conditional("ENABLE_PROFILER")]
  51. [Pure]
  52. public void Sample(T value)
  53. {
  54. #if ENABLE_PROFILER
  55. unsafe
  56. {
  57. var data = new ProfilerMarkerData
  58. {
  59. Type = m_Type,
  60. Size = (uint)UnsafeUtility.SizeOf<T>(),
  61. Ptr = UnsafeUtility.AddressOf(ref value)
  62. };
  63. ProfilerUnsafeUtility.SingleSampleWithMetadata(m_Ptr, 1, &data);
  64. }
  65. #endif
  66. }
  67. }
  68. }