-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
metric for heap fragmentation (#5090)
* +Esp.getHeapUnfragness() * only in debug mode * default value * always enable, 64->32, light 32 integer square root, comments * fix when debugging is disabled * give credits * cosmetics * fragmentation metric updates (doc, better api, added getMaxFreeBlockSize()) * api reworked, +example * fixe types, fix names * coding style fix * use astyle for example
- Loading branch information
Showing
11 changed files
with
227 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 Tristan.Muntsinger@gmail.com | ||
// (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 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 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 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 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 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 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