View Javadoc
1   /*
2    * Copyright 2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.driver.windows.perfmon;
6   
7   import static org.hamcrest.MatcherAssert.assertThat;
8   import static org.hamcrest.Matchers.aMapWithSize;
9   import static org.hamcrest.Matchers.is;
10  import static oshi.driver.windows.perfmon.PerfmonConstants.MEMORY;
11  import static oshi.driver.windows.perfmon.PerfmonConstants.PAGING_FILE;
12  import static oshi.driver.windows.perfmon.PerfmonConstants.PHYSICAL_DISK;
13  import static oshi.driver.windows.perfmon.PerfmonConstants.PROCESS;
14  import static oshi.driver.windows.perfmon.PerfmonConstants.PROCESSOR;
15  import static oshi.driver.windows.perfmon.PerfmonConstants.PROCESSOR_INFORMATION;
16  import static oshi.driver.windows.perfmon.PerfmonConstants.SYSTEM;
17  import static oshi.driver.windows.perfmon.PerfmonConstants.THREAD;
18  import static oshi.driver.windows.perfmon.PerfmonConstants.WIN32_PERFPROC_PROCESS;
19  import static oshi.driver.windows.perfmon.PerfmonConstants.WIN32_PERFPROC_PROCESS_WHERE_IDPROCESS_0;
20  import static oshi.driver.windows.perfmon.PerfmonConstants.WIN32_PERFPROC_PROCESS_WHERE_NOT_NAME_LIKE_TOTAL;
21  import static oshi.driver.windows.perfmon.PerfmonConstants.WIN32_PERF_RAW_DATA_COUNTERS_PROCESSOR_INFORMATION_WHERE_NOT_NAME_LIKE_TOTAL;
22  import static oshi.driver.windows.perfmon.PerfmonConstants.WIN32_PERF_RAW_DATA_PERF_DISK_PHYSICAL_DISK_WHERE_NAME_NOT_TOTAL;
23  import static oshi.driver.windows.perfmon.PerfmonConstants.WIN32_PERF_RAW_DATA_PERF_OS_MEMORY;
24  import static oshi.driver.windows.perfmon.PerfmonConstants.WIN32_PERF_RAW_DATA_PERF_OS_PAGING_FILE;
25  import static oshi.driver.windows.perfmon.PerfmonConstants.WIN32_PERF_RAW_DATA_PERF_OS_PROCESSOR_WHERE_NAME_NOT_TOTAL;
26  import static oshi.driver.windows.perfmon.PerfmonConstants.WIN32_PERF_RAW_DATA_PERF_OS_PROCESSOR_WHERE_NAME_TOTAL;
27  import static oshi.driver.windows.perfmon.PerfmonConstants.WIN32_PERF_RAW_DATA_PERF_OS_SYSTEM;
28  import static oshi.driver.windows.perfmon.PerfmonConstants.WIN32_PERF_RAW_DATA_PERF_PROC_THREAD_WHERE_NOT_NAME_LIKE_TOTAL;
29  
30  import java.util.List;
31  import java.util.Map;
32  
33  import org.junit.jupiter.api.Test;
34  import org.junit.jupiter.api.condition.EnabledOnOs;
35  import org.junit.jupiter.api.condition.OS;
36  
37  import com.sun.jna.platform.win32.VersionHelpers;
38  
39  import oshi.driver.windows.perfmon.MemoryInformation.PageSwapProperty;
40  import oshi.driver.windows.perfmon.PagingFile.PagingPercentProperty;
41  import oshi.driver.windows.perfmon.PhysicalDisk.PhysicalDiskProperty;
42  import oshi.driver.windows.perfmon.ProcessInformation.HandleCountProperty;
43  import oshi.driver.windows.perfmon.ProcessInformation.IdleProcessorTimeProperty;
44  import oshi.driver.windows.perfmon.ProcessInformation.ProcessPerformanceProperty;
45  import oshi.driver.windows.perfmon.ProcessorInformation.InterruptsProperty;
46  import oshi.driver.windows.perfmon.ProcessorInformation.ProcessorFrequencyProperty;
47  import oshi.driver.windows.perfmon.ProcessorInformation.ProcessorTickCountProperty;
48  import oshi.driver.windows.perfmon.ProcessorInformation.ProcessorUtilityTickCountProperty;
49  import oshi.driver.windows.perfmon.SystemInformation.ContextSwitchProperty;
50  import oshi.driver.windows.perfmon.ThreadInformation.ThreadPerformanceProperty;
51  import oshi.util.platform.windows.PerfCounterQuery;
52  import oshi.util.platform.windows.PerfCounterWildcardQuery;
53  import oshi.util.platform.windows.PerfCounterWildcardQuery.PdhCounterWildcardProperty;
54  import oshi.util.tuples.Pair;
55  
56  @EnabledOnOs(OS.WINDOWS)
57  class PerfmonDriversTest {
58  
59      @Test
60      void testQueryPageSwaps() {
61          assertThat("Failed PDH queryPageSwaps", PerfCounterQuery.queryValuesFromPDH(PageSwapProperty.class, MEMORY),
62                  is(aMapWithSize(PageSwapProperty.values().length)));
63          assertThat("Failed WMI queryPageSwaps",
64                  PerfCounterQuery.queryValuesFromWMI(PageSwapProperty.class, WIN32_PERF_RAW_DATA_PERF_OS_MEMORY),
65                  is(aMapWithSize(PageSwapProperty.values().length)));
66      }
67  
68      @Test
69      void testQuerySwapUsed() {
70          assertThat("Failed PDH querySwapUsed",
71                  PerfCounterQuery.queryValuesFromPDH(PagingPercentProperty.class, PAGING_FILE),
72                  is(aMapWithSize(PagingPercentProperty.values().length)));
73          assertThat("Failed WMI querySwapUsed",
74                  PerfCounterQuery.queryValuesFromWMI(PagingPercentProperty.class,
75                          WIN32_PERF_RAW_DATA_PERF_OS_PAGING_FILE),
76                  is(aMapWithSize(PagingPercentProperty.values().length)));
77      }
78  
79      @Test
80      void testQueryDiskCounters() {
81          testWildcardCounters("PDH DiskCounters",
82                  PerfCounterWildcardQuery.queryInstancesAndValuesFromPDH(PhysicalDiskProperty.class, PHYSICAL_DISK));
83          testWildcardCounters("WMI DiskCounters", PerfCounterWildcardQuery.queryInstancesAndValuesFromWMI(
84                  PhysicalDiskProperty.class, WIN32_PERF_RAW_DATA_PERF_DISK_PHYSICAL_DISK_WHERE_NAME_NOT_TOTAL));
85      }
86  
87      @Test
88      void testQueryProcessCounters() {
89          testWildcardCounters("PDH ProcessCounters",
90                  PerfCounterWildcardQuery.queryInstancesAndValuesFromPDH(ProcessPerformanceProperty.class, PROCESS));
91          testWildcardCounters("WMI ProcessCounters", PerfCounterWildcardQuery.queryInstancesAndValuesFromWMI(
92                  ProcessPerformanceProperty.class, WIN32_PERFPROC_PROCESS_WHERE_NOT_NAME_LIKE_TOTAL));
93      }
94  
95      @Test
96      void testQueryHandles() {
97          testWildcardCounters("PDH Handles",
98                  PerfCounterWildcardQuery.queryInstancesAndValuesFromPDH(HandleCountProperty.class, PROCESS));
99          testWildcardCounters("WMI Handles", PerfCounterWildcardQuery
100                 .queryInstancesAndValuesFromWMI(HandleCountProperty.class, WIN32_PERFPROC_PROCESS));
101     }
102 
103     @Test
104     void testQueryIdleProcessCounters() {
105         testWildcardCounters("PDH IdleProcessCounters",
106                 PerfCounterWildcardQuery.queryInstancesAndValuesFromPDH(IdleProcessorTimeProperty.class, PROCESS));
107         testWildcardCounters("WMI IdleProcessCounters", PerfCounterWildcardQuery.queryInstancesAndValuesFromWMI(
108                 IdleProcessorTimeProperty.class, WIN32_PERFPROC_PROCESS_WHERE_IDPROCESS_0));
109     }
110 
111     @Test
112     void testQueryProcessorCounters() {
113         testWildcardCounters("PDH ProcessorCounters",
114                 PerfCounterWildcardQuery.queryInstancesAndValuesFromPDH(ProcessorTickCountProperty.class, PROCESSOR));
115         testWildcardCounters("WMI ProcessorCounters", PerfCounterWildcardQuery.queryInstancesAndValuesFromWMI(
116                 ProcessorTickCountProperty.class, WIN32_PERF_RAW_DATA_PERF_OS_PROCESSOR_WHERE_NAME_NOT_TOTAL));
117         if (VersionHelpers.IsWindows7OrGreater()) {
118             testWildcardCounters("PDH ProcessorCounters", PerfCounterWildcardQuery
119                     .queryInstancesAndValuesFromPDH(ProcessorTickCountProperty.class, PROCESSOR_INFORMATION));
120             testWildcardCounters("WMI ProcessorCounters",
121                     PerfCounterWildcardQuery.queryInstancesAndValuesFromWMI(ProcessorTickCountProperty.class,
122                             WIN32_PERF_RAW_DATA_COUNTERS_PROCESSOR_INFORMATION_WHERE_NOT_NAME_LIKE_TOTAL));
123 
124             if (VersionHelpers.IsWindows8OrGreater()) {
125                 testWildcardCounters("PDH ProcessorUtilityCounters",
126                         PerfCounterWildcardQuery.queryInstancesAndValuesFromPDH(ProcessorUtilityTickCountProperty.class,
127                                 PROCESSOR_INFORMATION));
128                 testWildcardCounters("WMI ProcessorUtilityCounters",
129                         PerfCounterWildcardQuery.queryInstancesAndValuesFromWMI(ProcessorUtilityTickCountProperty.class,
130                                 WIN32_PERF_RAW_DATA_COUNTERS_PROCESSOR_INFORMATION_WHERE_NOT_NAME_LIKE_TOTAL));
131             }
132         }
133     }
134 
135     @Test
136     void testQueryInterruptCounters() {
137         assertThat("Failed PDH queryInterruptCounters",
138                 PerfCounterQuery.queryValuesFromPDH(InterruptsProperty.class, PROCESSOR),
139                 is(aMapWithSize(InterruptsProperty.values().length)));
140         assertThat("Failed WMI queryInterruptCounters",
141                 PerfCounterQuery.queryValuesFromWMI(InterruptsProperty.class,
142                         WIN32_PERF_RAW_DATA_PERF_OS_PROCESSOR_WHERE_NAME_TOTAL),
143                 is(aMapWithSize(InterruptsProperty.values().length)));
144     }
145 
146     @Test
147     void testQueryFrequencyCounters() {
148         testWildcardCounters("PDH FrequencyCounters", PerfCounterWildcardQuery
149                 .queryInstancesAndValuesFromPDH(ProcessorFrequencyProperty.class, PROCESSOR_INFORMATION));
150         testWildcardCounters("WMI FrequencyCounters",
151                 PerfCounterWildcardQuery.queryInstancesAndValuesFromWMI(ProcessorFrequencyProperty.class,
152                         WIN32_PERF_RAW_DATA_COUNTERS_PROCESSOR_INFORMATION_WHERE_NOT_NAME_LIKE_TOTAL));
153     }
154 
155     @Test
156     void testQueryContextSwitchCounters() {
157         assertThat("Failed PDH queryInterruptCounters",
158                 PerfCounterQuery.queryValuesFromPDH(ContextSwitchProperty.class, SYSTEM),
159                 is(aMapWithSize(ContextSwitchProperty.values().length)));
160         assertThat("Failed WMI queryInterruptCounters",
161                 PerfCounterQuery.queryValuesFromWMI(ContextSwitchProperty.class, WIN32_PERF_RAW_DATA_PERF_OS_SYSTEM),
162                 is(aMapWithSize(ContextSwitchProperty.values().length)));
163     }
164 
165     @Test
166     void testQueryThreadCounters() {
167         testWildcardCounters("PDH ThreadCounters",
168                 PerfCounterWildcardQuery.queryInstancesAndValuesFromPDH(ThreadPerformanceProperty.class, THREAD));
169         testWildcardCounters("WMI ThreadCounters", PerfCounterWildcardQuery.queryInstancesAndValuesFromWMI(
170                 ThreadPerformanceProperty.class, WIN32_PERF_RAW_DATA_PERF_PROC_THREAD_WHERE_NOT_NAME_LIKE_TOTAL));
171     }
172 
173     private <T extends PdhCounterWildcardProperty> void testWildcardCounters(String s,
174             Pair<List<String>, Map<T, List<Long>>> counterData) {
175         int instanceCount = counterData.getA().size();
176         Map<T, List<Long>> counters = counterData.getB();
177         for (List<Long> counter : counters.values()) {
178             assertThat("Failed " + s + " instance count", counter.size(), is(instanceCount));
179         }
180 
181         @SuppressWarnings("unchecked")
182         int expectedSize = ((Class<T>) counters.keySet().iterator().next().getClass()).getEnumConstants().length - 1;
183         assertThat("Failed " + s + " map size", counters, is(aMapWithSize(expectedSize)));
184     }
185 }