Simplify the implementation of get_nproc().
It came up on the musl mailing list that there's not actually any need
to iterate over the directory entries:
https://www.openwall.com/lists/musl/2022/07/27/1
This lets us reuse the code for "online" processors in the
implementation of "configured" processors. The question of whether
"configured" should correspond to Linux's "possible" or "present" isn't
obvious to me, but the distinction seems unlikely to matter on mobile
devices anyway, and that's a trivial change should it ever be needed.
Plus the motivating argument from the person who brought this up was
that callers asking for "configured" processors are probably asking for
an upper bound, which sounds convincing to me.
Test: treehugger
Change-Id: I0d4e13538dc6b09a6dba520d9ac24f436906f7c0
diff --git a/tests/unistd_test.cpp b/tests/unistd_test.cpp
index 02da585..5fce5b8 100644
--- a/tests/unistd_test.cpp
+++ b/tests/unistd_test.cpp
@@ -1103,6 +1103,10 @@
ASSERT_EQ(4, GetCpuCountFromString("0, 1-2, 4\n"));
}
+TEST(UNISTD_TEST, sysconf_SC_NPROCESSORS_make_sense) {
+ ASSERT_LE(sysconf(_SC_NPROCESSORS_ONLN), sysconf(_SC_NPROCESSORS_CONF));
+}
+
TEST(UNISTD_TEST, sysconf_SC_NPROCESSORS_ONLN) {
std::string line;
ASSERT_TRUE(android::base::ReadFileToString("/sys/devices/system/cpu/online", &line));