Skip to contentSkip to conversation

coccicheck: span checks across CPUs

PATCH In review Kees Cook 3 participants 3 messages Jun 18, 2013 – Jul 3, 2013

Subsystems (2)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH v3 1/1 coccicheck: span checks across CPUs

This adds parallelism by default to the "coccicheck" target using spatch's "-max" and "-index" arguments.

---
v3:
- quiet bash typo, thanks to Nicholas Palix for catching that.
v2:
- added job control to clean up on interrupt, suggested by Nicolas Palix.

---
 Documentation/coccinelle.txt |    5 +++++
 scripts/coccicheck           |   31 ++++++++++++++++++++++++++++---
 2 files changed, 33 insertions(+), 3 deletions(-)
Documentation/coccinelle.txt +5 -0
index 18de785..408439d 100644
@@ -91,6 +91,11 @@ To enable verbose messages set the V= variable, for example:
 
    make coccicheck MODE=report V=1
 
+By default, coccicheck tries to run as parallel as possible. To change
+the parallelism, set the J= variable. For example, to run across 4 CPUs:
+
+   make coccicheck MODE=report J=4
+
 
  Using Coccinelle with a single semantic patch
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
scripts/coccicheck +28 -3
index 06fcb33..446497f 100755
@@ -2,15 +2,24 @@
 
 SPATCH="`which ${SPATCH:=spatch}`"
 
+trap kill_running SIGTERM SIGINT
+declare -a SPATCH_PID
+
 # The verbosity may be set by the environmental parameter V=
 # as for example with 'make V=1 coccicheck'
 
 if [ -n "$V" -a "$V" != "0" ]; then
-	VERBOSE=1
+	VERBOSE="$V"
 else
 	VERBOSE=0
 fi
 
+if [ -z "$J" ]; then
+	NPROC=$(getconf _NPROCESSORS_ONLN)
+else
+	NPROC="$J"
+fi
+
 FLAGS="$SPFLAGS -very_quiet"
 
 # spatch only allows include directories with the syntax "-I include"
@@ -61,12 +70,28 @@ if [ "$ONLINE" = "0" ] ; then
 fi
 
 run_cmd() {
+	local i
 	if [ $VERBOSE -ne 0 ] ; then
-		echo "Running: $@"
+		echo "Running ($NPROC in parallel): $@"
 	fi
-	eval $@
+	for i in $(seq 0 $(( NPROC - 1)) ); do
+		eval "$@ -max $NPROC -index $i &"
+		SPATCH_PID[$i]=$!
+		if [ $VERBOSE -eq 2 ] ; then
+			echo "${SPATCH_PID[$i]} running"
+		fi
+	done
+	wait
 }
 
+kill_running() {
+	for i in $(seq $(( NPROC - 1 )) ); do
+		if [ $VERBOSE -eq 2 ] ; then
+			echo "Killing ${SPATCH_PID[$i]}"
+		fi
+		kill ${SPATCH_PID[$i]} 2>/dev/null
+	done
+}
 
 coccinelle () {
     COCCI="$1"
Signature · 6 lines
1.7.9.5


-- 
Kees Cook
Chrome OS Security
maintainer
#1 lore raw
Quoted 4 lines · Kees Cook

This adds parallelism by default to the "coccicheck" target using spatch's "-max" and "-index" arguments.

Quoted 91 lines

---
v3:
- quiet bash typo, thanks to Nicholas Palix for catching that.
v2:
- added job control to clean up on interrupt, suggested by Nicolas Palix.

---
 Documentation/coccinelle.txt |    5 +++++
 scripts/coccicheck           |   31 ++++++++++++++++++++++++++++---
 2 files changed, 33 insertions(+), 3 deletions(-)
Documentation/coccinelle.txt +5 -0
index 18de785..408439d 100644
@@ -91,6 +91,11 @@ To enable verbose messages set the V= variable, for example:
 
    make coccicheck MODE=report V=1
 
+By default, coccicheck tries to run as parallel as possible. To change
+the parallelism, set the J= variable. For example, to run across 4 CPUs:
+
+   make coccicheck MODE=report J=4
+
 
  Using Coccinelle with a single semantic patch
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
scripts/coccicheck +28 -4
index 06fcb33..446497f 100755
@@ -2,15 +2,24 @@
 
 SPATCH="`which ${SPATCH:=spatch}`"
 
+trap kill_running SIGTERM SIGINT
+declare -a SPATCH_PID
+
 # The verbosity may be set by the environmental parameter V=
 # as for example with 'make V=1 coccicheck'
 
 if [ -n "$V" -a "$V" != "0" ]; then
-       VERBOSE=1
+       VERBOSE="$V"
 else
        VERBOSE=0
 fi
 
+if [ -z "$J" ]; then
+       NPROC=$(getconf _NPROCESSORS_ONLN)
+else
+       NPROC="$J"
+fi
+
 FLAGS="$SPFLAGS -very_quiet"
 
 # spatch only allows include directories with the syntax "-I include"
@@ -61,12 +70,28 @@ if [ "$ONLINE" = "0" ] ; then
 fi
 
 run_cmd() {
+       local i
        if [ $VERBOSE -ne 0 ] ; then
-               echo "Running: $@"
+               echo "Running ($NPROC in parallel): $@"
        fi
-       eval $@
+       for i in $(seq 0 $(( NPROC - 1)) ); do
+               eval "$@ -max $NPROC -index $i &"
+               SPATCH_PID[$i]=$!
+               if [ $VERBOSE -eq 2 ] ; then
+                       echo "${SPATCH_PID[$i]} running"
+               fi
+       done
+       wait
 }
 
+kill_running() {
+       for i in $(seq $(( NPROC - 1 )) ); do
+               if [ $VERBOSE -eq 2 ] ; then
+                       echo "Killing ${SPATCH_PID[$i]}"
+               fi
+               kill ${SPATCH_PID[$i]} 2>/dev/null
+       done
+}
 
 coccinelle () {
     COCCI="$1"
--

1.7.9.5

Quoted 3 lines

--
Kees Cook
Chrome OS Security

--
Nicolas Palix
Tel: +33 4 76 51 46 27
http://membres-liglab.imag.fr/palix/