Skip to content

android: fall back to /proc/cpuinfo when /proc/stat is unreadable - #1715

Merged
GuillaumeGomez merged 2 commits into
GuillaumeGomez:mainfrom
dvrdnz:android-cpu-fallback
Aug 17, 2026
Merged

android: fall back to /proc/cpuinfo when /proc/stat is unreadable#1715
GuillaumeGomez merged 2 commits into
GuillaumeGomez:mainfrom
dvrdnz:android-cpu-fallback

Conversation

@dvrdnz

@dvrdnz dvrdnz commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

Problem

On Android, /proc/stat is typically unreadable by regular apps (including
Termux) — it's blocked by SELinux policy since Android 8. When this happens,
CpusWrapper::refresh() currently returns early with an empty cpus list,
so no CPU information (name, vendor, brand, frequency) is ever populated,
even though /proc/cpuinfo — which is readable — has all of it.

Fix

Add an Android-only fallback, build_cpus_from_cpuinfo, that builds the CPU
list from /proc/cpuinfo (via the existing get_vendor_id_and_brand) when
opening /proc/stat fails. It's only triggered on the first refresh (first),
matching the existing pattern used elsewhere in this function. CPU frequency
is optionally populated via the existing get_cpu_frequency, which reads
/sys/devices/system/cpu/cpuN/cpufreq/scaling_cur_freq and doesn't depend on
/proc/stat.

Note that cpu_usage() will remain 0.0 for CPUs built this way — usage
percentages require successive /proc/stat snapshots, and there's no public
Android API providing equivalent per-CPU tick counters. This is documented on
the new function.

Testing

Verified on a physical Android device (Termux):

  • Before: System::new_all()cpus() returns 0 CPUs
  • After: cpus() returns 8 CPUs, each with correct name (cpu0..cpu7),
    vendor (ARM), brand (e.g. Cortex-A725), and frequency

This was originally surfaced while building
otree on Termux via cargo install,
where vergen's si feature emitted VERGEN_SYSINFO_CPU_VENDOR /
_CPU_BRAND / _CPU_FREQUENCY warnings because sysinfo couldn't resolve
CPU info on this platform.

Companion PR for the same root cause, affecting Users: #1714

Added a function to build CPU list from /proc/cpuinfo for Android when /proc/stat is unreadable.
Comment thread src/unix/linux/cpu.rs
@@ -63,6 +63,11 @@ impl CpusWrapper {
Ok(f) => f,
Err(_e) => {
sysinfo_debug!("failed to retrieve CPU information: {:?}", _e);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe only move/modify this sysinfo_debug if on android since the information is still kinda computed.

Added logging for CPU usage information retrieval failure on Android.
@dvrdnz

dvrdnz commented Aug 16, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, good point. I updated this so the non-Android path keeps the existing
debug message, while Android now logs that CPU usage information could not be retrieved from /proc/stat.

I also rechecked the behavior on the Android/Termux device:

  • /proc/stat is not readable: Permission denied
  • /proc/cpuinfo is readable and lists 8 processors
  • main returns 0 CPUs from System::new_all()
  • with this PR, System::new_all() returns 8 CPUs
  • names are cpu0..cpu7, vendor is ARM, brand is Cortex-A725
  • frequency is populated
  • usage remains 0 as expected, because it still needs /proc/stat snapshots

So the fallback only recovers the static CPU information and frequency; CPU usage remains unavailable on this Android path.

@GuillaumeGomez
GuillaumeGomez merged commit bd698f1 into GuillaumeGomez:main Aug 17, 2026
31 checks passed
@dvrdnz
dvrdnz deleted the android-cpu-fallback branch August 17, 2026 09:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants