Skip to content

Commit

Permalink
add more instrumentation & also track gamescope_img fiber
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkautarch committed Jul 19, 2024
1 parent 5ae898a commit 3fd999a
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 7 deletions.
8 changes: 7 additions & 1 deletion src/commit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,23 @@ int commit_t::GetFD()

void commit_t::OnPollIn()
{
TracyFiberEnter(sl_img_waiter_fiber);
gpuvis_trace_end_ctx_printf( commitID, "wait fence" );

{
std::unique_lock lock( m_WaitableCommitStateMutex );
if ( !CloseFenceInternal() )
if ( !CloseFenceInternal() ) {
TracyCZoneEnd(cZone_img_waiter);
TracyFiberLeave;
return;
}
}

Signal();

nudge_steamcompmgr();
TracyCZoneEnd(cZone_img_waiter);
TracyFiberLeave;
}

void commit_t::Signal()
Expand Down
2 changes: 1 addition & 1 deletion src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ libdecor_dep = dependency('libdecor-0')
tracy_public_deps = []
tracy_public_args = []
if get_option('tracy_enable')
tracy_public_args += ['-DTRACY_ENABLE', '-DTRACY_VK_USE_SYMBOL_TABLE']
tracy_public_args += ['-DTRACY_ENABLE', '-DTRACY_VK_USE_SYMBOL_TABLE', '-DTRACY_FIBERS']

if get_option('tracy_on_demand')
tracy_public_args += ['-DTRACY_ON_DEMAND']
Expand Down
1 change: 1 addition & 0 deletions src/rendervulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2753,6 +2753,7 @@ static void present_wait_thread_func( void )
{
g_device.vk.WaitForPresentKHR( g_device.device(), g_output.swapChain, present_wait_id, 1'000'000'000lu );
uint64_t vblanktime = get_time_in_nanos();
TracyMessageL("present_wait_thread_func -> MarkVBlank()");
GetVBlankTimer().MarkVBlank( vblanktime, true );
mangoapp_output_update( vblanktime );
}
Expand Down
13 changes: 10 additions & 3 deletions src/steamcompmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6285,8 +6285,13 @@ void update_wayland_res(CommitDoneList_t *doneCommits, steamcompmgr_win_t *w, Re
newCommit->SetReleasePoint( reslistentry.oReleasePoint );
if ( bKnownReady )
newCommit->Signal();
else
else {
TracyFiberEnter(sl_img_waiter_fiber);
TracyCZone (ctx , 1);
cZone_img_waiter = ctx;
g_ImageWaiter.AddWaitable( newCommit.get() );
TracyFiberLeave;
}
}

w->commit_queue.push_back( std::move(newCommit) );
Expand Down Expand Up @@ -7324,8 +7329,10 @@ steamcompmgr_main(int argc, char **argv) TRACY_TRY
server->ctx->Dispatch();
}
}

g_SteamCompMgrWaiter.PollEvents();
{
ZoneScopedN("g_SteamCompMgrWaiter.PollEvents()");
g_SteamCompMgrWaiter.PollEvents();
}

if ( std::optional<gamescope::VBlankTime> pendingVBlank = GetVBlankTimer().ProcessVBlank() )
{
Expand Down
63 changes: 63 additions & 0 deletions src/tracy_include.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include <exception>
#include "tracy/Tracy.hpp"
#include "tracy/TracyC.h"


// (from 3.4.6 of Tracy manual) Tracy doesn't support exiting within a zone.
Expand All @@ -9,14 +10,27 @@

extern const char* const sl_steamcompmgr_name;
extern const char* const sl_vblankFrameName;
extern const char* const sl_img_waiter_fiber;
inline TracyCZoneCtx cZone_img_waiter;

#ifdef TRACY_ENABLE
#include <pthread.h>
# define EXIT(status) throw ETracyExit(status)
# define PTHREAD_EXIT(status) throw ETracyExit(status, true)
# define MAYBE_NORETURN
# define TRACY_TRY try
# define TRACY_CATCH catch(const ETracyExit& e) { e.m_bPthreadExit ? pthread_exit(const_cast<void*>(e.m_pStatus)) : exit(e.m_status); }
# define TracyDoubleLockable( type, varname ) tracy::DoubleLockable<type> varname { [] () -> const tracy::SourceLocationData* { static constexpr tracy::SourceLocationData srcloc { nullptr, #type " " #varname, TracyFile, TracyLine, 0 }; return &srcloc; }() }
//pthread lock instrumentation seems to trigger an assert on tracy server, so leaving it commented out for now:
# if 0
# define TracyPthreadLockable( type, varname, initializer ) tracy::PthreadLockable varname { [] () -> const tracy::SourceLocationData* { static constexpr tracy::SourceLocationData srcloc { nullptr, #type " " #varname, TracyFile, TracyLine, 0 }; return &srcloc; }() }; \
using tracy::pthread_mutex_lock; \
using tracy::pthread_mutex_unlock; \
using tracy::pthread_mutex_trylock
# else
# define TracyPthreadLockable( type, varname, initializer ) type varname = initializer
# endif

#else
# define EXIT(status) exit(status)
# define PTHREAD_EXIT(status) pthread_exit(status)
Expand All @@ -25,6 +39,7 @@ extern const char* const sl_vblankFrameName;
# define TRACY_CATCH
# define tracy_force_inline
# define TracyDoubleLockable( type, varname ) type varname
# define TracyPthreadLockable( type, varname, initializer ) type varname = initializer
#endif

#ifdef TRACY_ENABLE
Expand Down Expand Up @@ -104,5 +119,53 @@ namespace tracy
T m_lockable;
LockableCtx m_ctx;
};

class PthreadLockable
{
public:
tracy_force_inline PthreadLockable( const SourceLocationData* srcloc )
: m_ctx( srcloc )
{
}

PthreadLockable( const PthreadLockable& ) = delete;
PthreadLockable& operator=( const PthreadLockable& ) = delete;

tracy_force_inline void Mark( const SourceLocationData* srcloc )
{
m_ctx.Mark( srcloc );
}

tracy_force_inline void CustomName( const char* name, size_t size )
{
m_ctx.CustomName( name, size );
}

private:
friend tracy_force_inline void pthread_mutex_lock(PthreadLockable* mut);
friend tracy_force_inline void pthread_mutex_unlock(PthreadLockable* mut);
friend tracy_force_inline int pthread_mutex_trylock(PthreadLockable* mut);
pthread_mutex_t m_lockable = PTHREAD_MUTEX_INITIALIZER;
LockableCtx m_ctx;
};

tracy_force_inline void pthread_mutex_lock(PthreadLockable* mut)
{
const auto runAfter = mut->m_ctx.BeforeLock();
::pthread_mutex_lock( &(mut->m_lockable) );
if( runAfter ) mut->m_ctx.AfterLock();
}

tracy_force_inline void pthread_mutex_unlock(PthreadLockable* mut)
{
::pthread_mutex_unlock( &(mut->m_lockable) );
mut->m_ctx.AfterUnlock();
}
tracy_force_inline int pthread_mutex_trylock(PthreadLockable* mut)
{
const auto acquired = ::pthread_mutex_trylock( &(mut->m_lockable) );
mut->m_ctx.AfterTryLock( acquired );
return acquired;
}
}
#endif
3 changes: 2 additions & 1 deletion src/tracy_string_literals.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "tracy_include.h"
const char* const sl_steamcompmgr_name = "gamescope-xwm";
const char* const sl_vblankFrameName = "vblankmanager";
const char* const sl_vblankFrameName = "vblankmanager";
const char* const sl_img_waiter_fiber = "commit wait";
2 changes: 1 addition & 1 deletion src/wlserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1920,7 +1920,7 @@ bool wlserver_init( void ) {
return true;
}

pthread_mutex_t waylock = PTHREAD_MUTEX_INITIALIZER;
TracyPthreadLockable(pthread_mutex_t, waylock, PTHREAD_MUTEX_INITIALIZER);

bool wlserver_is_lock_held(void)
{
Expand Down

0 comments on commit 3fd999a

Please sign in to comment.