Skip to content

Commit 30487c6

Browse files
committed
[MOS-110] Statistics api for system
Gets data from freertos and prints on frequency change depending if it's important. Gathering is not costly, printing is though. For less intrusive checks I would rather disable names gathering as in worst case scenario it hangs rtos context switching till thread id is found.
1 parent 082966f commit 30487c6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+973
-67
lines changed

.gdbinit-1051

+4
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ thread 2
1515
tb main
1616
b HardFault_Handler
1717
b _exit
18+
b abort
19+
b WDOG1_IRQHandler
20+
b RTWDOG_IRQHandler
21+
b IntDefaultHandler

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,6 @@
106106
path = third-party/reedgefs/src
107107
url = ../reliance-edge.git
108108
branch = mudita
109+
[submodule "third-party/msgpack11/msgpack11"]
110+
path = third-party/msgpack11/msgpack11
111+
url = https://github.com/ar90n/msgpack11.git

cmake/modules/ProjectConfig.cmake

+8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ else()
2424
set (LOG_REDIRECT "RTT_JLINK" CACHE INTERNAL "")
2525
endif()
2626

27+
option(SYSTEM_PROFILE "SYSTEM_PROFILE" OFF)
28+
if(${SYSTEM_PROFILE} STREQUAL "ON")
29+
set(PROF_ON 1 CACHE INTERNAL "")
30+
else()
31+
set(PROF_ON 0 CACHE INTERNAL "")
32+
endif()
33+
2734
# add CurrentMeasurement enable option
2835
option(CURRENT_MEASUREMENT "CURRENT_MEASUREMENT" OFF)
2936

@@ -72,6 +79,7 @@ set(PROJECT_CONFIG_DEFINITIONS
7279
USBCDC_ECHO_ENABLED=${USBCDC_ECHO_ENABLED}
7380
LOG_LUART_ENABLED=${LOG_LUART_ENABLED}
7481
MAGIC_ENUM_RANGE_MAX=256
82+
PROF_ON=${PROF_ON}
7583
CACHE INTERNAL ""
7684
)
7785

module-bluetooth/Bluetooth/interface/profiles/SCO/SCO.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
1+
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
22
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
33

44
#include "SCO.hpp"
@@ -153,7 +153,7 @@ void SCO::SCOImpl::writeToHostEndian(int16_t *buffer, uint8_t *packet, int lengt
153153
void SCO::SCOImpl::receiveCvsd(uint8_t *packet, uint16_t size)
154154
{
155155

156-
std::array<int16_t, AUDIO_BUFFER_LENGTH> audioFrameOut;
156+
std::array<int16_t, AUDIO_BUFFER_LENGTH> audioFrameOut{};
157157

158158
if (size > audioFrameOut.size()) {
159159
LOG_WARN("SCO packet larger than local output buffer - dropping data.");

module-bsp/bsp/common.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,26 @@ namespace bsp{
1616
break;
1717
}
1818
}
19+
20+
uint8_t CpuMHZToLevel(enum CpuFrequencyMHz val)
21+
{
22+
switch (val) {
23+
case CpuFrequencyMHz::Level_0:
24+
return 0;
25+
case CpuFrequencyMHz::Level_1:
26+
return 1;
27+
case CpuFrequencyMHz::Level_2:
28+
return 2;
29+
case CpuFrequencyMHz::Level_3:
30+
return 3;
31+
case CpuFrequencyMHz::Level_4:
32+
return 4;
33+
case CpuFrequencyMHz::Level_5:
34+
return 5;
35+
case CpuFrequencyMHz::Level_6:
36+
return 6;
37+
}
38+
return -1;
39+
}
1940
};
2041

module-bsp/bsp/common.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#pragma once
55

6+
#include <cstdint>
67
namespace bsp
78
{
89
enum class RetCode{
@@ -24,6 +25,8 @@ namespace bsp
2425
Level_6 = 528
2526
};
2627

28+
uint8_t CpuMHZToLevel(enum CpuFrequencyMHz val);
29+
2730
constexpr auto MHz_frequency_multiplier = 1000000U;
2831

2932
enum class Board{

module-os/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ target_sources(module-os PRIVATE ${SOURCES})
3434
if(NOT ${SYSTEM_VIEW_ENABLED})
3535
target_sources(module-os PRIVATE FreeRTOS/tasks.c)
3636
endif()
37+
if(${PROF_ON})
38+
target_sources(module-os PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/FreeRTOS/prof.c)
39+
endif()
3740

3841
add_board_subdirectory(board)
3942

@@ -80,3 +83,7 @@ if((${PROJECT_TARGET} STREQUAL "TARGET_RT1051") AND (${SYSTEM_VIEW_ENABLED}))
8083
endif()
8184

8285
target_link_libraries(${PROJECT_NAME} PUBLIC log-api board-config)
86+
87+
if (${ENABLE_TESTS})
88+
add_subdirectory(test)
89+
endif ()

module-os/FreeRTOS/include/task.h

+2
Original file line numberDiff line numberDiff line change
@@ -1408,6 +1408,7 @@ char *pcTaskGetName( TaskHandle_t xTaskToQuery ) PRIVILEGED_FUNCTION; /*lint !e9
14081408
* \ingroup TaskUtils
14091409
*/
14101410
TaskHandle_t xTaskGetHandle( const char *pcNameToQuery ) PRIVILEGED_FUNCTION; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1411+
TaskHandle_t xTaskGetByTCBNumber(UBaseType_t uxTCBNumber ) PRIVILEGED_FUNCTION; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
14111412

14121413
/**
14131414
* task.h
@@ -2366,6 +2367,7 @@ void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder, UBas
23662367
* Get the uxTCBNumber assigned to the task referenced by the xTask parameter.
23672368
*/
23682369
UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
2370+
UBaseType_t uxTaskGetTCBNumber(TaskHandle_t xTask) PRIVILEGED_FUNCTION;
23692371

23702372
/*
23712373
* Set the uxTaskNumber of the task referenced by the xTask parameter to

module-os/FreeRTOS/prof.c

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
2+
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
3+
4+
#include "prof.h"
5+
#include <stdlib.h>
6+
#include <string.h>
7+
8+
struct prof_pool
9+
{
10+
struct task_prof_data *pool;
11+
unsigned int _overflow_counter;
12+
unsigned int _pos;
13+
14+
struct prof_pool_init_data data;
15+
void (*clean)();
16+
void (*handle_overflow)(uint32_t id);
17+
struct task_prof_data* (*get)(uint32_t id);
18+
};
19+
20+
21+
static struct prof_pool pool;
22+
23+
static void _pool_clean()
24+
{
25+
memset(pool.pool, 0, sizeof(*pool.pool)*pool.data.size);
26+
pool._overflow_counter =0;
27+
pool._pos = 0;
28+
}
29+
30+
static void _pool_overflow(uint32_t id)
31+
{
32+
pool._overflow_counter = id;
33+
}
34+
35+
/// just meant to be fast get of element
36+
static struct task_prof_data* _pool_get(uint32_t id)
37+
{
38+
if ( pool._pos == pool.data.size ) {
39+
pool.handle_overflow(id);
40+
}
41+
for ( size_t i =0; i < pool.data.size && i != pool._pos; ++i ) {
42+
if (id == pool.pool[i].task_TCB_id) {
43+
return &pool.pool[i];
44+
}
45+
}
46+
struct task_prof_data* p = &pool.pool[pool._pos];
47+
pool._pos++;
48+
return p;
49+
}
50+
51+
52+
53+
void prof_pool_init(struct prof_pool_init_data init)
54+
{
55+
pool.data = init;
56+
pool.clean = _pool_clean;
57+
pool.handle_overflow = _pool_overflow;
58+
pool.get = _pool_get;
59+
60+
pool.pool = (struct task_prof_data *)(malloc(sizeof(struct task_prof_data)*pool.data.size));
61+
pool.clean();
62+
}
63+
64+
void prof_pool_deinit()
65+
{
66+
free(pool.pool);
67+
pool.pool = NULL;
68+
}
69+
70+
71+
struct prof_pool_init_data prof_pool_get_data()
72+
{
73+
return pool.data;
74+
}
75+
76+
void prof_pool_data_set(uint8_t ts, uint32_t id)
77+
{
78+
struct task_prof_data *what = pool.get(id);
79+
if ( what == NULL) {
80+
return;
81+
}
82+
what->task_TCB_id=id;
83+
what->exec_time += ts;
84+
++(what->switches);
85+
}
86+
87+
unsigned int prof_pool_overflow()
88+
{
89+
return pool._overflow_counter;
90+
}
91+
92+
unsigned int prof_pool_flush(struct task_prof_data *mem, size_t cap)
93+
{
94+
unsigned int to_ret = pool._overflow_counter;
95+
memcpy(mem, pool.pool, cap * (sizeof(struct task_prof_data)));
96+
pool.clean();
97+
return to_ret;
98+
}

module-os/FreeRTOS/prof.h

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
2+
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
3+
4+
#pragma once
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
#include <stddef.h>
11+
#include <stdint.h>
12+
13+
/// just store what's interesting
14+
/// 1. do not store TaskHandle_t, id is better - we normally have up to 32 id's as we do not
15+
/// as we tend to keep up to ~30 threads alive
16+
/// 2. execution time, the better granulation, the better result
17+
struct task_prof_data {
18+
uint32_t task_TCB_id; /// task TCB id
19+
uint32_t exec_time; /// single task switch execution time summed up in TS
20+
uint32_t switches; /// count how many times it was switched out
21+
};
22+
23+
/// initialization structure
24+
struct prof_pool_init_data
25+
{
26+
size_t size; /// size of the pool, use should have linear eficiency
27+
};
28+
29+
#if PROF_ON
30+
31+
/// initialization of pool to store switch data
32+
void prof_pool_init(struct prof_pool_init_data init);
33+
void prof_pool_deinit();
34+
struct prof_pool_init_data prof_pool_get_data();
35+
36+
/// get next available slot from the pool
37+
/// struct task_prof_data* prof_pool_get_next();
38+
/// set the element
39+
void prof_pool_data_set(uint8_t ts, uint32_t id);
40+
41+
/// mark if overflow happened
42+
unsigned int prof_pool_overflow();
43+
44+
/// to `mem` flush up to `cap` data - then clean
45+
/// if passed:
46+
/// - set used count: how much data was used
47+
/// - returns overflow count
48+
/// requires sched lock before running
49+
unsigned int prof_pool_flush(struct task_prof_data *mem, size_t cap);
50+
51+
#else
52+
#define prof_pool_data_set(...)
53+
#endif
54+
55+
#ifdef __cplusplus
56+
}
57+
#endif

0 commit comments

Comments
 (0)