Skip to content

Conversation

@Manciukic
Copy link
Contributor

@Manciukic Manciukic commented Aug 11, 2025

Changes

Use .ok_or_else when the Error needs to be computed by a function call, which may be expensive.
I fixed it in these places:

  • MSI interrupt trigger
  • clippy tracing
  • cpu template helper

And I've added a clippy rule for the future.

Reason

I was looking at perf traces for a different thing and noticed an unexpected String format in a good path during interrupt trigger:

-    1.16%     0.29%  firecracker  firecracker        [.] <vmm::devices::virtio::transport::pci::device::VirtioInterruptMsix as vmm::devices::virtio::transport::VirtioInterrupt>::trigger                                                     ▒
   - 0.88% <vmm::devices::virtio::transport::pci::device::VirtioInterruptMsix as vmm::devices::virtio::transport::VirtioInterrupt>::trigger                                                                                                    ▒
      - <vmm::vstate::vm::MsiVectorGroup as vm_device::interrupt::InterruptSourceGroup>::trigger                                                                                                                                               ▒
         + 0.71% alloc::fmt::format::format_inner                                                                                                                                                                                              ▒
           0.05% enframe                                                                                                                                                                                                                       ▒
           0.05% __lock                                                                                                                                                                                                                        ▒
           0.01% __libc_malloc_impl                                                                                                                                                                                                            ▒
           0.01% size_to_class                                                                                                                                                                                                                 ▒
           0.00% get_stride                                                                                                                                                                                                                    ▒
           0.00% rdlock                                                                                                                                                                                                                        ▒
           0.00% core::ptr::drop_in_place<alloc::boxed::convert::<impl core::convert::From<alloc::string::String> for alloc::boxed::Box<dyn core::error::Error+core::marker::Sync+core::marker::Send>>::from::StringError>                     ▒
           0.00% default_malloc                                                                                                                                                                                                                ▒
           0.00% core::fmt::write                                                                                                                                                                                                              ▒
   + 0.28% libc_start_main_stage2                                                                                                                                                                                                              ▒
   + 0.00% 0x500007ffe                                                                                                                                                                                                                         ▒

This wastes 0.71% of the CPU consumed by Firecracker during the block io performance tests, which is very little, but the change is also a one-liner.

License Acceptance

By submitting this pull request, I confirm that my contribution is made under
the terms of the Apache 2.0 license. For more information on following Developer
Certificate of Origin and signing off your commits, please check
CONTRIBUTING.md.

PR Checklist

  • I have read and understand CONTRIBUTING.md.
  • I have run tools/devtool checkbuild --all to verify that the PR passes
    build checks on all supported architectures.
  • I have run tools/devtool checkstyle to verify that the PR passes the
    automated style checks.
  • I have described what is done in these changes, why they are needed, and
    how they are solving the problem in a clear and encompassing way.
  • I have updated any relevant documentation (both in code and in the docs)
    in the PR.
  • I have mentioned all user-facing changes in CHANGELOG.md.
  • If a specific issue led to this PR, this PR closes the issue.
  • When making API changes, I have followed the
    Runbook for Firecracker API changes.
  • I have tested all new and changed functionalities in unit tests and/or
    integration tests.
  • I have linked an issue to every new TODO.

  • This functionality cannot be added in rust-vmm.

@codecov
Copy link

codecov bot commented Aug 11, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.59%. Comparing base (1e675c2) to head (36cde07).
⚠️ Report is 2 commits behind head on feature/pcie.

Additional details and impacted files
@@              Coverage Diff              @@
##           feature/pcie    #5356   +/-   ##
=============================================
  Coverage         80.59%   80.59%           
=============================================
  Files               265      265           
  Lines             30690    30690           
=============================================
  Hits              24736    24736           
  Misses             5954     5954           
Flag Coverage Δ
5.10-c5n.metal 80.38% <100.00%> (ø)
5.10-m5n.metal 80.37% <100.00%> (-0.01%) ⬇️
5.10-m6a.metal 79.59% <100.00%> (ø)
5.10-m6g.metal 76.89% <100.00%> (ø)
5.10-m6i.metal 80.36% <100.00%> (-0.01%) ⬇️
5.10-m7a.metal-48xl 79.57% <100.00%> (ø)
5.10-m7g.metal 76.89% <100.00%> (ø)
5.10-m7i.metal-24xl 80.34% <100.00%> (-0.01%) ⬇️
5.10-m7i.metal-48xl 80.34% <100.00%> (ø)
5.10-m8g.metal-24xl 76.88% <100.00%> (ø)
5.10-m8g.metal-48xl 76.88% <100.00%> (ø)
6.1-c5n.metal 80.42% <100.00%> (ø)
6.1-m5n.metal 80.42% <100.00%> (-0.01%) ⬇️
6.1-m6a.metal 79.63% <100.00%> (ø)
6.1-m6g.metal 76.89% <100.00%> (ø)
6.1-m6i.metal 80.41% <100.00%> (ø)
6.1-m7a.metal-48xl 79.61% <100.00%> (-0.01%) ⬇️
6.1-m7g.metal 76.89% <100.00%> (ø)
6.1-m7i.metal-24xl 80.43% <100.00%> (ø)
6.1-m7i.metal-48xl 80.43% <100.00%> (+<0.01%) ⬆️
6.1-m8g.metal-24xl 76.88% <100.00%> (ø)
6.1-m8g.metal-48xl 76.88% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 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.

If a function is called in the `.ok_or`, this gets executed
independently on whether the function succeeds or not, although the
result is only used on failure.

Replace it with a .ok_or_else to avoid a useless string allocation and
memcpy.

Signed-off-by: Riccardo Mancini <[email protected]>
@Manciukic Manciukic force-pushed the pcie/prevent-unnecessary-fmt branch from d5ace4a to 2021c51 Compare August 12, 2025 08:10
"or_fun_call" warns us when a function is called in side a "*_or(...)"
function. In this case the value is computed (which may be expensive),
but is only used if the Result/Option is an Error/None.
In these cases, using the "*_or_else" variant is the best thing to do.

Signed-off-by: Riccardo Mancini <[email protected]>
@Manciukic Manciukic force-pushed the pcie/prevent-unnecessary-fmt branch from 2021c51 to 393bdf8 Compare August 12, 2025 08:29
@Manciukic Manciukic marked this pull request as ready for review August 12, 2025 09:36
@Manciukic Manciukic added the Status: Awaiting review Indicates that a pull request is ready to be reviewed label Aug 12, 2025
@ShadowCurse ShadowCurse merged commit 172a6f6 into firecracker-microvm:feature/pcie Aug 12, 2025
7 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Status: Awaiting review Indicates that a pull request is ready to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants