NuMaker Pelion Device Management example

Fork of mbed-os-example-pelion by cc li

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mem_stats.cpp Source File

mem_stats.cpp

00001 /* 
00002  * Copyright (c) 2019 Nuvoton Technology Corporation
00003  *
00004  * SPDX-License-Identifier: Apache-2.0
00005  *
00006  * Licensed under the Apache License, Version 2.0 (the "License");
00007  * you may not use this file except in compliance with the License.
00008  * You may obtain a copy of the License at
00009  *
00010  *     http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing, software
00013  * distributed under the License is distributed on an "AS IS" BASIS,
00014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  * See the License for the specific language governing permissions and
00016  * limitations under the License.
00017  */
00018 
00019 #include "mbed.h"
00020 
00021 #if (MBED_HEAP_STATS_ENABLED) || (MBED_STACK_STATS_ENABLED)
00022 /* Measure memory footprint */
00023 #include "mbed_stats.h"
00024 /* Fix up the compilation on AMRCC for PRIu32 */
00025 #define __STDC_FORMAT_MACROS
00026 #include <inttypes.h>
00027 #endif
00028 
00029 /* Support memory footprint */
00030 
00031 /* Check weak reference/definition at the link:
00032  * http://www.keil.com/support/man/docs/ARMLINK/armlink_pge1362065917715.htm */
00033 
00034 extern "C" {
00035 #if (MBED_HEAP_STATS_ENABLED)
00036     MBED_USED void print_heap_stats(void);
00037 #endif
00038 #if (MBED_STACK_STATS_ENABLED)
00039     MBED_USED void print_stack_statistics();
00040 #endif
00041 }
00042 
00043 #if (MBED_HEAP_STATS_ENABLED)
00044 void print_heap_stats(void)
00045 {
00046     mbed_stats_heap_t stats;
00047     mbed_stats_heap_get(&stats);
00048     printf("** MBED HEAP STATS **\n");
00049     printf("**** current_size: %" PRIu32 "\n", stats.current_size);
00050     printf("**** max_size    : %" PRIu32 "\n", stats.max_size);
00051     printf("*****************************\n\n");
00052 }
00053 #endif  // MBED_HEAP_STATS_ENABLED
00054 
00055 #if (MBED_STACK_STATS_ENABLED)
00056 void print_stack_statistics()
00057 {
00058     printf("** MBED THREAD STACK STATS **\n");
00059     int cnt = osThreadGetCount();
00060     mbed_stats_stack_t *stats = (mbed_stats_stack_t*) malloc(cnt * sizeof(mbed_stats_stack_t));
00061 
00062     if (stats) {
00063         cnt = mbed_stats_stack_get_each(stats, cnt);
00064         for (int i = 0; i < cnt; i++) {
00065             printf("Thread: 0x%" PRIx32 ", Stack size: %" PRIu32 ", Max stack: %" PRIu32 "\r\n", stats[i].thread_id, stats[i].reserved_size, stats[i].max_size);
00066         }
00067 
00068         free(stats);
00069     }
00070     printf("*****************************\n\n");
00071 }
00072 #endif  // MBED_STACK_STATS_ENABLED