]> Creatis software - clitk.git/blob - fast_make.sh
revived old fast_make script / clean some dependencies
[clitk.git] / fast_make.sh
1 #!/bin/bash
2 vv_dir=$(dirname $(readlink -e $(which $0)))
3 echo clitk3 directory: $vv_dir
4 cd ${vv_dir}/build
5
6
7 function handle_exit
8 {
9     rm mem_use 2>>/dev/null
10     #kill -s SIGCONT ${make_pid} $(get_descendance ${make_pid})
11     #sleep 1
12     kill -9 $(get_descendance ${make_pid})
13     kill -9 ${make_pid}
14     echo "Terminated, exiting..."
15     sleep 1
16     echo
17     echo
18     exit
19 }
20
21 function get_descendance
22 {
23     children=$(ps --ppid $1 -o pid --no-headers)
24     local desc=""
25     for c in $children
26     do
27         desc="${desc} ${c} $(get_descendance $c)"
28     done
29     echo $desc
30 }
31
32 trap handle_exit SIGINT
33 available_mem=$(cat /proc/meminfo | grep MemTotal | grep -o [0-9]*)
34 if [ -a "memory_exhausted_lock" ]
35 then
36    echo "Running in memory conservation mode..."
37    max_cpp_process_mem_use=1600000
38    cpus=$(( $available_mem / $max_cpp_process_mem_use ))
39    echo "Using $cpus cpu(s) should be safe..."
40    sleep 1
41    make -j${cpus}
42 else #use all the available computing power by default
43     cpus=$(( $(cat /proc/cpuinfo | grep -c ^processor) + 0 ))
44     echo "Building with ${cpus} cpus..."
45 fi
46
47 nice -n12 ionice -c3 make -j ${cpus} $@ &
48 make_pid=$(jobs -p %nice)
49
50 #watch memory use to avoid crashes
51 while ps $make_pid >>/dev/null 
52 do
53     descendance=$(get_descendance ${make_pid})
54     #echo ${make_pid} $descendance
55     ps -o vsize --no-headers ${make_pid} ${descendance} > mem_use
56     used_mem=$(awk 'BEGIN {sum=0;} {sum+=$1;} END {print sum;}' mem_use)
57     if (( "$used_mem"> ($available_mem - 300) ))
58     then
59         touch memory_exhausted_lock
60         echo "Stopping due to exagerated memory use ( $used_mem )"
61         handle_exit
62     elif (( "$used_mem"> ($available_mem/2) ))
63     then
64         if [ x$high_mem != xtrue ]
65         then
66             echo "Warning, high memory use, not spawning any more compilation jobs... ( $used_mem )"
67             #Stop all make commands
68             for pid in ${make_pid} ${descendance}
69             do
70                 (ps --no-headers -o command ${pid} | grep make &>/dev/null) && kill -s SIGSTOP ${pid}
71             done
72             high_mem="true"
73             date_mem=$(date +%s)
74         fi
75         echo mem $used_mem / $available_mem
76     elif [ x$high_mem = xtrue ] && (( $(date +%s) > ( $date_mem + 5 ) ))
77     then
78         echo "Memory use back to normal"
79         high_mem=""
80         #Restart all make commands
81         for pid in ${make_pid} ${descendance}
82         do
83             (ps --no-headers -o command ${pid} | grep make &>/dev/null) && kill -s SIGCONT ${pid}
84         done
85     fi
86     rm mem_use
87     sleep 1
88 done
89 rm memory_exhausted_lock 2>>/dev/null
90 echo Waiting for remaining jobs...
91 wait
92 echo Done!
93 echo