prelink加速应用启动速度

linux prelink加速应用启动速度

前言

最近在研究性能调优的问题,本文记录在使用prelink时的一些问题
测试平台:ubuntu 16.04

prelink 在ubuntu 下的使用

  1. 编译生成位置无关的程序
    简单的示例代码如下
    main.cc
#include <iostream>  
int main(int argc, const char* argv[]) 
{  
    std::cout << "Hello, World!" << std::endl;  
    return 0;  
}

main.cc 的交叉编译

g++ main.cc -fPIC -o fpic-a.out

查看链接重定位的数量

LD_DEBUG=statistics ./fpic-a.out 
     51307:	
     51307:	runtime linker statistics:
     51307:	  total startup time in dynamic loader: 1554101 cycles
     51307:		    time needed for relocation: 1057733 cycles (68.0%)
     51307:	                 number of relocations: 2009
     51307:	      number of relocations from cache: 1200
     51307:	        number of relative relocations: 1947
     51307:		   time needed to load objects: 349376 cycles (22.4%)
Hello, World!
     51307:	
     51307:	runtime linker statistics:
     51307:	           final number of relocations: 2096
     51307:	final number of relocations from cache: 1200

从这里可以看出,final number of relocations: 2096

  1. 使用prelink
prelink --cache-file=./prelink.cache -h fpic-a.out -vm

输出报错

Laying out 5 libraries in virtual address space 0000003000000000-0000004000000000
Assigned virtual address space slots for libraries:
/lib64/ld-linux-x86-64.so.2                                  0000003000000000-0000003000227168
/lib/x86_64-linux-gnu/libc.so.6                              0000003000400000-00000030007c99a0
/lib/x86_64-linux-gnu/libgcc_s.so.1                          0000003000800000-0000003000a15910
/lib/x86_64-linux-gnu/libm.so.6                              0000003000c00000-0000003000f080f8
/usr/lib/x86_64-linux-gnu/libstdc++.so.6                     0000003001000000-0000003001381400
Prelinking /lib/x86_64-linux-gnu/ld-2.23.so
prelink: Could not set /lib/x86_64-linux-gnu/ld-2.23.so owner or mode: Operation not permitted
prelink: Could not prelink /lib/x86_64-linux-gnu/libc.so.6 because its dependency /lib64/ld-linux-x86-64.so.2 could not be prelinked
prelink: Could not prelink /lib/x86_64-linux-gnu/libgcc_s.so.1 because its dependency /lib/x86_64-linux-gnu/libc.so.6 could not be prelinked
prelink: Could not prelink /lib/x86_64-linux-gnu/libm.so.6 because its dependency /lib/x86_64-linux-gnu/libc.so.6 could not be prelinked
prelink: Could not prelink /usr/lib/x86_64-linux-gnu/libstdc++.so.6 because its dependency /lib/x86_64-linux-gnu/libm.so.6 could not be prelinked
prelink: Could not prelink fpic-a.out because its dependency /usr/lib/x86_64-linux-gnu/libstdc++.so.6 could not be prelinked

解决方法,加上sudo

prelink --cache-file=./prelink.cache -h fpic-a.out -vm
liu@ubuntu:~/work/test/prelink$ sudo prelink --cache-file=./prelink.cache -h fpic-a.out -vm
Laying out 5 libraries in virtual address space 0000003000000000-0000004000000000
Assigned virtual address space slots for libraries:
/lib64/ld-linux-x86-64.so.2                                  0000003000000000-0000003000227168
/lib/x86_64-linux-gnu/libc.so.6                              0000003000400000-00000030007c99a0
/lib/x86_64-linux-gnu/libgcc_s.so.1                          0000003000800000-0000003000a15910
/lib/x86_64-linux-gnu/libm.so.6                              0000003000c00000-0000003000f080f8
/usr/lib/x86_64-linux-gnu/libstdc++.so.6                     0000003001000000-0000003001381400
Prelinking /lib/x86_64-linux-gnu/ld-2.23.so
Prelinking /lib/x86_64-linux-gnu/libc-2.23.so
Prelinking /lib/x86_64-linux-gnu/libgcc_s.so.1
Prelinking /lib/x86_64-linux-gnu/libm-2.23.so
Prelinking /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
Prelinking /home/liu/work/test/prelink/fpic-a.out

上面运行prelink只是为了测试在ubuntu 16.04下能否正常使用,最终执行的命令如下

sudo prelink --cache-file=./prelink.cache -h fpic-a.out -vmRfi
Laying out 5 libraries in virtual address space 0000003000000000-0000004000000000
Random base 0x00000032c6c00000
Assigned virtual address space slots for libraries:
/lib64/ld-linux-x86-64.so.2                                  00000032c6c00000-00000032c6e27168
/lib/x86_64-linux-gnu/libc.so.6                              00000032c7000000-00000032c73c99a0
/lib/x86_64-linux-gnu/libgcc_s.so.1                          00000032c7400000-00000032c7615910
/lib/x86_64-linux-gnu/libm.so.6                              00000032c7800000-00000032c7b080f8
/usr/lib/x86_64-linux-gnu/libstdc++.so.6                     00000032c7c00000-00000032c7f81400
Prelinking /lib/x86_64-linux-gnu/ld-2.23.so
Prelinking /lib/x86_64-linux-gnu/libc-2.23.so
Prelinking /lib/x86_64-linux-gnu/libgcc_s.so.1
Prelinking /lib/x86_64-linux-gnu/libm-2.23.so
Prelinking /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
Prelinking /home/liu/work/test/prelink/fpic-a.out

ubuntu 20.04下使用prelink会报错

prelink: fpic-a.out: Could not parse `lookup 0x00007ffe67176000 0xffffffffffff42d0 -> 0x00007ffe67176000 0x0000000000000a10 /0 __vdso_clock_gettime'

运行

 LD_DEBUG=statistics ./fpic-a.out 
     63859:	
     63859:	runtime linker statistics:
     63859:	  total startup time in dynamic loader: 596270 cycles
     63859:		    time needed for relocation: 137904 cycles (23.1%)
     63859:	                 number of relocations: 3
     63859:	      number of relocations from cache: 61
     63859:	        number of relative relocations: 0
     63859:		   time needed to load objects: 300258 cycles (50.3%)
Hello, World!
     63859:	
     63859:	runtime linker statistics:
     63859:	           final number of relocations: 5
     63859:	final number of relocations from cache: 61

对比上述中发现final number of relocations 从原来的2096 变成5,
time needed for relocation: 137904 cycles 从原来的68% 变成了23.1%

结论

以上是prelink的简单使用,刚开始在ubunut20.04下一个简单的示例一直在报错,后面报到16.04的ubuntu下才正常。
从上面的对比可以看出差异,因为程序非常简单 看time 看不出差异

/usr/bin/time ./fpic-a.out 
Hello, World!
0.00user 0.00system 0:00.00elapsed ?%CPU (0avgtext+0avgdata 2788maxresident)k
0inputs+0outputs (0major+99minor)pagefaults 0swaps

注意time 与 /usr/bin/time 不同

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值