查看进程运行时间及上下文切换次数

运行环境:

CentOS Linux release 7.5.1804 (Core)

Linux centosgpt 5.2.0-rc4 #1 SMP Sun Jun 16 13:25:49 CST 2019 x86_64 x86_64 x86_64 GNU/Linux

 

进程的运行时间:

  • 通过ps 查看

[root@centosgpt ~]# ps -o etime= -p "$$"
55:12


其中"$$"可以替换成具体的进程id, etime后的等号

man ps

-o format

...

If all column headers are empty (ps -o pid= -o comm=) then the header line will not be output.

...

 


上下文切换次数:

  • 通过proc文件

          /proc/{pid}/status

[root@centosgpt ~]# grep ctxt /proc/1177/status
voluntary_ctxt_switches: 248
nonvoluntary_ctxt_switches: 2

volutary_ctxt_switches : 表示主动调度;
发生场景 等待IO或资源 调用__schedule,主动让出cpu
nonvolutary_ctxt_switches : 表示抢占式调度,;
发生场景:1时间片用完了, 需要切换到其他进程; 2 发生在高优先级进程被唤醒。
  • 通过sysstat

        前提:安装sysstat:

        方法:yum install sysstat  (ubuntu 使用apt-get install sysstat)

        1.   可以查看所有进程的上下文切换情况((kernels 2.6.23 and later only)

[root@centosgpt ~]# pidstat -w 1
Linux 5.2.0-rc4 (centosgpt)     07/20/2019      _x86_64_        (2 CPU)

07:55:59 PM   UID       PID   cswch/s nvcswch/s  Command
07:56:00 PM     0         9      1.94      0.00  ksoftirqd/0
07:56:00 PM     0        10     25.24      0.00  rcu_sched
07:56:00 PM     0       431      4.85      0.00  irq/16-vmwgfx
07:56:00 PM     0       944     10.68      0.00  vmtoolsd
07:56:00 PM     0       945      0.97      0.00  irqbalance
07:56:00 PM     0     38762      9.71      0.00  kworker/1:2-events_power_efficient
07:56:00 PM     0     38775     15.53      0.00  kworker/0:0-mm_percpu_wq
07:56:00 PM     0     38853    420.39      0.00  tar
07:56:00 PM     0     38854      0.97     15.53  gzip
07:56:00 PM     0     38858      0.97      1.94  pidstat

       2.   也可以查看指定进程下所有线程的上下文切换情况

[root@centosgpt ~]# pidstat -w -I -t -p 1177 3
Linux 5.2.0-rc4 (centosgpt)     07/20/2019      _x86_64_        (2 CPU)

08:02:06 PM   UID      TGID       TID   cswch/s nvcswch/s  Command
08:02:09 PM     0      1177         -      0.00      0.00  rsyslogd
08:02:09 PM     0         -      1177      0.00      0.00  |__rsyslogd
08:02:09 PM     0         -      1195      1.00      0.00  |__in:imjournal
08:02:09 PM     0         -      1196      0.00      0.00  |__rs:main

 


参考:

How to check how long a process has been running?

How to see how many context switches a process makes?

进程切换:自愿(VOLUNTARY)与强制(INVOLUNTARY)

How to see the sum of all context switches of all threads within a Java process?

Be First to Comment

发表回复