Skip to content

perf: improve performance of update metrics - #1329

Merged
andygrove merged 7 commits into
apache:mainfrom
wForget:COMET-1328
Feb 8, 2025
Merged

andygrove merged 7 commits into
apache:mainfrom
wForget:COMET-1328

Conversation

@wForget

@wForget wForget commented Jan 23, 2025 •

Copy link
Copy Markdown
Member

Which issue does this PR close?

Closes #1328.

Rationale for this change

Improve performance of update metrics

What changes are included in this PR?

  • Define a NativeMetricNode proto type to pass all metric nodes at once to avoid iterative jni calls.
  • Call update metrics when releasing plan to reduce the number of calls.

How are these changes tested?

after this

sql metrics are displayed correctly:

cpu profile:
image

@wForget wForget changed the title Improve performance of update metrics perf: improve performance of update metrics Jan 23, 2025
@codecov-commenter

codecov-commenter commented Jan 23, 2025 •

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 90.90909% with 1 line in your changes missing coverage. Please review.

Project coverage is 39.06%. Comparing base (f09f8af) to head (71394ae).
Report is 19 commits behind head on main.

Files with missing lines Patch % Lines
...a/org/apache/spark/sql/comet/CometMetricNode.scala 83.33% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##               main    #1329       +/-   ##
=============================================
- Coverage     56.12%   39.06%   -17.07%     
- Complexity      976     2071     +1095     
=============================================
  Files           119      263      +144     
  Lines         11743    60742    +48999     
  Branches       2251    12909    +10658     
=============================================
+ Hits           6591    23729    +17138     
- Misses         4012    32530    +28518     
- Partials       1140     4483     +3343     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@wForget

wForget commented Jan 23, 2025

Copy link
Copy Markdown
Member Author

Although the proportion of udpate metric in cpu profile has been greatly reduced, the tpcds/tpch benchmark of small data set has not been improved.

Comment thread native/core/src/execution/metrics/utils.rs Outdated
@andygrove

Copy link
Copy Markdown
Member

@mbutrovich may be interested in reviewing this as well

let poll_output = exec_context.runtime.block_on(async { poll!(next_item) });

// Update metrics
update_metrics(&mut env, exec_context)?;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I wonder if we should add a config so that we can choose between frequent metrics updates vs just updating once the query completes. It can sometimes be helpful to see live metrics.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Per-batch is probably always overkill. For long-running jobs is there a period that makes sense? It looks like Spark History defaults to 10s.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I do like the idea of updating metrics every N seconds

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think checking a coarse-grained clock (i.e., CLOCK_MONOTONIC_COARSE) to see if N seconds has elapsed to produce updated metrics would be a reasonable compromise on performance impact vs. fresh metrics.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I also like the idea of updating every N seconds. One good reason for updating frequently is to keep updating the live UI.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@mbutrovich Thank you for your idea, sounds great to me, I will try to do that later.

@andygrove

andygrove commented Jan 23, 2025 •

Copy link
Copy Markdown
Member

Based on a single run of TPC-H @ 100GB, I see approximately 2% improvement in TPC-H (325s on main vs 318s with this PR)

@wForget

wForget commented Feb 5, 2025

Copy link
Copy Markdown
Member Author

@andygrove @mbutrovich @parthchandra Thank you for your review and sorry for the late reply. I have just finished my Chinese New Year holiday and will continue this work later.

@wForget
wForget marked this pull request as ready for review February 5, 2025 06:46
Comment on lines +78 to +81
spark_plan.children().iter().for_each(|child_plan| {
let child_node = to_native_metric_node(child_plan).unwrap();
native_metric_node.children.push(child_node);
});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If you change this to a for loop rather than using for_each then we can use ? to handle any error condition.

Suggested change
spark_plan.children().iter().for_each(|child_plan| {
let child_node = to_native_metric_node(child_plan).unwrap();
native_metric_node.children.push(child_node);
});
for child_plan in spark_plan.children() {
let child_node = to_native_metric_node(child_plan)?;
native_metric_node.children.push(child_node);
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thank you for your suggestion, changed. I am not familiar with rust yet, and I hope to learn rust by contributing to comet. 😁

runtime,
metrics,
metrics_update_interval,
metrics_last_update_time: Instant::now(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

https://github.com/jedisct1/rust-coarsetime

@andygrove thoughts on a coarse time crate? The overhead on these clock_gettime() as used underneath Instant::now() can really add up. Maybe it's a premature optimization, but I also don't want a "death by 1000 cuts" scenario with gettime() all over the place.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I ran coarsetime's benchmark on my laptop:

coarsetime_now():          126.93 M/s
coarsetime_recent():       340.32 M/s
coarsetime_elapsed():      142.64 M/s
coarsetime_since_recent(): 340.34 M/s
stdlib_now():              51.37 M/s
stdlib_elapsed():          42.42 M/s

I'm a bit stunned that Rust's stdlib doesn't provide a nice way to get coarse time on its own, since the performance difference can be quite large and a lot of tasks don't need nanosecond precision.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@mbutrovich I don't know much about coarse time, but I have no objection to adding this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I assume that we could make this change in a follow on PR

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I filed a follow on issue for this: #1381

@andygrove andygrove left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I tested this again and saw the incremental metrics updates. I also see a small improvement in the TPC-H benchmark locally. Thanks @wForget.

@andygrove
andygrove merged commit a1e6a39 into apache:main Feb 8, 2025
coderfender pushed a commit to coderfender/datafusion-comet that referenced this pull request Dec 13, 2025
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.

Improve performance of update metrics

5 participants