From 9241cc297767eb36820133532eb4d9d8fa7bcda2 Mon Sep 17 00:00:00 2001 From: elephantp Date: Sat, 13 Jan 2018 05:41:57 +0800 Subject: [PATCH] unix: fix uv_cpu_info() error on FreeBSD This commit updates the key used in uv_cpu_info() for ARM FreeBSD. Fixes: https://github.com/nodejs/node/issues/17995 Fixes: https://github.com/libuv/libuv/issues/1694 PR-URL: https://github.com/libuv/libuv/pull/1700 Reviewed-By: Colin Ihrig Reviewed-By: Ben Noordhuis Reviewed-By: Santiago Gimeno --- src/unix/freebsd.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/unix/freebsd.c b/src/unix/freebsd.c index 39db148a..f2b3f247 100644 --- a/src/unix/freebsd.c +++ b/src/unix/freebsd.c @@ -276,6 +276,7 @@ int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) { uv_cpu_info_t* cpu_info; const char* maxcpus_key; const char* cptimes_key; + const char* model_key; char model[512]; long* cp_times; int numcpus; @@ -294,8 +295,20 @@ int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) { cptimes_key = "kern.cp_times"; #endif +#if defined(__arm__) || defined(__aarch64__) + /* The key hw.model and hw.clockrate are not available on FreeBSD ARM. */ + model_key = "hw.machine"; + cpuspeed = 0; +#else + model_key = "hw.model"; + + size = sizeof(cpuspeed); + if (sysctlbyname("hw.clockrate", &cpuspeed, &size, NULL, 0)) + return -errno; +#endif + size = sizeof(model); - if (sysctlbyname("hw.model", &model, &size, NULL, 0)) + if (sysctlbyname(model_key, &model, &size, NULL, 0)) return -errno; size = sizeof(numcpus); @@ -308,12 +321,6 @@ int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) { *count = numcpus; - size = sizeof(cpuspeed); - if (sysctlbyname("hw.clockrate", &cpuspeed, &size, NULL, 0)) { - uv__free(*cpu_infos); - return -errno; - } - /* kern.cp_times on FreeBSD i386 gives an array up to maxcpus instead of * ncpu. */