Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Common - Profile.c
    »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".                                                   */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/* == Common ============================================================================================================================================================ */
#if TgCOMPILE_PROFILE


/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  File Local Types */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

TgTYPE_STRUCT(STg2_Profile_CallRecord,)
{
    union
    {
        TgPROFILE_CALL_ID                           m_tiID;
        STg2_UTM_Node                               m_sNext;
    }                                           m_sID;
    TgPROFILE_ID                                m_tiProf;
    TgSINT64                                    m_iStartOrElapsed;
};




/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  File Local Data */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

static STg2_Profile_Record                  s_asProfile_Record[KTgMAX_PROFILE];
static TgCHAR                               s_aszProfile_Tag[KTgMAX_PROFILE][KTgMAX_PROFILE_NAME_LENGTH];
static TgATOMIC_SINT32                      s_niProfile_Tag;

static STg2_Profile_Entry                   s_asProfile_Entry[KTgMAX_PROFILE];
static STg2_UTM_AM_RW                       s_asProfile_Entry_RW_Lock;

static STg2_Profile_CallRecord              s_asProfile_CallRecord[KTgMAX_PROFILE_CALL];
static STg2_UTM_AM_ST                       s_asProfile_CallRecord_Available_Stack;
static STg2_UTM_AM_ST                       s_asProfile_CallRecord_Process_Stack;

static ETgMODULE_STATE                      s_enProfile_State;




/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  Public Functions                                                                                                                                                      */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

/* ---- tgPF_Init ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT tgPF_Init( TgVOID )
{
    TgUINT32                            uiIndex;

    s_enProfile_State = ETgMODULE_STATE__INITIALIZING;

    for (uiIndex = 0; uiIndex < KTgMAX_PROFILE; ++uiIndex)
    {
        tgProfile_Sample_Reset( s_asProfile_Record + uiIndex );
    }

    memset( s_aszProfile_Tag, 0x00, sizeof( s_aszProfile_Tag ) );
    s_niProfile_Tag = 0;

    memset( s_asProfile_Entry, 0x00, sizeof( s_asProfile_Entry ) );
    tgCM_UTM_AM_RW_Init( &s_asProfile_Entry_RW_Lock );

    tgCM_UTM_AM_ST_Init_PreLoad( &s_asProfile_CallRecord_Available_Stack, s_asProfile_CallRecord, sizeof( STg2_Profile_CallRecord ), KTgMAX_PROFILE_CALL );
    tgCM_UTM_AM_ST_Init( &s_asProfile_CallRecord_Process_Stack );

    s_enProfile_State = ETgMODULE_STATE__INITIALIZED;
    return (KTgS_OK);
}


/* ---- tgPF_Boot ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT tgPF_Boot( TgVOID )
{
    TgERROR(ETgMODULE_STATE__INITIALIZED == s_enProfile_State);
    s_enProfile_State = ETgMODULE_STATE__BOOTED;
    return (KTgS_OK);
}


/* ---- tgPF_Stop ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgPF_Stop( TgVOID )
{
    TgERROR(ETgMODULE_STATE__BOOTED == s_enProfile_State);
    s_enProfile_State = ETgMODULE_STATE__STOPPED;
}


/* ---- tgPF_Free ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgPF_Free( TgVOID )
{
    TgERROR(ETgMODULE_STATE__STOPPED == s_enProfile_State || ETgMODULE_STATE__INITIALIZED == s_enProfile_State);

    tgCM_UTM_AM_ST_Free( &s_asProfile_CallRecord_Available_Stack );
    tgCM_UTM_AM_ST_Free( &s_asProfile_CallRecord_Process_Stack );
    tgCM_UTM_AM_RW_Free( &s_asProfile_Entry_RW_Lock );

    s_enProfile_State = ETgMODULE_STATE__FREED;
}


/* ---- tgPF_Update ---------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT tgPF_Update( C_TgFLOAT32 UNUSED_PARAM fDT )
{
    P_STg2_Profile_CallRecord           psCallRecord_Start;
    P_STg2_Profile_CallRecord           psCallRecord_End;
    TgSINT32                            iIndex;
    TgSINT32                            iMaxProfile;

    TgERROR(ETgMODULE_STATE__BOOTED == s_enProfile_State);

    psCallRecord_Start = (P_STg2_Profile_CallRecord)tgCM_UTM_AM_ST_Release( &s_asProfile_CallRecord_Process_Stack );
    psCallRecord_End = psCallRecord_Start;
    if (nullptr == psCallRecord_Start)
        return (KTgS_OK);

    tgCM_UTM_AM_RW_Enter_Write_Wait_Spin( &s_asProfile_Entry_RW_Lock  );
    while (1)
    {
        P_STg2_Profile_Entry                psEntry;

        psEntry = s_asProfile_Entry + psCallRecord_End->m_tiProf.m.iI;

        ++psEntry->m_niCall;
        psEntry->m_iCall_Time_Total += psCallRecord_End->m_iStartOrElapsed;
        psEntry->m_iCall_Time_Min = tgCM_MIN_S64( psEntry->m_iCall_Time_Min, psCallRecord_End->m_iStartOrElapsed );
        psEntry->m_iCall_Time_Max = tgCM_MAX_S64( psEntry->m_iCall_Time_Max, psCallRecord_End->m_iStartOrElapsed );

        if (nullptr == psCallRecord_End->m_sID.m_sNext.m_pNext_Node)
            break;
        psCallRecord_End = (P_STg2_Profile_CallRecord)psCallRecord_End->m_sID.m_sNext.m_pNext_Node;
    }

    iMaxProfile = tgCM_MIN_S32( s_niProfile_Tag, KTgMAX_PROFILE );
    for (iIndex = 0; iIndex < iMaxProfile; ++iIndex)
    {
        PC_STg2_Profile_Entry               psEntry = s_asProfile_Entry + iIndex;

        tgProfile_Sample_Report( s_asProfile_Record + iIndex );

        psEntry->m_fTime_Total = (TgFLOAT64)s_asProfile_Record[iIndex].m_fReport_Sample;
    };

    tgCM_UTM_AM_RW_Exit_Write( &s_asProfile_Entry_RW_Lock );

    tgCM_UTM_AM_ST_Merge( &s_asProfile_CallRecord_Available_Stack, (P_STg2_UTM_Node)psCallRecord_Start, (P_STg2_UTM_Node)psCallRecord_End );

    return (KTgS_OK);
}


/* ---- tgPF_Query_Init ------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgPF_Query_Init( TgVOID )
{
    return (ETgMODULE_STATE__INITIALIZED <= s_enProfile_State && s_enProfile_State <= ETgMODULE_STATE__STOPPED ? TgTRUE : TgFALSE);
}


/* ---- tgPF_Query_Boot ------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgPF_Query_Boot( TgVOID )
{
    return (ETgMODULE_STATE__BOOTED == s_enProfile_State ? TgTRUE : TgFALSE);
}


/* ---- tgPF_Query_Fixed_Memory ----------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgSIZE tgPF_Query_Fixed_Memory( TgVOID )
{
    return (0
             + sizeof( s_asProfile_Record )
             + sizeof( s_aszProfile_Tag )
             + sizeof( s_niProfile_Tag )
             + sizeof( s_asProfile_Entry )
             + sizeof( s_asProfile_Entry_RW_Lock )
             + sizeof( s_asProfile_CallRecord )
             + sizeof( s_asProfile_CallRecord_Available_Stack )
             + sizeof( s_asProfile_CallRecord_Process_Stack )
             + sizeof( s_enProfile_State )
    );
}


/* ---- tgPF_Report_Module ---------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgPF_Report_Module( PC_STg2_Output psOUT )
{
    TgSINT32                            iIndex;
    TgSINT32                            iMaxProfile;

    TgERROR(nullptr != psOUT);
 
    iMaxProfile = tgCM_MIN_S32( s_niProfile_Tag, KTgMAX_PROFILE );
    if (iMaxProfile <= 0)
        return;

    tgCM_UTM_AM_RW_Enter_Read_Wait_Spin( &s_asProfile_Entry_RW_Lock );

    tgIO_PrintF( psOUT, TgT("                                                                ") );
    tgIO_PrintF( psOUT, TgT("    Time Total  Call Count  Call Avg    Call Min    Call Max\n") );
    tgIO_PrintF( psOUT, TgT("----------------------------------------------------------------") );
    tgIO_PrintF( psOUT, TgT("    --------    --------    --------    --------    --------\n") );

    for (iIndex = 0; iIndex < iMaxProfile; ++iIndex)
    {
        CPC_STg2_Profile_Entry              psEntry = s_asProfile_Entry + iIndex;
        TgFLOAT32                           fCallAvg;

        fCallAvg = tgTM_Counter_Tick_To_MSec( psEntry->m_iCall_Time_Total );
        fCallAvg /= (TgFLOAT32)psEntry->m_niCall;

        tgIO_PrintF( psOUT, TgT("% 64.64s"), s_aszProfile_Tag[iIndex] );
        tgIO_PrintF( psOUT, TgT("    %8.2f"), psEntry->m_fTime_Total );
        tgIO_PrintF( psOUT, TgT("    %8d"), psEntry->m_niCall );
        tgIO_PrintF( psOUT, TgT("    %8.2f"), (TgFLOAT64)fCallAvg );
        tgIO_PrintF( psOUT, TgT("    %8.2f"), (TgFLOAT64)tgTM_Counter_Tick_To_MSec( psEntry->m_iCall_Time_Min ) );
        tgIO_PrintF( psOUT, TgT("    %8.2f"), (TgFLOAT64)tgTM_Counter_Tick_To_MSec( psEntry->m_iCall_Time_Max ) );
        tgIO_PrintF( psOUT, TgT("\n") );
    };

    tgCM_UTM_AM_RW_Exit_Read( &s_asProfile_Entry_RW_Lock );
}


/* ---- tgPF_Mark_Init -------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgPROFILE_ID tgPF_Mark_Init( CPC_TgCHAR szName )
{
    TgSINT32                            iIndex;
    TgPROFILE_ID                        tiProf;

    iIndex = tgAM32_INC( &s_niProfile_Tag ) - 1;
    if (iIndex >= KTgMAX_PROFILE)
        return (KTgPROFILE_ID__INVALID);

    TgERROR(0 == s_aszProfile_Tag[iIndex][0]);
    tgSZ_Copy( s_aszProfile_Tag[iIndex], KTgMAX_PROFILE_NAME_LENGTH, szName );
    tgInit_PROFILE_ID( &tiProf, (TgUINT16)iIndex );
    return (tiProf);
}


/* ---- tgPF_Mark_Start ------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgPROFILE_CALL_ID tgPF_Mark_Start( C_TgPROFILE_ID tiProf )
{
    P_STg2_Profile_CallRecord           psCallRecord;
    TgSINT64                            iTime;

    TgERROR(tgPROFILE_ID_Is_Valid( tiProf ));

    iTime = tgProfile_Sample_Start( s_asProfile_Record + tiProf.m.iI );

    psCallRecord = (P_STg2_Profile_CallRecord)tgCM_UTM_AM_ST_Pop( &s_asProfile_CallRecord_Available_Stack );
    if (nullptr == psCallRecord)
        return (KTgPROFILE_CALL_ID__INVALID);

    psCallRecord->m_tiProf = tiProf;
    psCallRecord->m_iStartOrElapsed = iTime;
    tgInit_PROFILE_CALL_ID( &psCallRecord->m_sID.m_tiID, (TgUINT16)((psCallRecord - s_asProfile_CallRecord) & KTgMAX_U16) );

    return (psCallRecord->m_sID.m_tiID);
}


/* ---- tgPF_Mark_Stop -------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgPF_Mark_Stop( C_TgPROFILE_ID tiProf, C_TgPROFILE_CALL_ID tiProfile_CallRecord )
{
    P_STg2_Profile_CallRecord           psCallRecord;
    TgSINT64                            iTime;

    TgERROR(tgPROFILE_ID_Is_Valid( tiProf ));

    iTime = tgProfile_Sample_Stop( s_asProfile_Record + tiProf.m.iI );
    if (!tgPROFILE_CALL_ID_Is_Valid( tiProfile_CallRecord ))
        return;

    TgERROR_INDEX( tiProfile_CallRecord.m.iI, s_asProfile_CallRecord );
    psCallRecord = s_asProfile_CallRecord + tiProfile_CallRecord.m.iI;
    psCallRecord->m_iStartOrElapsed = iTime - psCallRecord->m_iStartOrElapsed;
    tgCM_UTM_AM_ST_Push( &s_asProfile_CallRecord_Process_Stack, (P_STg2_UTM_Node)psCallRecord );
}


/* ---- tgPF_Mark_Reset ------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgPF_Mark_Reset( C_TgPROFILE_ID tiProf )
{
    TgERROR(tgPROFILE_ID_Is_Valid( tiProf ));

    tgProfile_Sample_Reset( s_asProfile_Record + tiProf.m.iI );

    tgCM_UTM_AM_RW_Enter_Write_Wait_Spin( &s_asProfile_Entry_RW_Lock );
    memset( s_asProfile_Entry + tiProf.m.iI, 0x00, sizeof( s_asProfile_Entry[0] ) );
    tgCM_UTM_AM_RW_Exit_Write( &s_asProfile_Entry_RW_Lock );
}


/* ---- tgPF_Query_Name ------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
CP_TgCHAR tgPF_Query_Name( C_TgPROFILE_ID tiProf )
{
    TgERROR(tgPROFILE_ID_Is_Valid( tiProf ));
    return (s_aszProfile_Tag[tiProf.m.iI]);
}


/* ---- tgPF_Query_Total_Time_Cached ------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgFLOAT64 tgPF_Query_Total_Time_Cached( C_TgPROFILE_ID tiProf )
{
    TgERROR(tgPROFILE_ID_Is_Valid( tiProf ));
    return (s_asProfile_Entry[tiProf.m.iI].m_fTime_Total);
}


#else

/* 2183: Empty Translation Unit */
TgEXTN TgVOID Prevent2183_Common_Profile( TgVOID );
TgVOID Prevent2183_Common_Profile( TgVOID )
{
}

#endif /* TgCOMPILE_PROFILE */