Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
geekMessi opened this issue Jun 6, 2018 · 7 comments

Comments

Projects
None yet
2 participants
@geekMessi
Copy link

commented Jun 6, 2018

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语句

@geekMessi geekMessi changed the title __top_threadId_cpu 有些问题吧 show-busy-java-threads#__top_threadId_cpu 有些问题吧 Jun 6, 2018

@oldratlee

This comment has been minimized.

Copy link
Owner

commented Jun 10, 2018

@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信息)。

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

# 3. use option `-d 0.5`(update interval 0.5 second) and `-n 2`(update 2 times),
# and use second time update data to get cpu percentage of thread in 0.5 second interval
HOME="$tmp_store_dir" top -H -b -d $top_delay -n 2 |
awk '{
if (idx == 3 && $NF == "java") # $NF(last field) is command field
# only print 4th text block(idx == 3), aka. process info of second top update
print $1 " " $9 # $1 is thread id field, $9 is %cpu field
if ($0 == "")
idx++
}' | sort -k2,2nr

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

@geekMessi

This comment has been minimized.

Copy link
Author

commented Jun 11, 2018

环境信息:

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

This comment has been minimized.

Copy link
Owner

commented Jun 11, 2018

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

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

@oldratlee oldratlee changed the title show-busy-java-threads#__top_threadId_cpu 有些问题吧 show-busy-java-threads#__top_threadId_cpu在top v3.2下提取不正确 Jun 12, 2018

@oldratlee oldratlee added the bug label Jun 12, 2018

@oldratlee

This comment has been minimized.

Copy link
Owner

commented Jun 12, 2018

问题已和 @geekMessi 确定:

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


期待 @geekMessiPull Request 修复问题 ❤️

oldratlee added a commit that referenced this issue Jun 13, 2018

@oldratlee

This comment has been minimized.

Copy link
Owner

commented Jun 13, 2018

写了个实现,Fix了一下,

#72 show-duplicate-java-classes support command name jsvc to find java process feature
这个的支持也加了

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

@geekMessi

This comment has been minimized.

Copy link
Author

commented Jun 13, 2018

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

@oldratlee

This comment has been minimized.

Copy link
Owner

commented Jun 13, 2018

@geekMessi OK 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.