perf-如何获取程序的当前运行时时钟

perf- how to get the current runtime clock of the program

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

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

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

此答案假定您想要获取记录 perf 个样本的 当前时间戳 ,以及 [=11] 跟踪进程的时间=] 是 运行.

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

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

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

#! /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