📄 instance_x86processor.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.
*///==============================================================================// Instance_Processor.cpp//// //==============================================================================//#include "stdafx.h"#include "inc/framework/win2linux.h"#include "inc/framework/base_ClassObject.h"#include "inc/base/Layer_Manager.h"#include "Instance_X86Processor.h"#include "inc/framework/base_Enum.h"#include "wchar.h"#include "Instance_X86Logical.h"#include "Instance_X86Core.h"//==============================================================================//// DefaultX86ProcessorInstanceObject implementation////==============================================================================bool DefaultX86ProcessorInstanceObject::Initialize( const IntelMobileString& szKey ){ try { // convert the string key to dwAffinityMask#ifdef _UNICODE DWORD dwAffinityMask = wcstol( szKey.c_str(), (IntelMobileChar **)NULL, 0 );#else DWORD dwAffinityMask = strtol( szKey.c_str(), (IntelMobileChar **)NULL, 0 );#endif if ( szKey == IntelMobileText("0") || dwAffinityMask != 0L ) // convert successfully { __dwAffinityMask = dwAffinityMask; __dwSavedAffinityMask = (DWORD)-1; } else { return false; } } catch(...) { THROWIMEXCEPTION("80043001"); } CPUINFO_T cpuInfo = CPUINFO_T_INIT_VAL; // use this var instead of the member var __cpuInfo so that CPUINFO_T_INIT_VAL can be used to initialize before calling WinCPUID_Init _Log( IntelMobileText("Initialize the processor" ) ); try { Lock(); Id.SetValue( szKey ); m_sKey = ( szKey ); SetAffinity(); // Ensure info comes from the correct processor CPUINFO_T cpuInfo = CPUINFO_T_INIT_VAL; cpuInfo.pCacheInfo = new CACHEINFO_T[NCACHETYPES]; if (NULL == cpuInfo.pCacheInfo) { Unlock(); return false; } if(cpuInfo.pCacheInfo) ZeroMemory(cpuInfo.pCacheInfo,sizeof(CACHEINFO_T)*NCACHETYPES); cpuInfo.htInfo.nMaxPhysGetProcAff = 8; cpuInfo.htInfo.pPhysProcAff = new PHYSPROCAFF_T[cpuInfo.htInfo.nMaxPhysGetProcAff]; if (cpuInfo.htInfo.pPhysProcAff) ZeroMemory(cpuInfo.htInfo.pPhysProcAff, sizeof(PHYSPROCAFF_T)*cpuInfo.htInfo.nMaxPhysGetProcAff); cpuInfo.htInfo.nMaxCoreGetProcAff = 8; for (int i=0; i < cpuInfo.htInfo.nMaxPhysGetProcAff; i++) { cpuInfo.htInfo.pPhysProcAff[i].pCoreProcAff = new COREPROCAFF_T[cpuInfo.htInfo.nMaxCoreGetProcAff]; if (cpuInfo.htInfo.pPhysProcAff[i].pCoreProcAff) ZeroMemory(cpuInfo.htInfo.pPhysProcAff[i].pCoreProcAff, sizeof(COREPROCAFF_T)*cpuInfo.htInfo.nMaxCoreGetProcAff); } cpuInfo.pProcString = new char [PROCSTRING_BYTES]; // need implement on Linux LinCPUID_Init(0, &cpuInfo); // Get the processor info __cpuInfo = cpuInfo; /* * Set Cores and Logical */ printf("begin to Create the X86ProcessorCoreInstanceObject I \r\n"); int i = 0; printf("__cpuInfo.htInfo.nPhysicalProcs = %d \r\n",__cpuInfo.htInfo.nPhysicalProcs); for ( i=0;i<__cpuInfo.htInfo.nPhysicalProcs;i++ ) { printf("____dwAffinityMask = %d,dest = %d\r\n",__dwAffinityMask,(__cpuInfo.htInfo.pPhysProcAff[i].dwPhysAffinity | __cpuInfo.htInfo.pPhysProcAff[i].dwLogAffinity) ); if ( __dwAffinityMask == (__cpuInfo.htInfo.pPhysProcAff[i].dwPhysAffinity | __cpuInfo.htInfo.pPhysProcAff[i].dwLogAffinity) ) break; } if ( i >= __cpuInfo.htInfo.nPhysicalProcs ) { //Unlock(); //THROWIMEXCEPTION("80043001"); Unlock(); return true; } printf("begin to Create the X86ProcessorCoreInstanceObject II, i = %d \r\n",i); for ( int j=0;j<__cpuInfo.htInfo.pPhysProcAff[i].nCores;j++ ) { ServerX86CoreInstanceObject *pInstance = (ServerX86CoreInstanceObject *)CreateCoreInstance( (DWORD)(__cpuInfo.htInfo.pPhysProcAff[i].pCoreProcAff[j].dwCoreAffinity | __cpuInfo.htInfo.pPhysProcAff[i].pCoreProcAff[j].dwLogAffinity) ); if ( pInstance == NULL ) throw 2; else m_vCores.push_back( pInstance ); } //memset( &__cacheSharing, 0, sizeof(__cacheSharing) ); //__cacheSharing.pSharingList = new CACHESHARINGELEMENT_T[8]; //this->m_nLogicalIndex = this->__cpuInfo.htInfo.pPhysProcAff[i].physID * this->__cpuInfo.htInfo.nLogicalPerPackage; // ResetAffinity(); // set the processor affinity back _Log( IntelMobileText("Processor Init complete." ) ); Unlock(); } catch(...) { Unlock(); THROWIMEXCEPTION("80043001"); } return true; }ServerInstanceObject* DefaultX86ProcessorInstanceObject::CreateCoreInstance( DWORD dwAffinityMask ){ ServerInstanceObject* pObject = NULL; try { char szKey[256]; sprintf(szKey,"%d",dwAffinityMask); pObject = GetLayer()->GetLayerClassObject( "Core" ).GetInstance( szKey ); if ( pObject == NULL ) { printf("Begin to Create a CoreInstanceObject,key = %s \r\n", szKey); pObject = new DefaultX86CoreInstanceObject( this, szKey, GetLayer()->GetLog() ); if ( pObject->GetKey()[0] == NULL ) { delete pObject; pObject = NULL; } else { ServerInstanceCollection *pCollection = GetLayer()->GetLayerClassObject( "Core" ).GetInstances(); if ( pCollection ) { pCollection->Add( pObject ); } } } } //catch(ServerException se) //{ // if (se.IsBaseException()) // { // ServerException nse(L"80043403"); // nse.SetParent( &se ); // throw nse; // } // throw se; //} catch(...) { THROWIMEXCEPTION("80043403"); } return pObject;}void DefaultX86ProcessorInstanceObject::SetAffinity(){ HANDLE hProcess = NULL; DWORD dwSystemAffinityMask; if (-1 != __dwAffinityMask) { hProcess = GetCurrentProcess(); if (hProcess) { // Save the current affinity mask and reset it to this instance's affinity mask //printf("Before GetProcessAffinityMask\n"); if (GetProcessAffinityMask(hProcess, &__dwSavedAffinityMask, &dwSystemAffinityMask)){ //printf("thread schedule mask: %x\n", __dwAffinityMask); SetProcessAffinityMask(hProcess, __dwAffinityMask); } else { //printf("GetProcessAffinityMask error\n"); THROWIMEXCEPTION("80043002"); } } else { THROWIMEXCEPTION("80043003"); } }}void DefaultX86ProcessorInstanceObject::ResetAffinity(){ HANDLE hProcess = NULL; if (-1 != __dwSavedAffinityMask) { hProcess = GetCurrentProcess(); if (hProcess) SetProcessAffinityMask(hProcess, __dwSavedAffinityMask); else THROWIMEXCEPTION("80043003"); }}DefaultX86ProcessorInstanceObject::~DefaultX86ProcessorInstanceObject(){ //_Log( LOG_DEBUG, IntelMobileText("Trace: DefaultX86ProcessorInstanceObject::~DefaultX86ProcessorInstanceObject()") ); if ( __cpuInfo.pCacheInfo ) delete [] __cpuInfo.pCacheInfo; if ( __cpuInfo.pProcString ) delete [] __cpuInfo.pProcString;}vector<IntelMobileString> DefaultX86ProcessorInstanceObject::GetLogicals(){ vector<IntelMobileString> vt; try { size_t nCores = m_vCores.size(); for ( unsigned int i=0;i<nCores;i++ ) { DefaultX86CoreInstanceObject *pCore = (DefaultX86CoreInstanceObject*)m_vCores[i]; size_t nLogicals = pCore->m_vLogicals.size(); for ( unsigned int j=0;j<nLogicals;j++ ) vt.push_back( pCore->m_vLogicals[j]->GetKey() ); } } //catch(ServerException se) //{ // if (se.IsBaseException()) // { // ServerException nse(L"80043402"); // nse.SetParent( &se ); // throw nse; // } // throw se; //} catch(...) { THROWIMEXCEPTION("80043402"); } return vt;}vector<IntelMobileString> DefaultX86ProcessorInstanceObject::GetCores(){ vector<IntelMobileString> vt; try { for ( unsigned int i=0;i<m_vCores.size();i++ ) vt.push_back( m_vCores[i]->GetKey() ); } //catch(ServerException se) //{ // if (se.IsBaseException()) // { // ServerException nse(L"80043404"); // nse.SetParent( &se ); // throw nse; // } // throw se; //} catch(...) { THROWIMEXCEPTION("80043404"); } return vt;}CacheInfoList DefaultX86ProcessorInstanceObject::GetCacheInfo(){ CacheInfoList cacheVector; eCacheID cacheID; cacheID = L1I; while (cacheID < ITLB) { CacheInfo cacheinfo; switch (cacheID) { case L1I: { cacheinfo.CacheType = L1Instruction; break; } case L1D: { cacheinfo.CacheType = L1Data; break; } case L2: { cacheinfo.CacheType = L2Unified; break; } case TC: { cacheinfo.CacheType = Trace; break; } case L3: { cacheinfo.CacheType = L3Unified; break; } } if (__cpuInfo.pCacheInfo[cacheID].sizeKB >=0) cacheinfo.Size = __cpuInfo.pCacheInfo[cacheID].sizeKB; else cacheinfo.Size = 0; if (__cpuInfo.pCacheInfo[cacheID].setAssoc >=0) cacheinfo.Associativity = __cpuInfo.pCacheInfo[cacheID].setAssoc; else cacheinfo.Associativity = 0; if (__cpuInfo.pCacheInfo[cacheID].lineSize >=0) cacheinfo.LineSize = __cpuInfo.pCacheInfo[cacheID].lineSize; else cacheinfo.LineSize =0; if (__cpuInfo.pCacheInfo[cacheID].lines>=0) cacheinfo.LineCount = __cpuInfo.pCacheInfo[cacheID].lines; else cacheinfo.LineCount = 0; if (cacheinfo.LineCount > 0 || cacheinfo.LineSize > 0 || cacheinfo.Associativity > 0 || cacheinfo.Size > 0 ) { cacheVector.push_back(cacheinfo); } cacheID = (eCacheID)(cacheID+1); } return cacheVector;}//==============================================================================//==============================================================================//// Processor properties////==============================================================================void ProcessorManufacturerClass::GetValueImpl(){ DefaultX86ProcessorInstanceObject* pThis = (DefaultX86ProcessorInstanceObject*) this->_pThis; if (pThis->__cpuInfo.bIsInitialized) { switch (pThis->__cpuInfo.eCPUVendor) { case 0: this->SetValueImpl( IntelMobileText("Unknown") ); break; case 1: this->SetValueImpl( IntelMobileText("Intel") ); break; case 2: this->SetValueImpl( IntelMobileText("AMD") ); this->SetNull( true ); break; default: break; } } else { //raise error //TODO this->SetNull( true ); }}void ProcessorBrandClass::GetValueImpl(){ try { //need implement char brand[256]; IntelMobileChar value[256]; DefaultX86ProcessorInstanceObject* pThis = (DefaultX86ProcessorInstanceObject*) this->_pThis; if (pThis->__cpuInfo.bIsInitialized) { if ( (*pThis->__cpuInfo.pProcString) != 0 ) strcpy( brand, pThis->__cpuInfo.pProcString ); else LinCPUID_getBrandStrEx( brand, sizeof(brand), &(pThis->__cpuInfo.coreInfo) );#ifdef _UNICODE mbstowcs(value, brand, 256);#else strncpy(value, brand, 256);#endif this->SetValueImpl( value ); //::SysFreeString( value ); //Leak fixed. } else { //raise error //TODO this->SetNull( true ); } } catch(...) { THROWIMEXCEPTION("80043006"); }}/*void ProcessorHTSupportedDescriptionClass::GetValueImpl(){ try { char desc[256]; BSTR value = NULL; DefaultX86ProcessorInstanceObject* pThis = (DefaultX86ProcessorInstanceObject*) this->_pThis; if (pThis->__cpuInfo.bIsInitialized) { WinCPUID_getHTResultStr( desc, sizeof(desc), pThis->__cpuInfo.htInfo.htResultCode ); value = _com_util::ConvertStringToBSTR( desc ); this->SetValueImpl( static_cast<IntelMobileChar*>( value ) ); ::SysFreeString( value ); //Leak fixed. } else { //raise error //TODO this->SetNull( true ); } } catch(...) { THROWIMEXCEPTION(80043008); }}*/void ProcessorRoleClass::GetValueImpl(){ this->SetNull( true );}void ProcessorFsbClass::GetValueImpl(){ //try //{ // DefaultX86ProcessorInstanceObject* pThis = (DefaultX86ProcessorInstanceObject*) this->_pThis; // unsigned __int32 value = 0; // HRESULT hr = gHelperWmi.GetGenericWmiAttribute( &value, "ExtClock", pThis->m_nLogicalIndex ); // if ( hr == S_OK && value > 0 ) // this->SetValueImpl( value ); // else // this->SetNull( true ); //} //catch(...) //{ // SetNull( true ); // THROWIMEXCEPTION(80043414); //} this->SetNull( true );}void ProcessorCpuUsageClass::GetValueImpl(){ try { DefaultX86ProcessorInstanceObject* pThis = (DefaultX86ProcessorInstanceObject*) this->_pThis; float value = 0.0f; int num = 0; for ( unsigned int i=0;i<pThis->m_vCores.size();i++ ) { ServerX86CoreInstanceObject *pObject = (ServerX86CoreInstanceObject*)pThis->m_vCores[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("80043420"); }}void ProcessorLogicalCountClass::GetValueImpl(){ try { DefaultX86ProcessorInstanceObject* pThis = (DefaultX86ProcessorInstanceObject*) this->_pThis; if (pThis->__cpuInfo.bIsInitialized) { this->SetValueImpl( pThis->__cpuInfo.htInfo.nLogicalPerCoreAvailable * pThis->__cpuInfo.htInfo.nCoresAvailable ); } else { this->SetNull( true ); } } catch(...) { SetNull( true ); THROWIMEXCEPTION("8004340B"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -