【问题标题】:perf- how to get the current runtime clock of the programperf-如何获取程序的当前运行时时钟
【发布时间】:2021-03-08 15:27:37
【问题描述】:

我正在使用性能工具来衡量性能 使用命令:

perf record -F 99 -T -d -p $(pid_of_my_program) 
perf sched script -v

我得到的结果:

TimerTask 23652 544157.760762:          1 cycles:          c0044606 finish_task_switch ([kernel.kallsyms])
       TimerTask 23652 544157.760788:          1 cycles:          c0044606 finish_task_switch ([kernel.kallsyms])
       TimerTask 23652 544157.760797:          1 cycles:          c0044606 finish_task_switch ([kernel.kallsyms])
       TimerTask 23652 544157.760804:         52 cycles:          c0044606 finish_task_switch ([kernel.kallsyms])
       TimerTask 23652 544157.760811:       5162 cycles:          c0044606 finish_task_switch ([kernel.kallsyms])

我尝试转换时间戳:

date -d @544157.760762

我得到了 Unix 时间:

Wed Jan  7 09:09:17 IST 1970

如何将时钟设置为当前时间? 我想获取程序的当前运行时间

【问题讨论】:

    标签: c++ c linux timestamp perf


    【解决方案1】:

    此答案假定您想要获取记录 perf 样本的当前时间戳,以及 perf 跟踪的进程运行的时间。

    perf 模块使用sched_clock 函数来计算事件的时间戳。您在perf sched script 命令中看到的时间戳实际上表示自系统启动以来的纳秒数(格式为seconds.nanoseconds)。这就是sched_clock 计算的内容。更多详情请查看this 答案。

    仅在此处转换时间戳,不会为您提供收集样本的当前时间。

    要获取当前时间,你可以这样做-

    #! /bin/bash
    
    # get uptime in yyyy-mm-dd HH:MM:SS format
    uptime_since=$(uptime -s)         
    
    # convert uptime to epoch
    epoch_uptime=`date --date="$uptime_since" +"%s%N"`
    
    # add the timestamp obtained from perf to the previous result
    current_time_in_epoch=$((epoch_uptime + 544157760762000))
    
    # convert the nanosecond epoch+perf time to seconds
    current_time_in_seconds=$((current_time_in_epoch/1000000000))
    
    # convert this to timestamp format
    current_time=`date -d @$current_time_in_seconds`
    
    echo $current_time
    

    以上所有命令都考虑到你使用的shell是bash

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-18
      • 2021-05-15
      • 1970-01-01
      • 2013-12-20
      • 1970-01-01
      相关资源
      最近更新 更多