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