From: Wanlong G. <gao...@cn...> - 2012-10-18 02:44:06
|
Add tst_ncpus and tst_ncpus_max api to get the number of CPUS online and configured. Signed-off-by: Wanlong Gao <gao...@cn...> --- include/safe_macros.h | 5 +++++ include/test.h | 4 ++++ lib/safe_macros.c | 21 +++++++++++++++++++++ lib/tst_cpu.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ tools/apicmds/Makefile | 3 ++- tools/apicmds/ltpapicmd.c | 4 ++++ 6 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 lib/tst_cpu.c diff --git a/include/safe_macros.h b/include/safe_macros.h index 3891e19..0e87a00 100644 --- a/include/safe_macros.h +++ b/include/safe_macros.h @@ -156,5 +156,10 @@ unsigned long safe_strtoul(const char *file, const int lineno, void (cleanup_fn) #define SAFE_STRTOUL(cleanup_fn, str, min, max) \ safe_strtoul(__FILE__, __LINE__, cleanup_fn, (str), (min), (max)) +long safe_sysconf(const char *file, const int lineno, + void (cleanup_fn)(void), int name); +#define SAFE_SYSCONF(cleanup_fn, name) + safe_sysconf(__FILE__, __LINE__, cleanup_fn, name); + #endif #endif diff --git a/include/test.h b/include/test.h index 934585e..a32a8cc 100644 --- a/include/test.h +++ b/include/test.h @@ -267,6 +267,10 @@ char *get_mountpoint(const char *path); /* Function from lib/get_path.c */ int tst_get_path(const char *prog_name, char *buf, size_t buf_len); +/* lib/tst_cpu.c */ +long tst_ncpus(void); +long tst_ncpus_max(void); + #ifdef TST_USE_COMPAT16_SYSCALL #define TCID_BIT_SUFFIX "_16" #elif TST_USE_NEWER64_SYSCALL diff --git a/lib/safe_macros.c b/lib/safe_macros.c index b9802fc..9c50f89 100644 --- a/lib/safe_macros.c +++ b/lib/safe_macros.c @@ -386,3 +386,24 @@ unsigned long safe_strtoul(const char *file, const int lineno, void (cleanup_fn) return rval; } + +long safe_sysconf(const char *file, const int lineno, + void (cleanup_fn)(void), int name) +{ + long rval; + errno = 0; + + rval = sysconf(name); + + if (rval == -1) { + if (errno == EINVAL) + tst_brkm(TBROK|TERRNO, cleanup_fn, + "sysconf failed at %s:%d", file, lineno); + else + tst_resm(TINFO, "queried option is not available" + " or thers is no definite limit at %s:%d", + file, lineno); + } + + return rval; +} diff --git a/lib/tst_cpu.c b/lib/tst_cpu.c new file mode 100644 index 0000000..fcbee32 --- /dev/null +++ b/lib/tst_cpu.c @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2012 Fujitsu Ltd. + * Author: Wanlong Gao <gao...@cn...> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See + * the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <stdlib.h> +#include <unistd.h> +#include "test.h" + +long tst_ncpus(void) +{ + long ncpus = -1; +#ifdef _SC_NPROCESSORS_ONLN + ncpus = SAFE_SYSCONF(_SC_NPROCESSORS_ONLN); +#else + tst_brkm(TBROK, NULL, "could not determine number of CPUs online"); +#endif + return ncpus; +} + +long tst_ncpus_max(void) +{ + long ncpus_max = -1; +#ifdef _SC_NPROCESSORS_CONF + ncpus_max = SAFE_SYSCONF(_SC_NPROCESSORS_CONF); +#else + tst_brkm(TBROK, NULL, "could not determine number of CPUs configured"); +#endif + return ncpus_max; +} diff --git a/tools/apicmds/Makefile b/tools/apicmds/Makefile index 59ce6be..faa49b5 100644 --- a/tools/apicmds/Makefile +++ b/tools/apicmds/Makefile @@ -26,7 +26,8 @@ include $(top_srcdir)/include/mk/testcases.mk CPPFLAGS += -D_GNU_SOURCE -MAKE_TARGETS := $(addprefix tst_,brk brkm exit flush kvercmp res resm) +MAKE_TARGETS := $(addprefix tst_,brk brkm exit flush kvercmp res resm \ + ncpus ncpus_max) include $(top_srcdir)/include/mk/generic_leaf_target.mk diff --git a/tools/apicmds/ltpapicmd.c b/tools/apicmds/ltpapicmd.c index fe7349b..32eb3f9 100644 --- a/tools/apicmds/ltpapicmd.c +++ b/tools/apicmds/ltpapicmd.c @@ -266,6 +266,10 @@ int main(int argc, char *argv[]) else if (exit_value > 0) exit_value = 2; exit(exit_value); + } else if (strcmp(cmd_name, "tst_ncpus") == 0) { + printf("%li\n", tst_ncpus()); + } else if (strcmp(cmd_name, "tst_ncpus_max") == 0) { + printf("%li\n", tst_ncpus_max()); } exit(0); -- 1.8.0.rc2.4.g42e55a5 |