Skip to content

Commit

Permalink
fix: optimize the algorithm for cpu count calculation in the linux pl…
Browse files Browse the repository at this point in the history
…aform (#34)
  • Loading branch information
beita1 authored Jul 5, 2022
1 parent 20ffbc1 commit 1ed09c5
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/physicalCpuCount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1ed09c5

Please sign in to comment.