From d6a02e09b1900cb48f1f19394ef37ad64f9ca142 Mon Sep 17 00:00:00 2001 From: Henning Schild Date: Mon, 2 Sep 2024 12:04:28 +0200 Subject: [PATCH] feat: add support for a persistent ccache Closes: #64 Signed-off-by: Henning Schild --- Dockerfile | 2 ++ README.md | 60 ++++++++++++++++++++++++++++++++++++++++ docker-entrypoint-tcp.sh | 4 +++ 3 files changed, 66 insertions(+) diff --git a/Dockerfile b/Dockerfile index 4e255a4..cdb9712 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,6 +8,8 @@ RUN --mount=type=bind,from=ksmanis/gentoo-distcc:tcp,source=/var/cache/binpkgs,t emerge --info; \ emerge distcc; \ distcc --version; \ + emerge ccache; \ + ccache --version; \ emerge --oneshot gentoolkit; \ eclean packages; \ CLEAN_DELAY=0 emerge --depclean gentoolkit; \ diff --git a/README.md b/README.md index 2f064c4..c4a6587 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,66 @@ through the optional `SSH_USERNAME` environment variable: docker run -d -p 30022:22 -e SSH_USERNAME=bob -e AUTHORIZED_KEYS="..." --name gentoo-distcc-ssh --rm ksmanis/gentoo-distcc:ssh ``` +### Persistent [ccache(1)](https://ccache.dev/manual/latest.html) + +Ccache speeds up recompilation by caching the result of previous compilations +and detecting when the same compilation is being done again. That can help save +time on compilation if you end up recompiling for any reason i.e. because you +have multiple gentoo machines which have an overlapping package selection. + +Check the [gentoo wiki](https://wiki.gentoo.org/wiki/Ccache) to learn more. If +you only have one gentoo machine and it has enough disk space to host its own +cache, you might be better off with one local ccache on that machine with +`FEATURES="ccache"`. + +To use ccache with the container images just mount a ccache folder into the +container as `/var/cache/ccache`. + +Create a new ccache folder: + +```shell +cd $HOME +mkdir gentoo-distcc-ccache +cat << EOF > gentoo-distcc-ccache/ccache.conf +max_size = 10.0G +hash_dir = false +compiler_check = %compiler% -v +EOF +# as root chown to 240:240 (uid:gid of distcc on gentoo) +su -c "chown -R 240:240 gentoo-distcc-ccache" +``` + +Now simply add `-v $HOME/gentoo-distcc-ccache/:/var/cache/ccache/:rw` to your +`docker run` arguments. i.e. + +```shell +docker run -d -p 3632:3632 --name gentoo-distcc-tcp --rm -v $HOME/gentoo-distcc-ccache/:/var/cache/ccache/:rw ksmanis/gentoo-distcc:tcp +``` + +To check the status of the cache you can run `ccache -s` with the container +image like that: + +```shell +docker run --rm -v $HOME/gentoo-distcc-ccache/:/var/cache/ccache/:rw -e CCACHE_DIR=/var/cache/ccache ksmanis/gentoo-distcc:tcp ccache -s +``` + +A much faster way would be to have ccache also installed on the container host +and get the same information with: + +```shell +CCACHE_DIR=$HOME/gentoo-distcc-ccache/ ccache -s +``` + +You should see the cache filling up and if you compile the same package twice +you should see cache hits coming in. If you are using `FEATURES="ccache"` from +gentoo you might want to disable it for a test. Otherwise local cache hits will +prevent remote compilation. + +The cache is a shared folder between the host and the container. If both want to +access it you might have to play with `chown` and `CCACHE_UMASK` (or `umask` in +ccache.conf). Note that a world writable cache will have security implications +on all systems using distcc. + ## Testing A manual way to test the containers is to compile a sample C file: diff --git a/docker-entrypoint-tcp.sh b/docker-entrypoint-tcp.sh index a70d5a7..c15d4a5 100755 --- a/docker-entrypoint-tcp.sh +++ b/docker-entrypoint-tcp.sh @@ -6,6 +6,10 @@ set -e # * if hyphenated flag arguments (e.g., '-f', '-foo', or '--foo') were # passed to the Docker command line if [ "$#" -eq 0 ] || [ "${1#-}" != "$1" ]; then + export CCACHE_DIR="/var/cache/ccache" + if [ -f "$CCACHE_DIR/ccache.conf" ]; then + export PATH="/usr/lib/ccache/bin:$PATH" + fi exec distccd --daemon --no-detach --log-level notice --log-stderr --allow-private "$@" fi