From cc6ac5ff78ce808b51682d2ebd76c98ab2235f74 Mon Sep 17 00:00:00 2001 From: beita1 <583159685@qq.com> Date: Tue, 5 Jul 2022 20:51:50 +0800 Subject: [PATCH 1/3] fix: optimize the algorithm for cpu count calculation in the linux plaform --- src/physicalCpuCount.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/physicalCpuCount.ts b/src/physicalCpuCount.ts index 683fc5c..e073922 100644 --- a/src/physicalCpuCount.ts +++ b/src/physicalCpuCount.ts @@ -16,8 +16,17 @@ try { const platform = os.platform() if (platform === 'linux') { - const output = exec('cat /proc/cpuinfo | grep "physical id" | sort | wc -l') - amount = parseInt(output.trim(), 10) + const output1 = exec( + 'cat /proc/cpuinfo | grep "physical id" | sort |uniq | wc -l' + ) // physical cpu number + const output2 = exec( + 'cat /proc/cpuinfo | grep "core id" | sort | uniq | wc -l' + ) // physical cpu core number in each cpu + + const physicalCpuAmount = parseInt(output1.trim(), 10) + const physicalCoreAmount = parseInt(output2.trim(), 10) + + amount = physicalCpuAmount * physicalCoreAmount } else if (platform === 'darwin') { const output = exec('sysctl -n hw.physicalcpu_max') amount = parseInt(output.trim(), 10) From b310cc904eeb0c2fa990278636503a7a9ad6f2c9 Mon Sep 17 00:00:00 2001 From: beita1 <583159685@qq.com> Date: Wed, 6 Jul 2022 11:43:51 +0800 Subject: [PATCH 2/3] fix: delete useless plafrom condition --- src/physicalCpuCount.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/physicalCpuCount.ts b/src/physicalCpuCount.ts index e073922..dee4f8c 100644 --- a/src/physicalCpuCount.ts +++ b/src/physicalCpuCount.ts @@ -31,7 +31,7 @@ try { const output = exec('sysctl -n hw.physicalcpu_max') amount = parseInt(output.trim(), 10) // @ts-ignore - } else if (platform === 'windows' || platform === 'win32') { + } else if (platform === 'win32') { // windows takes too long, so let's drop the support throw new Error() } else { From 061e07e062570fd8618c402f6333176afb518184 Mon Sep 17 00:00:00 2001 From: beita1 <583159685@qq.com> Date: Wed, 6 Jul 2022 12:00:28 +0800 Subject: [PATCH 3/3] style: delete useless comments --- src/physicalCpuCount.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/physicalCpuCount.ts b/src/physicalCpuCount.ts index dee4f8c..9a46944 100644 --- a/src/physicalCpuCount.ts +++ b/src/physicalCpuCount.ts @@ -30,7 +30,6 @@ try { } else if (platform === 'darwin') { const output = exec('sysctl -n hw.physicalcpu_max') amount = parseInt(output.trim(), 10) - // @ts-ignore } else if (platform === 'win32') { // windows takes too long, so let's drop the support throw new Error()