Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed_stats_helper.c Source File

mbed_stats_helper.c

00001 // ----------------------------------------------------------------------------
00002 // Copyright 2016-2017 ARM Ltd.
00003 //  
00004 // Licensed under the Apache License, Version 2.0 (the "License");
00005 // you may not use this file except in compliance with the License.
00006 // You may obtain a copy of the License at
00007 //  
00008 //     http://www.apache.org/licenses/LICENSE-2.0
00009 //  
00010 // Unless required by applicable law or agreed to in writing, software
00011 // distributed under the License is distributed on an "AS IS" BASIS,
00012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 // See the License for the specific language governing permissions and
00014 // limitations under the License.
00015 // ----------------------------------------------------------------------------
00016 
00017 #if defined(MBED_HEAP_STATS_ENABLED) || defined(MBED_STACK_STATS_ENABLED)
00018 
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 #include <string.h>
00022 #include <inttypes.h>
00023 #include "mbed_stats.h"
00024 #include "mbed_stats_helper.h"
00025 #include "pal.h"
00026 #include <string.h>
00027 #if MBED_CONF_RTOS_PRESENT
00028 #include "cmsis_os2.h"
00029 #endif
00030 
00031 #define MBED_STATS_FILE_NAME "/mbedos_stats.txt"
00032 
00033 /**
00034     Print mbed-os stack and heap usage to stdout and to text file named mbedos_stats.txt.
00035     Note: Make sure MBED_HEAP_STATS_ENABLED or MBED_STACK_STATS_ENABLED flags are defined
00036 */
00037 void print_mbed_stats(void)
00038 {
00039     palStatus_t pal_result = PAL_SUCCESS;
00040     // Max size of directory acquired by pal_fsGetMountPoint + file name (including '/') + '\0'
00041     char file_name[PAL_MAX_FOLDER_DEPTH_CHAR + sizeof(MBED_STATS_FILE_NAME)] = { 0 };
00042 
00043     pal_result = pal_fsGetMountPoint(PAL_FS_PARTITION_PRIMARY, PAL_MAX_FOLDER_DEPTH_CHAR + 1, file_name);
00044     if (pal_result != PAL_SUCCESS) {
00045         return;
00046     }
00047 
00048     strcat(file_name, MBED_STATS_FILE_NAME);
00049     FILE *f = fopen(file_name, "wt");
00050     if (f) {
00051         // Heap
00052 #if defined(MBED_HEAP_STATS_ENABLED)
00053         mbed_stats_heap_t heap_stats;
00054         mbed_stats_heap_get(&heap_stats);
00055         fprintf(f, "Current heap usage size: %" PRIu32 "\n", heap_stats.current_size);
00056         printf("Current heap usage size: %" PRIu32 "\n", heap_stats.current_size);
00057         fprintf(f, "Max heap usage size: %" PRIu32 "\n", heap_stats.max_size);
00058         printf("Max heap usage size: %" PRIu32 "\n", heap_stats.max_size);
00059 #endif
00060         // Stacks
00061 #if defined(MBED_STACK_STATS_ENABLED) && defined(MBED_CONF_RTOS_PRESENT)
00062         int cnt = osThreadGetCount();
00063         mbed_stats_stack_t *stack_stats = (mbed_stats_stack_t*)malloc(cnt * sizeof(mbed_stats_stack_t));
00064 
00065         if (stack_stats) {
00066             fprintf(f, "Thread's stack usage:\n");
00067             printf("Thread's stack usage:\n");
00068             cnt = mbed_stats_stack_get_each(stack_stats, cnt);
00069             for (int i = 0; i < cnt; i++) {
00070                 fprintf(f, "Thread: 0x%" PRIx32 ", Stack size: %" PRIu32 ", Max stack: %" PRIu32 "\r\n", stack_stats[i].thread_id, stack_stats[i].reserved_size, stack_stats[i].max_size);
00071                 printf("Thread: 0x%" PRIx32 ", Stack size: %" PRIu32 ", Max stack: %" PRIu32 "\r\n", stack_stats[i].thread_id, stack_stats[i].reserved_size, stack_stats[i].max_size);
00072             }
00073             free(stack_stats);
00074         }
00075 #endif
00076         fclose(f);
00077         printf("*****************************\n\n");
00078     }
00079 
00080 }
00081 #endif