Skip to content

feat: Adds instrumentation to collect tokio task metrics - #208

Open
devanbenz wants to merge 24 commits into
datafusion-contrib:mainfrom
devanbenz:db/night-of-the-living-thread
Open

devanbenz wants to merge 24 commits into
datafusion-contrib:mainfrom
devanbenz:db/night-of-the-living-thread

Conversation

@devanbenz

@devanbenz devanbenz commented May 14, 2025

Copy link
Copy Markdown
Contributor

This PR adds metrics collection for tokio tasks and polling statistics. Gathered some ideas from here and put together the following metrics to be collected.

struct LiquidTaskMetrics {
	total_tasks_n: AtomicU64,
	total_slow_poll_n: AtomicU64,
	total_poll_n: AtomicU64,
	mean_poll_duration_ms: AtomicU64,
	mean_idle_duration_ms: AtomicU64,
};
  • total_tasks_n is the amount of tasks spawned during runtime
  • total_slow_poll_n is the number of slow tasks
  • total_poll_n is the amount of tasks in a polling state
  • mean_poll_duration_ms shows how long polls are taking on average in milliseconds
  • mean_idle_duration_ms shows the average idle duration in milliseconds

I think the most interesting metric as we explore the different IO methods will be mean_idle_duration_ms as described by the link I referenced above:

Did mean_idle_duration increase?

  • This metric reflects the mean duration that tasks spent in the idle state. The idle state is the duration spanning the instant a task completes a poll, and the instant that it is next awoken. Tasks inhabit this state when they are waiting for task-external events to complete (e.g., an asynchronous sleep, a network request, file I/O, etc.). If this metric increases, tasks, in aggregate, spent more time waiting for task-external events to complete.

Overall different reasons for accounting these metrics can be seen here and in various sections throughout this document.

This PR is meant to be a building block so we can include these metrics via an admin server endpoint.

@codecov

codecov Bot commented May 28, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.79832% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 44.94%. Comparing base (c1f5855) to head (0183e06).
⚠️ Report is 12 commits behind head on main.

Files with missing lines Patch % Lines
...datafusion-server/src/admin_server/task_monitor.rs 95.87% 2 Missing and 2 partials ⚠️
src/datafusion/src/optimizers/lineage_opt.rs 50.00% 1 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (c1f5855) and HEAD (0183e06). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (c1f5855) HEAD (0183e06)
4 3
Additional details and impacted files
@@             Coverage Diff             @@
##             main     #208       +/-   ##
===========================================
- Coverage   83.83%   44.94%   -38.90%     
===========================================
  Files          91       82        -9     
  Lines       20246    13552     -6694     
  Branches    20246    13552     -6694     
===========================================
- Hits        16974     6091    -10883     
- Misses       2926     7251     +4325     
+ Partials      346      210      -136     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@devanbenz

Copy link
Copy Markdown
Contributor Author

@XiangpengHao I just need to collect some metrics both without and with collecting tokio task metrics and compare whether this impacts the performance of the application overall.

@devanbenz

Copy link
Copy Markdown
Contributor Author

Currently with my naive implementation I'm seeing that the performance metrics timings are a bit better without the task metrics. The flamegraphs also show a large chunk dealing with collecting task metrics. I'll likely need to refactor this to improve performance.

Without task metrics
without_task_metricslc
flamegraph-06825a2e-9a50-47d8-b6d0-9a28128dc553

With task metrics
with_task_metricslc
flamegraph-009be9b9-bef6-4304-9750-7a5930bacd47

@XiangpengHao

Copy link
Copy Markdown
Collaborator

Currently with my naive implementation I'm seeing that the performance metrics timings are a bit better without the task metrics. The flamegraphs also show a large chunk dealing with collecting task metrics. I'll likely need to refactor this to improve performance.

Without task metrics without_task_metricslc flamegraph-06825a2e-9a50-47d8-b6d0-9a28128dc553

With task metrics with_task_metricslc flamegraph-009be9b9-bef6-4304-9750-7a5930bacd47

happy to see that you're using this dashboard! This looks very cool

devanbenz added 2 commits May 30, 2025 07:09
* Consolidate tokio tasks so only one is spawned
* Update the update_high_mark function to use fetch_max
* Use loop for metrics collection instead of iterating over monitor intervals
@devanbenz

Copy link
Copy Markdown
Contributor Author

Alrighty, after a few adjustments I'm seeing much much better performance for tokio task metrics collection.

with_tasks_new
flamegraph-7de74f80-97a1-416f-b4d5-833a957318dd

@devanbenz

Copy link
Copy Markdown
Contributor Author

I'll set this PR ready for review and we can make any refinements you see fit. Could be nice to incorporate these metrics in to the admin dashboard as well. @XiangpengHao

@devanbenz
devanbenz marked this pull request as ready for review May 30, 2025 12:22
@XiangpengHao

Copy link
Copy Markdown
Collaborator

Thank you @devanbenz , I plan to take a look at this in the next few days!

@XiangpengHao

Copy link
Copy Markdown
Collaborator

Sorry for the delay, I took a high level look -- this is super cool @devanbenz

I plan to educate myself a bit more about tokio_unstable and understand how the metrics work under the hood.
will come back to merge this in a few days!

@codacy-production

codacy-production Bot commented Apr 18, 2026

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 10 complexity · 0 duplication

Metric Results
Complexity 10
Duplication 0

View in Codacy

TIP This summary will be updated as you push new changes. Give us feedback

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