Skip to content

Commit f1c903d

Browse files
avarttaylorr
authored andcommitted
cocci: make "coccicheck" rule incremental
Optimize the very slow "coccicheck" target to take advantage of incremental rebuilding, and fix outstanding dependency problems with the existing rule. The rule is now faster both on the initial run as we can make better use of GNU make's parallelism than the old ad-hoc combination of make's parallelism combined with $(SPATCH_BATCH_SIZE) and/or the "--jobs" argument to "spatch(1)". It also makes us *much* faster when incrementally building, it's now viable to "make coccicheck" as topic branches are merged down. The rule didn't use FORCE (or its equivalents) before, so a: make coccicheck make coccicheck Would report nothing to do on the second iteration. But all of our patch output depended on all $(COCCI_SOURCES) files, therefore e.g.: make -W grep.c coccicheck Would do a full re-run, i.e. a a change in a single file would force us to do a full re-run. The reason for this (not the initial rationale, but my analysis) is: * Since we create a single "*.cocci.patch+" we don't know where to pick up where we left off, or how to incrementally merge e.g. a "grep.c" change with an existing *.cocci.patch. * We've been carrying forward the dependency on the *.c files since 63f0a75 (add coccicheck make target, 2016-09-15) the rule was initially added as a sort of poor man's dependency discovery. As we don't include other *.c files depending on other *.c files has always been broken, as could be trivially demonstrated e.g. with: make coccicheck make -W strbuf.h coccicheck However, depending on the corresponding *.c files has been doing something, namely that *if* an API change modified both *.c and *.h files we'd catch the change to the *.h we care about via the *.c being changed. For API changes that happened only via *.h files we'd do the wrong thing before this change, but e.g. for function additions (not "static inline" ones) catch the *.h change by proxy. Now we'll instead: * Create a <RULE>/<FILE> pair in the .build directory, E.g. for swap.cocci and grep.c we'll create .build/contrib/coccinelle/swap.cocci.patch/grep.c. That file is the diff we'll apply for that <RULE>-<FILE> combination, if there's no changes to me made (the common case) it'll be an empty file. * Our generated *.patch file (e.g. contrib/coccinelle/swap.cocci.patch) is now a simple "cat $^" of all of all of the <RULE>/<FILE> files for a given <RULE>. In the case discussed above of "grep.c" being changed we'll do the full "cat" every time, so they resulting *.cocci.patch will always be correct and up-to-date, even if it's "incrementally updated". See 1cc0425 (Makefile: have "make pot" not "reset --hard", 2022-05-26) for another recent rule that used that technique. As before we'll: * End up generating a contrib/coccinelle/swap.cocci.patch, if we "fail" by creating a non-empty patch we'll still exit with a zero exit code. Arguably we should move to a more Makefile-native way of doing this, i.e. fail early, and if we want all of the "failed" changes we can use "make -k", but as the current "ci/run-static-analysis.sh" expects us to behave this way let's keep the existing behavior of exhaustively discovering all cocci changes, and only failing if spatch itself errors out. Further implementation details & notes: * Before this change running "make coccicheck" would by default end up pegging just one CPU at the very end for a while, usually as we'd finish whichever *.cocci rule was the most expensive. This could be mitigated by combining "make -jN" with SPATCH_BATCH_SIZE, see 960154b (coccicheck: optionally batch spatch invocations, 2019-05-06). There will be cases where getting rid of "SPATCH_BATCH_SIZE" makes things worse, but a from-scratch "make coccicheck" with the default of SPATCH_BATCH_SIZE=1 (and tweaking it doesn't make a difference) is faster (~3m36s v.s. ~3m56s) with this approach, as we can feed the CPU more work in a less staggered way. * Getting rid of "SPATCH_BATCH_SIZE" particularly helps in cases where the default of 1 yields parallelism under "make coccicheck", but then running e.g.: make -W contrib/coccinelle/swap.cocci coccicheck I.e. before that would use only one CPU core, until the user remembered to adjust "SPATCH_BATCH_SIZE" differently than the setting that makes sense when doing a non-incremental run of "make coccicheck". * Before the "make coccicheck" rule would have to clean "contrib/coccinelle/*.cocci.patch*", since we'd create "*+" and "*.log" files there. Now those are created in .build/contrib/coccinelle/, which is covered by the "cocciclean" rule already. Outstanding issues & future work: * We could get rid of "--all-includes" in favor of manually specifying a list of includes to give to "spatch(1)". As noted upthread of [1] a naïve removal of "--all-includes" will result in broken *.cocci patches, but if we know the exhaustive list of includes via COMPUTE_HEADER_DEPENDENCIES we don't need to re-scan for them, we could grab the headers to include from the .depend.d/<file>.o.d and supply them with the "--include" option to spatch(1).q 1. https://lore.kernel.org/git/[email protected]/ Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Taylor Blau <[email protected]>
1 parent 60cfad9 commit f1c903d

File tree

3 files changed

+58
-28
lines changed

3 files changed

+58
-28
lines changed

Makefile

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,18 +1298,13 @@ SANITIZE_ADDRESS =
12981298
SPATCH_INCLUDE_FLAGS = --all-includes
12991299
SPATCH_FLAGS =
13001300
SPATCH_TEST_FLAGS =
1301-
# Setting SPATCH_BATCH_SIZE higher will
1302-
# usually result in less CPU usage at the cost of higher peak memory.
1303-
# Setting it to 0 will feed all files in a single spatch invocation.
1304-
SPATCH_BATCH_SIZE = 1
13051301

13061302
# Rebuild 'coccicheck' if $(SPATCH), its flags etc. change
13071303
TRACK_SPATCH_DEFINES =
13081304
TRACK_SPATCH_DEFINES += $(SPATCH)
13091305
TRACK_SPATCH_DEFINES += $(SPATCH_INCLUDE_FLAGS)
13101306
TRACK_SPATCH_DEFINES += $(SPATCH_FLAGS)
13111307
TRACK_SPATCH_DEFINES += $(SPATCH_TEST_FLAGS)
1312-
TRACK_SPATCH_DEFINES += $(SPATCH_BATCH_SIZE)
13131308
GIT-SPATCH-DEFINES: FORCE
13141309
@FLAGS='$(TRACK_SPATCH_DEFINES)'; \
13151310
if test x"$$FLAGS" != x"`cat GIT-SPATCH-DEFINES 2>/dev/null`" ; then \
@@ -3158,39 +3153,73 @@ check: $(GENERATED_H)
31583153

31593154
COCCI_GLOB = $(wildcard contrib/coccinelle/*.cocci)
31603155
COCCI_RULES = $(COCCI_GLOB)
3156+
COCCI_NAMES = $(COCCI_RULES:contrib/coccinelle/%.cocci=%)
31613157

31623158
COCCICHECK_PENDING = $(filter %.pending.cocci,$(COCCI_RULES))
31633159
COCCICHECK = $(filter-out $(COCCICHECK_PENDING),$(COCCI_RULES))
31643160

31653161
COCCICHECK_PATCHES = $(COCCICHECK:%=%.patch)
31663162
COCCICHECK_PATCHES_PENDING = $(COCCICHECK_PENDING:%=%.patch)
31673163

3164+
# It's expensive to compute the many=many rules below, only eval them
3165+
# on $(MAKECMDGOALS) that match these $(COCCI_RULES)
3166+
COCCI_RULES_GLOB =
3167+
COCCI_RULES_GLOB += cocci%
3168+
COCCI_RULES_GLOB += .build/contrib/coccinelle/%
3169+
COCCI_RULES_GLOB += $(COCCICHECK_PATCHES)
3170+
COCCI_RULES_GLOB += $(COCCICHEC_PATCHES_PENDING)
3171+
COCCI_GOALS = $(filter $(COCCI_RULES_GLOB),$(MAKECMDGOALS))
3172+
31683173
COCCI_TEST_RES = $(wildcard contrib/coccinelle/tests/*.res)
31693174

3170-
COCCI_PATCHES = $(COCCI_RULES:%=%.patch)
3171-
$(COCCI_PATCHES): GIT-SPATCH-DEFINES
3172-
$(COCCI_PATCHES): $(COCCI_SOURCES)
3173-
$(COCCI_PATCHES): %.patch: %
3174-
$(QUIET_SPATCH) \
3175-
if test $(SPATCH_BATCH_SIZE) = 0; then \
3176-
limit=; \
3177-
else \
3178-
limit='-n $(SPATCH_BATCH_SIZE)'; \
3179-
fi; \
3180-
if ! echo $(COCCI_SOURCES) | xargs $$limit \
3181-
$(SPATCH) $(SPATCH_FLAGS) \
3182-
$(SPATCH_INCLUDE_FLAGS) \
3183-
--sp-file $< --patch . \
3184-
>$@+ 2>$@.log; \
3175+
.build/contrib/coccinelle/FOUND_H_SOURCES: $(FOUND_H_SOURCES)
3176+
$(call mkdir_p_parent_template)
3177+
$(QUIET_GEN) >$@
3178+
3179+
define cocci-rule
3180+
3181+
## Rule for .build/$(1).patch/$(2); Params:
3182+
# $(1) = e.g. "free.cocci"
3183+
# $(2) = e.g. "grep.c"
3184+
COCCI_$(1:contrib/coccinelle/%.cocci=%) += .build/$(1).patch/$(2)
3185+
.build/$(1).patch/$(2): GIT-SPATCH-DEFINES
3186+
.build/$(1).patch/$(2): .build/contrib/coccinelle/FOUND_H_SOURCES
3187+
.build/$(1).patch/$(2): $(1)
3188+
.build/$(1).patch/$(2): .build/$(1).patch/% : %
3189+
$$(call mkdir_p_parent_template)
3190+
$$(QUIET_SPATCH)if ! $$(SPATCH) $$(SPATCH_FLAGS) \
3191+
$$(SPATCH_INCLUDE_FLAGS) \
3192+
--sp-file $(1) --patch . $$< \
3193+
>$$@ 2>$$@.log; \
31853194
then \
3186-
cat $@.log; \
3195+
echo "ERROR when applying '$(1)' to '$$<'; '$$@.log' follows:"; \
3196+
cat $$@.log; \
31873197
exit 1; \
3188-
fi; \
3189-
mv $@+ $@; \
3190-
if test -s $@; \
3198+
fi
3199+
endef
3200+
3201+
define cocci-matrix
3202+
3203+
$(foreach s,$(COCCI_SOURCES),$(call cocci-rule,$(1),$(s)))
3204+
endef
3205+
3206+
ifdef COCCI_GOALS
3207+
$(eval $(foreach c,$(COCCI_RULES),$(call cocci-matrix,$(c))))
3208+
endif
3209+
3210+
define spatch-rule
3211+
3212+
contrib/coccinelle/$(1).cocci.patch: $$(COCCI_$(1))
3213+
$$(QUIET_SPATCH_CAT)cat $$^ >$$@ && \
3214+
if test -s $$@; \
31913215
then \
3192-
echo ' ' SPATCH result: $@; \
3216+
echo ' ' SPATCH result: $$@; \
31933217
fi
3218+
endef
3219+
3220+
ifdef COCCI_GOALS
3221+
$(eval $(foreach n,$(COCCI_NAMES),$(call spatch-rule,$(n))))
3222+
endif
31943223

31953224
COCCI_TEST_RES_GEN = $(addprefix .build/,$(COCCI_TEST_RES))
31963225
$(COCCI_TEST_RES_GEN): GIT-SPATCH-DEFINES
@@ -3482,7 +3511,7 @@ profile-clean:
34823511
cocciclean:
34833512
$(RM) GIT-SPATCH-DEFINES
34843513
$(RM) -r .build/contrib/coccinelle
3485-
$(RM) contrib/coccinelle/*.cocci.patch*
3514+
$(RM) contrib/coccinelle/*.cocci.patch
34863515

34873516
clean: profile-clean coverage-clean cocciclean
34883517
$(RM) -r .build

contrib/coccinelle/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
*.patch*
1+
*.patch

shared.mak

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ ifndef V
7171
QUIET_RC = @echo ' ' RC $@;
7272

7373
## Used in "Makefile": SPATCH
74-
QUIET_SPATCH = @echo ' ' SPATCH $<;
74+
QUIET_SPATCH = @echo ' ' SPATCH $@;
7575
QUIET_SPATCH_TEST = @echo ' ' SPATCH TEST $(@:.build/%=%);
76+
QUIET_SPATCH_CAT = @echo ' ' SPATCH CAT $$^ \>$@;
7677

7778
## Used in "Documentation/Makefile"
7879
QUIET_ASCIIDOC = @echo ' ' ASCIIDOC $@;

0 commit comments

Comments
 (0)