From 9d4727eba1703015c77501f321aa69819694a8a2 Mon Sep 17 00:00:00 2001 From: xen Date: Thu, 2 May 2024 06:40:35 +0300 Subject: [PATCH] Add VProfScopeHelper and update VPROF_* macros (#235) Co-authored-by: GAMMACASE <31375974+GAMMACASE@users.noreply.github.com> --- public/tier0/vprof.h | 80 ++++++++++++++++++++++++++++++++------------ 1 file changed, 58 insertions(+), 22 deletions(-) diff --git a/public/tier0/vprof.h b/public/tier0/vprof.h index 20386ba0..91de2d3f 100644 --- a/public/tier0/vprof.h +++ b/public/tier0/vprof.h @@ -1,4 +1,4 @@ -//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============// +//========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============// // // Purpose: Real-Time Hierarchical Profiling // @@ -14,11 +14,9 @@ #include "tier0/threadtools.h" // VProf is enabled by default in all configurations -except- X360 Retail. -#ifndef _LINUX #if !( defined( _X360 ) && defined( _CERT ) ) #define VPROF_ENABLED #endif -#endif #if defined(_X360) && defined(VPROF_ENABLED) #include "tier0/pmc360.h" @@ -147,28 +145,28 @@ #define VPROF_LEVEL 0 #endif -#define VPROF_0(name,group,assertAccounted,budgetFlags) CVProfScope VProf_(name, 0, group, assertAccounted, budgetFlags); +#define VPROF_0(name,group,assertAccounted,budgetFlags) VProfScopeHelper<0, assertAccounted> vprofHelper_(name, group, budgetFlags); #if VPROF_LEVEL > 0 -#define VPROF_1(name,group,assertAccounted,budgetFlags) CVProfScope VProf_(name, 1, group, assertAccounted, budgetFlags); +#define VPROF_1(name,group,assertAccounted,budgetFlags) VProfScopeHelper<1, assertAccounted> vprofHelper_(name, group, budgetFlags); #else #define VPROF_1(name,group,assertAccounted,budgetFlags) ((void)0) #endif #if VPROF_LEVEL > 1 -#define VPROF_2(name,group,assertAccounted,budgetFlags) CVProfScope VProf_(name, 2, group, assertAccounted, budgetFlags); +#define VPROF_2(name,group,assertAccounted,budgetFlags) VProfScopeHelper<2, assertAccounted> vprofHelper_(name, group, budgetFlags); #else #define VPROF_2(name,group,assertAccounted,budgetFlags) ((void)0) #endif #if VPROF_LEVEL > 2 -#define VPROF_3(name,group,assertAccounted,budgetFlags) CVProfScope VProf_(name, 3, group, assertAccounted, budgetFlags); +#define VPROF_3(name,group,assertAccounted,budgetFlags) VProfScopeHelper<3, assertAccounted> vprofHelper_(name, group, budgetFlags); #else #define VPROF_3(name,group,assertAccounted,budgetFlags) ((void)0) #endif #if VPROF_LEVEL > 3 -#define VPROF_4(name,group,assertAccounted,budgetFlags) CVProfScope VProf_(name, 4, group, assertAccounted, budgetFlags); +#define VPROF_4(name,group,assertAccounted,budgetFlags) VProfScopeHelper<4, assertAccounted> vprofHelper_(name, group, budgetFlags); #else #define VPROF_4(name,group,assertAccounted,budgetFlags) ((void)0) #endif @@ -735,11 +733,60 @@ inline void CVProfile::PopGroup( void ) //----------------------------------------------------------------------------- -class CVProfScope +typedef void (*VProfExitScopeCB)(); + +struct VProfBudgetGroupCallSite +{ + VProfBudgetGroupCallSite( const char *group_name, int flags, int group_id = VPROF_BUDGET_GROUP_ID_UNACCOUNTED ) : + m_pGroupName( group_name ), m_nFlags( flags ), m_nGroupId( group_id ) { } + + const char *m_pGroupName; + int m_nFlags; + int m_nGroupId; +}; + +template +class VProfScopeHelper { public: - CVProfScope( const tchar * pszName, int detailLevel, const tchar *pBudgetGroupName, bool bAssertAccounted, int budgetFlags ); - ~CVProfScope(); + VProfScopeHelper( const char *pszName ) + { + m_pExitScope = EnterScopeInternal( pszName ); + } + + VProfScopeHelper( const char *pszName, const char *pszGroupName, int nFlags ) + { + VProfBudgetGroupCallSite budget_group( pszGroupName, nFlags ); + m_pExitScope = EnterScopeInternalBudgetFlags( pszName, budget_group ); + } + + VProfScopeHelper( VProfScopeHelper &other ) + { + m_pExitScope = other.m_pExitScope; + } + + ~VProfScopeHelper() + { + ExitScope(); + } + + void ExitScope() + { + if(!m_pExitScope) + return; + + m_pExitScope(); + m_pExitScope = nullptr; + } + + DLL_CLASS_IMPORT VProfScopeHelper &operator=( const VProfScopeHelper &other ); + DLL_CLASS_IMPORT VProfScopeHelper &operator=( VProfScopeHelper &&other ); + + static DLL_CLASS_IMPORT VProfExitScopeCB EnterScopeInternal( const char *pszName ); + static DLL_CLASS_IMPORT VProfExitScopeCB EnterScopeInternalBudgetFlags( const char *pszName, VProfBudgetGroupCallSite &budgetGroup ); + +private: + VProfExitScopeCB m_pExitScope = nullptr; }; //----------------------------------------------------------------------------- @@ -1247,17 +1294,6 @@ inline unsigned int CVProfile::GetMultiTraceIndex() //----------------------------------------------------------------------------- -inline CVProfScope::CVProfScope( const tchar * pszName, int detailLevel, const tchar *pBudgetGroupName, bool bAssertAccounted, int budgetFlags ) -{ - g_VProfCurrentProfile.EnterScope( pszName, detailLevel, pBudgetGroupName, bAssertAccounted, budgetFlags ); -} - -//------------------------------------- - -inline CVProfScope::~CVProfScope() -{ - g_VProfCurrentProfile.ExitScope(); -} class CVProfCounter {