INT32U CTask::GetStackFree() { OS_STK_DATA stk; OSTaskStkChk(m_nPrio, &stk); return stk.OSFree; }
INT8U CTask::GetStackUsage() { OS_STK_DATA stk; OSTaskStkChk(m_nPrio, &stk); return stk.OSUsed * 100 / (stk.OSFree + stk.OSUsed); }
/* ********************************************************************************************************* * STACK CHECK TASK ********************************************************************************************************* */ void TaskStkChk(void *pdata) { INT8U fl_index = 0; INT8U fl_total_task = sizeof(task_para)/sizeof(TASK_PARA_ST); pdata = pdata; printf("TaskStkChk started!\r\n"); for(;;) { for(fl_index = 0; fl_index < fl_total_task; ++fl_index) { OSTaskStkChk(task_para[fl_index].prio, &task_para[fl_index].stk_chk_data); printf("[%02d] used: %4d, free: %4d\r\n", task_para[fl_index].id, \ task_para[fl_index].stk_chk_data.OSUsed, task_para[fl_index].stk_chk_data.OSFree); } OSTimeDly(OS_TICKS_PER_SEC); } }
void RunStackCheck(void) { OS_STK_DATA stk; PrintLog(LOGTYPE_DEBUG,"Running Stack Check!\r\n"); while(1) { int i; for(i=0;i<32;i++) { if(i == 26) continue; if(OSTaskStkChk(i,&stk) == OS_ERR_NONE) { const char *name = SysGetTaskName(i); if(name) PrintLog(LOGTYPE_DEBUG,"Stack pro:%d, free:%d, used:%d name:%s\r\n",i,stk.OSFree,stk.OSUsed,name); } } OSTimeDly(10); } }
void OS_StatTask (void *p_arg) { #if OS_CFG_DBG_EN > 0u #if OS_CFG_TASK_PROFILE_EN > 0u OS_CPU_USAGE usage; OS_CYCLES cycles_total; #endif OS_TCB *p_tcb; #endif OS_ERR err; OS_TICK dly; CPU_TS ts_start; CPU_TS ts_end; CPU_SR_ALLOC(); p_arg = p_arg; /* Prevent compiler warning for not using 'p_arg' */ while (OSStatTaskRdy != DEF_TRUE) { OSTimeDly(2u * OSCfg_StatTaskRate_Hz, /* Wait until statistic task is ready */ OS_OPT_TIME_DLY, &err); } OSStatReset(&err); /* Reset statistics */ dly = (OS_TICK)0; /* Compute statistic task sleep delay */ if (OSCfg_TickRate_Hz > OSCfg_StatTaskRate_Hz) { dly = (OS_TICK)(OSCfg_TickRate_Hz / OSCfg_StatTaskRate_Hz); } if (dly == (OS_TICK)0) { dly = (OS_TICK)(OSCfg_TickRate_Hz / (OS_RATE_HZ)10); } while (DEF_ON) { ts_start = OS_TS_GET(); #ifdef CPU_CFG_INT_DIS_MEAS_EN OSIntDisTimeMax = CPU_IntDisMeasMaxGet(); #endif CPU_CRITICAL_ENTER(); /* ----------------- OVERALL CPU USAGE ------------------ */ OSStatTaskCtrRun = OSStatTaskCtr; /* Obtain the of the stat counter for the past .1 second */ OSStatTaskCtr = (OS_TICK)0; /* Reset the stat counter for the next .1 second */ CPU_CRITICAL_EXIT(); if (OSStatTaskCtrMax > OSStatTaskCtrRun) { if (OSStatTaskCtrMax > (OS_TICK)0) { OSStatTaskCPUUsage = (OS_CPU_USAGE)((OS_TICK)100u - 100u * OSStatTaskCtrRun / OSStatTaskCtrMax); } else { OSStatTaskCPUUsage = (OS_CPU_USAGE)100; } } else { OSStatTaskCPUUsage = (OS_CPU_USAGE)100; } OSStatTaskHook(); /* Invoke user definable hook */ #if OS_CFG_DBG_EN > 0u #if OS_CFG_TASK_PROFILE_EN > 0u cycles_total = (OS_CYCLES)0; p_tcb = OSTaskDbgListPtr; while (p_tcb != (OS_TCB *)0) { /* ----------------- TOTAL CYCLES COUNT ----------------- */ OS_CRITICAL_ENTER(); p_tcb->CyclesTotalPrev = p_tcb->CyclesTotal; /* Save accumulated # cycles into a temp variable */ p_tcb->CyclesTotal = (OS_CYCLES)0; /* Reset total cycles for task for next run */ OS_CRITICAL_EXIT(); cycles_total += p_tcb->CyclesTotalPrev;/* Perform sum of all task # cycles */ p_tcb = p_tcb->DbgNextPtr; } #endif #if OS_CFG_TASK_PROFILE_EN > 0u cycles_total /= 100; /* ------------- INDIVIDUAL TASK CPU USAGE -------------- */ #endif p_tcb = OSTaskDbgListPtr; while (p_tcb != (OS_TCB *)0) { #if OS_CFG_TASK_PROFILE_EN > 0u /* Compute execution time of each task */ if (cycles_total > (OS_CYCLES)0) { usage = (OS_CPU_USAGE)(p_tcb->CyclesTotalPrev / cycles_total); if (usage > 100u) { usage = 100u; } } else { usage = 0u; } p_tcb->CPUUsage = usage; #endif #if OS_CFG_STAT_TASK_STK_CHK_EN > 0u OSTaskStkChk( p_tcb, /* Compute stack usage of active tasks only */ &p_tcb->StkFree, &p_tcb->StkUsed, &err); #endif p_tcb = p_tcb->DbgNextPtr; } #endif if (OSStatResetFlag == DEF_TRUE) { /* Check if need to reset statistics */ OSStatResetFlag = DEF_FALSE; OSStatReset(&err); } ts_end = OS_TS_GET() - ts_start; /* Measure execution time of statistic task */ if (ts_end > OSStatTaskTimeMax) { OSStatTaskTimeMax = ts_end; } OSTimeDly(dly, OS_OPT_TIME_DLY, &err); } }