当从正确的文件夹中搜索库文件时,我的应用程序当前会对可能的位置执行几个 lstat。现在我想知道这些 lstat(应用程序是 Web 服务,因此每秒可能会有数百个 lstat)是否总是导致额外的磁盘搜索,或者这些搜索是否被缓存了?
系统是 Linux(2.6 内核),带有基于 ext4 的文件系统。
PHP-CGI strace 的一部分:
PHP-CGI 跟踪:
lstat("/home/www/mydomain.com/inc/myLibrary/php/classes/myClass.php", 0x7fffa805a710) = -1 ENOENT (No such file or directory)
lstat("/usr/share/php/inc/myLibrary/php/classes/myClass.php", 0x7fffa805a710) = -1 ENOENT (No such file or directory)
getcwd("/home/www/mydomain.com/misc", 4096) = 32
lstat("/home/www/mydomain.com/misc/./inc/myLibrary/php/classes/myClass.php", 0x7fffa805a710) = -1 ENOENT (No such file or directory)
lstat("/home/www/mydomain.com/inc/inc/myLibrary/php/classes/myClass.php", 0x7fffa805a710) = -1 ENOENT (No such file or directory)
lstat("/home/www/mydomain.com/inc/myLibrary/php/classes/myClass.php", 0x7fffa805a660) = -1 ENOENT (No such file or directory)
lstat("/usr/share/php/inc/myLibrary/php/classes/myClass.php", 0x7fffa805a660) = -1 ENOENT (No such file or directory)
getcwd("/home/www/mydomain.com/misc", 4096) = 32
lstat("/home/www/mydomain.com/misc/./inc/myLibrary/php/classes/myClass.php", 0x7fffa805a660) = -1 ENOENT (No such file or directory)
lstat("/home/www/mydomain.com/inc/inc/myLibrary/php/classes/myClass.php", 0x7fffa805a660) = -1 ENOENT (No such file or directory)
getcwd("/home/www/mydomain.com/misc", 4096) = 32
lstat("/home/www/mydomain.com/misc/inc/myLibrary/php/classes/myClass.php", 0x7fffa805c7c0) = -1 ENOENT (No such file or directory)
lstat("/home/www/mydomain.com/misc/inc/myLibrary/php/classes", 0x7fffa805c600) = -1 ENOENT (No such file or directory)
lstat("/home/www/mydomain.com/misc/inc/myLibrary/php", 0x7fffa805c450) = -1 ENOENT (No such file or directory)
lstat("/home/www/mydomain.com/misc/inc/myLibrary", 0x7fffa805c2a0) = -1 ENOENT (No such file or directory)
lstat("/home/www/mydomain.com/misc/inc", 0x7fffa805c0f0) = -1 ENOENT (No such file or directory)
open("/home/www/mydomain.com/misc/inc/myLibrary/php/classes/myClass.php", O_RDONLY) = -1 ENOENT (No such file or directory)
/* FILE FOUND */
open("/home/www/mydomain.com/inc/classes/myClass.php", O_RDONLY) = 7
fstat(7, {st_mode=S_IFREG|0775, st_size=7734, ...}) = 0
fstat(7, {st_mode=S_IFREG|0775, st_size=7734, ...}) = 0
fstat(7, {st_mode=S_IFREG|0775, st_size=7734, ...}) = 0
fstat(7, {st_mode=S_IFREG|0775, st_size=7734, ...}) = 0
mmap(NULL, 7734, PROT_READ, MAP_SHARED, 7, 0) = 0x7f2225786000
stat("/home/www/mydomain.com/inc/classes/myClass.php", {st_mode=S_IFREG|0775, st_size=7734, ...}) = 0
答案1
无论文件是否存在,操作系统都需要这些信息。显然,它必须读取您要查找文件的所有父文件夹的文件夹列表。
但不要太担心。一旦读入,这些信息将保留在缓存中,因此它不必每次都在磁盘上搜索 - 只有当由于某种原因缓存的信息被删除时(也许操作系统需要内存中的这部分空间来存储更新的数据),操作系统才必须再次在磁盘上查找。