prelink加速应用启动速度

linux prelink加速应用启动速度

目录

  • linux prelink加速应用启动速度
    • 前言
    • prelink 在ubuntu 下的使用
    • 结论

前言

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

prelink 在ubuntu 下的使用

  1. 编译生成位置无关的程序
    简单的示例代码如下
    main.cc
#include   
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 不同

你可能感兴趣的:(linux,linux)