linux对进程的读写量统计

发布于:2023-03-12 ⋅ 阅读:(164) ⋅ 点赞:(0)

struct task_struct有struct task_io_accounting ioac;

struct task_io_accounting {
#ifdef CONFIG_TASK_XACCT
    /* bytes read */
    u64 rchar;
    /*  bytes written */
    u64 wchar;
    /* # of read syscalls */
    u64 syscr;
    /* # of write syscalls */
    u64 syscw;
#endif /* CONFIG_TASK_XACCT */

#ifdef CONFIG_TASK_IO_ACCOUNTING
    /*
     * The number of bytes which this task has caused to be read from
     * storage.
     */
    u64 read_bytes;

    /*
     * The number of bytes which this task has caused, or shall cause to be
     * written to disk.
     */
    u64 write_bytes;

    /*
     * A task can cause "negative" IO too.  If this task truncates some
     * dirty pagecache, some IO which another task has been accounted for
     * (in its write_bytes) will not be happening.  We _could_ just
     * subtract that from the truncating task's write_bytes, but there is
     * information loss in doing that.
     */
    u64 cancelled_write_bytes;
#endif /* CONFIG_TASK_IO_ACCOUNTING */
};

每个task_struct内嵌的write_bytes,统计是该进程写内存的字节数,这个写的,当前时刻可能还在内存里面,不一定写到磁盘里了。

void __set_page_dirty(struct page *page, struct address_space *mapping,
                 int warn)
{
    unsigned long flags;

    xa_lock_irqsave(&mapping->i_pages, flags);
    if (page->mapping) {    /* Race with truncate? */
        WARN_ON_ONCE(warn && !PageUptodate(page));
        account_page_dirtied(page, mapping);
        __xa_set_mark(&mapping->i_pages, page_index(page),
                PAGECACHE_TAG_DIRTY);
    }
    xa_unlock_irqrestore(&mapping->i_pages, flags);
}
static void account_page_dirtied(struct page *page,
        struct address_space *mapping)
{
    struct inode *inode = mapping->host;

    trace_writeback_dirty_page(page, mapping);

    if (mapping_can_writeback(mapping)) {
        struct bdi_writeback *wb;

        inode_attach_wb(inode, page);
        wb = inode_to_wb(inode);

        __inc_lruvec_page_state(page, NR_FILE_DIRTY);
        __inc_zone_page_state(page, NR_ZONE_WRITE_PENDING);
        __inc_node_page_state(page, NR_DIRTIED);
        inc_wb_stat(wb, WB_RECLAIMABLE);
        inc_wb_stat(wb, WB_DIRTIED);
        task_io_account_write(PAGE_SIZE);
        current->nr_dirtied++;
        __this_cpu_inc(bdp_ratelimits);

        mem_cgroup_track_foreign_dirty(page, wb);
    }
}
static inline void task_io_account_write(size_t bytes)
{
    current->ioac.write_bytes += bytes;
}
本文含有隐藏内容,请 开通VIP 后查看