暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

centos7 安装 bcc-tools 软件包

6697

一、升级内核

bcc-tools 需要内核版本为 4.1 或者更新的版本,如果你用的是 CentOS,那就需要手动升级,Centos7.6内核升级

注意:BPF 堆栈跟踪仅 4.6 版本以上可用!!!强烈建议升级到 4.6 内核以上。

yum install gcc-c++
wget https://cmake.org/files/v3.7/cmake-3.7.0.tar.gz
tar xf cmake-3.7.0.tar.gz
cd cmake-3.7.0
./configure
make -j $THREADS
make install
cd ..


二、安装 bison

curl -OL https://ftp.gnu.org/gnu/bison/bison-3.0.tar.xz
tar -xf bison-3.0.tar.xz
cd bison-3.0
./configure
make
make install
cd ..


三、安装并编译LLVM

curl -LO http://releases.llvm.org/3.9.1/cfe-3.9.1.src.tar.xz
curl -LO http://releases.llvm.org/3.9.1/llvm-3.9.1.src.tar.xz

tar -xf cfe-3.9.1.src.tar.xz
tar -xf llvm-3.9.1.src.tar.xz
mkdir clang-build
mkdir llvm-build

cd llvm-build
cmake -G "Unix Makefiles" -DLLVM_TARGETS_TO_BUILD="BPF;X86" \
  -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr ../llvm-3.9.1.src
make -j $THREADS
make install

cd ../clang-build
cmake -G "Unix Makefiles" -DLLVM_TARGETS_TO_BUILD="BPF;X86" \
  -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr ../cfe-3.9.1.src
make -j $THREADS
make install
cd ..


四、安装netperf

git clone https://github.com/multipath-tcp/netperf.git
cd netperf
./configure
make
make install


五、安装 bcc 依赖

yum install -y elfutils-libelf-devel flex iperf


六、安装bcc

git clone https://github.com/iovisor/bcc.git

export CFLAGS=-I${HOME}/build/linux-4.9.71/usr/include
mkdir bcc-build
cd bcc-build
cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/usr ../bcc
make -j $THREADS
make install


七、开启 debugfs

# Copy bcc package for python 2.7
cp -r usr/lib/python2.6/site-packages/bcc usr/local/lib/python2.7/site-packages/bcc

#
 MANUAL! Mount DEBUGFS in fstab:
#debugfs sys/kernel/debug debugfs defaults 0 0
# or
mount -t debugfs debugfs sys/kernel/debug


八、问题解决

[root@k8s ~]# cachestat 1 1
modprobe: FATAL: Module kheaders not found.
Unable to find kernel headers. Try rebuilding kernel with CONFIG_IKHEADERS=m (module) or installing the kernel development package for your running kernel version.
chdir(/lib/modules/4.4.236-1.el7.elrepo.x86_64/build): No such file or directory
Traceback (most recent call last):
  File "/usr/share/bcc/tools/cachestat", line 96, in <module>
    b = BPF(text=bpf_text)
  File "/usr/lib/python2.7/site-packages/bcc/__init__.py", line 364, in __init__
    raise Exception("Failed to compile BPF module %s" % (src_file or "<text>"))
Exception: Failed to compile BPF module <text>
[root@k8s ~]#


解决办法: 查看kernel 版本, 然后安装 kernel-devel,请确保它与系统上当前安装的内核版本匹配。

yum install "kernel-devel-uname-r == $(uname -r)"


问题:安装时出现显示 No package。

[root@k8s ~]# yum install "kernel-devel-uname-r == $(uname -r)"
已加载插件:fastestmirror
Determining fastest mirrors
epel/x86_64/metalink | 5.8 kB 00:00:00

 * base: mirrors.aliyun.com
 * elrepo: mirrors.tuna.tsinghua.edu.cn
 * epel: mirrors.bfsu.edu.cn
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base | 3.6 kB 00:00:00
dell | 2.3 kB 00:00:00
docker-ce-stable | 3.5 kB 00:00:00
elrepo | 2.9 kB 00:00:00
Not using downloaded elrepo/repomd.xml because it is older than what we have:
  Current : Fri Sep 25 08:15:03 2020
  Downloaded: Fri Sep 25 06:15:03 2020
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
没有可用软件包 kernel-devel-uname-r == 4.4.236-1.el7.elrepo.x86_64。
错误:无须任何处理
[root@k8s ~]#


解决办法:启用[elrepo-kerne]存储库:

 vim /etc/yum.repos.d/elrepo.repo


九、内核升级后也可以使用 yum 安装 

[root@k8s ~]# yum install -y bcc-tools
[root@k8s ~]## echo 'export PATH=$PATH:/usr/share/bcc/tools' > /etc/profile.d/bcc-tools.sh
[root@k8s ~]# . /etc/profile.d/bcc-tools.sh


十、使用

[root@k8s ~]# cachestat 1 5
    HITS MISSES DIRTIES HITRATIO BUFFERS_MB CACHED_MB
     239 0 311 100.00% 0 34865
    7847 0 173 100.00% 0 34865
    5752 0 176 100.00% 0 34866
     141 0 190 100.00% 0 34866
    1425 0 230 100.00% 0 34866
[root@k8s ~]#


cachetop 显示如下:


内存泄漏的工具 memleak,可以跟踪系统或指定进程的内存分配、释放请求,然后定期输出一个未释放内存和相应调用栈的汇总情况(默认 5 秒)。

memleak -a -p <PID>


总结

提供两种安装方式,不过源码编译安装过程中,会出现问题,这里简单记录下,方便下次使用。bcc-tools,提供几个常用工具如:cachestat 系统缓存命中率查看工具、cachetop 进程缓存命中率查看工具、memleak 内存泄露检测工具。

文章转载自Linux点滴运维实践,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论