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