View Javadoc
1   /*
2    * Copyright 2016-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.hardware.platform.unix.solaris;
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.driver.unix.solaris.kstat.SystemPages;
14  import oshi.hardware.VirtualMemory;
15  import oshi.hardware.common.AbstractGlobalMemory;
16  import oshi.util.ExecutingCommand;
17  import oshi.util.ParseUtil;
18  import oshi.util.tuples.Pair;
19  
20  /**
21   * Memory obtained by kstat
22   */
23  @ThreadSafe
24  final class SolarisGlobalMemory extends AbstractGlobalMemory {
25  
26      private final Supplier<Pair<Long, Long>> availTotal = memoize(SystemPages::queryAvailableTotal,
27              defaultExpiration());
28  
29      private final Supplier<Long> pageSize = memoize(SolarisGlobalMemory::queryPageSize);
30  
31      private final Supplier<VirtualMemory> vm = memoize(this::createVirtualMemory);
32  
33      @Override
34      public long getAvailable() {
35          return availTotal.get().getA() * getPageSize();
36      }
37  
38      @Override
39      public long getTotal() {
40          return availTotal.get().getB() * getPageSize();
41      }
42  
43      @Override
44      public long getPageSize() {
45          return pageSize.get();
46      }
47  
48      @Override
49      public VirtualMemory getVirtualMemory() {
50          return vm.get();
51      }
52  
53      private static long queryPageSize() {
54          return ParseUtil.parseLongOrDefault(ExecutingCommand.getFirstAnswer("pagesize"), 4096L);
55      }
56  
57      private VirtualMemory createVirtualMemory() {
58          return new SolarisVirtualMemory(this);
59      }
60  }