diff --git a/src/lang/bool.sh b/src/lang/bool.sh index 6104cfc..ed02d08 100644 --- a/src/lang/bool.sh +++ b/src/lang/bool.sh @@ -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 # ---------- @@ -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}" ] +} diff --git a/src/lang/p.sh b/src/lang/p.sh index fed7c13..7c2b782 100644 --- a/src/lang/p.sh +++ b/src/lang/p.sh @@ -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 diff --git a/src/lang/runtime.sh b/src/lang/runtime.sh new file mode 100644 index 0000000..6775b5a --- /dev/null +++ b/src/lang/runtime.sh @@ -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 +} diff --git a/src/lang/runtime_test.sh b/src/lang/runtime_test.sh new file mode 100644 index 0000000..32e1cc6 --- /dev/null +++ b/src/lang/runtime_test.sh @@ -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