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 com.sun.jna.platform.unix.aix.Perfstat;
8   import com.sun.jna.platform.unix.aix.Perfstat.perfstat_id_t;
9   import com.sun.jna.platform.unix.aix.Perfstat.perfstat_netinterface_t;
10  
11  import oshi.annotation.concurrent.ThreadSafe;
12  
13  /**
14   * Utility to query performance stats for network interfaces
15   */
16  @ThreadSafe
17  public final class PerfstatNetInterface {
18  
19      private static final Perfstat PERF = Perfstat.INSTANCE;
20  
21      private PerfstatNetInterface() {
22      }
23  
24      /**
25       * Queries perfstat_netinterface for per-netinterface usage statistics
26       *
27       * @return an array of usage statistics
28       */
29      public static perfstat_netinterface_t[] queryNetInterfaces() {
30          perfstat_netinterface_t netinterface = new perfstat_netinterface_t();
31          // With null, null, ..., 0, returns total # of elements
32          int total = PERF.perfstat_netinterface(null, null, netinterface.size(), 0);
33          if (total > 0) {
34              perfstat_netinterface_t[] statp = (perfstat_netinterface_t[]) netinterface.toArray(total);
35              perfstat_id_t firstnetinterface = new perfstat_id_t(); // name is ""
36              int ret = PERF.perfstat_netinterface(firstnetinterface, statp, netinterface.size(), total);
37              if (ret > 0) {
38                  return statp;
39              }
40          }
41          return new perfstat_netinterface_t[0];
42      }
43  }