加快linux程序加载速度

1. 查看系统启动的时间消耗:

# env LD_DEBUG=statistics LD_DEBUG_OUTPUT=debugfile /usr/bin/ls

# cat dbugfile.104876

    104876:

    104876: runtime linker statistics:

    104876:   total startup time in dynamic loader: 333159 cycles

    104876:             time needed for relocation: 77891 cycles (23.3%)

    104876:                 number of relocations: 248

    104876:       number of relocations from cache: 3

    104876:         number of relative relocations: 1558

    104876:           time needed to load objects: 197597 cycles (59.3%)

    104876:

    104876: runtime linker statistics:

    104876:           final number of relocations: 264

    104876: final number of relocations from cache: 3


2. 查看程序依赖的动态库

# ldd /usr/bin/ls

linux-vdso.so.1 (0x00007fff5dbbc000)

libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007ffb0ab1c000)

libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ffb0a92a000)

libpcre2-8.so.0 => /lib/x86_64-linux-gnu/libpcre2-8.so.0 (0x00007ffb0a89a000)

libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007ffb0a894000)

/lib64/ld-linux-x86-64.so.2 (0x00007ffb0ab84000)

libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007ffb0a871000)

3. 然后可以考虑使用prelink工具,在prelink.conf中指定预加载的动态库

# prelink -a

预链接之后,在坚持程序启动的耗时。

这个方法对启动时加载的动态库很多、很大时,效果明显。

缺点:

动态库变更后,要重写执行预加载的操作。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容