|
| 1 | +/* |
| 2 | + Esp.cpp - ESP8266-specific APIs |
| 3 | + Copyright (c) 2015 Ivan Grokhotkov. All rights reserved. |
| 4 | + This file is part of the esp8266 core for Arduino environment. |
| 5 | +
|
| 6 | + This library is free software; you can redistribute it and/or |
| 7 | + modify it under the terms of the GNU Lesser General Public |
| 8 | + License as published by the Free Software Foundation; either |
| 9 | + version 2.1 of the License, or (at your option) any later version. |
| 10 | +
|
| 11 | + This library is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + Lesser General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU Lesser General Public |
| 17 | + License along with this library; if not, write to the Free Software |
| 18 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | + */ |
| 20 | + |
| 21 | +#include "umm_malloc/umm_malloc.h" |
| 22 | +#include "umm_malloc/umm_malloc_cfg.h" |
| 23 | +#include "coredecls.h" |
| 24 | +#include "Esp.h" |
| 25 | + |
| 26 | +void EspClass::getHeapStats(uint32_t* hfree, uint16_t* hmax, uint8_t* hfrag) |
| 27 | +{ |
| 28 | + // L2 / Euclidian norm of free block sizes. |
| 29 | + // Having getFreeHeap()=sum(hole-size), fragmentation is given by |
| 30 | + // 100 * (1 - sqrt(sum(hole-size²)) / sum(hole-size)) |
| 31 | + |
| 32 | + umm_info(NULL, 0); |
| 33 | + uint8_t block_size = umm_block_size(); |
| 34 | + uint32_t fh = ummHeapInfo.freeBlocks * block_size; |
| 35 | + if (hfree) |
| 36 | + *hfree = fh; |
| 37 | + if (hmax) |
| 38 | + *hmax = ummHeapInfo.maxFreeContiguousBlocks * block_size; |
| 39 | + if (hfrag) |
| 40 | + *hfrag = 100 - (sqrt32(ummHeapInfo.freeSize2) * 100) / fh; |
| 41 | +} |
| 42 | + |
| 43 | +uint8_t EspClass::getHeapFragmentation() |
| 44 | +{ |
| 45 | + uint8_t hfrag; |
| 46 | + getHeapStats(nullptr, nullptr, &hfrag); |
| 47 | + return hfrag; |
| 48 | +} |
0 commit comments