Skip to content

Commit

Permalink
[shortfin] Support ccache in package builds and make tracing optional. (
Browse files Browse the repository at this point in the history
#646)

Splitting this off from #589 to
make progress on #584.

Tested with
```
CACHE_DIR=/tmp/shortfin/ sudo -E ./shortfin/build_tools/build_linux_package.sh

+ ccache --show-stats
Cacheable calls:   626 / 636 (98.43%)
  Hits:              2 / 626 ( 0.32%)
    Direct:          2 /   2 (100.0%)
    Preprocessed:    0 /   2 ( 0.00%)
  Misses:          624 / 626 (99.68%)
Uncacheable calls:  10 / 636 ( 1.57%)
Local storage:
  Cache size (GB): 0.1 / 2.0 ( 3.10%)
  Hits:              2 / 626 ( 0.32%)
  Misses:          624 / 626 (99.68%)

+ ccache --show-stats
ccache stats:
Cacheable calls:   1252 / 1272 (98.43%)
  Hits:             550 / 1252 (43.93%)
    Direct:         550 /  550 (100.0%)
    Preprocessed:     0 /  550 ( 0.00%)
  Misses:           702 / 1252 (56.07%)
Uncacheable calls:   20 / 1272 ( 1.57%)
Local storage:
  Cache size (GB):  0.1 /  2.0 ( 4.11%)
  Hits:             550 / 1252 (43.93%)
  Misses:           702 / 1252 (56.07%)

+ ccache --show-stats
Cacheable calls:   1878 / 1908 (98.43%)
  Hits:            1098 / 1878 (58.47%)
    Direct:        1098 / 1098 (100.0%)
    Preprocessed:     0 / 1098 ( 0.00%)
  Misses:           780 / 1878 (41.53%)
Uncacheable calls:   30 / 1908 ( 1.57%)
Local storage:
  Cache size (GB):  0.1 /  2.0 ( 5.12%)
  Hits:            1098 / 1878 (58.47%)
  Misses:           780 / 1878 (41.53%)

CACHE_DIR=/tmp/shortfin/ sudo -E ./shortfin/build_tools/build_linux_package.sh

+ ccache --show-stats
ccache stats:
Cacheable calls:   3756 / 3816 (98.43%)
  Hits:            2820 / 3756 (75.08%)
    Direct:        2820 / 2820 (100.0%)
    Preprocessed:     0 / 2820 ( 0.00%)
  Misses:           936 / 3756 (24.92%)
Uncacheable calls:   60 / 3816 ( 1.57%)
Local storage:
  Cache size (GB):  0.1 /  2.0 ( 5.19%)
  Hits:            2820 / 3756 (75.08%)
  Misses:           936 / 3756 (24.92%)
```

So we have multiple configurations getting built (Python versions,
tracing enable/disabled), but we still get a reasonable number of cache
hits. Definitely room to improve there, but better than nothing.
  • Loading branch information
ScottTodd authored Dec 5, 2024
1 parent 57a7390 commit 8ea135a
Showing 1 changed file with 63 additions and 2 deletions.
65 changes: 63 additions & 2 deletions shortfin/build_tools/build_linux_package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
# Build everything (all python versions):
# sudo ./build_tools/build_linux_package.sh
#
# Build specific Python versions to custom directory:
# Build specific Python versions to custom directory, with tracing enabled:
# OVERRIDE_PYTHON_VERSIONS="cp312-cp312 cp313-cp313" \
# OUTPUT_DIR="/tmp/wheelhouse" \
# SHORTFIN_ENABLE_TRACING="ON" \
# sudo -E ./build_tools/build_linux_package.sh
#
# Valid Python versions match a subdirectory under /opt/python in the docker
Expand All @@ -40,6 +41,8 @@ ARCH="$(uname -m)"
MANYLINUX_DOCKER_IMAGE="${MANYLINUX_DOCKER_IMAGE:-quay.io/pypa/manylinux_2_28_${ARCH}:latest}"
PYTHON_VERSIONS="${OVERRIDE_PYTHON_VERSIONS:-cp311-cp311 cp312-cp312 cp313-cp313}"
OUTPUT_DIR="${OUTPUT_DIR:-${THIS_DIR}/wheelhouse}"
CACHE_DIR="${CACHE_DIR:-}"
SHORTFIN_ENABLE_TRACING="${SHORTFIN_ENABLE_TRACING:-ON}"

function run_on_host() {
echo "Running on host"
Expand All @@ -50,12 +53,23 @@ function run_on_host() {
OUTPUT_DIR="$(cd "${OUTPUT_DIR}" && pwd)"
echo "Outputting to ${OUTPUT_DIR}"
mkdir -p "${OUTPUT_DIR}"

# Setup cache as needed.
extra_args=""
if ! [ -z "$CACHE_DIR" ]; then
echo "Setting up host cache dir ${CACHE_DIR}"
mkdir -p "${CACHE_DIR}/ccache"
extra_args="${extra_args} -v ${CACHE_DIR}:${CACHE_DIR} -e CACHE_DIR=${CACHE_DIR}"
fi

docker run --rm \
-v "${REPO_ROOT}:${REPO_ROOT}" \
-v "${OUTPUT_DIR}:${OUTPUT_DIR}" \
-e __MANYLINUX_BUILD_WHEELS_IN_DOCKER=1 \
-e "OVERRIDE_PYTHON_VERSIONS=${PYTHON_VERSIONS}" \
-e "OUTPUT_DIR=${OUTPUT_DIR}" \
-e "SHORTFIN_ENABLE_TRACING=${SHORTFIN_ENABLE_TRACING}" \
${extra_args} \
"${MANYLINUX_DOCKER_IMAGE}" \
-- ${THIS_DIR}/${SCRIPT_NAME}

Expand All @@ -72,6 +86,23 @@ function run_in_docker() {
echo "Using python versions: ${PYTHON_VERSIONS}"
local orig_path="${PATH}"

# Configure caching.
if [ -z "$CACHE_DIR" ]; then
echo "Cache directory not configured. No caching will take place."
else
# TODO: include this in the dockerfile we use so it gets cached
install_ccache

# TODO: debug low cache hit rate (~30% hits out of 98% cacheable) on CI
mkdir -p "${CACHE_DIR}"
CACHE_DIR="$(cd ${CACHE_DIR} && pwd)"
echo "Caching build artifacts to ${CACHE_DIR}"
export CCACHE_DIR="${CACHE_DIR}/ccache"
export CCACHE_MAXSIZE="2G"
export CMAKE_C_COMPILER_LAUNCHER=ccache
export CMAKE_CXX_COMPILER_LAUNCHER=ccache
fi

# Build phase.
echo "******************** BUILDING PACKAGE ********************"
for python_version in ${PYTHON_VERSIONS}; do
Expand All @@ -82,14 +113,44 @@ function run_in_docker() {
fi
export PATH="${python_dir}/bin:${orig_path}"
echo ":::: Python version $(python --version)"

clean_wheels "shortfin" "${python_version}"
build_shortfin
run_audit_wheel "shortfin" "${python_version}"

if ! [ -z "$CACHE_DIR" ]; then
echo "ccache stats:"
ccache --show-stats
fi
done
}

function install_ccache() {
# This gets an old version.
# yum install -y ccache

CCACHE_VERSION="4.10.2"

if [[ "${ARCH}" == "x86_64" ]]; then
curl --silent --fail --show-error --location \
"https://github.com/ccache/ccache/releases/download/v${CCACHE_VERSION}/ccache-${CCACHE_VERSION}-linux-${ARCH}.tar.xz" \
--output ccache.tar.xz

tar xf ccache.tar.xz
cp ccache-${CCACHE_VERSION}-linux-${ARCH}/ccache /usr/local/bin
elif [[ "${ARCH}" == "aarch64" ]]; then
# Latest version of ccache is not released for arm64, built it
git clone --depth 1 --branch "v${CCACHE_VERSION}" https://github.com/ccache/ccache.git
mkdir -p ccache/build && cd "$_"
cmake -G "Ninja" -DCMAKE_BUILD_TYPE=Release ..
ninja
cp ccache /usr/bin/
fi
}

function build_shortfin() {
export SHORTFIN_ENABLE_TRACING=ON
# Note: The SHORTFIN_ENABLE_TRACING environment variable should have been
# forwarded from the host environment into Docker above.
python -m pip wheel --disable-pip-version-check -v -w "${OUTPUT_DIR}" "${REPO_ROOT}/shortfin"
}

Expand Down

0 comments on commit 8ea135a

Please sign in to comment.