-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
runtime: Add functions for getting number of cpus (#5)
- Loading branch information
Showing
4 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/bin/bash | ||
# | ||
# https://github.com/EngineeringSoftware/gobash/blob/main/LICENSE | ||
# | ||
# Runtime functions. | ||
|
||
if [ -n "${LANG_RUNTIME_MOD:-}" ]; then return 0; fi | ||
readonly LANG_RUNTIME_MOD=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) | ||
|
||
. ${LANG_RUNTIME_MOD}/core.sh | ||
. ${LANG_RUNTIME_MOD}/bool.sh | ||
|
||
|
||
# ---------- | ||
# Functions. | ||
|
||
function runtime_num_cpu() { | ||
# Return the number of logical CPUs. | ||
local ctx; is_ctx "${1}" && ctx="${1}" && shift | ||
[ $# -ne 0 ] && { ctx_wn $ctx; return $EC; } | ||
shift 0 || { ctx_wn $ctx; return $EC; } | ||
|
||
if is_mac; then | ||
sysctl -n hw.logicalcpu_max | ||
else | ||
[ ! -f "/proc/cpuinfo" ] && \ | ||
{ ctx_w $ctx "no cpuinfo"; return $EC; } | ||
cat /proc/cpuinfo | grep 'processor' | wc -l | ||
fi | ||
} | ||
|
||
function runtime_num_physical_cpu() { | ||
# Return the number of physical CPUs. | ||
local ctx; is_ctx "${1}" && ctx="${1}" && shift | ||
[ $# -ne 0 ] && { ctx_wn $ctx; return $EC; } | ||
shift 0 || { ctx_wn $ctx; return $EC; } | ||
|
||
if is_mac; then | ||
sysctl -n hw.physicalcpu_max | ||
else | ||
[ ! -f "/proc/cpuinfo" ] && \ | ||
{ ctx_w $ctx "no cpuinfo"; return $EC; } | ||
cat /proc/cpuinfo | grep 'core id' | sort -u | wc -l | ||
fi | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/bin/bash | ||
# | ||
# https://github.com/EngineeringSoftware/gobash/blob/main/LICENSE | ||
# | ||
# Unit tests for the runtime module. | ||
|
||
if [ -n "${LANG_RUNTIME_TEST_MOD:-}" ]; then return 0; fi | ||
readonly LANG_RUNTIME_TEST_MOD=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) | ||
|
||
. ${LANG_RUNTIME_TEST_MOD}/assert.sh | ||
. ${LANG_RUNTIME_TEST_MOD}/os.sh | ||
|
||
|
||
# ---------- | ||
# Functions. | ||
|
||
function test_runtime_num_cpu() { | ||
local n | ||
n=$(runtime_num_cpu) || assert_fail | ||
|
||
! is_int "${n}" && assert_fail | ||
[ "${n}" -lt 1 ] && assert_fail | ||
return 0 | ||
} | ||
readonly -f test_runtime_num_cpu | ||
|
||
function test_runtime_num_physical_cpu() { | ||
local n | ||
n=$(runtime_num_physical_cpu) || assert_fail | ||
|
||
! is_int "${n}" && assert_fail | ||
[ "${n}" -lt 1 ] && assert_fail | ||
return 0 | ||
} | ||
readonly -f test_runtime_num_physical_cpu |