mbed-os5 only for TYBLE16
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
platform/source/mbed_stats.c@1:9db0e321a9f4, 2019-12-31 (annotated)
- Committer:
- kenjiArai
- Date:
- Tue Dec 31 06:02:27 2019 +0000
- Revision:
- 1:9db0e321a9f4
updated based on mbed-os5.15.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 1:9db0e321a9f4 | 1 | /* mbed Microcontroller Library |
kenjiArai | 1:9db0e321a9f4 | 2 | * Copyright (c) 2006-2019 ARM Limited |
kenjiArai | 1:9db0e321a9f4 | 3 | * SPDX-License-Identifier: Apache-2.0 |
kenjiArai | 1:9db0e321a9f4 | 4 | * |
kenjiArai | 1:9db0e321a9f4 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kenjiArai | 1:9db0e321a9f4 | 6 | * you may not use this file except in compliance with the License. |
kenjiArai | 1:9db0e321a9f4 | 7 | * You may obtain a copy of the License at |
kenjiArai | 1:9db0e321a9f4 | 8 | * |
kenjiArai | 1:9db0e321a9f4 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
kenjiArai | 1:9db0e321a9f4 | 10 | * |
kenjiArai | 1:9db0e321a9f4 | 11 | * Unless required by applicable law or agreed to in writing, software |
kenjiArai | 1:9db0e321a9f4 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
kenjiArai | 1:9db0e321a9f4 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kenjiArai | 1:9db0e321a9f4 | 14 | * See the License for the specific language governing permissions and |
kenjiArai | 1:9db0e321a9f4 | 15 | * limitations under the License. |
kenjiArai | 1:9db0e321a9f4 | 16 | */ |
kenjiArai | 1:9db0e321a9f4 | 17 | #include "platform/mbed_assert.h" |
kenjiArai | 1:9db0e321a9f4 | 18 | #include "platform/mbed_stats.h" |
kenjiArai | 1:9db0e321a9f4 | 19 | #include "platform/mbed_power_mgmt.h" |
kenjiArai | 1:9db0e321a9f4 | 20 | #include "platform/mbed_version.h" |
kenjiArai | 1:9db0e321a9f4 | 21 | #include <string.h> |
kenjiArai | 1:9db0e321a9f4 | 22 | #include <stdlib.h> |
kenjiArai | 1:9db0e321a9f4 | 23 | |
kenjiArai | 1:9db0e321a9f4 | 24 | #include "device.h" |
kenjiArai | 1:9db0e321a9f4 | 25 | #ifdef MBED_CONF_RTOS_PRESENT |
kenjiArai | 1:9db0e321a9f4 | 26 | #include "cmsis_os2.h" |
kenjiArai | 1:9db0e321a9f4 | 27 | #elif defined(MBED_STACK_STATS_ENABLED) || defined(MBED_THREAD_STATS_ENABLED) |
kenjiArai | 1:9db0e321a9f4 | 28 | #warning Statistics are currently not supported without the rtos. |
kenjiArai | 1:9db0e321a9f4 | 29 | #endif |
kenjiArai | 1:9db0e321a9f4 | 30 | |
kenjiArai | 1:9db0e321a9f4 | 31 | #if defined(MBED_CPU_STATS_ENABLED) && (!DEVICE_SLEEP) |
kenjiArai | 1:9db0e321a9f4 | 32 | #warning CPU statistics are not supported without sleep support. |
kenjiArai | 1:9db0e321a9f4 | 33 | #endif |
kenjiArai | 1:9db0e321a9f4 | 34 | |
kenjiArai | 1:9db0e321a9f4 | 35 | void mbed_stats_cpu_get(mbed_stats_cpu_t *stats) |
kenjiArai | 1:9db0e321a9f4 | 36 | { |
kenjiArai | 1:9db0e321a9f4 | 37 | MBED_ASSERT(stats != NULL); |
kenjiArai | 1:9db0e321a9f4 | 38 | memset(stats, 0, sizeof(mbed_stats_cpu_t)); |
kenjiArai | 1:9db0e321a9f4 | 39 | #if defined(MBED_CPU_STATS_ENABLED) && DEVICE_LPTICKER && DEVICE_SLEEP |
kenjiArai | 1:9db0e321a9f4 | 40 | stats->uptime = mbed_uptime(); |
kenjiArai | 1:9db0e321a9f4 | 41 | stats->idle_time = mbed_time_idle(); |
kenjiArai | 1:9db0e321a9f4 | 42 | stats->sleep_time = mbed_time_sleep(); |
kenjiArai | 1:9db0e321a9f4 | 43 | stats->deep_sleep_time = mbed_time_deepsleep(); |
kenjiArai | 1:9db0e321a9f4 | 44 | #endif |
kenjiArai | 1:9db0e321a9f4 | 45 | } |
kenjiArai | 1:9db0e321a9f4 | 46 | |
kenjiArai | 1:9db0e321a9f4 | 47 | // note: mbed_stats_heap_get defined in mbed_alloc_wrappers.cpp |
kenjiArai | 1:9db0e321a9f4 | 48 | void mbed_stats_stack_get(mbed_stats_stack_t *stats) |
kenjiArai | 1:9db0e321a9f4 | 49 | { |
kenjiArai | 1:9db0e321a9f4 | 50 | MBED_ASSERT(stats != NULL); |
kenjiArai | 1:9db0e321a9f4 | 51 | memset(stats, 0, sizeof(mbed_stats_stack_t)); |
kenjiArai | 1:9db0e321a9f4 | 52 | |
kenjiArai | 1:9db0e321a9f4 | 53 | #if defined(MBED_STACK_STATS_ENABLED) && defined(MBED_CONF_RTOS_PRESENT) |
kenjiArai | 1:9db0e321a9f4 | 54 | uint32_t thread_n = osThreadGetCount(); |
kenjiArai | 1:9db0e321a9f4 | 55 | unsigned i; |
kenjiArai | 1:9db0e321a9f4 | 56 | osThreadId_t *threads; |
kenjiArai | 1:9db0e321a9f4 | 57 | |
kenjiArai | 1:9db0e321a9f4 | 58 | threads = malloc(sizeof(osThreadId_t) * thread_n); |
kenjiArai | 1:9db0e321a9f4 | 59 | // Don't fail on lack of memory |
kenjiArai | 1:9db0e321a9f4 | 60 | if (!threads) { |
kenjiArai | 1:9db0e321a9f4 | 61 | return; |
kenjiArai | 1:9db0e321a9f4 | 62 | } |
kenjiArai | 1:9db0e321a9f4 | 63 | |
kenjiArai | 1:9db0e321a9f4 | 64 | osKernelLock(); |
kenjiArai | 1:9db0e321a9f4 | 65 | thread_n = osThreadEnumerate(threads, thread_n); |
kenjiArai | 1:9db0e321a9f4 | 66 | |
kenjiArai | 1:9db0e321a9f4 | 67 | for (i = 0; i < thread_n; i++) { |
kenjiArai | 1:9db0e321a9f4 | 68 | uint32_t stack_size = osThreadGetStackSize(threads[i]); |
kenjiArai | 1:9db0e321a9f4 | 69 | stats->max_size += stack_size - osThreadGetStackSpace(threads[i]); |
kenjiArai | 1:9db0e321a9f4 | 70 | stats->reserved_size += stack_size; |
kenjiArai | 1:9db0e321a9f4 | 71 | stats->stack_cnt++; |
kenjiArai | 1:9db0e321a9f4 | 72 | } |
kenjiArai | 1:9db0e321a9f4 | 73 | osKernelUnlock(); |
kenjiArai | 1:9db0e321a9f4 | 74 | |
kenjiArai | 1:9db0e321a9f4 | 75 | free(threads); |
kenjiArai | 1:9db0e321a9f4 | 76 | #endif |
kenjiArai | 1:9db0e321a9f4 | 77 | } |
kenjiArai | 1:9db0e321a9f4 | 78 | |
kenjiArai | 1:9db0e321a9f4 | 79 | size_t mbed_stats_stack_get_each(mbed_stats_stack_t *stats, size_t count) |
kenjiArai | 1:9db0e321a9f4 | 80 | { |
kenjiArai | 1:9db0e321a9f4 | 81 | MBED_ASSERT(stats != NULL); |
kenjiArai | 1:9db0e321a9f4 | 82 | memset(stats, 0, count * sizeof(mbed_stats_stack_t)); |
kenjiArai | 1:9db0e321a9f4 | 83 | |
kenjiArai | 1:9db0e321a9f4 | 84 | size_t i = 0; |
kenjiArai | 1:9db0e321a9f4 | 85 | |
kenjiArai | 1:9db0e321a9f4 | 86 | #if defined(MBED_STACK_STATS_ENABLED) && defined(MBED_CONF_RTOS_PRESENT) |
kenjiArai | 1:9db0e321a9f4 | 87 | osThreadId_t *threads; |
kenjiArai | 1:9db0e321a9f4 | 88 | |
kenjiArai | 1:9db0e321a9f4 | 89 | threads = malloc(sizeof(osThreadId_t) * count); |
kenjiArai | 1:9db0e321a9f4 | 90 | // Don't fail on lack of memory |
kenjiArai | 1:9db0e321a9f4 | 91 | if (!threads) { |
kenjiArai | 1:9db0e321a9f4 | 92 | return 0; |
kenjiArai | 1:9db0e321a9f4 | 93 | } |
kenjiArai | 1:9db0e321a9f4 | 94 | |
kenjiArai | 1:9db0e321a9f4 | 95 | osKernelLock(); |
kenjiArai | 1:9db0e321a9f4 | 96 | count = osThreadEnumerate(threads, count); |
kenjiArai | 1:9db0e321a9f4 | 97 | |
kenjiArai | 1:9db0e321a9f4 | 98 | for (i = 0; i < count; i++) { |
kenjiArai | 1:9db0e321a9f4 | 99 | uint32_t stack_size = osThreadGetStackSize(threads[i]); |
kenjiArai | 1:9db0e321a9f4 | 100 | stats[i].max_size = stack_size - osThreadGetStackSpace(threads[i]); |
kenjiArai | 1:9db0e321a9f4 | 101 | stats[i].reserved_size = stack_size; |
kenjiArai | 1:9db0e321a9f4 | 102 | stats[i].thread_id = (uint32_t)threads[i]; |
kenjiArai | 1:9db0e321a9f4 | 103 | stats[i].stack_cnt = 1; |
kenjiArai | 1:9db0e321a9f4 | 104 | } |
kenjiArai | 1:9db0e321a9f4 | 105 | osKernelUnlock(); |
kenjiArai | 1:9db0e321a9f4 | 106 | |
kenjiArai | 1:9db0e321a9f4 | 107 | free(threads); |
kenjiArai | 1:9db0e321a9f4 | 108 | #endif |
kenjiArai | 1:9db0e321a9f4 | 109 | |
kenjiArai | 1:9db0e321a9f4 | 110 | return i; |
kenjiArai | 1:9db0e321a9f4 | 111 | } |
kenjiArai | 1:9db0e321a9f4 | 112 | |
kenjiArai | 1:9db0e321a9f4 | 113 | size_t mbed_stats_thread_get_each(mbed_stats_thread_t *stats, size_t count) |
kenjiArai | 1:9db0e321a9f4 | 114 | { |
kenjiArai | 1:9db0e321a9f4 | 115 | MBED_ASSERT(stats != NULL); |
kenjiArai | 1:9db0e321a9f4 | 116 | memset(stats, 0, count * sizeof(mbed_stats_thread_t)); |
kenjiArai | 1:9db0e321a9f4 | 117 | size_t i = 0; |
kenjiArai | 1:9db0e321a9f4 | 118 | |
kenjiArai | 1:9db0e321a9f4 | 119 | #if defined(MBED_THREAD_STATS_ENABLED) && defined(MBED_CONF_RTOS_PRESENT) |
kenjiArai | 1:9db0e321a9f4 | 120 | osThreadId_t *threads; |
kenjiArai | 1:9db0e321a9f4 | 121 | |
kenjiArai | 1:9db0e321a9f4 | 122 | threads = malloc(sizeof(osThreadId_t) * count); |
kenjiArai | 1:9db0e321a9f4 | 123 | MBED_ASSERT(threads != NULL); |
kenjiArai | 1:9db0e321a9f4 | 124 | |
kenjiArai | 1:9db0e321a9f4 | 125 | osKernelLock(); |
kenjiArai | 1:9db0e321a9f4 | 126 | count = osThreadEnumerate(threads, count); |
kenjiArai | 1:9db0e321a9f4 | 127 | |
kenjiArai | 1:9db0e321a9f4 | 128 | for (i = 0; i < count; i++) { |
kenjiArai | 1:9db0e321a9f4 | 129 | stats[i].id = (uint32_t)threads[i]; |
kenjiArai | 1:9db0e321a9f4 | 130 | stats[i].state = (uint32_t)osThreadGetState(threads[i]); |
kenjiArai | 1:9db0e321a9f4 | 131 | stats[i].priority = (uint32_t)osThreadGetPriority(threads[i]); |
kenjiArai | 1:9db0e321a9f4 | 132 | stats[i].stack_size = osThreadGetStackSize(threads[i]); |
kenjiArai | 1:9db0e321a9f4 | 133 | stats[i].stack_space = osThreadGetStackSpace(threads[i]); |
kenjiArai | 1:9db0e321a9f4 | 134 | stats[i].name = osThreadGetName(threads[i]); |
kenjiArai | 1:9db0e321a9f4 | 135 | } |
kenjiArai | 1:9db0e321a9f4 | 136 | osKernelUnlock(); |
kenjiArai | 1:9db0e321a9f4 | 137 | free(threads); |
kenjiArai | 1:9db0e321a9f4 | 138 | #endif |
kenjiArai | 1:9db0e321a9f4 | 139 | return i; |
kenjiArai | 1:9db0e321a9f4 | 140 | } |
kenjiArai | 1:9db0e321a9f4 | 141 | |
kenjiArai | 1:9db0e321a9f4 | 142 | void mbed_stats_sys_get(mbed_stats_sys_t *stats) |
kenjiArai | 1:9db0e321a9f4 | 143 | { |
kenjiArai | 1:9db0e321a9f4 | 144 | MBED_ASSERT(stats != NULL); |
kenjiArai | 1:9db0e321a9f4 | 145 | memset(stats, 0, sizeof(mbed_stats_sys_t)); |
kenjiArai | 1:9db0e321a9f4 | 146 | |
kenjiArai | 1:9db0e321a9f4 | 147 | #if defined(MBED_SYS_STATS_ENABLED) |
kenjiArai | 1:9db0e321a9f4 | 148 | stats->os_version = MBED_VERSION; |
kenjiArai | 1:9db0e321a9f4 | 149 | #if defined(MBED_RAM_START) && defined(MBED_RAM_SIZE) |
kenjiArai | 1:9db0e321a9f4 | 150 | stats->ram_start[0] = MBED_RAM_START; |
kenjiArai | 1:9db0e321a9f4 | 151 | stats->ram_size[0] = MBED_RAM_SIZE; |
kenjiArai | 1:9db0e321a9f4 | 152 | #endif |
kenjiArai | 1:9db0e321a9f4 | 153 | #if defined(MBED_ROM_START) && defined(MBED_ROM_SIZE) |
kenjiArai | 1:9db0e321a9f4 | 154 | stats->rom_start[0] = MBED_ROM_START; |
kenjiArai | 1:9db0e321a9f4 | 155 | stats->rom_size[0] = MBED_ROM_SIZE; |
kenjiArai | 1:9db0e321a9f4 | 156 | #endif |
kenjiArai | 1:9db0e321a9f4 | 157 | #if defined(MBED_RAM1_START) && defined(MBED_RAM1_SIZE) |
kenjiArai | 1:9db0e321a9f4 | 158 | stats->ram_start[1] = MBED_RAM1_START; |
kenjiArai | 1:9db0e321a9f4 | 159 | stats->ram_size[1] = MBED_RAM1_SIZE; |
kenjiArai | 1:9db0e321a9f4 | 160 | #endif |
kenjiArai | 1:9db0e321a9f4 | 161 | #if defined(MBED_RAM2_START) && defined(MBED_RAM2_SIZE) |
kenjiArai | 1:9db0e321a9f4 | 162 | stats->ram_start[2] = MBED_RAM2_START; |
kenjiArai | 1:9db0e321a9f4 | 163 | stats->ram_size[2] = MBED_RAM2_SIZE; |
kenjiArai | 1:9db0e321a9f4 | 164 | #endif |
kenjiArai | 1:9db0e321a9f4 | 165 | #if defined(MBED_RAM3_START) && defined(MBED_RAM3_SIZE) |
kenjiArai | 1:9db0e321a9f4 | 166 | stats->ram_start[3] = MBED_RAM3_START; |
kenjiArai | 1:9db0e321a9f4 | 167 | stats->ram_size[3] = MBED_RAM3_SIZE; |
kenjiArai | 1:9db0e321a9f4 | 168 | #endif |
kenjiArai | 1:9db0e321a9f4 | 169 | #if defined(MBED_ROM1_START) && defined(MBED_ROM1_SIZE) |
kenjiArai | 1:9db0e321a9f4 | 170 | stats->rom_start[1] = MBED_ROM1_START; |
kenjiArai | 1:9db0e321a9f4 | 171 | stats->rom_size[1] = MBED_ROM1_SIZE; |
kenjiArai | 1:9db0e321a9f4 | 172 | #endif |
kenjiArai | 1:9db0e321a9f4 | 173 | #if defined(MBED_ROM2_START) && defined(MBED_ROM2_SIZE) |
kenjiArai | 1:9db0e321a9f4 | 174 | stats->rom_start[2] = MBED_ROM2_START; |
kenjiArai | 1:9db0e321a9f4 | 175 | stats->rom_size[2] = MBED_ROM2_SIZE; |
kenjiArai | 1:9db0e321a9f4 | 176 | #endif |
kenjiArai | 1:9db0e321a9f4 | 177 | #if defined(MBED_ROM3_START) && defined(MBED_ROM3_SIZE) |
kenjiArai | 1:9db0e321a9f4 | 178 | stats->rom_start[3] = MBED_ROM3_START; |
kenjiArai | 1:9db0e321a9f4 | 179 | stats->rom_size[3] = MBED_ROM3_SIZE; |
kenjiArai | 1:9db0e321a9f4 | 180 | #endif |
kenjiArai | 1:9db0e321a9f4 | 181 | |
kenjiArai | 1:9db0e321a9f4 | 182 | #if defined(__CORTEX_M) |
kenjiArai | 1:9db0e321a9f4 | 183 | stats->cpu_id = SCB->CPUID; |
kenjiArai | 1:9db0e321a9f4 | 184 | #endif |
kenjiArai | 1:9db0e321a9f4 | 185 | #if defined(__IAR_SYSTEMS_ICC__) |
kenjiArai | 1:9db0e321a9f4 | 186 | stats->compiler_id = IAR; |
kenjiArai | 1:9db0e321a9f4 | 187 | stats->compiler_version = __VER__; |
kenjiArai | 1:9db0e321a9f4 | 188 | #elif defined(__ARMCC_VERSION) |
kenjiArai | 1:9db0e321a9f4 | 189 | stats->compiler_id = ARM; |
kenjiArai | 1:9db0e321a9f4 | 190 | stats->compiler_version = __ARMCC_VERSION; |
kenjiArai | 1:9db0e321a9f4 | 191 | #elif defined(__GNUC__) |
kenjiArai | 1:9db0e321a9f4 | 192 | stats->compiler_id = GCC_ARM; |
kenjiArai | 1:9db0e321a9f4 | 193 | stats->compiler_version = (__GNUC__ * 10000 + __GNUC_MINOR__ * 100); |
kenjiArai | 1:9db0e321a9f4 | 194 | #endif |
kenjiArai | 1:9db0e321a9f4 | 195 | |
kenjiArai | 1:9db0e321a9f4 | 196 | #endif |
kenjiArai | 1:9db0e321a9f4 | 197 | return; |
kenjiArai | 1:9db0e321a9f4 | 198 | } |