微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决perf- 如何获取程序的当前运行时间

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

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 样本的当前时间戳,以及 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。