Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed_stats.c Source File

mbed_stats.c

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2019 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include "platform/mbed_assert.h"
00018 #include "platform/mbed_stats.h"
00019 #include "platform/mbed_power_mgmt.h"
00020 #include "platform/mbed_version.h"
00021 #include <string.h>
00022 #include <stdlib.h>
00023 
00024 #include "device.h"
00025 #ifdef MBED_CONF_RTOS_PRESENT
00026 #include "cmsis_os2.h"
00027 #elif defined(MBED_STACK_STATS_ENABLED) || defined(MBED_THREAD_STATS_ENABLED)
00028 #warning Statistics are currently not supported without the rtos.
00029 #endif
00030 
00031 #if defined(MBED_CPU_STATS_ENABLED) && (!DEVICE_SLEEP)
00032 #warning CPU statistics are not supported without sleep support.
00033 #endif
00034 
00035 void mbed_stats_cpu_get(mbed_stats_cpu_t *stats)
00036 {
00037     MBED_ASSERT(stats != NULL);
00038     memset(stats, 0, sizeof(mbed_stats_cpu_t));
00039 #if defined(MBED_CPU_STATS_ENABLED) && DEVICE_LPTICKER && DEVICE_SLEEP
00040     stats->uptime = mbed_uptime();
00041     stats->idle_time = mbed_time_idle();
00042     stats->sleep_time = mbed_time_sleep();
00043     stats->deep_sleep_time = mbed_time_deepsleep();
00044 #endif
00045 }
00046 
00047 // note: mbed_stats_heap_get defined in mbed_alloc_wrappers.cpp
00048 void mbed_stats_stack_get(mbed_stats_stack_t *stats)
00049 {
00050     MBED_ASSERT(stats != NULL);
00051     memset(stats, 0, sizeof(mbed_stats_stack_t));
00052 
00053 #if defined(MBED_STACK_STATS_ENABLED) && defined(MBED_CONF_RTOS_PRESENT)
00054     uint32_t thread_n = osThreadGetCount();
00055     unsigned i;
00056     osThreadId_t *threads;
00057 
00058     threads = malloc(sizeof(osThreadId_t) * thread_n);
00059     // Don't fail on lack of memory
00060     if (!threads) {
00061         return;
00062     }
00063 
00064     osKernelLock();
00065     thread_n = osThreadEnumerate(threads, thread_n);
00066 
00067     for (i = 0; i < thread_n; i++) {
00068         uint32_t stack_size = osThreadGetStackSize(threads[i]);
00069         stats->max_size += stack_size - osThreadGetStackSpace(threads[i]);
00070         stats->reserved_size += stack_size;
00071         stats->stack_cnt++;
00072     }
00073     osKernelUnlock();
00074 
00075     free(threads);
00076 #endif
00077 }
00078 
00079 size_t mbed_stats_stack_get_each(mbed_stats_stack_t *stats, size_t count)
00080 {
00081     MBED_ASSERT(stats != NULL);
00082     memset(stats, 0, count * sizeof(mbed_stats_stack_t));
00083 
00084     size_t i = 0;
00085 
00086 #if defined(MBED_STACK_STATS_ENABLED) && defined(MBED_CONF_RTOS_PRESENT)
00087     osThreadId_t *threads;
00088 
00089     threads = malloc(sizeof(osThreadId_t) * count);
00090     // Don't fail on lack of memory
00091     if (!threads) {
00092         return 0;
00093     }
00094 
00095     osKernelLock();
00096     count = osThreadEnumerate(threads, count);
00097 
00098     for (i = 0; i < count; i++) {
00099         uint32_t stack_size = osThreadGetStackSize(threads[i]);
00100         stats[i].max_size = stack_size - osThreadGetStackSpace(threads[i]);
00101         stats[i].reserved_size = stack_size;
00102         stats[i].thread_id = (uint32_t)threads[i];
00103         stats[i].stack_cnt = 1;
00104     }
00105     osKernelUnlock();
00106 
00107     free(threads);
00108 #endif
00109 
00110     return i;
00111 }
00112 
00113 size_t mbed_stats_thread_get_each(mbed_stats_thread_t *stats, size_t count)
00114 {
00115     MBED_ASSERT(stats != NULL);
00116     memset(stats, 0, count * sizeof(mbed_stats_thread_t));
00117     size_t i = 0;
00118 
00119 #if defined(MBED_THREAD_STATS_ENABLED) && defined(MBED_CONF_RTOS_PRESENT)
00120     osThreadId_t *threads;
00121 
00122     threads = malloc(sizeof(osThreadId_t) * count);
00123     MBED_ASSERT(threads != NULL);
00124 
00125     osKernelLock();
00126     count = osThreadEnumerate(threads, count);
00127 
00128     for (i = 0; i < count; i++) {
00129         stats[i].id = (uint32_t)threads[i];
00130         stats[i].state = (uint32_t)osThreadGetState(threads[i]);
00131         stats[i].priority = (uint32_t)osThreadGetPriority(threads[i]);
00132         stats[i].stack_size = osThreadGetStackSize(threads[i]);
00133         stats[i].stack_space = osThreadGetStackSpace(threads[i]);
00134         stats[i].name = osThreadGetName(threads[i]);
00135     }
00136     osKernelUnlock();
00137     free(threads);
00138 #endif
00139     return i;
00140 }
00141 
00142 void mbed_stats_sys_get(mbed_stats_sys_t *stats)
00143 {
00144     MBED_ASSERT(stats != NULL);
00145     memset(stats, 0, sizeof(mbed_stats_sys_t));
00146 
00147 #if defined(MBED_SYS_STATS_ENABLED)
00148     stats->os_version = MBED_VERSION;
00149 #if defined(MBED_RAM_START) && defined(MBED_RAM_SIZE)
00150     stats->ram_start[0] = MBED_RAM_START;
00151     stats->ram_size[0] = MBED_RAM_SIZE;
00152 #endif
00153 #if defined(MBED_ROM_START) && defined(MBED_ROM_SIZE)
00154     stats->rom_start[0] = MBED_ROM_START;
00155     stats->rom_size[0] = MBED_ROM_SIZE;
00156 #endif
00157 #if defined(MBED_RAM1_START) && defined(MBED_RAM1_SIZE)
00158     stats->ram_start[1] = MBED_RAM1_START;
00159     stats->ram_size[1] = MBED_RAM1_SIZE;
00160 #endif
00161 #if defined(MBED_RAM2_START) && defined(MBED_RAM2_SIZE)
00162     stats->ram_start[2] = MBED_RAM2_START;
00163     stats->ram_size[2] = MBED_RAM2_SIZE;
00164 #endif
00165 #if defined(MBED_RAM3_START) && defined(MBED_RAM3_SIZE)
00166     stats->ram_start[3] = MBED_RAM3_START;
00167     stats->ram_size[3] = MBED_RAM3_SIZE;
00168 #endif
00169 #if defined(MBED_ROM1_START) && defined(MBED_ROM1_SIZE)
00170     stats->rom_start[1] = MBED_ROM1_START;
00171     stats->rom_size[1] = MBED_ROM1_SIZE;
00172 #endif
00173 #if defined(MBED_ROM2_START) && defined(MBED_ROM2_SIZE)
00174     stats->rom_start[2] = MBED_ROM2_START;
00175     stats->rom_size[2] = MBED_ROM2_SIZE;
00176 #endif
00177 #if defined(MBED_ROM3_START) && defined(MBED_ROM3_SIZE)
00178     stats->rom_start[3] = MBED_ROM3_START;
00179     stats->rom_size[3] = MBED_ROM3_SIZE;
00180 #endif
00181 
00182 #if defined(__CORTEX_M)
00183     stats->cpu_id = SCB->CPUID;
00184 #endif
00185 #if defined(__IAR_SYSTEMS_ICC__)
00186     stats->compiler_id = IAR;
00187     stats->compiler_version = __VER__;
00188 #elif defined(__ARMCC_VERSION)
00189     stats->compiler_id = ARM;
00190     stats->compiler_version = __ARMCC_VERSION;
00191 #elif defined(__GNUC__)
00192     stats->compiler_id = GCC_ARM;
00193     stats->compiler_version = (__GNUC__ * 10000 + __GNUC_MINOR__ * 100);
00194 #endif
00195 
00196 #endif
00197     return;
00198 }