/****************************************************************************/
/*!

  @file        SysView_ReaderWriterLockStatistics.cpp

  -------------------------------------------------------------------------

  @author      Robin

  @ingroup     SystemViews
  @brief       This module implements the "ReaderWriterLockStaitstics" view class.

  @see        

*/
/*-------------------------------------------------------------------------

  copyright:    Copyright (c) 2002-2005 SAP AG


    ========== licence begin  GPL
    Copyright (c) 2002-2005 SAP AG

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
    ========== licence end




*****************************************************************************/



/*===========================================================================*
 *  INCLUDES                                                                 *
 *===========================================================================*/

#include "ggg00.h"
#include "SystemViews/SysView_Defines.hpp"
#include "SystemViews/SysView_ITableObj.hpp"
#include "SystemViews/SysView_ReaderWriterLockStatistics.hpp"
#include "SQLManager/SQLMan_Context.hpp"
#include "RunTime/Synchronisation/RTESync_RWRegion.hpp"
#include "SAPDBCommon/SAPDB_sprintf.h"
#include "RunTime/MemoryManagement/RTEMem_RteAllocator.hpp"

/*===========================================================================*
 *  DEFINES                                                                  *
 *===========================================================================*/



/*===========================================================================*
 *  MACROS                                                                   *
 *===========================================================================*/



/*===========================================================================*
 *  LOCAL CLASSES, STRUCTURES, TYPES, UNIONS ...                             *
 *===========================================================================*/



/*===========================================================================*
 *  STATIC/INLINE FUNCTIONS  (PROTOTYPES)                                     *
 *===========================================================================*/



/*===========================================================================*
 *  METHODS                                                                  *
 *===========================================================================*/

void SysView_ReaderWriterLockStatistics::Create(void)
{
    SAPDBTRACE_METHOD_DEBUG ("SysView_ReaderWriterLockStatistics::Create", SysView_Trace, 5);

	m_Table->AppendCol (ITOCT_CHAR, SV_ADDRESS, 20);
	m_Table->AppendCol (ITOCT_CHAR, SV_SPINLOCKPOOLNAME, 40);
    m_Table->AppendCol (ITOCT_CHAR, SV_LASTENTEREDTASKID, 10);
    m_Table->AppendCol (ITOCT_FIXED, SV_EXCLUSIVEACCESSCOUNT, 20);
	m_Table->AppendCol (ITOCT_FIXED, SV_NONEXCLUSIVEACCESSCOUNT, 20);
	m_Table->AppendCol (ITOCT_FIXED, SV_COLLISIONCOUNT, 20);
	m_Table->AppendCol (ITOCT_FIXED, SV_WAITCOUNT, 20);
	m_Table->AppendCol (ITOCT_FIXED, SV_SPINLOCKCOLLISIONCOUNT, 20);
#ifdef ADD_WAITERS_
    m_Table->AppendCol (ITOCT_CHAR,  SV_WAITINGFORTASK, 40);
#endif
}

/*---------------------------------------------------------------------------*/

SAPDB_Int SysView_ReaderWriterLockStatistics::GetColCount(void)
{
    SAPDBTRACE_METHOD_DEBUG ("SysView_ReaderWriterLockStatistics::GetColCount", SysView_Trace, 5);

	return SV_CC_READERWRITERLOCKSTATISTICS;
}

/*---------------------------------------------------------------------------*/

SAPDB_Int SysView_ReaderWriterLockStatistics::EstimateRows(void)
{
    SAPDBTRACE_METHOD_DEBUG ("SysView_ReaderWriterLockStatistics::EstimateRows", SysView_Trace, 5);

	return SV_ER_READERWRITERLOCKSTATISTICS;
}

/*---------------------------------------------------------------------------*/

void SysView_ReaderWriterLockStatistics::Execute(void)
{
    SAPDBTRACE_METHOD_DEBUG ("SysView_ReaderWriterLockStatistics::Execute", SysView_Trace, 5);

    RTESync_IRWRegionReader *pReader;

    SAPDB_UTF8              spinLockPoolName[44];
    SAPDB_UInt8             uint8val;
    SAPDB_Int4              int4val;
#ifdef ADD_WAITERS_
    SAPDB_Char              waitingTasks[44];
#endif

    m_Table->GetCatalogTable();

    pReader = RTESync_GetRWRegionReader(RTEMem_RteAllocator::Instance());

    if (pReader->First())
    {
        while(pReader->Next() && (m_Context.IsOk()))
        {
            pReader->Address(uint8val);
            SAPDB_ToStringClass address = SAPDB_ToString(uint8val,SAPDB_ToStringClass::hex,SAPDB_ToStringClass::showbase);
            m_Table->MoveToCol (ITOVT_CHARPTR, (SAPDB_Char *)address,address.Length());
            
            pReader->SpinlockPoolName (spinLockPoolName);
            m_Table->MoveToCol (ITOVT_CHARPTR, spinLockPoolName,(SAPDB_Int)strlen((SAPDB_Char *)spinLockPoolName));

            pReader->LastEnteredTask(int4val);
            if(-1 == int4val)
            {
                m_Table->MoveToCol (ITOVT_CHARPTR, "none",(SAPDB_Int)strlen((SAPDB_Char *)"none"));
            }
            else
            {
                SAPDB_ToStringClass taskId = SAPDB_ToString(int4val);
                m_Table->MoveToCol (ITOVT_CHARPTR, (SAPDB_Char *)taskId, taskId.Length());
            }

            pReader->ExclusiveAccessCount(uint8val);
            m_Table->MoveToCol (ITOVT_INT8, &uint8val, 0);
    
            pReader->NonExclusiveAccessCount(uint8val);
            m_Table->MoveToCol (ITOVT_INT8, &uint8val, 0);

            pReader->CollisionCount(uint8val);
            m_Table->MoveToCol (ITOVT_INT8, &uint8val, 0);

            pReader->WaitCount(uint8val);
            m_Table->MoveToCol (ITOVT_INT8, &uint8val, 0);

            pReader->SpinLockCollisionCount(uint8val);
            m_Table->MoveToCol (ITOVT_INT8, &uint8val, 0);
#ifdef ADD_WAITERS_
            pReader->Waiters (waitingTasks);
            m_Table->MoveToCol (ITOVT_CHARPTR, waitingTasks,(SAPDB_Int)strlen((SAPDB_Char *)waitingTasks));
#endif
        }
    }
    (RTEMem_RteAllocator::Instance()).Deallocate(pReader);
        
}

/*===========================================================================*
 *  END OF CODE                                                              *
 *===========================================================================*/
