public Test program for usbhostmsd looking at ridiculous resource usage
Dependencies: test_USBHost mbed
Fork of USBHostMSD_HelloWorld by
Revision 9:d86a5e08abcd, committed 2015-03-19
- Comitter:
- sbts
- Date:
- Thu Mar 19 02:11:22 2015 +0000
- Parent:
- 8:758190c6c455
- Commit message:
- public Test program for usbhostmsd looking at ridiculous resource usage
Changed in this revision
diff -r 758190c6c455 -r d86a5e08abcd USBHost.lib --- a/USBHost.lib Thu Mar 14 14:23:42 2013 +0000 +++ b/USBHost.lib Thu Mar 19 02:11:22 2015 +0000 @@ -1,1 +1,1 @@ -https://mbed.org/users/mbed_official/code/USBHost/#7671b6a8c363 +http://developer.mbed.org/users/sbts/code/test_USBHost/#aff4dce323f5
diff -r 758190c6c455 -r d86a5e08abcd main.cpp --- a/main.cpp Thu Mar 14 14:23:42 2013 +0000 +++ b/main.cpp Thu Mar 19 02:11:22 2015 +0000 @@ -1,5 +1,8 @@ #include "mbed.h" #include "USBHostMSD.h" +#include "ramstats.h" + +Serial pc(USBTX, USBRX); // tx, rx DigitalOut led(LED1); @@ -35,14 +38,17 @@ // if device disconnected, try to connect again if (!msd.connected()) break; - } + break;} - } + break;} + DisplayRAMBanks("\r\nmsd thread:\r\n"); } int main() { + pc.baud(115200); Thread msdTask(msd_task, NULL, osPriorityNormal, 1024 * 4); + DisplayRAMBanks("\r\nmain thread:\r\n"); while(1) { led=!led; Thread::wait(500);
diff -r 758190c6c455 -r d86a5e08abcd mbed.bld --- a/mbed.bld Thu Mar 14 14:23:42 2013 +0000 +++ b/mbed.bld Thu Mar 19 02:11:22 2015 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/5e5da4a5990b \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/487b796308b0 \ No newline at end of file
diff -r 758190c6c455 -r d86a5e08abcd ramstats.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ramstats.cpp Thu Mar 19 02:11:22 2015 +0000 @@ -0,0 +1,198 @@ +#include "ramstats.h" +#include "mbed.h" +#include <stdarg.h> + +//extern USBSerial DebugComms; + +//_sys_clock(); + +/*int myfprintf(FILE *stream, const char *format, const char *arg1) { + int done; + const char *arg; + concat(art1 + done=fprintf(stream,format,arg); + return done; +}*/ +int myfprintf(FILE *stream, const char *format, ...) { + va_list arg; + int done; + va_start (arg, format); + done=vfprintf(stream,format,arg); + va_end (arg); + fprintf(stream,"\r"); + fflush(stream); + return done; +} +// Displays the size of static allocations for each RAM bank as indicated by +// ARM linker to stdout. +//static void DisplayRAMBanks(void) +void DisplayRAMBanks(char * header) +{ +//void __heapstats(int (*dprint)( void*param, char const *format,...), void* param); + printf("\e[2J\e[1;33;44m %s",header); // clear the screen before starting + printf("\r\n==== Heap Stats ====\r\n"); + __heapstats((__heapprt)myfprintf,stdout); + printf("\r\n==== Heap Valid ====\r\n"); + __heapvalid((__heapprt)myfprintf,stdout, 1); + printf("\r\n==== Current Stack Pointer ===="); + printf("==== %10x ====\r\n\n",__current_sp()); + + printf("Static RAM bank allocations\r\n"); + printf(" Main RAM = %5u %04x\r\n", (unsigned int)&Image$$RW_IRAM1$$ZI$$Limit - (unsigned int)&Image$$RW_IRAM1$$Base, (unsigned int)&Image$$RW_IRAM1$$ZI$$Limit - (unsigned int)&Image$$RW_IRAM1$$Base); + printf(" RAM0 = %5u %04x\r\n", (unsigned int)&Image$$RW_IRAM2$$ZI$$Limit - (unsigned int)&Image$$RW_IRAM2$$Base, (unsigned int)&Image$$RW_IRAM2$$ZI$$Limit - (unsigned int)&Image$$RW_IRAM2$$Base); + printf(" RAM1 = %5u %04x\r\n", (unsigned int)&Image$$RW_IRAM3$$ZI$$Limit - (unsigned int)&Image$$RW_IRAM3$$Base, (unsigned int)&Image$$RW_IRAM3$$ZI$$Limit - (unsigned int)&Image$$RW_IRAM3$$Base); +// printf("\r\n\nPress 'M' to resume"); + printf("\r\n\n"); + +} + + +/* + To move a global from main RAM to the RAM0/RAM1 bank, you can declare it something like: + __attribute((section("AHBSRAM0"),aligned)) char LargeBuffer[1024]; + __attribute((section("AHBSRAM1"),aligned)) char LargeBuffer[1024]; + char bufA[10000] __attribute__((section("AHBSRAM1"))); + +*/ + + +// *********************************************************************** +// *********************************************************************** +// *********************************************************************** +// Examples of Alternative methods to capture and process this info.... +// *********************************************************************** +// *********************************************************************** +// *********************************************************************** +// the first two are from http://developer.mbed.org/forum/mbed/topic/2702/ +/* +#include <mbed.h> +#include <stdarg.h> + +int CaptureLine(void* pBuffer, char const* pFormatString, ...) +{ + char* pStringEnd = (char*)pBuffer + strlen((char*)pBuffer); + va_list valist; + + va_start(valist, pFormatString); + + return vsprintf(pStringEnd, pFormatString, valist); +} + +int main() +{ + char OutputBuffer[256]; + + printf("\r\nBefore malloc.\r\n"); + OutputBuffer[0] = '\0'; + __heapstats(CaptureLine, OutputBuffer); + printf("%s", OutputBuffer); + + void* pvTest = malloc(1024); + + printf("After malloc.\r\n"); + OutputBuffer[0] = '\0'; + __heapstats(CaptureLine, OutputBuffer); + printf("%s", OutputBuffer); + + free(pvTest); + + printf("After free.\r\n"); + OutputBuffer[0] = '\0'; + __heapstats(CaptureLine, OutputBuffer); + printf("%s", OutputBuffer); + + return 0; +} +*/ + +// *********************************************************************** +// *********************************************************************** +/* +#include <mbed.h> +#include <stdarg.h> + +struct SHeapInfo +{ + const char* HighestAllocBlock; + const char* HighestFreeBlock; +}; + +int CaptureLine(void* pvHeapInfo, char const* pFormatString, ...) +{ + static const char* pAllocFormatString = "alloc block %p size %3lx"; + static const char* pFreeFormatString = "free block %p size %3lx next=%p"; + static const char* pCompleteFormatString = "------- heap validation complete"; + SHeapInfo* pHeapInfo = (SHeapInfo*)pvHeapInfo; + va_list valist; + + va_start(valist, pFormatString); + + if (pFormatString == strstr(pFormatString, pAllocFormatString)) + { + const char* pBlock = va_arg(valist, const char*); + unsigned long BlockSize = va_arg(valist, unsigned long); + const char* pBlockLastByte = pBlock + BlockSize - 1; + + if (pBlockLastByte > pHeapInfo->HighestAllocBlock) + { + pHeapInfo->HighestAllocBlock = pBlockLastByte; + } + } + else if (pFormatString == strstr(pFormatString, pFreeFormatString)) + { + const char* pBlock = va_arg(valist, const char*); + unsigned long BlockSize = va_arg(valist, unsigned long); + const char* pBlockLastByte = pBlock + BlockSize - 1; + + if (pBlockLastByte > pHeapInfo->HighestFreeBlock) + { + pHeapInfo->HighestFreeBlock = pBlockLastByte; + } + } + else if (pFormatString == strstr(pFormatString, pCompleteFormatString)) + { + // Ignoring end of dump string. + } + else + { + // Unrecognized format string. + printf("Unrecognized format of %s", pFormatString); + } + + return 1; +} + +void DisplayAndClearHeapInfo(SHeapInfo* pHeapInfo) +{ + printf("Highest allocated block address: %p\r\n", pHeapInfo->HighestAllocBlock); + printf("Highest free block address: %p\r\n", pHeapInfo->HighestFreeBlock); + + memset(pHeapInfo, 0, sizeof(*pHeapInfo)); +} + +int main() +{ + SHeapInfo HeapInfo = { 0, 0 }; + + printf("\r\nBefore malloc.\r\n"); + __heapvalid(CaptureLine, &HeapInfo, 1); + DisplayAndClearHeapInfo(&HeapInfo); + + void* pvTest = malloc(1024); + + printf("After malloc.\r\n"); + __heapvalid(CaptureLine, &HeapInfo, 1); + DisplayAndClearHeapInfo(&HeapInfo); + + free(pvTest); + + printf("After free.\r\n"); + __heapvalid(CaptureLine, &HeapInfo, 1); + DisplayAndClearHeapInfo(&HeapInfo); + + return 0; +} +*/ + +// *********************************************************************** +// ***********************************************************************
diff -r 758190c6c455 -r d86a5e08abcd ramstats.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ramstats.h Thu Mar 19 02:11:22 2015 +0000 @@ -0,0 +1,21 @@ +#ifndef RAMSTATS_H + #define RAMSTATS_H +//http://mbed.org/forum/bugs-suggestions/topic/2322/ + +// important info on reducing heap useage by converting to static and const where possible +// https://mbed.org/forum/mbed/topic/2698/ +// http://mbed.org/handbook/Memory-Model + +// These external symbols are maintained by the linker to indicate the +// location of various regions in the device's memory. They will be used by +// DisplayRAMBanks() to dump the size of each RAM bank to stdout. +extern unsigned int Image$$RW_IRAM1$$Base; +extern unsigned int Image$$RW_IRAM1$$ZI$$Limit; +extern unsigned int Image$$RW_IRAM2$$Base; +extern unsigned int Image$$RW_IRAM2$$ZI$$Limit; +extern unsigned int Image$$RW_IRAM3$$Base; +extern unsigned int Image$$RW_IRAM3$$ZI$$Limit; + +void DisplayRAMBanks(char * header); + +#endif //RAMSTATS_H \ No newline at end of file