3

I'm using perf to do some optimization work. By using perf script to show my perf records and get results like this:

   MyServer 13631 [015] 2611179.755027: probe_app:stat_timer: (52bbe0)
   ......

I write a script to analysis the results and need to change timestamp to epoch timestamp (at least accurate in second).

To to this, I first get the host start up epoch timestamp

    $ date -d "`uptime -s`" +%s
    1562557105

And verified by adding the uptime

    $ cat /proc/uptime
    2612552.50 36615651.34

The the start epoch timestamp is correct.

But I found the perf timestamp 211179.755027 is not the elapsed seconds since system startup as document said, there are some error about 100s+. How to get accurate timestamp while exec "perf record"? I tried "-T" but it seems not work.

jasonxia
  • 321
  • 1
  • 8

1 Answers1

1

The default clock used by perf (or the underlying perf_event_open) is not actually documented - so I would not necessarily rely on it to be any specific clock. Fortunately, perf allows you to chose a specific clock with the -k option. In general CLOCK_MONOTONIC and CLOCK_MONOTONIC_RAW are supported. Other clocks may be available depending on the events and version.

If you choose a specific clock, you can then use clock_gettime to get matching timestamps. I'm not aware of a simple way to do this from command line, but a C program will do.

Zulan
  • 21,896
  • 6
  • 49
  • 109
  • thanks @Zulan for your information. I have tried CLOCK_MONOTONIC, and write a simple test code to check it: "clock_gettime(CLOCK_MONOTONIC, &ts); printf("sec=%d nsec=%d\n", ts.tv_sec, ts.tv_nsec);", the CLOCK_MONOTONIC time has the same value as /proc/uptime, the perf timestamp still has an error about 100s+ than clock_gettime result. looks like the "-k CLOCK_MONOTONIC" not work for my case. – jasonxia Aug 07 '19 at 12:01
  • Curious... On my system (Linux/perf 5.2.5 from Arch), there is a large difference (tens of seconds) between `/proc/uptime` and `CLOCK_MONOTONIC*`. And a small difference between the two `CLOCK_MONOTONIC*` (hundreds of milliseconds). And the perf clocks match perfectly. – Zulan Aug 07 '19 at 12:51
  • what I did is: run "sudo perf record sleep 1; ./test", ./test is my test app to get CLOCK_MONOTONIC, after that, run "sudo pref script" to check perf timestamp, I got "sec=2626411 nsec=206691096" from ./test and 2626311.114567 from perf script. My system (Linux version 3.10.0-514.26.1.el7.x86_64, CentOS Linux release 7.6.1810, perf 3.10.0) – jasonxia Aug 07 '19 at 13:15
  • I'm afraid the 6 year old Linux version doesn't support `perf` clock ids. That was introduced with Linux 4.1 (2015). I can't comment on what is backported in these custom ancient Linux kernels – Zulan Aug 07 '19 at 13:27