Fix the way to count online cpus.
Read /proc/stat to count online cpus is not correct for all android
kernels. Change to reading /sys/devices/system/cpu/online instead.
Bug: 24376925
Change-Id: I3785a6c7aa15a467022a9a261b457194d688fb38
diff --git a/libc/bionic/sysinfo.cpp b/libc/bionic/sysinfo.cpp
index a48bfea..1cb5c79 100644
--- a/libc/bionic/sysinfo.cpp
+++ b/libc/bionic/sysinfo.cpp
@@ -33,6 +33,7 @@
#include <string.h>
#include <unistd.h>
+#include "private/get_cpu_count_from_string.h"
#include "private/ScopedReaddir.h"
static bool __matches_cpuN(const char* s) {
@@ -61,26 +62,18 @@
}
int get_nprocs() {
- FILE* fp = fopen("/proc/stat", "re");
- if (fp == NULL) {
- return 1;
- }
-
- int result = 0;
- char buf[256];
- while (fgets(buf, sizeof(buf), fp) != NULL) {
- // Extract just the first word from the line.
- // 'cpu0 7976751 1364388 3116842 469770388 8629405 0 49047 0 0 0'
- char* p = strchr(buf, ' ');
- if (p != NULL) {
- *p = 0;
+ int cpu_count = 1;
+ FILE* fp = fopen("/sys/devices/system/cpu/online", "re");
+ if (fp != nullptr) {
+ char* line = nullptr;
+ size_t len = 0;
+ if (getline(&line, &len, fp) != -1) {
+ cpu_count = GetCpuCountFromString(line);
+ free(line);
}
- if (__matches_cpuN(buf)) {
- ++result;
- }
+ fclose(fp);
}
- fclose(fp);
- return result;
+ return cpu_count;
}
static int __get_meminfo_page_count(const char* pattern) {