Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Ollama Ollama Discover CPU Linux

From Leeroopedia
Knowledge Sources
Domains HardwareDiscovery, Linux
Last Updated 2025-02-15 00:00 GMT

Overview

Linux-specific CPU memory and topology discovery module that reads from /proc/meminfo, /proc/cpuinfo, and cgroup filesystems to determine available system resources.

Description

GetCPUMem reads /proc/meminfo to obtain total, available, free, buffers, cached, and swap memory values, then adjusts for cgroup v2 limits via getCPUMemByCgroups (reading /sys/fs/cgroup/memory.max and /sys/fs/cgroup/memory.current). GetCPUDetails parses /proc/cpuinfo using reflection-based struct tag matching on linuxCpuInfo to extract per-processor entries (vendor, model name, physical ID, core ID, siblings), then aggregates them into physical sockets tracking core counts, thread counts, and hyperthreading ratios. overwriteThreadCountByLinuxCgroups further adjusts thread counts based on /sys/fs/cgroup/cpu.max quotas. IsNUMA checks for multiple physical package IDs in sysfs to detect NUMA topologies.

Usage

Called during Ollama startup to determine available system resources (memory, CPU cores/threads) for optimal model loading decisions and inference thread allocation.

Code Reference

Source Location

  • Repository: Ollama
  • File: discover/cpu_linux.go
  • Lines: 1-242

Signature

func GetCPUMem() (memInfo, error)
func GetCPUDetails() []CPU
func IsNUMA() bool

type linuxCpuInfo struct {
    ID         string `cpuinfo:"processor"`
    VendorID   string `cpuinfo:"vendor_id"`
    ModelName  string `cpuinfo:"model name"`
    PhysicalID string `cpuinfo:"physical id"`
    Siblings   string `cpuinfo:"siblings"`
    CoreID     string `cpuinfo:"core id"`
}

Import

import "github.com/ollama/ollama/discover"

I/O Contract

Inputs

Name Type Required Description
/proc/meminfo file Yes Linux kernel memory information
/proc/cpuinfo file Yes Linux kernel CPU topology information
/sys/fs/cgroup/memory.max file No Cgroup v2 memory limit
/sys/fs/cgroup/cpu.max file No Cgroup v2 CPU time quota

Outputs

Name Type Description
memInfo struct TotalMemory, FreeMemory, FreeSwap in bytes
[]CPU slice List of CPU sockets with core/thread counts and vendor info
isNUMA bool Whether the system has a NUMA topology

Usage Examples

mem, err := discover.GetCPUMem()
if err != nil {
    log.Fatal(err)
}
fmt.Printf("Total: %s, Free: %s\n",
    format.HumanBytes2(mem.TotalMemory),
    format.HumanBytes2(mem.FreeMemory))

cpus := discover.GetCPUDetails()
for _, cpu := range cpus {
    fmt.Printf("Socket %s: %s (%d cores, %d threads)\n",
        cpu.ID, cpu.ModelName, cpu.CoreCount, cpu.ThreadCount)
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment