How llmfit Detects System RAM and CPU Cores: Complete Implementation Guide
llmfit detects system RAM and CPU cores through the SystemSpecs::detect() method in llmfit-core/src/hardware.rs, which uses the sysinfo crate to gather hardware data with platform-specific fallbacks for macOS memory reporting and Windows APU memory carve-outs.
The llmfit CLI tool needs accurate hardware information to determine which LLM models can run on a given machine. This article explains how the Rust codebase probes total RAM, available RAM, and CPU core count—plus the edge cases the developers handled for cross-platform reliability.
Core Detection Logic in SystemSpecs::detect()
The hardware detection pipeline lives in a single function. According to the llmfit source code, SystemSpecs::detect() at llmfit-core/src/hardware.rs lines 73-89 performs three main operations:
- Initialize and refresh a
sysinfo::Systeminstance - Read memory values from the refreshed system object
- Count CPU cores from the CPU vector
Memory Detection
Total and available RAM are fetched directly from sysinfo:
use sysinfo::System;
let mut sys = System::new_all();
sys.refresh_all();
let total_bytes = sys.total_memory();
let available_bytes = sys.available_memory();
Both values return in bytes. The code converts them to gibibytes (GiB) by dividing by 1024³. This matches how operating systems typically display installed memory.
CPU Core Detection
Logical CPU cores are determined by vector length:
let total_cpu_cores = sys.cpus().len();
This returns the number of hardware threads, not physical cores. For model-fitting purposes, llmfit uses logical cores since that's what matters for concurrent inference workloads.
Platform-Specific Fallbacks
Raw sysinfo data isn't always accurate. The llmfit codebase includes two corrections:
macOS Available Memory Bug
sysinfo occasionally reports zero available memory on macOS. When this occurs, SystemSpecs::available_ram_fallback() (around line 80 in hardware.rs) queries macOS-specific APIs to estimate free memory.
Windows APU Memory Carve-Out
AMD APUs with unified memory architecture present a special case. The BIOS reserves a portion of system RAM for the integrated GPU, causing sys.total_memory() to underreport physical DIMM capacity.
The windows_apu_total_ram_gb() function (lines 96-100) fixes this by:
- Querying
Win32_PhysicalMemoryvia WMI - Comparing against the
sysinfototal - Correcting the value when a GPU carve-out is detected
SystemSpecs Output Structure
The final values populate a struct consumed throughout the application:
use llmfit_core::hardware::SystemSpecs;
fn main() {
let specs = SystemSpecs::detect();
println!("Total RAM: {:.2} GiB", specs.total_ram_gb);
println!("Available RAM: {:.2} GiB", specs.available_ram_gb);
println!("CPU cores: {}", specs.total_cpu_cores);
println!("CPU name: {}", specs.cpu_name);
}
Typical output:
Total RAM: 31.98 GiB
Available RAM: 28.45 GiB
CPU cores: 16
CPU name: Intel(R) Core(TM) i9-12900K
Integration with the Model-Fit Pipeline
The CLI entry point in llmfit-tui/src/main.rs calls SystemSpecs::detect() during startup. The resulting hardware profile feeds into llmfit-core/src/models.rs, where detected RAM and CPU cores filter and score compatible LLM models.
This design keeps hardware detection centralized in hardware.rs while making the data available to downstream components without duplication.
Summary
SystemSpecs::detect()inllmfit-core/src/hardware.rsis the single entry point for hardware detection- RAM values come from
sys.total_memory()andsys.available_memory(), converted from bytes to GiB - CPU cores are counted via
sys.cpus().len()returning logical processors - macOS fallback handles zero-value available memory reports
- Windows APU correction adjusts for BIOS-reserved GPU memory using WMI
- Downstream code in
models.rsuses these specs for model compatibility scoring
Frequently Asked Questions
Why does llmfit convert bytes to gibibytes instead of gigabytes?
Operating systems and hardware vendors historically report memory in binary units (GiB) while using the "GB" label. By dividing raw byte values by 1024³, llmfit matches user expectations and DIMM labeling—for example, showing "31.98 GiB" for a 32 GB installed module rather than "34.36" (decimal GB).
What's the difference between sys.total_memory() and sys.available_memory()?
total_memory() returns installed physical RAM, while available_memory() returns RAM not currently in use by processes or OS caches. Available memory is what actually matters for loading LLM weights, which is why llmfit tracks both values and implements the macOS fallback when the latter reports incorrectly.
Why does Windows with an AMD APU need special handling?
AMD APUs use unified memory architecture where system RAM doubles as GPU memory. The BIOS reserves a fixed portion (typically 512 MB–2 GB) for the integrated GPU, causing sysinfo to report less total RAM than physically installed. llmfit queries Win32_PhysicalMemory to detect this discrepancy and restore the true capacity.
Can I use SystemSpecs::detect() in my own Rust project?
Yes—the SystemSpecs struct and its detect() method are public exports from llmfit-core. Add llmfit-core as a dependency and call SystemSpecs::detect() directly. The same platform-specific corrections (macOS fallback, Windows APU fix) will apply automatically based on your target OS.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →