summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 661423a)
raw | patch | inline | side by side (parent: 661423a)
author | Yabin Cui <yabinc@google.com> | |
Fri, 14 Nov 2014 23:51:58 +0000 (15:51 -0800) | ||
committer | Yabin Cui <yabinc@google.com> | |
Sat, 15 Nov 2014 00:22:48 +0000 (16:22 -0800) |
libc/Android.mk | patch | blob | history | |
libc/bionic/sysconf.cpp | patch | blob | history | |
libc/bionic/sysinfo.cpp | [new file with mode: 0644] | patch | blob |
libc/include/sys/sysinfo.h | patch | blob | history | |
tests/Android.mk | patch | blob | history | |
tests/sys_sysinfo_test.cpp | [new file with mode: 0644] | patch | blob |
diff --git a/libc/Android.mk b/libc/Android.mk
index 345a1f3f036756918ff6d84f6cac3ec5a16b3230..7d438f48b11ff8657d68eda2a5bacf2b3dae3bbb 100644 (file)
--- a/libc/Android.mk
+++ b/libc/Android.mk
bionic/stubs.cpp \
bionic/symlink.cpp \
bionic/sysconf.cpp \
+ bionic/sysinfo.cpp \
bionic/syslog.cpp \
bionic/sys_siglist.c \
bionic/sys_signame.c \
index fb704ad1ee634ff96379ffea90803f059644e19b..2df6accc5e6dd96abf9f774db294425c96911189 100644 (file)
--- a/libc/bionic/sysconf.cpp
+++ b/libc/bionic/sysconf.cpp
* SUCH DAMAGE.
*/
-#include <ctype.h>
-#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <pthread.h>
#include <stdio.h> // For FOPEN_MAX.
-#include <string.h>
#include <sys/sysconf.h>
+#include <sys/sysinfo.h>
#include <time.h>
#include <unistd.h>
#include "private/bionic_tls.h"
-#include "private/ScopedReaddir.h"
/* seems to be the default on Linux, per the GLibc sources and my own digging */
#define SYSTEM_2_UPE -1 /* No UPE for you ! (User Portability Utilities) */
#define SYSTEM_2_VERSION -1 /* No posix command-line tools */
-static bool __matches_cpuN(const char* s) {
- // The %c trick is to ensure that we have the anchored match "^cpu[0-9]+$".
- unsigned cpu;
- char dummy;
- return (sscanf(s, "cpu%u%c", &cpu, &dummy) == 1);
-}
-
-static int __sysconf_nprocessors_conf() {
- // On x86 kernels you can use /proc/cpuinfo for this, but on ARM kernels offline CPUs disappear
- // from there. This method works on both.
- ScopedReaddir reader("/sys/devices/system/cpu");
- if (reader.IsBad()) {
- return 1;
- }
-
- int result = 0;
- dirent* entry;
- while ((entry = reader.ReadEntry()) != NULL) {
- if (entry->d_type == DT_DIR && __matches_cpuN(entry->d_name)) {
- ++result;
- }
- }
- return result;
-}
-
-static int __sysconf_nprocessors_onln() {
- 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;
- }
- if (__matches_cpuN(buf)) {
- ++result;
- }
- }
- fclose(fp);
- return result;
-}
-
-static int __get_meminfo(const char* pattern) {
- FILE* fp = fopen("/proc/meminfo", "re");
- if (fp == NULL) {
- return -1;
- }
-
- int result = -1;
- char buf[256];
- while (fgets(buf, sizeof(buf), fp) != NULL) {
- long total;
- if (sscanf(buf, pattern, &total) == 1) {
- result = (int) (total / (PAGE_SIZE/1024));
- break;
- }
- }
- fclose(fp);
- return result;
-}
-
-static int __sysconf_phys_pages() {
- return __get_meminfo("MemTotal: %ld kB");
-}
-
-static int __sysconf_avphys_pages() {
- return __get_meminfo("MemFree: %ld kB");
-}
-
static int __sysconf_monotonic_clock() {
timespec t;
int rc = clock_getres(CLOCK_MONOTONIC, &t);
//case _SC_THREAD_SAFE_FUNCTIONS: return _POSIX_THREAD_SAFE_FUNCTIONS
case _SC_MONOTONIC_CLOCK: return __sysconf_monotonic_clock();
- case _SC_NPROCESSORS_CONF: return __sysconf_nprocessors_conf();
- case _SC_NPROCESSORS_ONLN: return __sysconf_nprocessors_onln();
- case _SC_PHYS_PAGES: return __sysconf_phys_pages();
- case _SC_AVPHYS_PAGES: return __sysconf_avphys_pages();
+ case _SC_NPROCESSORS_CONF: return get_nprocs_conf();
+ case _SC_NPROCESSORS_ONLN: return get_nprocs();
+ case _SC_PHYS_PAGES: return get_phys_pages();
+ case _SC_AVPHYS_PAGES: return get_avphys_pages();
default:
// Posix says EINVAL is the only error that shall be returned,
diff --git a/libc/bionic/sysinfo.cpp b/libc/bionic/sysinfo.cpp
--- /dev/null
+++ b/libc/bionic/sysinfo.cpp
@@ -0,0 +1,111 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/sysinfo.h>
+
+#include <dirent.h>
+#include <limits.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "private/ScopedReaddir.h"
+
+static bool __matches_cpuN(const char* s) {
+ // The %c trick is to ensure that we have the anchored match "^cpu[0-9]+$".
+ unsigned cpu;
+ char dummy;
+ return (sscanf(s, "cpu%u%c", &cpu, &dummy) == 1);
+}
+
+int get_nprocs_conf() {
+ // On x86 kernels you can use /proc/cpuinfo for this, but on ARM kernels offline CPUs disappear
+ // from there. This method works on both.
+ ScopedReaddir reader("/sys/devices/system/cpu");
+ if (reader.IsBad()) {
+ return 1;
+ }
+
+ int result = 0;
+ dirent* entry;
+ while ((entry = reader.ReadEntry()) != NULL) {
+ if (entry->d_type == DT_DIR && __matches_cpuN(entry->d_name)) {
+ ++result;
+ }
+ }
+ return result;
+}
+
+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;
+ }
+ if (__matches_cpuN(buf)) {
+ ++result;
+ }
+ }
+ fclose(fp);
+ return result;
+}
+
+static int __get_meminfo(const char* pattern) {
+ FILE* fp = fopen("/proc/meminfo", "re");
+ if (fp == NULL) {
+ return -1;
+ }
+
+ int result = -1;
+ char buf[256];
+ while (fgets(buf, sizeof(buf), fp) != NULL) {
+ long total;
+ if (sscanf(buf, pattern, &total) == 1) {
+ result = (int) (total / (PAGE_SIZE/1024));
+ break;
+ }
+ }
+ fclose(fp);
+ return result;
+}
+
+long get_phys_pages() {
+ return __get_meminfo("MemTotal: %ld kB");
+}
+
+long get_avphys_pages() {
+ return __get_meminfo("MemFree: %ld kB");
+}
index c7e46e6bfb03a139d741e73d4dc0f5a75d2cd3ef..b66bc8ef533a2fef3f1f39489f41732fcf9e6011 100644 (file)
__BEGIN_DECLS
-extern int sysinfo (struct sysinfo *info);
+int sysinfo(struct sysinfo* info);
+
+int get_nprocs_conf(void);
+
+int get_nprocs(void);
+
+long get_phys_pages(void);
+
+long get_avphys_pages(void);
__END_DECLS
diff --git a/tests/Android.mk b/tests/Android.mk
index 14a5e8331e846742f6167e6ad529267b186e1a8c..967ddbda556fe9cf4826f63a8a98eb843a7641c1 100644 (file)
--- a/tests/Android.mk
+++ b/tests/Android.mk
sys_stat_test.cpp \
sys_statvfs_test.cpp \
sys_syscall_test.cpp \
+ sys_sysinfo_test.cpp \
sys_time_test.cpp \
sys_types_test.cpp \
sys_vfs_test.cpp \
diff --git a/tests/sys_sysinfo_test.cpp b/tests/sys_sysinfo_test.cpp
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <sys/sysinfo.h>
+
+TEST(sys_sysinfo, smoke) {
+ int nprocessor = get_nprocs();
+ ASSERT_GT(nprocessor, 0);
+
+ int nprocessor_conf = get_nprocs_conf();
+ ASSERT_GE(nprocessor_conf, nprocessor);
+
+ long avail_phys_pages = get_avphys_pages();
+ ASSERT_GT(avail_phys_pages, 0);
+
+ long phys_pages = get_phys_pages();
+ ASSERT_GE(phys_pages, avail_phys_pages);
+}