From 6521405c07485eb77c625c01b7afc6b8dd6c2e61 Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 6 Sep 2017 15:01:17 -0400 Subject: [PATCH] misc: add function to get CPU affinity mask size Implement it on Linux, FreeBSD, and Windows for now, and return UV_ENOTSUP on other platforms. Inspired-by: Kiran Pamnany Fixes: https://github.com/libuv/libuv/issues/1389 PR-URL: https://github.com/libuv/libuv/pull/1527 Reviewed-By: Ben Noordhuis Reviewed-By: Santiago Gimeno --- docs/src/misc.rst | 7 +++++++ include/uv.h | 1 + src/unix/core.c | 12 +++++++++++- src/win/core.c | 4 ++++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/docs/src/misc.rst b/docs/src/misc.rst index 3587fdee..03878d60 100644 --- a/docs/src/misc.rst +++ b/docs/src/misc.rst @@ -246,6 +246,13 @@ API Frees the `cpu_infos` array previously allocated with :c:func:`uv_cpu_info`. +.. c:function:: int uv_cpumask_size(void) + + Returns the maximum size of the mask used for process/thread affinities, + or ``UV_ENOTSUP`` if affinities are not supported on the current platform. + + .. versionadded:: 2.0.0 + .. c:function:: int uv_interface_addresses(uv_interface_address_t** addresses, int* count) Gets address information about the network interfaces on the system. An diff --git a/include/uv.h b/include/uv.h index e1bc4d99..b7e4798c 100644 --- a/include/uv.h +++ b/include/uv.h @@ -1142,6 +1142,7 @@ UV_EXTERN uv_pid_t uv_os_getppid(void); UV_EXTERN int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count); UV_EXTERN void uv_free_cpu_info(uv_cpu_info_t* cpu_infos, int count); +UV_EXTERN int uv_cpumask_size(void); UV_EXTERN int uv_interface_addresses(uv_interface_address_t** addresses, int* count); diff --git a/src/unix/core.c b/src/unix/core.c index 2680bd84..03be41c0 100644 --- a/src/unix/core.c +++ b/src/unix/core.c @@ -40,6 +40,7 @@ #include /* writev */ #include /* getrusage */ #include +#include #ifdef __sun # include /* MAXHOSTNAMELEN on Solaris */ @@ -63,6 +64,8 @@ # include # include # include +# include +# include # define UV__O_CLOEXEC O_CLOEXEC # if defined(__FreeBSD__) # define uv__accept4 accept4 @@ -1336,7 +1339,6 @@ int uv_os_gethostname(char* buffer, size_t* size) { return 0; } - uv_pid_t uv_os_getpid(void) { return getpid(); } @@ -1345,3 +1347,11 @@ uv_pid_t uv_os_getpid(void) { uv_pid_t uv_os_getppid(void) { return getppid(); } + +int uv_cpumask_size(void) { +#if defined(__linux__) || defined(__FreeBSD__) + return CPU_SETSIZE; +#else + return UV_ENOTSUP; +#endif +} diff --git a/src/win/core.c b/src/win/core.c index fbaa8b0e..89a4944a 100644 --- a/src/win/core.c +++ b/src/win/core.c @@ -427,3 +427,7 @@ int uv__socket_sockopt(uv_handle_t* handle, int optname, int* value) { return 0; } + +int uv_cpumask_size(void) { + return (int)(sizeof(DWORD_PTR) * 8); +}