content: /*
   ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
   ~                                                                               ~
   ~ The MIT License (MIT)                                                         ~
   ~                                                                               ~
   ~ Copyright (c) 2015-2025 miaixz.org OSHI and other 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 org.miaixz.bus.health.mac.hardware;
  
  import java.util.function.Supplier;
  
  import org.miaixz.bus.core.lang.annotation.ThreadSafe;
  import org.miaixz.bus.core.lang.tuple.Pair;
  import org.miaixz.bus.health.Memoizer;
  import org.miaixz.bus.health.Parsing;
  import org.miaixz.bus.health.builtin.hardware.common.AbstractVirtualMemory;
  import org.miaixz.bus.health.builtin.jna.ByRef;
  import org.miaixz.bus.health.builtin.jna.Struct;
  import org.miaixz.bus.health.mac.SysctlKit;
  import org.miaixz.bus.logger.Logger;
  
  import com.sun.jna.Native;
  import com.sun.jna.platform.mac.SystemB;
  
  /**
   * Memory obtained by host_statistics (vm_stat) and sysctl.
   *
   * @author Kimi Liu
   * @since Java 17+
   */
  @ThreadSafe
  final class MacVirtualMemory extends AbstractVirtualMemory {
  
      private final MacGlobalMemory global;
  
      private final Supplier<Pair<Long, Long>> usedTotal = Memoizer
              .memoize(MacVirtualMemory::querySwapUsage, Memoizer.defaultExpiration());
  
      private final Supplier<Pair<Long, Long>> inOut = Memoizer
              .memoize(MacVirtualMemory::queryVmStat, Memoizer.defaultExpiration());
  
      /**
       * Constructor for MacVirtualMemory.
       *
       * @param macGlobalMemory The parent global memory class instantiating this
       */
      MacVirtualMemory(MacGlobalMemory macGlobalMemory) {
          this.global = macGlobalMemory;
      }
  
      private static Pair<Long, Long> querySwapUsage() {
          long swapUsed = 0L;
          long swapTotal = 0L;
          try (Struct.CloseableXswUsage xswUsage = new Struct.CloseableXswUsage()) {
              if (SysctlKit.sysctl("vm.swapusage", xswUsage)) {
                  swapUsed = xswUsage.xsu_used;
                  swapTotal = xswUsage.xsu_total;
              }
          }
          return Pair.of(swapUsed, swapTotal);
      }
  
      private static Pair<Long, Long> queryVmStat() {
          long swapPagesIn = 0L;
          long swapPagesOut = 0L;
          try (Struct.CloseableVMStatistics vmStats = new Struct.CloseableVMStatistics();
                  ByRef.CloseableIntByReference size = new ByRef.CloseableIntByReference(
                          vmStats.size() / SystemB.INT_SIZE)) {
              if (0 == SystemB.INSTANCE
                      .host_statistics(SystemB.INSTANCE.mach_host_self(), SystemB.HOST_VM_INFO, vmStats, size)) {
                  swapPagesIn = Parsing.unsignedIntToLong(vmStats.pageins);
                  swapPagesOut = Parsing.unsignedIntToLong(vmStats.pageouts);
              } else {
                  Logger.error("Failed to get host VM info. Error code: {}", Native.getLastError());
              }
          }
          return Pair.of(swapPagesIn, swapPagesOut);
      }
  
      @Override
      public long getSwapUsed() {
          return usedTotal.get().getLeft();
      }
  
      @Override
      public long getSwapTotal() {
          return usedTotal.get().getRight();
      }
  
      @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().getLeft();
      }
  
      @Override
      public long getSwapPagesOut() {
          return inOut.get().getRight();
      }
  
  }
  