使用 icecream 联合编译 Linux 下代码(备忘)

本文介绍了如何使用Icecream和ccache进行分布式编译加速。首先,详细阐述了Icecream相对于distcc的优势,并提供了安装和配置步骤,包括调度器和编译守护进程的设置。然后,讨论了ccache的使用,以进一步提升编译效率。最后,给出了本地项目编译的示例脚本,强调了并行编译参数的选择应根据集群规模和本地机器性能来调整。
摘要由CSDN通过智能技术生成

icecream

github 地址: https://github.com/icecc/icecream

是基于 distcc 实现的,相比较 distcc 有以下优点:

  • 安装部署简单,自带调度器。 distcc + dmucs 方式, dmucs 部署过于繁琐
  • 能打包本地编译环境,不需要手动对齐所有编译机的编译环境

编译 icecream

yum install icecream可以安装 icecream ,是 2017 年的版本,且测试下来有问题

因此选择编译 icecream 的方式安装 icecream

编译脚本如下:

#!/bin/bash
set -ex
yum install automake autoconf libtool libcap-ng-devel lzo-devel libzstd-devel libarchive-devel -y
git clone https://github.com/icecc/icecream.git
pushd icecream/
./autogen.sh
./configure --without-man
make
make install
popd

调度机

icecc-scheduler 是 icecream 的调度服务器

安装部署,类似如下脚本,这里包含做成 systemd 服务

先创建 icecc 账号,systemd 服务用该账号启动:

groupadd icecc
useradd -g icecc icecc -s /sbin/nologin

具体脚本如下:

#!/bin/bash
set -ex
cat >/etc/sysconfig/icecc-scheduler <<EOF
ICECREAM_NETNAME="icecream_net1"
ICECREAM_SCHEDULER_HOST="172.26.144.19"
ICECREAM_MAX_JOBS=""
ICECREAM_ALLOW_REMOTE="yes"
# ICECREAM_SCHEDULER_DEBUG="-vvv"
ICECREAM_SCHEDULER_DEBUG="-v"
EOF
cat > /usr/lib/systemd/system/icecc-scheduler.service <<EOF
[Unit]
Description=Icecream distributed compiler scheduler
[Service]
Type=simple
User=icecc
Group=icecc
SyslogIdentifier=icecc-scheduler
EnvironmentFile=-/etc/sysconfig/icecc-scheduler
ExecStart=/usr/local/sbin/icecc-scheduler -n \${ICECREAM_NETNAME} --persistent-client-connection \${ICECREAM_SCHEDULER_DEBUG}
[Install]
WantedBy=multi-user.target
EOF
systemctl enable icecc-scheduler.service
systemctl daemon-reload
systemctl restart icecc-scheduler.service

编译机

iceccd 是 icecream 的编译守护进程

编译机、开发机都要安装

安装部署,类似如下脚本,这里包含做成 systemd 服务

先创建 icecc 账号,systemd 服务用该账号启动:

groupadd icecc
useradd -g icecc icecc -s /sbin/nologin

具体脚本如下:

#!/bin/bash
set -ex
mkdir -p /data/iceccd/cache
chown -R icecc:icecc /data/iceccd/cache
cat >/etc/sysconfig/icecream <<EOF
ICECREAM_NETNAME="icecream_net1"
ICECREAM_SCHEDULER_HOST="172.26.144.19"
ICECREAM_MAX_JOBS=""
ICECREAM_ALLOW_REMOTE="yes"
ICECREAM_DEBUG="yes"
ICECREAM_LOG_FILE=/tmp/iceccd.log
ICECREAM_DEBUG="-v"
ICECREAM_CACHE_DIR="/data/iceccd/cache"
EOF
cat > /usr/lib/systemd/system/iceccd.service<<EOF
[Unit]
Description=Icecream Distributed Compiler
After=network.target nss-lookup.target
[Service]
Type=simple
Environment=SHELL=/bin/bash
SyslogIdentifier=iceccd
EnvironmentFile=-/etc/sysconfig/icecream
ExecStart=/usr/local/sbin/iceccd -u icecc -b \${ICECREAM_CACHE_DIR} -n \${ICECREAM_NETNAME} -s \${ICECREAM_SCHEDULER_HOST} \${ICECREAM_DEBUG}
Nice=5
[Install]
WantedBy=multi-user.target
EOF
systemctl enable iceccd.service
systemctl daemon-reload
systemctl restart iceccd.service

开发机

打包本地编译环境,类似如下脚本:

export CC=""
export CXX=""
export PATH=${your_gcc_bin_path}:$PATH
icecc --build-native```

本地项目编译脚本类似如下:

```shell
#!/bin/bash

set -ex

rm -rf build
mkdir -p build
pushd build

export PATH=/usr/local/libexec/icecc/bin/:$PATH
export ICECC_VERSION=xxx.tar.gz
export ICECC_DEBUG=info

cmake ..

make -j <num>
make install
popd

xxx.tar.gz 为 icecc --build-native 出来的文件

num 取多少值合适

引用官方的话:

Then you just compile with make -j , where is the amount of jobs you want to compile in parallel. As a start, take the number of logical processors multiplied with 2, or a larger number if your compile cluster can serve all the compilation jobs. But note that too large numbers may in fact make the build slower (for example if your local machine gets overloaded with preparing more jobs than it can handle at a time).

取决于编译集群规模与本地开发机的处理能力

需要实际跑几次确定

使用缓存 ccache

通过 ccache 缓存,编译速度还可以提高

github 地址: https://github.com/ccache/ccache

开发机安装 ccache

编译脚本,类似如下:

#!/bin/bash
set -ex
yum install asciidoctor -y
git clone https://github.com/ccache/ccache.git
pushd ccache
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DHIREDIS_FROM_INTERNET=ON ..
make
make install
popd
mkdir -p /usr/local/libexec/ccache/bin
ln -s /usr/local/bin/ccache /usr/local/libexec/ccache/bin/gcc
ln -s /usr/local/bin/ccache /usr/local/libexec/ccache/bin/g++
ln -s /usr/local/bin/ccache /usr/local/libexec/ccache/bin/cc
ln -s /usr/local/bin/ccache /usr/local/libexec/ccache/bin/c++

项目编译脚本类似:

#!/bin/bash

set -ex

rm -rf build
mkdir -p build
pushd build

export PATH=/usr/local/libexec/ccache/bin/:$PATH
export ICECC_VERSION=xxx.tar.gz
export ICECC_DEBUG=info
export CCACHE_PREFIX=icecc

cmake ..

make -j 16
make install
popd

以上

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fananchong2

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值