View Javadoc
1   /*
2    * Copyright 2020-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.driver.unix.solaris.kstat;
6   
7   import static oshi.software.os.unix.solaris.SolarisOperatingSystem.HAS_KSTAT2;
8   
9   import com.sun.jna.platform.unix.solaris.LibKstat.Kstat;
10  
11  import oshi.annotation.concurrent.ThreadSafe;
12  import oshi.util.platform.unix.solaris.KstatUtil;
13  import oshi.util.platform.unix.solaris.KstatUtil.KstatChain;
14  import oshi.util.tuples.Pair;
15  
16  /**
17   * Utility to query geom part list
18   */
19  @ThreadSafe
20  public final class SystemPages {
21  
22      private SystemPages() {
23      }
24  
25      /**
26       * Queries the {@code system_pages} kstat and returns available and physical memory
27       *
28       * @return A pair with the available and total memory, in pages. Mutiply by page size for bytes.
29       */
30      public static Pair<Long, Long> queryAvailableTotal() {
31          if (HAS_KSTAT2) {
32              // Use Kstat2 implementation
33              return queryAvailableTotal2();
34          }
35          long memAvailable = 0;
36          long memTotal = 0;
37          // Get first result
38          try (KstatChain kc = KstatUtil.openChain()) {
39              Kstat ksp = kc.lookup(null, -1, "system_pages");
40              // Set values
41              if (ksp != null && kc.read(ksp)) {
42                  memAvailable = KstatUtil.dataLookupLong(ksp, "availrmem"); // not a typo
43                  memTotal = KstatUtil.dataLookupLong(ksp, "physmem");
44              }
45          }
46          return new Pair<>(memAvailable, memTotal);
47      }
48  
49      private static Pair<Long, Long> queryAvailableTotal2() {
50          Object[] results = KstatUtil.queryKstat2("kstat:/pages/unix/system_pages", "availrmem", "physmem");
51          long avail = results[0] == null ? 0L : (long) results[0];
52          long total = results[1] == null ? 0L : (long) results[1];
53          return new Pair<>(avail, total);
54      }
55  }