使用字段分隔符选项获取性能耗时

9

我有一个解析Linux命令perf输出的程序。它需要使用选项-x,(字段分隔符选项)。我想使用perf提取经过的时间(而不是任务时间CPU时钟)。但是当我使用-x选项时,在输出中找不到经过的时间,并且我找不到相应的perf事件。以下是示例输出:

perf stat ls
============
 Performance counter stats for 'ls':

          0.934889 task-clock (msec)         #    0.740 CPUs utilized          
                 6 context-switches          #    0.006 M/sec                  
                 0 cpu-migrations            #    0.000 K/sec                  
               261 page-faults               #    0.279 M/sec                  
         1,937,910 cycles                    #    2.073 GHz                    
   <not supported> stalled-cycles-frontend 
   <not supported> stalled-cycles-backend  
         1,616,944 instructions              #    0.83  insns per cycle        
           317,016 branches                  #  339.095 M/sec                  
            12,439 branch-misses             #    3.92% of all branches        

       0.001262625 seconds time elapsed //here we have it

现在支持字段分隔符选项。
perf stat -x, ls
================
2.359807,task-clock
6,context-switches
0,cpu-migrations
261,page-faults
1863028,cycles
<not supported>,stalled-cycles-frontend
<not supported>,stalled-cycles-backend
1670644,instructions
325047,branches
12251,branch-misses

非常感谢您提供的任何帮助。


1
为什么不将perf源代码更改为打印它呢? - Milind Dumbare
1
在查看perf源代码后,我看到目前没有任何方法可以在使用字段分隔符时打印此内容。不过我想知道为什么会这样。如果有人想要在常规报告中获取这些数据,则可能希望在字段分隔视图中获取它们。 - Gabriel Southern
你的程序用哪种语言解析格式化输出?我看只有解析“perf stat ls”的输出才是唯一选择。 - amigadev
既然你正在运行perf,为什么不使用一些sed命令来处理它,并将perf stat ls的输出格式化为perf stat -x ls呢?当然要包括“seconds time elapsed”。 - Milind Dumbare
1
@Milind 是的,这是可能的。然而我使用的工具不支持普通输出。字段分隔输出更容易解析。最后,我创建了一个自定义解析器将普通输出转换为字段分隔输出并使用它。 - knightrider
显示剩余2条评论
2个回答

1
# perf stat ls 2>&1 >/dev/null | tail -n 2 | sed 's/ \+//' | sed 's/ /,/'

0.002272536,seconds time elapsed

0
从内核5.2-rc1开始,perf stat暴露了一个名为duration_time的新事件,以解决这个问题。该事件的值与time elapsed值完全相等,但单位为纳秒而不是秒。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接