热门搜索 :
考研考公
您的当前位置:首页正文

Linux 排查cpu高占用率的进程和线程命令

来源:东饰资讯网

影响程序的资源一般而言分为4个:CPU、内存、IO、网络。本文着重讲解一下在linux系统下,如何查看高CPU占用率的进程,线程。

编写python代码模拟真实场景

#-*- coding:utf-8 -*-
import time, threading
 
#测试线程的个数
num = 500
 
def loop():
        print 'thread %s is running...' % threading.current_thread().name
        k = 0
        while k < 500:
                k += 1
                time.sleep(1)
 
 
def special():
        print 'thread %s is running...' % threading.current_thread().name
        while True:
                pass
 
#启动一些普通线程
for i in range(num):
        t = threading.Thread(target=loop, name='normal-' + str(i))
        t.start()
 
t = threading.Thread(target=special, name='special')
t.start()

问题排查时使用的命令

如何确定高cpu消耗的进程

命令1:top

[root@VM_0_2_centos ~]# top
top - 14:17:48 up 182 days, 16:55,  4 users,  load average: 0.67, 0.52, 0.35
Tasks: 197 total,   1 running, 196 sleeping,   0 stopped,   0 zombie
%Cpu(s): 98.3 us,  1.7 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1883616 total,    70996 free,  1452144 used,   360476 buff/cache
KiB Swap:        0 total,        0 free,        0 used.   227156 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                            
12282 root      20   0 4766756  10940   2116 S 95.7  0.6   0:06.04 python2                                            
 4501 root      20   0  222060  83480   2084 S  2.7  4.4  12:18.02 YDService                                          
11671 root      20   0 1997604  56264    760 S  0.7  3.0 220:44.13 java                                               
15259 root      20   0  742296  13476   1488 S  0.7  0.7 559:54.71 barad_agent                                        
15587 root      20   0  203224  15088    584 S  0.3  0.8  17:22.58 supervisord                                        
24216 zabbix    20   0  150312   1580    124 S  0.3  0.1   3:42.66 zabbix_server                                      
32639 mysql     20   0 1195728 404816   3368 S  0.3 21.5 185:39.43 mysqld                                             
    1 root      20   0  190864   2660   1272 S  0.0  0.1  21:27.73 systemd                                            
    2 root      20   0       0      0      0 S  0.0  0.0   0:01.30 kthreadd                                           
    3 root      20   0       0      0      0 S  0.0  0.0   8:30.35 ksoftirqd/0                                        
    5 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H                                       
    7 root      rt   0       0      0      0 S  0.0  0.0   0:00.01 migration/0                                        
    8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh                                             
    9 root      20   0       0      0      0 S  0.0  0.0  58:24.59 rcu_sched                                          
   10 root      rt   0       0      0      0 S  0.0  0.0   1:25.38 watchdog/0

命令2:用ps -eo pid,pcpu | sort -n -k 2 (当然,可以tail一下)
提示:按第2列正序排列

[root@VM_0_2_centos ~]# ps -eo pid,pcpu | sort -n -k 2 | tail
   99  0.0
  PID %CPU
11555  0.1
 4501  0.1
  707  0.1
10177  0.2
15259  0.3
11671  0.4
32639  0.4
12282 97.2

如何确定高cpu消耗的线程

命令1:top -H

[root@VM_0_2_centos ~]# top -H
top - 14:21:38 up 182 days, 16:59,  4 users,  load average: 50.62, 22.48, 8.84
Threads: 923 total,   2 running, 921 sleeping,   0 stopped,   0 zombie
%Cpu(s): 97.4 us,  2.6 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1883616 total,    78292 free,  1452984 used,   352340 buff/cache
KiB Swap:        0 total,        0 free,        0 used.   226120 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                            
12793 root      20   0 4766756  10940   2116 R 95.0  0.6   3:47.29 python2                                            
   25 root      20   0       0      0      0 S  0.3  0.0   5:51.00 kswapd0                                            
 4516 root      20   0  222108  83528   2096 S  0.3  4.4   0:25.31 YDService                                          
11839 root      20   0 1997604  56264    760 S  0.3  3.0 159:24.41 java                                               
13799 root      20   0  158600   3120   1592 R  0.3  0.2   0:00.03 top                                                
15258 root      20   0  158196   8676   1356 S  0.3  0.5 105:26.68 barad_agent                                        
32651 mysql     20   0 1195728 404816   3368 S  0.3 21.5   2:34.47 mysqld                                             
32658 mysql     20   0 1195728 404816   3368 S  0.3 21.5   7:10.84 mysqld                                             
    1 root      20   0  190864   2660   1272 S  0.0  0.1  21:27.74 systemd                                            
    2 root      20   0       0      0      0 S  0.0  0.0   0:01.30 kthreadd                                           
    3 root      20   0       0      0      0 S  0.0  0.0   8:30.36 ksoftirqd/0                                        
    5 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H                                       
    7 root      rt   0       0      0      0 S  0.0  0.0   0:00.01 migration/0                                        
    8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh                                             
    9 root      20   0       0      0      0 S  0.0  0.0  58:24.67 rcu_sched                                          
   10 root      rt   0       0      0      0 S  0.0  0.0   1:25.38 watchdog/0                                         
   12 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kdevtmpfs                                          
   13 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 netns      

命令2:ps H -eo pid,tid,pcpu | sort -n -k 3
提示:按第3列正序排列

[root@VM_0_2_centos ~]# ps H -eo pid,tid,pcpu | sort -n -k 3 | tail
  892   892  0.0
  895   895  0.0
 9156  9156  0.0
    9     9  0.0
   99    99  0.0
  PID   TID %CPU
15259 15282  0.1
  707   707  0.1
11671 11839  0.2
12282 12793 96.1
进程号 线程号 cpu占用率

假如我知道高cpu消耗的进程号是多少了,查看它的线程信息的方法为

命令1:pstree -p pid

[root@VM_0_2_centos ~]# pstree -p 12282 
python2(12282)-+-{python2}(12283)
               |-{python2}(12284)
               |-{python2}(12285)
               |-{python2}(12286)
               |-{python2}(12287)
               |-{python2}(12288)
               |-{python2}(12289)
               |-{python2}(12290)
               |-{python2}(12291)
               |-{python2}(12292)

命令2:查看线程的详细信息:cat /proc/进程号/task/线程号/status

[root@VM_0_2_centos ~]# cat /proc/12282/task/12793/status 
Name:   python2
Umask:  0022
State:  R (running)
Tgid:   12282
Ngid:   0
Pid:    12793
PPid:   3248
TracerPid:  0
Uid:    0   0   0   0
Gid:    0   0   0   0
FDSize: 256
Groups: 0 
VmPeak:  4766756 kB
VmSize:   701540 kB
VmLck:         0 kB
VmPin:         0 kB
VmHWM:     10968 kB
VmRSS:      8152 kB
RssAnon:        6012 kB
RssFile:        2140 kB
RssShmem:          0 kB
VmData:   570192 kB
VmStk:       132 kB
VmExe:         4 kB
VmLib:      4724 kB
VmPTE:       168 kB
VmSwap:        0 kB
Threads:    2
SigQ:   1/7283
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000001001000
SigCgt: 0000000180000002
CapInh: 0000000000000000
CapPrm: 0000001fffffffff
CapEff: 0000001fffffffff
CapBnd: 0000001fffffffff
CapAmb: 0000000000000000
Seccomp:    0
Cpus_allowed:   1
Cpus_allowed_list:  0
Mems_allowed:   00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001
Mems_allowed_list:  0
voluntary_ctxt_switches:    0
nonvoluntary_ctxt_switches: 84797

命令3: top -H -p pid(实时显示)

[root@VM_0_2_centos ~]# top -H -p 12282
top - 14:26:49 up 182 days, 17:04,  4 users,  load average: 4.68, 15.42, 10.21
Threads:   2 total,   1 running,   1 sleeping,   0 stopped,   0 zombie
%Cpu(s): 99.7 us,  0.3 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1883616 total,    92920 free,  1436232 used,   354464 buff/cache
KiB Swap:        0 total,        0 free,        0 used.   244296 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                            
12793 root      20   0  701540   8152   2140 R 97.7  0.4   8:46.09 python2                                            
12282 root      20   0  701540   8152   2140 S  0.0  0.4   0:00.03 python2 

命令4: ps -efL | grep pid
查看该进程下所有的线程

[root@VM_0_2_centos ~]# ps -efL | grep 12282 | grep -v grep
root     12282  3248 12282  0    2 14:17 pts/2    00:00:00 python2 test.py
root     12282  3248 12793 96    2 14:17 pts/2    00:10:50 python2 test.py

结束语:
更多精彩内容持续更新中,关注微信公众号,有你更精彩。

微信公众号.jpg
Top