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.GlobalConfig;
29 import oshi.util.Util;
30 import oshi.util.platform.windows.PerfDataUtil.PerfCounter;
31 import oshi.util.tuples.Pair;
32
33
34
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
45 private static final Set<String> FAILED_QUERY_CACHE = ConcurrentHashMap.newKeySet();
46
47 private PerfCounterWildcardQuery() {
48 }
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
71
72
73
74
75
76
77
78
79
80
81
82
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
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
108
109
110
111
112
113
114
115
116
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
125
126
127
128
129
130
131
132
133
134
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
148
149 String perfObjectLocalized = PerfCounterQuery.localizeIfNeeded(perfObject, true);
150
151
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
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
169 EnumMap<T, List<PerfCounter>> counterListMap = new EnumMap<>(propertyEnum);
170
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
185 if (0 < pdhQueryHandler.updateQuery()) {
186
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
202
203
204
205
206
207
208
209
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
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
253
254 public interface PdhCounterWildcardProperty {
255
256
257
258
259 String getCounter();
260 }
261
262 }