1
2
3
4
5 package oshi.hardware.platform.unix.freebsd;
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.platform.unix.freebsd.BsdSysctlUtil;
17
18
19
20
21 @ThreadSafe
22 final class FreeBsdVirtualMemory extends AbstractVirtualMemory {
23
24 private final FreeBsdGlobalMemory global;
25
26 private final Supplier<Long> used = memoize(FreeBsdVirtualMemory::querySwapUsed, defaultExpiration());
27
28 private final Supplier<Long> total = memoize(FreeBsdVirtualMemory::querySwapTotal, defaultExpiration());
29
30 private final Supplier<Long> pagesIn = memoize(FreeBsdVirtualMemory::queryPagesIn, defaultExpiration());
31
32 private final Supplier<Long> pagesOut = memoize(FreeBsdVirtualMemory::queryPagesOut, defaultExpiration());
33
34 FreeBsdVirtualMemory(FreeBsdGlobalMemory freeBsdGlobalMemory) {
35 this.global = freeBsdGlobalMemory;
36 }
37
38 @Override
39 public long getSwapUsed() {
40 return used.get();
41 }
42
43 @Override
44 public long getSwapTotal() {
45 return total.get();
46 }
47
48 @Override
49 public long getVirtualMax() {
50 return this.global.getTotal() + getSwapTotal();
51 }
52
53 @Override
54 public long getVirtualInUse() {
55 return this.global.getTotal() - this.global.getAvailable() + getSwapUsed();
56 }
57
58 @Override
59 public long getSwapPagesIn() {
60 return pagesIn.get();
61 }
62
63 @Override
64 public long getSwapPagesOut() {
65 return pagesOut.get();
66 }
67
68 private static long querySwapUsed() {
69 String swapInfo = ExecutingCommand.getAnswerAt("swapinfo -k", 1);
70 String[] split = ParseUtil.whitespaces.split(swapInfo);
71 if (split.length < 5) {
72 return 0L;
73 }
74 return ParseUtil.parseLongOrDefault(split[2], 0L) << 10;
75 }
76
77 private static long querySwapTotal() {
78 return BsdSysctlUtil.sysctl("vm.swap_total", 0L);
79 }
80
81 private static long queryPagesIn() {
82 return BsdSysctlUtil.sysctl("vm.stats.vm.v_swappgsin", 0L);
83 }
84
85 private static long queryPagesOut() {
86 return BsdSysctlUtil.sysctl("vm.stats.vm.v_swappgsout", 0L);
87 }
88 }