top與ps命令入門

背景:想了解openssh-server響應用戶的sftp鏈接請求時是否多進程。ubuntu

實驗方法:建立了test1~4共四個用戶,同時用filezilla進行sftp鏈接。ssh

如今在openssh-server所在主機上運行top,能夠看到:測試

上圖能夠看到,每一個sftp鏈接都起了3個進程。那麼,這些進程分別跑在哪一個CPU上呢?這就要用到ps命令。以下:ui

zjd@ubuntu:/var/log$ ps -To 'pid,lwp,psr,cmd' -p 12461
   PID    LWP PSR CMD
 12461  12461   1 sshd: test1 [priv]
zjd@ubuntu:/var/log$ ps -To 'pid,lwp,psr,cmd' -p 12463
   PID    LWP PSR CMD
 12463  12463   1 sshd: test1@notty
zjd@ubuntu:/var/log$ ps -To 'pid,lwp,psr,cmd' -p 12464
   PID    LWP PSR CMD
 12464  12464   1 sshd: test1@internal-sftp
zjd@ubuntu:/var/log$ ps -To 'pid,lwp,psr,cmd' -p 12466
   PID    LWP PSR CMD
 12466  12466   1 sshd: test2 [priv]
zjd@ubuntu:/var/log$ ps -To 'pid,lwp,psr,cmd' -p 12468
   PID    LWP PSR CMD
 12468  12468   0 sshd: test2@notty
zjd@ubuntu:/var/log$ ps -To 'pid,lwp,psr,cmd' -p 12469
   PID    LWP PSR CMD
 12469  12469   0 sshd: test2@internal-sftp
zjd@ubuntu:/var/log$ ps -To 'pid,lwp,psr,cmd' -p 12470
   PID    LWP PSR CMD
 12470  12470   0 sshd: test3 [priv]
zjd@ubuntu:/var/log$ ps -To 'pid,lwp,psr,cmd' -p 12472
   PID    LWP PSR CMD
 12472  12472   0 sshd: test3@notty
zjd@ubuntu:/var/log$ ps -To 'pid,lwp,psr,cmd' -p 12473
   PID    LWP PSR CMD
 12473  12473   1 sshd: test3@internal-sftp
zjd@ubuntu:/var/log$ ps -To 'pid,lwp,psr,cmd' -p 12474
   PID    LWP PSR CMD
 12474  12474   1 sshd: test4 [priv]
zjd@ubuntu:/var/log$ ps -To 'pid,lwp,psr,cmd' -p 12476
   PID    LWP PSR CMD
 12476  12476   0 sshd: test4@notty
zjd@ubuntu:/var/log$ ps -To 'pid,lwp,psr,cmd' -p 12477
   PID    LWP PSR CMD
 12477  12477   0 sshd: test4@internal-sftp

上面看到的PSR就是CPU編號。我實驗的這個主機的CPU只有2核,所以能夠看出上面這些進程分別跑在覈0和核1上。spa

 

更進一步,將測試用的主機改成4核。3d

 查看物理CPU個數:code

zjd@ubuntu:~$ cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l
2server

查看每一個物理CPU的核數:blog

zjd@ubuntu:~$ cat /proc/cpuinfo| grep "cpu cores"| uniq
cpu cores : 2進程

查看邏輯CPU個數:

zjd@ubuntu:~$ cat /proc/cpuinfo| grep "processor"| wc -l
4

查看CPU型號:

zjd@ubuntu:~$ cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c
4 Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz

top以下:

而後用ps分別查看每一個進行跑在哪一個CPU核上:

zjd@ubuntu:~$ ps -To 'pid,lwp,psr,cmd' -p 953
   PID    LWP PSR CMD
   953    953   1 sshd: test4 [priv]
zjd@ubuntu:~$ ps -To 'pid,lwp,psr,cmd' -p 955
   PID    LWP PSR CMD
   955    955   3 /lib/systemd/systemd --user
zjd@ubuntu:~$ ps -To 'pid,lwp,psr,cmd' -p 956
   PID    LWP PSR CMD
   956    956   1 (sd-pam)
zjd@ubuntu:~$ ps -To 'pid,lwp,psr,cmd' -p 976
   PID    LWP PSR CMD
   976    976   2 sshd: test4@notty
zjd@ubuntu:~$ ps -To 'pid,lwp,psr,cmd' -p 977
   PID    LWP PSR CMD
   977    977   3 sshd: test4@internal-sftp
zjd@ubuntu:~$ ps -To 'pid,lwp,psr,cmd' -p 978
   PID    LWP PSR CMD
   978    978   3 sshd: test3 [priv]
zjd@ubuntu:~$ ps -To 'pid,lwp,psr,cmd' -p 980
   PID    LWP PSR CMD
   980    980   0 /lib/systemd/systemd --user
zjd@ubuntu:~$ ps -To 'pid,lwp,psr,cmd' -p 981
   PID    LWP PSR CMD
   981    981   0 (sd-pam)
zjd@ubuntu:~$ ps -To 'pid,lwp,psr,cmd' -p 1001
   PID    LWP PSR CMD
  1001   1001   0 sshd: test3@notty
zjd@ubuntu:~$ ps -To 'pid,lwp,psr,cmd' -p 1002
   PID    LWP PSR CMD
  1002   1002   1 sshd: test3@internal-sftp
zjd@ubuntu:~$ ps -To 'pid,lwp,psr,cmd' -p 1003
   PID    LWP PSR CMD
  1003   1003   3 sshd: test2 [priv]
zjd@ubuntu:~$ ps -To 'pid,lwp,psr,cmd' -p 1005
   PID    LWP PSR CMD
  1005   1005   3 /lib/systemd/systemd --user
zjd@ubuntu:~$ ps -To 'pid,lwp,psr,cmd' -p 1006
   PID    LWP PSR CMD
  1006   1006   3 (sd-pam)
zjd@ubuntu:~$ ps -To 'pid,lwp,psr,cmd' -p 1027
   PID    LWP PSR CMD
  1027   1027   2 sshd: test2@notty
zjd@ubuntu:~$ ps -To 'pid,lwp,psr,cmd' -p 1028
   PID    LWP PSR CMD
  1028   1028   3 sshd: test2@internal-sftp
zjd@ubuntu:~$ ps -To 'pid,lwp,psr,cmd' -p 1029
   PID    LWP PSR CMD
  1029   1029   2 sshd: test1 [priv]
zjd@ubuntu:~$ ps -To 'pid,lwp,psr,cmd' -p 1031
   PID    LWP PSR CMD
  1031   1031   3 /lib/systemd/systemd --user
zjd@ubuntu:~$ ps -To 'pid,lwp,psr,cmd' -p 1033
   PID    LWP PSR CMD
  1033   1033   2 (sd-pam)
zjd@ubuntu:~$ ps -To 'pid,lwp,psr,cmd' -p 1053
   PID    LWP PSR CMD
  1053   1053   2 sshd: test1@notty
zjd@ubuntu:~$ ps -To 'pid,lwp,psr,cmd' -p 1054
   PID    LWP PSR CMD
  1054   1054   3 sshd: test1@internal-sftp

OK,放心了,0~3共4個核都用上了。

相關文章
相關標籤/搜索