liveBook · Manning

Goprocinfo

As explained in Build and Orchestrator in Go (From Scratch)

goprocinfo is a Go library for reading system and process metrics from the proc filesystem.

goprocinfo is a Go library that reads machine and process information exposed through Linux interfaces, chiefly the /proc filesystem. It removes the need to parse /proc files directly when a worker needs current CPU, memory, load, and disk information. section 6.2 section 6.6

The useful mental model is simple: goprocinfo supplies typed readers; a worker wraps their results in one Stats value; helper methods turn raw fields into the measurements needed for scheduling. The worker can then expose its current machine state through a /stats API alongside task operations. section 6.3 section 6.6

The main goprocinfo types divide responsibility by metric source:

  • LoadAvg reads /proc/loadavg through ReadLoadAvg() to provide load-average data. section 6.3
  • CPUStat reads /proc/stat through ReadStat() to provide CPU accounting fields such as user, system, idle, and I/O wait time. section 6.3
  • MemInfo reads /proc/meminfo through ReadMemInfo() to provide memory values, including total and available memory. section 6.3
  • Disk reads disk-related data through ReadDisk(), using Go’s syscall package rather than /proc/diskstats. section 6.3 section 6.2

Collecting a worker snapshot

A GetStats() function can assemble a Stats value by calling separate memory, disk, CPU, and load helpers. Each helper calls the matching reader, logs an error when the read fails, and returns an empty value so callers still receive a complete snapshot. Methods on Stats can then expose total and available memory, used disk space, CPU usage, and load without requiring API consumers to know the underlying files. section 6.3

CPU usage is derived from CPUStat rather than treated as a single raw field: idle time combines Idle and IOWait, while non-idle time combines user, nice, system, interrupt, soft-interrupt, and steal fields. The calculation guards against a zero total before returning a usage ratio. section 6.3

This wrapper keeps low-level collection local to the worker and gives the manager a uniform HTTP-facing view of worker capacity and use. Those CPU, memory, and disk metrics support scheduling decisions for Docker-container workloads. section 1.7 section 6.6

Listing 6.1 Using cat to look at the /proc/stat file
$ cat /proc/stat
cpu  724661 181 374910 105390121 4468 59434 23083 0 0 0   #1
cpu0 59580 33 29642 8786508 191 3560 2244 0 0 0           #2
cpu1 60502 3 31300 8779359 150 9016 2729 0 0 0
cpu2 58574 7 32002 8785331 139 3688 3159 0 0 0
cpu3 59564 9 30935 8787000 137 3259 2017 0 0 0
cpu4 59555 6 29208 8786670 369 3312 1950 0 0 0
cpu5 63148 16 37486 8755311 430 16914 2993 0 0 0
cpu6 60653 76 31349 8780196 483 4168 2040 0 0 0
cpu7 62622 2 33386 8781129 533 3402 1325 0 0 0
cpu8 60286 1 31729 8783928 542 3175 1219 0 0 0
cpu9 59229 2 29664 8787395 571 3038 1118 0 0 0
cpu10 59550 1 28925 8789436 468 2945 1100 0 0 0
cpu11 61392 18 29278 8787854 449 2952 1184 0 0 0

Inspecting /proc/stat makes the raw CPU counters visible before they are read into CPUStat and combined into a usage calculation. section 6.2 section 6.3

How Build and Orchestrator in Go (From Scratch) covers it

How deep it goes
Builds a Stats wrapper, helper methods, derived CPU usage, and error-handling readers rather than parsing /proc directly.
Examples
Demonstrates /proc/stat output, a Stats struct, metric helpers, CPU-usage calculation, and GetStats reader functions.
What it assumes
Assumes familiarity with non-trivial programming while providing runnable Go code and a narrowed Docker-worker scope.
What you can do after
Apply it via goprocinfo to collect and expose CPU, memory, disk, and load snapshots for a worker.
How it connects
Connects /proc filesystem, CPU usage, memory metrics, disk metrics, load average, HTTP APIs, and scheduling.

This explainer was generated by Manning's knowledge graph from the text of Build and Orchestrator in Go (From Scratch). The section links point to the parts of the book it draws on.