linux: fix uv_cpu_info() arm cpu model detection (#4633)

Libuv looks for "Processor" in /proc/cpuinfo but it's been reported
that on at least some Raspberry Pi models, it's called "model name".
Look for both.

Fixes: https://github.com/nodejs/node/issues/56105
This commit is contained in:
Ben Noordhuis 2024-12-03 00:31:06 +01:00 committed by GitHub
parent 14644080c8
commit c431bc39c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1713,16 +1713,22 @@ int uv_uptime(double* uptime) {
int uv_cpu_info(uv_cpu_info_t** ci, int* count) { int uv_cpu_info(uv_cpu_info_t** ci, int* count) {
#if defined(__PPC__) #if defined(__PPC__)
static const char model_marker[] = "cpu\t\t: "; static const char model_marker[] = "cpu\t\t: ";
static const char model_marker2[] = "";
#elif defined(__arm__) #elif defined(__arm__)
static const char model_marker[] = "Processor\t: "; static const char model_marker[] = "model name\t: ";
static const char model_marker2[] = "Processor\t: ";
#elif defined(__aarch64__) #elif defined(__aarch64__)
static const char model_marker[] = "CPU part\t: "; static const char model_marker[] = "CPU part\t: ";
static const char model_marker2[] = "";
#elif defined(__mips__) #elif defined(__mips__)
static const char model_marker[] = "cpu model\t\t: "; static const char model_marker[] = "cpu model\t\t: ";
static const char model_marker2[] = "";
#elif defined(__loongarch__) #elif defined(__loongarch__)
static const char model_marker[] = "cpu family\t\t: "; static const char model_marker[] = "cpu family\t\t: ";
static const char model_marker2[] = "";
#else #else
static const char model_marker[] = "model name\t: "; static const char model_marker[] = "model name\t: ";
static const char model_marker2[] = "";
#endif #endif
static const char parts[] = static const char parts[] =
#ifdef __aarch64__ #ifdef __aarch64__
@ -1821,14 +1827,22 @@ int uv_cpu_info(uv_cpu_info_t** ci, int* count) {
if (1 != fscanf(fp, "processor\t: %u\n", &cpu)) if (1 != fscanf(fp, "processor\t: %u\n", &cpu))
break; /* Parse error. */ break; /* Parse error. */
found = 0; while (fgets(buf, sizeof(buf), fp)) {
while (!found && fgets(buf, sizeof(buf), fp)) if (!strncmp(buf, model_marker, sizeof(model_marker) - 1)) {
found = !strncmp(buf, model_marker, sizeof(model_marker) - 1); p = buf + sizeof(model_marker) - 1;
goto parts;
}
if (!*model_marker2)
continue;
if (!strncmp(buf, model_marker2, sizeof(model_marker2) - 1)) {
p = buf + sizeof(model_marker2) - 1;
goto parts;
}
}
if (!found) goto next; /* Not found. */
goto next;
p = buf + sizeof(model_marker) - 1; parts:
n = (int) strcspn(p, "\n"); n = (int) strcspn(p, "\n");
/* arm64: translate CPU part code to model name. */ /* arm64: translate CPU part code to model name. */