]> Creatis software - clitk.git/blobdiff - fast_make.sh
With ITK 5, add itkReadRawBytesAfterSwappingMacro and itkWriteRawBytesAfterSwappingMacro
[clitk.git] / fast_make.sh
old mode 100755 (executable)
new mode 100644 (file)
index 38d94a1..8de13fc
@@ -1,21 +1,36 @@
 #!/bin/bash
-vv_dir=$(dirname $(readlink -e $(which $0)))
+temp=$(which "$0")
+temp=$(readlink -e "${temp}")
+vv_dir=$(dirname "${temp}")
 echo clitk3 directory: $vv_dir
-cd ${vv_dir}/build
+cd "${vv_dir}/build"
 
 
 function handle_exit
 {
     rm mem_use 2>>/dev/null
-    killall -s SIGCONT make
-    killall make
-    killall cc1plus
+    #kill -s SIGCONT ${make_pid} $(get_descendance ${make_pid})
+    #sleep 1
+    kill -9 $(get_descendance ${make_pid})
+    kill -9 ${make_pid}
     echo "Terminated, exiting..."
+    sleep 1
     echo
     echo
     exit
 }
 
+function get_descendance
+{
+    children=$(ps --ppid $1 -o pid --no-headers)
+    local desc=""
+    for c in $children
+    do
+        desc="${desc} ${c} $(get_descendance $c)"
+    done
+    echo $desc
+}
+
 trap handle_exit SIGINT
 available_mem=$(cat /proc/meminfo | grep MemTotal | grep -o [0-9]*)
 if [ -a "memory_exhausted_lock" ]
@@ -35,39 +50,46 @@ nice -n12 ionice -c3 make -j ${cpus} $@ &
 make_pid=$(jobs -p %nice)
 
 #watch memory use to avoid crashes
-#while ps $make_pid >>/dev/null 
-#do
-#    if [ x"$(ps aux | grep cc1plus | grep -v grep | wc -l)" != x0 ]
-#    then
-#        ps ax -o vsize,comm | grep cc1plus | grep -o "\<[0-9]*\>" > mem_use
-#        used_mem=$(awk 'BEGIN {sum=0;} {sum+=$1;} END {print sum;}' mem_use)
-#        if (( "$used_mem"> ($available_mem - 300) ))
-#        then
-#            touch memory_exhausted_lock
-#            echo "Stopping due to exagerated memory use ( $used_mem )"
-#            handle_exit
-#        elif (( "$used_mem"> ($available_mem/2) ))
-#        then
-#            if [ x$high_mem != xtrue ]
-#            then
-#                echo "Warning, high memory use, not spawning any more compilation jobs... ( $used_mem )"
-#                killall -s SIGSTOP make
-#                killall -s SIGCONT cc1plus
-#                high_mem="true"
-#                date_mem=$(date +%s)
-#            fi
-#            echo mem $used_mem / $available_mem
-#        elif [ x$high_mem = xtrue ] && (( $(date +%s) > ( $date_mem + 5 ) ))
-#        then
-#            echo "Memory use back to normal"
-#            high_mem=""
-#            killall -s SIGCONT make
-#        fi
-#        rm mem_use
-#    fi
-#    sleep 1
-#done
-#rm memory_exhausted_lock 2>>/dev/null
+while ps $make_pid >>/dev/null 
+do
+    descendance=$(get_descendance ${make_pid})
+    #echo ${make_pid} $descendance
+    ps -o vsize --no-headers ${make_pid} ${descendance} > mem_use
+    used_mem=$(awk 'BEGIN {sum=0;} {sum+=$1;} END {print sum;}' mem_use)
+    if (( "$used_mem"> ($available_mem - 300) ))
+    then
+        touch memory_exhausted_lock
+        echo "Stopping due to exagerated memory use ( $used_mem )"
+        handle_exit
+    elif (( "$used_mem"> ($available_mem/2) ))
+    then
+        if [ x$high_mem != xtrue ]
+        then
+            echo "Warning, high memory use, not spawning any more compilation jobs... ( $used_mem )"
+            #Stop all make commands
+            for pid in ${make_pid} ${descendance}
+            do
+                (ps --no-headers -o command ${pid} | grep make &>/dev/null) && kill -s SIGSTOP ${pid}
+            done
+            high_mem="true"
+            date_mem=$(date +%s)
+        fi
+        echo mem $used_mem / $available_mem
+    elif [ x$high_mem = xtrue ] && (( $(date +%s) > ( $date_mem + 5 ) ))
+    then
+        echo "Memory use back to normal"
+        high_mem=""
+        #Restart all make commands
+        for pid in ${make_pid} ${descendance}
+        do
+            (ps --no-headers -o command ${pid} | grep make &>/dev/null) && kill -s SIGCONT ${pid}
+        done
+    fi
+    rm mem_use
+    sleep 1
+done
+rm memory_exhausted_lock 2>>/dev/null
+echo Waiting for remaining jobs...
 wait
 echo Done!
 echo