From 412a9c9f6719a6c62e8f5b6265714428d2ad4013 Mon Sep 17 00:00:00 2001 From: Jade Lovelace Date: Mon, 18 Mar 2024 23:03:48 -0700 Subject: [PATCH] Enable clang build timing analysis I didn't enable this by default for clang due to making the build time 10% worse or so. Unfortunate, but tbh devs for whom 10% of build time is not *that* bad should probably simply enable this. Change-Id: I8d1e5b6f3f76c649a4e2f115f534f7f97cee46e6 --- .gitignore | 3 +++ .../rl-next-dev/build-timing-analysis.md | 15 +++++++++++ flake.nix | 3 +++ maintainers/buildtime_report.sh | 20 ++++++++++++++ meson.build | 8 ++++++ meson.options | 4 +++ misc/clangbuildanalyzer.nix | 27 +++++++++++++++++++ 7 files changed, 80 insertions(+) create mode 100644 doc/manual/rl-next-dev/build-timing-analysis.md create mode 100755 maintainers/buildtime_report.sh create mode 100644 misc/clangbuildanalyzer.nix diff --git a/.gitignore b/.gitignore index 02c577c76..4527ab45f 100644 --- a/.gitignore +++ b/.gitignore @@ -154,3 +154,6 @@ result-* # Mac OS .DS_Store + +# ClangBuildAnalyzer output, see maintainers/buildtime_report.sh +buildtime.bin diff --git a/doc/manual/rl-next-dev/build-timing-analysis.md b/doc/manual/rl-next-dev/build-timing-analysis.md new file mode 100644 index 000000000..e59f3ca02 --- /dev/null +++ b/doc/manual/rl-next-dev/build-timing-analysis.md @@ -0,0 +1,15 @@ +--- +synopsis: Clang build timing analysis +cls: 587 +--- + +We now have Clang build profiling available, which generates Chrome +tracing files for each compilation unit. To enable it, run `meson configure +build -Dprofile-build=enabled` then rerun the compilation. + +If you want to make the build go faster, do a clang build with meson, then run +`maintainers/buildtime_report.sh build`, then contemplate how to improve the +build time. + +You can also look at individual object files' traces in +. diff --git a/flake.nix b/flake.nix index 253e16116..8c967aaa2 100644 --- a/flake.nix +++ b/flake.nix @@ -152,6 +152,7 @@ build-release-notes = final.buildPackages.callPackage ./maintainers/build-release-notes.nix { }; + clangbuildanalyzer = final.buildPackages.callPackage ./misc/clangbuildanalyzer.nix { }; boehmgc-nix = (final.boehmgc.override { enableLargeConfig = true; }).overrideAttrs (o: { @@ -360,6 +361,8 @@ # FIXME(Qyriad): remove once the migration to Meson is complete. pkgs.buildPackages.meson pkgs.buildPackages.ninja + + pkgs.buildPackages.clangbuildanalyzer ]; src = null; diff --git a/maintainers/buildtime_report.sh b/maintainers/buildtime_report.sh new file mode 100755 index 000000000..54ce75eb5 --- /dev/null +++ b/maintainers/buildtime_report.sh @@ -0,0 +1,20 @@ +#!/bin/sh + +# Generates a report of build time based on a meson build using -ftime-trace in +# Clang. +if [ $# -lt 1 ]; then + echo "usage: $0 BUILD-DIR [filename]" >&2 + exit 1 +fi + +scriptdir=$(cd "$(dirname -- "$0")" || exit ; pwd -P) +filename=${2:-$scriptdir/../buildtime.bin} + +if [ "$(meson introspect "$1" --buildoptions | jq -r '.[] | select(.name == "profile-build") | .value')" != enabled ]; then + echo 'This build was not done with profile-build enabled, so cannot generate a report' >&2 + # shellcheck disable=SC2016 + echo 'Run `meson configure build -Dprofile-build=enabled` then rebuild, first' >&2 + exit 1 +fi + +ClangBuildAnalyzer --all "$1" "$filename" && ClangBuildAnalyzer --analyze "$filename" diff --git a/meson.build b/meson.build index 66a30f716..0a6a6b4b1 100644 --- a/meson.build +++ b/meson.build @@ -307,6 +307,14 @@ if cxx.get_linker_id() in ['ld.bfd', 'ld.gold'] add_project_link_arguments('-Wl,--no-copy-dt-needed-entries', language : 'cpp') endif +# Generate Chromium tracing files for each compiled file, which enables +# maintainers/buildtime_report.sh BUILD-DIR to simply work in clang builds. +# +# They can also be manually viewed at https://ui.perfetto.dev +if get_option('profile-build').require(meson.get_compiler('cpp').get_id() == 'clang').enabled() + add_project_arguments('-ftime-trace', language: 'cpp') +endif + subdir('src') if enable_tests subdir('tests/unit') diff --git a/meson.options b/meson.options index 4e8323689..ef5a669dd 100644 --- a/meson.options +++ b/meson.options @@ -31,6 +31,10 @@ option('tests-brief', type : 'boolean', value : false, description : 'set to true for shorter tests output', ) +option('profile-build', type : 'feature', value: 'disabled', + description : 'whether to enable -ftime-trace in clang builds, allowing for speeding up the build.' +) + option('store-dir', type : 'string', value : '/nix/store', description : 'path of the Nix store', ) diff --git a/misc/clangbuildanalyzer.nix b/misc/clangbuildanalyzer.nix new file mode 100644 index 000000000..a317e5144 --- /dev/null +++ b/misc/clangbuildanalyzer.nix @@ -0,0 +1,27 @@ +# Upstreaming here, can be deleted once it's upstreamed: +# https://github.com/NixOS/nixpkgs/pull/297102 +{ stdenv, lib, cmake, fetchFromGitHub }: +stdenv.mkDerivation (finalAttrs: { + pname = "clangbuildanalyzer"; + version = "1.5.0"; + + src = fetchFromGitHub { + owner = "aras-p"; + repo = "ClangBuildAnalyzer"; + rev = "v${finalAttrs.version}"; + sha256 = "sha256-kmgdk634zM0W0OoRoP/RzepArSipa5bNqdVgdZO9gxo="; + }; + + nativeBuildInputs = [ + cmake + ]; + + meta = { + description = "Tool for analyzing Clang's -ftrace-time files"; + homepage = "https://github.com/aras-p/ClangBuildAnalyzer"; + maintainers = with lib.maintainers; [ lf- ]; + license = lib.licenses.unlicense; + platforms = lib.platforms.unix; + mainProgram = "ClangBuildAnalyzer"; + }; +})