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

Issue/352 single TX copy #407

Merged
merged 4 commits into from
Dec 5, 2024
Merged

Issue/352 single TX copy #407

merged 4 commits into from
Dec 5, 2024

Conversation

serges147
Copy link
Collaborator

@serges147 serges147 commented Nov 29, 2024

These changes (along with necessary changes at lizards) make TX pipeline as "low copy" (see issue #352).

  • Memory for TX payload fragments are allocated from IMedia::getTxMemoryResource() memory resource.
  • Result TX items now contain mutable MediaPayload payloads, which are capable to transfer payload memory ownership (f.e. back to the media) - so now it's possible f.e. implement usage of CAN memory hardware for transmission.

- Added new `IMedia::getTxMemoryResource` method.
- Media implementations (at examples) now have their own TX memory
resource.
- Addopted recent v4 canard api changes.
  - each `canardTxInit` now initialized with media TX memory resource
- addressed "// TODO: Remove this workaround when the issue is
resolved." - canard memory management now has de-allocation amount
parameter.
- Extended all transport related unit tests to verify that TX pipeline
uses its dedicated memory resource.
- Introduced `libcyphal::transport::MediaPayload` - in use to pass
payload data between the transport layer and its media.
- Use lates latest Canard v4 api
- latest canard v4
- latest udpard v2
@serges147 serges147 self-assigned this Nov 29, 2024
@serges147 serges147 changed the title Issue/352 zero copy Issue/352 single TX copy Nov 29, 2024
@serges147 serges147 marked this pull request as ready for review November 29, 2024 13:50
/// In use to pass payload data between the transport layer and its media.
/// It also manages memory ownership of the allocated payload buffer.
///
class MediaPayload final // NOSONAR : cpp:S4963 - we do directly handle resources here.
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'll try. Thanks for noticing it!

Copy link
Collaborator Author

@serges147 serges147 Dec 3, 2024

Choose a reason for hiding this comment

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

It worked, but if you don't mind I would like make these changes (in all places) in the next pr.

public:
/// Constructs a new empty payload.
///
MediaPayload() = default;
Copy link
Contributor

Choose a reason for hiding this comment

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

Violates AUTOSAR A12-1-2 since the move and copy constructor initialize data members.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

@@ -66,9 +67,16 @@ class IMedia

/// @brief Schedules the frame for transmission asynchronously and return immediately.
///
/// Concrete media implementation has multiple options with how to handle `payload` buffer:
/// - just copy the buffer data byts (using `payload.getSpan` const method) and return without changing the payload;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// - just copy the buffer data byts (using `payload.getSpan` const method) and return without changing the payload;
/// - just copy the buffer data bytes (using `payload.getSpan` const method) and return without changing the payload;

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

@serges147 serges147 merged commit 6f801fc into main Dec 5, 2024
34 checks passed
@serges147 serges147 deleted the issue/352_zero_copy branch December 5, 2024 11:32
/// It's the caller's responsibility to deallocate the buffer with the correct memory resource,
/// or move it somewhere else with the same guarantee (like f.e. back to a lizard TX queue item).
///
std::tuple<std::size_t, cetl::byte*, std::size_t> release() noexcept
Copy link

Choose a reason for hiding this comment

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

Why a tuple and not a named structure? The user will have to remember which element is the allocated size instead of it being named.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ok, I'll create separate nested Ownership type, like following:

class MediaPayload final
{
public:
    /// Structure with the payload size, pointer to the payload data, and the allocated size.
    /// It's the caller's responsibility to deallocate the buffer with the correct memory resource,
    /// or move it somewhere else with the same guarantee (like f.e. back to a lizard TX queue item).
    /// See also `release` method.
    struct Ownership
    {
        /// Size of the payload data in bytes.
        ///
        /// Could be less or equal to the allocated size.
        /// `0` when the payload is moved.
        ///
        std::size_t size;

        /// Pointer to the payload buffer.
        ///
        /// `nullptr` when the payload is moved.
        ///
        cetl::byte* data;

        /// Size of the allocated buffer.
        ///
        /// Could be greater or equal to the payload size.
        /// `0` when the payload is moved.
        ///
        std::size_t allocated_size;

    };  // Ownership
    ...
    Ownership release() noexcept
    {
        ...

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.

3 participants