2 vv_dir=$(dirname $(readlink -e $(which $0)))
3 echo clitk3 directory: $vv_dir
9 rm mem_use 2>>/dev/null
10 #kill -s SIGCONT ${make_pid} $(get_descendance ${make_pid})
12 kill -9 $(get_descendance ${make_pid})
14 echo "Terminated, exiting..."
21 function get_descendance
23 children=$(ps --ppid $1 -o pid --no-headers)
27 desc="${desc} ${c} $(get_descendance $c)"
32 trap handle_exit SIGINT
33 available_mem=$(cat /proc/meminfo | grep MemTotal | grep -o [0-9]*)
34 if [ -a "memory_exhausted_lock" ]
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..."
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..."
47 nice -n12 ionice -c3 make -j ${cpus} $@ &
48 make_pid=$(jobs -p %nice)
50 #watch memory use to avoid crashes
51 while ps $make_pid >>/dev/null
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) ))
59 touch memory_exhausted_lock
60 echo "Stopping due to exagerated memory use ( $used_mem )"
62 elif (( "$used_mem"> ($available_mem/2) ))
64 if [ x$high_mem != xtrue ]
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}
70 (ps --no-headers -o command ${pid} | grep make &>/dev/null) && kill -s SIGSTOP ${pid}
75 echo mem $used_mem / $available_mem
76 elif [ x$high_mem = xtrue ] && (( $(date +%s) > ( $date_mem + 5 ) ))
78 echo "Memory use back to normal"
80 #Restart all make commands
81 for pid in ${make_pid} ${descendance}
83 (ps --no-headers -o command ${pid} | grep make &>/dev/null) && kill -s SIGCONT ${pid}
89 rm memory_exhausted_lock 2>>/dev/null
90 echo Waiting for remaining jobs...