-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathNativeMethods.cs
131 lines (107 loc) · 5.29 KB
/
NativeMethods.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using System.Runtime.InteropServices;
namespace OpenTelemetry.AutoInstrumentation;
internal static class NativeMethods
{
private static readonly bool IsWindows = string.Equals(FrameworkDescription.Instance.OSPlatform, "Windows", StringComparison.OrdinalIgnoreCase);
public static void AddInstrumentations(string id, NativeCallTargetDefinition[] methodArrays)
{
if (methodArrays is null || methodArrays.Length == 0)
{
return;
}
if (IsWindows)
{
Windows.AddInstrumentations(id, methodArrays, methodArrays.Length);
}
else
{
NonWindows.AddInstrumentations(id, methodArrays, methodArrays.Length);
}
}
public static void AddDerivedInstrumentations(string id, NativeCallTargetDefinition[] methodArrays)
{
if (methodArrays is null || methodArrays.Length == 0)
{
return;
}
if (IsWindows)
{
Windows.AddDerivedInstrumentations(id, methodArrays, methodArrays.Length);
}
else
{
NonWindows.AddDerivedInstrumentations(id, methodArrays, methodArrays.Length);
}
}
#if NET
public static void ConfigureNativeContinuousProfiler(bool threadSamplingEnabled, uint threadSamplingInterval, bool allocationSamplingEnabled, uint maxMemorySamplesPerMinute)
{
if (IsWindows)
{
Windows.ConfigureContinuousProfiler(threadSamplingEnabled, threadSamplingInterval, allocationSamplingEnabled, maxMemorySamplesPerMinute);
}
else
{
NonWindows.ConfigureContinuousProfiler(threadSamplingEnabled, threadSamplingInterval, allocationSamplingEnabled, maxMemorySamplesPerMinute);
}
}
public static int ContinuousProfilerReadThreadSamples(int len, byte[] buf)
{
return IsWindows ? Windows.ContinuousProfilerReadThreadSamples(len, buf) : NonWindows.ContinuousProfilerReadThreadSamples(len, buf);
}
public static int ContinuousProfilerReadAllocationSamples(int len, byte[] buf)
{
return IsWindows ? Windows.ContinuousProfilerReadAllocationSamples(len, buf) : NonWindows.ContinuousProfilerReadAllocationSamples(len, buf);
}
public static void ContinuousProfilerSetNativeContext(ulong traceIdHigh, ulong traceIdLow, ulong spanId)
{
if (IsWindows)
{
Windows.ContinuousProfilerSetNativeContext(traceIdHigh, traceIdLow, spanId);
}
else
{
NonWindows.ContinuousProfilerSetNativeContext(traceIdHigh, traceIdLow, spanId);
}
}
#endif
// the "dll" extension is required on .NET Framework
// and optional on .NET Core
private static class Windows
{
[DllImport("OpenTelemetry.AutoInstrumentation.Native.dll")]
public static extern void AddInstrumentations([MarshalAs(UnmanagedType.LPWStr)] string id, [In] NativeCallTargetDefinition[] methodArrays, int size);
[DllImport("OpenTelemetry.AutoInstrumentation.Native.dll")]
public static extern void AddDerivedInstrumentations([MarshalAs(UnmanagedType.LPWStr)] string id, [In] NativeCallTargetDefinition[] methodArrays, int size);
[DllImport("OpenTelemetry.AutoInstrumentation.Native.dll")]
public static extern void ConfigureContinuousProfiler(bool threadSamplingEnabled, uint threadSamplingInterval, bool allocationSamplingEnabled, uint maxMemorySamplesPerMinute);
#if NET
[DllImport("OpenTelemetry.AutoInstrumentation.Native.dll")]
public static extern int ContinuousProfilerReadThreadSamples(int len, byte[] buf);
[DllImport("OpenTelemetry.AutoInstrumentation.Native.dll")]
public static extern int ContinuousProfilerReadAllocationSamples(int len, byte[] buf);
[DllImport("OpenTelemetry.AutoInstrumentation.Native.dll")]
public static extern void ContinuousProfilerSetNativeContext(ulong traceIdHigh, ulong traceIdLow, ulong spanId);
#endif
}
// assume .NET Core if not running on Windows
private static class NonWindows
{
[DllImport("OpenTelemetry.AutoInstrumentation.Native")]
public static extern void AddInstrumentations([MarshalAs(UnmanagedType.LPWStr)] string id, [In] NativeCallTargetDefinition[] methodArrays, int size);
[DllImport("OpenTelemetry.AutoInstrumentation.Native")]
public static extern void AddDerivedInstrumentations([MarshalAs(UnmanagedType.LPWStr)] string id, [In] NativeCallTargetDefinition[] methodArrays, int size);
[DllImport("OpenTelemetry.AutoInstrumentation.Native")]
public static extern void ConfigureContinuousProfiler(bool threadSamplingEnabled, uint threadSamplingInterval, bool allocationSamplingEnabled, uint maxMemorySamplesPerMinute);
#if NET
[DllImport("OpenTelemetry.AutoInstrumentation.Native")]
public static extern int ContinuousProfilerReadThreadSamples(int len, byte[] buf);
[DllImport("OpenTelemetry.AutoInstrumentation.Native")]
public static extern int ContinuousProfilerReadAllocationSamples(int len, byte[] buf);
[DllImport("OpenTelemetry.AutoInstrumentation.Native")]
public static extern void ContinuousProfilerSetNativeContext(ulong traceIdHigh, ulong traceIdLow, ulong spanId);
#endif
}
}