⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 instance_x86core.cpp

📁 270的linux说明
💻 CPP
字号:
/*

Copyright (c) 2008, Intel Corporation. 

All rights reserved.

 

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:


    * Redistributions of source code must retain the above copyright notice, 
this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
this list of conditions and the following disclaimer in the documentation and/or 
other materials provided with the distribution.

    * Neither the name of Intel Corporation nor the names of its contributors 
may be used to endorse or promote products derived from this software without 
specific prior written permission.

 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.

*/

#include<stdio.h> 
#include "inc/framework/win2linux.h"
#include "./Instance_X86Core.h"
#include "./Instance_X86Logical.h"
//#include "HelperUtils.h"
#include "inc/base/Layer_Manager.h"
#include "inc/framework/base_ClassObject.h"
//#include "helper\core_utils.h"


//extern HelperWmi gHelperWmi;

char* MakeLower( char* szString )
{
	size_t iLen = strlen( szString );
	for ( size_t pos = 0; pos < iLen; pos++ )
		szString[pos] = tolower( szString[pos] );
	return szString;
}

DefaultX86CoreInstanceObject::~DefaultX86CoreInstanceObject(void)
{
	if (m_bUpdateFrequencyThreadRunning)
	{
		m_bUpdateFrequencyThreadRunning = false;
		HANDLE handle[1] = { m_hUpdateFrequencyThread };
		WaitForMultipleObjects( 1, handle, TRUE, 2000 );
		CloseHandle( m_hUpdateFrequencyThread );
	}
}

void DefaultX86CoreInstanceObject::OnInstanceRemoved()
{
	if (m_bUpdateFrequencyThreadRunning)
	{
		m_bUpdateFrequencyThreadRunning = false;
		HANDLE handle[1] = { m_hUpdateFrequencyThread };
		WaitForMultipleObjects( 1, handle, TRUE, 2000 );
		CloseHandle( m_hUpdateFrequencyThread );
	}
}

bool DefaultX86CoreInstanceObject::Initialize( const IntelMobileString& szKey )
{
	try {
		// convert the string key to dwAffinityMask
		DWORD dwAffinityMask = atoi( szKey.c_str() );
		if ( szKey == "0" || dwAffinityMask != 0L )  // convert successfully
		{
			__dwAffinityMask = dwAffinityMask;
			//if ( !m_Helper.Init( __dwAffinityMask ) )
			//	return false;
		}
		else
		{
			return false;
		}
	}
	catch(...) {
		THROWIMEXCEPTION("8004340C");
	}

	bool bRet = true;
	Lock();
	m_sKey = ( szKey );


	try
	{
		DWORD dwAff = 1;
		while ( dwAff <= __dwAffinityMask )
		{
			if ( ( dwAff & __dwAffinityMask ) != 0 )
			{
				ServerX86LogicalInstanceObject *pInstance = (ServerX86LogicalInstanceObject *)CreateLogicalInstance( dwAff );
				if ( pInstance == NULL )
					throw 1;
				else
					m_vLogicals.push_back( pInstance );
			}
			dwAff <<= 1;
		}
		unsigned int frequency = 0;
		//m_Helper.GetCPUFrequency( frequency );
		Frequency.SetValue( frequency );
		Unlock();
		m_bUpdateFrequencyThreadRunning = true;
		m_hUpdateFrequencyThread = CreateThread( NULL, 0, UpdateFrequencyThreadProc, (void*)this, 0, &m_dwUpdateFrequencyThreadID );
		if ( !m_hUpdateFrequencyThread )
			throw 1;
	}
	catch(...) {
		Unlock();
		THROWIMEXCEPTION("8004340C");
	}

	return bRet;

}

ServerInstanceObject* DefaultX86CoreInstanceObject::CreateLogicalInstance( DWORD dwAffinityMask )
{
	ServerInstanceObject* pObject = NULL;
	try 
	{
		char szKey[256];
		sprintf(szKey,"%d",dwAffinityMask );

		pObject = GetLayer()->GetLayerClassObject( "Logical" ).GetInstance( szKey );

		if ( pObject == NULL )
		{
			pObject = new DefaultX86LogicalInstanceObject( this, szKey, GetLayer()->GetLog() );

			if ( pObject->GetKey()[0] == NULL )
			{
				delete pObject;
				pObject = NULL;
			}
			else
			{
				ServerInstanceCollection *pCollection = GetLayer()->GetLayerClassObject( "Logical" ).GetInstances();
				if ( pCollection )
				{
					pCollection->Add( pObject );
				}
			}
		}
	}
	//catch(ServerException se) 
	//{
	//	if (se.IsBaseException()) 
	//	{
	//		ServerException nse("8004340E");
	//		nse.SetParent( &se );
	//		throw nse;
	//	}
	//	throw se;
	//}
	catch(...) 
	{
		THROWIMEXCEPTION("8004340E");
	}
    return pObject;
}

IntelMobileString	DefaultX86CoreInstanceObject::GetParent()
{
	if ( m_pParent )
		return m_pParent->GetKey();
	else
		THROWIMEXCEPTION("8004340F");
	
}

vector<IntelMobileString>	DefaultX86CoreInstanceObject::GetLogicals()
{
	vector<IntelMobileString> vt;
	try 
	{
		for ( unsigned int i=0;i<m_vLogicals.size();i++ )
			vt.push_back( m_vLogicals[i]->GetKey() );
	}
	//catch(ServerException se) 
	//{
	//	if (se.IsBaseException()) 
	//	{
	//		ServerException nse("8004340D");
	//		nse.SetParent( &se );
	//		throw nse;
	//	}
	//	throw se;
	//}
	catch(...) 
	{
		THROWIMEXCEPTION("8004340D");
	}

	return vt;
}

CacheInfoList DefaultX86CoreInstanceObject::GetCacheInfo()
{
	CacheInfoList cacheVector;
	eCacheID cacheID;
	cacheID = L1I;

	while (cacheID < ITLB)
	{
		CacheInfo cacheinfo;
		switch (cacheID)
		{
			case L1I:
				{					
					cacheinfo.CacheType = ProcessorCacheTypeEnum::L1Instruction;
					break;
				}
			case L1D:
				{
					cacheinfo.CacheType = ProcessorCacheTypeEnum::L1Data;
					break;
				}
			case L2:
				{
					cacheinfo.CacheType = ProcessorCacheTypeEnum::L2Unified;
					break;
				}
			case TC:
				{
					cacheinfo.CacheType = ProcessorCacheTypeEnum::Trace;
					break;
                }
			case L3:
				{
					cacheinfo.CacheType = ProcessorCacheTypeEnum::L3Unified;
					break;
				}
		}
		if (m_pParent->__cpuInfo.pCacheInfo[cacheID].sizeKB >=0)
            cacheinfo.Size = m_pParent->__cpuInfo.pCacheInfo[cacheID].sizeKB;
		else
			cacheinfo.Size = 0;

		if (m_pParent->__cpuInfo.pCacheInfo[cacheID].setAssoc >=0)
            cacheinfo.Associativity = m_pParent->__cpuInfo.pCacheInfo[cacheID].setAssoc;
		else
			cacheinfo.Associativity = 0;

		if (m_pParent->__cpuInfo.pCacheInfo[cacheID].lineSize >=0)
            cacheinfo.LineSize = m_pParent->__cpuInfo.pCacheInfo[cacheID].lineSize;
		else
			cacheinfo.LineSize =0;

		if (m_pParent->__cpuInfo.pCacheInfo[cacheID].lines>=0)
            cacheinfo.LineCount = m_pParent->__cpuInfo.pCacheInfo[cacheID].lines;
		else
			cacheinfo.LineCount = 0;

//		if ( cacheinfo.CacheType == ProcessorCacheTypeEnum::L2Unified && m_pParent->__cpuInfo.pCacheInfo[L2].ThreadsPerCache>0 && m_pParent->__cacheSharing.NumCaches < m_pParent->__cpuInfo.htInfo.nCores )
//           cacheinfo.Shared = true;
//		else
//  		cacheinfo.Shared = false;

		if (cacheinfo.LineCount > 0 || cacheinfo.LineSize > 0 || cacheinfo.Associativity > 0 || cacheinfo.Size > 0 )
		{
			cacheVector.push_back(cacheinfo);
		}

		cacheID = (eCacheID)(cacheID+1);

	}

	return cacheVector;
}

DWORD DefaultX86CoreInstanceObject::UpdateFrequencyThreadProc( LPVOID lpParameter )
{
	//DefaultX86CoreInstanceObject *pObject = (DefaultX86CoreInstanceObject*) lpParameter;

	//DWORD pollingRate = pObject->Frequency.GetMinPollRate();
	//unsigned int frequency = 0;

	//if ( pollingRate > CProcessorHelper::m_nTestFrequencyTime )
	//	pollingRate -= CProcessorHelper::m_nTestFrequencyTime;
	//else
	//	pollingRate = 0;

	//while ( pObject->m_bUpdateFrequencyThreadRunning )
	//{
	//	unsigned int value;
	//	if ( !pObject->m_Helper.GetCPUFrequency( value ) )
	//		value = 0;
	//	pObject->Frequency.SetValue( value );

	//	Sleep( pollingRate );
	//}
	return 1;
}

void ProcessorCoreFrequencyClass::GetValueImpl()
{
	try
	{
		if ( GetCachedValue() == 0 )
			SetNull( true );
	}
	catch(...) 
	{
		SetNull( true );
		THROWIMEXCEPTION("80043404");
	}
}

void ProcessorCoreMinFrequencyClass::GetValueImpl()
{
	this->SetNull(true);
}

void ProcessorCoreMaxFrequencyClass::GetValueImpl()
{
	try {
		DefaultX86CoreInstanceObject* pThis = (DefaultX86CoreInstanceObject*) this->_pThis;

		unsigned long freq = 0;
		if (pThis->m_pParent->__cpuInfo.bIsInitialized)
		{
			if ( pThis->m_pParent->__cpuInfo.pProcString != NULL )
			{
				char *brand = new char [strlen(pThis->m_pParent->__cpuInfo.pProcString)+1 ];
				strcpy( brand, pThis->m_pParent->__cpuInfo.pProcString );
				MakeLower( brand ); //strlwr ToLower
			

				char *pUnit = NULL;
				char *pNumStart = NULL;

				// Find 'mhz' or 'ghz'
				pUnit = strstr( brand, "mhz" );
				if ( pUnit == NULL )
					pUnit = strstr( brand, "ghz" );
				if ( pUnit != NULL )
				{
					pNumStart = pUnit - 1;
					while ( pNumStart >= brand 
					&& ( *pNumStart<= '9' && *pNumStart >= '0' || *pNumStart == '.') )
						pNumStart--;
					if ( pNumStart >= brand )
						pNumStart++;
					double tmp = atof( pNumStart );
					if ( tmp > 0.001 )
					{
						if ( *pUnit == 'm' )
							freq = (unsigned long)(tmp + 0.5);
						else
							freq = (unsigned long)(tmp * 1000 + 0.5);
					}
				}
	
				delete [] brand;
			}
		}
		if ( freq > 0 )
			this->SetValueImpl( freq );
		else
		{
			this->SetNull( true );
		}
	}
	catch(...) 
	{
		SetNull( true );
		THROWIMEXCEPTION(80043405);
	}
}

void ProcessorCoreCacheLineFlushSizeClass::GetValueImpl()
{
	try {
		DefaultX86CoreInstanceObject* pThis = (DefaultX86CoreInstanceObject*) this->_pThis;

		if (pThis->m_pParent->__cpuInfo.bIsInitialized )
		{
			if ( pThis->m_pParent->__cpuInfo.bNonIntelFlag || pThis->m_pParent->__cpuInfo.coreInfo.dwCLFlushLineSize == 0 )
				this->SetNull( true );
			else
				this->SetValueImpl( pThis->m_pParent->__cpuInfo.coreInfo.dwCLFlushLineSize*8 );
		}
		else
			THROWIMEXCEPTION("8004341D");
	}
	catch(...) 
	{
		SetNull( true );
		THROWIMEXCEPTION("8004341D");
	}
}

void ProcessorCoreDataWidthClass::GetValueImpl()
{
	//try
	//{
	//	DefaultX86CoreInstanceObject* pThis = (DefaultX86CoreInstanceObject*) this->_pThis;
	//	unsigned __int16 value;

	//	HRESULT hr = gHelperWmi.GetGenericWmiAttribute( &value, "DataWidth", pThis->m_pParent->m_nLogicalIndex );
	//	if ( hr == S_OK && value > 0 )
	//		this->SetValueImpl( value );
	//	else
	//		this->SetNull( true );
	//}
	//catch(...)
	//{
	//	SetNull( true );
	//	THROWIMEXCEPTION(80043415);
	//}
	SetNull( true );
}

void ProcessorCoreVoltageClass::GetValueImpl()
{
	//try
	//{
	//	DefaultX86CoreInstanceObject* pThis = (DefaultX86CoreInstanceObject*) this->_pThis;
	//	unsigned short int value;//__int16

	//	HRESULT hr = gHelperWmi.GetGenericWmiAttribute( &value, "CurrentVoltage", pThis->m_pParent->m_nLogicalIndex );
	//	value = value & 0x3F;

	//	if ( hr == S_OK && value > 0 )
	//		this->SetValueImpl( value );
	//	else
	//		this->SetNull( true );
	//}
	//catch(...)
	//{
	//	SetNull( true );
	//	THROWIMEXCEPTION(80043416);
	//}
	SetNull( true );
}

void ProcessorCoreMinVoltageClass::GetValueImpl()
{
	this->SetNull( true );
}

void ProcessorCoreMaxVoltageClass::GetValueImpl()
{
	this->SetNull( true );
}

void ProcessorCoreMultiplierClass::GetValueImpl()
{
	this->SetNull( true );
}

void ProcessorCoreMinMultiplierClass::GetValueImpl()
{
	this->SetNull( true );
}

void ProcessorCoreMaxMultiplierClass::GetValueImpl()
{
	this->SetNull( true );
}

void ProcessorCoreTemperatureClass::GetValueImpl()
{
	this->SetNull( true );
}

void ProcessorCoreCpuTicksClass::GetValueImpl()
{
	//try {
	//		
	//	DefaultX86CoreInstanceObject* pThis = (DefaultX86CoreInstanceObject*) this->_pThis;

	//	unsigned __int64 value;
	//	if ( pThis->m_Helper.GetCPUTicks( value ) )
	//		this->SetValueImpl( value );
	//	else
	//		this->SetNull( true );
	//}
	//catch(...) {
	//	this->SetNull( true );
	//	THROWIMEXCEPTION(80043422);
	//}
	SetNull( true );
}

void ProcessorCoreCpuUsageClass::GetValueImpl()
{
	try {
		DefaultX86CoreInstanceObject* pThis = (DefaultX86CoreInstanceObject*) this->_pThis;

		float value = 0.0f;
		int num = 0;

		for ( unsigned int i=0;i<pThis->m_vLogicals.size();i++ )
		{
			ServerX86LogicalInstanceObject *pObject = (ServerX86LogicalInstanceObject*)pThis->m_vLogicals[i];
			if ( !pObject->CpuUsage.IsNull() )
			{
				value += pObject->CpuUsage.GetValue();
				num ++;
			}
		}

		if ( num > 0 )
		{
			value /= num;
			this->SetValueImpl( value );
		}
		else
			this->SetNull( true );
	}
	catch(...) {
		this->SetNull( true );
		THROWIMEXCEPTION(80043421);
	}
}

void ProcessorCoreLogicalCountClass::GetValueImpl()
{
	try {
		DefaultX86CoreInstanceObject* pThis = (DefaultX86CoreInstanceObject*) this->_pThis;

		if (pThis->m_pParent->__cpuInfo.bIsInitialized)
		{
			this->SetValueImpl( pThis->m_pParent->__cpuInfo.htInfo.nLogicalPerCoreAvailable );
		}
		else
		{
			this->SetNull( true );
		}
	}
	catch(...) 
	{
		SetNull( true );
		THROWIMEXCEPTION(80043410);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -