File tree Expand file tree Collapse file tree 4 files changed +104
-0
lines changed Expand file tree Collapse file tree 4 files changed +104
-0
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ readonly BOOL_MOD=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
99
1010. ${BOOL_MOD} /bash.sh
1111. ${BOOL_MOD} /core.sh
12+ . ${BOOL_MOD} /os.sh
1213
1314
1415# ----------
@@ -255,3 +256,25 @@ function is_bash5() {
255256
256257 [ $( bash_version_major $ctx ) = " 5" ]
257258}
259+
260+ function is_linux() {
261+ # Return true/0 if linux.
262+ local ctx; is_ctx " ${1} " && ctx=" ${1} " && shift
263+ [ $# -ne 0 ] && { ctx_wn $ctx ; return $EC ; }
264+ shift 0 || { ctx_wn $ctx ; return $EC ; }
265+
266+ local name
267+ name=$( os_name $ctx )
268+ [ " ${name} " = " ${OS_LINUX} " ]
269+ }
270+
271+ function is_mac() {
272+ # Return true/0 if mac.
273+ local ctx; is_ctx " ${1} " && ctx=" ${1} " && shift
274+ [ $# -ne 0 ] && { ctx_wn $ctx ; return $EC ; }
275+ shift 0 || { ctx_wn $ctx ; return $EC ; }
276+
277+ local name
278+ name=$( os_name $ctx )
279+ [ " ${name} " = " ${OS_MAC} " ]
280+ }
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ readonly LANG_PACKAGE=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
1010. ${LANG_PACKAGE} /core.sh
1111. ${LANG_PACKAGE} /unsafe.sh
1212. ${LANG_PACKAGE} /os.sh
13+ . ${LANG_PACKAGE} /runtime.sh
1314. ${LANG_PACKAGE} /sys.sh
1415. ${LANG_PACKAGE} /make.sh
1516. ${LANG_PACKAGE} /log.sh
Original file line number Diff line number Diff line change 1+ #! /bin/bash
2+ #
3+ # https://github.com/EngineeringSoftware/gobash/blob/main/LICENSE
4+ #
5+ # Runtime functions.
6+
7+ if [ -n " ${LANG_RUNTIME_MOD:- } " ]; then return 0; fi
8+ readonly LANG_RUNTIME_MOD=$( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd )
9+
10+ . ${LANG_RUNTIME_MOD} /core.sh
11+ . ${LANG_RUNTIME_MOD} /bool.sh
12+
13+
14+ # ----------
15+ # Functions.
16+
17+ function runtime_num_cpu() {
18+ # Return the number of logical CPUs.
19+ local ctx; is_ctx " ${1} " && ctx=" ${1} " && shift
20+ [ $# -ne 0 ] && { ctx_wn $ctx ; return $EC ; }
21+ shift 0 || { ctx_wn $ctx ; return $EC ; }
22+
23+ if is_mac; then
24+ sysctl -n hw.logicalcpu_max
25+ else
26+ [ ! -f " /proc/cpuinfo" ] && \
27+ { ctx_w $ctx " no cpuinfo" ; return $EC ; }
28+ cat /proc/cpuinfo | grep ' processor' | wc -l
29+ fi
30+ }
31+
32+ function runtime_num_physical_cpu() {
33+ # Return the number of physical CPUs.
34+ local ctx; is_ctx " ${1} " && ctx=" ${1} " && shift
35+ [ $# -ne 0 ] && { ctx_wn $ctx ; return $EC ; }
36+ shift 0 || { ctx_wn $ctx ; return $EC ; }
37+
38+ if is_mac; then
39+ sysctl -n hw.physicalcpu_max
40+ else
41+ [ ! -f " /proc/cpuinfo" ] && \
42+ { ctx_w $ctx " no cpuinfo" ; return $EC ; }
43+ cat /proc/cpuinfo | grep ' core id' | sort -u | wc -l
44+ fi
45+ }
Original file line number Diff line number Diff line change 1+ #! /bin/bash
2+ #
3+ # https://github.com/EngineeringSoftware/gobash/blob/main/LICENSE
4+ #
5+ # Unit tests for the runtime module.
6+
7+ if [ -n " ${LANG_RUNTIME_TEST_MOD:- } " ]; then return 0; fi
8+ readonly LANG_RUNTIME_TEST_MOD=$( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd )
9+
10+ . ${LANG_RUNTIME_TEST_MOD} /assert.sh
11+ . ${LANG_RUNTIME_TEST_MOD} /os.sh
12+
13+
14+ # ----------
15+ # Functions.
16+
17+ function test_runtime_num_cpu() {
18+ local n
19+ n=$( runtime_num_cpu) || assert_fail
20+
21+ ! is_int " ${n} " && assert_fail
22+ [ " ${n} " -lt 1 ] && assert_fail
23+ return 0
24+ }
25+ readonly -f test_runtime_num_cpu
26+
27+ function test_runtime_num_physical_cpu() {
28+ local n
29+ n=$( runtime_num_physical_cpu) || assert_fail
30+
31+ ! is_int " ${n} " && assert_fail
32+ [ " ${n} " -lt 1 ] && assert_fail
33+ return 0
34+ }
35+ readonly -f test_runtime_num_physical_cpu
You can’t perform that action at this time.
0 commit comments