ci(coverity): scale build jobs with control group CPU quota

We currently scale the number of CPU jobs with the number of CPUs on the
host system. This is fine for a real system, but our Jenkins jobs run
within containers, which are given hard CPU quotas. Consequently, we are
running up against severe CPU throttling during static analysis.

This change ensures that we scale the number of Make jobs with the CPU
quota we have been provided, to mitigate against oversubscription.

Change-Id: I76dcfdbbebfc721af67f41d6370116c9c1126532
Signed-off-by: Chris Kay <chris.kay@arm.com>
diff --git a/script/tf-coverity/common-def.sh b/script/tf-coverity/common-def.sh
index e80128b..3471afa 100644
--- a/script/tf-coverity/common-def.sh
+++ b/script/tf-coverity/common-def.sh
@@ -24,14 +24,27 @@
 # Defines common flags between platforms
 common_flags() {
     local release="${1:-}"
-    local num_cpus="$(/usr/bin/getconf _NPROCESSORS_ONLN)"
-    local parallel_make="-j $num_cpus"
+    local jobs
+
+    # By default, scale number of jobs based on number of available processors
+    jobs=$(nproc || getconf _NPROCESSORS_ONLN || echo 1)
+
+    # Scale number of jobs based on control group CPU quota if configured
+    if [[ -r /sys/fs/cgroup/cpu.max ]]; then
+        if read -r quota period < /sys/fs/cgroup/cpu.max; then
+            # If quota is "max", then there is no restriction on CPU usage
+            if [[ "${quota}" != "max" ]]; then
+                jobs=$((quota / period))
+                jobs=$((jobs == 0 ? 1 : jobs))
+            fi
+        fi
+    fi
 
     # default to debug mode, unless a parameter is passed to the function
     debug="DEBUG=1"
     [ -n "$release" ] && debug=""
 
-    echo " $parallel_make $debug -s "
+    echo " --jobs ${jobs@Q} $debug -s "
 }
 
 # Check if execution environment is ARM's jenkins (Jenkins running under ARM