Class SystemInfo

java.lang.Object
cloud.opencode.base.core.system.SystemInfo

public final class SystemInfo extends Object
SystemInfo - Comprehensive system information facade SystemInfo - 全面的系统信息门面

Provides detailed CPU, memory, disk, OS, and JVM runtime information beyond what Environment offers. All snapshot methods return immutable records that are safe to cache and share across threads.

提供比 Environment 更详细的 CPU、内存、磁盘、 操作系统和 JVM 运行时信息。所有快照方法返回不可变记录,可安全缓存和跨线程共享。

Features | 主要功能:

  • CPU load and processor info - CPU 负载和处理器信息
  • JVM heap/non-heap and physical memory metrics - JVM 堆/非堆及物理内存指标
  • Disk/file-store capacity and usage - 磁盘/文件存储容量和使用情况
  • OS identification and hostname - 操作系统识别和主机名
  • JVM runtime details (version, uptime, PID, arguments) - JVM 运行时详情

Usage Examples | 使用示例:

// CPU information
CpuInfo cpu = SystemInfo.cpu();
double load = SystemInfo.cpuLoad();

// Memory information
MemoryInfo heap = SystemInfo.heapMemory();
long physMem = SystemInfo.physicalMemoryTotal();

// Disk information
List<DiskInfo> disks = SystemInfo.disks();
DiskInfo root = SystemInfo.disk(Path.of("/"));

// OS and runtime
String host = SystemInfo.hostname();
RuntimeInfo rt = SystemInfo.runtime();

Thread Safety | 线程安全:

All methods are stateless and thread-safe. Returned records are immutable.

所有方法都是无状态且线程安全的。返回的记录是不可变的。

Since:
JDK 25, opencode-base-core V1.0.3
Author:
Leon Soo
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static CpuInfo
    cpu()
    Returns a snapshot of current CPU information.
    static double
    Returns the current system-wide CPU load as a value in [0.0, 1.0], or -1.0 if not available.
    static DiskInfo
    disk(Path path)
    Returns disk information for the file store containing the given path.
    static long
    diskFree(Path path)
    Returns the free (unallocated) disk space for the given path, in bytes.
    static List<DiskInfo>
    Returns information for all file stores (disks/partitions).
    static long
    Returns the total disk space for the given path, in bytes.
    static long
    Returns the usable disk space for the given path, in bytes.
    static Map<String,String>
    Returns an unmodifiable view of the system environment variables.
    static MemoryInfo
    Returns a snapshot of JVM heap memory information.
    static String
    Returns the machine hostname.
    static double[]
    Returns the system load averages for 1, 5, and 15 minutes.
    static MemoryInfo
    Returns a snapshot of physical memory information.
    static MemoryInfo
    Returns a snapshot of JVM non-heap memory information.
    static OsInfo
    os()
    Returns a snapshot of operating system information.
    static long
    Returns the free physical memory in bytes, or -1 if unavailable.
    static long
    Returns the total physical memory in bytes, or -1 if unavailable.
    static double
    Returns the current JVM process CPU load as a value in [0.0, 1.0], or -1.0 if not available.
    Returns a snapshot of JVM runtime information.
    static long
    Returns the free swap space in bytes, or -1 if unavailable.
    static long
    Returns the total swap space in bytes, or -1 if unavailable.
    static long
    Returns the JVM uptime in milliseconds.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • cpu

      public static CpuInfo cpu()
      Returns a snapshot of current CPU information. 返回当前 CPU 信息的快照。
      Returns:
      CPU info snapshot
    • cpuLoad

      public static double cpuLoad()
      Returns the current system-wide CPU load as a value in [0.0, 1.0], or -1.0 if not available. 返回当前系统级 CPU 负载,值在 [0.0, 1.0] 范围内,不可用时返回 -1.0。
      Returns:
      system CPU load, or -1.0 if unavailable
    • processCpuLoad

      public static double processCpuLoad()
      Returns the current JVM process CPU load as a value in [0.0, 1.0], or -1.0 if not available. 返回当前 JVM 进程 CPU 负载,值在 [0.0, 1.0] 范围内,不可用时返回 -1.0。
      Returns:
      process CPU load, or -1.0 if unavailable
    • loadAverage

      public static double[] loadAverage()
      Returns the system load averages for 1, 5, and 15 minutes. 返回 1、5、15 分钟的系统平均负载。

      On Linux, attempts to read /proc/loadavg for all three values. On other Unix-like systems, returns the 1-minute average from OperatingSystemMXBean.getSystemLoadAverage(). On Windows, returns an empty array.

      在 Linux 上尝试从 /proc/loadavg 读取全部三个值。 在其他类 Unix 系统上,返回 OperatingSystemMXBean.getSystemLoadAverage() 的 1 分钟平均值。在 Windows 上返回空数组。

      Returns:
      load average array (may be empty on unsupported platforms)
    • memory

      public static MemoryInfo memory()
      Returns a snapshot of physical memory information. 返回物理内存信息的快照。
      Returns:
      physical memory info snapshot
    • heapMemory

      public static MemoryInfo heapMemory()
      Returns a snapshot of JVM heap memory information. 返回 JVM 堆内存信息的快照。
      Returns:
      heap memory info snapshot
    • nonHeapMemory

      public static MemoryInfo nonHeapMemory()
      Returns a snapshot of JVM non-heap memory information. 返回 JVM 非堆内存信息的快照。
      Returns:
      non-heap memory info snapshot
    • physicalMemoryTotal

      public static long physicalMemoryTotal()
      Returns the total physical memory in bytes, or -1 if unavailable. 返回总物理内存(字节),不可用时返回 -1。
      Returns:
      total physical memory bytes, or -1
    • physicalMemoryFree

      public static long physicalMemoryFree()
      Returns the free physical memory in bytes, or -1 if unavailable. 返回空闲物理内存(字节),不可用时返回 -1。
      Returns:
      free physical memory bytes, or -1
    • swapTotal

      public static long swapTotal()
      Returns the total swap space in bytes, or -1 if unavailable. 返回总交换空间(字节),不可用时返回 -1。
      Returns:
      total swap bytes, or -1
    • swapFree

      public static long swapFree()
      Returns the free swap space in bytes, or -1 if unavailable. 返回空闲交换空间(字节),不可用时返回 -1。
      Returns:
      free swap bytes, or -1
    • disks

      public static List<DiskInfo> disks()
      Returns information for all file stores (disks/partitions). 返回所有文件存储(磁盘/分区)的信息。

      File stores that throw exceptions during query are silently skipped.

      查询时抛出异常的文件存储将被静默跳过。

      Returns:
      unmodifiable list of disk info snapshots
    • disk

      public static DiskInfo disk(Path path) throws IOException
      Returns disk information for the file store containing the given path. 返回包含给定路径的文件存储的磁盘信息。
      Parameters:
      path - the path to query - 要查询的路径
      Returns:
      disk info for the path's file store
      Throws:
      IOException - if an I/O error occurs
      NullPointerException - if path is null
    • diskTotal

      public static long diskTotal(Path path)
      Returns the total disk space for the given path, in bytes. 返回给定路径的总磁盘空间(字节)。
      Parameters:
      path - the path to query - 要查询的路径
      Returns:
      total disk space in bytes, or -1 if an error occurs
    • diskFree

      public static long diskFree(Path path)
      Returns the free (unallocated) disk space for the given path, in bytes. 返回给定路径的空闲(未分配)磁盘空间(字节)。
      Parameters:
      path - the path to query - 要查询的路径
      Returns:
      free disk space in bytes, or -1 if an error occurs
    • diskUsable

      public static long diskUsable(Path path)
      Returns the usable disk space for the given path, in bytes. 返回给定路径的可用磁盘空间(字节)。

      Usable space takes into account OS-level restrictions and may be less than unallocated space.

      可用空间考虑了操作系统级别的限制,可能小于未分配空间。

      Parameters:
      path - the path to query - 要查询的路径
      Returns:
      usable disk space in bytes, or -1 if an error occurs
    • os

      public static OsInfo os()
      Returns a snapshot of operating system information. 返回操作系统信息的快照。

      This method performs several system calls (hostname resolution, MXBean queries). Cache the returned OsInfo if you need repeated access.

      此方法执行多个系统调用(主机名解析、MXBean 查询)。 如果需要反复访问,请缓存返回的 OsInfo

      Returns:
      OS info snapshot
    • hostname

      public static String hostname()
      Returns the machine hostname. 返回机器主机名。

      Falls back to "unknown" if hostname resolution fails.

      如果主机名解析失败则回退为 "unknown"。

      Returns:
      the hostname, or "unknown" on failure
    • uptime

      public static long uptime()
      Returns the JVM uptime in milliseconds. 返回 JVM 运行时间(毫秒)。
      Returns:
      uptime in milliseconds
    • environmentVariables

      public static Map<String,String> environmentVariables()
      Returns an unmodifiable view of the system environment variables. 返回系统环境变量的不可修改视图。

      Security Warning | 安全警告: Environment variables may contain sensitive credentials (API keys, database passwords, tokens). Never log, serialize, or expose the returned map to untrusted parties.

      安全警告: 环境变量可能包含敏感凭据(API 密钥、数据库密码、令牌)。 切勿将返回的 Map 记录日志、序列化或暴露给不可信方。

      Returns:
      unmodifiable map of environment variables
    • runtime

      public static RuntimeInfo runtime()
      Returns a snapshot of JVM runtime information. 返回 JVM 运行时信息的快照。
      Returns:
      runtime info snapshot