View Javadoc
1   /*
2    * Copyright 2020-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.driver.unix.aix.perfstat;
6   
7   import java.util.Arrays;
8   
9   import com.sun.jna.platform.unix.aix.Perfstat;
10  import com.sun.jna.platform.unix.aix.Perfstat.perfstat_id_t;
11  import com.sun.jna.platform.unix.aix.Perfstat.perfstat_process_t;
12  
13  import oshi.annotation.concurrent.ThreadSafe;
14  
15  /**
16   * Utility to query performance stats for processes
17   */
18  @ThreadSafe
19  public final class PerfstatProcess {
20  
21      private static final Perfstat PERF = Perfstat.INSTANCE;
22  
23      private PerfstatProcess() {
24      }
25  
26      /**
27       * Queries perfstat_process for per-process usage statistics
28       *
29       * @return an array of usage statistics
30       */
31      public static perfstat_process_t[] queryProcesses() {
32          perfstat_process_t process = new perfstat_process_t();
33          // With null, null, ..., 0, returns total # of elements
34          int procCount = PERF.perfstat_process(null, null, process.size(), 0);
35          if (procCount > 0) {
36              perfstat_process_t[] proct = (perfstat_process_t[]) process.toArray(procCount);
37              perfstat_id_t firstprocess = new perfstat_id_t(); // name is ""
38              int ret = PERF.perfstat_process(firstprocess, proct, process.size(), procCount);
39              if (ret > 0) {
40                  return Arrays.copyOf(proct, ret);
41              }
42          }
43          return new perfstat_process_t[0];
44      }
45  }