-
Notifications
You must be signed in to change notification settings - Fork 13.3k
metric for heap fragmentation #5090
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d91ca71
+Esp.getHeapUnfragness()
d-a-v 83b44d7
only in debug mode
d-a-v 26d64a4
default value
d-a-v 8f22381
always enable, 64->32, light 32 integer square root, comments
d-a-v 9bce009
fix when debugging is disabled
d-a-v 08b2304
give credits
d-a-v f55f0ba
cosmetics
d-a-v 0d2f7a0
fragmentation metric updates (doc, better api, added getMaxFreeBlockS…
d-a-v 84c92b5
api reworked, +example
d-a-v e93e9f6
read all lines from slow servers (#5113)
devyte fc96312
ESP8266HTTPClient: allow getString() more than once (#5091)
yoursunny ab5a94d
fixe types, fix names
d-a-v 19a3488
coding style fix
d-a-v 469729a
use astyle for example
d-a-v 40694d1
Merge branch 'master' into fragness
devyte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
Esp.cpp - ESP8266-specific APIs | ||
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved. | ||
This file is part of the esp8266 core for Arduino environment. | ||
|
||
This library is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU Lesser General Public | ||
License as published by the Free Software Foundation; either | ||
version 2.1 of the License, or (at your option) any later version. | ||
|
||
This library is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
Lesser General Public License for more details. | ||
|
||
You should have received a copy of the GNU Lesser General Public | ||
License along with this library; if not, write to the Free Software | ||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
#include "umm_malloc/umm_malloc.h" | ||
#include "umm_malloc/umm_malloc_cfg.h" | ||
#include "coredecls.h" | ||
#include "Esp.h" | ||
|
||
void EspClass::getHeapStats(uint32_t* hfree, uint16_t* hmax, uint8_t* hfrag) | ||
{ | ||
// L2 / Euclidian norm of free block sizes. | ||
// Having getFreeHeap()=sum(hole-size), fragmentation is given by | ||
// 100 * (1 - sqrt(sum(hole-size²)) / sum(hole-size)) | ||
|
||
umm_info(NULL, 0); | ||
uint8_t block_size = umm_block_size(); | ||
uint32_t fh = ummHeapInfo.freeBlocks * block_size; | ||
if (hfree) | ||
*hfree = fh; | ||
if (hmax) | ||
*hmax = ummHeapInfo.maxFreeContiguousBlocks * block_size; | ||
if (hfrag) | ||
*hfrag = 100 - (sqrt32(ummHeapInfo.freeSize2) * 100) / fh; | ||
} | ||
|
||
uint8_t EspClass::getHeapFragmentation() | ||
{ | ||
uint8_t hfrag; | ||
getHeapStats(nullptr, nullptr, &hfrag); | ||
return hfrag; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
|
||
#include <coredecls.h> | ||
#include <stdint.h> | ||
|
||
uint32_t sqrt32 (uint32_t n) | ||
{ | ||
// http://www.codecodex.com/wiki/Calculate_an_integer_square_root#C | ||
// Another very fast algorithm donated by [email protected] | ||
// (note: tested across the full 32 bits range, see comment below) | ||
|
||
// 15 iterations (c=1<<15) | ||
|
||
unsigned int c = 0x8000; | ||
unsigned int g = 0x8000; | ||
|
||
for(;;) | ||
{ | ||
if (g*g > n) | ||
g ^= c; | ||
c >>= 1; | ||
if (!c) | ||
return g; | ||
g |= c; | ||
} | ||
} | ||
|
||
/* | ||
* tested with: | ||
* | ||
|
||
#include <stdio.h> | ||
#include <stdint.h> | ||
#include <math.h> | ||
|
||
int main (void) | ||
{ | ||
for (uint32_t i = 0; ++i; ) | ||
{ | ||
uint32_t sr = sqrt32(i); | ||
uint32_t ifsr = sqrt(i); | ||
|
||
if (ifsr != sr) | ||
printf("%d: i%d f%d\n", i, sr, ifsr); | ||
|
||
if (!(i & 0xffffff)) | ||
{ | ||
printf("%i%% (0x%08x)\r", ((i >> 16) * 100) >> 16, i); | ||
fflush(stdout); | ||
} | ||
} | ||
|
||
printf("\n"); | ||
} | ||
|
||
* | ||
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
|
||
// nothing else than showing heap metric usage | ||
// released to public domain | ||
|
||
#include <ESP8266WiFi.h> | ||
|
||
void stats(const char* what) { | ||
// we could use getFreeHeap() getMaxFreeBlockSize() and getHeapFragmentation() | ||
// or all at once: | ||
uint32_t free; | ||
uint16_t max; | ||
uint8_t frag; | ||
ESP.getHeapStats(&free, &max, &frag); | ||
|
||
Serial.printf("free: %5d - max: %5d - frag: %3d%% <- ", free, max, frag); | ||
// %s requires a malloc that could fail, using println instead: | ||
Serial.println(what); | ||
} | ||
|
||
void tryit(int blocksize) { | ||
void** p; | ||
int blocks; | ||
|
||
// heap-used ~= blocks*sizeof(void*) + blocks*blocksize | ||
blocks = ((ESP.getMaxFreeBlockSize() / (blocksize + sizeof(void*))) + 3) & ~3; // rounded up, multiple of 4 | ||
|
||
Serial.printf("\nFilling memory with blocks of %d bytes each\n", blocksize); | ||
stats("before"); | ||
|
||
p = (void**)malloc(sizeof(void*) * blocks); | ||
for (int i = 0; i < blocks; i++) { | ||
p[i] = malloc(blocksize); | ||
} | ||
stats("array and blocks allocation"); | ||
|
||
for (int i = 0; i < blocks; i += 2) { | ||
if (p[i]) { | ||
free(p[i]); | ||
} | ||
p[i] = nullptr; | ||
} | ||
stats("freeing every other blocks"); | ||
|
||
for (int i = 0; i < blocks; i += 4) { | ||
if (p[i + 1]) { | ||
free(p[i + 1]); | ||
} | ||
p[i + 1] = nullptr; | ||
} | ||
stats("freeing every other remaining blocks"); | ||
|
||
for (int i = 0; i < blocks; i++) { | ||
if (p[i]) { | ||
free(p[i]); | ||
} | ||
} | ||
stats("freeing array"); | ||
|
||
free(p); | ||
stats("after"); | ||
} | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
WiFi.mode(WIFI_OFF); | ||
|
||
tryit(8000); | ||
tryit(4000); | ||
tryit(2000); | ||
tryit(1000); | ||
tryit(500); | ||
tryit(200); | ||
tryit(100); | ||
tryit(50); | ||
tryit(15); | ||
} | ||
|
||
void loop() { | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we sure that this sdk api is the same as:
used in getHeapStats()? After a quick look I can't tell how the integration works between that sdk api and umm.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed this is a fw call that calls fw api's
xPortGetFreeHeapSize
in which we call umm (heap.c).