Skip to main content
The cpu package provides functions to retrieve CPU-related information including times spent in different modes, CPU information, core counts, and utilization percentages.

Import

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

Functions

Times

Get CPU time statistics for different execution modes

Info

Retrieve detailed CPU hardware information

Counts

Get the number of physical or logical CPU cores

Percent

Calculate CPU utilization percentage

Data Structures

TimesStat

Represents the amount of time the CPU has spent performing different types of work. All time values are in seconds.
type TimesStat struct {
    CPU       string  // CPU identifier (e.g., "cpu0", "cpu1", or "cpu-total")
    User      float64 // Time spent in user mode
    System    float64 // Time spent in system mode
    Idle      float64 // Time spent idle
    Nice      float64 // Time spent in user mode with low priority
    Iowait    float64 // Time spent waiting for I/O to complete
    Irq       float64 // Time spent servicing hardware interrupts
    Softirq   float64 // Time spent servicing software interrupts
    Steal     float64 // Time stolen by virtualization
    Guest     float64 // Time spent running a virtual CPU for guest OS
    GuestNice float64 // Time spent running a niced guest
}

InfoStat

Contains detailed information about a CPU core.
type InfoStat struct {
    CPU        int32    // CPU number
    VendorID   string   // CPU vendor ID (e.g., "GenuineIntel", "AuthenticAMD")
    Family     string   // CPU family
    Model      string   // CPU model
    Stepping   int32    // CPU stepping
    PhysicalID string   // Physical processor ID
    CoreID     string   // Core ID
    Cores      int32    // Number of cores
    ModelName  string   // Human-readable model name
    Mhz        float64  // CPU frequency in MHz
    CacheSize  int32    // Cache size in KB
    Flags      []string // CPU feature flags
    Microcode  string   // Microcode version
}

Quick Start

package main

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

func main() {
    // Get number of CPU cores
    logical, _ := cpu.Counts(true)
    physical, _ := cpu.Counts(false)
    fmt.Printf("Logical CPUs: %d, Physical CPUs: %d\n", logical, physical)
    
    // Get CPU information
    info, _ := cpu.Info()
    if len(info) > 0 {
        fmt.Printf("CPU: %s\n", info[0].ModelName)
        fmt.Printf("Frequency: %.2f MHz\n", info[0].Mhz)
    }
    
    // Get CPU usage percentage
    percent, _ := cpu.Percent(time.Second, false)
    fmt.Printf("CPU Usage: %.2f%%\n", percent[0])
    
    // Get per-CPU usage
    perCPU, _ := cpu.Percent(time.Second, true)
    for i, p := range perCPU {
        fmt.Printf("CPU%d Usage: %.2f%%\n", i, p)
    }
}

Platform Support

All functions work across multiple platforms:
  • Linux
  • Windows
  • macOS (Darwin)
  • FreeBSD
  • OpenBSD
  • NetBSD
  • DragonFly BSD
  • Solaris
  • AIX
  • Plan 9
Behavior may vary slightly between platforms based on available system APIs.

Build docs developers (and LLMs) love