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

Revise PacketBufferWriter interface #4874

Merged
merged 5 commits into from
Feb 17, 2021
Merged

Conversation

kpschoedel
Copy link
Contributor

Problem

The PacketBufferWriter constructor that takes a size and allocates the
PacketBuffer internally is awkward for some cases, and useless for the
case of an existing PacketBufferHandle.

Summary of Changes

  • PacketBufferWriter() takes ownership of an existing PacketBufferHandle
    (same as PacketBufferTLVWriter).
  • Added a Finalize() overload that matches that of PacketBufferTLVWriter.

#### Problem

The `PacketBufferWriter` constructor that takes a size and allocates the
`PacketBuffer` internally is awkward for some cases, and useless for the
case of an existing `PacketBufferHandle`.

#### Summary of Changes

- `PacketBufferWriter()` takes ownership of an existing `PacketBufferHandle`
  (same as `PacketBufferTLVWriter`).
- Added a `Finalize()` overload that matches that of `PacketBufferTLVWriter`.
@@ -730,10 +730,9 @@ class PacketBufferWriterBase : public Writer
* @param[in] aAvailableSize Length bound of the BufferWriter.
* @param[in] aReservedSize Reserved packet buffer space for protocol headers; see \c PacketBufferHandle::New().
*/
PacketBufferWriterBase(size_t aAvailableSize, uint16_t aReservedSize = CHIP_SYSTEM_CONFIG_HEADER_RESERVE_SIZE) :
Writer(nullptr, 0)
PacketBufferWriterBase(System::PacketBufferHandle && aPacket) : Writer(aPacket->Start(), aPacket->AvailableDataLength())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like this still needs a length (possibly optional; checking the cases), for the case that the pool-backed buffer is actually larger than requested.

@kpschoedel kpschoedel marked this pull request as draft February 16, 2021 15:56
Copy link
Contributor

@bzbarsky-apple bzbarsky-apple left a comment

Choose a reason for hiding this comment

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

I'm OK with a followup for the MessagePacketBuffer bits, so that we don't combine behavior changes with refactoring in a single PR. Up to you.

But we do need to sort out the way we get the pointer+length out of the buffer.

@@ -25,19 +25,13 @@ constexpr size_t kStatusReportMinSize = 2 + 4 + 2; ///< 16 bits for GeneralCode,
CHIP_ERROR WriteToPacketBuffer(const ::chip::bdx::BdxMessage & msgStruct, ::chip::System::PacketBufferHandle & msgBuf)
{
size_t msgDataSize = msgStruct.MessageSize();
::chip::Encoding::LittleEndian::PacketBufferWriter bbuf(msgDataSize);
::chip::Encoding::LittleEndian::PacketBufferWriter bbuf(::chip::System::PacketBufferHandle::New(msgDataSize));
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 this should be a MessagePacketBuffer eventually, but a followup for that is OK.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll add them here so they don't get overlooked.

@@ -848,21 +842,21 @@ void TransferSession::PrepareStatusReport(StatusCode code)
{
mStatusReportData.StatusCode = code;

Encoding::LittleEndian::PacketBufferWriter bbuf(kStatusReportMinSize);
Encoding::LittleEndian::PacketBufferWriter bbuf(System::PacketBufferHandle::New(kStatusReportMinSize));
Copy link
Contributor

Choose a reason for hiding this comment

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

Again, I expect MessagePacketBuffer here.

@@ -730,10 +730,9 @@ class PacketBufferWriterBase : public Writer
* @param[in] aAvailableSize Length bound of the BufferWriter.
* @param[in] aReservedSize Reserved packet buffer space for protocol headers; see \c PacketBufferHandle::New().
*/
PacketBufferWriterBase(size_t aAvailableSize, uint16_t aReservedSize = CHIP_SYSTEM_CONFIG_HEADER_RESERVE_SIZE) :
Writer(nullptr, 0)
PacketBufferWriterBase(System::PacketBufferHandle && aPacket) : Writer(aPacket->Start(), aPacket->AvailableDataLength())
Copy link
Contributor

Choose a reason for hiding this comment

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

That combination of args doesn't really make sense if there's data in the buffer already.

We want to either use aPacket->Start() with aPacket->MaxDataLength(), or aPacket->Start() + aPacket->DataLength() with aPacket->AvailableDataLength().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct, I started off intended the former, but I think the latter is more useful.

@@ -757,6 +756,22 @@ class PacketBufferWriterBase : public Writer
*/
System::PacketBufferHandle Finalize() { return PacketBufferWriterUtil::Finalize(*this, mPacket); }

/**
* Finish the writing to the buffer and release ownership of the underlying PacketBuffer.
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
* Finish the writing to the buffer and release ownership of the underlying PacketBuffer.
* Finish writing to the buffer and release ownership of the underlying PacketBuffer.

/**
* Finish the writing to the buffer and release ownership of the underlying PacketBuffer.
*
* This signature matches `PacketBufferTLVWriter::Finalize()`.
Copy link
Contributor

Choose a reason for hiding this comment

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

It's really not clear how one decides between the two signatures in practice... It might be better to only have one signature; if we want to match PacketBufferTLVWriter, let's just do that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The original signature is more useful in practice; almost all Writer users immediately pass the buffer somewhere else.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, but why add the new one then?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Consistency with PacketBufferTLVWriter seemed like a good idea at the time.

@@ -264,7 +264,7 @@ CHIP_ERROR NetworkProvisioning::SendThreadCredentials(const DeviceLayer::Interna
4; // threadData.ThreadChannel, threadData.FieldPresent.ThreadExtendedPANId,
// threadData.FieldPresent.ThreadMeshPrefix, threadData.FieldPresent.ThreadPSKc
/* clang-format on */
Encoding::LittleEndian::PacketBufferWriter bbuf(credentialSize);
Encoding::LittleEndian::PacketBufferWriter bbuf(System::PacketBufferHandle::New(credentialSize));
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be a MessagePacketBuffer too.

@kpschoedel kpschoedel marked this pull request as ready for review February 16, 2021 16:56
@github-actions
Copy link

Size increase report for "esp32-example-build" from 3c2238a

File Section File VM
chip-all-clusters-app.elf .flash.text 44 44
Full report output
BLOAT REPORT

Files found only in the build output:
    report.csv

Comparing ./master_artifact/chip-pigweed-app.elf and ./pull_artifact/chip-pigweed-app.elf:

sections,vmsize,filesize

Comparing ./master_artifact/chip-all-clusters-app.elf and ./pull_artifact/chip-all-clusters-app.elf:

sections,vmsize,filesize
.debug_info,0,18102
.debug_loc,0,9364
.debug_line,0,8858
.shstrtab,0,685
.strtab,0,363
.debug_str,0,336
.symtab,0,160
.xt.prop._ZN4chip8Encoding22PacketBufferWriterBaseINS0_12BufferWriterEEC2EONS_6System18PacketBufferHandleE,0,116
.xt.prop._ZN4chip8Encoding22PacketBufferWriterBaseINS0_12LittleEndian12BufferWriterEEC2EONS_6System18PacketBufferHandleEj,0,76
.debug_frame,0,48
.flash.text,44,44
.xt.prop._ZN4chip11DeviceLayer37DeviceNetworkProvisioningDelegateImplD2Ev,0,40
.xt.prop._ZN4chip11DeviceLayer8Internal44GenericDeviceNetworkProvisioningDelegateImplINS0_37DeviceNetworkProvisioningDelegateImplEE15ProvisionThreadERNS1_17DeviceNetworkInfoE,0,40
.xt.prop._ZNK4chip8OptionalIyE5ValueEv,0,40
.debug_abbrev,0,34
.debug_aranges,0,16
.xt.prop._ZTVN4chip11DeviceLayer37DeviceNetworkProvisioningDelegateImplE,0,-2
.debug_ranges,0,-168


@github-actions
Copy link

Size increase report for "nrfconnect-example-build" from 3c2238a

File Section File VM
chip-lock.elf text 72 72
chip-lock.elf shell_root_cmds_sections -8 -8
chip-lighting.elf text 72 72
chip-lighting.elf shell_root_cmds_sections -8 -8
Full report output
BLOAT REPORT

Files found only in the build output:
    report.csv

Comparing ./master_artifact/chip-lock.elf and ./pull_artifact/chip-lock.elf:

sections,vmsize,filesize
.debug_info,0,19277
.debug_loc,0,7968
.debug_line,0,5807
.debug_abbrev,0,380
.debug_str,0,336
.strtab,0,313
.symtab,0,96
text,72,72
.debug_frame,0,56
.debug_aranges,0,16
.shstrtab,0,-1
shell_root_cmds_sections,-8,-8
.debug_ranges,0,-136

Comparing ./master_artifact/chip-shell.elf and ./pull_artifact/chip-shell.elf:

sections,vmsize,filesize
.debug_info,0,527
.debug_abbrev,0,270
.debug_aranges,0,-8
.debug_frame,0,-32
.debug_ranges,0,-32
.debug_line,0,-117
.debug_str,0,-120
.debug_loc,0,-272

Comparing ./master_artifact/chip-lighting.elf and ./pull_artifact/chip-lighting.elf:

sections,vmsize,filesize
.debug_info,0,19277
.debug_loc,0,7968
.debug_line,0,5807
.debug_abbrev,0,380
.debug_str,0,336
.strtab,0,313
.symtab,0,96
text,72,72
.debug_frame,0,56
.debug_aranges,0,16
.shstrtab,0,-1
shell_root_cmds_sections,-8,-8
.debug_ranges,0,-136


@@ -25,7 +26,7 @@ constexpr size_t kStatusReportMinSize = 2 + 4 + 2; ///< 16 bits for GeneralCode,
CHIP_ERROR WriteToPacketBuffer(const ::chip::bdx::BdxMessage & msgStruct, ::chip::System::PacketBufferHandle & msgBuf)
{
size_t msgDataSize = msgStruct.MessageSize();
::chip::Encoding::LittleEndian::PacketBufferWriter bbuf(msgDataSize);
::chip::Encoding::LittleEndian::PacketBufferWriter bbuf(chip::MessagePacketBuffer::New(msgDataSize));
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this also pass msgDataSize as second arg? Or document why we don't care if this overallocates?

@woody-apple woody-apple merged commit 0729778 into project-chip:master Feb 17, 2021
@kpschoedel kpschoedel deleted the pbw3 branch February 17, 2021 21:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants