1 /*
2  * Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  *
23  */
24 
25 #include "precompiled.hpp"
26 #include "pdh_interface.hpp"
27 #include "runtime/os.hpp"
28 #include "utilities/macros.hpp"
29 
30 // PDH API
31 typedef PDH_STATUS (WINAPI *PdhAddCounter_Fn)(HQUERY, LPCSTR, DWORD, HCOUNTER*);
32 typedef PDH_STATUS (WINAPI *PdhOpenQuery_Fn)(LPCWSTR, DWORD, HQUERY*);
33 typedef DWORD      (WINAPI *PdhCloseQuery_Fn)(HQUERY);
34 typedef PDH_STATUS (WINAPI *PdhCollectQueryData_Fn)(HQUERY);
35 typedef DWORD      (WINAPI *PdhGetFormattedCounterValue_Fn)(HCOUNTER, DWORD, LPDWORD, PPDH_FMT_COUNTERVALUE);
36 typedef PDH_STATUS (WINAPI *PdhEnumObjectItems_Fn)(LPCTSTR, LPCTSTR, LPCTSTR, LPTSTR, LPDWORD, LPTSTR, LPDWORD, DWORD, DWORD);
37 typedef PDH_STATUS (WINAPI *PdhRemoveCounter_Fn)(HCOUNTER);
38 typedef PDH_STATUS (WINAPI *PdhLookupPerfNameByIndex_Fn)(LPCSTR, DWORD, LPSTR, LPDWORD);
39 typedef PDH_STATUS (WINAPI *PdhMakeCounterPath_Fn)(PDH_COUNTER_PATH_ELEMENTS*, LPTSTR, LPDWORD, DWORD);
40 typedef PDH_STATUS (WINAPI *PdhExpandWildCardPath_Fn)(LPCSTR, LPCSTR, PZZSTR, LPDWORD, DWORD);
41 
42 PdhAddCounter_Fn PdhDll::_PdhAddCounter = NULL;
43 PdhOpenQuery_Fn  PdhDll::_PdhOpenQuery = NULL;
44 PdhCloseQuery_Fn PdhDll::_PdhCloseQuery = NULL;
45 PdhCollectQueryData_Fn PdhDll::_PdhCollectQueryData = NULL;
46 PdhGetFormattedCounterValue_Fn PdhDll::_PdhGetFormattedCounterValue = NULL;
47 PdhEnumObjectItems_Fn PdhDll::_PdhEnumObjectItems = NULL;
48 PdhRemoveCounter_Fn PdhDll::_PdhRemoveCounter = NULL;
49 PdhLookupPerfNameByIndex_Fn PdhDll::_PdhLookupPerfNameByIndex = NULL;
50 PdhMakeCounterPath_Fn PdhDll::_PdhMakeCounterPath = NULL;
51 PdhExpandWildCardPath_Fn PdhDll::_PdhExpandWildCardPath = NULL;
52 
53 LONG PdhDll::_critical_section = 0;
54 LONG PdhDll::_initialized = 0;
55 LONG PdhDll::_pdh_reference_count = 0;
56 HMODULE PdhDll::_hModule = NULL;
57 
initialize(void)58 void PdhDll::initialize(void) {
59   _hModule = os::win32::load_Windows_dll("pdh.dll", NULL, 0);
60   if (NULL == _hModule) {
61     return;
62   }
63   // The 'A' at the end means the ANSI (not the UNICODE) vesions of the methods
64   _PdhAddCounter               = (PdhAddCounter_Fn)::GetProcAddress(_hModule, "PdhAddCounterA");
65   _PdhOpenQuery                = (PdhOpenQuery_Fn)::GetProcAddress(_hModule, "PdhOpenQueryA");
66   _PdhCloseQuery               = (PdhCloseQuery_Fn)::GetProcAddress(_hModule, "PdhCloseQuery");
67   _PdhCollectQueryData         = (PdhCollectQueryData_Fn)::GetProcAddress(_hModule, "PdhCollectQueryData");
68   _PdhGetFormattedCounterValue = (PdhGetFormattedCounterValue_Fn)::GetProcAddress(_hModule, "PdhGetFormattedCounterValue");
69   _PdhEnumObjectItems          = (PdhEnumObjectItems_Fn)::GetProcAddress(_hModule, "PdhEnumObjectItemsA");
70   _PdhRemoveCounter            = (PdhRemoveCounter_Fn)::GetProcAddress(_hModule, "PdhRemoveCounter");
71   _PdhLookupPerfNameByIndex    = (PdhLookupPerfNameByIndex_Fn)::GetProcAddress(_hModule, "PdhLookupPerfNameByIndexA");
72   _PdhMakeCounterPath          = (PdhMakeCounterPath_Fn)::GetProcAddress(_hModule, "PdhMakeCounterPathA");
73   _PdhExpandWildCardPath       = (PdhExpandWildCardPath_Fn)::GetProcAddress(_hModule, "PdhExpandWildCardPathA");
74   InterlockedExchange(&_initialized, 1);
75 }
76 
PdhDetach(void)77 bool PdhDll::PdhDetach(void) {
78   LONG prev_ref_count = InterlockedExchangeAdd(&_pdh_reference_count, -1);
79   BOOL ret = false;
80   if (1 == prev_ref_count) {
81     if (_initialized && _hModule != NULL) {
82       ret = FreeLibrary(_hModule);
83       if (ret) {
84         _hModule = NULL;
85         _PdhAddCounter = NULL;
86         _PdhOpenQuery = NULL;
87         _PdhCloseQuery = NULL;
88         _PdhCollectQueryData = NULL;
89         _PdhGetFormattedCounterValue = NULL;
90         _PdhEnumObjectItems = NULL;
91         _PdhRemoveCounter = NULL;
92         _PdhLookupPerfNameByIndex = NULL;
93         _PdhMakeCounterPath = NULL;
94         _PdhExpandWildCardPath = NULL;
95         InterlockedExchange(&_initialized, 0);
96       }
97     }
98   }
99   return ret != 0;
100 }
101 
PdhAttach(void)102 bool PdhDll::PdhAttach(void) {
103   InterlockedExchangeAdd(&_pdh_reference_count, 1);
104   if (1 == _initialized) {
105     return true;
106   }
107   while (InterlockedCompareExchange(&_critical_section, 1, 0) == 1);
108   if (0 == _initialized) {
109     initialize();
110   }
111   while (InterlockedCompareExchange(&_critical_section, 0, 1) == 0);
112   return (_PdhAddCounter != NULL && _PdhOpenQuery != NULL
113          && _PdhCloseQuery != NULL && PdhCollectQueryData != NULL
114          && _PdhGetFormattedCounterValue != NULL && _PdhEnumObjectItems != NULL
115          && _PdhRemoveCounter != NULL && PdhLookupPerfNameByIndex != NULL
116          && _PdhMakeCounterPath != NULL && _PdhExpandWildCardPath != NULL);
117 }
118 
PdhAddCounter(HQUERY hQuery,LPCSTR szFullCounterPath,DWORD dwUserData,HCOUNTER * phCounter)119 PDH_STATUS PdhDll::PdhAddCounter(HQUERY hQuery, LPCSTR szFullCounterPath, DWORD dwUserData, HCOUNTER* phCounter) {
120   assert(_initialized && _PdhAddCounter != NULL, "PdhAvailable() not yet called");
121   return _PdhAddCounter(hQuery, szFullCounterPath, dwUserData, phCounter);
122 }
123 
PdhOpenQuery(LPCWSTR szDataSource,DWORD dwUserData,HQUERY * phQuery)124 PDH_STATUS PdhDll::PdhOpenQuery(LPCWSTR szDataSource, DWORD dwUserData, HQUERY* phQuery) {
125   assert(_initialized && _PdhOpenQuery != NULL, "PdhAvailable() not yet called");
126   return _PdhOpenQuery(szDataSource, dwUserData, phQuery);
127 }
128 
PdhCloseQuery(HQUERY hQuery)129 DWORD PdhDll::PdhCloseQuery(HQUERY hQuery) {
130   assert(_initialized && _PdhCloseQuery != NULL, "PdhAvailable() not yet called");
131   return _PdhCloseQuery(hQuery);
132 }
133 
PdhCollectQueryData(HQUERY hQuery)134 PDH_STATUS PdhDll::PdhCollectQueryData(HQUERY hQuery) {
135   assert(_initialized && _PdhCollectQueryData != NULL, "PdhAvailable() not yet called");
136   return _PdhCollectQueryData(hQuery);
137 }
138 
PdhGetFormattedCounterValue(HCOUNTER hCounter,DWORD dwFormat,LPDWORD lpdwType,PPDH_FMT_COUNTERVALUE pValue)139 DWORD PdhDll::PdhGetFormattedCounterValue(HCOUNTER hCounter, DWORD dwFormat, LPDWORD lpdwType, PPDH_FMT_COUNTERVALUE pValue) {
140   assert(_initialized && _PdhGetFormattedCounterValue != NULL, "PdhAvailable() not yet called");
141   return _PdhGetFormattedCounterValue(hCounter, dwFormat, lpdwType, pValue);
142 }
143 
PdhEnumObjectItems(LPCTSTR szDataSource,LPCTSTR szMachineName,LPCTSTR szObjectName,LPTSTR mszCounterList,LPDWORD pcchCounterListLength,LPTSTR mszInstanceList,LPDWORD pcchInstanceListLength,DWORD dwDetailLevel,DWORD dwFlags)144 PDH_STATUS PdhDll::PdhEnumObjectItems(LPCTSTR szDataSource, LPCTSTR szMachineName, LPCTSTR szObjectName,
145     LPTSTR mszCounterList, LPDWORD pcchCounterListLength, LPTSTR mszInstanceList,
146     LPDWORD pcchInstanceListLength, DWORD dwDetailLevel, DWORD dwFlags) {
147   assert(_initialized && _PdhEnumObjectItems != NULL, "PdhAvailable() not yet called");
148   return _PdhEnumObjectItems(szDataSource, szMachineName, szObjectName, mszCounterList, pcchCounterListLength,
149     mszInstanceList, pcchInstanceListLength, dwDetailLevel, dwFlags);
150 }
151 
PdhRemoveCounter(HCOUNTER hCounter)152 PDH_STATUS PdhDll::PdhRemoveCounter(HCOUNTER hCounter) {
153   assert(_initialized && _PdhRemoveCounter != NULL, "PdhAvailable() not yet called");
154   return _PdhRemoveCounter(hCounter);
155 }
156 
PdhLookupPerfNameByIndex(LPCSTR szMachineName,DWORD dwNameIndex,LPSTR szNameBuffer,LPDWORD pcchNameBufferSize)157 PDH_STATUS PdhDll::PdhLookupPerfNameByIndex(LPCSTR szMachineName, DWORD dwNameIndex, LPSTR szNameBuffer, LPDWORD pcchNameBufferSize) {
158   assert(_initialized && _PdhLookupPerfNameByIndex != NULL, "PdhAvailable() not yet called");
159   return _PdhLookupPerfNameByIndex(szMachineName, dwNameIndex, szNameBuffer, pcchNameBufferSize);
160 }
161 
PdhMakeCounterPath(PDH_COUNTER_PATH_ELEMENTS * pCounterPathElements,LPTSTR szFullPathBuffer,LPDWORD pcchBufferSize,DWORD dwFlags)162 PDH_STATUS PdhDll::PdhMakeCounterPath(PDH_COUNTER_PATH_ELEMENTS* pCounterPathElements, LPTSTR szFullPathBuffer, LPDWORD pcchBufferSize, DWORD dwFlags) {
163   assert(_initialized && _PdhMakeCounterPath != NULL, "PdhAvailable() not yet called");
164   return _PdhMakeCounterPath(pCounterPathElements, szFullPathBuffer, pcchBufferSize, dwFlags);
165 }
166 
PdhExpandWildCardPath(LPCSTR szDataSource,LPCSTR szWildCardPath,PZZSTR mszExpandedPathList,LPDWORD pcchPathListLength,DWORD dwFlags)167 PDH_STATUS PdhDll::PdhExpandWildCardPath(LPCSTR szDataSource, LPCSTR szWildCardPath, PZZSTR mszExpandedPathList, LPDWORD pcchPathListLength, DWORD dwFlags) {
168   assert(_initialized && PdhExpandWildCardPath != NULL, "PdhAvailable() not yet called");
169   return _PdhExpandWildCardPath(szDataSource, szWildCardPath, mszExpandedPathList, pcchPathListLength, dwFlags);
170 }
171 
PdhStatusFail(PDH_STATUS pdhStat)172 bool PdhDll::PdhStatusFail(PDH_STATUS pdhStat) {
173   return pdhStat != ERROR_SUCCESS && pdhStat != PDH_MORE_DATA;
174 }
175