Linux 上 inotify 监视的合理数量是多少?
我正在开发一个守护进程,它通过 inotify 监视文件事件,以在访问文件时触发各种类型的事件。 我读到,监视有点贵,因为内核存储了正在监视的每个文件的完整路径名。
多少块手表就太多了?
编辑:大多数情况下,我想知道..您是否见过明显的性能下降,如果是的话,有多少块手表发生了这种情况? 是的,我必须递归地监视/(但是它是一个最小的引导系统)。
I am working on a daemon that monitors file events via inotify to trigger various types of events when files are accessed. I have read that watches are a little expensive, because the Kernel is storing the full path name of every file being watched.
How many watches would be too many?
Edit: Mostly, I'm wondering .. have you ever seen a noticeable performance hit, if so, at how many watches did it happen? Yes, I have to monitor / recursively (however its a minimal bootstrapped system).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
据我所知,内核不存储路径名,而是存储索引节点。 尽管如此,在 32 位系统上每个 Watch 有 540 个字节。 64 位上的两倍。
我从 Lsyncd(也许你想看看?)知道有人拥有一百万块手表。 它只消耗一千兆字节的内存。
AFAIK the kernel isn't storing the pathname, but the inode. Nevertheless, there are 540 bytes per Watch on a 32bit system. Double as much on 64bit.
I know from Lsyncd (maybe you want to check that out?) people who have a million watches. It just eats a Gigabyte of memory.
/proc/sys/fs/inotify/max_user_watches
是当前每个用户的最大手表数量。从历史上看,内核将其默认为 8192,但考虑到许多 Linux 发行版都对其内核构建进行了相当多的定制,因此并非每个 Linux 系统都如此。 最近的内核更改 [1] 根据系统拥有的 RAM 动态选择范围 [8192, 1048576] 内的默认
max_user_watches
值。 (5.11 是包含此更改的第一个内核版本。)AFAICT,
root
可以将max_user_watches
更改为 2147483647 (231-1) 等任何值或以下,只要您确信自己有足够的 RAM 来支持该数量的手表即可。[1] https://github.com/torvalds/linux/commit/92890123749bafc317bbfacbe0a62ce08d78efb7
/proc/sys/fs/inotify/max_user_watches
is the current max number of watches per user.Historically, the kernel has defaulted this to 8192, but given that many Linux distros customize their kernel builds quite a bit, this may not be true on every Linux system. A recent kernel change [1] dynamically selects a default
max_user_watches
value in the range [8192, 1048576] based on how much RAM the system has. (5.11 is the first kernel release containing this change.)AFAICT,
root
can changemax_user_watches
to any value that's 2147483647 (231-1) or under, as long as you're confident you have enough RAM to support that number of watches.[1] https://github.com/torvalds/linux/commit/92890123749bafc317bbfacbe0a62ce08d78efb7
您可以通过阅读
/proc/sys/fs/inotify/max_user_instances
(inotify“对象”的最大数量)和/proc/sys/fs/inotify/max_user_watches
来查找系统限制code> (观看的最大文件数),所以如果超过这些数字,那就太多了;-) 观看的最大数量通常是几万或更高 - 在我的系统上,262143 - 这可能比你的还要多'除非您试图监视文件系统中的每个文件,否则您可能需要这样做,但您不应该这样做。 我想说的是,尽量不要使用超出您需要的 inotify 手表,并且不要担心它,除非您发现性能显着下降。You can find the system limits by reading
/proc/sys/fs/inotify/max_user_instances
(maximum number of inotify "objects") and/proc/sys/fs/inotify/max_user_watches
(maximum number of files watched), so if you exceed those numbers, it's too many ;-) The maximum number of watches is usually several tens of thousands or higher - on my system, 262143 - which is probably more than you'd ever need unless you're trying to watch every file in a file system, but you shouldn't be doing that. I would say, just try not to use more inotify watches than you need to, and don't worry about it unless you notice a significant decrease in performance.这取决于您有多少内存。
虽然 524288 是可以观看的最大文件数,但如果您处于内存特别有限的环境中,您可能希望降低该数字。 每个文件监视占用 540 字节(32 位)或 ~1kB(64 位),因此假设所有 524288 个监视都被消耗,导致上限约为 256MB(32 位)或 512MB(64 位) 。
It depends on how much ram you've got
While 524288 is the maximum number of files that can be watched, if you're in an environment that is particularly memory constrained, you may wish to lower the number. Each file watch takes up 540 bytes (32-bit) or ~1kB (64-bit), so assuming that all 524288 watches are consumed that results in an upper bound of around 256MB (32-bit) or 512MB (64-bit).
我的信息:
lsyncd 使用大约 130M 内存。
我使用 lsyncd 使某些目录与灾难恢复服务器保持同步。
主服务器上没有性能影响/损失。
My info:
lsyncd uses about 130M of memory.
I use lsyncd to keep some directories in sync with the disaster recovery server.
No performance hit/penalty on the main server.
1000 亿万亿可能太多了。 Kernel Korner - inotify 简介 提到“数千个手表”,所以至少这个数字不应该是一个问题。
100 billions trillions gazillions would be too many, probably. Kernel Korner - Intro to inotify mentions “thousands of watches” so at least that number should not be a problem.