准备工作:
JavaDemo.java
import java.util.Random;
public class JavaDemo {
public static void main(String[] args) {
while(true) {
System.out.println(new Random().nextInt(77778888));
}
}
}
终端命令行执行:
# 编译
javac JavaDemo.java
# 运行
java JavaDemo
直接执行top命令
top
top - 15:00:07 up 3:39, 3 users, load average: 0.89, 0.64, 0.35
Tasks: 260 total, 2 running, 258 sleeping, 0 stopped, 0 zombie
%Cpu(s): 7.2 us, 5.9 sy, 0.0 ni, 86.8 id, 0.1 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 1863252 total, 111108 free, 916128 used, 836016 buff/cache
KiB Swap: 2097148 total, 2095100 free, 2048 used. 641492 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
16970 wsh 20 0 3244408 94828 12524 S 62.5 5.1 0:47.51 java
15799 wsh 20 0 771448 37496 20304 R 39.2 2.0 1:33.38 gnome-terminal-
13628 wsh 20 0 4244796 252816 69964 S 4.7 13.6 1:30.83 gnome-shell
7623 root 20 0 424852 82132 55152 S 3.0 4.4 0:28.00 X
13 root rt 0 0 0 0 S 0.3 0.0 0:00.07 migration/1
61 root 20 0 0 0 0 S 0.3 0.0 0:01.45 kworker/7:1
81 root 20 0 0 0 0 S 0.3 0.0 0:01.47 kworker/0:1
99 root 20 0 0 0 0 S 0.3 0.0 0:00.67 kworker/3:1
100 root 20 0 0 0 0 S 0.3 0.0 0:00.92 kworker/2:1
773 root 20 0 0 0 0 S 0.3 0.0 0:01.00 kworker/4:1
6347 root 20 0 90500 3164 2300 S 0.3 0.2 0:02.90 rngd
1 root 20 0 194216 6440 3340 S 0.0 0.3 0:03.61 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.02 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:00.37 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.07 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 0:02.44 rcu_sched
10 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 lru-add-drain
上面执行结果中的
load average: 0.89, 0.64, 0.35
表示cpu 1分钟、5分钟、15分钟 使用率(%)
这时如果按一下键盘上的1,就会显示具体的各个核的cpu
top - 15:04:27 up 3:43, 3 users, load average: 1.71, 1.21, 0.65
Tasks: 260 total, 3 running, 257 sleeping, 0 stopped, 0 zombie
%Cpu0 : 12.5 us, 5.1 sy, 0.0 ni, 82.2 id, 0.3 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu1 : 8.1 us, 3.7 sy, 0.0 ni, 88.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu2 : 8.8 us, 10.1 sy, 0.0 ni, 81.1 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu3 : 4.4 us, 4.4 sy, 0.0 ni, 91.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu4 : 5.0 us, 7.0 sy, 0.0 ni, 88.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu5 : 7.0 us, 5.7 sy, 0.0 ni, 87.0 id, 0.3 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu6 : 5.0 us, 8.4 sy, 0.0 ni, 86.6 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu7 : 4.0 us, 4.7 sy, 0.0 ni, 91.0 id, 0.0 wa, 0.0 hi, 0.3 si, 0.0 st
KiB Mem : 1863252 total, 113492 free, 913500 used, 836260 buff/cache
KiB Swap: 2097148 total, 2095100 free, 2048 used. 644060 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
16970 wsh 20 0 3244408 94828 12524 S 66.8 5.1 3:33.11 java
15799 wsh 20 0 771448 37568 20308 D 38.2 2.0 3:12.12 gnome-terminal-
13628 wsh 20 0 4244796 252828 69964 S 2.0 13.6 1:31.90 gnome-shell
7623 root 20 0 424852 82132 55152 S 1.7 4.4 0:28.76 X
35 root 20 0 0 0 0 R 0.3 0.0 0:10.50 kworker/5:0
60 root 20 0 0 0 0 S 0.3 0.0 0:01.98 kworker/6:1
61 root 20 0 0 0 0 S 0.3 0.0 0:02.19 kworker/7:1
81 root 20 0 0 0 0 S 0.3 0.0 0:01.86 kworker/0:1
100 root 20 0 0 0 0 S 0.3 0.0 0:01.64 kworker/2:1
773 root 20 0 0 0 0 S 0.3 0.0 0:01.80 kworker/4:1
3162 root 20 0 0 0 0 S 0.3 0.0 0:02.13 xfsaild/dm-0
6360 root 20 0 320324 5752 4256 S 0.3 0.3 0:16.30 vmtoolsd
查看cpu 精简版命令
[wsh@localhost ~]$ uptime
15:06:58 up 3:46, 3 users, load average: 1.57, 1.39, 0.81
一般vmstat工具的使用是通过两个数字参数来完成的,第一个参数是采样的时间间隔数单位是秒,第二个参数是采样的次数
procs
cpu
[wsh@localhost ~]$ vmstat -n 2 3
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
3 0 2048 110012 36 835700 0 0 13 8 149 210 2 2 95 0 0
2 0 2048 109884 36 835744 0 0 0 74 3365 4975 6 7 87 0 0
1 0 2048 109692 36 835464 0 0 0 116 3337 4826 7 6 87 0 0
# 每10s 采样一次
mpstat -P ALL 10
[wsh@localhost ~]$ mpstat -P ALL 10
Linux 3.10.0-957.el7.x86_64 (localhost.localdomain) 2021年05月15日 _x86_64_ (8 CPU)
18时16分55秒 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
18时16分57秒 all 6.29 0.00 6.29 0.13 0.00 0.06 0.00 0.00 0.00 87.24
18时16分57秒 0 9.64 0.00 3.05 0.00 0.00 0.51 0.00 0.00 0.00 86.80
18时16分57秒 1 4.55 0.00 3.03 0.00 0.00 0.00 0.00 0.00 0.00 92.42
18时16分57秒 2 4.04 0.00 10.10 0.00 0.00 0.00 0.00 0.00 0.00 85.86
18时16分57秒 3 2.51 0.00 5.03 0.00 0.00 0.00 0.00 0.00 0.00 92.46
18时16分57秒 4 13.71 0.00 7.11 0.51 0.00 0.00 0.00 0.00 0.00 78.68
18时16分57秒 5 9.55 0.00 3.52 0.50 0.00 0.00 0.00 0.00 0.00 86.43
18时16分57秒 6 3.52 0.00 10.55 0.00 0.00 0.00 0.00 0.00 0.00 85.93
18时16分57秒 7 2.51 0.00 7.04 0.00 0.00 0.00 0.00 0.00 0.00 90.45
[wsh@localhost ~]$ ps -ef | grep java
wsh 16970 16881 63 14:58 pts/1 02:06:54 java JavaDemo
wsh 19532 16992 0 18:19 pts/0 00:00:00 grep --color=auto java
# pidstat -u 频率(s) -p 进程id
# 每10s钟采样一次
[wsh@localhost ~]$ pidstat -u 10 -p 16970
Linux 3.10.0-957.el7.x86_64 (localhost.localdomain) 2021年05月15日 _x86_64_ (8 CPU)
18时20分18秒 UID PID %usr %system %guest %CPU CPU Command
18时20分28秒 1000 16970 27.30 35.50 0.00 62.80 3 java
18时20分38秒 1000 16970 26.70 35.60 0.00 62.30 3 java
# -m 指定显示的单位,m是兆
[wsh@localhost ~]$ free -m
total used free shared buff/cache available
Mem: 1819 908 100 59 810 612
Swap: 2047 2 2045
一般:
[wsh@localhost ~]$ ps -ef | grep java
wsh 16970 16881 63 14:58 pts/1 02:12:23 java JavaDemo
wsh 19685 16992 0 18:28 pts/0 00:00:00 grep --color=auto java
# pidstat -p 进程id-r 采样频率
[wsh@localhost ~]$ pidstat -p 16970 -r 2
Linux 3.10.0-957.el7.x86_64 (localhost.localdomain) 2021年05月15日 _x86_64_ (8 CPU)
18时29分01秒 UID PID minflt/s majflt/s VSZ RSS %MEM Command
18时29分03秒 1000 16970 2.50 0.00 3244408 94900 5.09 java
18时29分05秒 1000 16970 4.50 0.00 3244408 94900 5.09 java
18时29分07秒 1000 16970 2.50 0.00 3244408 94900 5.09 java
[wsh@localhost ~]$ df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 15G 8.4G 6.7G 56% /
devtmpfs 894M 0 894M 0% /dev
tmpfs 910M 0 910M 0% /dev/shm
tmpfs 910M 11M 900M 2% /run
tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/sda1 197M 157M 41M 80% /boot
tmpfs 182M 8.0K 182M 1% /run/user/42
tmpfs 182M 40K 182M 1% /run/user/1000
/dev/sr0 4.3G 4.3G 0 100% /run/media/wsh/CentOS 7 x86_64
# 每2s采样一次,共采样3次
[wsh@localhost ~]$ iostat -xdk 2 3
Linux 3.10.0-957.el7.x86_64 (localhost.localdomain) 2021年05月15日 _x86_64_ (8 CPU)
Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sda 0.01 0.07 1.14 4.55 78.65 78.22 55.18 0.03 4.78 9.87 3.50 1.82 1.04
scd0 0.00 0.00 0.00 0.00 0.04 0.00 72.41 0.00 13.79 13.79 0.00 13.28 0.00
dm-0 0.00 0.00 1.06 4.52 77.36 78.08 55.64 0.04 6.89 10.53 6.03 1.85 1.03
dm-1 0.00 0.00 0.01 0.01 0.11 0.06 16.07 0.00 8.10 3.22 10.37 0.81 0.00
Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sda 0.00 0.00 0.50 7.00 4.00 84.00 23.47 0.01 1.13 0.00 1.21 1.13 0.85
scd0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
dm-0 0.00 0.00 0.50 7.00 4.00 84.00 23.47 0.01 1.13 0.00 1.21 1.13 0.85
dm-1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sda 0.00 0.00 0.00 7.46 0.00 101.49 27.20 0.02 2.33 0.00 2.33 2.33 1.74
scd0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
dm-0 0.00 0.00 0.00 7.46 0.00 101.49 27.20 0.02 2.33 0.00 2.33 2.33 1.74
dm-1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
磁盘块设备分布
查看对应进程的磁盘IO读写情况
[wsh@localhost ~]$ ps -ef | grep java
wsh 16970 16881 63 14:58 pts/1 02:17:19 java JavaDemo
wsh 19829 16992 0 18:36 pts/0 00:00:00 grep --color=auto java
# pidstat -d 采样频率 -p 进程id
[wsh@localhost ~]$ pidstat -d 2 -p 16970
Linux 3.10.0-957.el7.x86_64 (localhost.localdomain) 2021年05月15日 _x86_64_ (8 CPU)
18时37分02秒 UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
18时37分04秒 1000 16970 0.00 0.00 0.00 java
18时37分06秒 1000 16970 0.00 8.00 0.00 java
18时37分08秒 1000 16970 0.00 0.00 0.00 java
# 每两秒采样一次
ifstat 2
需要安装的话,安装命令为:
[wsh@localhost ~]$ wget http://gael.roualland.free.fr/ifstat/ifstat-1.1.tar.gz
[wsh@localhost ~]$ tar zxvf ifstat-1.1.tar.gz
[wsh@localhost ~]$ cd ifstat-1.1/
[wsh@localhost ifstat-1.1]$ ./configure
[wsh@localhost ifstat-1.1]$ make
[wsh@localhost ifstat-1.1]$ make install
top - 18:55:41 up 7:35, 3 users, load average: 1.69, 1.73, 1.71
Tasks: 257 total, 3 running, 254 sleeping, 0 stopped, 0 zombie
%Cpu(s): 6.5 us, 6.5 sy, 0.0 ni, 86.8 id, 0.1 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 1863252 total, 88248 free, 931280 used, 843724 buff/cache
KiB Swap: 2097148 total, 2095100 free, 2048 used. 626508 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
16970 wsh 20 0 3244408 94900 12528 S 62.3 5.1 149:11.35 java
15799 wsh 20 0 773848 39540 20576 R 39.7 2.1 91:57.92 gnome-terminal-
13628 wsh 20 0 4260820 263544 64620 S 3.6 14.1 2:43.74 gnome-shell
7623 root 20 0 425156 82396 55416 R 3.3 4.4 1:03.59 X
13646 wsh 9 -11 1285512 6844 3976 S 0.7 0.4 0:01.48 pulseaudio
61 root 20 0 0 0 0 S 0.3 0.0 0:40.08 kworker/7:1
81 root 20 0 0 0 0 S 0.3 0.0 0:23.10 kworker/0:1
13854 wsh 20 0 779864 14664 9480 S 0.3 0.8 0:02.76 gsd-color
17768 root 20 0 0 0 0 S 0.3 0.0 0:33.13 kworker/6:2
19382 root 20 0 0 0 0 S 0.3 0.0 0:07.71 kworker/2:2
20775 root 20 0 0 0 0 S 0.3 0.0 0:00.80 kworker/1:0
1 root 20 0 194216 6440 3340 S 0.0 0.3 0:04.26 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.04 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:00.47 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:03.17 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 0:14.39 rcu_sched
10 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 lru-add-drain
从上面可以看出进程id为 16970 java程序占用cpu过高
16970 wsh 20 0 3244408 94900 12528 S 62.3 5.1 149:11.35 java
也可以详细查看单个进程的top情况:
# top -Hp 进程id
[wsh@localhost ~]$ top -Hp 16970
top - 18:58:07 up 7:37, 3 users, load average: 1.72, 1.76, 1.72
Threads: 20 total, 1 running, 19 sleeping, 0 stopped, 0 zombie
%Cpu(s): 6.5 us, 6.2 sy, 0.0 ni, 86.9 id, 0.2 wa, 0.0 hi, 0.1 si, 0.0 st
KiB Mem : 1863252 total, 88680 free, 931328 used, 843244 buff/cache
KiB Swap: 2097148 total, 2095100 free, 2048 used. 626312 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
16971 wsh 20 0 3244408 94900 12528 R 59.7 5.1 145:56.30 java
16972 wsh 20 0 3244408 94900 12528 S 0.3 5.1 0:32.92 java
16974 wsh 20 0 3244408 94900 12528 S 0.3 5.1 0:32.99 java
16976 wsh 20 0 3244408 94900 12528 S 0.3 5.1 0:32.12 java
16978 wsh 20 0 3244408 94900 12528 S 0.3 5.1 0:32.64 java
16970 wsh 20 0 3244408 94900 12528 S 0.0 5.1 0:00.00 java
16973 wsh 20 0 3244408 94900 12528 S 0.0 5.1 0:32.60 java
16975 wsh 20 0 3244408 94900 12528 S 0.0 5.1 0:32.40 java
16977 wsh 20 0 3244408 94900 12528 S 0.0 5.1 0:32.83 java
16979 wsh 20 0 3244408 94900 12528 S 0.0 5.1 0:32.54 java
16980 wsh 20 0 3244408 94900 12528 S 0.0 5.1 0:14.71 VM Thread
16981 wsh 20 0 3244408 94900 12528 S 0.0 5.1 0:00.00 Reference Handl
16982 wsh 20 0 3244408 94900 12528 S 0.0 5.1 0:00.00 Finalizer
# ps -mp 进程id -o THREAD,tid,time
[wsh@localhost ~]$ ps -mp 16970 -o THREAD,tid,time
USER %CPU PRI SCNT WCHAN USER SYSTEM TID TIME
wsh 62.9 - - - - - - 02:31:36
wsh 0.0 19 - futex_ - - 16970 00:00:00
wsh 60.9 19 - n_tty_ - - 16971 02:26:48
wsh 0.2 19 - futex_ - - 16972 00:00:33
wsh 0.2 19 - futex_ - - 16973 00:00:32
wsh 0.2 19 - futex_ - - 16974 00:00:33
wsh 0.2 19 - futex_ - - 16975 00:00:32
wsh 0.2 19 - futex_ - - 16976 00:00:32
wsh 0.2 19 - futex_ - - 16977 00:00:33
wsh 0.2 19 - futex_ - - 16978 00:00:32
wsh 0.2 19 - futex_ - - 16979 00:00:32
wsh 0.1 19 - futex_ - - 16980 00:00:14
wsh 0.0 19 - futex_ - - 16981 00:00:00
wsh 0.0 19 - futex_ - - 16982 00:00:00
wsh 0.0 19 - futex_ - - 16983 00:00:00
wsh 0.0 19 - futex_ - - 16984 00:00:00
wsh 0.0 19 - futex_ - - 16985 00:00:00
wsh 0.0 19 - futex_ - - 16986 00:00:00
wsh 0.0 19 - futex_ - - 16987 00:00:00
wsh 0.0 19 - futex_ - - 16988 00:00:00
wsh 0.0 19 - futex_ - - 16989 00:00:09
发现 16971 这个线程id占用cpu过高:
wsh 60.9 19 - n_tty_ - - 16971 02:26:48
# 将线程号转化成16进制
# printf "%x\n" <线程的pid>
[wsh@localhost ~]$ printf "%x\n" 16971
424b
# jstack 进程id | grep 线程id的16进制数(小写) -A60
[wsh@localhost ~]$ jstack 16970 | grep 424b -A60
"main" #1 prio=5 os_prio=0 tid=0x00007fb2e804b800 nid=0x424b runnable [0x00007fb2f089d000]
java.lang.Thread.State: RUNNABLE
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(FileOutputStream.java:326)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
- locked <0x00000000e3805d38> (a java.io.BufferedOutputStream)
at java.io.PrintStream.write(PrintStream.java:482)
- locked <0x00000000e3805170> (a java.io.PrintStream)
at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)
at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:291)
at sun.nio.cs.StreamEncoder.flushBuffer(StreamEncoder.java:104)
- locked <0x00000000e3805128> (a java.io.OutputStreamWriter)
at java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:185)
at java.io.PrintStream.newLine(PrintStream.java:546)
- eliminated <0x00000000e3805170> (a java.io.PrintStream)
at java.io.PrintStream.println(PrintStream.java:737)
- locked <0x00000000e3805170> (a java.io.PrintStream)
at JavaDemo.main(JavaDemo.java:8)
"VM Thread" os_prio=0 tid=0x00007fb2e80df000 nid=0x4254 runnable
"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00007fb2e805e000 nid=0x424c runnable
"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00007fb2e8060000 nid=0x424d runnable
"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00007fb2e8062000 nid=0x424e runnable
"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00007fb2e8063800 nid=0x424f runnable
"GC task thread#4 (ParallelGC)" os_prio=0 tid=0x00007fb2e8065800 nid=0x4250 runnable
"GC task thread#5 (ParallelGC)" os_prio=0 tid=0x00007fb2e8067800 nid=0x4251 runnable
"GC task thread#6 (ParallelGC)" os_prio=0 tid=0x00007fb2e8069800 nid=0x4252 runnable
"GC task thread#7 (ParallelGC)" os_prio=0 tid=0x00007fb2e806b000 nid=0x4253 runnable
"VM Periodic Task Thread" os_prio=0 tid=0x00007fb2e813c000 nid=0x425d waiting on condition
JNI global references: 5
从上面可以看出:
at JavaDemo.main(JavaDemo.java:8)