|
| 1 | +// <copyright file="ContextTracker.cs" company="Datadog"> |
| 2 | +// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. |
| 4 | +// </copyright> |
| 5 | + |
| 6 | +using System.Runtime.CompilerServices; |
| 7 | +using System.Runtime.InteropServices; |
| 8 | + |
| 9 | +namespace Pyroscope |
| 10 | +{ |
| 11 | + internal class ContextTracker |
| 12 | + { |
| 13 | + |
| 14 | + |
| 15 | + private readonly ProfilerStatus _status; |
| 16 | + |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// _traceContextPtr points to a structure with this layout |
| 20 | + /// The structure is as follow: |
| 21 | + /// offset size(bytes) |
| 22 | + /// |--------------------| |
| 23 | + /// 0 8 | WriteGuard | // 8-byte for the alignment |
| 24 | + /// |--------------------| |
| 25 | + /// 8 8 | Local Root Span Id | |
| 26 | + /// |--------------------| |
| 27 | + /// 16 8 | Span Id | |
| 28 | + /// |--------------------| |
| 29 | + /// This allows us to inform the profiler sampling thread when we are writing or not the data |
| 30 | + /// and avoid torn read/write (Using memory barriers). |
| 31 | + /// We take advantage of this layout in SpanContext.Write |
| 32 | + /// </summary> |
| 33 | + private readonly ThreadLocal<IntPtr> _traceContextPtr; |
| 34 | + |
| 35 | + |
| 36 | + public ContextTracker(ProfilerStatus status) |
| 37 | + { |
| 38 | + _status = status; |
| 39 | + _traceContextPtr = new ThreadLocal<IntPtr>(); |
| 40 | + } |
| 41 | + |
| 42 | + public bool IsEnabled |
| 43 | + { |
| 44 | + get |
| 45 | + { |
| 46 | + return _status.IsProfilerReady; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public void Set(ulong localRootSpanId, ulong spanId) |
| 51 | + { |
| 52 | + WriteToNative(new SpanContext(localRootSpanId, spanId)); |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + public void Reset() |
| 57 | + { |
| 58 | + WriteToNative(SpanContext.Zero); |
| 59 | + } |
| 60 | + |
| 61 | + private void EnsureIsInitialized() |
| 62 | + { |
| 63 | + if (_traceContextPtr.IsValueCreated) |
| 64 | + { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + try |
| 69 | + { |
| 70 | + _traceContextPtr.Value = NativeInterop.GetTraceContextNativePointer(); |
| 71 | + } |
| 72 | + catch (Exception) |
| 73 | + { |
| 74 | + _traceContextPtr.Value = IntPtr.Zero; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private void WriteToNative(in SpanContext ctx) |
| 79 | + { |
| 80 | + if (!IsEnabled) |
| 81 | + { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + EnsureIsInitialized(); |
| 86 | + |
| 87 | + var ctxPtr = _traceContextPtr.Value; |
| 88 | + |
| 89 | + if (ctxPtr == IntPtr.Zero) |
| 90 | + { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + try |
| 95 | + { |
| 96 | + ctx.Write(ctxPtr); |
| 97 | + } |
| 98 | + catch (Exception) |
| 99 | + { |
| 100 | + |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // See the description and the layout depicted above |
| 105 | + private readonly struct SpanContext |
| 106 | + { |
| 107 | + public static readonly SpanContext Zero = new(0, 0); |
| 108 | + |
| 109 | + public readonly ulong LocalRootSpanId; |
| 110 | + public readonly ulong SpanId; |
| 111 | + |
| 112 | + public SpanContext(ulong localRootSpanId, ulong spanId) |
| 113 | + { |
| 114 | + LocalRootSpanId = localRootSpanId; |
| 115 | + SpanId = spanId; |
| 116 | + } |
| 117 | + |
| 118 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 119 | + public void Write(IntPtr ptr) |
| 120 | + { |
| 121 | + // Set the WriteGuard |
| 122 | + Marshal.WriteInt64(ptr, 1); |
| 123 | + Thread.MemoryBarrier(); |
| 124 | + |
| 125 | + // Using WriteInt64 to write 2 long values is ~8x faster than using Marshal.StructureToPtr |
| 126 | + // For the offset, we follow the layout depicted above |
| 127 | + Marshal.WriteInt64(ptr + 8, (long)LocalRootSpanId); |
| 128 | + // Marshal.WriteInt64(ptr + 16, (long)SpanId); |
| 129 | + |
| 130 | + // Reset the WriteGuard |
| 131 | + Thread.MemoryBarrier(); |
| 132 | + Marshal.WriteInt64(ptr, 0); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments