Binary HacksとprelinkとPythonとわたし

prelink-test

Binary Hacks ―ハッカー秘伝のテクニック100選を読み始めました。

前から気になっていたprelinkについて書かれていましたので、通勤前に試してみました。

prelinkは共有ライブラリをリンクしたプログラムの起動時間を短縮してくれるツールのようです。
書籍ではRubyスクリプトですが、(それほど難しい処理とかはないですが)Pythonの練習をかねてPythonで書き直してみました。

ダイナミックリンクで10倍くらいは速くなってますね。

% time python prelink-test.py                                           [~/py/prelinktest]
00000.c
00001.c
  :
  :
00999.c
python prelink-test.py  198.43s user 114.05s system 99% cpu 5:13.84 total
% repeat 3 time ./test-dynamic                                          [~/py/prelinktest]
./test-dynamic  11.46s user 0.24s system 99% cpu 11.724 total
./test-dynamic  11.15s user 0.20s system 99% cpu 11.353 total
./test-dynamic  11.14s user 0.22s system 99% cpu 11.389 total
% time /usr/sbin/prelink -N ./test-dynamic *.so                         [~/py/prelinktest]
/usr/sbin/prelink -N ./test-dynamic *.so  18.64s user 5.75s system 85% cpu 28.512 total
% repeat 3 time ./test-dynamic                                          [~/py/prelinktest]
./test-dynamic  1.26s user 0.22s system 97% cpu 1.514 total
./test-dynamic  0.95s user 0.21s system 99% cpu 1.166 total
./test-dynamic  0.98s user 0.19s system 100% cpu 1.172 total
% time ./test-static                                                    [~/py/prelinktest]
./test-static  0.00s user 0.00s system 76% cpu 0.005 total
% uname -a                                                              [~/py/prelinktest]
Linux zeus 2.6.20-3.ydl.2 #1 SMP Sun Mar 25 12:14:58 EDT 2007 ppc ppc ppc GNU/Linux
% cat /proc/cpuinfo                                                     [~/py/prelinktest]
processor       : 0
cpu             : 7410, altivec supported
temperature     : 1-3 C (uncalibrated)
clock           : 300.000000MHz
revision        : 0.2 (pvr 800c 1102)
bogomips        : 49.79

total bogomips  : 49.79
timebase        : 24966218
platform        : PowerMac
machine         : PowerBook3,2
motherboard     : PowerBook3,2 MacRISC2 MacRISC Power Macintosh
detected as     : 71 (PowerBook Titanium)
pmac flags      : 0000001b
L2 cache        : 1024K unified
pmac-generation : NewWorld
%                                                                       [~/py/prelinktest]
### prelink-test.py
import os

os.system("rm -f *.c *.so")
f = open("test.c", "w")
f.write('int main() { return 0; }')

objs = []
dsos = []

for i in xrange(1000):
    c_file_name = "%05d.c" % i
    print c_file_name
    f = open(c_file_name, "w")
    f.write("int func%05d() { return 0; }\n" % i)

    obj_file_name = "%05d.o" % i
    dso_file_name = "%05d.so" % i
    os.system("gcc -c %s" % c_file_name)
    os.system("gcc -fPIC -shared -o %s %s" % (dso_file_name, c_file_name))
    objs.append(obj_file_name)
    dsos.append("./" + dso_file_name)

os.system("gcc -o test-static test.c %s" % " ".join(objs))
os.system("gcc -o test-dynamic test.c %s" % " ".join(dsos))