1
2
3
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
34
35 @ThreadSafe
36 public final class PerfCounterWildcardQuery {
37
38 private static final Logger LOG = LoggerFactory.getLogger(PerfCounterWildcardQuery.class);
39
40
41 private static final Set<String> FAILED_QUERY_CACHE = ConcurrentHashMap.newKeySet();
42
43 private PerfCounterWildcardQuery() {
44 }
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
67
68
69
70
71
72
73
74
75
76
77
78
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
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
99
100
101
102
103
104
105
106
107
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
116
117
118
119
120
121
122
123
124
125
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
139
140 String perfObjectLocalized = PerfCounterQuery.localizeIfNeeded(perfObject, true);
141
142
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
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
160 EnumMap<T, List<PerfCounter>> counterListMap = new EnumMap<>(propertyEnum);
161
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
176 if (0 < pdhQueryHandler.updateQuery()) {
177
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
193
194
195
196
197
198
199
200
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
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
244
245 public interface PdhCounterWildcardProperty {
246
247
248
249
250 String getCounter();
251 }
252
253 }