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 Signed-off-by: "Theodore Ts'o" <tytso@mit.edu> Cc: Andi Kleen <ak@linux.intel.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Theodore Ts'o committed Feb 6, 2012 at 01:00 UTC f2d713fc3e8e0b7be89584f04b421808aa99c403
2 files changed +48 -22
INSTALL
+13 -4
@@ -28,16 +28,25 @@ set up install paths (via config.mak.autogen), so you can write instead
28 If you're willing to trade off (much) longer build time for a later
29 faster git you can also do a profile feedback build with
30
31 - $ make profile-all
32 - # make prefix=... install
31 + $ make prefix=/usr PROFILE=BUILD all
32 + # make prefix=/usr PROFILE=BUILD install
33
34 This will run the complete test suite as training workload and then
35 rebuild git with the generated profile feedback. This results in a git
36 which is a few percent faster on CPU intensive workloads. This
37 may be a good tradeoff for distribution packagers.
38
39 -Note that the profile feedback build stage currently generates
40 -a lot of additional compiler warnings.
39 +Or if you just want to install a profile-optimized version of git into
40 +your home directory, you could run:
41 +
42 + $ make PROFILE=BUILD install
43 +
44 +As a caveat: a profile-optimized build takes a *lot* longer since the
45 +git tree must be built twice, and in order for the profiling
46 +measurements to work properly, ccache must be disabled and the test
47 +suite has to be run using only a single CPU. In addition, the profile
48 +feedback build stage currently generates a lot of additional compiler
49 +warnings.
50
51 Issues of note:
52
Makefile
+35 -18
@@ -1768,6 +1768,24 @@ ifdef ASCIIDOC7
1768 export ASCIIDOC7
1769 endif
1770
1771 +### profile feedback build
1772 +#
1773 +
1774 +# Can adjust this to be a global directory if you want to do extended
1775 +# data gathering
1776 +PROFILE_DIR := $(CURDIR)
1777 +
1778 +ifeq "$(PROFILE)" "GEN"
1779 + CFLAGS += -fprofile-generate=$(PROFILE_DIR) -DNO_NORETURN=1
1780 + EXTLIBS += -lgcov
1781 + export CCACHE_DISABLE=t
1782 + V=1
1783 +else ifneq "$(PROFILE)" ""
1784 + CFLAGS += -fprofile-use=$(PROFILE_DIR) -fprofile-correction -DNO_NORETURN=1
1785 + export CCACHE_DISABLE=t
1786 + V=1
1787 +endif
1788 +
1789 # Shell quote (do not use $(call) to accommodate ancient setups);
1790
1791 SHA1_HEADER_SQ = $(subst ','\'',$(SHA1_HEADER))
@@ -1824,7 +1842,17 @@ export DIFF TAR INSTALL DESTDIR SHELL_PATH
1842
1843 SHELL = $(SHELL_PATH)
1844
1827 -all:: shell_compatibility_test $(ALL_PROGRAMS) $(SCRIPT_LIB) $(BUILT_INS) $(OTHER_PROGRAMS) GIT-BUILD-OPTIONS
1845 +all:: shell_compatibility_test
1846 +
1847 +ifeq "$(PROFILE)" "BUILD"
1848 +ifeq ($(filter all,$(MAKECMDGOALS)),all)
1849 +all:: profile-clean
1850 + $(MAKE) PROFILE=GEN all
1851 + $(MAKE) PROFILE=GEN -j1 test
1852 +endif
1853 +endif
1854 +
1855 +all:: $(ALL_PROGRAMS) $(SCRIPT_LIB) $(BUILT_INS) $(OTHER_PROGRAMS) GIT-BUILD-OPTIONS
1856 ifneq (,$X)
1857 $(QUIET_BUILT_IN)$(foreach p,$(patsubst %$X,%,$(filter %$X,$(ALL_PROGRAMS) $(BUILT_INS) git$X)), test -d '$p' -o '$p' -ef '$p$X' || $(RM) '$p';)
1858 endif
@@ -2552,7 +2580,11 @@ distclean: clean
2580 $(RM) configure
2581 $(RM) po/git.pot
2582
2555 -clean:
2583 +profile-clean:
2584 + $(RM) $(addsuffix *.gcda,$(addprefix $(PROFILE_DIR)/, $(object_dirs)))
2585 + $(RM) $(addsuffix *.gcno,$(addprefix $(PROFILE_DIR)/, $(object_dirs)))
2586 +
2587 +clean: profile-clean
2588 $(RM) *.o block-sha1/*.o ppc/*.o compat/*.o compat/*/*.o xdiff/*.o vcs-svn/*.o \
2589 builtin/*.o $(LIB_FILE) $(XDIFF_LIB) $(VCSSVN_LIB)
2590 $(RM) $(ALL_PROGRAMS) $(SCRIPT_LIB) $(BUILT_INS) git$X
@@ -2582,7 +2614,7 @@ ifndef NO_TCLTK
2614 endif
2615 $(RM) GIT-VERSION-FILE GIT-CFLAGS GIT-LDFLAGS GIT-GUI-VARS GIT-BUILD-OPTIONS
2616
2585 -.PHONY: all install clean strip
2617 +.PHONY: all install profile-clean clean strip
2618 .PHONY: shell_compatibility_test please_set_SHELL_PATH_to_a_more_modern_shell
2619 .PHONY: FORCE cscope
2620
@@ -2692,18 +2724,3 @@ cover_db: coverage-report
2724 cover_db_html: cover_db
2725 cover -report html -outputdir cover_db_html cover_db
2726
2695 -### profile feedback build
2696 -#
2697 -.PHONY: profile-all profile-clean
2698 -
2699 -PROFILE_GEN_CFLAGS := $(CFLAGS) -fprofile-generate -DNO_NORETURN=1
2700 -PROFILE_USE_CFLAGS := $(CFLAGS) -fprofile-use -fprofile-correction -DNO_NORETURN=1
2701 -
2702 -profile-clean:
2703 - $(RM) $(addsuffix *.gcda,$(object_dirs))
2704 - $(RM) $(addsuffix *.gcno,$(object_dirs))
2705 -
2706 -profile-all: profile-clean
2707 - $(MAKE) CFLAGS="$(PROFILE_GEN_CFLAGS)" all
2708 - $(MAKE) CFLAGS="$(PROFILE_GEN_CFLAGS)" -j1 test
2709 - $(MAKE) CFLAGS="$(PROFILE_USE_CFLAGS)" all