umm: work on DBGLOG

This commit is contained in:
Philippe Teuwen 2020-09-01 00:15:35 +02:00
commit 4c5e981958
4 changed files with 58 additions and 26 deletions

View file

@ -80,10 +80,11 @@ void send_wtx(uint16_t wtx) {
static void umm_test(void) {
umm_info(NULL, true);
uint8_t* dest = (uint8_t*)umm_malloc(2000);
umm_free(dest);
dest = (uint8_t*)umm_malloc(12000);
umm_info(dest, false);
umm_info(dest, true);
umm_free(dest);
}

View file

@ -14,6 +14,8 @@
#include "common.h"
#include "ansi.h"
#define NOLF "\xff"
#define Dbprintf_usb(...) {\
bool tmpfpc = g_reply_via_fpc;\
bool tmpusb = g_reply_via_usb;\
@ -52,25 +54,46 @@ void print_result(const char *name, uint8_t *buf, size_t len);
// Functions for umm_malloc
// Alternatively, use https://github.com/rhempel/c-helper-macros/blob/develop/dbglog/dbglog.h
#define DBGLOG_FORCE(...) {\
Dbprintf (__VA_ARGS__); \
#define DBGLOGS_FORCE(force, format) {\
if (force) Dbprintf (format NOLF); \
}
#define DBGLOG_ERROR(...) {\
if (DBGLEVEL >= DBG_ERROR) Dbprintf (__VA_ARGS__); \
#define DBGLOG_FORCE(force, format, ...) {\
if (force) Dbprintf (format NOLF, __VA_ARGS__); \
}
#define DBGLOG_CRITICAL(...) {\
if (DBGLEVEL >= DBG_ERROR) Dbprintf (__VA_ARGS__); \
#define DBGLOGS_ERROR(format) {\
if (DBGLEVEL >= DBG_ERROR) Dbprintf (format NOLF); \
}
#define DBGLOG_DEBUG(...) {\
if (DBGLEVEL >= DBG_DEBUG) Dbprintf (__VA_ARGS__); \
#define DBGLOG_ERROR(format, ...) {\
if (DBGLEVEL >= DBG_ERROR) Dbprintf (format NOLF, __VA_ARGS__); \
}
#define DBGLOG_TRACE(...) {\
if (DBGLEVEL >= DBG_EXTENDED) Dbprintf (__VA_ARGS__); \
#define DBGLOGS_CRITICAL(format) {\
if (DBGLEVEL >= DBG_ERROR) Dbprintf (format NOLF); \
}
#define DBGLOG_32_BIT_PTR(...) __VA_ARGS__
#define DBGLOG_CRITICAL(format, ...) {\
if (DBGLEVEL >= DBG_ERROR) Dbprintf (format NOLF, __VA_ARGS__); \
}
#define DBGLOGS_DEBUG(format) {\
if (DBGLEVEL >= DBG_DEBUG) Dbprintf (format NOLF); \
}
#define DBGLOG_DEBUG(format, ...) {\
if (DBGLEVEL >= DBG_DEBUG) Dbprintf (format NOLF, __VA_ARGS__); \
}
#define DBGLOGS_TRACE(format) {\
if (DBGLEVEL >= DBG_EXTENDED) Dbprintf (format NOLF); \
}
#define DBGLOG_TRACE(format, ...) {\
if (DBGLEVEL >= DBG_EXTENDED) Dbprintf (format NOLF, __VA_ARGS__); \
}
#define DBGLOG_32_BIT_PTR(ptr) (ptr)
#endif

View file

@ -3,7 +3,7 @@
#include <stddef.h>
#include <stdbool.h>
#include <math.h>
//#include <math.h>
#ifdef UMM_INFO
/* ----------------------------------------------------------------------------
@ -40,8 +40,8 @@ void *umm_info( void *ptr, bool force ) {
*/
memset( &ummHeapInfo, 0, sizeof( ummHeapInfo ) );
DBGLOG_FORCE( force, "\n" );
DBGLOG_FORCE( force, "+----------+-------+--------+--------+-------+--------+--------+\n" );
DBGLOGS_FORCE( force, "\n" );
DBGLOGS_FORCE( force, "+----------+-------+--------+--------+-------+--------+--------+\n" );
DBGLOG_FORCE( force, "|0x%08x|B %5i|NB %5i|PB %5i|Z %5i|NF %5i|PF %5i|\n",
DBGLOG_32_BIT_PTR(&UMM_BLOCK(blockNo)),
blockNo,
@ -125,7 +125,7 @@ void *umm_info( void *ptr, bool force ) {
UMM_NFREE(blockNo),
UMM_PFREE(blockNo) );
DBGLOG_FORCE( force, "+----------+-------+--------+--------+-------+--------+--------+\n" );
DBGLOGS_FORCE( force, "+----------+-------+--------+--------+-------+--------+--------+\n" );
DBGLOG_FORCE( force, "Total Entries %5i Used Entries %5i Free Entries %5i\n",
ummHeapInfo.totalEntries,
@ -137,12 +137,12 @@ void *umm_info( void *ptr, bool force ) {
ummHeapInfo.usedBlocks,
ummHeapInfo.freeBlocks );
DBGLOG_FORCE( force, "+--------------------------------------------------------------+\n" );
DBGLOGS_FORCE( force, "+--------------------------------------------------------------+\n" );
DBGLOG_FORCE( force, "Usage Metric: %5i\n", umm_usage_metric());
DBGLOG_FORCE( force, "Fragmentation Metric: %5i\n", umm_fragmentation_metric());
DBGLOG_FORCE( force, "+--------------------------------------------------------------+\n" );
DBGLOGS_FORCE( force, "+--------------------------------------------------------------+\n" );
/* Release the critical section... */
UMM_CRITICAL_EXIT();
@ -168,12 +168,20 @@ int umm_usage_metric( void ) {
return (int)((ummHeapInfo.usedBlocks * 100)/(ummHeapInfo.freeBlocks));
}
static uint32_t sqrt(uint32_t x) {
double n=x;
while((n*n-x>0.0001)||(n*n-x<-0.0001)){
n=(n+x/n)/2;
}
return (uint32_t)n;
}
int umm_fragmentation_metric( void ) {
DBGLOG_DEBUG( "freeBlocks %i freeBlocksSquared %i\n", ummHeapInfo.freeBlocks, ummHeapInfo.freeBlocksSquared);
if (0 == ummHeapInfo.freeBlocks) {
return 0;
} else {
return (100 - (((uint32_t)(sqrtf(ummHeapInfo.freeBlocksSquared)) * 100)/(ummHeapInfo.freeBlocks)));
return (100 - ((sqrt(ummHeapInfo.freeBlocksSquared) * 100)/(ummHeapInfo.freeBlocks)));
}
}

View file

@ -185,7 +185,7 @@ static void umm_assimilate_up( uint16_t c ) {
* the free list
*/
DBGLOG_DEBUG( "Assimilate up to next block, which is FREE\n" );
DBGLOGS_DEBUG( "Assimilate up to next block, which is FREE\n" );
/* Disconnect the next block from the FREE list */
@ -315,7 +315,7 @@ static void umm_free_core( void *ptr ) {
if( UMM_NBLOCK(UMM_PBLOCK(c)) & UMM_FREELIST_MASK ) {
DBGLOG_DEBUG( "Assimilate down to previous block, which is FREE\n" );
DBGLOGS_DEBUG( "Assimilate down to previous block, which is FREE\n" );
c = umm_assimilate_down(c, UMM_FREELIST_MASK);
} else {
@ -325,7 +325,7 @@ static void umm_free_core( void *ptr ) {
*/
UMM_FRAGMENTATION_METRIC_ADD(c);
DBGLOG_DEBUG( "Just add to head of free list\n" );
DBGLOGS_DEBUG( "Just add to head of free list\n" );
UMM_PFREE(UMM_NFREE(0)) = c;
UMM_NFREE(c) = UMM_NFREE(0);
@ -347,7 +347,7 @@ void umm_free( void *ptr ) {
/* If we're being asked to free a NULL pointer, well that's just silly! */
if( (void *)0 == ptr ) {
DBGLOG_DEBUG( "free a null pointer -> do nothing\n" );
DBGLOGS_DEBUG( "free a null pointer -> do nothing\n" );
return;
}
@ -492,7 +492,7 @@ void *umm_malloc( size_t size ) {
*/
if( 0 == size ) {
DBGLOG_DEBUG( "malloc a block of 0 bytes -> do nothing\n" );
DBGLOGS_DEBUG( "malloc a block of 0 bytes -> do nothing\n" );
return( ptr );
}
@ -534,7 +534,7 @@ void *umm_realloc( void *ptr, size_t size ) {
*/
if( ((void *)NULL == ptr) ) {
DBGLOG_DEBUG( "realloc the NULL pointer - call malloc()\n" );
DBGLOGS_DEBUG( "realloc the NULL pointer - call malloc()\n" );
return( umm_malloc(size) );
}
@ -546,7 +546,7 @@ void *umm_realloc( void *ptr, size_t size ) {
*/
if( 0 == size ) {
DBGLOG_DEBUG( "realloc to 0 size, just free the block\n" );
DBGLOGS_DEBUG( "realloc to 0 size, just free the block\n" );
umm_free( ptr );