From f425f2bbd6a8c36a91965bc5a6a6bc28a0912ce3 Mon Sep 17 00:00:00 2001
From: schaerer <schaerer>
Date: Tue, 5 Oct 2010 18:00:15 +0000
Subject: [PATCH] revived old fast_make script / clean some dependencies

---
 common/clitkFilterBase.cxx                  |  1 +
 common/clitkFilterBase.h                    |  2 +-
 common/clitkImageCommon.cxx                 |  1 +
 common/clitkImageCommon.h                   |  2 +-
 common/clitkImageToImageGenericFilterBase.h |  1 -
 common/clitkLabelizeParameters.h            |  2 +-
 common/clitkTimer.h                         |  2 +-
 fast_make.sh                                | 74 +++++++++++++--------
 8 files changed, 53 insertions(+), 32 deletions(-)

diff --git a/common/clitkFilterBase.cxx b/common/clitkFilterBase.cxx
index 0f7e852..09797d8 100644
--- a/common/clitkFilterBase.cxx
+++ b/common/clitkFilterBase.cxx
@@ -18,6 +18,7 @@
 
 // clitk
 #include "clitkFilterBase.h"
+#include "clitkCommon.h"
 
 //--------------------------------------------------------------------
 clitk::FilterBase::FilterBase() 
diff --git a/common/clitkFilterBase.h b/common/clitkFilterBase.h
index 1cd7579..a53f655 100644
--- a/common/clitkFilterBase.h
+++ b/common/clitkFilterBase.h
@@ -20,7 +20,7 @@
 #define CLITKFILTERBASE_H
 
 // clitk
-#include "clitkCommon.h"
+//#include "clitkCommon.h"
 #include "clitkTimer.h"
 #include "clitkFilterMacros.txx"
 #include "clitkLabelizeParameters.h"
diff --git a/common/clitkImageCommon.cxx b/common/clitkImageCommon.cxx
index 9367257..cfcbbe3 100644
--- a/common/clitkImageCommon.cxx
+++ b/common/clitkImageCommon.cxx
@@ -20,6 +20,7 @@
 #define CLITKIMAGECOMMON_CXX
 
 #include "clitkImageCommon.h"
+#include "clitkCommon.h"
 
 //--------------------------------------------------------------------
 void clitk::ReadImageDimensionAndPixelType(const std::string & filename,
diff --git a/common/clitkImageCommon.h b/common/clitkImageCommon.h
index 9744fc9..9e61223 100644
--- a/common/clitkImageCommon.h
+++ b/common/clitkImageCommon.h
@@ -19,7 +19,7 @@
 #define CLITKIMAGECOMMON_H
 
 // clitk
-#include "clitkCommon.h"
+//#include "clitkCommon.h"
 
 // itk
 #include "itkImage.h"
diff --git a/common/clitkImageToImageGenericFilterBase.h b/common/clitkImageToImageGenericFilterBase.h
index 86cd77a..280dd65 100644
--- a/common/clitkImageToImageGenericFilterBase.h
+++ b/common/clitkImageToImageGenericFilterBase.h
@@ -20,7 +20,6 @@
 #define CLITKIMAGETOIMAGEGENERICFILTERBASE_H
 
 // clitk 
-#include "clitkCommon.h"
 #include "clitkImageCommon.h"
 #include "clitkCommonGenericFilter.h"
 #include "clitkFilterBase.h"
diff --git a/common/clitkLabelizeParameters.h b/common/clitkLabelizeParameters.h
index 7a4ced9..8f7c126 100644
--- a/common/clitkLabelizeParameters.h
+++ b/common/clitkLabelizeParameters.h
@@ -20,7 +20,7 @@
 #define CLITKLABELIZEPARAMETERS_H
 
 // clitk
-#include "clitkCommon.h"
+//#include "clitkCommon.h"
 
 // itk
 #include "itkObject.h"
diff --git a/common/clitkTimer.h b/common/clitkTimer.h
index 2134f41..dcc32be 100644
--- a/common/clitkTimer.h
+++ b/common/clitkTimer.h
@@ -27,7 +27,7 @@
 
    ===================================================================*/
 
-#include "clitkCommon.h"
+//#include "clitkCommon.h"
 #include <ctime> 
 #if defined(unix) || defined(__APPLE__)
 #  include <sys/time.h>
diff --git a/fast_make.sh b/fast_make.sh
index 0ee2e4f..7a11e0a 100755
--- a/fast_make.sh
+++ b/fast_make.sh
@@ -7,15 +7,28 @@ cd ${vv_dir}/build
 function handle_exit
 {
     rm mem_use 2>>/dev/null
-    killall -s SIGCONT make
-    killall make
-    killall cc1plus
+    #kill -s SIGCONT ${make_pid} $(get_descendance ${make_pid})
+    #sleep 1
+    kill -9 $(get_descendance ${make_pid})
+    kill -9 ${make_pid}
     echo "Terminated, exiting..."
+    sleep 1
     echo
     echo
     exit
 }
 
+function get_descendance
+{
+    children=$(ps --ppid $1 -o pid --no-headers)
+    local desc=""
+    for c in $children
+    do
+        desc="${desc} ${c} $(get_descendance $c)"
+    done
+    echo $desc
+}
+
 trap handle_exit SIGINT
 available_mem=$(cat /proc/meminfo | grep MemTotal | grep -o [0-9]*)
 if [ -a "memory_exhausted_lock" ]
@@ -37,37 +50,44 @@ make_pid=$(jobs -p %nice)
 #watch memory use to avoid crashes
 while ps $make_pid >>/dev/null 
 do
-    if [ x"$(ps aux | grep cc1plus | grep -v grep | wc -l)" != x0 ]
+    descendance=$(get_descendance ${make_pid})
+    #echo ${make_pid} $descendance
+    ps -o vsize --no-headers ${make_pid} ${descendance} > mem_use
+    used_mem=$(awk 'BEGIN {sum=0;} {sum+=$1;} END {print sum;}' mem_use)
+    if (( "$used_mem"> ($available_mem - 300) ))
     then
-        ps ax -o vsize,comm | grep cc1plus | grep -o "\<[0-9]*\>" > mem_use
-        used_mem=$(awk 'BEGIN {sum=0;} {sum+=$1;} END {print sum;}' mem_use)
-        if (( "$used_mem"> ($available_mem - 300) ))
-        then
-            touch memory_exhausted_lock
-            echo "Stopping due to exagerated memory use ( $used_mem )"
-            handle_exit
-        elif (( "$used_mem"> ($available_mem/2) ))
-        then
-            if [ x$high_mem != xtrue ]
-            then
-                echo "Warning, high memory use, not spawning any more compilation jobs... ( $used_mem )"
-                killall -s SIGSTOP make
-                killall -s SIGCONT cc1plus
-                high_mem="true"
-                date_mem=$(date +%s)
-            fi
-            echo mem $used_mem / $available_mem
-        elif [ x$high_mem = xtrue ] && (( $(date +%s) > ( $date_mem + 5 ) ))
+        touch memory_exhausted_lock
+        echo "Stopping due to exagerated memory use ( $used_mem )"
+        handle_exit
+    elif (( "$used_mem"> ($available_mem/2) ))
+    then
+        if [ x$high_mem != xtrue ]
         then
-            echo "Memory use back to normal"
-            high_mem=""
-            killall -s SIGCONT make
+            echo "Warning, high memory use, not spawning any more compilation jobs... ( $used_mem )"
+            #Stop all make commands
+            for pid in ${make_pid} ${descendance}
+            do
+                (ps --no-headers -o command ${pid} | grep make &>/dev/null) && kill -s SIGSTOP ${pid}
+            done
+            high_mem="true"
+            date_mem=$(date +%s)
         fi
-        rm mem_use
+        echo mem $used_mem / $available_mem
+    elif [ x$high_mem = xtrue ] && (( $(date +%s) > ( $date_mem + 5 ) ))
+    then
+        echo "Memory use back to normal"
+        high_mem=""
+        #Restart all make commands
+        for pid in ${make_pid} ${descendance}
+        do
+            (ps --no-headers -o command ${pid} | grep make &>/dev/null) && kill -s SIGCONT ${pid}
+        done
     fi
+    rm mem_use
     sleep 1
 done
 rm memory_exhausted_lock 2>>/dev/null
+echo Waiting for remaining jobs...
 wait
 echo Done!
 echo
-- 
2.49.0