|
| 1 | +/* |
| 2 | + * Copyright (c) 2013-2016, ARM Limited, All Rights Reserved |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | + * not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "mbed.h" |
| 19 | +#include "rtos.h" |
| 20 | +#include "mbed_stats.h" |
| 21 | +#include "cmsis_os.h" |
| 22 | +#include "greentea-client/test_env.h" |
| 23 | +#include "greentea-client/greentea_metrics.h" |
| 24 | +#include "SingletonPtr.h" |
| 25 | +#include "CircularBuffer.h" |
| 26 | + |
| 27 | +#define THREAD_BUF_COUNT 16 |
| 28 | + |
| 29 | +typedef struct { |
| 30 | + uint32_t entry; |
| 31 | + uint32_t arg; |
| 32 | + uint32_t stack_size; |
| 33 | + uint32_t max_stack; |
| 34 | +} thread_info_t; |
| 35 | + |
| 36 | +// Mutex to protect "buf" |
| 37 | +SingletonPtr<Mutex> mutex; |
| 38 | +static char buf[128]; |
| 39 | +static SingletonPtr<CircularBuffer<thread_info_t, THREAD_BUF_COUNT> > queue; |
| 40 | + |
| 41 | +static void send_heap_info(void); |
| 42 | +static void send_stack_info(void); |
| 43 | +static void on_thread_terminate(osThreadId id); |
| 44 | +static void enqeue_thread_info(osThreadId id); |
| 45 | +static void deque_and_print_thread_info(void); |
| 46 | + |
| 47 | +// sprintf uses a lot of stack so use these instead |
| 48 | +static uint32_t print_hex(char *buf, uint32_t value); |
| 49 | +static uint32_t print_dec(char *buf, uint32_t value); |
| 50 | + |
| 51 | +void greentea_metrics_setup() |
| 52 | +{ |
| 53 | +#if defined(MBED_STACK_STATS_ENABLED) && MBED_STACK_STATS_ENABLED |
| 54 | + Thread::attach_terminate_hook(on_thread_terminate); |
| 55 | +#endif |
| 56 | +} |
| 57 | + |
| 58 | +void greentea_metrics_report() |
| 59 | +{ |
| 60 | + send_heap_info(); |
| 61 | +#if defined(MBED_STACK_STATS_ENABLED) && MBED_STACK_STATS_ENABLED |
| 62 | + send_stack_info(); |
| 63 | + Thread::attach_terminate_hook(NULL); |
| 64 | +#endif |
| 65 | +} |
| 66 | + |
| 67 | +static void send_heap_info() |
| 68 | +{ |
| 69 | + mbed_stats_heap_t heap_stats; |
| 70 | + mbed_stats_heap_get(&heap_stats); |
| 71 | + greentea_send_kv("max_heap_usage",heap_stats.max_size); |
| 72 | +} |
| 73 | + |
| 74 | +MBED_UNUSED static void send_stack_info() |
| 75 | +{ |
| 76 | + mutex->lock(); |
| 77 | + |
| 78 | + // Flush any queued stack entries |
| 79 | + while (!queue->empty()) { |
| 80 | + deque_and_print_thread_info(); |
| 81 | + } |
| 82 | + |
| 83 | + // Print info for all other threads |
| 84 | + osThreadEnumId enum_id = osThreadsEnumStart(); |
| 85 | + while (true) { |
| 86 | + osThreadId thread_id = osThreadEnumNext(enum_id); |
| 87 | + if (NULL == thread_id) { |
| 88 | + // End of enumeration |
| 89 | + break; |
| 90 | + } |
| 91 | + enqeue_thread_info(thread_id); |
| 92 | + deque_and_print_thread_info(); |
| 93 | + } |
| 94 | + osThreadEnumFree(enum_id); |
| 95 | + |
| 96 | + mutex->unlock(); |
| 97 | +} |
| 98 | + |
| 99 | +MBED_UNUSED static void on_thread_terminate(osThreadId id) |
| 100 | +{ |
| 101 | + mutex->lock(); |
| 102 | + |
| 103 | + // There should always be space in the queue |
| 104 | + enqeue_thread_info(id); |
| 105 | + |
| 106 | + // If queue is full then print out a single entry |
| 107 | + if (queue->full()) { |
| 108 | + deque_and_print_thread_info(); |
| 109 | + } |
| 110 | + |
| 111 | + mutex->unlock(); |
| 112 | +} |
| 113 | + |
| 114 | +static void enqeue_thread_info(osThreadId id) |
| 115 | +{ |
| 116 | + osEvent info; |
| 117 | + thread_info_t thread_info = {}; |
| 118 | + info = osThreadGetInfo(id, osThreadInfoEntry); |
| 119 | + if (info.status != osOK) { |
| 120 | + return; |
| 121 | + } |
| 122 | + thread_info.entry = (uint32_t)info.value.p; |
| 123 | + info = osThreadGetInfo(id, osThreadInfoArg); |
| 124 | + if (info.status != osOK) { |
| 125 | + return; |
| 126 | + } |
| 127 | + thread_info.arg = (uint32_t)info.value.p; |
| 128 | + info = osThreadGetInfo(id, osThreadInfoStackSize); |
| 129 | + if (info.status != osOK) { |
| 130 | + return; |
| 131 | + } |
| 132 | + thread_info.stack_size = (uint32_t)info.value.v; |
| 133 | + info = osThreadGetInfo(id, osThreadInfoStackMax); |
| 134 | + if (info.status != osOK) { |
| 135 | + return; |
| 136 | + } |
| 137 | + thread_info.max_stack = (uint32_t)info.value.v; |
| 138 | + queue->push(thread_info); |
| 139 | +} |
| 140 | + |
| 141 | +static void deque_and_print_thread_info() |
| 142 | +{ |
| 143 | + thread_info_t thread_info; |
| 144 | + bool ret = queue->pop(thread_info); |
| 145 | + MBED_ASSERT(ret); |
| 146 | + uint32_t pos = 0; |
| 147 | + buf[pos++] = '\"'; |
| 148 | + pos += print_hex(buf + pos, thread_info.entry); |
| 149 | + buf[pos++] = '-'; |
| 150 | + pos += print_hex(buf + pos, thread_info.arg); |
| 151 | + buf[pos++] = '\"'; |
| 152 | + buf[pos++] = ','; |
| 153 | + pos += print_dec(buf + pos, thread_info.max_stack); |
| 154 | + buf[pos++] = ','; |
| 155 | + pos += print_dec(buf + pos, thread_info.stack_size); |
| 156 | + buf[pos++] = 0; |
| 157 | + greentea_send_kv("__thread_info", buf); |
| 158 | +} |
| 159 | + |
| 160 | +static uint32_t print_hex(char *buf, uint32_t value) |
| 161 | +{ |
| 162 | + uint32_t pos = 0; |
| 163 | + buf[pos] = '0'; |
| 164 | + pos++; |
| 165 | + buf[pos] = 'x'; |
| 166 | + pos++; |
| 167 | + for (int i = 8; i >= 0; i--) { |
| 168 | + uint32_t val = (value >> (4 * i)) & 0xF; |
| 169 | + if (val <= 9) { |
| 170 | + buf[pos] = '0' + val; |
| 171 | + pos++; |
| 172 | + } else { |
| 173 | + buf[pos] = 'a' + val - 10; |
| 174 | + pos++; |
| 175 | + } |
| 176 | + } |
| 177 | + return pos; |
| 178 | +} |
| 179 | + |
| 180 | +static uint32_t print_dec(char *buf, uint32_t value) |
| 181 | +{ |
| 182 | + uint32_t pos = 0; |
| 183 | + |
| 184 | + // The value 0 is special case |
| 185 | + if (0 == value) { |
| 186 | + buf[pos] = '0'; |
| 187 | + pos++; |
| 188 | + return pos; |
| 189 | + } |
| 190 | + |
| 191 | + // Write out value in reverse order |
| 192 | + while (value != 0) { |
| 193 | + uint32_t next = value / 10; |
| 194 | + buf[pos] = '0' + (value - next * 10); |
| 195 | + value = next; |
| 196 | + pos++; |
| 197 | + } |
| 198 | + |
| 199 | + // Reverse order |
| 200 | + for (uint32_t i = 0; i < pos / 2; i++) { |
| 201 | + char temp = buf[i]; |
| 202 | + buf[i] = buf[pos - 1 - i]; |
| 203 | + buf[pos - 1 - i] = temp; |
| 204 | + } |
| 205 | + |
| 206 | + return pos; |
| 207 | +} |
0 commit comments