| #!/bin/bash |
| # Copyright 2019 The Fuchsia Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| #### CATEGORY=Build |
| ### Collect Ninja trace information for analysis in chrome://tracing |
| |
| ## Usage: fx ninjatrace2json |
| ## |
| ## Use this after a build to write "ninjatrace.json" in the build directory. |
| |
| set -e |
| |
| source "$(cd "$(dirname "${BASH_SOURCE[0]}")" > /dev/null 2>&1 && pwd)"/lib/vars.sh || exit $? |
| fx-config-read |
| |
| function usage() { |
| cat << EOF |
| Use this after a build to write "ninjatrace.json" in the build directory. |
| See https://fuchsia.dev/internal/intree/concepts/build-performance?hl=en#build-performance-analysis-as-a-trace. |
| |
| fx ninjatrace2json [options] |
| |
| Options: |
| --help|-h: print this help and exit |
| --rbe-rpl-path: when provided, interleave remote execution stats from RBE into |
| the main trace |
| EOF |
| } |
| |
| rbe_rpl_path='' |
| # Extract script options before -- |
| for opt; do |
| # handle --option arg |
| if test -n "$prev_opt"; then |
| eval "$prev_opt"=\$opt |
| prev_opt= |
| shift |
| continue |
| fi |
| # Extract optarg from --opt=optarg |
| case "$opt" in |
| *=?*) optarg=$(expr "X$opt" : '[^=]*=\(.*\)') ;; |
| *=) optarg= ;; |
| esac |
| case "$opt" in |
| --help | -h) |
| usage |
| exit |
| ;; |
| --rbe-rpl-path=*) rbe_rpl_path="$optarg" ;; |
| --rbe-rpl-path) prev_opt='rbe_rpl_path' ;; |
| *) |
| echo "Unknown option ${opt}" |
| usage |
| exit 1 |
| ;; |
| esac |
| shift |
| done |
| |
| test -z "$prev_opt" || { |
| echo "Option is missing argument to set $prev_opt." |
| exit 1 |
| } |
| |
| TMP_DIR="$(mktemp -d)" |
| readonly TMP_DIR |
| trap 'rm -rf "${TMP_DIR}"' EXIT |
| |
| "${PREBUILT_NINJA}" -C "${FUCHSIA_BUILD_DIR}" -t compdb > "${TMP_DIR}/compdb.json" |
| "${PREBUILT_NINJA}" -C "${FUCHSIA_BUILD_DIR}" -t graph > "${TMP_DIR}/graph.dot" |
| |
| ninjatrace_args=( |
| -ninjalog "${FUCHSIA_BUILD_DIR}/.ninja_log" |
| -compdb "${TMP_DIR}/compdb.json" |
| -graph "${TMP_DIR}/graph.dot" |
| -trace-json "${FUCHSIA_BUILD_DIR}/ninjatrace.json" |
| -critical-path |
| ) |
| |
| test -z "${rbe_rpl_path}" || { |
| ninjatrace_args+=( |
| -rbe-rpl-path "${rbe_rpl_path}" |
| '-rpl2trace-path' "${PREBUILT_RECLIENT_DIR}/rpl2trace" |
| ) |
| } |
| |
| "${HOST_OUT_DIR}/ninjatrace" "${ninjatrace_args[@]}" |
| |
| echo "Now visit chrome://tracing and load ${FUCHSIA_BUILD_DIR}/ninjatrace.json" |