LXR | KVM | PM | Time | Interrupt | Systems Performance | Bootup Optimization

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

posted on 2016-12-22 15:52  ArnoldLu  阅读(3581)  评论(0编辑  收藏  举报

导航