oldratlee / useful-scripts

🐌 useful scripts for making developer's everyday life easier and happier, involved java, shell etc.
https://github.com/oldratlee/useful-scripts
Apache License 2.0
7.29k stars 2.8k forks source link

show-busy-java-threads#__top_threadId_cpu在top v3.2下提取不正确 #71

Closed geekMessi closed 6 years ago

geekMessi commented 6 years ago
HOME="$tmp_store_dir" top -H -b -d $top_delay -n 2 |
    awk '{
            if (idx == 3 && $NF == "java")    # $NF is command
                # only print 4th text block(idx == 3), aka. process info of second top update
                print $1 " " $9    # $1 is thread id, $9 is %cpu
            if ($0 == "")
                idx++
    }' | sort -k2,2nr

这部分逻辑是不是有些问题?为什么只抓4th? 执行了一下,没有输出 改成下面是OK的

HOME="$tmp_store_dir" top -H -b -d $top_delay -n 2 |
    awk '{
            if ($NF == "java")    # $NF is command
                # only print 4th text block(idx == 3), aka. process info of second top update
                print $1 " " $9    # $1 is thread id, $9 is %cpu
    }' | sort -k2,2nr

awk语句

oldratlee commented 6 years ago

@geekMessi 实现后自己测试了多次。

给一下

  1. 你的环境信息
    • uname -a
    • top -h
    • awk --version
    • etc
  2. 运行方式、运行过程 和 运行结果

另外,如果上面的你问题定位对的,已经缩到上面的范围, 可以分段运行并给一下结果,以定位和Fix问题: @geekMessi

# Check point 1
top -H -b -d 0.5 -n 2

# Check point 2
top -H -b -d 0.5 -n 2 | tee top.out |
        awk '{
                if (idx == 3 && $NF == "java")    # $NF is command
                    # only print 4th text block(idx == 3), aka. process info of second top update
                    print $1 " " $9    # $1 is thread id, $9 is %cpu
                if ($0 == "")
                    idx++
        }'

可能性比较大的原因 是 不同环境/版本的Top输出格式 和 我的不一致。 @geekMessi


PS:

这部分逻辑是不是有些问题?为什么只抓4th? 执行了一下,没有输出

Top一次输出 有2段(汇总信息 和 进程信息)。 用awk 提取第4段,即要的是 第二次更新的进程信息(0.5秒间隔的时间段内的CPU信息)。

这些原因在代码注释中有说明到:

https://github.com/oldratlee/useful-scripts/blob/0b03b02aa26fa800b0c315bc41d331162ff964f2/show-busy-java-threads#L296-L305

分段运行一下上面命令也可以看出来。

geekMessi commented 6 years ago

环境信息:

top -v
    top: procps version 3.2.8
awk --version
      GNU Awk 3.1.7

top -H -b -d 0.5 -n 2 | tee top.out |
        awk '{
                if (idx == 3)    # $NF is command
                    # only print 4th text block(idx == 3), aka. process info of second top update
                    print "4th ? --> : N1-" $1 ", N9-" $9 ", NF-"$NF    # $1 is thread id, $9 is %cpu
                if ($0 == ""){
                    print idx
                    idx++
                }

        } END { print idx} '

output:

1
2
4th ? --> : N1-top, N9-user,, NF-0.14
4th ? --> : N1-Tasks:, N9-stopped,, NF-zombie
4th ? --> : N1-Cpu(s):, N9-0.0%st, NF-0.0%st
4th ? --> : N1-Mem:, N9-buffers, NF-buffers
4th ? --> : N1-Swap:, N9-cached, NF-cached
4th ? --> : N1-, N9-, NF-
3
4
5

top.out


top - 22:07:20 up 444 days,  6:52,  1 user,  load average: 0.25, 0.20, 0.14
Tasks: 1382 total,   2 running, 1380 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.4%sy,  0.7%ni, 98.8%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  24605608k total, 21993768k used,  2611840k free,   701296k buffers
Swap:  4191228k total,    14196k used,  4177032k free, 14083768k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
25576 abcd   30  10 11.9g 5.1g  23m R 31.7 21.6   0:49.07 jsvc
 5955 efgh  30  10 4069m 244m  16m S  9.3  1.0   3792:31 java
 1924 abcd   20   0 15964 2164  832 R  3.7  0.0   0:00.03 top
24838 abcd   30  10 11.9g 5.1g  23m S  1.9 21.6   0:00.21 jsvc
25218 abcd   30  10 11.9g 5.1g  23m S  1.9 21.6   0:00.04 jsvc
25270 abcd   30  10 11.9g 5.1g  23m S  1.9 21.6   0:42.15 jsvc
25765 abcd   30  10 11.9g 5.1g  23m S  1.9 21.6   0:29.47 jsvc
oldratlee commented 6 years ago

给一下 top的输出, 即 top.out 文件 @geekMessi

可以 在 oldratlee/useful-scripts - gitter.im 即时聊

oldratlee commented 6 years ago

问题已和 @geekMessi 确定:

两次Top的输出之间, top v3.2 有2个空行。 导致 __top_threadId_cpu在top v3.2下提取不正确。


期待 @geekMessi 的Pull Request 修复问题 ❤️

oldratlee commented 6 years ago

写了个实现,Fix了一下,

72 show-duplicate-java-classes support command name jsvc to find java process feature

这个的支持也加了

你试试是不是 OK 的? ❤️ @geekMessi

geekMessi commented 6 years ago

@oldratlee 验证了一下,没问题 👍

oldratlee commented 6 years ago

@geekMessi OK 🎉