Skip to contentSkip to conversation

python3: enable profile optimized builds

PATCH In review Anuj Mittal 5 participants 8 messages Aug 16, 2018 – Aug 23, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 python3: enable profile optimized builds

Enable profile guided optimization (pgo) for python3. Enabling pgo in python is generally as simple as invoking the target profile-opt which:

- builds python binaries with profile instrumentation enabled,
- runs a specific profile task using that python to get the profile data and,
- feeds the compiler with this profile data and rebuilds python.

This change invokes qemu-user for the second step of running a profile task using target python. Depending on how long profile task takes to run, this might add a significant time to compilation (which would be true for native builds too). The default profile task can be changed by the users depending on what makes sense for their use case (or can be left empty). In case qemu-user isn't supported, profile task won't be run.

---
 ...d-target-to-split-profile-generation.patch | 40 +++++++++++++
 ...GS-for-extensions-when-cross-compili.patch | 56 +++++++++++++++++++
 meta/recipes-devtools/python/python3_3.5.5.bb | 55 +++++++++++-------
 3 files changed, 130 insertions(+), 21 deletions(-)
 create mode 100644 meta/recipes-devtools/python/python3/0002-Makefile-add-target-to-split-profile-generation.patch
 create mode 100644 meta/recipes-devtools/python/python3/Use-correct-CFLAGS-for-extensions-when-cross-compili.patch
meta/recipes-devtools/python/python3/0002-Makefile-add-target-to-split-profile-generation.patch new file +40 -0
new file mode 100644
index 0000000000..bb01c5bcb4
@@ -0,0 +1,40 @@
+From 98586d6dc598e40b8b821b0dde57599e188a7ca4 Mon Sep 17 00:00:00 2001
+From: Anuj Mittal <[email protected]>
+Date: Tue, 7 Aug 2018 16:43:17 +0800
+Subject: [PATCH 2/2] Makefile: add target to split profile generation
+
+We don't want to have profile task invoked from here and want to use
+qemu-user instead. Split the profile-opt task so qemu can be invoked
+once binaries have been built with instrumentation and then we can go
+ahead and build again using the profile data generated.
+
+Upstream-Status: Inappropriate [OE-specific]
+
+Signed-off-by: Anuj Mittal <[email protected]>
+---
+ Makefile.pre.in | 6 ++----
+ 1 file changed, 2 insertions(+), 4 deletions(-)
+
+diff --git a/Makefile.pre.in b/Makefile.pre.in
+index 84bc3ff..017a2c4 100644
+--- a/Makefile.pre.in
++++ b/Makefile.pre.in
+@@ -469,13 +469,11 @@ profile-opt:
+ 	$(MAKE) profile-removal
+ 	$(MAKE) build_all_generate_profile
+ 	$(MAKE) profile-removal
+-	@echo "Running code to generate profile data (this can take a while):"
+-	$(MAKE) run_profile_task
+-	$(MAKE) build_all_merge_profile
++
++clean_and_use_profile:
+ 	@echo "Rebuilding with profile guided optimizations:"
+ 	$(MAKE) clean
+ 	$(MAKE) build_all_use_profile
+-	$(MAKE) profile-removal
+ 
+ build_all_generate_profile:
+ 	$(MAKE) @DEF_MAKE_RULE@ CFLAGS_NODIST="$(CFLAGS) $(EXTRA_CFLAGS) $(PGO_PROF_GEN_FLAG) @LTOFLAGS@" LDFLAGS="$(LDFLAGS) $(PGO_PROF_GEN_FLAG) @LTOFLAGS@" LIBS="$(LIBS)"
+-- 
+2.17.1
+
meta/recipes-devtools/python/python3/Use-correct-CFLAGS-for-extensions-when-cross-compili.patch new file +56 -0

56 lines not on this page · raw message

meta/recipes-devtools/python/python3_3.5.5.bb +34 -21

97 lines not on this page · raw message

Signature · 1 line
2.17.1
#1 lore raw
Quoted 8 lines · Anuj Mittal

Enable profile guided optimization (pgo) for python3. Enabling pgo in python is generally as simple as invoking the target profile-opt which:

- builds python binaries with profile instrumentation enabled,
- runs a specific profile task using that python to get the profile data and,
- feeds the compiler with this profile data and rebuilds python.

Quoted 6 lines

This change invokes qemu-user for the second step of running a profile task using target python. Depending on how long profile task takes to run, this might add a significant time to compilation (which would be true for native builds too). The default profile task can be changed by the users depending on what makes sense for their use case (or can be left empty). In case qemu-user isn't supported, profile task won't be run.

Is it important to re-create the profile data during every build or would we get most of the same benefits from using reference data which is generated offline? How big is the data file? Is it binary or text? Is the data expected to be target architecture specific?

If reference data were used, what are the consequences of the data not corresponding exactly to the current build configuration? A build failure? Or just a decrease in the effectiveness of the optimisation?

Does the profiling instrumentation measure execution timing? Or only the frequency / order in which functions are called? ie is there any concern that data generated from running under qemu may not be optimal for running on the target?

#2 lore raw
Quoted 16 lines · Andre McCurdy
Quoted 8 lines · Anuj Mittal

Enable profile guided optimization (pgo) for python3. Enabling pgo in python is generally as simple as invoking the target profile-opt which:

- builds python binaries with profile instrumentation enabled,
- runs a specific profile task using that python to get the profile data and,
- feeds the compiler with this profile data and rebuilds python.

Quoted 6 lines

This change invokes qemu-user for the second step of running a profile task using target python. Depending on how long profile task takes to run, this might add a significant time to compilation (which would be true for native builds too). The default profile task can be changed by the users depending on what makes sense for their use case (or can be left empty). In case qemu-user isn't supported, profile task won't be run.

Quoted 3 lines

Is it important to re-create the profile data during every build or would we get most of the same benefits from using reference data which is generated offline?

We should get the same benefit using the data generated offline as long as the source code/configure options/flags are same I believe. I have only tried with data generate offline using the same build configuration though.

It would however need tweaking of the Makefiles to pass -fprofile-dir=<path> while using the profile data among other things. Please see this if you'd like an example that works:

https://git.yoctoproject.org/clean/cgit.cgi/poky-contrib/commit/?h=anujm/9338&id=e57654cb51b121e9dfa76e66432c4d37fd339d42

Quoted 1 line

How big is the data file? Is it binary or text?

gcc -fprofile-generate generates .gcda files which are used only for profile use and can be deleted later and aren't installed. For more information:

https://gcc.gnu.org/onlinedocs/gcc/Gcov-Data-Files.html

Quoted 4 lines

Is the data expected to be target architecture specific?> If reference data were used, what are the consequences of the data not corresponding exactly to the current build configuration? A build failure? Or just a decrease in the effectiveness of the optimisation?

From gcc man page:

"By default, GCC emits an error message if the feedback profiles do not match the source code. This error can be turned into a warning by using -Wcoverage-mismatch. Note this may result in poorly optimized code."

Quoted 5 lines

Does the profiling instrumentation measure execution timing? Or only the frequency / order in which functions are called? ie is there any concern that data generated from running under qemu may not be optimal for running on the target?

It tries to identify code hot spots, how many times each branch and call is executed and how many times it is taken or returns etc. so I don't think it should matter. I did try on target hardware and using qemu as well and at least performance wise, I didn't see any difference. I didn't perform any exhaustive analysis though.

#3 lore raw
Quoted 17 lines · Anuj Mittal
Quoted 16 lines · Andre McCurdy
Quoted 14 lines · Anuj Mittal

Enable profile guided optimization (pgo) for python3. Enabling pgo in python is generally as simple as invoking the target profile-opt which:

- builds python binaries with profile instrumentation enabled,
- runs a specific profile task using that python to get the profile data and,
- feeds the compiler with this profile data and rebuilds python.

This change invokes qemu-user for the second step of running a profile task using target python. Depending on how long profile task takes to run, this might add a significant time to compilation (which would be true for native builds too). The default profile task can be changed by the users depending on what makes sense for their use case (or can be left empty). In case qemu-user isn't supported, profile task won't be run.

Quoted 8 lines
Quoted 3 lines

Is it important to re-create the profile data during every build or would we get most of the same benefits from using reference data which is generated offline?

We should get the same benefit using the data generated offline as long as the source code/configure options/flags are same I believe. I have only tried with data generate offline using the same build configuration though.

As an additional data point, python do_compile now takes approx 18 minutes on my laptop. A single qemu-i386 thread loading one CPU core at 100%, with nothing else being scheduled in parallel for most of that time.

If we can get most of the benefits of pgo with a pre-generated data file then that might still be something to explore.

Quoted 37 lines

It would however need tweaking of the Makefiles to pass -fprofile-dir=<path> while using the profile data among other things. Please see this if you'd like an example that works:

https://git.yoctoproject.org/clean/cgit.cgi/poky-contrib/commit/?h=anujm/9338&id=e57654cb51b121e9dfa76e66432c4d37fd339d42

Quoted 1 line

How big is the data file? Is it binary or text?

gcc -fprofile-generate generates .gcda files which are used only for profile use and can be deleted later and aren't installed. For more information:

https://gcc.gnu.org/onlinedocs/gcc/Gcov-Data-Files.html

Quoted 4 lines

Is the data expected to be target architecture specific?> If reference data were used, what are the consequences of the data not corresponding exactly to the current build configuration? A build failure? Or just a decrease in the effectiveness of the optimisation?

From gcc man page:

"By default, GCC emits an error message if the feedback profiles do not match the source code. This error can be turned into a warning by using -Wcoverage-mismatch. Note this may result in poorly optimized code."

Quoted 5 lines

Does the profiling instrumentation measure execution timing? Or only the frequency / order in which functions are called? ie is there any concern that data generated from running under qemu may not be optimal for running on the target?

It tries to identify code hot spots, how many times each branch and call is executed and how many times it is taken or returns etc. so I don't think it should matter. I did try on target hardware and using qemu as well and at least performance wise, I didn't see any difference. I didn't perform any exhaustive analysis though.

#4 lore raw
Quoted 19 lines · Andre McCurdy
Quoted 9 lines · Anuj Mittal
Quoted 7 lines · Andre McCurdy

On Wed, Aug 15, 2018 at 11:26 PM, Anuj Mittal <anuj.mittal@intel. com> wrote:
Is it important to re-create the profile data during every build or
would we get most of the same benefits from using reference data which
is generated offline?

Quoted 7 lines

We should get the same benefit using the data generated offline as long
as the source code/configure options/flags are same I believe. I have
only tried with data generate offline using the same build configuration
though.

Quoted 7 lines

As an additional data point, python do_compile now takes approx 18 minutes on my laptop. A single qemu-i386 thread loading one CPU core at 100%, with nothing else being scheduled in parallel for most of that time.

If we can get most of the benefits of pgo with a pre-generated data file then that might still be something to explore.

I've also noticed the increased build times. I'd love to see if we could create an sstate object which would contain the pgo data and which didn't depend on the usual compiler/toolchain pieces, only the do_patch stage checksum. In theory, the same sstate object should be reusable between target/native/nativesdk too as long as we had the same python patchset (which I know we don't for native).

Anyone fancy such a challenge? We should probably have a bug open for this at least...

Cheers,

Richard

#5 lore raw

IMO PGO should really stay opt-in, not opt-out, given the performance impact.

Quoted 48 lines · Richard Purdie
Quoted 19 lines · Andre McCurdy
Quoted 9 lines · Anuj Mittal
Quoted 7 lines · Andre McCurdy

On Wed, Aug 15, 2018 at 11:26 PM, Anuj Mittal <anuj.mittal@intel. com> wrote:
Is it important to re-create the profile data during every build or
would we get most of the same benefits from using reference data which
is generated offline?

Quoted 7 lines

We should get the same benefit using the data generated offline as long
as the source code/configure options/flags are same I believe. I have
only tried with data generate offline using the same build configuration
though.

Quoted 7 lines

As an additional data point, python do_compile now takes approx 18 minutes on my laptop. A single qemu-i386 thread loading one CPU core at 100%, with nothing else being scheduled in parallel for most of that time.

If we can get most of the benefits of pgo with a pre-generated data file then that might still be something to explore.

I've also noticed the increased build times. I'd love to see if we could create an sstate object which would contain the pgo data and which didn't depend on the usual compiler/toolchain pieces, only the do_patch stage checksum. In theory, the same sstate object should be reusable between target/native/nativesdk too as long as we had the same python patchset (which I know we don't for native).

Anyone fancy such a challenge? We should probably have a bug open for this at least...

Cheers,

Richard

--
_______________________________________________
Openembedded-core mailing list
[email protected]
http://lists.openembedded.org/mailman/listinfo/openembedded-core

Signature · 4 lines
Christopher Larson
kergoth at gmail dot com
Founder - BitBake, OpenEmbedded, OpenZaurus
Senior Software Engineer, Mentor Graphics
#6 lore raw

+1 for this being opt in. It's a substantial hit to build time

Quoted 67 lines · Christopher Larson

IMO PGO should really stay opt-in, not opt-out, given the performance impact.

Quoted 40 lines · Richard Purdie
Quoted 19 lines · Andre McCurdy
Quoted 9 lines · Anuj Mittal
Quoted 7 lines · Andre McCurdy

On Wed, Aug 15, 2018 at 11:26 PM, Anuj Mittal <anuj.mittal@intel. com> wrote:
Is it important to re-create the profile data during every build or
would we get most of the same benefits from using reference data which
is generated offline?

Quoted 7 lines

We should get the same benefit using the data generated offline as long
as the source code/configure options/flags are same I believe. I have
only tried with data generate offline using the same build configuration
though.

Quoted 7 lines

As an additional data point, python do_compile now takes approx 18 minutes on my laptop. A single qemu-i386 thread loading one CPU core at 100%, with nothing else being scheduled in parallel for most of that time.

If we can get most of the benefits of pgo with a pre-generated data file then that might still be something to explore.

I've also noticed the increased build times. I'd love to see if we could create an sstate object which would contain the pgo data and which didn't depend on the usual compiler/toolchain pieces, only the do_patch stage checksum. In theory, the same sstate object should be reusable between target/native/nativesdk too as long as we had the same python patchset (which I know we don't for native).

Anyone fancy such a challenge? We should probably have a bug open for this at least...

Cheers,

Quoted 8 lines

Richard

--
_______________________________________________
Openembedded-core mailing list
[email protected]
http://lists.openembedded.org/mailman/listinfo/openembedded-core

--
Christopher Larson
kergoth at gmail dot com
Founder - BitBake, OpenEmbedded, OpenZaurus
Senior Software Engineer, Mentor Graphics
--
_______________________________________________
Openembedded-core mailing list
[email protected]
http://lists.openembedded.org/mailman/listinfo/openembedded-core

#7 lore raw
Quoted 6 lines · Derek Straka

+1 for this being opt in. It's a substantial hit to build time

Quoted 2 lines · Christopher Larson

IMO PGO should really stay opt-in, not opt-out, given the performance impact.

I'm not arguing against that however I'd note that the performance of the resulting python binary is substantially different too, much more that you'd think.

For older distros where we use buildtools tarball on the builders, pgo python binaries should help significantly with things like bitbake parsing speed (I'd love to see numbers).

Cheers,

Richard