Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/os/windows/vm/pdh_interface.cpp
32285 views
/*1* Copyright (c) 2012, 2018, 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);3940PdhAddCounter_Fn PdhDll::_PdhAddCounter = NULL;41PdhOpenQuery_Fn PdhDll::_PdhOpenQuery = NULL;42PdhCloseQuery_Fn PdhDll::_PdhCloseQuery = NULL;43PdhCollectQueryData_Fn PdhDll::_PdhCollectQueryData = NULL;44PdhGetFormattedCounterValue_Fn PdhDll::_PdhGetFormattedCounterValue = NULL;45PdhEnumObjectItems_Fn PdhDll::_PdhEnumObjectItems = NULL;46PdhRemoveCounter_Fn PdhDll::_PdhRemoveCounter = NULL;47PdhLookupPerfNameByIndex_Fn PdhDll::_PdhLookupPerfNameByIndex = NULL;48PdhMakeCounterPath_Fn PdhDll::_PdhMakeCounterPath = NULL;4950LONG PdhDll::_critical_section = 0;51LONG PdhDll::_initialized = 0;52LONG PdhDll::_pdh_reference_count = 0;53HMODULE PdhDll::_hModule = NULL;5455void PdhDll::initialize(void) {56_hModule = os::win32::load_Windows_dll("pdh.dll", NULL, 0);57if (NULL == _hModule) {58return;59}60// The 'A' at the end means the ANSI (not the UNICODE) vesions of the methods61_PdhAddCounter = (PdhAddCounter_Fn)::GetProcAddress(_hModule, "PdhAddCounterA");62_PdhOpenQuery = (PdhOpenQuery_Fn)::GetProcAddress(_hModule, "PdhOpenQueryA");63_PdhCloseQuery = (PdhCloseQuery_Fn)::GetProcAddress(_hModule, "PdhCloseQuery");64_PdhCollectQueryData = (PdhCollectQueryData_Fn)::GetProcAddress(_hModule, "PdhCollectQueryData");65_PdhGetFormattedCounterValue = (PdhGetFormattedCounterValue_Fn)::GetProcAddress(_hModule, "PdhGetFormattedCounterValue");66_PdhEnumObjectItems = (PdhEnumObjectItems_Fn)::GetProcAddress(_hModule, "PdhEnumObjectItemsA");67_PdhRemoveCounter = (PdhRemoveCounter_Fn)::GetProcAddress(_hModule, "PdhRemoveCounter");68_PdhLookupPerfNameByIndex = (PdhLookupPerfNameByIndex_Fn)::GetProcAddress(_hModule, "PdhLookupPerfNameByIndexA");69_PdhMakeCounterPath = (PdhMakeCounterPath_Fn)::GetProcAddress(_hModule, "PdhMakeCounterPathA");70InterlockedExchange(&_initialized, 1);71}7273bool PdhDll::PdhDetach(void) {74LONG prev_ref_count = InterlockedExchangeAdd(&_pdh_reference_count, -1);75BOOL ret = false;76if (1 == prev_ref_count) {77if (_initialized && _hModule != NULL) {78ret = FreeLibrary(_hModule);79if (ret) {80_hModule = NULL;81_PdhAddCounter = NULL;82_PdhOpenQuery = NULL;83_PdhCloseQuery = NULL;84_PdhCollectQueryData = NULL;85_PdhGetFormattedCounterValue = NULL;86_PdhEnumObjectItems = NULL;87_PdhRemoveCounter = NULL;88_PdhLookupPerfNameByIndex = NULL;89_PdhMakeCounterPath = NULL;90InterlockedExchange(&_initialized, 0);91}92}93}94return ret != 0;95}9697bool PdhDll::PdhAttach(void) {98InterlockedExchangeAdd(&_pdh_reference_count, 1);99if (1 == _initialized) {100return true;101}102while (InterlockedCompareExchange(&_critical_section, 1, 0) == 1);103if (0 == _initialized) {104initialize();105}106while (InterlockedCompareExchange(&_critical_section, 0, 1) == 0);107return (_PdhAddCounter != NULL && _PdhOpenQuery != NULL108&& _PdhCloseQuery != NULL && PdhCollectQueryData != NULL109&& _PdhGetFormattedCounterValue != NULL && _PdhEnumObjectItems != NULL110&& _PdhRemoveCounter != NULL && PdhLookupPerfNameByIndex != NULL111&& _PdhMakeCounterPath != NULL);112}113114PDH_STATUS PdhDll::PdhAddCounter(HQUERY hQuery, LPCSTR szFullCounterPath, DWORD dwUserData, HCOUNTER* phCounter) {115assert(_initialized && _PdhAddCounter != NULL, "PdhAvailable() not yet called");116return _PdhAddCounter(hQuery, szFullCounterPath, dwUserData, phCounter);117}118119PDH_STATUS PdhDll::PdhOpenQuery(LPCWSTR szDataSource, DWORD dwUserData, HQUERY* phQuery) {120assert(_initialized && _PdhOpenQuery != NULL, "PdhAvailable() not yet called");121return _PdhOpenQuery(szDataSource, dwUserData, phQuery);122}123124DWORD PdhDll::PdhCloseQuery(HQUERY hQuery) {125assert(_initialized && _PdhCloseQuery != NULL, "PdhAvailable() not yet called");126return _PdhCloseQuery(hQuery);127}128129PDH_STATUS PdhDll::PdhCollectQueryData(HQUERY hQuery) {130assert(_initialized && _PdhCollectQueryData != NULL, "PdhAvailable() not yet called");131return _PdhCollectQueryData(hQuery);132}133134DWORD PdhDll::PdhGetFormattedCounterValue(HCOUNTER hCounter, DWORD dwFormat, LPDWORD lpdwType, PPDH_FMT_COUNTERVALUE pValue) {135assert(_initialized && _PdhGetFormattedCounterValue != NULL, "PdhAvailable() not yet called");136return _PdhGetFormattedCounterValue(hCounter, dwFormat, lpdwType, pValue);137}138139PDH_STATUS PdhDll::PdhEnumObjectItems(LPCTSTR szDataSource, LPCTSTR szMachineName, LPCTSTR szObjectName,140LPTSTR mszCounterList, LPDWORD pcchCounterListLength, LPTSTR mszInstanceList,141LPDWORD pcchInstanceListLength, DWORD dwDetailLevel, DWORD dwFlags) {142assert(_initialized && _PdhEnumObjectItems != NULL, "PdhAvailable() not yet called");143return _PdhEnumObjectItems(szDataSource, szMachineName, szObjectName, mszCounterList, pcchCounterListLength,144mszInstanceList, pcchInstanceListLength, dwDetailLevel, dwFlags);145}146147PDH_STATUS PdhDll::PdhRemoveCounter(HCOUNTER hCounter) {148assert(_initialized && _PdhRemoveCounter != NULL, "PdhAvailable() not yet called");149return _PdhRemoveCounter(hCounter);150}151152PDH_STATUS PdhDll::PdhLookupPerfNameByIndex(LPCSTR szMachineName, DWORD dwNameIndex, LPSTR szNameBuffer, LPDWORD pcchNameBufferSize) {153assert(_initialized && _PdhLookupPerfNameByIndex != NULL, "PdhAvailable() not yet called");154return _PdhLookupPerfNameByIndex(szMachineName, dwNameIndex, szNameBuffer, pcchNameBufferSize);155}156157PDH_STATUS PdhDll::PdhMakeCounterPath(PDH_COUNTER_PATH_ELEMENTS* pCounterPathElements, LPTSTR szFullPathBuffer, LPDWORD pcchBufferSize, DWORD dwFlags) {158assert(_initialized && _PdhMakeCounterPath != NULL, "PdhAvailable() not yet called");159return _PdhMakeCounterPath(pCounterPathElements, szFullPathBuffer, pcchBufferSize, dwFlags);160}161162bool PdhDll::PdhStatusFail(PDH_STATUS pdhStat) {163return pdhStat != ERROR_SUCCESS && pdhStat != PDH_MORE_DATA;164}165166167