Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Common - Profile [Thread Local].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_TL_CallRecord,)
{
    union
    {
        TgPROFILE_TL_CALL_ID                        m_tiID;
        STg2_UTM_Node                               m_sNext;
    }                                           m_sID;
    TgPROFILE_TL_ID                             m_tiProf;
    TgSINT64                                    m_iStartOrElapsed;
};




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

static TgTLS STg2_Profile_Record            tls_asProfile_Record[KTgMAX_PROFILE_TL];
static TgTLS TgCHAR                         tls_aszProfile_Tag[KTgMAX_PROFILE_TL][KTgMAX_PROFILE_NAME_LENGTH];
static TgTLS TgATOMIC_SINT32                tls_niProfile_Tag;

static TgTLS STg2_Profile_Entry             tls_asProfile_Entry[KTgMAX_PROFILE_TL];

static TgTLS STg2_Profile_TL_CallRecord     tls_asProfile_CallRecord[KTgMAX_PROFILE_TL_CALL];
static TgTLS P_STg2_Profile_TL_CallRecord   tls_psProfile_CallRecord_Available_Head;
static TgTLS P_STg2_Profile_TL_CallRecord   tls_psProfile_CallRecord_Process_Head;

static TgTLS ETgMODULE_STATE                tls_enProfile_TL_State;




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

/* ---- tgPF_TL_Init_Boot ----------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT tgPF_TL_Init_Boot( TgVOID )
{
    TgSINT32                            iIndex;

    tls_enProfile_TL_State = ETgMODULE_STATE__INITIALIZING;

    for (iIndex = 0; iIndex < KTgMAX_PROFILE_TL; ++iIndex)
    {
        tgProfile_Sample_Reset( tls_asProfile_Record + iIndex );
    }

    memset( tls_asProfile_Entry, 0x00, sizeof( tls_asProfile_Entry ) );
    memset( tls_aszProfile_Tag, 0x00, sizeof( tls_aszProfile_Tag ) );
    tls_niProfile_Tag = 0;

    tls_psProfile_CallRecord_Available_Head = tls_asProfile_CallRecord;
    for (iIndex = 0; iIndex + 1 < KTgMAX_PROFILE_TL_CALL; ++iIndex)
    {
        tls_asProfile_CallRecord[iIndex].m_sID.m_sNext.m_pNext_Node = tls_asProfile_CallRecord + iIndex + 1;
    }

    tls_psProfile_CallRecord_Process_Head = nullptr;

    tls_enProfile_TL_State = ETgMODULE_STATE__BOOTED;
    return (KTgS_OK);
}


/* ---- tgPF_TL_Stop_Free ----------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgPF_TL_Stop_Free( TgVOID )
{
    TgERROR(ETgMODULE_STATE__BOOTED == tls_enProfile_TL_State);
    tls_enProfile_TL_State = ETgMODULE_STATE__FREED;
}


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

    TgERROR(ETgMODULE_STATE__BOOTED == tls_enProfile_TL_State);

    psCallRecord_Start = tls_psProfile_CallRecord_Process_Head;
    tls_psProfile_CallRecord_Process_Head = nullptr;
    if (nullptr == psCallRecord_Start)
        return (KTgS_OK);

    psCallRecord_End = psCallRecord_Start;
    while (1)
    {
        P_STg2_Profile_Entry                psEntry;

        psEntry = tls_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_TL_CallRecord)psCallRecord_End->m_sID.m_sNext.m_pNext_Node;
    }

    iMaxProfile = tgCM_MIN_S32( tls_niProfile_Tag, KTgMAX_PROFILE_TL );
    for (iIndex = 0; iIndex < iMaxProfile; ++iIndex)
    {
        PC_STg2_Profile_Entry                  psEntry = tls_asProfile_Entry + iIndex;

        tgProfile_Sample_Report( tls_asProfile_Record + iIndex );

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

    psCallRecord_End->m_sID.m_sNext.m_pNext_Node = tls_psProfile_CallRecord_Available_Head;
    tls_psProfile_CallRecord_Available_Head = psCallRecord_Start;

    return (KTgS_OK);
}


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


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


/* ---- tgPF_TL_Query_Fixed_Memory -------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgSIZE tgPF_TL_Query_Fixed_Memory( TgVOID )
{
    return (0
             + sizeof( tls_asProfile_Record )
             + sizeof( tls_aszProfile_Tag )
             + sizeof( tls_niProfile_Tag )
             + sizeof( tls_asProfile_Entry )
             + sizeof( tls_asProfile_CallRecord )
             + sizeof( tls_psProfile_CallRecord_Available_Head )
             + sizeof( tls_psProfile_CallRecord_Process_Head )
             + sizeof( tls_enProfile_TL_State )
    );
}


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

    TgERROR(nullptr != psOUT);

    iMaxProfile = tgCM_MIN_S32( tls_niProfile_Tag, KTgMAX_PROFILE_TL );
    if (iMaxProfile <= 0)
        return;

    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 = tls_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"), tls_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") );
    };
}


/* ---- tgPF_TL_Mark_Init ----------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgPROFILE_TL_ID tgPF_TL_Mark_Init( CPC_TgCHAR szName )
{
    TgSINT32                            iIndex;
    TgPROFILE_TL_ID                     tiProf;

    iIndex = tgAM32_INC( &tls_niProfile_Tag ) - 1;
    if (iIndex >= KTgMAX_PROFILE_TL)
        return (KTgPROFILE_TL_ID__INVALID);

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


/* ---- tgPF_TL_Mark_Start ---------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgPROFILE_TL_CALL_ID tgPF_TL_Mark_Start( C_TgPROFILE_TL_ID tiProf )
{
    P_STg2_Profile_TL_CallRecord        psCallRecord;
    TgSINT64                            iTime;

    TgERROR(tgPROFILE_TL_ID_Is_Valid( tiProf ));

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

    psCallRecord = tls_psProfile_CallRecord_Available_Head;
    if (nullptr == psCallRecord)
        return (KTgPROFILE_TL_CALL_ID__INVALID);

    tls_psProfile_CallRecord_Available_Head = psCallRecord->m_sID.m_sNext.m_pNext_Node;
    psCallRecord->m_tiProf = tiProf;
    psCallRecord->m_iStartOrElapsed = iTime;
    tgInit_PROFILE_TL_CALL_ID( &psCallRecord->m_sID.m_tiID, (TgUINT16)((psCallRecord - tls_asProfile_CallRecord) & KTgMAX_U16) );

    return (psCallRecord->m_sID.m_tiID);
}


/* ---- tgPF_TL_Mark_Stop ----------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgPF_TL_Mark_Stop( C_TgPROFILE_TL_ID tiProf, C_TgPROFILE_TL_CALL_ID tiProfile_CallRecord )
{
    P_STg2_Profile_TL_CallRecord        psCallRecord;
    TgSINT64                            iTime;

    TgERROR(tgPROFILE_TL_ID_Is_Valid( tiProf ));

    iTime = tgProfile_Sample_Stop( tls_asProfile_Record + tiProf.m.iI );
    if (!tgPROFILE_TL_CALL_ID_Is_Valid( tiProfile_CallRecord ))
        return;

    TgERROR_INDEX( tiProfile_CallRecord.m.iI, tls_asProfile_CallRecord );
    psCallRecord = tls_asProfile_CallRecord + tiProfile_CallRecord.m.iI;
    psCallRecord->m_iStartOrElapsed = iTime - psCallRecord->m_iStartOrElapsed;

    psCallRecord->m_sID.m_sNext.m_pNext_Node = tls_psProfile_CallRecord_Process_Head;
    tls_psProfile_CallRecord_Process_Head = psCallRecord;
}


/* ---- tgPF_TL_Mark_Reset ---------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgPF_TL_Mark_Reset( C_TgPROFILE_TL_ID tiProf )
{
    TgERROR(tgPROFILE_TL_ID_Is_Valid( tiProf ));

    tgProfile_Sample_Reset( tls_asProfile_Record + tiProf.m.iI );
    memset( tls_asProfile_Entry + tiProf.m.iI, 0x00, sizeof( tls_asProfile_Entry[0] ) );
}


/* ---- tgPF_TL_Query_Name ---------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
CP_TgCHAR tgPF_TL_Query_Name( C_TgPROFILE_TL_ID tiProf )
{
    TgERROR(tgPROFILE_TL_ID_Is_Valid( tiProf ));
    return (tls_aszProfile_Tag[tiProf.m.iI]);
}


/* ---- tgPF_TL_Query_Total_Time_Cached --------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgFLOAT64 tgPF_TL_Query_Total_Time_Cached( C_TgPROFILE_TL_ID tiProf )
{
    TgERROR(tgPROFILE_TL_ID_Is_Valid( tiProf ));
    return (tls_asProfile_Entry[tiProf.m.iI].m_fTime_Total);
}


#else

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

#endif /* TgCOMPILE_PROFILE */