对于我系统上的某个目录 DIR ls --color=always
,虽然它包含的文件和子目录少于 10 个,但大约需要 8 秒。没有颜色参数,它不需要时间。
为什么ls
颜色参数需要这么长时间,我怎样才能找出到底是什么需要这么长时间?可能是挂载了 DIR 中的某个子目录,但是我如何找出哪个是麻烦制造者?
他们只是在我的工作中禁用了服务器上的颜色。根据这个博客:https : //web.archive.org/web/20160410082228/http : //www.techper.net/2011/01/25/ls-command-slow-on-very-large-directories/
这可能是由于在特定目录中的所有不同挂载上调用了 stat() 函数以获取颜色显示的信息......
很容易确认这一点:
time command ls /dir/with/many/toplevel/entries/ >/dev/null
time $SHELL -c "ls --color=always /dir/with/many/toplevel/entries/ >/dev/null"
Run Code Online (Sandbox Code Playgroud)
对于我创建的某个有问题的目录结构,第一个命令给出:
real 0m0.523s
user 0m0.284s
sys 0m0.052s
Run Code Online (Sandbox Code Playgroud)
第二个:
real 1m47.799s
user 0m0.360s
sys 0m0.928s
Run Code Online (Sandbox Code Playgroud)
请记住,如果您重复底部的“基准”,其第二次运行将 stat() 数据已经在缓存中。彩色输出的第二次运行给了我:
real 0m0.409s
user 0m0.256s
sys 0m0.120s
Run Code Online (Sandbox Code Playgroud)
我无法完全清除缓存以确保可以重现“超过 90 秒”的结果。/sf/ask/41980361/中vm.drop_caches
描述的sysctl不足。