C

如何在應用程序啟動時避免多次“libFoo.so”open() 呼叫?

  • November 21, 2018

我在*/UNI/System/Libs/libmbedcrypto.so.3目錄中有下一個 so 文件。當我啟動我的應用程序時,使用libmbedcrypto.so.3*和strace我看到:

open("/UNI/System/Libs/tls/v7l/neon/vfp/libmbedcrypto.so.3", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat64("/UNI/System/Libs/tls/v7l/neon/vfp", 0x7ef80610) = -1 ENOENT (No such file or directory)
open("/UNI/System/Libs/tls/v7l/neon/libmbedcrypto.so.3", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat64("/UNI/System/Libs/tls/v7l/neon", 0x7ef80610) = -1 ENOENT (No such file or directory)
open("/UNI/System/Libs/tls/v7l/vfp/libmbedcrypto.so.3", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat64("/UNI/System/Libs/tls/v7l/vfp", 0x7ef80610) = -1 ENOENT (No such file or directory)
open("/UNI/System/Libs/tls/v7l/libmbedcrypto.so.3", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)

//...   more stat64 & open

open("/UNI/System/Libs/libmbedcrypto.so.3", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat64("/UNI/System/Libs", {st_mode=S_IFDIR|0777, st_size=4096, ...}) = 0 

如何擺脫所有 open 和 stat64 呼叫?

**這很正常。**如果不從頭開始在彙編中重寫程序,或者編寫自己的 C 庫來配合它,您實際上無法擺脫這種情況。這幾乎是任何程序的標準。說真的,你不需要優化像這樣無關緊要的東西。嘗試訪問不存在的文件所浪費的時間可以忽略不計,正如您在下面的系統呼叫跟踪中看到的那樣true,該程序旨在從嵌入式系統返回 0:

root@UP-1044:~# strace -T -e trace=open true
open("/tmp/t/usr/lib/libgcc_s.so.1", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000643>
open("/tmp/t/lib/libgcc_s.so.1", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = -1 ENOENT (No such file or directory) <0.000861>
open("/lib/libgcc_s.so.1", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = 3 <0.000208>
+++ exited with 0 +++

在每個系統呼叫上浪費的時間似乎不到一毫秒!

引用自:https://unix.stackexchange.com/questions/483123