LXR | KVM | PM | Time | Interrupt | Systems Performance | Bootup Optimization

Linux线程proc信息解析

 每个线程对应的/proc/xxx有助于分析线程的各种资源占用情况。之前零零散散做了一些分析,这里总结记录一下。

/proc/183/
|-- auxv
|-- cgroup
|-- clear_refs
|-- cmdline--执行进程的命令行,包括启动参数等等。
|-- comm--进程名称。
|-- coredump_filter--参考《/proc/<pid>/coredump_filter》。
|-- cpuset
|-- cwd -> /--指向当前工作目录。
|-- environ--当前线程执行的环境变量。
|-- exe -> /bin/busybox--可执行文件路径。
|-- fd--打开的文件句柄。
|   |-- 0 -> socket:[283]
|   |-- 1 -> /dev/null
|   |-- 2 -> /dev/null
|   `-- 3 -> /tmp/messages
|-- fdinfo--打开的文件句柄信息:flags和mnt_id。
|   |-- 0
|   |-- 1
|   |-- 2
|   `-- 3
|-- gid_map--user namespace的group id映射。
|-- io--统计当前线程产生的存储器IO统计信息,需要打开CONFIG_TASK_IO_ACCOUNTING。参考《/proc/<pid>/io简析|-- limits--参考《/proc/<pid>/limits》。
|-- map_files--映射到线程内存空间的文件。
|   |-- 449000-50e000 -> /bin/busybox
|   |-- 51e000-520000 -> /bin/busybox
|   |-- 520000-521000 -> /bin/busybox
|   |-- b6f13000-b6f98000 -> /lib/libuClibc-1.0.41.so
|   |-- b6fa7000-b6fa8000 -> /lib/libuClibc-1.0.41.so
|   |-- b6fa8000-b6fa9000 -> /lib/libuClibc-1.0.41.so
|   |-- b6fc0000-b6fc6000 -> /lib/ld-uClibc-1.0.41.so
|   |-- b6fd5000-b6fd6000 -> /lib/ld-uClibc-1.0.41.so
|   `-- b6fd6000-b6fd7000 -> /lib/ld-uClibc-1.0.41.so
|-- maps--参考《/proc/<pid>/maps简要分析》。
|-- mem
|-- mountinfo--共三个都显示挂载信息,mountinfo更详细。
|-- mounts
|-- mountstats
|-- net--网络相关统计消息。
|   |-- arp
|   |-- can
|   |   |-- rcvlist_all
|   |   |-- rcvlist_eff
|   |   |-- rcvlist_err
|   |   |-- rcvlist_fil
|   |   |-- rcvlist_inv
|   |   |-- rcvlist_sff
|   |   |-- reset_stats
|   |   |-- stats
|   |   `-- version
|   |-- can-bcm
|   |-- dev
|   |-- dev_mcast
|   |-- fib_trie
|   |-- fib_triestat
|   |-- hci
|   |-- icmp
|   |-- igmp
|   |-- ip_mr_cache
|   |-- ip_mr_vif
|   |-- ipconfig
|   |   `-- ntp_servers
|   |-- l2cap
|   |-- mcfilter
|   |-- netfilter
|   |   `-- nf_log
|   |-- netlink
|   |-- netstat
|   |-- nfsfs
|   |   |-- servers
|   |   `-- volumes
|   |-- packet
|   |-- pnp
|   |-- pppoe
|   |-- protocols
|   |-- psched
|   |-- ptype
|   |-- raw
|   |-- rfcomm
|   |-- route
|   |-- rpc
|   |   |-- auth.rpcsec.context
|   |   |   |-- channel
|   |   |   `-- flush
|   |   |-- auth.rpcsec.init
|   |   |   |-- channel
|   |   |   `-- flush
|   |   |-- auth.unix.gid
|   |   |   |-- channel
|   |   |   |-- content
|   |   |   `-- flush
|   |   |-- auth.unix.ip
|   |   |   |-- channel
|   |   |   |-- content
|   |   |   `-- flush
|   |   |-- nfs
|   |   |-- nfs4.idtoname
|   |   |   |-- channel
|   |   |   |-- content
|   |   |   `-- flush
|   |   |-- nfs4.nametoid
|   |   |   |-- channel
|   |   |   |-- content
|   |   |   `-- flush
|   |   |-- nfsd
|   |   |-- nfsd.export
|   |   |   |-- channel
|   |   |   |-- content
|   |   |   `-- flush
|   |   |-- nfsd.fh
|   |   |   |-- channel
|   |   |   |-- content
|   |   |   `-- flush
|   |   `-- use-gss-proxy
|   |-- rt_acct
|   |-- rt_cache
|   |-- sco
|   |-- snmp
|   |-- sockstat
|   |-- softnet_stat
|   |-- stat
|   |   |-- arp_cache
|   |   `-- rt_cache
|   |-- tcp
|   |-- udp
|   |-- udplite
|   |-- unix
|   `-- wireless
|-- ns
|   |-- cgroup -> cgroup:[4026531835]
|   |-- ipc -> ipc:[4026531839]
|   |-- mnt -> mnt:[4026531840]
|   |-- net -> net:[4026531897]
|   |-- pid -> pid:[4026531836]
|   |-- pid_for_children -> pid:[4026531836]
|   |-- user -> user:[4026531837]
|   `-- uts -> uts:[4026531838]
|-- oom_adj--OOM参考《/proc/<pid>/oom_xxx》。
|-- oom_score
|-- oom_score_adj
|-- pagemap
|-- personality--显示当前系统一些属性,参考《personality.h - include/uapi/linux/personality.h》。
|-- projid_map
|-- root -> /
|-- schedstat--调度统计信息,分别为:进程从启动开始的总运行时间、在runqueue等待时间、当前CPU上运行时间。
|-- setgroups--是否允许超级用户设置组ID表。
|-- smaps--参考《/proc/<pid>/smaps解析》。
|-- smaps_rollup--是对smaps的统计整合。
|-- stack--当前线程的栈信息。
|-- stat--显示进程统计信息,参考《do_task_stat()》。
|-- statm--内存统计信息,参考《statm)》。
|-- status--参考《/proc/<pid>/status简要分析》。
|-- syscall
|-- task--子进程信息。
|   `-- 183
|       |-- auxv
...
|       `-- wchan
|-- timers--线程的计时器列表。
|-- timerslack_ns--进程当前计时器差值。
|-- uid_map--user namespace的user id映射。
`-- wchan--进程在内核中睡眠位置符号名称。

1 /proc/<pid>/io简析

进程的io由do_io_accounting()输出:

struct task_io_accounting {
#ifdef CONFIG_TASK_XACCT
    u64 rchar;--总读字节数,不完全是存储器读。
    u64 wchar;--总写字节数,不完全是存储器写。
    u64 syscr;--总read系统调用次数。
    u64 syscw;--总write系统调用次数。
#endif /* CONFIG_TASK_XACCT */

#ifdef CONFIG_TASK_IO_ACCOUNTING
    u64 read_bytes;--当前进程导致的从存储器读字节数。
    u64 write_bytes;--当前进程导致的到存储器的写字节数。
    u64 cancelled_write_bytes;--由于截断pagecache导致应该发生而没有发生的写入字节数。
#endif /* CONFIG_TASK_IO_ACCOUNTING */
};

static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
{
    struct task_io_accounting acct = task->ioac;
    unsigned long flags;
    int result;
...
    if (whole && lock_task_sighand(task, &flags)) {--更新当前线程的ioac。
        struct task_struct *t = task;

        task_io_accounting_add(&acct, &task->signal->ioac);
        while_each_thread(task, t)
            task_io_accounting_add(&acct, &t->ioac);

        unlock_task_sighand(task, &flags);
    }
    seq_printf(m,--打印ioac成员,参照struct task_io_accounting。
           "rchar: %llu\n"
           "wchar: %llu\n"
           "syscr: %llu\n"
           "syscw: %llu\n"
           "read_bytes: %llu\n"
           "write_bytes: %llu\n"
           "cancelled_write_bytes: %llu\n",
           (unsigned long long)acct.rchar,
           (unsigned long long)acct.wchar,
           (unsigned long long)acct.syscr,
           (unsigned long long)acct.syscw,
           (unsigned long long)acct.read_bytes,
           (unsigned long long)acct.write_bytes,
           (unsigned long long)acct.cancelled_write_bytes);
    result = 0;

out_unlock:
    mutex_unlock(&task->signal->cred_guard_mutex);
    return result;
}

一个实例:

rchar: 1088190
wchar: 1360495
syscr: 11101
syscw: 11097
read_bytes: 0
write_bytes: 0
cancelled_write_bytes: 0

2 /proc/<pid>/limits

Soft Limit和Hard Limit区别是:超过Soft Limit则发送SIGXCPU信号;超过Hard Limit命令则发送SIGKILL。

Limit                     Soft Limit           Hard Limit           Units
Max cpu time              unlimited            unlimited            seconds
Max file size             unlimited            unlimited            bytes
Max data size             unlimited            unlimited            bytes
Max stack size            8388608              unlimited            bytes
Max core file size        0                    unlimited            bytes
Max resident set          unlimited            unlimited            bytes
Max processes             5923                 5923                 processes
Max open files            1024                 4096                 files
Max locked memory         65536                65536                bytes
Max address space         unlimited            unlimited            bytes
Max file locks            unlimited            unlimited            locks
Max pending signals       5923                 5923                 signals
Max msgqueue size         819200               819200               bytes
Max nice priority         0                    0
Max realtime priority     0                    0
Max realtime timeout      unlimited            unlimited            us

更多关于limits参考《ulimit功能以及如何在内核中生效》。

3 /proc/<pid>/mountinfo

以第2条输出为例:mnt_id(20)、parent mnt_id(19)、主设备号(0)、子设备号(6)、挂载点(/dev)、挂载标志(rw,relatime)、设备名称(devtmpfs)、文件系统类型(devtmpfs)、读写等属性(rw,size=379096k,nr_inodes=94774,mode=755)。

19 1 179:5 / / rw,relatime - ext4 /dev/root rw
20 19 0:6 / /dev rw,relatime - devtmpfs devtmpfs rw,size=379096k,nr_inodes=94774,mode=755
21 19 0:5 / /proc rw,relatime - proc proc rw
22 20 0:18 / /dev/pts rw,relatime - devpts devpts rw,gid=5,mode=620,ptmxmode=666
23 20 0:19 / /dev/shm rw,relatime - tmpfs tmpfs rw,mode=777
24 19 0:20 / /tmp rw,relatime - tmpfs tmpfs rw
25 19 0:21 / /run rw,nosuid,nodev,relatime - tmpfs tmpfs rw,mode=755
26 19 0:22 / /sys rw,relatime - sysfs sysfs rw

 

posted on 2024-02-06 23:59  ArnoldLu  阅读(332)  评论(0编辑  收藏  举报

导航