V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
asanelder
V2EX  ›  程序员

System limit for number of file watchers reached, 我想知道当前打开了多少 file watchers

  •  
  •   asanelder · 2020-05-26 22:53:16 +08:00 · 1345 次点击
    这是一个创建于 1432 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在运行 node 程序时 stderr 报以上错误。

    俺知道如何修改内核参数。

    但就是想知道系统当前打开了多少的 file watchers. 俺想知道是哪些程序在大量使用 file watchers

    google 和百度都没找到答案。

    于是求助于此

    14 条回复    2020-05-27 00:38:40 +08:00
    ManjusakaL
        1
    ManjusakaL  
       2020-05-26 23:26:22 +08:00 via Android   ❤️ 2
    lsof | grep inotify | wc -l
    asanelder
        2
    asanelder  
    OP
       2020-05-26 23:30:05 +08:00
    @ManjusakaL 这个俺在 google 查到过,就是不明白原理。

    俺系统的 max_user_watchers = 8192,

    但使用 lsof | grep inotify | wc -l 的结果是 101,还有 8000 多可以用啊!

    为啥就报错了呢?

    对这一块不熟悉啊,还望详细指教
    ManjusakaL
        3
    ManjusakaL  
       2020-05-26 23:31:07 +08:00 via Android
    Google 搜索 get Linux file watcher number 第一个就是
    里面还有人提供了脚本直接跑
    himself65
        4
    himself65  
       2020-05-26 23:32:06 +08:00 via iPad
    看这行代码

    https://github.com/nodejs/node/blob/9949a2e1e3100c4ff1f228bac57c1af95cdc3e9d/lib/fs.js#L1458

    这个东西貌似没有 expose 出来,你提交个 issue 看看 nodejs member 什么看法
    ManjusakaL
        5
    ManjusakaL  
       2020-05-26 23:38:23 +08:00 via Android
    我不太清楚你的库是啥,但是我猜是做了严格的限制导致的,按道理说应该是给一个 warning

    而 inotify 不支持递归监听,所以递归监听一个 N 级目录需要 N 个描述符,而如果监听 N 级目录中总共 M 个文件需要 N*M 个描述符,所以我猜这是它做严格限制的原因

    /proc/sys/fs/inotify/max_user_watches 限制每个 User ID 能够创建的 watcher 数,以免监听过多导致内存爆炸

    https://manjusaka.itscoder.com/posts/2019/07/02/something-about-file-system-watch/

    具体可以看我这篇文章
    ManjusakaL
        6
    ManjusakaL  
       2020-05-26 23:42:53 +08:00 via Android
    @ManjusakaL 更正一下,描述符写错了,是 watcher,具体我不太熟悉 Node,估计还得 @himself65 看看
    asanelder
        7
    asanelder  
    OP
       2020-05-26 23:44:14 +08:00
    @ManjusakaL @himself65 感谢二位回复,俺自己再看看,辛苦辛苦
    himself65
        8
    himself65  
       2020-05-26 23:45:19 +08:00 via iPad
    仔细考虑了一下,nodejs 支持监听这个好像没有什么意义。毕竟 fs watcher 是系统调用,nodejs 只是 caller
    himself65
        9
    himself65  
       2020-05-26 23:47:45 +08:00 via iPad
    我才发现 1L 是 manjusaka 大佬!我献丑了
    asanelder
        10
    asanelder  
    OP
       2020-05-26 23:50:12 +08:00
    @himself65 这个应该是关于系统调用相关的知识,但俺对 inotify 是很不熟悉的,所以想问个有没有人了解。
    asanelder
        11
    asanelder  
    OP
       2020-05-26 23:51:28 +08:00
    @ManjusakaL 原来是大佬!在下有眼无珠,失敬失敬!
    himself65
        12
    himself65  
       2020-05-26 23:59:58 +08:00 via iPad
    我提交了一个 issue,能够监听 watchFile 的 API,这样就可以跟踪那些代码调用多次 watch

    https://github.com/nodejs/node/issues/33569
    asanelder
        13
    asanelder  
    OP
       2020-05-27 00:03:14 +08:00
    @himself65 666,铁汁早点休息
    asanelder
        14
    asanelder  
    OP
       2020-05-27 00:38:40 +08:00
    @ManjusakaL @himself65 说的没错,可以使用
    https://github.com/fatso83/dotfiles/blob/master/utils/scripts/inotify-consumers

    这个脚本来查看某一进程到底消费了多少 inotify watcher
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1010 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 19:09 · PVG 03:09 · LAX 12:09 · JFK 15:09
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.