]> Creatis software - clitk.git/blob - fast_make.sh
cleanup / better handling of errors
[clitk.git] / fast_make.sh
1 #!/bin/bash
2 vv_dir=$(dirname $(readlink -e $(which $0)))
3 echo vv directory: $vv_dir
4 cd ${vv_dir}/build
5
6 function handle_exit
7 {
8     rm mem_use 2>>/dev/null
9     killall -s SIGCONT make
10     killall make
11     killall cc1plus
12     echo "Terminated, exiting..."
13     echo
14     echo
15     exit
16 }
17
18 trap handle_exit SIGINT
19 available_mem=$(cat /proc/meminfo | grep MemTotal | grep -o [0-9]*)
20 if [ -a "memory_exhausted_lock" ]
21 then
22    echo "Running in memory conservation mode..."
23    max_cpp_process_mem_use=1600000
24    cpus=$(( $available_mem / $max_cpp_process_mem_use ))
25    echo "Using $cpus cpu(s) should be safe..."
26    sleep 1
27    make -j${cpus}
28 else #use all the available computing power by default
29     cpus=$(( $(cat /proc/cpuinfo | grep -c ^processor) + 0 ))
30 fi
31
32 nice -n12 ionice -c3 make -j ${cpus} $@ &
33 make_pid=$(jobs -p %nice)
34
35 #watch memory use to avoid crashes
36 while ps $make_pid >>/dev/null 
37 do
38     if [ x"$(ps aux | grep cc1plus | grep -v grep | wc -l)" != x0 ]
39     then
40         ps ax -o vsize,comm | grep cc1plus | grep -o "\<[0-9]*\>" > mem_use
41         used_mem=$(awk 'BEGIN {sum=0;} {sum+=$1;} END {print sum;}' mem_use)
42         if (( "$used_mem"> ($available_mem - 300) ))
43         then
44             touch memory_exhausted_lock
45             echo "Stopping due to exagerated memory use ( $used_mem )"
46             handle_exit
47         elif (( "$used_mem"> ($available_mem/2) ))
48         then
49             if [ x$high_mem != xtrue ]
50             then
51                 echo "Warning, high memory use, not spawning any more compilation jobs... ( $used_mem )"
52                 killall -s SIGSTOP make
53                 killall -s SIGCONT cc1plus
54                 high_mem="true"
55                 date_mem=$(date +%s)
56             fi
57             echo mem $used_mem / $available_mem
58         elif [ x$high_mem = xtrue ] && (( $(date +%s) > ( $date_mem + 5 ) ))
59         then
60             echo "Memory use back to normal"
61             high_mem=""
62             killall -s SIGCONT make
63         fi
64         rm mem_use
65     fi
66     sleep 1
67 done
68 rm memory_exhausted_lock 2>>/dev/null
69 echo Done!
70 echo