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