| #!/bin/bash |
| # Copyright 2024 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=Other |
| ### manage build profile configuration |
| |
| ## usage: |
| ## fx build-profile status |
| ## Show if system profiling during fx build is enabled or disabled. |
| ## |
| ## fx build-profile enable |
| ## Enable system profiling during fx build. |
| ## |
| ## fx build-profile disable |
| ## Disable system profiling during fx build. |
| ## |
| ## Build profiles contain information about the state of your system |
| ## resources such as CPU, memory, disk I/O. |
| ## 'vmstat' is one such tool that collects this data. |
| ## Build profile data contains no PII. |
| |
| set -e |
| |
| source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/vars.sh || exit $? |
| source "${FUCHSIA_DIR}/tools/devshell/lib/style.sh" || exit $? |
| |
| readonly BUILD_PROFILE_CONFIG="${FUCHSIA_DIR}/.fx/config/build-profile" |
| readonly BUILD_PROFILE_CONFIG_OLD="${FUCHSIA_DIR}/.fx-build-profile-config" |
| |
| function usage { |
| fx-command-help |
| } |
| |
| function styled-status { |
| local enabled="$1" |
| local profile_status |
| if [[ "$BUILD_PROFILE_ENABLED" -eq 1 ]]; then |
| profile_status="enabled" |
| color="green" |
| else |
| profile_status="disabled" |
| color="red" |
| fi |
| style::echo --bold "--${color}" "${profile_status}" |
| } |
| |
| fx-standard-switches "$@" |
| set -- "${FX_ARGV[@]}" |
| |
| if [[ $# -lt 1 ]]; then |
| fx-error Invalid syntax |
| fx-command-help |
| exit 1 |
| fi |
| |
| action="$1" |
| shift |
| action_args=("$@") |
| |
| function build-profile-read-config { |
| BUILD_PROFILE_ENABLED=0 |
| if [[ -f "${BUILD_PROFILE_CONFIG_OLD}" ]]; then |
| fx-info "Moving ${BUILD_PROFILE_CONFIG_OLD} to new location ${BUILD_PROFILE_CONFIG}. No further action is necessary." |
| mv "${BUILD_PROFILE_CONFIG_OLD}" "${BUILD_PROFILE_CONFIG}" |
| fi |
| if [[ ! -f "${BUILD_PROFILE_CONFIG}" ]]; then |
| return 1 |
| fi |
| source "${BUILD_PROFILE_CONFIG}" |
| return 0 |
| } |
| |
| function build-profile-write-config { |
| local enabled="$1" |
| local -r tempfile="${BUILD_PROFILE_CONFIG}.tmp" |
| |
| cat > "${tempfile}" <<EOF |
| # Autogenerated config file for 'fx build-profile'. |
| # Run 'fx help build-profile' for more information. |
| BUILD_PROFILE_ENABLED=${enabled} |
| EOF |
| # Only rewrite the config file if content has changed |
| if ! cmp --silent "${tempfile}" "${BUILD_PROFILE_CONFIG}" |
| then mv -f "${tempfile}" "${BUILD_PROFILE_CONFIG}" |
| else rm -f "${tempfile}" |
| fi |
| } |
| |
| # Read config in a way that exit code is preserved but doesn't exit because |
| # of "set -e" if config file doesn't exist |
| __successfully_read_config=0 |
| build-profile-read-config || __successfully_read_config="$?" |
| |
| case "$action" in |
| -h|--help) |
| usage |
| exit 0 |
| ;; |
| |
| status) |
| echo "Collection of system profiles during builds is currently $(styled-status "$BUILD_PROFILE_ENABLED") for ${FUCHSIA_DIR}" |
| echo "To change it, run: fx build-profile <enable|disable>" |
| exit 0 |
| ;; |
| |
| enable) |
| if [[ "$BUILD_PROFILE_ENABLED" -eq 1 ]]; then |
| echo "System profiling during build is already enabled." |
| exit 0 |
| fi |
| BUILD_PROFILE_ENABLED=1 |
| build-profile-write-config "${BUILD_PROFILE_ENABLED}" |
| echo "System profiling during build is enabled at user's request" |
| ;; |
| |
| disable) |
| if [[ "$BUILD_PROFILE_ENABLED" -eq 0 ]]; then |
| if [[ "${__successfully_read_config}" -ne 0 ]]; then |
| # if profile is already disabled but the config file does not exist or |
| # is inconsistent, we rewrite the file before exiting |
| build-profile-write-config "${BUILD_PROFILE_ENABLED}" |
| fi |
| echo "System profiling during build is already disabled." |
| exit 0 |
| fi |
| BUILD_PROFILE_ENABLED=0 |
| build-profile-write-config "${BUILD_PROFILE_ENABLED}" |
| echo "System profiling during build is disabled at user's request" |
| ;; |
| |
| *) |
| fx-error Invalid syntax |
| fx-command-help |
| exit 1 |
| ;; |
| esac |
| |
| echo "System profiling during build is now $(styled-status "$BUILD_PROFILE_ENABLED") for ${FUCHSIA_DIR}" |