Skip to content

Commit

Permalink
runtime: Add functions for getting number of cpus (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
gliga authored Jan 10, 2024
1 parent 3d4e526 commit fc800a0
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/lang/bool.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ readonly BOOL_MOD=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )

. ${BOOL_MOD}/bash.sh
. ${BOOL_MOD}/core.sh
. ${BOOL_MOD}/os.sh


# ----------
Expand Down Expand Up @@ -255,3 +256,25 @@ function is_bash5() {

[ $(bash_version_major $ctx) = "5" ]
}

function is_linux() {
# Return true/0 if linux.
local ctx; is_ctx "${1}" && ctx="${1}" && shift
[ $# -ne 0 ] && { ctx_wn $ctx; return $EC; }
shift 0 || { ctx_wn $ctx; return $EC; }

local name
name=$(os_name $ctx)
[ "${name}" = "${OS_LINUX}" ]
}

function is_mac() {
# Return true/0 if mac.
local ctx; is_ctx "${1}" && ctx="${1}" && shift
[ $# -ne 0 ] && { ctx_wn $ctx; return $EC; }
shift 0 || { ctx_wn $ctx; return $EC; }

local name
name=$(os_name $ctx)
[ "${name}" = "${OS_MAC}" ]
}
1 change: 1 addition & 0 deletions src/lang/p.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ readonly LANG_PACKAGE=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
. ${LANG_PACKAGE}/core.sh
. ${LANG_PACKAGE}/unsafe.sh
. ${LANG_PACKAGE}/os.sh
. ${LANG_PACKAGE}/runtime.sh
. ${LANG_PACKAGE}/sys.sh
. ${LANG_PACKAGE}/make.sh
. ${LANG_PACKAGE}/log.sh
Expand Down
45 changes: 45 additions & 0 deletions src/lang/runtime.sh
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
}
35 changes: 35 additions & 0 deletions src/lang/runtime_test.sh
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

0 comments on commit fc800a0

Please sign in to comment.