Skip to content

Commit

Permalink
Enable native benchmarking in new shootout directory
Browse files Browse the repository at this point in the history
This change refactors how the shootout native benchmarks are built. The
`Dockerfile.native` file is retained and is expected to be _the_ way to
build the native shared libraries for this kind of benchmarking. A
`build-native.sh` script is included in the directory to (a) be used by
`Dockerfile.native` and (b) for building the native benchmarks in
environments where running Docker may not be possible.

Now that all of the benchmarks are built in one directory, the native
libraries cannot all be named `benchmark.so`. Because of this and the
hard-coded path expected by the native engine (see bytecodealliance#259), this change
also modifies the associated `*-native.sh` scripts to set up a temporary
directory that looks like the `benchmark.so` environment that was there
previously. This additional logic could be removed once bytecodealliance#259 is fixed.
  • Loading branch information
abrown committed Jul 5, 2023
1 parent 1885f51 commit 069e089
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 79 deletions.
2 changes: 1 addition & 1 deletion benchmarks/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
*/benchmark.so
*/*.so
*/target
12 changes: 0 additions & 12 deletions benchmarks/Dockerfile.native

This file was deleted.

63 changes: 26 additions & 37 deletions benchmarks/build-native.sh
Original file line number Diff line number Diff line change
@@ -1,35 +1,27 @@
#!/usr/bin/env bash

# Build a single native benchmark
# Build a single native benchmark.
#
# Usage: ./build-native.sh [--host] <path to benchmark directory>

set -e

BENCHMARKS_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
SIGHTGLASS_BASE=$(dirname $BENCHMARKS_DIR)
ENGINE=$SIGHTGLASS_BASE/engines/native/libengine.so

BENCHMARK_DIR="";

for ARG in "$@"; do
if [[ -d $ARG ]]; then
BENCHMARK_DIR=$ARG;
break
fi
done
BENCHMARK_DIR=$1;

print_header() {
>&2 echo
>&2 echo ===== $@ =====
}


if [[ $BENCHMARK_DIR == "" ]]; then
if [[ ! -d $BENCHMARK_DIR ]]; then
echo "Unknown benchmark directory; usage: ./build-native.sh [--host] <path to benchmark directory>"
exit 1
fi


# Build engine if not availble since it needs to be present for linking
# Build engine if not availble since it needs to be present for linking.
if [[ ! -f $ENGINE ]]; then
cd $SIGHTGLASS_BASE/engines/native/libengine/
cargo build --release
Expand All @@ -40,29 +32,26 @@ fi
# Build directly on host
FLAG='--host'
if [[ $* == *$FLAG* ]]; then

print_header "Build Native Benchmark Using Host"
(set -x; cd $BENCHMARK_DIR && cargo run --release)


print_header "Build native benchmark directly on host (no Docker)"
(set -x; cd $BENCHMARK_DIR && ./build-native.sh)
else

BENCHMARK_NAME=$(readlink -f $BENCHMARK_DIR | xargs basename)
IMAGE_NAME=sightglass-benchmark-$BENCHMARK_NAME-native

# To allow the use of symlinks in the benchmark directories (docker ignores them), we `tar` up the
# directory and `--dereference` (i.e., follow) all symlinks provided.
print_header "Create build context"
TMP_TAR=$(mktemp /tmp/sightglass-benchmark-dir-XXXXXX.tar)
(set -x; cd $BENCHMARK_DIR && ln -f -s ../../engines/native/libengine.so ./libengine.so && tar --create --file $TMP_TAR --dereference --verbose . && rm libengine.so)

# Build the benchmark and extract the build results from the container
print_header "Build Native Benchmark Using Host"
(set -x; docker build -f Dockerfile.native --tag $IMAGE_NAME - < $TMP_TAR)
CONTAINER_ID=$(set -x; docker create $IMAGE_NAME)
(set -x; mkdir -p $BENCHMARK_DIR/target; docker cp --follow-link $CONTAINER_ID:/benchmark/target/. $BENCHMARK_DIR/target/;)

# Copy host files to container and build inside a container

BENCHMARK_NAME=$(readlink -f $BENCHMARK_DIR | xargs basename)
IMAGE_NAME=sightglass-benchmark-$BENCHMARK_NAME-native

# To allow the use of symlinks in the benchmark directories (docker ignores
# them), we `tar` up the directory and `--dereference` (i.e., follow) all
# symlinks provided.
print_header "Create build context"
TMP_TAR=$(mktemp /tmp/sightglass-benchmark-dir-XXXXXX.tar)
(set -x; cd $BENCHMARK_DIR && \
ln -f -s ../../engines/native/libengine.so ./libengine.so && \
tar --create --file $TMP_TAR --dereference --verbose . && \
rm libengine.so)

# Build the benchmark and extract the build results from the container.
print_header "Build native benchmark in Docker"
(set -x; docker build -f Dockerfile.native --tag $IMAGE_NAME - < $TMP_TAR)
CONTAINER_ID=$(set -x; docker create $IMAGE_NAME)
(set -x; docker cp --follow-link $CONTAINER_ID:/benchmark/. $BENCHMARK_DIR/;)
fi

52 changes: 23 additions & 29 deletions benchmarks/run-native.sh
Original file line number Diff line number Diff line change
@@ -1,40 +1,22 @@
#!/usr/bin/env bash

# Run a single native benchmark that is already built
# Run a single native benchmark that is already built.
#
# Usage: ./run-native.sh <path-to-benchmark-folder/target/benchmark.so>
# Usage: ./run-native.sh <path-to-benchmark-folder/benchmark.so>

set -e

BENCHMARKS_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
SIGHTGLASS_BASE=$(dirname $BENCHMARKS_DIR)

SIGHTGLASS="cargo +nightly run --release --bin sightglass-cli --"

SIGHTGLASS="cargo run --release --bin sightglass-cli --"
ENGINE=$SIGHTGLASS_BASE/engines/native/libengine.so

BENCHMARK_NATIVE_SO="";

for ARG in "$@"; do
if [[ -f $ARG ]]; then
BENCHMARK_NATIVE_SO=$ARG;
break
fi
done
BENCHMARK_NATIVE_SO=$1

if [[ -z $BENCHMARK_NATIVE_SO ]]; then
echo "Missing benchmark"
echo "Usage: run-native.sh <path-to-benchmark-folder/target/benchmark.so>"
exit
echo "Usage: run-native.sh <path-to-benchmark-folder/benchmark.so>"
exit 1
fi

BENCHMARK_NATIVE_SO="$(realpath $BENCHMARK_NATIVE_SO)"

print_header() {
>&2 echo
>&2 echo ===== $@ =====
}

# If an engine is not available, build it.
if [[ ! -f $ENGINE ]]; then
cd $SIGHTGLASS_BASE/engines/native/libengine/
Expand All @@ -43,10 +25,22 @@ if [[ ! -f $ENGINE ]]; then
cd - > /dev/null
fi

# Run a benchmark with the newly created library
# Because of some hard-coding in the native engine (TODO:
# https://github.com/bytecodealliance/sightglass/issues/259), we need to set up
# the temporary directory with the hard-coded paths.
BENCHMARK_NATIVE_SO="$(realpath $BENCHMARK_NATIVE_SO)"
MD5SUM=$(md5sum $BENCHMARK_NATIVE_SO | awk '{ print $1 }')
TMP_BENCHMARK_DIR=/tmp/sightglass-benchmark-native-$MD5SUM
mkdir -p $TMP_BENCHMARK_DIR
ln -fs $BENCHMARK_NATIVE_SO $TMP_BENCHMARK_DIR/benchmark.so
BENCHMARK_DIR=$(dirname $BENCHMARK_NATIVE_SO)
NAME=$(basename $BENCHMARK_NATIVE_SO .so);
for FILE in $(find $BENCHMARK_DIR -name "$NAME*.input"); do
ln -fs $FILE $TMP_BENCHMARK_DIR/
done

# Run a benchmark with the native library.
cd $SIGHTGLASS_BASE
BENCH_DIR=$(dirname $BENCHMARK_NATIVE_SO)
echo $BENCH_DIR
LD_LIBRARY_PATH=./engines/native/ $SIGHTGLASS benchmark --engine engines/native/libengine.so --working-dir $BENCH_DIR -- $BENCHMARK_NATIVE_SO
LD_LIBRARY_PATH=$(dirname $ENGINE) $SIGHTGLASS benchmark --engine $ENGINE \
--working-dir $TMP_BENCHMARK_DIR -- $TMP_BENCHMARK_DIR/benchmark.so
cd - > /dev/null

16 changes: 16 additions & 0 deletions benchmarks/shootout/Dockerfile.native
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM ubuntu:20.04
ENV DEBIAN_FRONTEND="noninteractive" TZ="America"
RUN apt update && apt-get install -y --no-install-recommends wget build-essential

# Copy in the `src` directory.
ENV SRC=/usr/src/shootout
WORKDIR $SRC
COPY src .
COPY build-native.sh .
COPY libengine.so /usr/lib

# Compile each of the benchmarks into the `/benchmark` directory.
WORKDIR /benchmark
RUN SRC_DIR=$SRC ENGINE_DIR=/usr/lib $SRC/build-native.sh
# We output the shared libraries to the `/benchmark` directory, where the client
# expects it.
16 changes: 16 additions & 0 deletions benchmarks/shootout/build-native.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

# Build all of the shootout benchmarks as native shared libraries (Linux-only).
#
# Usage: ./build-native.sh

ENGINE_DIR=${ENGINE_LIBRARY_DIR:-"../../engines/native"}
SRC_DIR=${SRC_DIR:-"src"}
BENCHMARKS=${BENCHMARKS:-$(find $SRC_DIR -name '*.c')}
CFLAGS=${CFLAGS:-"-O3 -fPIC -I$SRC_DIR -Wno-attributes"}
LDFLAGS=${LDFLAGS:-"-shared -L$ENGINE_DIR -lengine"}

for BENCHMARK in $BENCHMARKS; do
NAME=$(basename $BENCHMARK .c);
cc -Dmain=native_entry $CFLAGS $LDFLAGS $BENCHMARK -o shootout-$NAME.so
done

0 comments on commit 069e089

Please sign in to comment.