在进程退出之前,从/proc提取该进程的所有统计信息(Linux) [英] Extract all statistic of a process from /proc just before the process exit (Linux)
问题描述
我需要获取一个进程在其生命周期内的统计信息(io,网络).无论如何,是否有在进程退出之前从/proc获取那些信息的信息? Linux Kernel API?
I need to get some statistic(io, network) of a process during its lifetime. Is there anyway to get those information from /proc just before the process exit ? Linux Kernel API ?
推荐答案
wait4()
和struct rusage
在 child 进程终止后收集一些统计信息的一种简单方法是 wait4(2)
syscall,它可以填充 rusage
结构.
wait4()
and struct rusage
A simple way to gather some statistics after child process termination is wait4(2)
syscall, which can fill rusage
struct.
如果这还不够,您可以使用 ptrace(2)
在进程终止之前停止该进程:
If it's not enough, you can probably use ptrace(2)
to stop a process just before its termination:
PTRACE_O_TRACEEXIT (从Linux 2.5.60开始)
PTRACE_O_TRACEEXIT (since Linux 2.5.60)
在出口处停止追迹.跟踪器的waitpid(2)
将返回状态值,例如
Stop the tracee at exit. A waitpid(2)
by the tracer
will return a status value such that
status>>8 == (SIGTRAP | (PTRACE_EVENT_EXIT<<8))
可以使用以下方法检索被跟踪者的退出状态
PTRACE_GETEVENTMSG
.
The tracee's exit status can be retrieved with
PTRACE_GETEVENTMSG
.
在进程退出过程中,当 寄存器仍然可用,从而使示踪剂能够 查看退出发生在哪里,而正常退出 流程完成后,通知完成 退出.即使上下文可用,跟踪器 无法阻止此时发生退出.
The tracee is stopped early during process exit, when registers are still available, allowing the tracer to see where the exit occurred, whereas the normal exit notification is done after the process is finished exiting. Even though context is available, the tracer cannot prevent the exit from happening at this point.
当 waitpid(2)
报告该过程正在进行时要终止和停止,您可以从/proc
收集统计信息,但是我没有尝试过.
When waitpid(2)
will report that process is going to terminate and stopped, you can gather statistics from /proc
, but I didn't try this.
我所知道的最通用的解决方案是 KProbes
及其派生工具.您可能可以使用DTrace或SystemTap捕获sys_exit()
并收集统计信息.
The most generic solution I know is KProbes
and derivatives. You can probably use DTrace or SystemTap to trap sys_exit()
and gather statistics.
这篇关于在进程退出之前,从/proc提取该进程的所有统计信息(Linux)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!