View Javadoc
1   /*
2    * Copyright 2020-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.driver.windows.perfmon;
6   
7   import static oshi.driver.windows.perfmon.PerfmonConstants.PROCESS;
8   import static oshi.driver.windows.perfmon.PerfmonConstants.WIN32_PERFPROC_PROCESS;
9   import static oshi.driver.windows.perfmon.PerfmonConstants.WIN32_PERFPROC_PROCESS_WHERE_IDPROCESS_0;
10  import static oshi.driver.windows.perfmon.PerfmonConstants.WIN32_PERFPROC_PROCESS_WHERE_NOT_NAME_LIKE_TOTAL;
11  
12  import java.util.Collections;
13  import java.util.List;
14  import java.util.Map;
15  
16  import oshi.annotation.concurrent.ThreadSafe;
17  import oshi.util.platform.windows.PerfCounterQuery;
18  import oshi.util.platform.windows.PerfCounterWildcardQuery;
19  import oshi.util.platform.windows.PerfCounterWildcardQuery.PdhCounterWildcardProperty;
20  import oshi.util.tuples.Pair;
21  
22  /**
23   * Utility to query Process Information performance counter
24   */
25  @ThreadSafe
26  public final class ProcessInformation {
27  
28      /**
29       * Process performance counters
30       */
31      public enum ProcessPerformanceProperty implements PdhCounterWildcardProperty {
32          // First element defines WMI instance name field and PDH instance filter
33          NAME(PerfCounterQuery.NOT_TOTAL_INSTANCES),
34          // Remaining elements define counters
35          PRIORITYBASE("Priority Base"), //
36          ELAPSEDTIME("Elapsed Time"), //
37          IDPROCESS("ID Process"), //
38          CREATINGPROCESSID("Creating Process ID"), //
39          IOREADBYTESPERSEC("IO Read Bytes/sec"), //
40          IOWRITEBYTESPERSEC("IO Write Bytes/sec"), //
41          PRIVATEBYTES("Working Set - Private"), //
42          PAGEFAULTSPERSEC("Page Faults/sec");
43  
44          private final String counter;
45  
46          ProcessPerformanceProperty(String counter) {
47              this.counter = counter;
48          }
49  
50          @Override
51          public String getCounter() {
52              return counter;
53          }
54      }
55  
56      /**
57       * Handle performance counters
58       */
59      public enum HandleCountProperty implements PdhCounterWildcardProperty {
60          // First element defines WMI instance name field and PDH instance filter
61          NAME(PerfCounterQuery.TOTAL_INSTANCE),
62          // Remaining elements define counters
63          HANDLECOUNT("Handle Count");
64  
65          private final String counter;
66  
67          HandleCountProperty(String counter) {
68              this.counter = counter;
69          }
70  
71          @Override
72          public String getCounter() {
73              return counter;
74          }
75      }
76  
77      /**
78       * Processor performance counters
79       */
80      public enum IdleProcessorTimeProperty implements PdhCounterWildcardProperty {
81          // First element defines WMI instance name field and PDH instance filter
82          NAME(PerfCounterQuery.TOTAL_OR_IDLE_INSTANCES),
83          // Remaining elements define counters
84          PERCENTPROCESSORTIME("% Processor Time"), //
85          ELAPSEDTIME("Elapsed Time");
86  
87          private final String counter;
88  
89          IdleProcessorTimeProperty(String counter) {
90              this.counter = counter;
91          }
92  
93          @Override
94          public String getCounter() {
95              return counter;
96          }
97      }
98  
99      private ProcessInformation() {
100     }
101 
102     /**
103      * Returns process counters.
104      *
105      * @return Process counters for each process.
106      */
107     public static Pair<List<String>, Map<ProcessPerformanceProperty, List<Long>>> queryProcessCounters() {
108         if (PerfmonDisabled.PERF_PROC_DISABLED) {
109             return new Pair<>(Collections.emptyList(), Collections.emptyMap());
110         }
111         return PerfCounterWildcardQuery.queryInstancesAndValues(ProcessPerformanceProperty.class, PROCESS,
112                 WIN32_PERFPROC_PROCESS_WHERE_NOT_NAME_LIKE_TOTAL);
113     }
114 
115     /**
116      * Returns handle counters
117      *
118      * @return Process handle counters for each process.
119      */
120     public static Pair<List<String>, Map<HandleCountProperty, List<Long>>> queryHandles() {
121         if (PerfmonDisabled.PERF_PROC_DISABLED) {
122             return new Pair<>(Collections.emptyList(), Collections.emptyMap());
123         }
124         return PerfCounterWildcardQuery.queryInstancesAndValues(HandleCountProperty.class, PROCESS,
125                 WIN32_PERFPROC_PROCESS);
126     }
127 
128     /**
129      * Returns cooked idle process performance counters.
130      *
131      * @return Cooked performance counters for idle process.
132      */
133     public static Pair<List<String>, Map<IdleProcessorTimeProperty, List<Long>>> queryIdleProcessCounters() {
134         if (PerfmonDisabled.PERF_OS_DISABLED) {
135             return new Pair<>(Collections.emptyList(), Collections.emptyMap());
136         }
137         return PerfCounterWildcardQuery.queryInstancesAndValues(IdleProcessorTimeProperty.class, PROCESS,
138                 WIN32_PERFPROC_PROCESS_WHERE_IDPROCESS_0);
139     }
140 }