/proc/{pid}/stat 文件说明

在 Linux 系统中,/proc 是一个虚拟文件系统,它提供了大量关于系统和正在运行的进程的信息。/proc/[pid]/stat 是其中一个文件,它提供了关于指定进程 ID(PID)的详细状态信息。

/proc/{pid}/stat

/proc/[pid]/stat 文件包含了进程的许多详细信息,这些信息对于系统管理员和开发者在调试、性能分析或系统监控时非常有用。

以下是 /proc/[pid]/stat 文件中一些字段的简要说明:

  • pid:进程的唯一标识符(PID)。
  • comm:进程的命令名(通常是程序的基名)。
  • state:进程的状态,例如 R(运行)、S(睡眠)、D(不可中断的睡眠)等。
  • ppid:父进程的 PID。
  • pgrp:进程的进程组 ID。
  • session:进程所在的会话 ID。
  • tty_nr:控制进程的终端设备的编号。
  • tpgid:前台进程组的 ID。
  • flags:进程的标志,表示进程的状态和行为。
  • minfltmajflt:进程发生的页面错误的次数(次要和主要)。
  • cminfltcmajflt:子进程发生的页面错误的次数(次要和主要)。
  • utimestime:进程在用户模式和内核模式下花费的 CPU 时间。
  • cutimecstime:子进程在用户模式和内核模式下花费的 CPU 时间。
  • prioritynice:进程的优先级和 nice 值。
  • num_threads:进程中的线程数。
  • itrealvaluestimerealvalue:进程的时间片长度和累积的睡眠时间。
  • start_time:进程的启动时间(自系统启动以来的时钟滴答数)。
  • vsize:进程的虚拟内存大小。
  • rss:进程使用的驻留集大小。
  • rsslim:进程的驻留集大小限制。
  • start_codeend_code:进程代码段的地址范围。
  • start_stackkstkesp:进程堆栈的地址范围。
  • kstkeip:进程指令指针的地址。
  • signalblockedsigignoresigcatch:进程的信号相关信息。
  • wchan:进程在等待的通道或资源的地址。
  • nswap:进程交换出的页面数。
  • cnswap:子进程交换出的页面数。
  • exit_signal:进程终止时的信号。
  • processor:执行进程的 CPU 编号。
  • rt_priority:进程的实时优先级。
  • policy:进程的调度策略。
  • delayacct_blkio_ticks:进程在块 I/O 上花费的延迟时间(已废弃)。
  • guest_timecguest_time:进程在虚拟机中花费的 CPU 时间。

要获取关于特定系统上 /proc/[pid]/stat 文件的详细信息,您可以查看 man 5 proc 手册页。

查询进程使用的内存大小

在 Linux 中,你可以使用多种命令来查询进程使用的内存大小。以下是一些常用的方法:

  1. 使用 /proc/[pid]/status 文件
    每个进程在 /proc 目录下都有一个与其 PID 对应的子目录,其中包含了一个名为 status 的文件。这个文件包含了进程的详细信息,包括内存使用情况。

    cat /proc/[pid]/status | grep Vm

    这将显示一系列以 Vm 开头的行,其中 VmSize 表示进程虚拟内存的总大小,VmRSS 表示进程正在使用的物理内存大小(驻留集大小),VmPeak 表示进程虚拟内存使用量的峰值等。

  2. 使用 ps 命令
    ps 命令是一个用于报告当前系统进程状态的工具。你可以使用它来查看进程的内存使用情况。

    ps -o pid,user,%mem,vsz,rss,comm -p [pid]

    这里:

    • %mem 显示进程使用的物理内存百分比。
    • vsz 显示进程的虚拟内存大小(以 KB 为单位)。
    • rss 显示进程使用的驻留集大小(以 KB 为单位)。
    • comm 显示命令名(通常是程序的基名)。
  3. 使用 top 或 htop 命令
    这两个命令都提供了实时的系统状态视图,包括进程的内存使用情况。

    在 top 中,你可以直接看到 %MEM 列,它显示了每个进程使用的物理内存百分比。你还可以按 M 键来根据内存使用量对进程进行排序。

    htop 是 top 的一个增强版本,它提供了一个更友好的界面和更多的功能。你可以直接在界面中看到进程的内存使用情况,并可以使用鼠标或键盘进行操作。

  4. 使用 pmap 命令
    pmap 命令用于报告进程的内存映射。它可以显示进程地址空间中的各个区域的详细信息,包括哪些区域被映射到了文件、哪些区域是私有的等。

    pmap -x [pid]

    这将显示进程的内存映射,你可以从中计算出进程使用的总内存大小。

  5. 使用 /proc/[pid]/smaps 文件
    这个文件提供了进程内存映射的详细信息,包括每个映射区域的内存使用情况。这对于深入了解进程的内存布局和使用情况非常有用。

    cat /proc/[pid]/smaps | grep -E '^(Rss|Size):'

    这将显示每个映射区域的 Rss(驻留集大小)和 Size(总大小)。注意,这个文件包含的信息非常详细,可能需要一些时间来解析。计算进程的 CPU 时间

计算进程的 CPU 时间

 

在 Linux 中,你可以通过几种不同的方法来计算进程的 CPU 时间。CPU 时间是指进程在 CPU 上执行的时间,通常分为用户态时间(user CPU time)和内核态时间(system CPU time)。

  1. 使用 /proc/[pid]/stat 文件
    /proc/[pid]/stat 文件中包含了进程的 CPU 时间信息。你可以解析这个文件来获取用户态时间和内核态时间。

    cat /proc/[pid]/stat | awk '{print "User CPU time:", $14, "System CPU time:", $15}'

    这里,$14 是 utime(用户态时间),$15 是 stime(内核态时间),它们都是以时钟滴答数(clock ticks)表示的。你可以通过 /proc/timer_list 或使用 sysconf(_SC_CLK_TCK) 来获取时钟滴答的频率,通常是 100(在较新的系统中可能是 1000),然后将其转换为秒。

  2. 使用 ps 命令
    ps 命令可以显示进程的 CPU 时间。使用 -o 选项可以定制输出格式。

    ps -p [pid] -o pid,tid,etime,time,%cpu,cmd

    这里:

    • etime 表示进程的已过去运行时间(elapsed time)。
    • time 表示进程的总 CPU 时间,格式为 mm:ss.xx 或 m:ss.xx
    • %cpu 表示进程占用的 CPU 百分比。

    注意:ps 命令的 time 字段显示的是总 CPU 时间,它是用户态时间和内核态时间的总和。

  3. 使用 top 或 htop 命令
    top 和 htop 命令提供了实时的系统状态视图,它们都可以显示进程的 CPU 时间。

    在 top 中,你可以直接看到 %CPU 列,它显示了进程占用的 CPU 百分比。TIME+ 列显示了进程的总 CPU 时间。

    htop 提供了类似的视图,但界面更加友好。

  4. 计算进程的 CPU 时间百分比
    如果你想要计算进程在某个时间段内的 CPU 使用率,你可以使用 ps 命令在两个时间点采样 %cpu 值,并计算它们之间的差异。或者使用 pidstat 工具(需要安装 sysstat 包)来报告进程的 CPU 使用情况。

    pidstat -p [pid] 1 5 # 每秒报告一次,共报告 5 次

    这将显示包括 %usr(用户态时间百分比)和 %system(内核态时间百分比)在内的 CPU 使用情况。

  • 22
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The /proc/PID/stat file contains various statistics about the process with the specified PID. The file is a single line of text, with each field separated by a space. The fields in the file are as follows: 1. pid - process ID 2. comm - name of the command that started the process 3. state - current state of the process (e.g. running, sleeping, etc.) 4. ppid - parent process ID 5. pgrp - process group ID 6. session - session ID 7. tty_nr - controlling terminal of the process 8. tpgid - ID of the foreground process group of the controlling terminal 9. flags - process flags (e.g. whether it is being traced, etc.) 10. minflt - number of minor faults (i.e. page faults that could be resolved without disk IO) 11. cminflt - number of minor faults of child processes 12. majflt - number of major faults (i.e. page faults that required disk IO) 13. cmajflt - number of major faults of child processes 14. utime - amount of time the process has spent in user mode 15. stime - amount of time the process has spent in kernel mode 16. cutime - amount of time waited-for children have been in user mode 17. cstime - amount of time waited-for children have been in kernel mode 18. priority - priority of the process 19. nice - nice value of the process 20. num_threads - number of threads in the process 21. starttime - time the process started, in jiffies (1/100th of a second) 22. vsize - virtual memory size of the process 23. rss - resident set size of the process (i.e. amount of physical memory it is using) 24. rsslim - current limit on the resident set size 25. startcode - address of the start of the code segment 26. endcode - address of the end of the code segment 27. startstack - address of the start of the stack 28. kstkesp - current value of the stack pointer 29. kstkeip - current value of the instruction pointer 30. signal - bitmap of pending signals for the process 31. blocked - bitmap of blocked signals for the process 32. sigignore - bitmap of ignored signals for the process 33. sigcatch - bitmap of caught signals for the process 34. wchan - address of the kernel function the process is currently waiting in 35. nswap - number of pages swapped to disk 36. cnswap - number of pages swapped to disk of child processes 37. exit_signal - signal to be sent to the parent process when the process exits 38. processor - CPU the process last ran on 39. rt_priority - real-time priority of the process 40. policy - scheduling policy of the process Note: Some fields may be displayed as 0 if they are not applicable to the process or if they are not available. Also, the values for utime, stime, cutime, and cstime are given in clock ticks, which can be converted to seconds by dividing by the value of the system variable HZ (which is typically 100 on most systems).

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值