Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Log protocol upgrades on trace #3128

Merged
merged 9 commits into from
Dec 14, 2022

Conversation

rkuhn
Copy link
Contributor

@rkuhn rkuhn commented Nov 16, 2022

Description

Previously, every inbound or outbound upgrade generated a log at debug level, without information about the upgrade.
This commit changes it such that successful upgrades are logged at trace level (due to ubiquitous use of OneShot handlers) and that the negotiated protocol name is included in the message.

Notes

Links to any relevant issues

Open Questions

Change checklist

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • A changelog entry has been made in the appropriate crates

Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

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

Thanks!

A few minor suggestions.

core/src/upgrade.rs Outdated Show resolved Hide resolved
Comment on lines 152 to 156
#[test]
fn protocol_name_display() {
assert_eq!(DisplayProtocolName("hello").to_string(), "hello");
assert_eq!(DisplayProtocolName("hellö/").to_string(), "hell<C3><B6>/");
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we put this with the other tests at the bottom please in a mod tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah, I looked at some tests.rs but found it irrelevant, so placed the test next to the thing; recently I began doubting the dogma of “mod tests at the bottom” for such things because they serve to clearly document the intended behaviour right where people read the code

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't mind much where the tests are but I want them to be feature-gated on cfg(test) so we don't compile them into binaries of our users.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah, good point: we have a deal :-)

Copy link
Contributor Author

@rkuhn rkuhn Nov 17, 2022

Choose a reason for hiding this comment

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

Wow, that was interesting: I checked what was needed, and indeed #[test] alone already hides the function in normal builds. This is the expansion:

    extern crate test;
    #[cfg(test)]
    #[rustc_test_marker = "upgrade::protocol_name_display"]
    pub const protocol_name_display: test::TestDescAndFn = test::TestDescAndFn {
        desc: test::TestDesc {
            name: test::StaticTestName("upgrade::protocol_name_display"),
            ignore: false,
            ignore_message: ::core::option::Option::None,
            compile_fail: false,
            no_run: false,
            should_panic: test::ShouldPanic::No,
            test_type: test::TestType::UnitTest,
        },
        testfn: test::StaticTestFn(|| test::assert_test_result(protocol_name_display())),
    };
    fn protocol_name_display() {
        ...
    }

So thanks to our exchange I learnt that #[cfg(test)] is only needed for hiding test-related support functions and infrastructure.
(EDIT: fn protocol_name_display is elided from the .rlib as dead code)

Copy link
Contributor

Choose a reason for hiding this comment

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

Are you sure extern crate test is not being dragged in?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, after lunch I followed this rabbit hole for a little while longer and compared the liblibp2p_core.rlib for three cases:

  1. without the test (commented out)
  2. test with only the #[test] attribute (120 bytes size increase, caused by a 16 byte increase and other changes in lib.rmeta)
  3. with additional #[cfg(test)] attribute (106 bytes size increase, caused by a 104 byte increase in lib.rmeta)

As the metadata format is somewhat obscure, I decided to stop here. To my knowledge, extern crate test only brings a declared dependency explicitly into scope, which has been unnecessary since the 2015 edition (IIRC). Judging by the absence of the string protocol_name_display in the rlib, I’d leave the code as is, unless you (or others) have a strong opinion.

Copy link
Contributor

@thomaseizinger thomaseizinger Nov 22, 2022

Choose a reason for hiding this comment

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

I am leaning towards adding a mod tests block:

  1. Based on your investigation (thanks for doing that!), it seems to reduce code size.
  2. It is consistent across the codebase.

I don't have a strong opinion on where the mod tests block sits although we tend to put it at the bottom of the file.

The argument that it should be next to the function is definitely valid, however I think the more common case is that you want to read the production code, not the test code.

If you want to actually co-locate it, you could make it a rustdoc test :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is a brilliant idea, in particular, because it found a usability bug (no public constructor).

core/src/upgrade/apply.rs Outdated Show resolved Hide resolved
core/src/upgrade/apply.rs Outdated Show resolved Hide resolved
rkuhn and others added 2 commits November 17, 2022 12:32
Co-authored-by: Thomas Eizinger <thomas@eizinger.io>
@thomaseizinger thomaseizinger changed the title core: improve logging of libp2p_core::upgrade::apply feat(core): Log protocol name upon upgrades Nov 22, 2022
@thomaseizinger thomaseizinger changed the title feat(core): Log protocol name upon upgrades feat(core): Log protocol upon upgrades Nov 22, 2022
@thomaseizinger thomaseizinger changed the title feat(core): Log protocol upon upgrades feat(core): Log protocol upon upgrades on trace Nov 22, 2022
@thomaseizinger thomaseizinger changed the title feat(core): Log protocol upon upgrades on trace feat(core): Log protocol upgrades on trace Nov 22, 2022
@rkuhn
Copy link
Contributor Author

rkuhn commented Nov 24, 2022

The failures in earlier commits are related to semver, which doesn’t seem to be caused by this PR, so I consider this ready now.

core/src/upgrade.rs Outdated Show resolved Hide resolved
core/src/upgrade.rs Outdated Show resolved Hide resolved
Copy link
Member

@mxinden mxinden left a comment

Choose a reason for hiding this comment

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

Oh wonderful. Just 3 days ago I was debugging an issue and wished I had this patch in place. Thanks @rkuhn for the improvement.

core/src/upgrade.rs Outdated Show resolved Hide resolved
@@ -105,6 +108,7 @@ where
},
Upgrade {
future: Pin<Box<U::Future>>,
name: SmallVec<[u8; 32]>,
Copy link
Member

Choose a reason for hiding this comment

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

A stream upgrade involves various I/O operations on many layers. I don't think a heap allocated 32 byte vector have a significant performance impact.

Thus, unless the performance improvement is proven through a benchmark, I suggest allocating on the heap here.

Suggested change
name: SmallVec<[u8; 32]>,
name: Vec<u8>,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Given that all protocol names I’m aware of are quite small, I see no reason to unconditionally allocate on the heap — why should that be the default that needs no reason? I could reduce the inline capacity to keep size_of equal to that of a Vec<u8>, if you want.

Copy link
Contributor

Choose a reason for hiding this comment

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

I am with @rkuhn here. I think it is good to avoid allocations for the majority of the cases. I don't see the use of a different type as too much complexity.

Copy link
Member

Choose a reason for hiding this comment

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

I see no reason to unconditionally allocate on the heap — why should that be the default that needs no reason?

Reason for me to not use SmallVec by default is that it adds noise, additional compile time and increases the attack vector in our dependency supply chain. Though the last one might be a lost fight in the first place.

I wrote a benchmark to try to replicate a performance benefit using SmallVec here. I have not been able thus far. Please take a look at Actyx#95. Maybe I can nerd-snipe either of you into proving me wrong @rkuhn and @thomaseizinger.

I am sorry in case this comes across as nit-picky. I am not trying to start an argument. I am genuinely interested in performance optimizations in rust-libp2p. I am curious in which cases we could optimize through stack allocation. I don't think this is one of those cases.

On a more general note, again out of curiosity, is either of you aware of a case where heap allocation is faster than stack allocation? I can not come up with a real world case where the heap would be faster than the stack.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To me this is foremost a question of using the most appropriate data structure for the job. SmallVec was already in the dependencies, and it is better suited than Vec due to usage characteristics, so I used it.


Regarding the benchmark: memory management cannot be benchmarked in this fashion. In general, microbenchmarks like yours are very rarely meaningful and even then they require careful interpretation of the result. The reason is that most of the time you are looking at a huge machine doing a tiny task, with all sorts of optimisations kicking in. And when you run a relevant task instead, different optimisations will completely change the picture. It is quite obvious that a stack allocation takes less than one CPU cycle and that a heap allocation can in general not be this fast. If your microbenchmark finds them to be equally fast, then your heap usage allows the malloc implementation to optimise down to a single pointer subtraction — which is a very likely thread-local optimisation to make for malloc().

But all of this theory is irrelevant in my opinion because we’re not optimising for the last percent of performance (as far as I’m aware we’re nowhere close to getting there at this point). So I just propose usage of the most appropriate data structure at hand.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it is reasonable to trust in CS knowledge like "stack allocations are faster than heap", even if you can't produce a micro benchmark that proves it.

Soon, this will be replaced by Cow so I am happy either way. If @mxinden feels strongly, let's go with Vec.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

BTW: this discussion uses “heap” and “stack”, but “stack” doesn’t actually enter the picture in the code in question since we’re talking about a memory slot in a generator, which will end up spawned onto a thread pool, so allocated on the heap in any case.

Copy link
Contributor

Choose a reason for hiding this comment

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

Right, what would be the right terminology then (out of curiosity?). "local variable"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That sounds appropriate to me — it’ll end up being stored in a compiler-generated enum variant if it lives across an .await point.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for expanding here. I think the points above are valid. That said, I haven't yet formed a good guiding principle for rust-libp2p.

As said above, the concrete instance is not relevant. Let's get this merged. Looking forward to the easier debugging of protocol upgrades on streams.

core/src/upgrade.rs Outdated Show resolved Hide resolved
rkuhn and others added 2 commits November 24, 2022 12:54
Co-authored-by: Max Inden <mail@max-inden.de>
Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

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

One more thought! I'll let @mxinden decide regarding Vec vs SmallVec.

core/src/upgrade.rs Outdated Show resolved Hide resolved
thomaseizinger
thomaseizinger previously approved these changes Dec 12, 2022
Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

mxinden
mxinden previously approved these changes Dec 14, 2022
Copy link
Member

@mxinden mxinden left a comment

Choose a reason for hiding this comment

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

Thanks @rkuhn!

@mergify mergify bot dismissed stale reviews from mxinden and thomaseizinger December 14, 2022 16:43

Approvals have been dismissed because the PR was updated after the send-it label was applied.

@mergify mergify bot merged commit c1e68b7 into libp2p:master Dec 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants