Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Common - Util MP - Profile Sample.inl
    »Author«    Andrew Aye (EMail: mailto:andrew.aye@gmail.com, Web: http://www.andrewaye.com)
    »Version«   4.51 / »GUID« A9981407-3EC9-42AF-8B6F-8BE6DD919615                                                                                                        */
/*   -------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
/*  Copyright: © 2002-2017, Andrew Aye.  All Rights Reserved.
    This software is free for non-commercial use.  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
      following conditions are met:
        Redistribution of source code must retain this copyright notice, this list of conditions and the following disclaimers.
        Redistribution in binary form must reproduce this copyright notice, this list of conditions and the following disclaimers in the documentation and other materials
          provided with the distribution.
    The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
    The intellectual property rights of the algorithms used reside with Andrew Aye.
    You may not use this software, in whole or in part, in support of any commercial product without the express written consent of the author.
    There is no warranty or other guarantee of fitness of this software for any purpose. It is provided solely "as is".                                                   */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
#if !defined(TGS_COMMON_UTIL_MP_PROFILE_SAMPLE_INL)
#define TGS_COMMON_UTIL_MP_PROFILE_SAMPLE_INL
#pragma once


/* == Common ============================================================================================================================================================ */

/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  Public Functions                                                                                                                                                      */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
#if TgCOMPILE_64BIT_ATOMIC

/* ---- tgProfile_Sample_Reset ------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgVOID tgProfile_Sample_Reset( PCU_STg2_Profile_Record psMonitor )
{
    psMonitor->m_iRecord_DepthAndStart = 0;
    psMonitor->m_iRecord_Idle_Total = 0;
    psMonitor->m_iRecord_Sample_Total = 0;

    psMonitor->m_fReport_Sample = 0.0F;
    psMonitor->m_fReport_Idle = 0.0F;
    psMonitor->m_fReport_Total = 0.0F;
}


/* ---- tgProfile_Sample_Start ------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgSINT64 tgProfile_Sample_Start( PCU_STg2_Profile_Record psMonitor )
{
    TgSINT64                            iDepthAndStart, iNewDepthAndStart;
    TgSINT64                            iDepth;
    TgSINT64                            iSampleTick;
    TgSINT64                            iCurrent;

    do
    {
        iCurrent = tgTM_Query_Counter_Tick();
        iDepthAndStart = psMonitor->m_iRecord_DepthAndStart;
        iDepth = (iDepthAndStart >> 48) & KTgMAX_U16;
        TgPARAM_CHECK( iDepth < KTgMAX_U16 ); /* Max 16 bits of start depth */
        TgPARAM_CHECK( iCurrent < ((1LL << 48) - 1) ); /* Make sure that time does not overflow */
        iNewDepthAndStart = ((iDepth + 1) << 48) + iCurrent;
    }
    while (iDepthAndStart != tgAM64_XCMP( &psMonitor->m_iRecord_DepthAndStart, iNewDepthAndStart, iDepthAndStart ));

    tgAM_WRITE_FENCE();

    /* Not guaranteed to execute in the same frame as above but pragmatically safe for profiling */
    if (iDepth <= 0)
    {
        iSampleTick = (TgSINT64)(iCurrent - (iDepthAndStart & ((1LL << 48) - 1)));
        tgAM64_XADD( &psMonitor->m_iRecord_Idle_Total, iSampleTick );
    }
    else
    {
        iSampleTick = (TgSINT64)(iDepth * (iCurrent - (iDepthAndStart & ((1LL << 48) - 1))));
        tgAM64_XADD( &psMonitor->m_iRecord_Sample_Total, iSampleTick );
    }

    return iCurrent;
}


/* ---- tgProfile_Sample_Stop ------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgSINT64 tgProfile_Sample_Stop( PCU_STg2_Profile_Record psMonitor )
{
    TgSINT64                            iDepthAndStart, iNewDepthAndStart;
    TgSINT64                            iDepth;
    TgSINT64                            iSampleTick;
    TgSINT64                            iCurrent;

    do
    {
        iCurrent = tgTM_Query_Counter_Tick();
        iDepthAndStart = psMonitor->m_iRecord_DepthAndStart;
        iDepth = (iDepthAndStart >> 48) & KTgMAX_U16;
        TgPARAM_CHECK( iDepth > 0 ); /* Max 16 bits of start depth */
        TgPARAM_CHECK( iCurrent < ((1LL << 48) - 1) ); /* Make sure that time does not overflow */
        iNewDepthAndStart = ((iDepth - 1) << 48) + iCurrent;
    }
    while (iDepthAndStart != tgAM64_XCMP( &psMonitor->m_iRecord_DepthAndStart, iNewDepthAndStart, iDepthAndStart ));

    tgAM_WRITE_FENCE();

    /* Not guaranteed to execute in the same frame as above but pragmatically safe for profiling */
    iSampleTick = (TgSINT64)(iDepth * (iCurrent - (iDepthAndStart & ((1LL << 48) - 1))));
    tgAM64_XADD( &psMonitor->m_iRecord_Sample_Total, iSampleTick );

    return iCurrent;
}


/* ---- tgProfile_Sample_Report ----------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgVOID tgProfile_Sample_Report( PCU_STg2_Profile_Record psMonitor )
{
    TgSINT64                            iDepthAndStart, iNewDepthAndStart;
    TgSINT64                            iDepth;
    TgSINT64                            iCurrent;
    TgSINT64                            iSampleTotal;
    TgSINT64                            iIdleTotal;

    iSampleTotal = 0;
    iIdleTotal = 0;
    do
    {
        iCurrent = tgTM_Query_Counter_Tick();
        iDepthAndStart = psMonitor->m_iRecord_DepthAndStart;
        iDepth = (iDepthAndStart >> 48) & KTgMAX_U16;
        TgPARAM_CHECK( iCurrent < ((1LL << 48) - 1) ); /* Make sure that time does not overflow */
        iNewDepthAndStart = (iDepth << 48) + iCurrent;
        iSampleTotal += tgAM64_XCHG( &psMonitor->m_iRecord_Sample_Total, 0 );
        iIdleTotal += tgAM64_XCHG( &psMonitor->m_iRecord_Idle_Total, 0 );
    }
    while (iDepthAndStart != tgAM64_XCMP( &psMonitor->m_iRecord_DepthAndStart, iNewDepthAndStart, iDepthAndStart ));

    tgAM_WRITE_FENCE();

    /* Not guaranteed to execute in the same frame as above but pragmatically safe for profiling */
    if (iDepth <= 0)
    {
        iIdleTotal += iCurrent - (iDepthAndStart & ((1LL << 48) - 1));
    }
    else
    {
        iSampleTotal += iDepth * (iCurrent - (iDepthAndStart & ((1LL << 48) - 1)));
    }

    psMonitor->m_fReport_Sample = tgTM_Counter_Tick_To_MSec( iSampleTotal );
    psMonitor->m_fReport_Idle = tgTM_Counter_Tick_To_MSec( iIdleTotal );
    psMonitor->m_fReport_Total = tgTM_Counter_Tick_To_MSec( iSampleTotal + iIdleTotal );
}

/*# TgCOMPILE_64BIT_ATOMIC */
#endif


/* ====================================================================================================================================================================== */
#endif