Skip to content

Conversation

kavehshahedi
Copy link
Contributor

@kavehshahedi kavehshahedi commented Sep 28, 2025

Description

This PR adds support for UTC-adjusted timestamps in Chrome trace format output. Before, when using uftrace dump --chrome, the timestamps are based on monotonic clock values, which start from an arbitrary point (appearing as timestamps from 1970 when converted to dates). This makes it challenging to correlate trace events with actual wall-clock time (e.g., manually correlating with kernel traces from LTTng or perf).

With this change, we can now get timestamps that represent actual UTC time. The implementation uses the existing UTC offset information already stored in uftrace metadata (in #1923).

Usage

Record a trace

uftrace record ./program

Dump to a Chrome JSON file

uftrace dump --chrome > trace_utc.json
# timestamps should represent actual wall-clock time
[
   {
      "ts":1759035568795552.450,
      "ph":"B",
      "pid":4003,
      "name":"main"
   },
   {
      "ts":1759035568795561.123,
      "ph":"B",
      "pid":4003,
      "name":"sqlite3_sourceid"
   },
   {
      "ts":1759035568795561.262,
      "ph":"E",
      "pid":4003,
      "name":"sqlite3_sourceid"
   },
   {
      "ts":1759035568795857.122,
      "ph":"B",
      "pid":4003,
      "name":"cli_strncmp"
   },
   {
      "ts":1759035568795857.745,
      "ph":"E",
      "pid":4003,
      "name":"cli_strncmp"
   },
   {
      "ts":1759035568795858.157,
      "ph":"B",
      "pid":4003,
      "name":"main_init"
   },
...
]

Copy link
Owner

@namhyung namhyung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution!

It looks good to me in general. I wonder if it's better to use UTC-offset always (without adding an option).

cmds/dump.c Outdated
static bool offset_initialized = false;

/* Initialize the UTC offset once if the option is enabled */
if (chrome->opts && chrome->opts->with_utc_offset && !offset_initialized) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think chrome->opts can be NULL. Also I think we can just use utc_offset_ns == 0 if it's initialized or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your feedback!

I removed the --with-utc-offset flag as you recommended. Also, I fixed the issue regarding checking for chrome->opts and offset initialization.

This patch applies the UTC offset to timestamps in Chrome trace
output. When enabled, the timestamps represent actual UTC time rather
than monotonic time, making it easier to correlate trace events with
external time-based data or logs.

The UTC offset information is already available in the trace metadata
(added in commit fae85a8), and this patch leverages that data to
adjust timestamps when the option is specified.

Signed-off-by: Kaveh Shahedi <[email protected]>
@kavehshahedi kavehshahedi changed the title dump: Add --with-utc-offset option for Chrome trace format dump: Add UTC offset to Chrome trace format Sep 29, 2025
Copy link
Owner

@namhyung namhyung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@namhyung namhyung merged commit f0228b8 into namhyung:master Oct 10, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants