Skip to main content

Overview

gopsutil provides strong support for macOS (Darwin) across both Intel and Apple Silicon architectures. The implementation uses native macOS APIs and syscalls for accurate system monitoring.

Supported Architectures

amd64 (Intel)

Intel-based Macs (pre-2020 and some current models)

arm64 (Apple Silicon)

M1, M2, M3, and newer Apple Silicon processors
gopsutil fully supports both Intel and Apple Silicon Macs with identical APIs across architectures.

Feature Support

Fully Supported Features

CPU Metrics

  • cpu.Times() - CPU time statistics
  • cpu.Count() - Number of logical and physical CPUs
  • cpu.Percent() - CPU utilization percentage
  • cpu.TimesPercent() - Per-CPU time percentages
  • cpu.Info() - Detailed CPU information:
    • VendorID
    • Family, Model, Stepping
    • Number of cores
    • ModelName (e.g., “Apple M1”, “Intel(R) Core(TM) i7-9750H”)
    • CPU features and capabilities

Memory Metrics

  • mem.VirtualMemory() - Physical memory statistics:
    • Total, available, used memory
    • Memory pressure and usage percentage
    • Active, inactive, wired, and free memory
  • mem.SwapMemory() - Swap memory information:
    • Total swap space
    • Used and free swap
    • Swap ins and outs

Disk Metrics

  • disk.Partitions() - List all disk partitions
  • disk.Usage() - Disk usage statistics per partition
  • disk.IOCounters() - I/O statistics for disk drives

Network Metrics

  • net.IOCounters() - Network interface statistics
  • net.Connections() - Active network connections
  • net.Interfaces() - Network interface information
net.IOCounters() has partial support (marked as ‘b’ in compatibility table) with some known limitations.

System Information

  • host.Info() - Comprehensive host information:
    • Hostname
    • Uptime
    • OS (“darwin”)
    • Platform (e.g., “darwin”)
    • PlatformFamily
    • PlatformVersion (macOS version)
    • KernelVersion
    • KernelArch
  • host.BootTime() - System boot time
  • host.Users() - Currently logged-in users
  • load.Avg() - System load averages (1, 5, 15 minutes)

Temperature Sensors

import "github.com/shirou/gopsutil/v4/sensors"

temps, err := sensors.TemperaturesWithContext(ctx)
macOS supports temperature sensor reading for monitoring CPU and system temperatures.

Process Information

macOS provides comprehensive process monitoring:

Supported Process Features

  • Basic: pid, ppid, name, cmdline, create_time, cwd
  • User/Group: uids, gids, username
  • Resources: memory_info, cpu_percent
  • File Descriptors: num_fds
  • Priority: nice (process priority)
  • Threads: num_threads
  • Relations: parent, children
  • Control: send_signal, suspend, resume, terminate, kill
  • Network: connections (per-process network connections)

Partially Supported

  • status: Process state information (running, sleeping, etc.)

Unsupported Process Features

  • exe (executable path)
  • terminal
  • io_counters
  • cpu_times
  • memory_maps
  • open_files
  • num_ctx_switches
  • rlimit
  • threads (detailed thread info)
  • memory_percent
  • page_faults

macOS-Specific Considerations

System Integrity Protection (SIP)

macOS System Integrity Protection may restrict access to certain system information:
import "github.com/shirou/gopsutil/v4/process"

// May fail for system processes or processes owned by other users
p, err := process.NewProcess(1)  // PID 1 is launchd
if err != nil {
    log.Printf("Cannot access process: %v", err)
}
Some process information may require elevated privileges or may be restricted by SIP.

Rosetta 2 on Apple Silicon

On Apple Silicon Macs, x86_64 binaries run through Rosetta 2 translation:
import "runtime"

fmt.Println("Architecture:", runtime.GOARCH)
// Output: arm64 (native) or amd64 (Rosetta 2)
gopsutil works correctly under both native ARM64 and Rosetta 2 translation.

Darwin Syscalls

macOS implementations use Darwin-specific syscalls:
  • sysctl for system information
  • host_statistics64 for host stats
  • task_info for process information
  • proc_pidinfo for process details

APFS File System

On modern macOS systems with APFS:
import "github.com/shirou/gopsutil/v4/disk"

partitions, err := disk.Partitions(false)
for _, p := range partitions {
    // APFS partitions have type "apfs"
    fmt.Printf("%s: %s (%s)\n", p.Device, p.Mountpoint, p.Fstype)
}
APFS snapshots and space sharing may affect reported disk usage.

Known Limitations

Network Interface Stats

Network I/O counters have some limitations:
  • Certain interface types may not report all metrics
  • Virtual interfaces may have incomplete statistics
counters, err := net.IOCounters(true)  // per-interface
for _, counter := range counters {
    // Some fields may be zero or unavailable
    fmt.Printf("%s: %d bytes in, %d bytes out\n",
        counter.Name, counter.BytesRecv, counter.BytesSent)
}

Process Executable Path

The exe field is not supported for processes on macOS:
p, _ := process.NewProcess(pid)
// exe, err := p.Exe()  // Not supported on macOS

Process I/O Counters

Per-process I/O statistics are not available:
p, _ := process.NewProcess(pid)
// ioCounters, err := p.IOCounters()  // Not supported

Open Files

The OpenFiles() method is not currently supported:
p, _ := process.NewProcess(pid)
// files, err := p.OpenFiles()  // Not supported

Code Examples

Basic System Monitoring

package main

import (
    "fmt"
    "github.com/shirou/gopsutil/v4/cpu"
    "github.com/shirou/gopsutil/v4/disk"
    "github.com/shirou/gopsutil/v4/host"
    "github.com/shirou/gopsutil/v4/load"
    "github.com/shirou/gopsutil/v4/mem"
    "time"
)

func main() {
    // Host info
    info, _ := host.Info()
    fmt.Printf("OS: %s %s\n", info.Platform, info.PlatformVersion)
    fmt.Printf("Kernel: %s\n", info.KernelVersion)
    
    // CPU usage
    percent, _ := cpu.Percent(time.Second, false)
    fmt.Printf("CPU Usage: %.2f%%\n", percent[0])
    
    // Load average
    avg, _ := load.Avg()
    fmt.Printf("Load average: %.2f %.2f %.2f\n", avg.Load1, avg.Load5, avg.Load15)
    
    // Memory
    vmem, _ := mem.VirtualMemory()
    fmt.Printf("Memory: %.2f%% used (%.2f GB / %.2f GB)\n",
        vmem.UsedPercent,
        float64(vmem.Used)/1024/1024/1024,
        float64(vmem.Total)/1024/1024/1024)
    
    // Disk usage for root
    usage, _ := disk.Usage("/")
    fmt.Printf("Disk /: %.2f%% used\n", usage.UsedPercent)
}

CPU Information

package main

import (
    "fmt"
    "github.com/shirou/gopsutil/v4/cpu"
)

func main() {
    // Get CPU info
    info, _ := cpu.Info()
    for i, ci := range info {
        fmt.Printf("CPU %d:\n", i)
        fmt.Printf("  Model: %s\n", ci.ModelName)
        fmt.Printf("  Cores: %d\n", ci.Cores)
        fmt.Printf("  MHz: %.2f\n", ci.Mhz)
    }
    
    // Physical vs logical cores
    physical, _ := cpu.Counts(false)
    logical, _ := cpu.Counts(true)
    fmt.Printf("\nPhysical cores: %d\n", physical)
    fmt.Printf("Logical cores: %d\n", logical)
}

Memory Details

package main

import (
    "fmt"
    "github.com/shirou/gopsutil/v4/mem"
)

func main() {
    vmem, _ := mem.VirtualMemory()
    
    fmt.Printf("Total: %.2f GB\n", float64(vmem.Total)/1024/1024/1024)
    fmt.Printf("Available: %.2f GB\n", float64(vmem.Available)/1024/1024/1024)
    fmt.Printf("Used: %.2f GB (%.2f%%)\n",
        float64(vmem.Used)/1024/1024/1024,
        vmem.UsedPercent)
    fmt.Printf("Free: %.2f GB\n", float64(vmem.Free)/1024/1024/1024)
    
    // macOS-specific memory stats
    fmt.Printf("\nActive: %.2f GB\n", float64(vmem.Active)/1024/1024/1024)
    fmt.Printf("Inactive: %.2f GB\n", float64(vmem.Inactive)/1024/1024/1024)
    fmt.Printf("Wired: %.2f GB\n", float64(vmem.Wired)/1024/1024/1024)
    
    // Swap info
    swap, _ := mem.SwapMemory()
    fmt.Printf("\nSwap Total: %.2f GB\n", float64(swap.Total)/1024/1024/1024)
    fmt.Printf("Swap Used: %.2f GB\n", float64(swap.Used)/1024/1024/1024)
}

Process Monitoring

package main

import (
    "fmt"
    "github.com/shirou/gopsutil/v4/process"
)

func main() {
    processes, _ := process.Processes()
    
    for _, p := range processes {
        name, err := p.Name()
        if err != nil {
            continue  // Skip processes we can't access
        }
        
        cpuPercent, _ := p.CPUPercent()
        memInfo, _ := p.MemoryInfo()
        
        if cpuPercent > 1.0 {  // Show processes using > 1% CPU
            fmt.Printf("PID: %d, Name: %s, CPU: %.2f%%, Memory: %d MB\n",
                p.Pid, name, cpuPercent, memInfo.RSS/1024/1024)
        }
    }
}

Best Practices

Handle Permission Errors

import "github.com/shirou/gopsutil/v4/process"

processes, _ := process.Processes()
for _, p := range processes {
    name, err := p.Name()
    if err != nil {
        // Skip processes we can't access (permission denied)
        continue
    }
    // Process accessible information
}

Use Context for Timeouts

import (
    "context"
    "time"
    "github.com/shirou/gopsutil/v4/cpu"
)

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

info, err := cpu.InfoWithContext(ctx)
if err != nil {
    log.Printf("CPU info query failed: %v", err)
}

Monitor Load Averages

import "github.com/shirou/gopsutil/v4/load"

avg, _ := load.Avg()

// On macOS, load average above CPU count indicates high load
logical, _ := cpu.Counts(true)

if avg.Load1 > float64(logical) {
    fmt.Println("System is under heavy load")
}

Check Architecture

import "runtime"

if runtime.GOOS == "darwin" {
    if runtime.GOARCH == "arm64" {
        fmt.Println("Running on Apple Silicon")
    } else {
        fmt.Println("Running on Intel Mac")
    }
}

Apple Silicon Notes

Performance Cores vs Efficiency Cores

Apple Silicon Macs have both performance (P) and efficiency (E) cores:
physical, _ := cpu.Counts(false)
logical, _ := cpu.Counts(true)

// M1: 8 cores (4P + 4E), 8 logical
// M1 Pro: 10 cores (8P + 2E), 10 logical
// M2: 8 cores (4P + 4E), 8 logical

Energy Efficiency

Apple Silicon is more energy-efficient. Monitor power usage with temperature sensors:
import "github.com/shirou/gopsutil/v4/sensors"

temps, _ := sensors.TemperaturesWithContext(ctx)
for _, temp := range temps {
    fmt.Printf("%s: %.1f°C\n", temp.SensorKey, temp.Temperature)
}

Platform Overview

Compare macOS support with other platforms

BSD Systems

Learn about BSD platform support (Darwin is BSD-based)

Build docs developers (and LLMs) love