1
0
mirror of https://github.com/wolfpld/tracy.git synced 2025-03-20 07:40:02 +08:00

async prototype

This commit is contained in:
n0lavar 2023-08-01 21:23:20 +03:00
parent 0e26b9e53f
commit bda9a21f40
5 changed files with 314 additions and 0 deletions

View File

@ -0,0 +1,127 @@
#pragma once
#include "TracyProfiler.hpp"
#define DEBUG_TEMP 1
#if DEBUG_TEMP
#include <syncstream>
#include <iostream>
#include <string>
#endif
namespace tracy
{
class AsyncScopedZone;
struct SourceLocationData;
inline thread_local tracy::AsyncScopedZone* g_pCurrentZone = nullptr;
void StartAsyncEvent(const AsyncScopedZone* pAsyncScopedZone
#if DEBUG_TEMP
, size_t nFiber
#endif
);
void StopAsyncEvent();
class AsyncScopedZone
{
public:
AsyncScopedZone(const SourceLocationData* pSourceLocation
#if DEBUG_TEMP
, size_t nFiber
#endif
);
~AsyncScopedZone();
// private:
const SourceLocationData* const m_pSourceLocation = nullptr;
#if DEBUG_TEMP
const size_t m_nFiber = -1;
#endif
AsyncScopedZone* m_pParent = nullptr;
};
}
tracy_force_inline void tracy::StartAsyncEvent(const AsyncScopedZone* pAsyncScopedZone
#if DEBUG_TEMP
, size_t nFiber
#endif
)
{
TracyQueuePrepare( QueueType::ZoneBegin );
MemWrite( &item->zoneBegin.time, Profiler::GetTime() );
MemWrite( &item->zoneBegin.srcloc, (uint64_t)pAsyncScopedZone->m_pSourceLocation );
TracyQueueCommit( zoneBeginThread );
#if DEBUG_TEMP
if (nFiber == 0)
{
const std::string sMessage = "fiber: " + std::to_string(nFiber);
tracy::Profiler::Message(sMessage.c_str(), sMessage.size(), 0);
}
#endif
}
tracy_force_inline void tracy::StopAsyncEvent()
{
TracyQueuePrepare(QueueType::ZoneEnd);
MemWrite(&item->zoneEnd.time, Profiler::GetTime());
TracyQueueCommit(zoneEndThread);
}
tracy_force_inline tracy::AsyncScopedZone::AsyncScopedZone(const SourceLocationData* pSourceLocation
#if DEBUG_TEMP
, size_t nFiber
#endif
)
: m_pSourceLocation(pSourceLocation)
#if DEBUG_TEMP
, m_nFiber(nFiber)
#endif
{
if (g_pCurrentZone)
{
#if DEBUG_TEMP
std::osyncstream(std::cout) << "AsyncScopedZone: constructor, StopAsyncEvent"
<< ", location: " << g_pCurrentZone->m_pSourceLocation->function
<< ", fiber: " << nFiber
<< ", thread id: " << std::this_thread::get_id()
<< ", frame: " << GetProfiler().GetFrame()
<< std::endl;
#endif
m_pParent = g_pCurrentZone;
StopAsyncEvent();
}
#if DEBUG_TEMP
std::osyncstream(std::cout) << "AsyncScopedZone: constructor, StartAsyncEvent"
<< ", location: " << pSourceLocation->function
<< ", fiber: " << nFiber
<< ", thread id: " << std::this_thread::get_id()
<< ", frame: " << GetProfiler().GetFrame()
<< std::endl;
#endif
g_pCurrentZone = this;
StartAsyncEvent(this
#if DEBUG_TEMP
, nFiber
#endif
);
}
tracy_force_inline tracy::AsyncScopedZone::~AsyncScopedZone()
{
if (g_pCurrentZone)
{
StopAsyncEvent();
g_pCurrentZone = g_pCurrentZone->m_pParent;
if (g_pCurrentZone)
StartAsyncEvent(g_pCurrentZone
#if DEBUG_TEMP
, g_pCurrentZone->m_nFiber
#endif
);
}
}

View File

@ -0,0 +1,108 @@
#pragma once
#include "TracyAsyncEvents.hpp"
#include <type_traits>
namespace tracy
{
template<class BaseAwaitableType>
class Awaitable : public BaseAwaitableType
{
public:
Awaitable(BaseAwaitableType baseAwaitable);
#if DEBUG_TEMP
~Awaitable();
#endif
auto await_suspend(auto handle);
auto await_resume();
private:
AsyncScopedZone* m_pCurrentZone = nullptr;
};
template<class BaseAwaitableType>
Awaitable<BaseAwaitableType>::Awaitable(BaseAwaitableType baseAwaitable)
: BaseAwaitableType(std::move(baseAwaitable))
{
#if DEBUG_TEMP
std::osyncstream(std::cout) << "Awaitable: constructor"
<< ", thread id: " << std::this_thread::get_id()
<< ", frame: " << GetProfiler().GetFrame()
<< std::endl;
#endif
}
#if DEBUG_TEMP
template <class BaseAwaitableType>
Awaitable<BaseAwaitableType>::~Awaitable()
{
std::osyncstream(std::cout) << "Awaitable: destructor"
<< ", thread id: " << std::this_thread::get_id()
<< ", frame: " << GetProfiler().GetFrame()
<< std::endl;
}
#endif
template<class BaseAwaitableType>
auto Awaitable<BaseAwaitableType>::await_suspend(auto handle)
{
#if DEBUG_TEMP
std::osyncstream(std::cout) << "Awaitable: await_suspend"
<< ", thread id: " << std::this_thread::get_id()
<< ", frame: " << GetProfiler().GetFrame()
<< std::endl;
#endif
if (g_pCurrentZone)
{
#if DEBUG_TEMP
std::osyncstream(std::cout) << "Awaitable: await_suspend, StopAsyncEvent"
<< ", location: " << g_pCurrentZone->m_pSourceLocation->function
<< ", fiber: " << g_pCurrentZone->m_nFiber
<< ", thread id: " << std::this_thread::get_id()
<< ", frame: " << GetProfiler().GetFrame()
<< std::endl;
#endif
m_pCurrentZone = g_pCurrentZone;
g_pCurrentZone = nullptr;
StopAsyncEvent();
}
return BaseAwaitableType::await_suspend(handle);
}
template<class BaseAwaitableType>
auto Awaitable<BaseAwaitableType>::await_resume()
{
#if DEBUG_TEMP
std::osyncstream(std::cout) << "Awaitable: await_resume"
<< ", thread id: " << std::this_thread::get_id()
<< ", frame: " << GetProfiler().GetFrame()
<< std::endl;
#endif
if (m_pCurrentZone)
{
#if DEBUG_TEMP
std::osyncstream(std::cout) << "Awaitable: await_resume, StartAsyncEvent"
<< ", location: " << m_pCurrentZone->m_pSourceLocation->function
<< ", fiber: " << m_pCurrentZone->m_nFiber
<< ", thread id: " << std::this_thread::get_id()
<< ", frame: " << GetProfiler().GetFrame()
<< std::endl;
#endif
StartAsyncEvent(m_pCurrentZone
#if DEBUG_TEMP
, m_pCurrentZone->m_nFiber
#endif
);
g_pCurrentZone = m_pCurrentZone;
m_pCurrentZone = nullptr;
}
return BaseAwaitableType::await_resume();
}
}

View File

@ -237,6 +237,11 @@ public:
return m_zoneId.fetch_add( 1, std::memory_order_relaxed ); return m_zoneId.fetch_add( 1, std::memory_order_relaxed );
} }
tracy_force_inline uint64_t GetFrame() const
{
return m_frameCount.load( std::memory_order_relaxed );
}
static tracy_force_inline QueueItem* QueueSerial() static tracy_force_inline QueueItem* QueueSerial()
{ {
auto& p = GetProfiler(); auto& p = GetProfiler();

View File

@ -0,0 +1,61 @@
#pragma once
#include "TracyAwaitable.hpp"
#include <coroutine>
namespace tracy
{
template<class BaseResultType>
class Result : public BaseResultType
{
public:
Result(BaseResultType&& result);
#if DEBUG_TEMP
~Result();
#endif
auto operator co_await();
};
template<class BaseResultType>
Result<BaseResultType>::Result(BaseResultType&& result)
: BaseResultType(std::forward<BaseResultType>(result))
{
#if DEBUG_TEMP
std::osyncstream(std::cout) << "Result: constructor"
<< ", thread id: " << std::this_thread::get_id()
<< ", frame: " << GetProfiler().GetFrame()
<< std::endl;
#endif
}
#if DEBUG_TEMP
template <class BaseResultType>
Result<BaseResultType>::~Result()
{
std::osyncstream(std::cout) << "Result: destructor"
<< ", thread id: " << std::this_thread::get_id()
<< ", frame: " << GetProfiler().GetFrame()
<< std::endl;
}
#endif
template<class BaseResultType>
auto Result<BaseResultType>::operator co_await()
{
#if DEBUG_TEMP
std::osyncstream(std::cout) << "Result: operator co_await"
<< ", thread id: " << std::this_thread::get_id()
<< ", frame: " << GetProfiler().GetFrame()
<< std::endl;
#endif
return Awaitable(BaseResultType::operator co_await());
}
}
template<template<class> class original_result, class type, class... arguments>
struct std::coroutine_traits<tracy::Result<original_result<type>>, arguments...>
{
using promise_type = typename coroutine_traits<original_result<type>, arguments...>::promise_type;
};

View File

@ -0,0 +1,13 @@
#pragma once
#include "../client/TracyResult.hpp"
#if DEBUG_TEMP
#define ZoneAsyncC(color, fiber) \
static constexpr tracy::SourceLocationData TracyConcat(__tracy_source_location,TracyLine) { nullptr, TracyFunction, TracyFile, (uint32_t)TracyLine, color }; \
tracy::AsyncScopedZone ___tracy_async_scoped_zone( &TracyConcat(__tracy_source_location, TracyLine), fiber );
#else
#define ZoneAsyncC(color, fiber) \
static constexpr tracy::SourceLocationData TracyConcat(__tracy_source_location,TracyLine) { nullptr, TracyFunction, TracyFile, (uint32_t)TracyLine, color }; \
tracy::AsyncScopedZone ___tracy_async_scoped_zone( &TracyConcat(__tracy_source_location, TracyLine) );
#endif