darallium's tech blog
home
blog
tech

qiita: @darallium
github: darallium
twitter: /darallium/
instagram: yu_kyu_n
email
Home tech sccache-setup
  • sccache分散コンパイル 簡易セットアップマニュアル

    1. 目次
    2. 概要
    3. スケジューラーの設定
    4. コンパイルサーバーの設定
    5. クライアントの設定

    概要

    処理の流れが分かりづらいので、先に概要をまとめる。

    1. コンパイルしたいマシン((クライアント)がスケジューラーにジョブを投げる。
    2. スケジューラーは認証済みコンパイルサーバーにジョブを分散する。
    3. スケジューラーからコンパイル結果がクライアントに帰ってくる。
    4. コンパイルしたいマシンでリンクして完了。

    このとき、処理性能的に避けたいところではあるが、ノード数が少ない場合はスケジューラーが入っているマシンにサーバーも突っ込んでしまっても動作する。

    スケジューラーの設定

    https://github.com/mozilla/sccache/releases/latest

    公式リポジトリから、以下のファイルをダウンロードする。sha256でtgzを検証する。解凍する。

    • sccache-dist-****-x86_64-unknown-linux-musl.tar.gz
    • sccache-dist-****-x86_64-unknown-linux-musl.tar.gz.sha256
    cd /tmp
    wget https://github.com/mozilla/sccache/releases/download/v0.10.0/sccache-dist-v0.10.0-x86_64-unknown-linux-musl.tar.gz
    tar xzvf sccache-dist-v0.10.0-x86_64-unknown-linux-musl.tar.gz
    cd sccache-dist-v0.10.0-x86_64-unknown-linux-musl
    sudo cp sccache-dist /usr/bin/

    続けて、スケジューラーがコンパイルサーバーを認証するための秘密鍵を発行する。

    $ sccache-dist auth generate-jwt-hs256-key
    XT2e-hs256256256256-examplerfnyanf-hogehoge

    public_addr は、外部からアクセスするためのアドレスを指定する。

    /etc/sccache/scheduler.conf

    # The socket address the scheduler will listen on. It's strongly recommended
    # to listen on localhost and put a HTTPS server in front of it.
    public_addr = "127.0.0.1:10600"
    
    [client_auth]
    type = "token"
    token = "my client token"
    
    [server_auth]
    type = "jwt_hs256"
    secret_key = "your command token" #XT2e-hs256256256256-examplerfnyanf-hogehoge

    これで起動するようになったので、サービスとして登録する。スケジューラーはデフォルトでデーモンとして起動するようになっているが、いちいち起動するのがめんどくさいので systemd に管理してもらう。

    /etc/systemd/system/sccache-scheduler.service

    [Unit]
    Description=sccache-dist server
    Wants=network-online.target sccache-scheduler.service
    After=network-online.target sccache-scheduler.service
    
    [Service]
    ExecStartPre=/bin/sleep 5
    ExecStart=/usr/bin/sccache-dist server --config /etc/sccache/server.conf
    
    [Install]
    WantedBy=multi-user.target
    root@apt:~# cat /etc/systemd/system/sccache-scheduler.service;
    [Unit]
    Description=sccache-dist scheduler
    Wants=network-online.target
    After=network-online.target
    
    [Service]
    Environment="SCCACHE_NO_DAEMON=1"
    ExecStart=/usr/bin/sccache-dist scheduler --config /etc/sccache/scheduler.conf
    
    
    [Install]
    WantedBy=multi-user.target

    コンパイルサーバーの設定

    必要なバイナリはスケジューラーと同じなので省略する。

    続けて、スケジューラーがコンパイルサーバーを認証するためのトークンを発行する。このとき、—serverの引数はコンパイルサーバー自身のアドレスを指定する。

    $ sccache-dist auth generate-jwt-hs256-server-token --config /etc/sccache/scheduler.conf --server 127.0.0.1:10600
    jwthereherehereherehre.1298ujondcsa9iuhpaoisd.fnya0a93rjfafew09ij

    このとき、public_addr は、先程指定したものと揃える必要がある。scheduler_urlはコンパイルサーバーに向ける。スケジューラーの設定ファイルで指定したものとは揃えなくても良い。

    /etc/sccache/server.conf

    # This is where client toolchains will be stored.
    cache_dir = "/ccache/toolchains"
    # The maximum size of the toolchain cache, in bytes.
    # If unspecified the default is 10GB.
    toolchain_cache_size = 10737418240
    # A public IP address and port that clients will use to connect to this builder.
    public_addr = "127.0.0.1:10501"
    # The URL used to connect to the scheduler (should use https, given an ideal
    # setup of a HTTPS server in front of the scheduler)
    scheduler_url = "http://localhost:10600"
    
    [builder]
    type = "overlay"
    # The directory under which a sandboxed filesystem will be created for builds.
    build_dir = "/ccache/build"
    # The path to the bubblewrap version 0.3.0+ `bwrap` binary.
    bwrap_path = "/usr/bin/bwrap"
    
    [scheduler_auth]
    type = "jwt_token"
    token = "your jwt" #"jwthereherehereherehre.1298ujondcsa9iuhpaoisd.fnya0a93rjfafew09ij"

    スケジューラーと同様にして、 systemd にサービスとして管理してもらう。こちらはデーモン化しないが、起動タイミングを調節しないとスケジューラーよりも先に起動してしまうのでsleepを挟む。

    /etc/systemd/system/sccache-server.service

    [Unit]
    Description=sccache-dist server
    Wants=network-online.target sccache-scheduler.service
    After=network-online.target sccache-scheduler.service
    
    [Service]
    ExecStartPre=/bin/sleep 5
    ExecStart=/usr/bin/sccache-dist server --config /etc/sccache/server.conf
    
    [Install]
    WantedBy=multi-user.target

    クライアントの設定

    公式リポジトリから、以下のファイルをダウンロードする。sha256でtgzを検証する。解凍する。

    • sccache-****-****.tar.gz
    • sccache-****-****.tar.gz.sha256

    もしくは、cargo install sccacheで手元でビルドする。こちらはあまり設定するものもなく、dist.scheduler_urlと[dist.auth]が設定されていれば十分。

    ~/.config/sccache/config

    [dist]
    scheduler_url = "http://172.23.88.21:10600"
    # Used for mapping local toolchains to remote cross-compile toolchains. Empty in
    # this example where the client and build server are both Linux.
    toolchains = []
    # Size of the local toolchain cache, in bytes (5GB here, 10GB if unspecified).
    toolchain_cache_size = 5368709120
    
    [dist.auth]
    type = "token"
    # This should match the `client_auth` section of the scheduler config.
    token = "my client token"

    ここまで設定が終わって、以下の表示が出れば正解。

    $ sccache --dist-status
    {"SchedulerStatus":["http://172.23.88.21:10600/",{"num_servers":1,"num_cpus":1,"in_progress":0}]}%
    ===============広告=================

    darallium

    Sun Jun 08 2025 00:18:00 GMT+0900 (Japan Standard Time)