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

Restructure CHIP_ERROR numeric values #8179

Merged
merged 5 commits into from
Jul 12, 2021

Conversation

kpschoedel
Copy link
Contributor

Problem

Large constants hurt code density on small processors, and CHIP_ERROR
constants are very common.

Summary of Changes

Changed the internal structure of CHIP_ERROR numbers so that commonly
used constants are small. The new representation uses bit fields managed
by a helper class, ::chip::ChipError.

Several examples have been using CHIP_CONFIG_CORE_ERROR_MAX as a
single application-level error code. Since this no longer exists, and
the new representation makes it straightforward, a portion of the
CHIP_ERROR space is now explicitly made available for SDK users.

Converted remaining uses of int for CHIP_ERROR values.

Changed error constant definitions to hexadecimal to make them easier to find
given the bit field representation.

Testing

No changes to functionality. Manual sanity test with chip-tool and
all-clusters-app.

@bzbarsky-apple
Copy link
Contributor

@kpschoedel @andreilitvin Any idea why we're not getting codesize numbers here?

@bzbarsky-apple
Copy link
Contributor

Any idea why we're not getting codesize numbers here?

Probably because of the thing #8189 is trying to fix: no completed bloat checks in the last 13 hours!

@github-actions
Copy link

github-actions bot commented Jul 8, 2021

Size increase report for "nrfconnect-example-build" from 8b5b24f

File Section File VM
chip-lock.elf device_handles 8 8
chip-lock.elf rodata -8 -8
chip-lock.elf text -1464 -1464
chip-shell.elf device_handles -4 -4
chip-shell.elf rodata -8 -8
chip-shell.elf text -476 -476
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,100235
.debug_abbrev,0,9218
.debug_line,0,951
.debug_str,0,742
.debug_ranges,0,280
.debug_loc,0,154
device_handles,8,8
.debug_frame,0,-4
.debug_aranges,0,-8
rodata,-8,-8
.symtab,0,-64
text,-1464,-1464

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

sections,vmsize,filesize
.debug_info,0,49803
.debug_abbrev,0,5395
.debug_line,0,1418
.debug_str,0,742
.debug_loc,0,602
.debug_ranges,0,136
device_handles,-4,-4
.debug_aranges,0,-8
.debug_frame,0,-8
rodata,-8,-8
.symtab,0,-32
text,-476,-476


@github-actions
Copy link

github-actions bot commented Jul 8, 2021

Size increase report for "esp32-example-build" from 8b5b24f

File Section File VM
chip-persistent-storage.elf .flash.text -20 -20
Full report output
BLOAT REPORT

Files found only in the build output:
    report.csv

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

BLOAT EXECUTION FAILED WITH CODE 1:
bloaty: integer overflow

Comparing ./master_artifact/chip-persistent-storage.elf and ./pull_artifact/chip-persistent-storage.elf:

sections,vmsize,filesize
.debug_info,0,3214
.debug_str,0,797
.debug_abbrev,0,445
.debug_loc,0,29
[Unmapped],0,20
.debug_ranges,0,-16
.flash.text,-20,-20
.debug_line,0,-229

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

sections,vmsize,filesize
.debug_str,0,1030
.debug_info,0,677
.debug_abbrev,0,36
.debug_loc,0,-1
.debug_line,0,-66

Comparing ./master_artifact/chip-temperature-measurement-app.elf and ./pull_artifact/chip-temperature-measurement-app.elf:

BLOAT EXECUTION FAILED WITH CODE 1:
bloaty: integer overflow

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

BLOAT EXECUTION FAILED WITH CODE 1:
bloaty: integer overflow

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

BLOAT EXECUTION FAILED WITH CODE 1:
bloaty: integer overflow


@github-actions
Copy link

github-actions bot commented Jul 8, 2021

Size increase report for "gn_qpg-example-build" from 8b5b24f

File Section File VM
chip-qpg6100-lighting-example.out .text -2896 -2896
Full report output
BLOAT REPORT

Files found only in the build output:
    report.csv

Comparing ./master_artifact/chip-qpg6100-lighting-example.out and ./pull_artifact/chip-qpg6100-lighting-example.out:

sections,vmsize,filesize
.debug_info,0,98115
.debug_abbrev,0,7738
[Unmapped],0,2896
.debug_str,0,707
.debug_line,0,413
.debug_ranges,0,232
.debug_aranges,0,-16
.debug_frame,0,-48
.debug_loc,0,-1941
.text,-2896,-2896
.symtab,0,-4128

Comparing ./master_artifact/chip-qpg6100-lighting-example.out.map and ./pull_artifact/chip-qpg6100-lighting-example.out.map:

BLOAT EXECUTION FAILED WITH CODE 1:
bloaty: unknown file type for file './pull_artifact/chip-qpg6100-lighting-example.out.map'


@andy31415
Copy link
Contributor

andy31415 commented Jul 8, 2021

@kpschoedel - it looks like darwin/Framwork/CHIP/ChipError.h redefines CHIP_ERROR :(

@sagar-apple - any ideas how to resolve this? Eventually on large platforms (darwin, linux, ios, android) we would want CHIP_ERROR to be a class, so we ca attach line information of origin to them. This would require a 'single definition' place.

{
heap_trace_dump();
streamer_printf(streamer_get(), "Free heap %d/%d\n", heap_caps_get_free_size(MALLOC_CAP_8BIT),
heap_caps_get_total_size(MALLOC_CAP_8BIT));
return 0;
return CHIP_ERROR;
Copy link
Contributor

@mspang mspang Jul 8, 2021

Choose a reason for hiding this comment

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

CHIP_NO_ERROR?

(I guess this isn't being compiled?)

@sagar-apple
Copy link
Contributor

@kpschoedel - it looks like darwin/Framwork/CHIP/ChipError.h redefines CHIP_ERROR :(

@sagar-apple - any ideas how to resolve this? Eventually on large platforms (darwin, linux, ios, android) we would want CHIP_ERROR to be a class, so we ca attach line information of origin to them. This would require a 'single definition' place.

CHIPError is already a class in the darwin Framework but as you've pointed out, that class doesn't map to the underlying CHIP_ERROR correctly. This is a known problem. I'm unsure if there's a straightforward solution here. Errors that we want to allow consumers of the SDK to use(for case handling and such) need to be exposed as part of the CHIPErrorDomain in ObjC, afaik that involves a redefining them (maybe we can declare the enum values to map to CHIP_ERROR values?). For all other error types, we already just map them to "unknown" and print out the underlying CHIP_ERROR in the description.

As a start if we can define a underlying object for CHIP_ERROR with description and code information, I think we should be able to rewrite the ObjC CHIPError to just more directly wrap the underlying CHIP_ERROR objects while updating the values in the enum in ObjC to be equal to the raw CHIP_ERROR ones.

@kpschoedel
Copy link
Contributor Author

For this particular PR I think we can just make the Darwin integer typedef match (assuming there are no more lurking signed/unsigned issues).

@andy31415
Copy link
Contributor

@kpschoedel - do we need the qpg6100 changes in this PR?

@kpschoedel
Copy link
Contributor Author

@kpschoedel - do we need the qpg6100 changes in this PR?

No, it's just that git and I hate each other.

/// Construct a `CHIP_ERROR` encapsulating @a value inside the @a range.
static BaseType Encapsulate(Range range, BaseType value)
{
return MakeInteger(range, (value & MakeMask(kValueStart, kValueLength)));
Copy link
Contributor

Choose a reason for hiding this comment

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

So this is relying on kValueStart being 0, right? Otherwise I'd expect to see:

Suggested change
return MakeInteger(range, (value & MakeMask(kValueStart, kValueLength)));
return MakeInteger(range, (value & MakeMask(0, kValueLength)));

Otherwise you are effectively left-shifting by kValueLength after taking the bits that are already shifted by kValueLength,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, good catch.

static constexpr BaseType MakeInteger(SdkPart part, BaseType code)
{
return MakeInteger(Range::kSDK,
MakeField(kSdkPartStart, static_cast<std::underlying_type<SdkPart>::type>(part)) |
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
MakeField(kSdkPartStart, static_cast<std::underlying_type<SdkPart>::type>(part)) |
MakeField(kSdkPartStart, to_underlying(part)) |

after including lib/support/TypeTraits.h

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, that's a nice addition.

template <SdkPart part, BaseType code>
struct MakeSdkErrorConstant
{
static_assert(FitsInField(kSdkPartLength, static_cast<std::underlying_type<SdkPart>::type>(part)), "part is too large");
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
static_assert(FitsInField(kSdkPartLength, static_cast<std::underlying_type<SdkPart>::type>(part)), "part is too large");
static_assert(FitsInField(kSdkPartLength, to_underlying(part)), "part is too large");

// CHIP_SYSTEM_LWIP_ERROR_MAX all fits inside err_t. See static_assert in
// MapErrorLwIP.
const err_t lError = static_cast<err_t>(-lErrorWithoutOffset);
const err_t lError = static_cast<err_t>(-ChipError::GetValue(aError));
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
const err_t lError = static_cast<err_t>(-ChipError::GetValue(aError));
const err_t lError = -static_cast<err_t>(ChipError::GetValue(aError));

to undo the operations we did in MapErrorLwIP, where we negated, then cast.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ack

#### Problem

Large constants hurt code density on small processors, and `CHIP_ERROR`
constants are very common.

#### Summary of Changes

The internal details of `CHIP_ERROR` numbers change so that commonly
used constants are small. The new representation uses bit fields managed
by a helper class, `::chip::ChipError`.

Several examples have been using `CHIP_CONFIG_CORE_ERROR_MAX` as a
single application-level error code. Since this no longer exists, and
the new representation makes it straightforward, a portion of the
`CHIP_ERROR` space is now explicitly available for SDK users.

#### Testing

No changes to functionality. Manual sanity test with chip-tool and
all-clusters-app.
@mspang mspang merged commit 6ed99f4 into project-chip:master Jul 12, 2021
@kpschoedel kpschoedel deleted the error-bitmask branch July 12, 2021 20:03
nikita-s-wrk pushed a commit to nikita-s-wrk/connectedhomeip that referenced this pull request Sep 23, 2021
* Restructure CHIP_ERROR numeric values

#### Problem

Large constants hurt code density on small processors, and `CHIP_ERROR`
constants are very common.

#### Summary of Changes

The internal details of `CHIP_ERROR` numbers change so that commonly
used constants are small. The new representation uses bit fields managed
by a helper class, `::chip::ChipError`.

Several examples have been using `CHIP_CONFIG_CORE_ERROR_MAX` as a
single application-level error code. Since this no longer exists, and
the new representation makes it straightforward, a portion of the
`CHIP_ERROR` space is now explicitly available for SDK users.

#### Testing

No changes to functionality. Manual sanity test with chip-tool and
all-clusters-app.

* fix examples/platform/esp32/shell_extension/heap_trace.cpp

* Darwin typedef

* review

* zap regen
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.

6 participants