- 论坛徽章:
- 0
|
和内核沾点边,来这里求助了,
我想在用户层分别得到使用磁盘,cpu最高的几个进程(或者进程组), 不知道该如何实现,
linux下有个iotop能以类似top(1)的形式显示进程的I/O利用情况,但是那个软件是python写的,我读起来很(非常)吃力,
他大致是先在从proc读到进程列表,然后又读了 /proc/xxpid/status, 然后用netlink获得某些进程的taskstats
不清楚从proc pid status下他想读什么,貌似是读进程的uid,gid之类的东东
- // /usr/include/linux/taskstats.h
- struct taskstats {
- ...
- __u8 ac_flag; /* Record flags */
- __u8 ac_nice; /* task_nice */
- __u64 cpu_count __attribute__((aligned(8)));
- __u64 cpu_delay_total;
- /* Following four fields atomically updated using task->delays->lock */
- /* Delay waiting for synchronous block I/O to complete
- * does not account for delays in I/O submission
- */
- __u64 blkio_count;
- __u64 blkio_delay_total;
- /* Delay waiting for page fault I/O (swap in only) */
- __u64 swapin_count;
- __u64 swapin_delay_total;
- /* cpu "wall-clock" running time
- * On some architectures, value will adjust for cpu time stolen
- * from the kernel in involuntary waits due to virtualization.
- * Value is cumulative, in nanoseconds, without a corresponding count
- * and wraps around to zero silently on overflow
- */
- __u64 cpu_run_real_total;
- /* cpu "virtual" running time
- * Uses time intervals seen by the kernel i.e. no adjustment
- * for kernel's involuntary waits due to virtualization.
- * Value is cumulative, in nanoseconds, without a corresponding count
- * and wraps around to zero silently on overflow
- */
- __u64 cpu_run_virtual_total;
- /* Delay accounting fields end */
- /* version 1 ends here */
- /* Basic Accounting Fields start */
- char ac_comm[TS_COMM_LEN]; /* Command name */
- __u8 ac_sched __attribute__((aligned(8)));
- /* Scheduling discipline */
- __u8 ac_pad[3];
- __u32 ac_uid __attribute__((aligned(8)));
- /* User ID */
- __u32 ac_gid; /* Group ID */
- __u32 ac_pid; /* Process ID */
- __u32 ac_ppid; /* Parent process ID */
- __u32 ac_btime; /* Begin time [sec since 1970] */
- __u64 ac_etime __attribute__((aligned(8)));
- /* Elapsed time [usec] */
- __u64 ac_utime; /* User CPU time [usec] */
- __u64 ac_stime; /* SYstem CPU time [usec] */
- __u64 ac_minflt; /* Minor Page Fault Count */
- __u64 ac_majflt; /* Major Page Fault Count */
- ...
- /* The following four fields are I/O statistics of a task. */
- __u64 read_char; /* bytes read */
- __u64 write_char; /* bytes written */
- __u64 read_syscalls; /* read syscalls */
- __u64 write_syscalls; /* write syscalls */
- /* Extended accounting fields end */
- #define TASKSTATS_HAS_IO_ACCOUNTING
- /* Per-task storage I/O accounting starts */
- __u64 read_bytes; /* bytes of read I/O */
- __u64 write_bytes; /* bytes of write I/O */
- __u64 cancelled_write_bytes; /* bytes of cancelled write I/O */
- __u64 nvcsw; /* voluntary_ctxt_switches */
- __u64 nivcsw; /* nonvoluntary_ctxt_switches */
- /* time accounting for SMT machines */
- __u64 ac_utimescaled; /* utime scaled on frequency etc */
- __u64 ac_stimescaled; /* stime scaled on frequency etc */
- __u64 cpu_scaled_run_real_total; /* scaled cpu_run_real_total */
- };
复制代码
貌似和磁盘io有关的是
- #define TASKSTATS_HAS_IO_ACCOUNTING
- /* Per-task storage I/O accounting starts */
- __u64 read_bytes; /* bytes of read I/O */
- __u64 write_bytes; /* bytes of write I/O */
- __u64 cancelled_write_bytes; /* bytes of cancelled write I/O */ 这个如何理解呢???
复制代码
cancelled_write_bytes是怎么一回事?
另外和cpu利用率有关的是哪些成员? 最后说的SMT又是什么意思呢?
要是想得到利用率的话是不是需要读取两次相减再除以时间间隔?还是有什么别的直接的办法?
这里面讲的task和process的区别是什么呢? task带表进程组 process带表进程?
另外再请问有没有除proc外的其他接口直接获得所有进程号的呢?
问了好多问题,希望大家不吝赐教
谢谢!
http://kernelnewbies.org/Linux_2 ... ed22c281ca9ab54bb12
IO Accounting
The present per-task IO accounting isn't very useful. It simply counts the number of bytes passed into read() and write(). So if a process reads 1MB from an already-cached file, it is accused of having performed 1MB of I/O, which is 'wrong'. So this IO accounting implements per-process statistics of "storage I/O" (i.e.: I/O that _really_ does I/O on the storage device - Linux already had I/O storage statistics but it's not per-task). The data is reported through taskstats and procfs (/proc/$PID/io) (commit 1, 2, 3, 4, 5, 6, 7, 8, 10, 11)
最后一句话说的proc/$id/io是说的新的io accountting还是前面说的present per-task IO accounting ?
如果是新的的话就可以直接读proc下的pid下的io了,比netlink更方便, 不知道效率上是不是会比netlink差 |
|