Fix C# profiler events from other threads

This commit is contained in:
Wojtek Figat
2021-06-12 20:10:16 +02:00
parent 93cdb7ce8f
commit 25c00a0d55
3 changed files with 29 additions and 16 deletions

View File

@@ -122,6 +122,14 @@ void ProfilerCPU::Thread::EndEvent(int32 index)
e.End = time;
}
void ProfilerCPU::Thread::EndEvent()
{
const double time = Platform::GetTimeSeconds() * 1000.0;
_depth--;
Event& e = Buffer.Get(Buffer.GetCount() - 1);
e.End = time;
}
bool ProfilerCPU::IsProfilingCurrentThread()
{
return Enabled && Thread::Current != nullptr;
@@ -194,11 +202,14 @@ int32 ProfilerCPU::BeginEvent(const char* name)
void ProfilerCPU::EndEvent(int32 index)
{
if (!Enabled)
return;
if (Enabled && Thread::Current)
Thread::Current->EndEvent(index);
}
ASSERT(Thread::Current);
Thread::Current->EndEvent(index);
void ProfilerCPU::EndEvent()
{
if (Enabled && Thread::Current)
Thread::Current->EndEvent();
}
void ProfilerCPU::Dispose()

View File

@@ -289,6 +289,11 @@ public:
/// </summary>
/// <param name="index">The event index returned by the BeginEvent method.</param>
void EndEvent(int32 index);
/// <summary>
/// Ends the last event running on a this thread.
/// </summary>
void EndEvent();
};
public:
@@ -341,6 +346,11 @@ public:
/// <param name="index">The event index returned by the BeginEvent method.</param>
static void EndEvent(int32 index);
/// <summary>
/// Ends the last event.
/// </summary>
static void EndEvent();
/// <summary>
/// Releases resources. Calls to the profiling API after Dispose are not valid.
/// </summary>

View File

@@ -14,29 +14,21 @@
namespace ProfilerInternal
{
/// <summary>
/// The managed events IDs.
/// </summary>
Array<int32> ManagedEvents;
/// <summary>
/// The managed events IDs for GPU profiling.
/// </summary>
#if COMPILE_WITH_PROFILER
Array<int32> ManagedEventsGPU;
#endif
void BeginEvent(MonoString* nameObj)
{
#if COMPILE_WITH_PROFILER
const auto index = ProfilerCPU::BeginEvent((const Char*)mono_string_chars(nameObj));
ManagedEvents.Push(index);
ProfilerCPU::BeginEvent((const Char*)mono_string_chars(nameObj));
#endif
}
void EndEvent()
{
#if COMPILE_WITH_PROFILER
const auto index = ManagedEvents.Pop();
ProfilerCPU::EndEvent(index);
ProfilerCPU::EndEvent();
#endif
}