我必须对如下所示的线程转储进行排序:
$ jstack -l 5213 | grep cpu
"Reference Handler" #2 daemon prio=10 os_prio=0 cpu=2.21ms elapsed=478.03s tid=0x00000000013bb800 nid=0x1465 waiting on condition [0x00007f386cc0c000]
"Finalizer" #3 daemon prio=8 os_prio=0 cpu=1.30ms elapsed=478.03s tid=0x00000000013c6000 nid=0x1466 in Object.wait() [0x00007f386cb0b000]
"Signal Dispatcher" #4 daemon prio=9 os_prio=0 cpu=0.31ms elapsed=478.03s tid=0x00000000013db000 nid=0x1467 runnable [0x0000000000000000]
"C2 CompilerThread0" #5 daemon prio=9 os_prio=0 cpu=3042.89ms elapsed=478.02s tid=0x00000000013dd800 nid=0x1468 waiting on condition [0x0000000000000000]
"C1 CompilerThread0" #8 daemon prio=9 os_prio=0 cpu=1840.51ms elapsed=478.02s tid=0x00000000013e0000 nid=0x1469 waiting on condition [0x0000000000000000]
转储非常大,因此我想按“cpu”列对其进行排序(升序或降序)。排序命令似乎接受“k”参数来指示用作排序标准的列。 “cpu”是第五列,所以我尝试过:
$ jstack -l 5213 | grep cpu | sort -k 5
"ServerService Thread Pool -- 1" #26 prio=5 os_prio=0 cpu=10.86ms elapsed=753.24s tid=0x0000000002b68800 nid=0x1482 waiting on condition [0x00007f385c33b000]
"Transaction Expired Entry Monitor" #131 daemon prio=5 os_prio=0 cpu=0.48ms elapsed=751.66s tid=0x00000000082ed000 nid=0x14eb in Object.wait() [0x00007f3853826000]
"Transaction Reaper Worker 0" #133 daemon prio=5 os_prio=0 cpu=0.13ms elapsed=751.63s tid=0x00000000084d9000 nid=0x14ee in Object.wait() [0x00007f3853523000]
我看到订单已更改,但它不符合我期望的标准(“cpu”)。知道像“cpu=value”这样的文本可以用作排序标准还是需要与其他命令结合使用吗?谢谢
答案1
在对输入子集上的整行进行排序时,可以采用一种设计“模式”,而输入子集本身无法直接检索。在这种情况下,在如下所示的多级管道中执行此操作比在 Awk 或 perl 中编写完整的命令更好
您可以做的一种方法是使用 Awk 提取ms
子字符串,转换为数字并打印整行和毫秒值,然后根据后者进行排序,然后将其修剪掉。
将命令输出传送到以下内容
awk 'match($0, /cpu=[[:digit:].]+/){print substr($0, RSTART+4, RLENGTH-1)+0, $0}' |
sort -n -k1,1 |
cut -d' ' -f2-
对于时间降序排列,将-r
标志添加到sort
答案2
如果用作=
字段分隔符,则 cpu 值位于第 4 个字段中。你可以做
jstack -l 5213 | grep cpu | sort -t '=' -g -k4,4
用于-g
“一般数字”排序。
对于给定的数据,输出是
"Signal Dispatcher" #4 daemon prio=9 os_prio=0 cpu=0.31ms elapsed=478.03s tid=0x00000000013db000 nid=0x1467 runnable [0x0000000000000000]
"Finalizer" #3 daemon prio=8 os_prio=0 cpu=1.30ms elapsed=478.03s tid=0x00000000013c6000 nid=0x1466 in Object.wait() [0x00007f386cb0b000]
"Reference Handler" #2 daemon prio=10 os_prio=0 cpu=2.21ms elapsed=478.03s tid=0x00000000013bb800 nid=0x1465 waiting on condition [0x00007f386cc0c000]
"C1 CompilerThread0" #8 daemon prio=9 os_prio=0 cpu=1840.51ms elapsed=478.02s tid=0x00000000013e0000 nid=0x1469 waiting on condition [0x0000000000000000]
"C2 CompilerThread0" #5 daemon prio=9 os_prio=0 cpu=3042.89ms elapsed=478.02s tid=0x00000000013dd800 nid=0x1468 waiting on condition [0x0000000000000000]
这假设等号没有出现在引用的字符串中,所以它不像@Inian的答案那么稳定。