Skip to content

Conversation

rajkan01
Copy link
Contributor

@rajkan01 rajkan01 commented Aug 24, 2020

Summary of changes

overhead_size heap stats element was added to maintain real malloc internal overhead size but Mbed OS assumes that ( malloc returned address - 4 ) stores the actual allocated block size( including internal overhead size), but GCC_ARM newlib and newlib nano this location is used for padding when non-alignment memory allocation requested and it stores the negative offset.

So updated Mbed OS allocated block size calculation like when reading from ( malloc returned address - 4 ) is non-negative value consider to be an allocated block size. If it is a negative value ( offset ), then add this negative value into (malloc returned address - 4 ) and read the allocated block size from that location.

Fixes #13324

Impact of changes

With these changes, Mbed OS overhead_size reports proper malloc internal overhead size when using GCC_ARM with newlib-nano

Migration actions required

None.

Documentation

None.


Pull request type

[x] Patch update (Bug fix / Target update / Docs update / Test update / Refactor)
[] Feature update (New feature / Functionality change / New API)
[] Major update (Breaking change E.g. Return code change / API behaviour change)

Test results

[] No Tests required for this change (E.g docs only update)
[x] Covered by existing mbed-os tests (Greentea or Unittest)
[] Tests / results supplied as part of this PR

Reviewers

@evedon


@ciarmcom ciarmcom added the release-type: patch Indentifies a PR as containing just a patch label Aug 24, 2020
@ciarmcom ciarmcom requested review from evedon and a team August 24, 2020 15:00
@ciarmcom
Copy link
Member

@rajkan01, thank you for your changes.
@evedon @ARMmbed/mbed-os-core @ARMmbed/mbed-os-maintainers please review.

@rajkan01 rajkan01 changed the title Fix malloc internal overhead size calculation for newlib and newlib-nano Fix heap stats overhead_size calculation Aug 24, 2020
if (c->size < 0) {
c = (malloc_internal_overhead_t *)((char *)c + c->size);
}
return (c->size & ~0x1);
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 not sure why & ~0x1 is required.

Copy link
Contributor Author

@rajkan01 rajkan01 Aug 24, 2020

Choose a reason for hiding this comment

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

When using the newlib, overhead size (total allocated block size + size of overhead) value always size + 1 (Bug in newlib malloc implementation) so added a workaround size & ~0x1 logic as memory allocation is always aligned with 8 bytes block. Even this logic was there on existing code https://github.com/ARMmbed/mbed-os/blob/master/platform/source/mbed_alloc_wrappers.cpp#L61 .

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 that the LSB is used for usage flags so we have to mask it.

@ciarmcom ciarmcom requested review from evedon and a team August 24, 2020 17:30
@ciarmcom
Copy link
Member

@rajkan01, thank you for your changes.
@evedon @ARMmbed/mbed-os-core @ARMmbed/mbed-os-maintainers please review.

@rajkan01 rajkan01 force-pushed the fix_malloc_overhead_calculation branch from 75aaa5b to f10c97e Compare August 25, 2020 11:07
evedon
evedon previously requested changes Aug 25, 2020
Copy link
Contributor

@evedon evedon left a comment

Choose a reason for hiding this comment

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

Can you provide a generic solution for all toolchains?

@rajkan01 rajkan01 force-pushed the fix_malloc_overhead_calculation branch from f10c97e to ec2ca2e Compare September 1, 2020 13:38
@mergify mergify bot dismissed evedon’s stale review September 1, 2020 13:39

Pull request has been modified.

@rajkan01 rajkan01 force-pushed the fix_malloc_overhead_calculation branch from ec2ca2e to f3c9132 Compare September 1, 2020 13:46
@rajkan01 rajkan01 requested a review from evedon September 1, 2020 14:53
Copy link
Contributor

@evedon evedon left a comment

Choose a reason for hiding this comment

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

Looks good

@mergify mergify bot added needs: CI and removed needs: work labels Sep 2, 2020
@rajkan01
Copy link
Contributor Author

rajkan01 commented Sep 2, 2020

@0xc0170 Could you review this PR and start the CI

@0xc0170
Copy link
Contributor

0xc0170 commented Sep 3, 2020

CI started

@mbed-ci
Copy link

mbed-ci commented Sep 3, 2020

Jenkins CI Test : ✔️ SUCCESS

Build Number: 1 | 🔒 Jenkins CI Job | 🌐 Logs & Artifacts

CLICK for Detailed Summary

jobs Status
jenkins-ci/mbed-os-ci_unittests ✔️
jenkins-ci/mbed-os-ci_build-ARM ✔️
jenkins-ci/mbed-os-ci_build-GCC_ARM ✔️
jenkins-ci/mbed-os-ci_dynamic-memory-usage ✔️
jenkins-ci/mbed-os-ci_cloud-client-pytest ✔️
jenkins-ci/mbed-os-ci_greentea-test ✔️

@mergify mergify bot removed the needs: CI label Sep 3, 2020
@rajkan01
Copy link
Contributor Author

rajkan01 commented Sep 3, 2020

@0xc0170 Thanks for your review. This PR is ready to merge, please merge

@0xc0170 0xc0170 merged commit 4a68b8a into ARMmbed:master Sep 3, 2020
@mergify mergify bot removed the ready for merge label Sep 3, 2020
@mbedmain mbedmain added release-version: 6.3.0 Release-pending and removed release-type: patch Indentifies a PR as containing just a patch Release-pending labels Sep 14, 2020
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.

Bug in memory statistics
6 participants