oshi.hardware.platform.mac.MacVirtualMemory Maven / Gradle / Ivy
/**
* MIT License
*
* Copyright (c) 2010 - 2021 The OSHI Project Contributors: https://github.com/oshi/oshi/graphs/contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package oshi.hardware.platform.mac;
import static oshi.util.Memoizer.defaultExpiration;
import static oshi.util.Memoizer.memoize;
import java.util.function.Supplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sun.jna.Native; // NOSONAR
import com.sun.jna.platform.mac.SystemB;
import com.sun.jna.platform.mac.SystemB.VMStatistics;
import com.sun.jna.platform.mac.SystemB.XswUsage;
import com.sun.jna.ptr.IntByReference;
import oshi.annotation.concurrent.ThreadSafe;
import oshi.hardware.common.AbstractVirtualMemory;
import oshi.util.ParseUtil;
import oshi.util.platform.mac.SysctlUtil;
import oshi.util.tuples.Pair;
/**
* Memory obtained by host_statistics (vm_stat) and sysctl.
*/
@ThreadSafe
final class MacVirtualMemory extends AbstractVirtualMemory {
private static final Logger LOG = LoggerFactory.getLogger(MacVirtualMemory.class);
private final MacGlobalMemory global;
private final Supplier> usedTotal = memoize(MacVirtualMemory::querySwapUsage, defaultExpiration());
private final Supplier> inOut = memoize(MacVirtualMemory::queryVmStat, defaultExpiration());
/**
* Constructor for MacVirtualMemory.
*
* @param macGlobalMemory
* The parent global memory class instantiating this
*/
MacVirtualMemory(MacGlobalMemory macGlobalMemory) {
this.global = macGlobalMemory;
}
@Override
public long getSwapUsed() {
return usedTotal.get().getA();
}
@Override
public long getSwapTotal() {
return usedTotal.get().getB();
}
@Override
public long getVirtualMax() {
return this.global.getTotal() + getSwapTotal();
}
@Override
public long getVirtualInUse() {
return this.global.getTotal() - this.global.getAvailable() + getSwapUsed();
}
@Override
public long getSwapPagesIn() {
return inOut.get().getA();
}
@Override
public long getSwapPagesOut() {
return inOut.get().getB();
}
private static Pair querySwapUsage() {
long swapUsed = 0L;
long swapTotal = 0L;
XswUsage xswUsage = new XswUsage();
if (SysctlUtil.sysctl("vm.swapusage", xswUsage)) {
swapUsed = xswUsage.xsu_used;
swapTotal = xswUsage.xsu_total;
}
return new Pair<>(swapUsed, swapTotal);
}
private static Pair queryVmStat() {
long swapPagesIn = 0L;
long swapPagesOut = 0L;
VMStatistics vmStats = new VMStatistics();
if (0 == SystemB.INSTANCE.host_statistics(SystemB.INSTANCE.mach_host_self(), SystemB.HOST_VM_INFO, vmStats,
new IntByReference(vmStats.size() / SystemB.INT_SIZE))) {
swapPagesIn = ParseUtil.unsignedIntToLong(vmStats.pageins);
swapPagesOut = ParseUtil.unsignedIntToLong(vmStats.pageouts);
} else {
LOG.error("Failed to get host VM info. Error code: {}", Native.getLastError());
}
return new Pair<>(swapPagesIn, swapPagesOut);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy