1 /*
2 * Copyright 2019-2024 The OSHI Project Contributors
3 * SPDX-License-Identifier: MIT
4 */
5 package oshi.util.platform.windows;
6
7 import java.util.ArrayList;
8 import java.util.Collections;
9 import java.util.EnumMap;
10 import java.util.List;
11 import java.util.Locale;
12 import java.util.Map;
13 import java.util.Objects;
14 import java.util.Set;
15 import java.util.concurrent.ConcurrentHashMap;
16
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 import com.sun.jna.platform.win32.PdhUtil;
21 import com.sun.jna.platform.win32.PdhUtil.PdhEnumObjectItems;
22 import com.sun.jna.platform.win32.PdhUtil.PdhException;
23 import com.sun.jna.platform.win32.COM.Wbemcli;
24 import com.sun.jna.platform.win32.COM.WbemcliUtil.WmiQuery;
25 import com.sun.jna.platform.win32.COM.WbemcliUtil.WmiResult;
26
27 import oshi.annotation.concurrent.ThreadSafe;
28 import oshi.util.GlobalConfig;
29 import oshi.util.Util;
30 import oshi.util.platform.windows.PerfDataUtil.PerfCounter;
31 import oshi.util.tuples.Pair;
32
33 /**
34 * Enables queries of Performance Counters using wild cards to filter instances
35 */
36 @ThreadSafe
37 public final class PerfCounterWildcardQuery {
38
39 private static final Logger LOG = LoggerFactory.getLogger(PerfCounterWildcardQuery.class);
40
41 private static final boolean PERF_DISABLE_ALL_ON_FAILURE = GlobalConfig
42 .get(GlobalConfig.OSHI_OS_WINDOWS_PERF_DISABLE_ALL_ON_FAILURE, false);
43
44 // Use a thread safe set to cache failed pdh queries
45 private static final Set<String> FAILED_QUERY_CACHE = ConcurrentHashMap.newKeySet();
46
47 private PerfCounterWildcardQuery() {
48 }
49
50 /**
51 * Query the a Performance Counter using PDH, with WMI backup on failure, for values corresponding to the property
52 * enum.
53 *
54 * @param <T> The enum type of {@code propertyEnum}
55 * @param propertyEnum An enum which implements
56 * {@link oshi.util.platform.windows.PerfCounterQuery.PdhCounterProperty} and contains the WMI
57 * field (Enum value) and PDH Counter string (instance and counter)
58 * @param perfObject The PDH object for this counter; all counters on this object will be refreshed at the same
59 * time
60 * @param perfWmiClass The WMI PerfData_RawData_* class corresponding to the PDH object
61 * @return A pair containing a list of instances and an {@link EnumMap} of the corresponding values indexed by
62 * {@code propertyEnum} on success, or an empty list and empty map if both PDH and WMI queries failed.
63 */
64 public static <T extends Enum<T>> Pair<List<String>, Map<T, List<Long>>> queryInstancesAndValues(
65 Class<T> propertyEnum, String perfObject, String perfWmiClass) {
66 return queryInstancesAndValues(propertyEnum, perfObject, perfWmiClass, null);
67 }
68
69 /**
70 * Query the a Performance Counter using PDH, with WMI backup on failure, for values corresponding to the property
71 * enum.
72 *
73 * @param <T> The enum type of {@code propertyEnum}
74 * @param propertyEnum An enum which implements
75 * {@link oshi.util.platform.windows.PerfCounterQuery.PdhCounterProperty} and contains the WMI
76 * field (Enum value) and PDH Counter string (instance and counter)
77 * @param perfObject The PDH object for this counter; all counters on this object will be refreshed at the same
78 * time
79 * @param perfWmiClass The WMI PerfData_RawData_* class corresponding to the PDH object
80 * @param customFilter a custom instance filter to use. If null, uses the first element of the property enum
81 * @return A pair containing a list of instances and an {@link EnumMap} of the corresponding values indexed by
82 * {@code propertyEnum} on success, or an empty list and empty map if both PDH and WMI queries failed.
83 */
84 public static <T extends Enum<T>> Pair<List<String>, Map<T, List<Long>>> queryInstancesAndValues(
85 Class<T> propertyEnum, String perfObject, String perfWmiClass, String customFilter) {
86 if (FAILED_QUERY_CACHE.isEmpty()
87 || (!PERF_DISABLE_ALL_ON_FAILURE && !FAILED_QUERY_CACHE.contains(perfObject))) {
88 Pair<List<String>, Map<T, List<Long>>> instancesAndValuesMap = queryInstancesAndValuesFromPDH(propertyEnum,
89 perfObject, customFilter);
90 if (!instancesAndValuesMap.getA().isEmpty()) {
91 return instancesAndValuesMap;
92 }
93 // If we are here, query returned no results
94 if (Util.isBlank(customFilter)) {
95 if (PERF_DISABLE_ALL_ON_FAILURE) {
96 LOG.info("Disabling further attempts to query performance counters.");
97 } else {
98 LOG.info("Disabling further attempts to query {}.", perfObject);
99 }
100 FAILED_QUERY_CACHE.add(perfObject);
101 }
102 }
103 return queryInstancesAndValuesFromWMI(propertyEnum, perfWmiClass);
104 }
105
106 /**
107 * Query the a Performance Counter using PDH for values corresponding to the property enum.
108 *
109 * @param <T> The enum type of {@code propertyEnum}
110 * @param propertyEnum An enum which implements
111 * {@link oshi.util.platform.windows.PerfCounterQuery.PdhCounterProperty} and contains the WMI
112 * field (Enum value) and PDH Counter string (instance and counter)
113 * @param perfObject The PDH object for this counter; all counters on this object will be refreshed at the same
114 * time
115 * @return An pair containing a list of instances and an {@link EnumMap} of the corresponding values indexed by
116 * {@code propertyEnum} on success, or an empty list and empty map if the PDH query failed.
117 */
118 public static <T extends Enum<T>> Pair<List<String>, Map<T, List<Long>>> queryInstancesAndValuesFromPDH(
119 Class<T> propertyEnum, String perfObject) {
120 return queryInstancesAndValuesFromPDH(propertyEnum, perfObject, null);
121 }
122
123 /**
124 * Query the a Performance Counter using PDH for values corresponding to the property enum.
125 *
126 * @param <T> The enum type of {@code propertyEnum}
127 * @param propertyEnum An enum which implements
128 * {@link oshi.util.platform.windows.PerfCounterQuery.PdhCounterProperty} and contains the WMI
129 * field (Enum value) and PDH Counter string (instance and counter)
130 * @param perfObject The PDH object for this counter; all counters on this object will be refreshed at the same
131 * time
132 * @param customFilter a custom instance filter to use. If null, uses the first element of the property enum
133 * @return An pair containing a list of instances and an {@link EnumMap} of the corresponding values indexed by
134 * {@code propertyEnum} on success, or an empty list and empty map if the PDH query failed.
135 */
136 public static <T extends Enum<T>> Pair<List<String>, Map<T, List<Long>>> queryInstancesAndValuesFromPDH(
137 Class<T> propertyEnum, String perfObject, String customFilter) {
138 T[] props = propertyEnum.getEnumConstants();
139 if (props.length < 2) {
140 throw new IllegalArgumentException("Enum " + propertyEnum.getName()
141 + " must have at least two elements, an instance filter and a counter.");
142 }
143 String instanceFilter = Util.isBlank(customFilter)
144 ? ((PdhCounterWildcardProperty) propertyEnum.getEnumConstants()[0]).getCounter()
145 .toLowerCase(Locale.ROOT)
146 : customFilter;
147 // Localize the perfObject using different variable for the EnumObjectItems
148 // Will still use unlocalized perfObject for the query
149 String perfObjectLocalized = PerfCounterQuery.localizeIfNeeded(perfObject, true);
150
151 // Get list of instances
152 PdhEnumObjectItems objectItems = null;
153 try {
154 objectItems = PdhUtil.PdhEnumObjectItems(null, null, perfObjectLocalized, 100);
155 } catch (PdhException e) {
156 LOG.warn(
157 "Failed to locate performance object for {} in the registry. Performance counters may be corrupt. {}",
158 perfObjectLocalized, e.getMessage());
159 }
160 if (objectItems == null) {
161 return new Pair<>(Collections.emptyList(), Collections.emptyMap());
162 }
163 List<String> instances = objectItems.getInstances();
164 // Filter out instances not matching filter
165 instances.removeIf(i -> !Util.wildcardMatch(i.toLowerCase(Locale.ROOT), instanceFilter));
166 EnumMap<T, List<Long>> valuesMap = new EnumMap<>(propertyEnum);
167 try (PerfCounterQueryHandler pdhQueryHandler = new PerfCounterQueryHandler()) {
168 // Set up the query and counter handles
169 EnumMap<T, List<PerfCounter>> counterListMap = new EnumMap<>(propertyEnum);
170 // Start at 1, first counter defines instance filter
171 for (int i = 1; i < props.length; i++) {
172 T prop = props[i];
173 List<PerfCounter> counterList = new ArrayList<>(instances.size());
174 for (String instance : instances) {
175 PerfCounter counter = PerfDataUtil.createCounter(perfObject, instance,
176 ((PdhCounterWildcardProperty) prop).getCounter());
177 if (!pdhQueryHandler.addCounterToQuery(counter)) {
178 return new Pair<>(Collections.emptyList(), Collections.emptyMap());
179 }
180 counterList.add(counter);
181 }
182 counterListMap.put(prop, counterList);
183 }
184 // And then query. Zero timestamp means update failed
185 if (0 < pdhQueryHandler.updateQuery()) {
186 // Start at 1, first counter defines instance filter
187 for (int i = 1; i < props.length; i++) {
188 T prop = props[i];
189 List<Long> values = new ArrayList<>();
190 for (PerfCounter counter : counterListMap.get(prop)) {
191 values.add(pdhQueryHandler.queryCounter(counter));
192 }
193 valuesMap.put(prop, values);
194 }
195 }
196 }
197 return new Pair<>(instances, valuesMap);
198 }
199
200 /**
201 * Query the a Performance Counter using WMI for values corresponding to the property enum.
202 *
203 * @param <T> The enum type of {@code propertyEnum}
204 * @param propertyEnum An enum which implements
205 * {@link oshi.util.platform.windows.PerfCounterQuery.PdhCounterProperty} and contains the WMI
206 * field (Enum value) and PDH Counter string (instance and counter)
207 * @param wmiClass The WMI PerfData_RawData_* class corresponding to the PDH object
208 * @return An pair containing a list of instances and an {@link EnumMap} of the corresponding values indexed by
209 * {@code propertyEnum} on success, or an empty list and empty map if the WMI query failed.
210 */
211 public static <T extends Enum<T>> Pair<List<String>, Map<T, List<Long>>> queryInstancesAndValuesFromWMI(
212 Class<T> propertyEnum, String wmiClass) {
213 List<String> instances = new ArrayList<>();
214 EnumMap<T, List<Long>> valuesMap = new EnumMap<>(propertyEnum);
215 WmiQuery<T> query = new WmiQuery<>(wmiClass, propertyEnum);
216 WmiResult<T> result = Objects.requireNonNull(WmiQueryHandler.createInstance()).queryWMI(query);
217 if (result.getResultCount() > 0) {
218 for (T prop : propertyEnum.getEnumConstants()) {
219 // First element is instance name
220 if (prop.ordinal() == 0) {
221 for (int i = 0; i < result.getResultCount(); i++) {
222 instances.add(WmiUtil.getString(result, prop, i));
223 }
224 } else {
225 List<Long> values = new ArrayList<>();
226 for (int i = 0; i < result.getResultCount(); i++) {
227 switch (result.getCIMType(prop)) {
228 case Wbemcli.CIM_UINT16:
229 values.add((long) WmiUtil.getUint16(result, prop, i));
230 break;
231 case Wbemcli.CIM_UINT32:
232 values.add(WmiUtil.getUint32asLong(result, prop, i));
233 break;
234 case Wbemcli.CIM_UINT64:
235 values.add(WmiUtil.getUint64(result, prop, i));
236 break;
237 case Wbemcli.CIM_DATETIME:
238 values.add(WmiUtil.getDateTime(result, prop, i).toInstant().toEpochMilli());
239 break;
240 default:
241 throw new ClassCastException("Unimplemented CIM Type Mapping.");
242 }
243 }
244 valuesMap.put(prop, values);
245 }
246 }
247 }
248 return new Pair<>(instances, valuesMap);
249 }
250
251 /**
252 * Contract for Counter Property Enums
253 */
254 public interface PdhCounterWildcardProperty {
255 /**
256 * @return Returns the counter. The first element of the enum will return the instance filter rather than a
257 * counter.
258 */
259 String getCounter();
260 }
261
262 }