Linux常用獲取進程占用資源情況手段


測試環境:Ubuntu14.04

1.  獲取進程ID號

ps -aux | grep your_process_name

例如:

xxx@xxx:~$ ps -e |grep Midlet|awk '{print $1}' 

9356

2.  查看進程相關信息

2.1 查看進程樹信息:

xxx@xxx:~$ pstree -p 9356 

runMidlet(9356)───{runMidlet}(9357)

2.2 查看進程打開的文件:

  1. xxx@xxx:~$ lsof -p 9356  
  2. lsof: WARNING: can't stat() ext4 file system /var/lib/docker/aufs  
  3.       Output information may be incomplete.  
  4. COMMAND    PID      USER   FD   TYPE DEVICE SIZE/OFF     NODE NAME  
  5. runMidlet 9356 lubaoquan  cwd    DIR    8,5     4096 21096156 /home/lubaoquan/java/phoneme/temp/bin/i386  
  6. runMidlet 9356 lubaoquan  rtd    DIR    8,1     4096        2 /  
  7. runMidlet 9356 lubaoquan  txt    REG    8,5  2033266 20979964 /home/lubaoquan/java/phoneme/temp/bin/i386/runMidlet  
  8. runMidlet 9356 lubaoquan  mem    REG    8,1  1754876  3682281 /lib/i386-linux-gnu/libc-2.19.so  
  9. runMidlet 9356 lubaoquan  mem    REG    8,1   114372  3670186 /lib/i386-linux-gnu/libgcc_s.so.1  
  10. runMidlet 9356 lubaoquan  mem    REG    8,1   134614  3682267 /lib/i386-linux-gnu/libpthread-2.19.so  
  11. runMidlet 9356 lubaoquan  DEL    REG    0,5           3375128 /SYSV62010009  
  12. runMidlet 9356 lubaoquan  mem    REG    8,1   134380  3682268 /lib/i386-linux-gnu/ld-2.19.so  
  13. runMidlet 9356 lubaoquan    0u   CHR  136,3      0t0        6 /dev/pts/3  
  14. runMidlet 9356 lubaoquan    1u   CHR  136,3      0t0        6 /dev/pts/3  
  15. runMidlet 9356 lubaoquan    2u   CHR  136,3      0t0        6 /dev/pts/3  
  16. runMidlet 9356 lubaoquan    3r  FIFO    8,1      0t0  1441801 /tmp/.qtvfb_mouse-0  
  17. runMidlet 9356 lubaoquan    4r  FIFO    8,1      0t0  1441800 /tmp/.qtvfb_keyboard-0 

2.3 查看進程系統調用相關信息:

  1. xxx@xxx:~$ sudo strace -p 9356</p>Process 9356 attached  
  2. [ Process PID=9356 runs in 32 bit mode. ]  
  3. select(5, [3 4], [], [], {0, 244888})   = 0 (Timeout)  
  4. select(5, [3 4], [], [], {0, 250000})   = 0 (Timeout)  
  5. select(5, [3 4], [], [], {0, 250000})   = 0 (Timeout)  
  6. select(5, [3 4], [], [], {0, 250000})   = 0 (Timeout)  
  7. select(5, [3 4], [], [], {0, 250000})   = 0 (Timeout)  
  8. select(5, [3 4], [], [], {0, 250000})   = 0 (Timeout) 

2.4 統計各系統調用耗費的時間:

  1. xxx@xxx:~$ sudo strace -p 9356 -c  
  2. Process 9356 attached  
  3. [ Process PID=9356 runs in 32 bit mode. ]  
  4. System call usage summary for 32 bit mode:  
  5. % time     seconds  usecs/call     calls    errors syscall  
  6. ------ ----------- ----------- --------- --------- ----------------  
  7. 98.85    0.030703          24      1275           select  
  8.   1.01    0.000314           0      8498           gettimeofday  
  9.   0.14    0.000042           0      1965       489 read  
  10.   0.00    0.000000           0        27           write  
  11.   0.00    0.000000           0        24        20 open  
  12.   0.00    0.000000           0         4           close  
  13.   0.00    0.000000           0        13           lseek  
  14.   0.00    0.000000           0         2           munmap  
  15.   0.00    0.000000           0       751           nanosleep  
  16.   0.00    0.000000           0         8         2 stat64  
  17.   0.00    0.000000           0         3           fstat64  
  18.   0.00    0.000000           0        17           futex  
  19. ------ ----------- ----------- --------- --------- ----------------  
  20. 100.00    0.031059                 12587       511 total 

2.5 ltrace統計信息

  1. xxx@xxxt:~$ ltrace -p 12394  
  2. Cannot attach to pid 12394: Operation not permitted  
  3. Could not attach to process.  If your uid matches the uid of the target  
  4. process, check the setting of /proc/sys/kernel/yama/ptrace_scope, or try  
  5. again as the root user.  For more details, see /etc/sysctl.d/10-ptrace.conf  
  6. lubaoquan@zx8200-Lenovo-Product:~$ sudo ltrace -p 12394  
  7. __fdelt_chk(4, 0xfff120d4, 0xfff12154, 0xfff121d4)                                                       = 0  
  8. __fdelt_chk(3, 0xfff120d4, 0xfff12154, 0xfff121d4)                                                       = 0  
  9. __divdi3(250, 0, 1000, 0)                                                                                = 0  
  10. __moddi3(250, 0, 1000, 0)                                                                                = 250  
  11. select(5, 0xfff120d4, 0xfff12154, 0xfff121d4)                                                            = 0  
  12. __fdelt_chk(4, 0xfff120d4, 0xfff12154, 0xfff121d4)                                                       = 0  
  13. __fdelt_chk(3, 0xfff120d4, 0xfff12154, 0xfff121d4)                                                       = 0  
  14. __divdi3(250, 0, 1000, 0)                                                                                = 0  
  15. __moddi3(250, 0, 1000, 0)                                                                                = 250  
  16. select(5, 0xfff120d4, 0xfff12154, 0xfff121d4)                                                            = 0  
  17. __fdelt_chk(4, 0xfff120d4, 0xfff12154, 0xfff121d4)                                                       = 0 

2.6 pidstat的詳細解釋

      -r     Report page faults andmemory utilization.

When reporting statistics for individualtasks, the following values may be displayed:

UID

      The real user identification number of the task being monitored.

USER

      The name of the real user owning the task being monitored.

PID

      The identification number of the task being monitored.

minflt/s

      Total number of minor faults the task has made per second, those whichhave not required loading a memory page from disk.

majflt/s

      Total number of major faults the task has made per second, those whichhave required loading a memory page from disk.

VSZ

      Virtual Size: The virtual memory usage of entire task in kilobytes.

RSS

      Resident Set Size: The non-swapped physical memory used by the task inkilobytes.

%MEM

      The tasks's currently used share of available physical memory.

Command

      The command name of the task.

  1. lubaoquan@xxx:~$ pidstat -r -p 12691 1  
  2. Linux 4.4.0-46-generic (zx8200-Lenovo-Product)    11/11/2016      _x86_64_         (4 CPU)  
  3. 09:55:51 AM   UID       PID  minflt/s  majflt/s     VSZ    RSS   %MEM  Command  
  4. 09:55:52 AM  1003     12691      0.00      0.00   17060   7216   0.12  runMidlet  
  5. 09:55:53 AM  1003     12691      0.00      0.00   17060   7216   0.12  runMidlet  
  6. 09:55:54 AM  1003     12691      0.00      0.00   17060   7216   0.12  runMidlet  
  7. 09:55:55 AM  1003     12691      0.00      0.00   17060   7216   0.12  runMidlet  
  8. 09:55:56 AM  1003     12691      0.00      0.00   17060   7216   0.12  runMidlet  
  9. 09:55:57 AM  1003     12691      0.00      0.00   17060   7216   0.12  runMidlet  
  10. 09:55:58 AM  1003     12691      0.00      0.00   17060   7216   0.12  runMidlet  
  11. 09:55:59 AM  1003     12691      0.00      0.00   17060   7216   0.12  runMidlet  
  12. 09:56:00 AM  1003     12691      0.00      0.00   17060   7216   0.12  runMidlet 

顯示stack使用情況:

-s    Report stack utilization.

  1. Linux 4.4.0-46-generic (zx8200-Lenovo-Product)    11/11/2016      _x86_64_         (4 CPU)  
  2. 10:15:25 AM   UID       PID StkSize  StkRef  Command  
  3. 10:15:26 AM  1003     12691     136      20  runMidlet  
  4. 10:15:27 AM  1003     12691     136      20  runMidlet  
  5. 10:15:28 AM  1003     12691     136      20  runMidlet 

顯示IO使用情況:

-d    Report I/O statistics (kernels 2.6.20 and later only).

  1. root@xxx:/proc/12691# pidstat -d -p 12691 1  
  2. Linux 4.4.0-46-generic (zx8200-Lenovo-Product)    11/11/2016      _x86_64_         (4 CPU)  
  3. 10:15:50 AM   UID       PID   kB_rd/s   kB_wr/s kB_ccwr/s  Command  
  4. 10:15:51 AM  1003     12691      0.00      0.00      0.00  runMidlet  
  5. 10:15:52 AM  1003     12691      0.00      0.00      0.00  runMidlet 

顯示CPU使用情況:

-u    Report CPU utilization.

背景是運行數獨游戲。

  1. root@xxx:/proc/12691# pidstat -u -p 12691 1  
  2. Linux 4.4.0-46-generic (zx8200-Lenovo-Product)    11/11/2016      _x86_64_         (4 CPU)  
  3. 10:23:30 AM   UID       PID    %usr %system  %guest    %CPU   CPU  Command  
  4. 10:23:31 AM  1003     12691    0.00    2.00    0.00    2.00     3  runMidlet  
  5. 10:23:32 AM  1003     12691    1.00    1.00    0.00    2.00     1  runMidlet  
  6. 10:23:33 AM  1003     12691    0.00    2.00    0.00    2.00     0  runMidlet  
  7. 10:23:34 AM  1003     12691    1.00    1.00    0.00    2.00     2  runMidlet  
  8. 10:23:35 AM  1003     12691    1.00    2.00    0.00    3.00     3  runMidlet 

2.7 ps顯示內存

  1. root@xxxt:/proc/12691# ps -p 12691 -u  
  2. USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND  
  3. lubaoqu+ 12691  0.0  0.1  17060  8180 pts/3    Sl   09:50   0:00 ./runMidlet internal com.sun.midp.appmanager.MVMManager 

使用ps更細致的顯示內存占用情況:

ps -p 2395 -eo pid,comm,args,class,vsz,rss,sz | grep Midlet

    vsz - virtual memory size of the process in KiB. Device mappings are currently excluded; this is subject to change.

    rsz - resident set size, the non-swapped physical memory that a task has used(in killobytes).

    sz - size in physical pages of the core image of the process. This includes text, data, and stack space. Device mappings are currently excluded; this is subject to change.

  1. xxx@xxx:~/java/phoneme/temp/bin/i386$ ps -eo pid,comm,args,class,vsz,rss,sz | grep Midlet  
  2. 8952 runMidlet       ./runMidlet internal com.su TS   17060  7360  4265 

2.8 procmem

  1. xxx@xxx:~/java/phoneme/temp/bin/i386$ sudo procmem -p 8952   
  2.     Vss      Rss      Pss      Uss     ShCl     ShDi     PrCl     PrDi  Name  
  3. -------  -------  -------  -------  -------  -------  -------  -------    
  4.   1748K    1600K    1600K    1600K       0K       0K    1600K       0K  /home/lubaoquan/java/phoneme/temp/bin/i386/runMidlet  
  5. 12552K    1540K    1540K    1540K       0K       0K    3584K       0K    
  6.   1696K    1200K     618K      36K    1164K       0K      36K       0K  /lib/i386-linux-gnu/libc-2.19.so  
  7.    152K     144K     144K     144K       0K       0K     144K       0K    
  8.    132K     100K     100K     100K       0K       0K     100K       0K  [heap]  
  9.    152K     152K      76K       0K       0K     152K       0K       0K  /SYSV6201000e  
  10.    128K     124K      66K       8K     116K       0K       8K       0K  /lib/i386-linux-gnu/ld-2.19.so  
  11.     56K      56K      56K      56K       0K       0K      56K       0K    
  12.    112K      96K      48K       0K      96K       0K       0K       0K  /lib/i386-linux-gnu/libgcc_s.so.1  
  13.     96K      88K      44K       0K      88K       0K       0K       0K  /lib/i386-linux-gnu/libpthread-2.19.so  
  14.    132K      20K      20K      20K       0K       0K      20K       0K  [stack]  
  15.     16K      16K      16K      16K       0K       0K      16K       0K    
  16.      8K       8K       8K       8K       0K       0K       8K       0K  /lib/i386-linux-gnu/libc-2.19.so  
  17.     12K       8K       8K       8K       0K       0K       8K       0K    
  18.      8K       8K       8K       8K       0K       0K       8K       0K  /home/lubaoquan/java/phoneme/temp/bin/i386/runMidlet  
  19.      4K       4K       4K       4K       0K       0K       4K       0K  /home/lubaoquan/java/phoneme/temp/bin/i386/runMidlet  
  20.      4K       4K       4K       4K       0K       0K       4K       0K  /lib/i386-linux-gnu/libc-2.19.so  
  21.      4K       4K       4K       4K       0K       0K       4K       0K  /lib/i386-linux-gnu/libgcc_s.so.1  
  22.      4K       4K       4K       4K       0K       0K       4K       0K  /lib/i386-linux-gnu/libpthread-2.19.so  
  23.      4K       4K       4K       4K       0K       0K       4K       0K  /lib/i386-linux-gnu/ld-2.19.so  
  24.      4K       4K       4K       4K       0K       0K       4K       0K    
  25.      4K       4K       4K       4K       0K       0K       4K       0K  /lib/i386-linux-gnu/libpthread-2.19.so  
  26.      4K       4K       4K       4K       0K       0K       4K       0K  /lib/i386-linux-gnu/ld-2.19.so  
  27.      8K       4K       2K       0K       4K       0K       0K       0K  [vdso]  
  28.      4K       0K       0K       0K       0K       0K       0K       0K  /lib/i386-linux-gnu/libc-2.19.so  
  29.      4K       0K       0K       0K       0K       0K       0K       0K    
  30.      8K       0K       0K       0K       0K       0K       0K       0K  [vvar]  
  31. -------  -------  -------  -------  -------  -------  -------  -------    
  32. 17056K    5196K    4386K    3576K    1468K     152K    5620K       0K  TOTAL 

解釋如下:

  • VSS- Virtual Set Size 虛擬耗用內存(包含共享庫占用的內存)
  • RSS- Resident Set Size 實際使用物理內存(包含共享庫占用的內存)
  • PSS- Proportional Set Size 實際使用的物理內存(比例分配共享庫占用的內存)
  • USS- Unique Set Size 進程獨自占用的物理內存(不包含共享庫占用的內存)

VSS (reported as VSZ from ps) is the total accessible address space of a process.This size also includes memory that may not be resident in RAM like mallocs that have been allocated but not written to. VSS is of very little use for determing real memory usage of a process.

RSS is the total memory actually held in RAM for a process.RSS can be misleading, because it reports the total all of the shared libraries that the process uses, even though a shared library is only loaded into memory once regardless of how many processes use it. RSS is not an accurate representation of the memory usage for a single process.

PSS differs from RSS in that it reports the proportional size of its shared libraries, i.e. if three processes all use a shared library that has 30 pages, that library will only contribute 10 pages to the PSS that is reported for each of the three processes. PSS is a very useful number because when the PSS for all processes in the system are summed together, that is a good representation for the total memory usage in the system. When a process is killed, the shared libraries that contributed to its PSS will be proportionally distributed to the PSS totals for the remaining processes still using that library. In this way PSS can be slightly misleading, because when a process is killed, PSS does not accurately represent the memory returned to the overall system.

USS is the total private memory for a process, i.e. that memory that is completely unique to that process.USS is an extremely useful number because it indicates the true incremental cost of running a particular process. When a process is killed, the USS is the total memory that is actually returned to the system. USS is the best number to watch when initially suspicious of memory leaksin a process.

一般來說內存占用大小有如下規律:VSS >= RSS >= PSS >= USS


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM