-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathProfiler.bindings.cs
433 lines (358 loc) · 17.6 KB
/
Profiler.bindings.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Collections;
using UnityEngine.Bindings;
using UnityEngine.Scripting;
using UnityEngine.Scripting.APIUpdating;
using Unity.Profiling;
namespace UnityEngine.Profiling
{
public enum ProfilerArea
{
CPU,
GPU,
Rendering,
Memory,
Audio,
Video,
Physics,
Physics2D,
NetworkMessages,
NetworkOperations,
UI,
UIDetails,
GlobalIllumination,
VirtualTexturing,
}
[UsedByNativeCode]
[MovedFrom("UnityEngine")]
[NativeHeader("Runtime/Allocator/MemoryManager.h")]
[NativeHeader("Runtime/Profiler/MemoryProfiler.h")]
[NativeHeader("Runtime/Profiler/Profiler.h")]
[NativeHeader("Runtime/Profiler/ScriptBindings/Profiler.bindings.h")]
[NativeHeader("Runtime/ScriptingBackend/ScriptingApi.h")]
[NativeHeader("Runtime/Utilities/MemoryUtilities.h")]
public sealed class Profiler
{
internal const uint invalidProfilerArea = ~0u;
// This class can't be explicitly created
private Profiler() {}
// *undocumented*
public extern static bool supported
{
[NativeMethod(Name = "profiler_is_available", IsFreeFunction = true)]
get;
}
// Sets profiler output file in built players.
[StaticAccessor("ProfilerBindings", StaticAccessorType.DoubleColon)]
public extern static string logFile
{
get;
set;
}
// Sets profiler output file in built players.
public extern static bool enableBinaryLog
{
[NativeMethod(Name = "ProfilerBindings::IsBinaryLogEnabled", IsFreeFunction = true)]
get;
[NativeMethod(Name = "ProfilerBindings::SetBinaryLogEnabled", IsFreeFunction = true)]
set;
}
public extern static int maxUsedMemory
{
[NativeMethod(Name = "ProfilerBindings::GetMaxUsedMemory", IsFreeFunction = true)]
get;
[NativeMethod(Name = "ProfilerBindings::SetMaxUsedMemory", IsFreeFunction = true)]
set;
}
// Enables the Profiler.
public extern static bool enabled
{
[NativeConditional("ENABLE_PROFILER")]
[NativeMethod(Name = "profiler_is_enabled", IsFreeFunction = true, IsThreadSafe = true)]
get;
[NativeMethod(Name = "ProfilerBindings::SetProfilerEnabled", IsFreeFunction = true)]
set;
}
public extern static bool enableAllocationCallstacks
{
[NativeMethod(Name = "ProfilerBindings::IsAllocationCallstackCaptureEnabled", IsFreeFunction = true)]
get;
[NativeMethod(Name = "ProfilerBindings::SetAllocationCallstackCaptureEnabled", IsFreeFunction = true)]
set;
}
[Conditional("ENABLE_PROFILER")]
[FreeFunction("ProfilerBindings::profiler_set_area_enabled")]
public extern static void SetAreaEnabled(ProfilerArea area, bool enabled);
// TODO
//[Obsolete("", false)]
public static int areaCount
{
get
{
return Enum.GetNames(typeof(ProfilerArea)).Length;
}
}
[NativeConditional("ENABLE_PROFILER")]
[FreeFunction("ProfilerBindings::profiler_is_area_enabled")]
public extern static bool GetAreaEnabled(ProfilerArea area);
// Displays the recorded profiledata in the profiler.
[Conditional("UNITY_EDITOR")]
public static void AddFramesFromFile(string file)
{
if (string.IsNullOrEmpty(file))
{
Debug.LogError("AddFramesFromFile: Invalid or empty path");
return;
}
AddFramesFromFile_Internal(file, true);
}
[NativeHeader("Modules/ProfilerEditor/Public/ProfilerSession.h")]
[NativeConditional("ENABLE_PROFILER && UNITY_EDITOR")]
[NativeMethod(Name = "LoadFromFile")]
[StaticAccessor("profiling::GetProfilerSessionPtr()", StaticAccessorType.Arrow)]
private extern static void AddFramesFromFile_Internal(string file, bool keepExistingFrames);
[Conditional("ENABLE_PROFILER")]
public static void BeginThreadProfiling(string threadGroupName, string threadName)
{
if (string.IsNullOrEmpty(threadGroupName))
throw new ArgumentException("Argument should be a valid string", "threadGroupName");
if (string.IsNullOrEmpty(threadName))
throw new ArgumentException("Argument should be a valid string", "threadName");
BeginThreadProfilingInternal(threadGroupName, threadName);
}
[NativeConditional("ENABLE_PROFILER")]
[NativeMethod(Name = "ProfilerBindings::BeginThreadProfiling", IsFreeFunction = true, IsThreadSafe = true)]
private extern static void BeginThreadProfilingInternal(string threadGroupName, string threadName);
[NativeConditional("ENABLE_PROFILER")]
public static void EndThreadProfiling() {}
// Begin profiling a piece of code with a custom label.
// TODO: make obsolete
//OBSOLETE warning Profiler.BeginSample method is deprecated. Please use faster CustomSampler.Begin method instead.
[MethodImpl(256)]
[Conditional("ENABLE_PROFILER")]
public static void BeginSample(string name)
{
ValidateArguments(name);
BeginSampleImpl(name, null);
}
// Begin profiling a piece of code with a custom label.
// TODO: make obsolete
//OBSOLETE warning Profiler.BeginSample method is deprecated. Please use faster CustomSampler.Begin method instead.
[MethodImpl(256)]
[Conditional("ENABLE_PROFILER")]
public static void BeginSample(string name, Object targetObject)
{
ValidateArguments(name);
BeginSampleImpl(name, targetObject);
}
[MethodImpl(256)]
static void ValidateArguments(string name)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentException("Argument should be a valid string.", "name");
}
}
[NativeMethod(Name = "ProfilerBindings::BeginSample", IsFreeFunction = true, IsThreadSafe = true)]
private extern static void BeginSampleImpl(string name, Object targetObject);
// End profiling a piece of code with a custom label.
// TODO: make obsolete
//OBSOLETE warning Profiler.EndSample method is deprecated. Please use faster CustomSampler.End method instead.
[Conditional("ENABLE_PROFILER")]
[NativeMethod(Name = "ProfilerBindings::EndSample", IsFreeFunction = true, IsThreadSafe = true)]
public extern static void EndSample();
[Obsolete("maxNumberOfSamplesPerFrame has been depricated. Use maxUsedMemory instead")]
public static int maxNumberOfSamplesPerFrame
{
get
{
return 0;
}
set
{
}
}
[Obsolete("usedHeapSize has been deprecated since it is limited to 4GB. Please use usedHeapSizeLong instead.")]
public static uint usedHeapSize
{
get { return (uint)usedHeapSizeLong; }
}
// Heap size used by the program
public extern static long usedHeapSizeLong
{
[NativeMethod(Name = "GetUsedHeapSize", IsFreeFunction = true)]
get;
}
// Returns the runtime memory usage of the resource.
[Obsolete("GetRuntimeMemorySize has been deprecated since it is limited to 2GB. Please use GetRuntimeMemorySizeLong() instead.")]
public static int GetRuntimeMemorySize(Object o)
{
return (int)GetRuntimeMemorySizeLong(o);
}
[NativeMethod(Name = "ProfilerBindings::GetRuntimeMemorySizeLong", IsFreeFunction = true)]
public extern static long GetRuntimeMemorySizeLong([NotNull] Object o);
[Obsolete("GetMonoHeapSize has been deprecated since it is limited to 4GB. Please use GetMonoHeapSizeLong() instead.")]
public static uint GetMonoHeapSize()
{
return (uint)GetMonoHeapSizeLong();
}
// Returns the size of the mono heap
[NativeMethod(Name = "scripting_gc_get_heap_size", IsFreeFunction = true)]
public extern static long GetMonoHeapSizeLong();
[Obsolete("GetMonoUsedSize has been deprecated since it is limited to 4GB. Please use GetMonoUsedSizeLong() instead.")]
public static uint GetMonoUsedSize()
{
return (uint)GetMonoUsedSizeLong();
}
// Returns the used size from mono
[NativeMethod(Name = "scripting_gc_get_used_size", IsFreeFunction = true)]
public extern static long GetMonoUsedSizeLong();
// Sets the size of the MainThread's StackAllocator which is used for temp allocs
[StaticAccessor("GetMemoryManager()", StaticAccessorType.Dot)]
[NativeConditional("ENABLE_MEMORY_MANAGER")]
public extern static bool SetTempAllocatorRequestedSize(uint size);
// Gets the size of the MainThread's StackAllocator which is used for temp allocs
[StaticAccessor("GetMemoryManager()", StaticAccessorType.Dot)]
[NativeConditional("ENABLE_MEMORY_MANAGER")]
public extern static uint GetTempAllocatorSize();
[Obsolete("GetTotalAllocatedMemory has been deprecated since it is limited to 4GB. Please use GetTotalAllocatedMemoryLong() instead.")]
public static uint GetTotalAllocatedMemory()
{
return (uint)GetTotalAllocatedMemoryLong();
}
[NativeMethod(Name = "GetTotalAllocatedMemory")]
[StaticAccessor("GetMemoryManager()", StaticAccessorType.Dot)]
[NativeConditional("ENABLE_MEMORY_MANAGER")]
public extern static long GetTotalAllocatedMemoryLong();
[Obsolete("GetTotalUnusedReservedMemory has been deprecated since it is limited to 4GB. Please use GetTotalUnusedReservedMemoryLong() instead.")]
public static uint GetTotalUnusedReservedMemory()
{
return (uint)GetTotalUnusedReservedMemoryLong();
}
[NativeMethod(Name = "GetTotalUnusedReservedMemory")]
[StaticAccessor("GetMemoryManager()", StaticAccessorType.Dot)]
[NativeConditional("ENABLE_MEMORY_MANAGER")]
public extern static long GetTotalUnusedReservedMemoryLong();
[Obsolete("GetTotalReservedMemory has been deprecated since it is limited to 4GB. Please use GetTotalReservedMemoryLong() instead.")]
public static uint GetTotalReservedMemory()
{
return (uint)GetTotalReservedMemoryLong();
}
[NativeMethod(Name = "GetTotalReservedMemory")]
[StaticAccessor("GetMemoryManager()", StaticAccessorType.Dot)]
[NativeConditional("ENABLE_MEMORY_MANAGER")]
public extern static long GetTotalReservedMemoryLong();
[NativeConditional("ENABLE_MEMORY_MANAGER")]
public static unsafe long GetTotalFragmentationInfo(NativeArray<int> stats)
{
return InternalGetTotalFragmentationInfo((IntPtr)stats.GetUnsafePtr(), stats.Length);
}
[NativeMethod(Name = "GetTotalFragmentationInfo")]
[StaticAccessor("GetMemoryManager()", StaticAccessorType.Dot)]
[NativeConditional("ENABLE_MEMORY_MANAGER")]
private extern static long InternalGetTotalFragmentationInfo(IntPtr pStats, int count);
[NativeMethod(Name = "GetRegisteredGFXDriverMemory", IsThreadSafe = true)]
[StaticAccessor("MemoryProfiler", StaticAccessorType.DoubleColon)]
[NativeConditional("ENABLE_PROFILER")]
public extern static long GetAllocatedMemoryForGraphicsDriver();
[Conditional("ENABLE_PROFILER")]
public static unsafe void EmitFrameMetaData(Guid id, int tag, Array data)
{
if (data == null)
throw new ArgumentNullException("data");
var elementType = data.GetType().GetElementType();
if (!UnsafeUtility.IsBlittable(elementType))
throw new ArgumentException(string.Format("{0} type must be blittable", elementType));
Internal_EmitGlobalMetaData_Array(&id, 16, tag, data, data.Length, UnsafeUtility.SizeOf(elementType), true);
}
[Conditional("ENABLE_PROFILER")]
public static unsafe void EmitFrameMetaData<T>(Guid id, int tag, List<T> data) where T : struct
{
if (data == null)
throw new ArgumentNullException("data");
var elementType = typeof(T);
if (!UnsafeUtility.IsBlittable(typeof(T)))
throw new ArgumentException(string.Format("{0} type must be blittable", elementType));
Internal_EmitGlobalMetaData_Array(&id, 16, tag, NoAllocHelpers.ExtractArrayFromList(data), data.Count, UnsafeUtility.SizeOf(elementType), true);
}
[Conditional("ENABLE_PROFILER")]
public static unsafe void EmitFrameMetaData<T>(Guid id, int tag, Unity.Collections.NativeArray<T> data) where T : struct
{
Internal_EmitGlobalMetaData_Native(&id, 16, tag, (IntPtr)data.GetUnsafeReadOnlyPtr(), data.Length, UnsafeUtility.SizeOf<T>(), true);
}
[Conditional("ENABLE_PROFILER")]
public static unsafe void EmitSessionMetaData(Guid id, int tag, Array data)
{
if (data == null)
throw new ArgumentNullException("data");
var elementType = data.GetType().GetElementType();
if (!UnsafeUtility.IsBlittable(elementType))
throw new ArgumentException(string.Format("{0} type must be blittable", elementType));
Internal_EmitGlobalMetaData_Array(&id, 16, tag, data, data.Length, UnsafeUtility.SizeOf(elementType), false);
}
[Conditional("ENABLE_PROFILER")]
public static unsafe void EmitSessionMetaData<T>(Guid id, int tag, List<T> data) where T : struct
{
if (data == null)
throw new ArgumentNullException("data");
var elementType = typeof(T);
if (!UnsafeUtility.IsBlittable(typeof(T)))
throw new ArgumentException(string.Format("{0} type must be blittable", elementType));
Internal_EmitGlobalMetaData_Array(&id, 16, tag, NoAllocHelpers.ExtractArrayFromList(data), data.Count, UnsafeUtility.SizeOf(elementType), false);
}
[Conditional("ENABLE_PROFILER")]
public static unsafe void EmitSessionMetaData<T>(Guid id, int tag, Unity.Collections.NativeArray<T> data) where T : struct
{
Internal_EmitGlobalMetaData_Native(&id, 16, tag, (IntPtr)data.GetUnsafeReadOnlyPtr(), data.Length, UnsafeUtility.SizeOf<T>(), false);
}
[NativeMethod(Name = "ProfilerBindings::Internal_EmitGlobalMetaData_Array", IsFreeFunction = true, IsThreadSafe = true)]
[NativeConditional("ENABLE_PROFILER")]
static extern unsafe void Internal_EmitGlobalMetaData_Array(void* id, int idLen, int tag, Array data, int count, int elementSize, bool frameData);
[NativeMethod(Name = "ProfilerBindings::Internal_EmitGlobalMetaData_Native", IsFreeFunction = true, IsThreadSafe = true)]
[NativeConditional("ENABLE_PROFILER")]
static extern unsafe void Internal_EmitGlobalMetaData_Native(void* id, int idLen, int tag, IntPtr data, int count, int elementSize, bool frameData);
[Conditional("ENABLE_PROFILER")]
public static void SetCategoryEnabled(ProfilerCategory category, bool enabled)
{
if (category == ProfilerCategory.Any)
throw new ArgumentException("Argument should be a valid category", "category");
Internal_SetCategoryEnabled((ushort)category, enabled);
}
public static bool IsCategoryEnabled(ProfilerCategory category)
{
if (category == ProfilerCategory.Any)
throw new ArgumentException("Argument should be a valid category", "category");
return Internal_IsCategoryEnabled((ushort)category);
}
[NativeHeader("Runtime/Profiler/ProfilerManager.h")]
[NativeMethod(Name = "GetCategoriesCount")]
[StaticAccessor("profiling::GetProfilerManagerPtr()", StaticAccessorType.Arrow)]
[NativeConditional("ENABLE_PROFILER")]
public static extern uint GetCategoriesCount();
[Conditional("ENABLE_PROFILER")]
public static void GetAllCategories(ProfilerCategory[] categories)
{
for (int i = 0; i < Math.Min(GetCategoriesCount(), categories.Length); i++)
categories[i] = new ProfilerCategory((ushort)i);
}
[Conditional("ENABLE_PROFILER")]
public static void GetAllCategories(NativeArray<ProfilerCategory> categories)
{
for (int i = 0; i < Math.Min(GetCategoriesCount(), categories.Length); i++)
categories[i] = new ProfilerCategory((ushort)i);
}
[NativeMethod(Name = "profiler_set_category_enable", IsFreeFunction = true, IsThreadSafe = true)]
[NativeConditional("ENABLE_PROFILER")]
private extern static void Internal_SetCategoryEnabled(ushort categoryId, bool enabled);
[NativeMethod(Name = "profiler_is_category_enabled", IsFreeFunction = true, IsThreadSafe = true)]
[NativeConditional("ENABLE_PROFILER")]
private extern static bool Internal_IsCategoryEnabled(ushort categoryId);
}
}