Skip to content

Commit

Permalink
Add time analysis trace to monitor rtos kernal use.
Browse files Browse the repository at this point in the history
  • Loading branch information
At-EC committed Mar 21, 2024
1 parent e791fce commit 841194b
Show file tree
Hide file tree
Showing 16 changed files with 212 additions and 27 deletions.
6 changes: 3 additions & 3 deletions build_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
extern "C" {
#endif

#define ATOS_BUILD_TIME "2024-03-20,11:42"
#define ATOS_COMMIT_HEAD_ID "2473e49870fbf1d073cb4dc23a50e156472c63d3"
#define ATOS_BUILD_TIME "2024-03-21,21:21"
#define ATOS_COMMIT_HEAD_ID "e791fcee4eeb5ec0ef97ad0f98b4258cf4791f16"
#define ATOS_VERSION_MAJOR_NUMBER (1u)
#define ATOS_VERSION_MINOR_NUMBER (2u)
#define ATOS_VERSION_PATCH_NUMBER (9u)
#define ATOS_VERSION_PATCH_NUMBER (10u)

#define ATOS_VERSION_MAJOR_NUMBER_MASK (0x03FFu)
#define ATOS_VERSION_MAJOR_NUMBER_POS (22u)
Expand Down
2 changes: 1 addition & 1 deletion include/kernal/at_rtos.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ static inline u32p_t timer_isBusy(os_timer_id_t id)
*/
static inline u32_t timer_system_total_ms(void)
{
return (u32_t)_impl_timer_total_system_get();
return (u32_t)_impl_timer_total_system_ms_get();
}

/**
Expand Down
6 changes: 4 additions & 2 deletions include/kernal/kernal.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ b_t _impl_kernal_isInThreadMode(void);
u32_t _impl_kernal_thread_schedule_request(void);
void _impl_kernal_message_notification(void);
void _impl_kernal_scheduler_inPendSV_c(u32_t **ppCurPsp, u32_t **ppNextPSP);
u32_t _impl_kernal_schedule_time_get(void);
u32_t impl_kernal_thread_use_percent_take(os_id_t id);
void _impl_kernal_privilege_call_inSVC_c(u32_t *svc_args);
u32_t _impl_kernal_privilege_invoke(const void *pCallFun, arguments_t *pArgs);
void _impl_kernal_atos_schedule_thread(void);
void _impl_kernal_atos_idle_thread(void);
void _impl_kernal_thread_schedule(void);
void _impl_kernal_thread_idle(void);
void _impl_kernal_semaphore_list_transfer_toLock(linker_head_t *pCurHead);
thread_context_t *_impl_kernal_thread_runContextGet(void);

Expand Down
22 changes: 22 additions & 0 deletions include/kernal/kstruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "typedef.h"
#include "linker.h"
#include "unique.h"
#include "configuration.h"

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -153,8 +154,27 @@ typedef struct {
pThread_callbackFunc_t pEntryCallFun;
} thread_entry_t;

typedef struct {
u32_t pend_ms;

u32_t run_ms;

u32_t exit_ms;

u32_t active_ms;

u32_t cycle_ms;

u16_t percent;
} analyze_t;

typedef struct {
os_id_t hold;

#if defined KTRACE
analyze_t analyze;
#endif

union {
thread_exit_t exit;

Expand Down Expand Up @@ -243,6 +263,8 @@ typedef struct {

/* The kernal already start to do schedule */
b_t run;

u32_t pendsv_ms;
} kernal_context_t;

typedef struct {
Expand Down
3 changes: 2 additions & 1 deletion include/kernal/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ b_t _impl_timer_status_isBusy(os_id_t id);
os_id_t _impl_timer_init(pTimer_callbackFunc_t pCallFun, b_t isCycle, u32_t timeout_ms, const char_t *pName);
u32p_t _impl_timer_start(os_id_t id, b_t isCycle, u32_t timeout_ms);
u32p_t _impl_timer_stop(os_id_t id);
u32_t _impl_timer_total_system_get(void);
u32_t _impl_timer_total_system_us_get(void);
u32_t _impl_timer_total_system_ms_get(void);
void _impl_timer_reamining_elapsed_handler(void);
void _impl_timer_elapsed_handler(u32_t elapsed_us);

Expand Down
4 changes: 3 additions & 1 deletion include/kernal/trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ extern "C" {
typedef struct {
u32_t priority;
u32_t current_psp;
u32_t usage;
u32_t ram;
u32_t cpu;
u32_t delay;
} thread_snapshot_t;

typedef struct {
Expand Down
4 changes: 4 additions & 0 deletions kernal/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ static void _event_schedule(os_id_t id)
*/
b_t _impl_trace_event_snapshot(u32_t instance, kernal_snapshot_t *pMsgs)
{
#if defined KTRACE
event_context_t *pCurEvent = NULL;
u32_t offset = 0u;
os_id_t id = OS_INVALID_ID;
Expand Down Expand Up @@ -440,6 +441,9 @@ b_t _impl_trace_event_snapshot(u32_t instance, kernal_snapshot_t *pMsgs)

EXIT_CRITICAL_SECTION();
return TRUE;
#else
return FALSE;
#endif
}

#ifdef __cplusplus
Expand Down
101 changes: 97 additions & 4 deletions kernal/kernal.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ static kernal_context_t g_kernal_resource = {
.current = 0u,
.list = LIST_NULL,
.run = FALSE,
.pendsv_ms = 0u,
.member =
{
.pListContainer = (list_t *)&g_kernal_member_list[0],
Expand Down Expand Up @@ -84,14 +85,68 @@ static b_t _kernal_isInPrivilegeMode(void)
return _impl_port_isInInterruptContent();
}

/**
* @brief Update pendsv executed time.
*/
static void _kernal_pendsv_time_update(void)
{
g_kernal_resource.pendsv_ms = _impl_timer_total_system_ms_get();
}

/**
* @brief Get pendsv executed time.
*
* @return The value of pendsv executed time.
*/
static u32_t _kernal_pendsv_time_get(void)
{
return g_kernal_resource.pendsv_ms;
}

/**
* @brief Kernal schedule exit time analyze.
*/
static void _kernal_schedule_exit_time_analyze(os_id_t id)
{
/* Nothing to do */
}

/**
* @brief Kernal schedule entry time analyze.
*/
static void _kernal_schedule_entry_time_analyze(os_id_t id)
{
#if defined KTRACE
thread_context_t *pEntryThread = (thread_context_t *)_impl_kernal_member_unified_id_toContainerAddress(id);
pEntryThread->schedule.analyze.pend_ms = _kernal_pendsv_time_get();
#endif
}

/**
* @brief Kernal schedule run time analyze.
*/
static void _kernal_schedule_run_time_analyze(os_id_t from, os_id_t to)
{
#if defined KTRACE
thread_context_t *pFromThread = (thread_context_t *)_impl_kernal_member_unified_id_toContainerAddress(from);
thread_context_t *pToThread = (thread_context_t *)_impl_kernal_member_unified_id_toContainerAddress(to);
u32_t sv_ms = _kernal_pendsv_time_get();

pFromThread->schedule.analyze.active_ms += (u32_t)(sv_ms - pFromThread->schedule.analyze.run_ms);

pFromThread->schedule.analyze.exit_ms = sv_ms;
pToThread->schedule.analyze.run_ms = sv_ms;
#endif
}

/**
* @brief Get the thread PSP stack address.
*
* @param id The thread unique id.
*
* @return The PSP stacke address.
*/
u32_t *_kernal_thread_PSP_Get(os_id_t id)
static u32_t *_kernal_thread_PSP_Get(os_id_t id)
{
thread_context_t *pCurThread = (thread_context_t *)_impl_kernal_member_unified_id_toContainerAddress(id);

Expand Down Expand Up @@ -176,6 +231,7 @@ static void _kernal_thread_entry_schedule(void)
pCurThread->schedule.hold = OS_INVALID_ID;
}

_kernal_schedule_entry_time_analyze(pCurThread->head.id);
_impl_kernal_thread_list_transfer_toPend((linker_head_t *)&pCurThread->head);
}
}
Expand Down Expand Up @@ -207,6 +263,7 @@ static b_t _kernal_thread_exit_schedule(void)
}
}

_kernal_schedule_exit_time_analyze(pCurThread->head.id);
_kernal_thread_list_transfer_toTargetBlocking((linker_head_t *)&pCurThread->head, (list_t *)pExit->pToList);
}

Expand Down Expand Up @@ -273,6 +330,37 @@ static u32_t _kernal_member_id_toUnifiedIdRange(u8_t member_id)
return (u32_t)(_kernal_member_id_toUnifiedIdEnd(member_id) - _impl_kernal_member_id_toUnifiedIdStart(member_id));
}

/**
* @brief Get pendsv executed time.
*
* @return The value of pendsv executed time.
*/
u32_t _impl_kernal_schedule_time_get(void)
{
return _kernal_pendsv_time_get();
}

/**
* @brief Kernal thread use percent value take.
*
* @return The value of thread use percent.
*/
u32_t impl_kernal_thread_use_percent_take(os_id_t id)
{
#if defined KTRACE
thread_context_t *pCurThread = (thread_context_t *)_impl_kernal_member_unified_id_toContainerAddress(id);

pCurThread->schedule.analyze.percent =
(pCurThread->schedule.analyze.active_ms * 1000u) / (_kernal_pendsv_time_get() - pCurThread->schedule.analyze.cycle_ms);
pCurThread->schedule.analyze.active_ms = 0u;
pCurThread->schedule.analyze.cycle_ms = _kernal_pendsv_time_get();

return pCurThread->schedule.analyze.percent;
#else
return 0u;
#endif
}

/**
* @brief kernal schedule in PendSV interrupt content.
*
Expand All @@ -281,17 +369,19 @@ static u32_t _kernal_member_id_toUnifiedIdRange(u8_t member_id)
*/
void _impl_kernal_scheduler_inPendSV_c(u32_t **ppCurPsp, u32_t **ppNextPSP)
{
_kernal_pendsv_time_update();

if (_kernal_thread_exit_schedule()) {
_impl_kernal_timer_schedule_request();
}

_kernal_thread_entry_schedule();

os_id_t next = _kernal_thread_nextIdGet();

*ppCurPsp = (u32_t *)_kernal_thread_PSP_Get(g_kernal_resource.current);
*ppNextPSP = (u32_t *)_kernal_thread_PSP_Get(next);

_kernal_schedule_run_time_analyze(g_kernal_resource.current, next);
g_kernal_resource.current = next;
}

Expand Down Expand Up @@ -765,7 +855,7 @@ static u32_t _kernal_message_arrived(void)
/**
* @brief The kernal thread only serve for RTOS with highest priority.
*/
void _impl_kernal_atos_schedule_thread(void)
void _impl_kernal_thread_schedule(void)
{
while (1) {
u32p_t postcode = _kernal_message_arrived();
Expand All @@ -775,13 +865,16 @@ void _impl_kernal_atos_schedule_thread(void)
}
}

u32_t g_idle_cnt = 0;

/**
* @brief The idle thread entry function.
*/
void _impl_kernal_atos_idle_thread(void)
void _impl_kernal_thread_idle(void)
{
while (1) {
/* TODO: Power Management */
g_idle_cnt++;
}
}

Expand Down
8 changes: 4 additions & 4 deletions kernal/kthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ void _impl_kernal_thread_init(void)
{
.level = OS_PRIORITY_KERNAL_THREAD_SCHEDULE_LEVEL,
},
.pEntryFunc = _impl_kernal_atos_schedule_thread,
.pEntryFunc = _impl_kernal_thread_schedule,
.pStackAddr = (u32_t *)&_kernal_schedule[0],
.stackSize = KERNAL_SCHEDULE_THREAD_STACK_SIZE,
.PSPStartAddr = (u32_t)_impl_kernal_stack_frame_init(_impl_kernal_atos_schedule_thread, (u32_t *)&_kernal_schedule[0],
.PSPStartAddr = (u32_t)_impl_kernal_stack_frame_init(_impl_kernal_thread_schedule, (u32_t *)&_kernal_schedule[0],
KERNAL_SCHEDULE_THREAD_STACK_SIZE),

},
Expand All @@ -163,10 +163,10 @@ void _impl_kernal_thread_init(void)
{
.level = OS_PRIORITY_KERNAL_THREAD_IDLE_LEVEL,
},
.pEntryFunc = _impl_kernal_atos_idle_thread,
.pEntryFunc = _impl_kernal_thread_idle,
.pStackAddr = (u32_t *)&_kernal_idle[0],
.stackSize = KERNAL_IDLE_THREAD_STACK_SIZE,
.PSPStartAddr = (u32_t)_impl_kernal_stack_frame_init(_impl_kernal_atos_idle_thread, (u32_t *)&_kernal_idle[0],
.PSPStartAddr = (u32_t)_impl_kernal_stack_frame_init(_impl_kernal_thread_idle, (u32_t *)&_kernal_idle[0],
KERNAL_IDLE_THREAD_STACK_SIZE),
},
};
Expand Down
4 changes: 4 additions & 0 deletions kernal/mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ static u32_t _mutex_unlock_privilege_routine(arguments_t *pArgs)
*/
b_t _impl_trace_mutex_snapshot(u32_t instance, kernal_snapshot_t *pMsgs)
{
#if defined KTRACE
mutex_context_t *pCurMutex = NULL;
u32_t offset = 0u;
os_id_t id = OS_INVALID_ID;
Expand Down Expand Up @@ -399,6 +400,9 @@ b_t _impl_trace_mutex_snapshot(u32_t instance, kernal_snapshot_t *pMsgs)

EXIT_CRITICAL_SECTION();
return TRUE;
#else
return FALSE;
#endif
}

#ifdef __cplusplus
Expand Down
4 changes: 4 additions & 0 deletions kernal/queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@ static void _queue_schedule(os_id_t id)
*/
b_t _impl_trace_queue_snapshot(u32_t instance, kernal_snapshot_t *pMsgs)
{
#if defined KTRACE
queue_context_t *pCurQueue = NULL;
u32_t offset = 0u;
os_id_t id = OS_INVALID_ID;
Expand Down Expand Up @@ -664,6 +665,9 @@ b_t _impl_trace_queue_snapshot(u32_t instance, kernal_snapshot_t *pMsgs)

EXIT_CRITICAL_SECTION();
return TRUE;
#else
return FALSE;
#endif
}

#ifdef __cplusplus
Expand Down
4 changes: 4 additions & 0 deletions kernal/semaphore.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ static void _semaphore_schedule(os_id_t id)
*/
b_t _impl_trace_semaphore_snapshot(u32_t instance, kernal_snapshot_t *pMsgs)
{
#if defined KTRACE
semaphore_context_t *pCurSemaphore = NULL;
u32_t offset = 0u;
os_id_t id = OS_INVALID_ID;
Expand Down Expand Up @@ -568,6 +569,9 @@ b_t _impl_trace_semaphore_snapshot(u32_t instance, kernal_snapshot_t *pMsgs)

EXIT_CRITICAL_SECTION();
return TRUE;
#else
return FALSE;
#endif
}

#ifdef __cplusplus
Expand Down
Loading

0 comments on commit 841194b

Please sign in to comment.