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.SYSTEM;
8   import static oshi.driver.windows.perfmon.PerfmonConstants.WIN32_PERF_RAW_DATA_PERF_OS_SYSTEM;
9   
10  import java.util.Collections;
11  import java.util.Map;
12  
13  import oshi.annotation.concurrent.ThreadSafe;
14  import oshi.util.platform.windows.PerfCounterQuery;
15  import oshi.util.platform.windows.PerfCounterQuery.PdhCounterProperty;
16  
17  /**
18   * Utility to query System performance counters
19   */
20  @ThreadSafe
21  public final class SystemInformation {
22  
23      /**
24       * Context switch property
25       */
26      public enum ContextSwitchProperty implements PdhCounterProperty {
27          CONTEXTSWITCHESPERSEC(null, "Context Switches/sec");
28  
29          private final String instance;
30          private final String counter;
31  
32          ContextSwitchProperty(String instance, String counter) {
33              this.instance = instance;
34              this.counter = counter;
35          }
36  
37          @Override
38          public String getInstance() {
39              return instance;
40          }
41  
42          @Override
43          public String getCounter() {
44              return counter;
45          }
46      }
47  
48      /**
49       * Processor Queue Length property
50       */
51      public enum ProcessorQueueLengthProperty implements PdhCounterProperty {
52          PROCESSORQUEUELENGTH(null, "Processor Queue Length");
53  
54          private final String instance;
55          private final String counter;
56  
57          ProcessorQueueLengthProperty(String instance, String counter) {
58              this.instance = instance;
59              this.counter = counter;
60          }
61  
62          @Override
63          public String getInstance() {
64              return instance;
65          }
66  
67          @Override
68          public String getCounter() {
69              return counter;
70          }
71      }
72  
73      private SystemInformation() {
74      }
75  
76      /**
77       * Returns system context switch counters.
78       *
79       * @return Context switches counter for the total of all processors.
80       */
81      public static Map<ContextSwitchProperty, Long> queryContextSwitchCounters() {
82          if (PerfmonDisabled.PERF_OS_DISABLED) {
83              return Collections.emptyMap();
84          }
85          return PerfCounterQuery.queryValues(ContextSwitchProperty.class, SYSTEM, WIN32_PERF_RAW_DATA_PERF_OS_SYSTEM);
86      }
87  
88      /**
89       * Returns processor queue length.
90       *
91       * @return Processor Queue Length.
92       */
93      public static Map<ProcessorQueueLengthProperty, Long> queryProcessorQueueLength() {
94          if (PerfmonDisabled.PERF_OS_DISABLED) {
95              return Collections.emptyMap();
96          }
97          return PerfCounterQuery.queryValues(ProcessorQueueLengthProperty.class, SYSTEM,
98                  WIN32_PERF_RAW_DATA_PERF_OS_SYSTEM);
99      }
100 }