从 Linux 内核将数据附加到文件
我正在尝试收集 Linux 内核中特定系统调用(sys_clone)的周期计数测量值。也就是说,我的进程不会是唯一调用它的进程,而且我无法提前知道我的 pid;所以我必须记录每个 pid 的每次调用。
我遇到的问题是,我能弄清楚如何输出这些数据(debugfs、sysfs、procfs)的唯一方法涉及静态大小的缓冲区,这些缓冲区将很快被调用 sys_clone 的其他进程的不相关数据覆盖。
那么,有谁知道如何将任意数量的行附加到 Linux 中的用户空间可访问文件中?
I'm trying gather measurements of cycle counts for a particular sys call (sys_clone) in the linux kernel. That said, my process won't be the only one calling it and I can't know my pid ahead of time; so I'll have to record every invocation of it for every pid.
The problem that I've got is that the only ways I can figure out how to output this data (debugfs, sysfs, procfs) involve statically sized buffers, which will be quickly overwritten with irrelevant data from other processes calling sys_clone.
So, does anyone know how to append an arbitrary number of lines to a user space accessible file in linux?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以采用 printk()/klogd 方法,并使用通过 /proc 导出的循环缓冲区。用户空间进程在读取 /proc 文件时会阻塞,一旦读取到某些内容,就会从缓冲区中删除。事实上,您可以看看是否可以修改 klogd/syslogd 来读取您的 /proc 文件,这样您就不需要实现用户空间部分。
如果您擅长更简单的东西,只需以带有某些前缀的标准化形式 printk() 您的信息,然后使用此前缀从系统日志中过滤掉它。
还有一些可能性(例如使用 netlink 将消息发送到用户空间),但我不建议从内核写入文件。
You can take the printk()/klogd approach, and use a circular buffer that is exported via /proc. A user-space process blocks on reading your /proc file, and once it reads something that is removed from the buffer. In fact, you could take a look whether klogd/syslogd can be modified to also read your /proc file, thus you wouldn't need to implement the userspace part.
If you are good with something simpler, just printk() your information in a normalized form with some prefix, and then just filter it out from your syslog using this prefix.
There are a few more possibilities (e.g. using netlink to send messages to userspace), but writing to a file from the kernel is not something I'd recommend.
您可以将计数存储在右侧的
task_struct
中,并通过/proc//
中的每个进程文件使其可见。You could stash the counts in the right
task_struct
, and make it visible through a per-process file in/proc/<pid>/
.