Path: blob/master/src/hotspot/os/windows/pdh_interface.cpp
40931 views
/*1* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "pdh_interface.hpp"26#include "runtime/os.hpp"27#include "utilities/macros.hpp"2829// PDH API30typedef PDH_STATUS (WINAPI *PdhAddCounter_Fn)(HQUERY, LPCSTR, DWORD, HCOUNTER*);31typedef PDH_STATUS (WINAPI *PdhOpenQuery_Fn)(LPCWSTR, DWORD, HQUERY*);32typedef DWORD (WINAPI *PdhCloseQuery_Fn)(HQUERY);33typedef PDH_STATUS (WINAPI *PdhCollectQueryData_Fn)(HQUERY);34typedef DWORD (WINAPI *PdhGetFormattedCounterValue_Fn)(HCOUNTER, DWORD, LPDWORD, PPDH_FMT_COUNTERVALUE);35typedef PDH_STATUS (WINAPI *PdhEnumObjectItems_Fn)(LPCTSTR, LPCTSTR, LPCTSTR, LPTSTR, LPDWORD, LPTSTR, LPDWORD, DWORD, DWORD);36typedef PDH_STATUS (WINAPI *PdhRemoveCounter_Fn)(HCOUNTER);37typedef PDH_STATUS (WINAPI *PdhLookupPerfNameByIndex_Fn)(LPCSTR, DWORD, LPSTR, LPDWORD);38typedef PDH_STATUS (WINAPI *PdhMakeCounterPath_Fn)(PDH_COUNTER_PATH_ELEMENTS*, LPTSTR, LPDWORD, DWORD);39typedef PDH_STATUS (WINAPI *PdhExpandWildCardPath_Fn)(LPCSTR, LPCSTR, PZZSTR, LPDWORD, DWORD);4041PdhAddCounter_Fn PdhDll::_PdhAddCounter = NULL;42PdhOpenQuery_Fn PdhDll::_PdhOpenQuery = NULL;43PdhCloseQuery_Fn PdhDll::_PdhCloseQuery = NULL;44PdhCollectQueryData_Fn PdhDll::_PdhCollectQueryData = NULL;45PdhGetFormattedCounterValue_Fn PdhDll::_PdhGetFormattedCounterValue = NULL;46PdhEnumObjectItems_Fn PdhDll::_PdhEnumObjectItems = NULL;47PdhRemoveCounter_Fn PdhDll::_PdhRemoveCounter = NULL;48PdhLookupPerfNameByIndex_Fn PdhDll::_PdhLookupPerfNameByIndex = NULL;49PdhMakeCounterPath_Fn PdhDll::_PdhMakeCounterPath = NULL;50PdhExpandWildCardPath_Fn PdhDll::_PdhExpandWildCardPath = NULL;5152LONG PdhDll::_critical_section = 0;53LONG PdhDll::_initialized = 0;54LONG PdhDll::_pdh_reference_count = 0;55HMODULE PdhDll::_hModule = NULL;5657void PdhDll::initialize(void) {58_hModule = os::win32::load_Windows_dll("pdh.dll", NULL, 0);59if (NULL == _hModule) {60return;61}62// The 'A' at the end means the ANSI (not the UNICODE) vesions of the methods63_PdhAddCounter = (PdhAddCounter_Fn)::GetProcAddress(_hModule, "PdhAddCounterA");64_PdhOpenQuery = (PdhOpenQuery_Fn)::GetProcAddress(_hModule, "PdhOpenQueryA");65_PdhCloseQuery = (PdhCloseQuery_Fn)::GetProcAddress(_hModule, "PdhCloseQuery");66_PdhCollectQueryData = (PdhCollectQueryData_Fn)::GetProcAddress(_hModule, "PdhCollectQueryData");67_PdhGetFormattedCounterValue = (PdhGetFormattedCounterValue_Fn)::GetProcAddress(_hModule, "PdhGetFormattedCounterValue");68_PdhEnumObjectItems = (PdhEnumObjectItems_Fn)::GetProcAddress(_hModule, "PdhEnumObjectItemsA");69_PdhRemoveCounter = (PdhRemoveCounter_Fn)::GetProcAddress(_hModule, "PdhRemoveCounter");70_PdhLookupPerfNameByIndex = (PdhLookupPerfNameByIndex_Fn)::GetProcAddress(_hModule, "PdhLookupPerfNameByIndexA");71_PdhMakeCounterPath = (PdhMakeCounterPath_Fn)::GetProcAddress(_hModule, "PdhMakeCounterPathA");72_PdhExpandWildCardPath = (PdhExpandWildCardPath_Fn)::GetProcAddress(_hModule, "PdhExpandWildCardPathA");73InterlockedExchange(&_initialized, 1);74}7576bool PdhDll::PdhDetach(void) {77LONG prev_ref_count = InterlockedExchangeAdd(&_pdh_reference_count, -1);78BOOL ret = false;79if (1 == prev_ref_count) {80if (_initialized && _hModule != NULL) {81ret = FreeLibrary(_hModule);82if (ret) {83_hModule = NULL;84_PdhAddCounter = NULL;85_PdhOpenQuery = NULL;86_PdhCloseQuery = NULL;87_PdhCollectQueryData = NULL;88_PdhGetFormattedCounterValue = NULL;89_PdhEnumObjectItems = NULL;90_PdhRemoveCounter = NULL;91_PdhLookupPerfNameByIndex = NULL;92_PdhMakeCounterPath = NULL;93_PdhExpandWildCardPath = NULL;94InterlockedExchange(&_initialized, 0);95}96}97}98return ret != 0;99}100101bool PdhDll::PdhAttach(void) {102InterlockedExchangeAdd(&_pdh_reference_count, 1);103if (1 == _initialized) {104return true;105}106while (InterlockedCompareExchange(&_critical_section, 1, 0) == 1);107if (0 == _initialized) {108initialize();109}110while (InterlockedCompareExchange(&_critical_section, 0, 1) == 0);111return (_PdhAddCounter != NULL && _PdhOpenQuery != NULL112&& _PdhCloseQuery != NULL && PdhCollectQueryData != NULL113&& _PdhGetFormattedCounterValue != NULL && _PdhEnumObjectItems != NULL114&& _PdhRemoveCounter != NULL && PdhLookupPerfNameByIndex != NULL115&& _PdhMakeCounterPath != NULL && _PdhExpandWildCardPath != NULL);116}117118PDH_STATUS PdhDll::PdhAddCounter(HQUERY hQuery, LPCSTR szFullCounterPath, DWORD dwUserData, HCOUNTER* phCounter) {119assert(_initialized && _PdhAddCounter != NULL, "PdhAvailable() not yet called");120return _PdhAddCounter(hQuery, szFullCounterPath, dwUserData, phCounter);121}122123PDH_STATUS PdhDll::PdhOpenQuery(LPCWSTR szDataSource, DWORD dwUserData, HQUERY* phQuery) {124assert(_initialized && _PdhOpenQuery != NULL, "PdhAvailable() not yet called");125return _PdhOpenQuery(szDataSource, dwUserData, phQuery);126}127128DWORD PdhDll::PdhCloseQuery(HQUERY hQuery) {129assert(_initialized && _PdhCloseQuery != NULL, "PdhAvailable() not yet called");130return _PdhCloseQuery(hQuery);131}132133PDH_STATUS PdhDll::PdhCollectQueryData(HQUERY hQuery) {134assert(_initialized && _PdhCollectQueryData != NULL, "PdhAvailable() not yet called");135return _PdhCollectQueryData(hQuery);136}137138DWORD PdhDll::PdhGetFormattedCounterValue(HCOUNTER hCounter, DWORD dwFormat, LPDWORD lpdwType, PPDH_FMT_COUNTERVALUE pValue) {139assert(_initialized && _PdhGetFormattedCounterValue != NULL, "PdhAvailable() not yet called");140return _PdhGetFormattedCounterValue(hCounter, dwFormat, lpdwType, pValue);141}142143PDH_STATUS PdhDll::PdhEnumObjectItems(LPCTSTR szDataSource, LPCTSTR szMachineName, LPCTSTR szObjectName,144LPTSTR mszCounterList, LPDWORD pcchCounterListLength, LPTSTR mszInstanceList,145LPDWORD pcchInstanceListLength, DWORD dwDetailLevel, DWORD dwFlags) {146assert(_initialized && _PdhEnumObjectItems != NULL, "PdhAvailable() not yet called");147return _PdhEnumObjectItems(szDataSource, szMachineName, szObjectName, mszCounterList, pcchCounterListLength,148mszInstanceList, pcchInstanceListLength, dwDetailLevel, dwFlags);149}150151PDH_STATUS PdhDll::PdhRemoveCounter(HCOUNTER hCounter) {152assert(_initialized && _PdhRemoveCounter != NULL, "PdhAvailable() not yet called");153return _PdhRemoveCounter(hCounter);154}155156PDH_STATUS PdhDll::PdhLookupPerfNameByIndex(LPCSTR szMachineName, DWORD dwNameIndex, LPSTR szNameBuffer, LPDWORD pcchNameBufferSize) {157assert(_initialized && _PdhLookupPerfNameByIndex != NULL, "PdhAvailable() not yet called");158return _PdhLookupPerfNameByIndex(szMachineName, dwNameIndex, szNameBuffer, pcchNameBufferSize);159}160161PDH_STATUS PdhDll::PdhMakeCounterPath(PDH_COUNTER_PATH_ELEMENTS* pCounterPathElements, LPTSTR szFullPathBuffer, LPDWORD pcchBufferSize, DWORD dwFlags) {162assert(_initialized && _PdhMakeCounterPath != NULL, "PdhAvailable() not yet called");163return _PdhMakeCounterPath(pCounterPathElements, szFullPathBuffer, pcchBufferSize, dwFlags);164}165166PDH_STATUS PdhDll::PdhExpandWildCardPath(LPCSTR szDataSource, LPCSTR szWildCardPath, PZZSTR mszExpandedPathList, LPDWORD pcchPathListLength, DWORD dwFlags) {167assert(_initialized && PdhExpandWildCardPath != NULL, "PdhAvailable() not yet called");168return _PdhExpandWildCardPath(szDataSource, szWildCardPath, mszExpandedPathList, pcchPathListLength, dwFlags);169}170171bool PdhDll::PdhStatusFail(PDH_STATUS pdhStat) {172return pdhStat != ERROR_SUCCESS && pdhStat != PDH_MORE_DATA;173}174175176