From 8f59b2ca6f20c18e1517364e2388943346f016ee Mon Sep 17 00:00:00 2001 From: Maksym Vlasov Date: Wed, 14 Feb 2024 16:06:11 +0200 Subject: [PATCH 01/26] Update _common.sh --- hooks/_common.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/hooks/_common.sh b/hooks/_common.sh index e41541aa5..f9eb250a7 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -223,6 +223,7 @@ function common::per_dir_hook { # `nproc` - linux, `sysctl -n hw.ncpu` - macOS, `echo 1` - fallback local CPU CPU=$(nproc 2> /dev/null || sysctl -n hw.ncpu 2> /dev/null || echo 1) +common::colorify "yellow" "CPU: $CPU" local parallelism_limit local parallelism_disabled=false From 5f84408a548fca6ad3b300a0c1c375b3ff9fbc83 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Wed, 14 Feb 2024 17:17:18 +0200 Subject: [PATCH 02/26] Set right CPU count for containers --- hooks/_common.sh | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/hooks/_common.sh b/hooks/_common.sh index f9eb250a7..a7929392d 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -222,7 +222,21 @@ function common::per_dir_hook { # Limit the number of parallel processes to the number of CPU cores -1 # `nproc` - linux, `sysctl -n hw.ncpu` - macOS, `echo 1` - fallback local CPU - CPU=$(nproc 2> /dev/null || sysctl -n hw.ncpu 2> /dev/null || echo 1) + + if [[ ! -f /sys/fs/cgroup/cpu.max ]]; then + # On host machine + CPU=$(nproc 2> /dev/null || sysctl -n hw.ncpu 2> /dev/null || echo 1) + else + # Inside Linux container + local millicpu + millicpu=$(cut -d' ' -f1 /sys/fs/cgroup/cpu.max) + if [[ "$millicpu" == "max" ]]; then + CPU=$(nproc 2> /dev/null || echo 1) + else + CPU=$((millicpu / 1000)) + fi + fi + common::colorify "yellow" "CPU: $CPU" local parallelism_limit local parallelism_disabled=false From 4fc221905a85ab0b2c155572bc2a7991d2d4f1a7 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Wed, 14 Feb 2024 17:21:26 +0200 Subject: [PATCH 03/26] More logging --- hooks/_common.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/hooks/_common.sh b/hooks/_common.sh index a7929392d..9e83d67b1 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -225,19 +225,25 @@ function common::per_dir_hook { if [[ ! -f /sys/fs/cgroup/cpu.max ]]; then # On host machine + echo "DBG: On host machine" CPU=$(nproc 2> /dev/null || sysctl -n hw.ncpu 2> /dev/null || echo 1) else # Inside Linux container + echo "DBG: Inside Linux container" local millicpu millicpu=$(cut -d' ' -f1 /sys/fs/cgroup/cpu.max) + echo "DBG:" + cut -/sys/fs/cgroup/cpu.max if [[ "$millicpu" == "max" ]]; then + echo "DBG: CPU: max" CPU=$(nproc 2> /dev/null || echo 1) else + echo "DBG: CPU: $((millicpu / 1000))" CPU=$((millicpu / 1000)) fi fi -common::colorify "yellow" "CPU: $CPU" + common::colorify "yellow" "CPU: $CPU" local parallelism_limit local parallelism_disabled=false From 3d31ac91890aeccf2d896d0c8a36cafa56c83756 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Wed, 14 Feb 2024 17:25:40 +0200 Subject: [PATCH 04/26] t --- hooks/_common.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/hooks/_common.sh b/hooks/_common.sh index 9e83d67b1..0b905bdde 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -223,6 +223,10 @@ function common::per_dir_hook { # `nproc` - linux, `sysctl -n hw.ncpu` - macOS, `echo 1` - fallback local CPU + tree /sys/fs/cgroup/ + + exit 1 + if [[ ! -f /sys/fs/cgroup/cpu.max ]]; then # On host machine echo "DBG: On host machine" From 7e14cbb85ad57026cf6ea3e78161c2021cf56f3c Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Wed, 14 Feb 2024 17:29:06 +0200 Subject: [PATCH 05/26] t --- hooks/_common.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/hooks/_common.sh b/hooks/_common.sh index 0b905bdde..8ac47d4fb 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -223,6 +223,8 @@ function common::per_dir_hook { # `nproc` - linux, `sysctl -n hw.ncpu` - macOS, `echo 1` - fallback local CPU + apt update + apt install -y tree tree /sys/fs/cgroup/ exit 1 From c411f63dbc2165acefa8736b605c39e2b1553128 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Wed, 14 Feb 2024 17:35:33 +0200 Subject: [PATCH 06/26] t --- hooks/_common.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hooks/_common.sh b/hooks/_common.sh index 8ac47d4fb..3e9d5884f 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -226,6 +226,7 @@ function common::per_dir_hook { apt update apt install -y tree tree /sys/fs/cgroup/ + more /sys/fs/cgroup/* | cat exit 1 @@ -239,7 +240,7 @@ function common::per_dir_hook { local millicpu millicpu=$(cut -d' ' -f1 /sys/fs/cgroup/cpu.max) echo "DBG:" - cut -/sys/fs/cgroup/cpu.max + cat /sys/fs/cgroup/cpu.max if [[ "$millicpu" == "max" ]]; then echo "DBG: CPU: max" CPU=$(nproc 2> /dev/null || echo 1) From 6ea0e0af75d3b5908c54b4cc1c3888c5ec092520 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Wed, 14 Feb 2024 18:29:51 +0200 Subject: [PATCH 07/26] t --- hooks/_common.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hooks/_common.sh b/hooks/_common.sh index 3e9d5884f..dc1c82372 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -226,7 +226,7 @@ function common::per_dir_hook { apt update apt install -y tree tree /sys/fs/cgroup/ - more /sys/fs/cgroup/* | cat + more /sys/fs/cgroup/cpu* | cat exit 1 From 1f9c39909593ee27c0c0387c4b905ee1e42ee624 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Wed, 14 Feb 2024 18:34:35 +0200 Subject: [PATCH 08/26] t --- hooks/_common.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hooks/_common.sh b/hooks/_common.sh index dc1c82372..94c84581e 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -226,7 +226,7 @@ function common::per_dir_hook { apt update apt install -y tree tree /sys/fs/cgroup/ - more /sys/fs/cgroup/cpu* | cat + more /sys/fs/cgroup/cpu/* | cat exit 1 From 4ab8e0e4702b14e32b2e66fb33706b9c6fbb2ae9 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Wed, 14 Feb 2024 18:51:17 +0200 Subject: [PATCH 09/26] t --- hooks/_common.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/hooks/_common.sh b/hooks/_common.sh index 94c84581e..631c6dd95 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -228,6 +228,9 @@ function common::per_dir_hook { tree /sys/fs/cgroup/ more /sys/fs/cgroup/cpu/* | cat + echo cpuset------------------------------------------- + more /sys/fs/cgroup/cpuset/* | cat + exit 1 if [[ ! -f /sys/fs/cgroup/cpu.max ]]; then From 4b6767e9f2b5c8db970d42e37974e7d5cc42eb7b Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Wed, 14 Feb 2024 19:10:29 +0200 Subject: [PATCH 10/26] t --- hooks/_common.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/hooks/_common.sh b/hooks/_common.sh index 631c6dd95..0ea67ead6 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -231,6 +231,7 @@ function common::per_dir_hook { echo cpuset------------------------------------------- more /sys/fs/cgroup/cpuset/* | cat + sleep 1000000000000000000000 exit 1 if [[ ! -f /sys/fs/cgroup/cpu.max ]]; then From cdad120971c0092445308fa643b7fa645c18643b Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Wed, 14 Feb 2024 20:19:49 +0200 Subject: [PATCH 11/26] t --- hooks/_common.sh | 103 +++++++++++++++++++++++++++++------------------ 1 file changed, 64 insertions(+), 39 deletions(-) diff --git a/hooks/_common.sh b/hooks/_common.sh index 0ea67ead6..92da343f9 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -219,45 +219,8 @@ function common::per_dir_hook { # Lookup hook-config for modifiers that impact common behavior local change_dir_in_unique_part=false - # Limit the number of parallel processes to the number of CPU cores -1 - # `nproc` - linux, `sysctl -n hw.ncpu` - macOS, `echo 1` - fallback - local CPU - - apt update - apt install -y tree - tree /sys/fs/cgroup/ - more /sys/fs/cgroup/cpu/* | cat - - echo cpuset------------------------------------------- - more /sys/fs/cgroup/cpuset/* | cat - - sleep 1000000000000000000000 - exit 1 - - if [[ ! -f /sys/fs/cgroup/cpu.max ]]; then - # On host machine - echo "DBG: On host machine" - CPU=$(nproc 2> /dev/null || sysctl -n hw.ncpu 2> /dev/null || echo 1) - else - # Inside Linux container - echo "DBG: Inside Linux container" - local millicpu - millicpu=$(cut -d' ' -f1 /sys/fs/cgroup/cpu.max) - echo "DBG:" - cat /sys/fs/cgroup/cpu.max - if [[ "$millicpu" == "max" ]]; then - echo "DBG: CPU: max" - CPU=$(nproc 2> /dev/null || echo 1) - else - echo "DBG: CPU: $((millicpu / 1000))" - CPU=$((millicpu / 1000)) - fi - fi - common::colorify "yellow" "CPU: $CPU" local parallelism_limit - local parallelism_disabled=false - IFS=";" read -r -a configs <<< "${HOOK_CONFIG[*]}" for c in "${configs[@]}"; do IFS="=" read -r -a config <<< "$c" @@ -278,11 +241,69 @@ function common::per_dir_hook { ;; --parallelism-limit) # this flag will limit the number of parallel processes - parallelism_limit=$((value)) + parallelism_limit="$value" ;; esac done + # Limit the number of parallel processes to the number of CPU cores -1 + # `nproc` - linux, `sysctl -n hw.ncpu` - macOS, `echo 1` - fallback + local CPU millicpu + + # apt update + # apt install -y tree + # tree /sys/fs/cgroup/ + # more /sys/fs/cgroup/cpu/* | cat + + # echo cpuset------------------------------------------- + # more /sys/fs/cgroup/cpuset/* | cat + + # sleep 1000000000000000000000 + # exit 1 + + if [[ -f /sys/fs/cgroup/cpu/cpu.cfs_quota_us ]]; then + # Inside K8s pod or DInD in K8s + millicpu=$(cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us) + if [[ $millicpu -eq -1 ]]; then + # K8s no limits or in DinD + CPU=1 + + if [[ ! $parallelism_limit ]]; then + common::colorify "yellow" "Unable to calculate available CPU cors.\n" \ + "You in K8s pod without limits or in DinD without limits propagation.\n" \ + "To avoid possible harm, parallelism disabled.\n" \ + "To reenable it, set limits or specify '--parallelism-limit' for hooks" + fi + else + CPU=$((millicpu / 1000)) + echo "DBG: Inside K8s, CPU: $CPU" + fi + elif [[ -f /sys/fs/cgroup/cpu.max ]]; then + # Inside Linux container + echo "DBG: Inside Linux container" + local millicpu + millicpu=$(cut -d' ' -f1 /sys/fs/cgroup/cpu.max) + echo "DBG:" + cat /sys/fs/cgroup/cpu.max + if [[ "$millicpu" == "max" ]]; then + echo "DBG: CPU: max" + CPU=$(nproc 2> /dev/null || echo 1) + else + echo "DBG: CPU: $((millicpu / 1000))" + CPU=$((millicpu / 1000)) + fi + else + # On host machine + echo "DBG: On host machine" + CPU=$(nproc 2> /dev/null || sysctl -n hw.ncpu 2> /dev/null || echo 1) + fi + + common::colorify "yellow" "CPU: $CPU" + echo "DBG: parallelism_limit before: $parallelism_limit" + + parallelism_limit=$((parallelism_limit)) + local parallelism_disabled=false + if [[ ! $parallelism_limit ]]; then parallelism_limit=$((CPU - 1)) elif [[ $parallelism_limit -le 1 ]]; then @@ -290,6 +311,9 @@ function common::per_dir_hook { parallelism_disabled=true fi + echo "DBG: parallelism_limit after: $parallelism_limit" + exit 1 + local final_exit_code=0 local pids=() @@ -353,7 +377,8 @@ function common::colorify { # Params start # local COLOR="${!1}" - local -r TEXT=$2 + shift + local -r TEXT="$*" # Params end # if [ "$PRE_COMMIT_COLOR" = "never" ]; then From 1aa3f505006e021797ec4eaba3918abf16cf9502 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Wed, 14 Feb 2024 20:48:27 +0200 Subject: [PATCH 12/26] Set right CPU count for containers --- hooks/_common.sh | 109 ++++++++++++++++++++++------------------------- 1 file changed, 50 insertions(+), 59 deletions(-) diff --git a/hooks/_common.sh b/hooks/_common.sh index 92da343f9..9e7983e63 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -170,6 +170,52 @@ function common::is_hook_run_on_whole_repo { fi } +####################################################################### +# Get the number of CPU logical cores available to pre-commit use +# Arguments: +# parallelism_limit (string) Used for checking if user redefined defaults +# Outputs: +# Return number CPU logical cores, rounded to below integer +####################################################################### +function common::get_cpu_num { + local -r parallelism_limit=$1 + + local millicpu + + if [[ -f /sys/fs/cgroup/cpu/cpu.cfs_quota_us ]]; then + # Inside K8s pod or DInD in K8s + millicpu=$(cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us) + + if [[ $millicpu -eq -1 ]]; then + # K8s no limits or in DinD + if [[ ! $parallelism_limit ]]; then + common::colorify "yellow" "Unable to calculate available CPU cors.\n" \ + "You in K8s pod without limits or in DinD without limits propagation.\n" \ + "To avoid possible harm, parallelism disabled.\n" \ + "To reenable it, set limits or specify '--parallelism-limit' for hooks" + fi + return 1 + fi + + return $((millicpu / 1000)) + fi + + if [[ -f /sys/fs/cgroup/cpu.max ]]; then + # Inside Linux (Docker?) container + millicpu=$(cut -d' ' -f1 /sys/fs/cgroup/cpu.max) + + if [[ "$millicpu" == "max" ]]; then + # No limits + return "$(nproc 2> /dev/null || echo 1)" + fi + return $((millicpu / 1000)) + fi + + # On host machine + # `nproc` - linux, `sysctl -n hw.ncpu` - macOS, `echo 1` - fallback + return "$(nproc 2> /dev/null || sysctl -n hw.ncpu 2> /dev/null || echo 1)" +} + ####################################################################### # Hook execution boilerplate logic which is common to hooks, that run # on per dir basis. @@ -246,74 +292,19 @@ function common::per_dir_hook { esac done - # Limit the number of parallel processes to the number of CPU cores -1 - # `nproc` - linux, `sysctl -n hw.ncpu` - macOS, `echo 1` - fallback - local CPU millicpu - - # apt update - # apt install -y tree - # tree /sys/fs/cgroup/ - # more /sys/fs/cgroup/cpu/* | cat - - # echo cpuset------------------------------------------- - # more /sys/fs/cgroup/cpuset/* | cat - - # sleep 1000000000000000000000 - # exit 1 - - if [[ -f /sys/fs/cgroup/cpu/cpu.cfs_quota_us ]]; then - # Inside K8s pod or DInD in K8s - millicpu=$(cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us) - if [[ $millicpu -eq -1 ]]; then - # K8s no limits or in DinD - CPU=1 - - if [[ ! $parallelism_limit ]]; then - common::colorify "yellow" "Unable to calculate available CPU cors.\n" \ - "You in K8s pod without limits or in DinD without limits propagation.\n" \ - "To avoid possible harm, parallelism disabled.\n" \ - "To reenable it, set limits or specify '--parallelism-limit' for hooks" - fi - else - CPU=$((millicpu / 1000)) - echo "DBG: Inside K8s, CPU: $CPU" - fi - elif [[ -f /sys/fs/cgroup/cpu.max ]]; then - # Inside Linux container - echo "DBG: Inside Linux container" - local millicpu - millicpu=$(cut -d' ' -f1 /sys/fs/cgroup/cpu.max) - echo "DBG:" - cat /sys/fs/cgroup/cpu.max - if [[ "$millicpu" == "max" ]]; then - echo "DBG: CPU: max" - CPU=$(nproc 2> /dev/null || echo 1) - else - echo "DBG: CPU: $((millicpu / 1000))" - CPU=$((millicpu / 1000)) - fi - else - # On host machine - echo "DBG: On host machine" - CPU=$(nproc 2> /dev/null || sysctl -n hw.ncpu 2> /dev/null || echo 1) - fi - - common::colorify "yellow" "CPU: $CPU" - echo "DBG: parallelism_limit before: $parallelism_limit" - + CPU=$(common::get_cpu_num "$parallelism_limit") + # parallelism_limit can include reference to 'CPU' variable parallelism_limit=$((parallelism_limit)) local parallelism_disabled=false if [[ ! $parallelism_limit ]]; then parallelism_limit=$((CPU - 1)) - elif [[ $parallelism_limit -le 1 ]]; then + fi + if [[ $parallelism_limit -le 1 ]]; then parallelism_limit=1 parallelism_disabled=true fi - echo "DBG: parallelism_limit after: $parallelism_limit" - exit 1 - local final_exit_code=0 local pids=() From 4f9693c8883dd27c579ce16f439f8b085ca31e0f Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Wed, 14 Feb 2024 21:00:08 +0200 Subject: [PATCH 13/26] f --- hooks/_common.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hooks/_common.sh b/hooks/_common.sh index 9e7983e63..7a80327b3 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -292,7 +292,8 @@ function common::per_dir_hook { esac done - CPU=$(common::get_cpu_num "$parallelism_limit") + common::get_cpu_num "$parallelism_limit" + CPU=$? # parallelism_limit can include reference to 'CPU' variable parallelism_limit=$((parallelism_limit)) local parallelism_disabled=false From d54181553b46e73837935c62bd42abf95979b61a Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Wed, 14 Feb 2024 21:00:55 +0200 Subject: [PATCH 14/26] f --- hooks/_common.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/hooks/_common.sh b/hooks/_common.sh index 7a80327b3..97c823d3e 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -292,8 +292,11 @@ function common::per_dir_hook { esac done + set +e common::get_cpu_num "$parallelism_limit" CPU=$? + set -e + # parallelism_limit can include reference to 'CPU' variable parallelism_limit=$((parallelism_limit)) local parallelism_disabled=false From 9df913bad26323514765b587fdd0fbf2639fb2db Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Wed, 14 Feb 2024 21:26:32 +0200 Subject: [PATCH 15/26] parallelism_bypass_safety_check --- hooks/_common.sh | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/hooks/_common.sh b/hooks/_common.sh index 97c823d3e..315a84ad5 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -174,6 +174,7 @@ function common::is_hook_run_on_whole_repo { # Get the number of CPU logical cores available to pre-commit use # Arguments: # parallelism_limit (string) Used for checking if user redefined defaults +# parallelism_bypass_safety_check (bool) If true - skip K8s safety check # Outputs: # Return number CPU logical cores, rounded to below integer ####################################################################### @@ -188,11 +189,15 @@ function common::get_cpu_num { if [[ $millicpu -eq -1 ]]; then # K8s no limits or in DinD + if [[ $parallelism_bypass_safety_check == true ]]; then + return "$(nproc 2> /dev/null || echo 1)" + fi + if [[ ! $parallelism_limit ]]; then common::colorify "yellow" "Unable to calculate available CPU cors.\n" \ "You in K8s pod without limits or in DinD without limits propagation.\n" \ "To avoid possible harm, parallelism disabled.\n" \ - "To reenable it, set limits or specify '--parallelism-limit' for hooks" + "To reenable it, set limits or specify '--parallelism-limit' for hooks\n" fi return 1 fi @@ -289,11 +294,15 @@ function common::per_dir_hook { # this flag will limit the number of parallel processes parallelism_limit="$value" ;; + --parallelism-bypass-safety-check) + # this flag will limit the number of parallel processes + parallelism_bypass_safety_check="$value" + ;; esac done set +e - common::get_cpu_num "$parallelism_limit" + common::get_cpu_num "$parallelism_limit" "$parallelism_bypass_safety_check" CPU=$? set -e From 1b19f28640944daa2d4d18ac9591b8218ed08d72 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Wed, 14 Feb 2024 21:29:26 +0200 Subject: [PATCH 16/26] f --- hooks/_common.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/hooks/_common.sh b/hooks/_common.sh index 315a84ad5..744e62f22 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -180,6 +180,7 @@ function common::is_hook_run_on_whole_repo { ####################################################################### function common::get_cpu_num { local -r parallelism_limit=$1 + local -r parallelism_bypass_safety_check=$2 local millicpu From b26e30b4c050294f7fee1b78769c4731405e4a92 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Wed, 14 Feb 2024 22:04:39 +0200 Subject: [PATCH 17/26] Simplify logic --- hooks/_common.sh | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/hooks/_common.sh b/hooks/_common.sh index 744e62f22..2db1a27cc 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -174,13 +174,11 @@ function common::is_hook_run_on_whole_repo { # Get the number of CPU logical cores available to pre-commit use # Arguments: # parallelism_limit (string) Used for checking if user redefined defaults -# parallelism_bypass_safety_check (bool) If true - skip K8s safety check # Outputs: # Return number CPU logical cores, rounded to below integer ####################################################################### function common::get_cpu_num { local -r parallelism_limit=$1 - local -r parallelism_bypass_safety_check=$2 local millicpu @@ -190,17 +188,13 @@ function common::get_cpu_num { if [[ $millicpu -eq -1 ]]; then # K8s no limits or in DinD - if [[ $parallelism_bypass_safety_check == true ]]; then - return "$(nproc 2> /dev/null || echo 1)" - fi - if [[ ! $parallelism_limit ]]; then common::colorify "yellow" "Unable to calculate available CPU cors.\n" \ "You in K8s pod without limits or in DinD without limits propagation.\n" \ - "To avoid possible harm, parallelism disabled.\n" \ - "To reenable it, set limits or specify '--parallelism-limit' for hooks\n" + "To avoid possible harm, set '--parallelism-limit' for hooks" fi - return 1 + + return "$(nproc 2> /dev/null || echo 1)" fi return $((millicpu / 1000)) @@ -295,15 +289,11 @@ function common::per_dir_hook { # this flag will limit the number of parallel processes parallelism_limit="$value" ;; - --parallelism-bypass-safety-check) - # this flag will limit the number of parallel processes - parallelism_bypass_safety_check="$value" - ;; esac done set +e - common::get_cpu_num "$parallelism_limit" "$parallelism_bypass_safety_check" + common::get_cpu_num "$parallelism_limit" CPU=$? set -e From 42959d612b84f3fed5490bde429900243f498be2 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Wed, 14 Feb 2024 22:45:19 +0200 Subject: [PATCH 18/26] Finally good colution --- hooks/_common.sh | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/hooks/_common.sh b/hooks/_common.sh index 2db1a27cc..26be059d4 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -173,12 +173,13 @@ function common::is_hook_run_on_whole_repo { ####################################################################### # Get the number of CPU logical cores available to pre-commit use # Arguments: -# parallelism_limit (string) Used for checking if user redefined defaults +# parallelism_ci_cpu_cores (string) Used in corner cases when CPU cores +# can't be calculated # Outputs: # Return number CPU logical cores, rounded to below integer ####################################################################### function common::get_cpu_num { - local -r parallelism_limit=$1 + local -r parallelism_ci_cpu_cores=$1 local millicpu @@ -188,13 +189,19 @@ function common::get_cpu_num { if [[ $millicpu -eq -1 ]]; then # K8s no limits or in DinD - if [[ ! $parallelism_limit ]]; then + if [[ ! $parallelism_ci_cpu_cores ]]; then common::colorify "yellow" "Unable to calculate available CPU cors.\n" \ "You in K8s pod without limits or in DinD without limits propagation.\n" \ - "To avoid possible harm, set '--parallelism-limit' for hooks" + "To avoid possible harm, parallelism disabled.\n" \ + "If you'd like reenable it - set next for current hook:\n" \ + " args:\n" \ + " - --hook-config=--parallelism-ci-cpu-cores=N\n" \ + "where N is the number of CPU cores available in your environment." + + return 1 fi - return "$(nproc 2> /dev/null || echo 1)" + return "$parallelism_ci_cpu_cores" fi return $((millicpu / 1000)) @@ -289,11 +296,15 @@ function common::per_dir_hook { # this flag will limit the number of parallel processes parallelism_limit="$value" ;; + --parallelism-ci-cpu-cores) + # Used in corner cases when CPU cores can't be calculated + parallelism_ci_cpu_cores="$value" + ;; esac done set +e - common::get_cpu_num "$parallelism_limit" + common::get_cpu_num "$parallelism_ci_cpu_cores" CPU=$? set -e From f70bddde7e48371f54c21e705525c75e20c03dfa Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Wed, 14 Feb 2024 23:03:13 +0200 Subject: [PATCH 19/26] docs --- README.md | 10 ++++++++++ hooks/_common.sh | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 86e9c3ee4..2dd64c18f 100644 --- a/README.md +++ b/README.md @@ -393,6 +393,16 @@ If you'd like to set parallelism value relative to number of CPU logical cores - > > + + +```yaml +args: + - --hook-config=--parallelism-ci-cpu-cores=N +``` + +If you don't see code above in your `pre-commit-config.yaml` or logs - you don't need it. +`--parallelism-ci-cpu-cores` used only in corner cases. Check-out it usage in [hooks/_common.sh](hooks/_common.sh) + ### checkov (deprecated) and terraform_checkov > `checkov` hook is deprecated, please use `terraform_checkov`. diff --git a/hooks/_common.sh b/hooks/_common.sh index 26be059d4..532c7fe39 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -193,10 +193,10 @@ function common::get_cpu_num { common::colorify "yellow" "Unable to calculate available CPU cors.\n" \ "You in K8s pod without limits or in DinD without limits propagation.\n" \ "To avoid possible harm, parallelism disabled.\n" \ - "If you'd like reenable it - set next for current hook:\n" \ + "If you'd like reenable it - set corresponding limits, or set next for current hook:\n" \ " args:\n" \ " - --hook-config=--parallelism-ci-cpu-cores=N\n" \ - "where N is the number of CPU cores available in your environment." + "where N is the number of CPU cores you providing to pre-commit." return 1 fi From d2ec4d1ea99638a79a879ddb872d11d6cacbee57 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Thu, 15 Feb 2024 00:32:07 +0200 Subject: [PATCH 20/26] polishing --- hooks/_common.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hooks/_common.sh b/hooks/_common.sh index 532c7fe39..5113a81ca 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -305,7 +305,7 @@ function common::per_dir_hook { set +e common::get_cpu_num "$parallelism_ci_cpu_cores" - CPU=$? + local -r CPU=$? set -e # parallelism_limit can include reference to 'CPU' variable From 882c0f105c1ff16caf08134b69d3d8624c1febe8 Mon Sep 17 00:00:00 2001 From: Maksym Vlasov Date: Thu, 15 Feb 2024 16:04:45 +0200 Subject: [PATCH 21/26] Apply suggestions from code review Co-authored-by: George L. Yermulnik --- README.md | 2 +- hooks/_common.sh | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 2dd64c18f..8df1c74d2 100644 --- a/README.md +++ b/README.md @@ -401,7 +401,7 @@ args: ``` If you don't see code above in your `pre-commit-config.yaml` or logs - you don't need it. -`--parallelism-ci-cpu-cores` used only in corner cases. Check-out it usage in [hooks/_common.sh](hooks/_common.sh) +`--parallelism-ci-cpu-cores` used only in edge cases. Check-out it usage in [hooks/_common.sh](hooks/_common.sh) ### checkov (deprecated) and terraform_checkov diff --git a/hooks/_common.sh b/hooks/_common.sh index 5113a81ca..315adcdb3 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -171,10 +171,10 @@ function common::is_hook_run_on_whole_repo { } ####################################################################### -# Get the number of CPU logical cores available to pre-commit use +# Get the number of CPU logical cores available for pre-commit to use # Arguments: -# parallelism_ci_cpu_cores (string) Used in corner cases when CPU cores -# can't be calculated +# parallelism_ci_cpu_cores (string) Used in edge cases when number of +# CPU cores can't be derived automatically # Outputs: # Return number CPU logical cores, rounded to below integer ####################################################################### @@ -185,18 +185,18 @@ function common::get_cpu_num { if [[ -f /sys/fs/cgroup/cpu/cpu.cfs_quota_us ]]; then # Inside K8s pod or DInD in K8s - millicpu=$(cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us) + millicpu=$(< /sys/fs/cgroup/cpu/cpu.cfs_quota_us) if [[ $millicpu -eq -1 ]]; then # K8s no limits or in DinD if [[ ! $parallelism_ci_cpu_cores ]]; then - common::colorify "yellow" "Unable to calculate available CPU cors.\n" \ - "You in K8s pod without limits or in DinD without limits propagation.\n" \ - "To avoid possible harm, parallelism disabled.\n" \ + common::colorify "yellow" "Unable to derive number of available CPU cores.\n" \ + "Running inside K8s pod without limits or inside DinD without limits propagation.\n" \ + "To avoid possible harm, parallelism is disabled.\n" \ "If you'd like reenable it - set corresponding limits, or set next for current hook:\n" \ " args:\n" \ " - --hook-config=--parallelism-ci-cpu-cores=N\n" \ - "where N is the number of CPU cores you providing to pre-commit." + "where N is the number of CPU cores to allocate to pre-commit." return 1 fi @@ -211,7 +211,7 @@ function common::get_cpu_num { # Inside Linux (Docker?) container millicpu=$(cut -d' ' -f1 /sys/fs/cgroup/cpu.max) - if [[ "$millicpu" == "max" ]]; then + if [[ $millicpu == max ]]; then # No limits return "$(nproc 2> /dev/null || echo 1)" fi @@ -297,7 +297,7 @@ function common::per_dir_hook { parallelism_limit="$value" ;; --parallelism-ci-cpu-cores) - # Used in corner cases when CPU cores can't be calculated + # Used in edge cases when number of CPU cores can't be derived automatically parallelism_ci_cpu_cores="$value" ;; esac From 8b898e5d3e54b733a80d66b12ada81d7fe05f1ac Mon Sep 17 00:00:00 2001 From: Maksym Vlasov Date: Thu, 15 Feb 2024 16:07:14 +0200 Subject: [PATCH 22/26] Apply suggestions from code review --- hooks/_common.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/hooks/_common.sh b/hooks/_common.sh index 315adcdb3..0d290cae7 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -314,8 +314,7 @@ function common::per_dir_hook { if [[ ! $parallelism_limit ]]; then parallelism_limit=$((CPU - 1)) - fi - if [[ $parallelism_limit -le 1 ]]; then + elif [[ $parallelism_limit -le 1 ]]; then parallelism_limit=1 parallelism_disabled=true fi From a2b2db36cb16a3c3c2b48de612b8b92a7b964fa7 Mon Sep 17 00:00:00 2001 From: Maksym Vlasov Date: Thu, 15 Feb 2024 16:08:24 +0200 Subject: [PATCH 23/26] Apply suggestions from code review Co-authored-by: George L. Yermulnik --- hooks/_common.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hooks/_common.sh b/hooks/_common.sh index 0d290cae7..ced851186 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -193,7 +193,7 @@ function common::get_cpu_num { common::colorify "yellow" "Unable to derive number of available CPU cores.\n" \ "Running inside K8s pod without limits or inside DinD without limits propagation.\n" \ "To avoid possible harm, parallelism is disabled.\n" \ - "If you'd like reenable it - set corresponding limits, or set next for current hook:\n" \ + "To re-enable it, set corresponding limits, or set the following for the current hook:\n" \ " args:\n" \ " - --hook-config=--parallelism-ci-cpu-cores=N\n" \ "where N is the number of CPU cores to allocate to pre-commit." From 56c9ff8dd946a48a347760318acefcfd0961e076 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Thu, 15 Feb 2024 16:53:16 +0200 Subject: [PATCH 24/26] Apply review suggestions + simplify naming --- README.md | 4 ++-- hooks/_common.sh | 58 ++++++++++++++++++++++++++---------------------- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 8df1c74d2..26558c642 100644 --- a/README.md +++ b/README.md @@ -397,11 +397,11 @@ If you'd like to set parallelism value relative to number of CPU logical cores - ```yaml args: - - --hook-config=--parallelism-ci-cpu-cores=N + - --hook-config=--parallelism-cpu-cores=N ``` If you don't see code above in your `pre-commit-config.yaml` or logs - you don't need it. -`--parallelism-ci-cpu-cores` used only in edge cases. Check-out it usage in [hooks/_common.sh](hooks/_common.sh) +`--parallelism-cpu-cores` used only in edge cases. Check-out it usage in [hooks/_common.sh](hooks/_common.sh) ### checkov (deprecated) and terraform_checkov diff --git a/hooks/_common.sh b/hooks/_common.sh index ced851186..25adc18aa 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -173,13 +173,21 @@ function common::is_hook_run_on_whole_repo { ####################################################################### # Get the number of CPU logical cores available for pre-commit to use # Arguments: -# parallelism_ci_cpu_cores (string) Used in edge cases when number of +# parallelism_cpu_cores (string) Used in edge cases when number of # CPU cores can't be derived automatically # Outputs: # Return number CPU logical cores, rounded to below integer ####################################################################### function common::get_cpu_num { - local -r parallelism_ci_cpu_cores=$1 + local -r parallelism_cpu_cores=$1 + + if [[ -n $parallelism_cpu_cores ]]; then + # 22 EINVAL Invalid argument. Some invalid argument was supplied. + [[ $parallelism_cpu_cores =~ ^[[:digit:]]+$ ]] || return 22 + + echo "$parallelism_cpu_cores" + return + fi local millicpu @@ -189,22 +197,20 @@ function common::get_cpu_num { if [[ $millicpu -eq -1 ]]; then # K8s no limits or in DinD - if [[ ! $parallelism_ci_cpu_cores ]]; then - common::colorify "yellow" "Unable to derive number of available CPU cores.\n" \ - "Running inside K8s pod without limits or inside DinD without limits propagation.\n" \ - "To avoid possible harm, parallelism is disabled.\n" \ - "To re-enable it, set corresponding limits, or set the following for the current hook:\n" \ - " args:\n" \ - " - --hook-config=--parallelism-ci-cpu-cores=N\n" \ - "where N is the number of CPU cores to allocate to pre-commit." - - return 1 - fi - - return "$parallelism_ci_cpu_cores" + common::colorify "yellow" "Unable to derive number of available CPU cores.\n" \ + "Running inside K8s pod without limits or inside DinD without limits propagation.\n" \ + "To avoid possible harm, parallelism is disabled.\n" \ + "To re-enable it, set corresponding limits, or set the following for the current hook:\n" \ + " args:\n" \ + " - --hook-config=--parallelism-ci-cpu-cores=N\n" \ + "where N is the number of CPU cores to allocate to pre-commit." + + echo 1 + return fi - return $((millicpu / 1000)) + echo $((millicpu / 1000)) + return fi if [[ -f /sys/fs/cgroup/cpu.max ]]; then @@ -213,14 +219,17 @@ function common::get_cpu_num { if [[ $millicpu == max ]]; then # No limits - return "$(nproc 2> /dev/null || echo 1)" + nproc 2> /dev/null || echo 1 + return fi - return $((millicpu / 1000)) + + echo $((millicpu / 1000)) + return fi - # On host machine + # On host machine or any other case # `nproc` - linux, `sysctl -n hw.ncpu` - macOS, `echo 1` - fallback - return "$(nproc 2> /dev/null || sysctl -n hw.ncpu 2> /dev/null || echo 1)" + nproc 2> /dev/null || sysctl -n hw.ncpu 2> /dev/null || echo 1 } ####################################################################### @@ -296,17 +305,14 @@ function common::per_dir_hook { # this flag will limit the number of parallel processes parallelism_limit="$value" ;; - --parallelism-ci-cpu-cores) + --parallelism-cpu-cores) # Used in edge cases when number of CPU cores can't be derived automatically - parallelism_ci_cpu_cores="$value" + parallelism_cpu_cores="$value" ;; esac done - set +e - common::get_cpu_num "$parallelism_ci_cpu_cores" - local -r CPU=$? - set -e + CPU=$(common::get_cpu_num "$parallelism_cpu_cores") # parallelism_limit can include reference to 'CPU' variable parallelism_limit=$((parallelism_limit)) From 1851e114a259e42cb453ede967ed19e54aa66473 Mon Sep 17 00:00:00 2001 From: Maksym Vlasov Date: Thu, 15 Feb 2024 16:54:29 +0200 Subject: [PATCH 25/26] Update hooks/_common.sh Co-authored-by: George L. Yermulnik --- hooks/_common.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hooks/_common.sh b/hooks/_common.sh index 25adc18aa..6f6ce7bbe 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -176,7 +176,7 @@ function common::is_hook_run_on_whole_repo { # parallelism_cpu_cores (string) Used in edge cases when number of # CPU cores can't be derived automatically # Outputs: -# Return number CPU logical cores, rounded to below integer +# Returns number of CPU logical cores, rounded down to nearest integer ####################################################################### function common::get_cpu_num { local -r parallelism_cpu_cores=$1 From 0d0ce574cd95b0d86ae16d9910d15b8ca05e8e3d Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Thu, 15 Feb 2024 17:54:41 +0200 Subject: [PATCH 26/26] Show msgs i sterr (needed for common::get_cpu_num err_msg show up) --- hooks/_common.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hooks/_common.sh b/hooks/_common.sh index 6f6ce7bbe..cbe318e4f 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -396,7 +396,7 @@ function common::colorify { COLOR=$RESET fi - echo -e "${COLOR}${TEXT}${RESET}" + echo -e "${COLOR}${TEXT}${RESET}" >&2 } #######################################################################