From f3cf11dcb526843fbc9fdc8f51e6a3021b981cb3 Mon Sep 17 00:00:00 2001 From: chenrun1 Date: Wed, 3 Jul 2024 10:14:14 +0800 Subject: [PATCH] libc/msic:Implement get_nprocs API Summary: 1.Remove the macro definition of getnproc in sysinfo 2.New get_nprocs_conf and get_nprocs interfaces, return CONFIG_SMP_NCPUS, return 1 when not defined Signed-off-by: chenrun1 --- include/sys/sysinfo.h | 10 +--- libs/libc/misc/CMakeLists.txt | 3 +- libs/libc/misc/Make.defs | 2 +- libs/libc/misc/lib_getnprocs.c | 86 ++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 10 deletions(-) create mode 100644 libs/libc/misc/lib_getnprocs.c diff --git a/include/sys/sysinfo.h b/include/sys/sysinfo.h index fab861ad39..1b918c2a07 100644 --- a/include/sys/sysinfo.h +++ b/include/sys/sysinfo.h @@ -33,14 +33,6 @@ #define SI_LOAD_SHIFT 16 -#ifdef CONFIG_SMP_NCPUS -#define get_nprocs() CONFIG_SMP_NCPUS -#define get_nprocs_conf() CONFIG_SMP_NCPUS -#else -#define get_nprocs() 1 -#define get_nprocs_conf() 1 -#endif - /**************************************************************************** * Type Definitions ****************************************************************************/ @@ -76,6 +68,8 @@ extern "C" #endif int sysinfo(FAR struct sysinfo *info); +int get_nprocs_conf(void); +int get_nprocs(void); #undef EXTERN #if defined(__cplusplus) diff --git a/libs/libc/misc/CMakeLists.txt b/libs/libc/misc/CMakeLists.txt index 9a2c55bd9d..d1fa2211c0 100644 --- a/libs/libc/misc/CMakeLists.txt +++ b/libs/libc/misc/CMakeLists.txt @@ -42,7 +42,8 @@ list( lib_openat.c lib_mkdirat.c lib_utimensat.c - lib_memoryregion.c) + lib_memoryregion.c + lib_getnprocs.c) # Support for platforms that do not have long long types diff --git a/libs/libc/misc/Make.defs b/libs/libc/misc/Make.defs index 77d7838f6a..b515bf65ec 100644 --- a/libs/libc/misc/Make.defs +++ b/libs/libc/misc/Make.defs @@ -25,7 +25,7 @@ CSRCS += lib_xorshift128.c lib_tea_encrypt.c lib_tea_decrypt.c CSRCS += lib_cxx_initialize.c lib_impure.c lib_memfd.c lib_mutex.c CSRCS += lib_fchmodat.c lib_fstatat.c lib_getfullpath.c lib_openat.c CSRCS += lib_mkdirat.c lib_utimensat.c lib_mallopt.c lib_memoryregion.c -CSRCS += lib_idr.c +CSRCS += lib_idr.c lib_getnprocs.c # Support for platforms that do not have long long types diff --git a/libs/libc/misc/lib_getnprocs.c b/libs/libc/misc/lib_getnprocs.c new file mode 100644 index 0000000000..06d68a94d9 --- /dev/null +++ b/libs/libc/misc/lib_getnprocs.c @@ -0,0 +1,86 @@ +/**************************************************************************** + * libs/libc/misc/lib_getnprocs.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: get_nprocs_conf + * + * Description: + * Retrieve the number of configured processors in the system. + * The get_nprocs_conf() function returns the number of processors (CPUs) + * configured in the system, regardless of whether they are currently + * enabled or available. This function is useful for determining the + * processor configuration in multiprocessor systems. + * + * Input Parameters: + * None + * + * Returned Value: + * On success, the number of configured processors is returned. + * If the system does not define a processor configuration, it returns 1. + * + ****************************************************************************/ + +int get_nprocs_conf(void) +{ +#ifdef CONFIG_SMP_NCPUS + return CONFIG_SMP_NCPUS; +#else + return 1; +#endif +} + +/**************************************************************************** + * Name: get_nprocs + * + * Description: + * Retrieve the number of online processors in the system. + * The get_nprocs() function returns the number of processors (CPUs) that + * are currently online and available for use in the system. This function + * can be useful for determining the number of processors that can be used + * by applications or the operating system for parallel processing. + * + * Input Parameters: + * None + * + * Returned Value: + * On success, the number of online processors is returned. + * If the system does not define a processor configuration, it returns 1. + * + ****************************************************************************/ + +int get_nprocs(void) +{ +#ifdef CONFIG_SMP_NCPUS + return CONFIG_SMP_NCPUS; +#else + return 1; +#endif +}