View Javadoc
1   /*
2    * Copyright 2021-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.hardware.platform.unix.openbsd;
6   
7   import static oshi.util.Memoizer.defaultExpiration;
8   import static oshi.util.Memoizer.memoize;
9   
10  import java.util.function.Supplier;
11  
12  import oshi.annotation.concurrent.ThreadSafe;
13  import oshi.hardware.common.AbstractVirtualMemory;
14  import oshi.util.ExecutingCommand;
15  import oshi.util.ParseUtil;
16  import oshi.util.tuples.Triplet;
17  
18  /**
19   * Memory info on OpenBSD
20   */
21  @ThreadSafe
22  final class OpenBsdVirtualMemory extends AbstractVirtualMemory {
23  
24      private final OpenBsdGlobalMemory global;
25  
26      private final Supplier<Triplet<Integer, Integer, Integer>> usedTotalPgin = memoize(
27              OpenBsdVirtualMemory::queryVmstat, defaultExpiration());
28      private final Supplier<Integer> pgout = memoize(OpenBsdVirtualMemory::queryUvm, defaultExpiration());
29  
30      OpenBsdVirtualMemory(OpenBsdGlobalMemory freeBsdGlobalMemory) {
31          this.global = freeBsdGlobalMemory;
32      }
33  
34      @Override
35      public long getSwapUsed() {
36          return usedTotalPgin.get().getA() * global.getPageSize();
37      }
38  
39      @Override
40      public long getSwapTotal() {
41          return usedTotalPgin.get().getB() * global.getPageSize();
42      }
43  
44      @Override
45      public long getVirtualMax() {
46          return this.global.getTotal() + getSwapTotal();
47      }
48  
49      @Override
50      public long getVirtualInUse() {
51          return this.global.getTotal() - this.global.getAvailable() + getSwapUsed();
52      }
53  
54      @Override
55      public long getSwapPagesIn() {
56          return usedTotalPgin.get().getC() * global.getPageSize();
57      }
58  
59      @Override
60      public long getSwapPagesOut() {
61          return pgout.get() * global.getPageSize();
62      }
63  
64      private static Triplet<Integer, Integer, Integer> queryVmstat() {
65          int used = 0;
66          int total = 0;
67          int swapIn = 0;
68          for (String line : ExecutingCommand.runNative("vmstat -s")) {
69              if (line.contains("swap pages in use")) {
70                  used = ParseUtil.getFirstIntValue(line);
71              } else if (line.contains("swap pages")) {
72                  total = ParseUtil.getFirstIntValue(line);
73              } else if (line.contains("pagein operations")) {
74                  swapIn = ParseUtil.getFirstIntValue(line);
75              }
76          }
77          return new Triplet<>(used, total, swapIn);
78      }
79  
80      private static int queryUvm() {
81          for (String line : ExecutingCommand.runNative("systat -ab uvm")) {
82              if (line.contains("pdpageouts")) {
83                  // First column is non-numeric "Constants" header
84                  return ParseUtil.getFirstIntValue(line);
85              }
86          }
87          return 0;
88      }
89  }