Skip to content

Commit c930a1b

Browse files
mcgrofMichal Marek
authored and
Michal Marek
committed
coccicheck: enable parmap support
Coccinelle has had parmap support since 1.0.2, this means it supports --jobs, enabling built-in multithreaded functionality, instead of needing one to script it out. Just look for --jobs in the help output to determine if this is supported and use it only if your number of processors detected is > 1. If parmap is enabled also enable the load balancing to be dynamic, so that if a thread finishes early we keep feeding it. stderr is currently sent to /dev/null, addressing a way to capture that will be addressed next. If --jobs is not supported we fallback to the old mechanism. We expect to deprecate the old mechanism as soon as we can get confirmation all users are ready. While at it propagate back into the shell script any coccinelle error code. When used in serialized mode where all cocci files are run this also stops processing if an error has occured. This lets us handle some errors in coccinelle cocci files and if they bail out we should inspect the errors. This will be more useful later to help annotate coccinelle version dependency requirements. This will let you run only SmPL files that your system supports. Extend Documentation/coccinelle.txt as well. As a small example, prior to this change, on an 8-core system: Before: $ export COCCI=scripts/coccinelle/free/kfree.cocci $ time make coccicheck MODE=report ... real 29m14.912s user 103m1.796s sys 0m4.464s After: real 16m22.435s user 128m30.060s sys 0m2.712s v4: o expand Documentation/coccinelle.txt to reflect parmap support info o update commit log to reflect what we actually do now with stderr o split out DEBUG_FILE use into another patch o detect number of CPUs and if its 1 then skip parmap support, note that if you still support parmap, but have 1 CPU you will also go through the new branches, so the old complex multithreaded process is skipped as well. v3: o move USE_JOBS to avoid being overriden v2: o redirect coccinelle stderr to /dev/null by default and only if DEBUG_FILE is used do we pass it to a file o fix typo of paramap/parmap Signed-off-by: Luis R. Rodriguez <[email protected]> Acked-by: Nicolas Palix <[email protected]> Signed-off-by: Michal Marek <[email protected]>
1 parent 8e826ad commit c930a1b

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

Documentation/coccinelle.txt

+15
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,26 @@ To enable verbose messages set the V= variable, for example:
9494

9595
make coccicheck MODE=report V=1
9696

97+
Coccinelle parallelization
98+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
99+
97100
By default, coccicheck tries to run as parallel as possible. To change
98101
the parallelism, set the J= variable. For example, to run across 4 CPUs:
99102

100103
make coccicheck MODE=report J=4
101104

105+
As of Coccinelle 1.0.2 Coccinelle uses Ocaml parmap for parallelization,
106+
if support for this is detected you will benefit from parmap parallelization.
107+
108+
When parmap is enabled coccicheck will enable dynamic load balancing by using
109+
'--chunksize 1' argument, this ensures we keep feeding threads with work
110+
one by one, so that we avoid the situation where most work gets done by only
111+
a few threads. With dynamic load balancing, if a thread finishes early we keep
112+
feeding it more work.
113+
114+
When parmap is enabled, if an error occurs in Coccinelle, this error
115+
value is propagated back, the return value of the 'make coccicheck'
116+
captures this return value.
102117

103118
Using Coccinelle with a single semantic patch
104119
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

scripts/coccicheck

+32-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ if [ ! -x "$SPATCH" ]; then
1212
exit 1
1313
fi
1414

15-
trap kill_running SIGTERM SIGINT
16-
declare -a SPATCH_PID
15+
USE_JOBS="no"
16+
$SPATCH --help | grep "\-\-jobs" > /dev/null && USE_JOBS="yes"
1717

1818
# The verbosity may be set by the environmental parameter V=
1919
# as for example with 'make V=1 coccicheck'
@@ -56,6 +56,16 @@ if [ "$KBUILD_EXTMOD" != "" ] ; then
5656
OPTIONS="--patch $srctree $OPTIONS"
5757
fi
5858

59+
# You can override by using SPFLAGS
60+
if [ "$USE_JOBS" = "no" ]; then
61+
trap kill_running SIGTERM SIGINT
62+
declare -a SPATCH_PID
63+
elif [ "$NPROC" != "1" ]; then
64+
# Using 0 should work as well, refer to _SC_NPROCESSORS_ONLN use on
65+
# https://github.com/rdicosmo/parmap/blob/master/setcore_stubs.c
66+
OPTIONS="$OPTIONS --jobs $NPROC --chunksize 1"
67+
fi
68+
5969
if [ "$MODE" = "" ] ; then
6070
if [ "$ONLINE" = "0" ] ; then
6171
echo 'You have not explicitly specified the mode to use. Using default "report" mode.'
@@ -82,7 +92,18 @@ if [ "$ONLINE" = "0" ] ; then
8292
echo ''
8393
fi
8494

85-
run_cmd() {
95+
run_cmd_parmap() {
96+
if [ $VERBOSE -ne 0 ] ; then
97+
echo "Running ($NPROC in parallel): $@"
98+
fi
99+
$@ 2>/dev/null
100+
if [[ $? -ne 0 ]]; then
101+
echo "coccicheck failed"
102+
exit $?
103+
fi
104+
}
105+
106+
run_cmd_old() {
86107
local i
87108
if [ $VERBOSE -ne 0 ] ; then
88109
echo "Running ($NPROC in parallel): $@"
@@ -97,6 +118,14 @@ run_cmd() {
97118
wait
98119
}
99120

121+
run_cmd() {
122+
if [ "$USE_JOBS" = "yes" ]; then
123+
run_cmd_parmap $@
124+
else
125+
run_cmd_old $@
126+
fi
127+
}
128+
100129
kill_running() {
101130
for i in $(seq 0 $(( NPROC - 1 )) ); do
102131
if [ $VERBOSE -eq 2 ] ; then

0 commit comments

Comments
 (0)