Fix build problems related to profile-directed optimization
RFC PATCH 1/1 Fix build problems related to profile-directed optimization
There was a number of problems I ran into when trying the profile-directed optimizations added by Andi Kleen in git commit 7ddc2710b9. (This was using gcc 4.4 found on many enterprise distros.)
1) The -fprofile-generate and -fprofile-use commands are incompatible with ccache; the code ends up looking in the wrong place for the gcda files based on the ccache object names.
2) If the makefile notices that CFLAGS are different, it will rebuild all of the binaries. Hence the recipe originally specified by the INSTALL file ("make profile-all" followed by "make install") doesn't work. It will appear to work, but the binaries will end up getting built with no optimization.
This patch fixes this by using an explicit set of options passed via PROFILE_GEN and PROFILE_USE and then using these to directly manipulate CFLAGS and EXTLIBS.
--- INSTALL | 4 ++-- Makefile | 31 +++++++++++++++++++++++-------- 2 files changed, 25 insertions(+), 10 deletions(-)
INSTALL +2 -2
@@ -28,8 +28,8 @@ set up install paths (via config.mak.autogen), so you can write instead If you're willing to trade off (much) longer build time for a later faster git you can also do a profile feedback build with - $ make profile-all - # make prefix=... install + $ make prefix=... profile-all + # make prefix=... PROFILE_USE=t install This will run the complete test suite as training workload and then rebuild git with the generated profile feedback. This results in a git
Makefile +23 -8
@@ -1772,6 +1772,24 @@ ifdef ASCIIDOC7 export ASCIIDOC7 endif +### profile feedback build +# + +# Can adjust this to be a global directory if you want to do extended +# data gathering +PROFILE_DIR := $(CURDIR) + +ifdef PROFILE_GEN + CFLAGS += -fprofile-generate=$(PROFILE_DIR) -DNO_NORETURN=1 + EXTLIBS += -lgcov + export CCACHE_DISABLE=t +endif + +ifdef PROFILE_USE + CFLAGS += -fprofile-use=$(PROFILE_DIR) -fprofile-correction -DNO_NORETURN=1 + export CCACHE_DISABLE=t +endif + # Shell quote (do not use $(call) to accommodate ancient setups); SHA1_HEADER_SQ = $(subst ','\'',$(SHA1_HEADER))
@@ -2701,14 +2719,11 @@ cover_db_html: cover_db # .PHONY: profile-all profile-clean -PROFILE_GEN_CFLAGS := $(CFLAGS) -fprofile-generate -DNO_NORETURN=1 -PROFILE_USE_CFLAGS := $(CFLAGS) -fprofile-use -fprofile-correction -DNO_NORETURN=1 - profile-clean: - $(RM) $(addsuffix *.gcda,$(object_dirs)) - $(RM) $(addsuffix *.gcno,$(object_dirs)) + $(RM) $(addsuffix *.gcda,$(addprefix $(PROFILE_DIR)/, $(object_dirs))) + $(RM) $(addsuffix *.gcno,$(addprefix $(PROFILE_DIR)/, $(object_dirs))) profile-all: profile-clean - $(MAKE) CFLAGS="$(PROFILE_GEN_CFLAGS)" all - $(MAKE) CFLAGS="$(PROFILE_GEN_CFLAGS)" -j1 test - $(MAKE) CFLAGS="$(PROFILE_USE_CFLAGS)" all + $(MAKE) PROFILE_GEN=t all + $(MAKE) PROFILE_GEN=t -j1 test + $(MAKE) PROFILE_USE=t all
Signature · 1 line
1.7.8.11.gefc1f.dirty
INSTALL set up install paths (via config.mak.autogen), so you can write instead · line 32
Quoted 12 lines · Theodore Ts'o
INSTALL +2
-2@@ -28,8 +28,8 @@ set up install paths (via config.mak.autogen), so you can write instead If you're willing to trade off (much) longer build time for a later faster git you can also do a profile feedback build with - $ make profile-all - # make prefix=... install + $ make prefix=... profile-all + # make prefix=... PROFILE_USE=t install
Thanks for a patch. How does this compare with what was discussed in the other thread?
http://thread.gmane.org/gmane.comp.version-control.git/188992/focus=189172
I would wish a solution ideally would support
make PROFILE_BUILD=YesPlease
make PROFILE_BUILD=YesPlease install
or even
echo >>config.mak PROFILE_BUILD
make
su make install
and I think your patch takes us in the right direction.
Quoted 5 lines · Junio C Hamano
Thanks for a patch. How does this compare with what was discussed in the other thread?
http://thread.gmane.org/gmane.comp.version-control.git/188992/focus=189172
I wasn't aware of this other approach when I created this patch (I must have missed the e-mail thread, sorry).
One of the reasons why I did it this way was for more flexibility. I wanted to be able to do:
$ make PROFILE_GEN=YesPlease PROFILE_DIR=/var/cache/FDO all # make PROFILE_GEN=YesPlease PROFILE_DIR=/var/cache/FDO install
run a bunch of git commands on various git repositories to get real-life usage...
Then do...
$ make PROFILE_USE=YesPlease PROFILE_DIR=/var/cache/FDO all # make PROFILE_GEN=YesPlease PROFILE_DIR=/var/cache/FDO install
But for many people they would probably be satisfied with something that builds git using a single magic recipe, even if they give up some fractional performance improvement (keep in mind that the feedback directed optimization seems to buy you only a single digit percentage improvement according according to Andi's original experiment; I just got interested in this more for amusement value than any thought that it would save me serious amounts of time).
Quoted 4 lines
I would wish a solution ideally would support
make PROFILE_BUILD=YesPlease
make PROFILE_BUILD=YesPlease install
At least in theory, it should be possible to have something which supports both PROFILE_GEN/PROFILE_USE as well as a combined PROFILE_BUILD.
The hard part is that PROFILE_BUILD requires a multi-pass process; you need to build with one set of CFLAGS, then run the sample workload to get the data for your feedback directed optimizations, and then re-run the build with another set of CFLAGS. I think what we could to check for PROFILE_BUILD, and if it is set, do the first PROFILE_GEN / make test commands as part of the top-level Makefile's all: rule, and then do the normal build after that.
It's a little kludgy, but does that sound acceptable to you?
- Ted
Quoted 4 lines · Ted Ts'o
Then do...
$ make PROFILE_USE=YesPlease PROFILE_DIR=/var/cache/FDO all # make PROFILE_GEN=YesPlease PROFILE_DIR=/var/cache/FDO install
Err, that last line should have been:
# make PROFILE_USE=YesPlease PROFILE_DIR=/var/cache/FDO install
of course...
- Ted
Quoted 9 lines · Ted Ts'o
...
At least in theory, it should be possible to have something which supports both PROFILE_GEN/PROFILE_USE as well as a combinedPROFILE_BUILD.The hard part is that
PROFILE_BUILDrequires a multi-pass process; you need to build with one set of CFLAGS, then run the sample workload to get the data for your feedback directed optimizations, and then re-run the build with another set of CFLAGS.
Yeah, I can see how that forces us to some kludgy solution, but I tend to agree that the separation between GEN/USE is a good thing.
Quoted 4 lines
I think what we could to check
forPROFILE_BUILD, and if it is set, do the firstPROFILE_GEN/ make test commands as part of the top-level Makefile's all: rule, and then do the normal build after that.
Yeah, something like that would emulate the "make profile-all" well enough for people not to notice the change while giving us the flexibility of GEN/USE separation. I kinda like it.
Thanks.
What do you think of this? I'm still running a test build --- "make PROFILE=BUILD all" takes quite a long time, so this is still an RFC; I figure there will still be some places where people will point out more nits to be polished. :-)
(In particular, I just noticed I left the V=1 for debugging purposes in this version....)
- Ted
Quoted 1 line
From
4bf14e732216fd1327da2e3c8c6dfc0a3f689e1bMon Sep 17 00:00:00 2001
From: Theodore Ts'o <[email protected]>
Date: Thu, 2 Feb 2012 13:56:22 -0500
Subject: [PATCH] Fix build problems related to profile-directed optimization
There was a number of problems I ran into when trying the profile-directed optimizations added by Andi Kleen in git commit 7ddc2710b9. (This was using gcc 4.4 found on many enterprise distros.)
1) The -fprofile-generate and -fprofile-use commands are incompatible with ccache; the code ends up looking in the wrong place for the gcda files based on the ccache object names.
2) If the makefile notices that CFLAGS are different, it will rebuild all of the binaries. Hence the recipe originally specified by the INSTALL file ("make profile-all" followed by "make install") doesn't work. It will appear to work, but the binaries will end up getting built with no optimization.
This patch fixes this by using an explicit set of options passed via the PROFILE variable then using this to directly manipulate CFLAGS and EXTLIBS.
The developer can run "make PROFILE=BUILD all ; make PROFILE=BUILD install" to do an automatic two-pass build using the test suite as the sample workload for the purpose of profiling.
Alternatively, the profiling version of binaries can be built using:
make PROFILE=GEN PROFILE_DIR=/var/cache/profile all make PROFILE=GEN install
and then after git has been used a number of times, the optimized version of the binary can be built as follows:
make PROFILE=USE PROFILE_DIR=/var/cache/profile all make PROFILE=USE install
--- INSTALL | 4 ++-- Makefile | 41 ++++++++++++++++++++++++++++++----------- 2 files changed, 32 insertions(+), 13 deletions(-)
INSTALL +2 -2
@@ -28,8 +28,8 @@ set up install paths (via config.mak.autogen), so you can write instead If you're willing to trade off (much) longer build time for a later faster git you can also do a profile feedback build with - $ make profile-all - # make prefix=... install + $ make --prefix=/usr PROFILE=BUILD all + # make --prefix=/usr PROFILE=BUILD install This will run the complete test suite as training workload and then rebuild git with the generated profile feedback. This results in a git
Makefile +30 -11
59 lines not on this page · raw message
Signature · 1 line
1.7.8.11.gefc1f.dirty
Quoted 7 lines · Ted Ts'o
What do you think of this? I'm still running a test build --- "make PROFILE=BUILD all" takes quite a long time, so this is still an RFC; I figure there will still be some places where people will point out more nits to be polished. :-)
(In particular, I just noticed I left the V=1 for debugging purposes in this version....)
Thanks.
Three comments:
* I am happy that this version handles this well:
$ make PROFILE=BUILD install
even though you did not advertise as such in INSTALL ;-).
* However, I think "clean" target should remove *.gcda unconditionally.
$ make PROFILE=BUILD install ; make clean ; git clean -n -x | grep gcda
* Running "make PROFILE=BUILD install" immediately after another one, without "make clean" in between, resulted in full rebuild and test before the second "install", which somewhat surprised me. I however do not think this is a big show-stopper problem.
Thanks everyone for improving this.
I should add that any improvements will depend on your compiler version. I would also expect better numbers when combined with LTO in gcc 4.7, but haven't tried so far.
BTW it would be really nice to figure out a subset of the test suite that runs faster and gives similar speedup like the full one.
-Andi
Signature · 1 line
[email protected] -- Speaking for myself only
Quoted 6 lines · Junio C Hamano
* I am happy that this version handles this well:
$ make PROFILE=BUILD installeven though you did not advertise as such in INSTALL ;-).
I can mention it, although it will mean adding more verbiage about profile-directed optimization into the INSTALL.
My assumption was that people who did this would usually be installing into --prefix=/usr as root, but there certainly will be anal people like myself who want to install profile-optimized binaries into ~/bin. :-)
Quoted 3 lines
* However, I think "clean" target should remove *.gcda unconditionally.
$ make PROFILE=BUILD install ; make clean ; git clean -n -x | grep gcda
Will fix.
Quoted 4 lines
* Running "make PROFILE=BUILD install" immediately after another one, without "make clean" in between, resulted in full rebuild and test before the second "install", which somewhat surprised me. I however do not think this is a big show-stopper problem.
Hmm… that surprises me too. If
make PROFILE=BUILD all
make PROFILE=BUILD install
works correctly, I don't understand why a second "make PROFILE=BUILD install" issued after the above sequence would result in complete rebuild and test pass, unless something in the "make install" rules is modifying the build tree as a side-effect of the install pass, which I'd argue is a bug. I'll take a look at it.
-- Ted
Quoted 11 lines · Theodore Tso
Quoted 6 lines · Junio C Hamano
* I am happy that this version handles this well:
$ make PROFILE=BUILD installeven though you did not advertise as such in INSTALL ;-).
I can mention it, although it will mean adding more verbiage about profile-directed optimization into the INSTALL.
Oh, sorry, I didn't mean it that way. Please read it as: "Something that is a natural thing for people to expect after reading what is in INSTALL works correctly. Yay! Thanks."
PATCH 1/1 Fix build problems related to profile-directed optimization
There was a number of problems I ran into when trying the profile-directed optimizations added by Andi Kleen in git commit 7ddc2710b9. (This was using gcc 4.4 found on many enterprise distros.)
1) The -fprofile-generate and -fprofile-use commands are incompatible with ccache; the code ends up looking in the wrong place for the gcda files based on the ccache object names.
2) If the makefile notices that CFLAGS are different, it will rebuild all of the binaries. Hence the recipe originally specified by the INSTALL file ("make profile-all" followed by "make install") doesn't work. It will appear to work, but the binaries will end up getting built with no optimization.
This patch fixes this by using an explicit set of options passed via the PROFILE variable then using this to directly manipulate CFLAGS and EXTLIBS.
The developer can run "make PROFILE=BUILD all ; sudo make PROFILE=BUILD install" automatically run a two-pass build with the test suite run in between as the sample workload for the purpose of recording profiling information to do the profile-directed optimization.
Alternatively, the profiling version of binaries can be built using:
make PROFILE=GEN PROFILE_DIR=/var/cache/profile all make PROFILE=GEN install
and then after git has been used for a while, the optimized version of the binary can be built as follows:
make PROFILE=USE PROFILE_DIR=/var/cache/profile all make PROFILE=USE install
--- INSTALL | 17 +++++++++++++---- Makefile | 53 +++++++++++++++++++++++++++++++++++------------------ 2 files changed, 48 insertions(+), 22 deletions(-)
INSTALL +13 -4
@@ -28,16 +28,25 @@ set up install paths (via config.mak.autogen), so you can write instead If you're willing to trade off (much) longer build time for a later faster git you can also do a profile feedback build with - $ make profile-all - # make prefix=... install + $ make --prefix=/usr PROFILE=BUILD all + # make --prefix=/usr PROFILE=BUILD install This will run the complete test suite as training workload and then rebuild git with the generated profile feedback. This results in a git which is a few percent faster on CPU intensive workloads. This may be a good tradeoff for distribution packagers. -Note that the profile feedback build stage currently generates -a lot of additional compiler warnings. +Or if you just want to install a profile-optimized version of git into +your home directory, you could run: + + $ make PROFILE=BUILD install + +As a caveat: a profile-optimized build takes a *lot* longer since it +is the sources have to be built twice, and in order for the profiling +measurements to work properly, ccache must be disabled and the test +suite has to be run using only a single CPU. In addition, the profile +feedback build stage currently generates a lot of additional compiler +warnings. Issues of note:
Makefile +35 -18
80 lines not on this page · raw message
Signature · 1 line
1.7.9.107.g8e04a
INSTALL set up install paths (via config.mak.autogen), so you can write instead · line 32
Quoted 12 lines · Theodore Ts'o
INSTALL +2
-2@@ -28,16 +28,25 @@ set up install paths (via config.mak.autogen), so you can write instead If you're willing to trade off (much) longer build time for a later faster git you can also do a profile feedback build with - $ make profile-all - # make prefix=... install + $ make --prefix=/usr PROFILE=BUILD all + # make --prefix=/usr PROFILE=BUILD install
Eh? --prefix?
INSTALL set up install paths (via config.mak.autogen), so you can write instead · line 45
Quoted 2 lines
INSTALL +2
-0+As a caveat: a profile-optimized build takes a *lot* longer since it +is the sources have to be built twice, and in order for the profiling
s/it is//
Quoted 6 lines
Makefile +0
-0@@ -1772,6 +1772,24 @@ ifdef ASCIIDOC7
Makefile ifdef ASCIIDOC7 · line 1791
Quoted 10 lines
Makefile +10
-0+ifeq "$(PROFILE)" "GEN" + CFLAGS += -fprofile-generate=$(PROFILE_DIR) -DNO_NORETURN=1 + EXTLIBS += -lgcov + export CCACHE_DISABLE=t + V=1 +else ifneq "$PROFILE" "" + CFLAGS += -fprofile-use=$(PROFILE_DIR) -fprofile-correction -DNO_NORETURN=1 + export CCACHE_DISABLE=t + V=1 +endif
Did you mean "$(PROFILE)" in the second conditional?
-Peff
Quoted 6 lines · Jeff King
INSTALL set up install paths (via config.mak.autogen), so you can write instead · line 32
Quoted 4 lines
INSTALL +2
-2- $ make profile-all - # make prefix=... install + $ make --prefix=/usr PROFILE=BUILD all + # make --prefix=/usr PROFILE=BUILD installEh? --prefix?
Oops, configure meme strikes; will fix.
Quoted 4 lines
INSTALL set up install paths (via config.mak.autogen), so you can write instead · line 45
Quoted 2 lines
INSTALL +2
-0+As a caveat: a profile-optimized build takes a *lot* longer since it +is the sources have to be built twice, and in order for the profilings/it is//
Thanks, will fix.
Quoted 8 lines
Makefile ifdef ASCIIDOC7 · line 1787
Quoted 6 lines
Makefile +6
-0+ifeq "$(PROFILE)" "GEN" + CFLAGS += -fprofile-generate=$(PROFILE_DIR) -DNO_NORETURN=1 + EXTLIBS += -lgcov + export CCACHE_DISABLE=t + V=1 +else ifneq "$PROFILE" ""Did you mean "$(PROFILE)" in the second conditional?
Yes, thanks. Will fix.
- Ted
PATCH 1/1 Fix build problems related to profile-directed optimization
There was a number of problems I ran into when trying the profile-directed optimizations added by Andi Kleen in git commit 7ddc2710b9. (This was using gcc 4.4 found on many enterprise distros.)
1) The -fprofile-generate and -fprofile-use commands are incompatible with ccache; the code ends up looking in the wrong place for the gcda files based on the ccache object names.
2) If the makefile notices that CFLAGS are different, it will rebuild all of the binaries. Hence the recipe originally specified by the INSTALL file ("make profile-all" followed by "make install") doesn't work. It will appear to work, but the binaries will end up getting built with no optimization.
This patch fixes this by using an explicit set of options passed via the PROFILE variable then using this to directly manipulate CFLAGS and EXTLIBS.
The developer can run "make PROFILE=BUILD all ; sudo make PROFILE=BUILD install" automatically run a two-pass build with the test suite run in between as the sample workload for the purpose of recording profiling information to do the profile-directed optimization.
Alternatively, the profiling version of binaries can be built using:
make PROFILE=GEN PROFILE_DIR=/var/cache/profile all make PROFILE=GEN install
and then after git has been used for a while, the optimized version of the binary can be built as follows:
make PROFILE=USE PROFILE_DIR=/var/cache/profile all make PROFILE=USE install
--- INSTALL | 17 +++++++++++++---- Makefile | 53 +++++++++++++++++++++++++++++++++++------------------ 2 files changed, 48 insertions(+), 22 deletions(-)
INSTALL +13 -4
@@ -28,16 +28,25 @@ set up install paths (via config.mak.autogen), so you can write instead If you're willing to trade off (much) longer build time for a later faster git you can also do a profile feedback build with - $ make profile-all - # make prefix=... install + $ make prefix=/usr PROFILE=BUILD all + # make prefix=/usr PROFILE=BUILD install This will run the complete test suite as training workload and then rebuild git with the generated profile feedback. This results in a git which is a few percent faster on CPU intensive workloads. This may be a good tradeoff for distribution packagers. -Note that the profile feedback build stage currently generates -a lot of additional compiler warnings. +Or if you just want to install a profile-optimized version of git into +your home directory, you could run: + + $ make PROFILE=BUILD install + +As a caveat: a profile-optimized build takes a *lot* longer since the +git tree must be built twice, and in order for the profiling +measurements to work properly, ccache must be disabled and the test +suite has to be run using only a single CPU. In addition, the profile +feedback build stage currently generates a lot of additional compiler +warnings. Issues of note:
Makefile +35 -18
80 lines not on this page · raw message
Signature · 1 line
1.7.9.107.g8e04a
Junio, any comments on my most recent spin of this patch? Any changes you'd like to see?
Thanks,
- Ted
Am 2/8/2012 19:53, schrieb Ted Ts'o:
Quoted 2 lines
Junio, any comments on my most recent spin of this patch? Any changes you'd like to see?
I need the following to unbreak my build on Windows.
--- >8 ---
From: Johannes Sixt <[email protected]>
Subject: [PATCH] Makefile: fix syntax for older make
It is necessary to write the else branch as a nested conditional. Also, write the conditions with parentheses because we use them throughout the Makefile.
--- Makefile | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
Makefile +4 -2
@@ -1784,16 +1784,18 @@ endif # data gathering PROFILE_DIR := $(CURDIR) -ifeq "$(PROFILE)" "GEN" +ifeq ("$(PROFILE)","GEN") CFLAGS += -fprofile-generate=$(PROFILE_DIR) -DNO_NORETURN=1 EXTLIBS += -lgcov export CCACHE_DISABLE=t V=1 -else ifneq "$(PROFILE)" "" +else +ifneq ("$(PROFILE)","") CFLAGS += -fprofile-use=$(PROFILE_DIR) -fprofile-correction -DNO_NORETURN=1 export CCACHE_DISABLE=t V=1 endif +endif # Shell quote (do not use $(call) to accommodate ancient setups);
Signature · 1 line
1.7.9.1420.gae2d6
Quoted 5 lines · Johannes Sixt
Am 2/8/2012 19:53, schrieb Ted Ts'o:
Quoted 2 lines
Junio, any comments on my most recent spin of this patch? Any changes you'd like to see?
I need the following to unbreak my build on Windows.
Thanks; will apply.
Quoted 37 lines
--- >8 ---
From: Johannes Sixt <[email protected]>
Subject: [PATCH]Makefile: fix syntax for older makeIt is necessary to write the else branch as a nested conditional. Also, write the conditions with parentheses because we use them throughout the
Makefile.Signed-off-by Johannes Sixt <[email protected]>--- Makefile | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)Makefile +4
-2@@ -1784,16 +1784,18 @@ endif # data gathering PROFILE_DIR := $(CURDIR) -ifeq "$(PROFILE)" "GEN" +ifeq ("$(PROFILE)","GEN") CFLAGS += -fprofile-generate=$(PROFILE_DIR) -DNO_NORETURN=1 EXTLIBS += -lgcov export CCACHE_DISABLE=t V=1 -else ifneq "$(PROFILE)" "" +else +ifneq ("$(PROFILE)","") CFLAGS += -fprofile-use=$(PROFILE_DIR) -fprofile-correction -DNO_NORETURN=1 export CCACHE_DISABLE=t V=1 endif +endif # Shell quote (do not use $(call) to accommodate ancient setups);
Quoted 2 lines · Ted Ts'o
Junio, any comments on my most recent spin of this patch? Any changes you'd like to see?
Nothing from me; all looked good.
Let's cook it in 'next' for a few days to give developers a chance to play with it and merge down to 'master'.
Thanks.