Example application demonstrating Memory stats API and debug functions

Dependencies:   mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
SenRam
Date:
Fri Oct 27 19:50:37 2017 +0000
Commit message:
Example to demonstrate Memory stats API and debug functions

Changed in this revision

README.md Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 2ab002d36417 README.md
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md	Fri Oct 27 19:50:37 2017 +0000
@@ -0,0 +1,82 @@
+# mbed-os-example-platform-utils #
+
+Platform Utilities usage example for mbed OS
+
+This is a example showing how to use mbed-OS Debug and MemoryStats utility functions
+
+The program retrieves stack and heap usage and also demonstrates the usage of Debug APIs.
+
+### Suggested hardware for running this example ###
+
+* [K64F](https://os.mbed.com/platforms/FRDM-K64F/)
+
+##  Getting started
+
+1. Import the example
+
+   ```
+   mbed import mbed-os-example-platform-utils
+   cd mbed-os-example-platform-utils
+   ```
+2. Compile and generate binary
+
+   For example, for `ARMCC`:
+
+   ```
+   mbed compile -t arm -m k64f -DMBED_STACK_STATS_ENABLED -DMBED_HEAP_STATS_ENABLED -DMBED_MEM_TRACING_ENABLED
+   ```
+   
+   NOTE: Make sure you define the following variables:
+   MBED_STACK_STATS_ENABLED 
+   MBED_HEAP_STATS_ENABLED 
+   MBED_MEM_TRACING_ENABLED
+   
+ 5. Open a serial console session with the target platform using the following parameters:
+    * **Baud rate:** 9600
+    * **Data bits:** 8
+    * **Stop bits:** 1
+    * **Parity:** None
+ 
+ 6. Copy or drag the application `mbed-os-example-platform-utils.bin` in the folder `mbed-os-example-platform-utils/BUILD/<TARGET NAME>/<PLATFORM NAME>` onto the target board.
+ 
+ 7. The serial console should display a similar output to below:
+ ```
+This message is from debug function
+This message is from debug_if
+MemoryStats:
+        Bytes allocated currently: 80
+        Max bytes allocated at a given time: 80
+        Cumulative sum of bytes ever allocated: 80
+        Current number of bytes allocated for the heap: 186884
+        Current number of allocations: 2
+        Number of failed allocations: 0
+Cumulative Stack Info:
+        Maximum number of bytes used on the stack: 560
+        Current number of bytes allocated for the stack: 5376
+        Number of stacks stats accumulated in the structure: 3
+Thread Stack Info:
+        Thread: 0
+                Thread Id: 0x20001EE8
+                Thread Name: "main_thread"
+                Maximum number of bytes used on the stack: 384
+                Current number of bytes allocated for the stack: 4096
+                Number of stacks stats accumulated in the structure: 1
+        Thread: 1
+                Thread Id: 0x20000394
+                Thread Name: ""
+                Maximum number of bytes used on the stack: 64
+                Current number of bytes allocated for the stack: 512
+                Number of stacks stats accumulated in the structure: 1
+        Thread: 2
+                Thread Id: 0x200003DC
+                Thread Name: ""
+                Maximum number of bytes used on the stack: 112
+                Current number of bytes allocated for the stack: 768
+                Number of stacks stats accumulated in the structure: 1
+Done...
+
+ ```
+
+## Documentation ##
+
+More information on the MemoryStats and Debug API can be found in the [mbed handbook]().
diff -r 000000000000 -r 2ab002d36417 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Oct 27 19:50:37 2017 +0000
@@ -0,0 +1,61 @@
+/* Example usage for Debug, Assert, Error and MemoryStats 
+ * Copyright (c) 2016 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "mbed.h"
+#include "platform/mbed_assert.h"
+#include "platform/mbed_debug.h"
+#include "platform/mbed_error.h"
+#include "platform/mbed_stats.h"
+
+#define MAX_THREAD_INFO 10
+
+mbed_stats_heap_t heap_info;
+mbed_stats_stack_t stack_info[ MAX_THREAD_INFO ];
+int main()
+{
+    debug("\nThis message is from debug function");
+    debug_if(1,"\nThis message is from debug_if function");
+    debug_if(0,"\nSOMETHING WRONG!!! This message from debug_if function shouldn't show on bash");
+    
+    printf("\nMemoryStats:");
+    mbed_stats_heap_get( &heap_info );
+    printf("\n\tBytes allocated currently: %d", heap_info.current_size);
+    printf("\n\tMax bytes allocated at a given time: %d", heap_info.max_size);
+    printf("\n\tCumulative sum of bytes ever allocated: %d", heap_info.total_size);
+    printf("\n\tCurrent number of bytes allocated for the heap: %d", heap_info.reserved_size);
+    printf("\n\tCurrent number of allocations: %d", heap_info.alloc_cnt);
+    printf("\n\tNumber of failed allocations: %d", heap_info.alloc_fail_cnt);
+    
+    mbed_stats_stack_get( &stack_info[0] );
+    printf("\nCumulative Stack Info:");
+    printf("\n\tMaximum number of bytes used on the stack: %d", stack_info[0].max_size);
+    printf("\n\tCurrent number of bytes allocated for the stack: %d", stack_info[0].reserved_size);
+    printf("\n\tNumber of stacks stats accumulated in the structure: %d", stack_info[0].stack_cnt);
+    
+    mbed_stats_stack_get_each( stack_info, MAX_THREAD_INFO );
+    printf("\nThread Stack Info:");
+    for(int i=0;i < MAX_THREAD_INFO; i++) {
+        if(stack_info[i].thread_id != 0) {
+            printf("\n\tThread: %d", i);
+            printf("\n\t\tThread Id: 0x%08X", stack_info[i].thread_id);
+            printf("\n\t\tMaximum number of bytes used on the stack: %d", stack_info[i].max_size);
+            printf("\n\t\tCurrent number of bytes allocated for the stack: %d", stack_info[i].reserved_size);
+            printf("\n\t\tNumber of stacks stats accumulated in the structure: %d", stack_info[i].stack_cnt); 
+        }        
+    }
+    
+    printf("\nDone...\n\n");
+}
diff -r 000000000000 -r 2ab002d36417 mbed-rtos.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Fri Oct 27 19:50:37 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#5713cbbdb706
diff -r 000000000000 -r 2ab002d36417 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri Oct 27 19:50:37 2017 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/fb8e0ae1cceb
\ No newline at end of file