首先设置:#define OS_TASK_STAT_EN 1
在OSInit();中就建立了统计任务
- #if OS_TASK_STAT_EN > 0
- void OS_TaskStat (void *p_arg)
- {
- INT32U run;
- INT32U max;
- INT8S usage;
- #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
- OS_CPU_SR cpu_sr = 0;
- #endif
- p_arg = p_arg; /* Prevent compiler warning for not using 'parg' */
- while (OSStatRdy == OS_FALSE) {
- OSTimeDly(2 * OS_TICKS_PER_SEC / 10); /* Wait until statistic task is ready */
- }
- max = OSIdleCtrMax / 100L;
- for (;;) {
- OS_ENTER_CRITICAL();
- OSIdleCtrRun = OSIdleCtr; /* Obtain the of the idle counter for the past second */
- run = OSIdleCtr;
- OSIdleCtr = 0L; /* Reset the idle counter for the next second */
- OS_EXIT_CRITICAL();
- if (max > 0L) {
- usage = (INT8S)(100L - run / max);
- if (usage >= 0) { /* Make sure we don't have a negative percentage */
- OSCPUUsage = usage;
- } else {
- OSCPUUsage = 0;
- }
- } else {
- OSCPUUsage = 0;
- max = OSIdleCtrMax / 100L;
- }
- OSTaskStatHook(); /* Invoke user definable hook */
- #if (OS_TASK_STAT_STK_CHK_EN > 0) && (OS_TASK_CREATE_EXT_EN > 0)
- OS_TaskStatStkChk(); /* Check the stacks for each task */
- #endif
- OSTimeDly(OS_TICKS_PER_SEC / 10); /* Accumulate OSIdleCtr for the next 1/10 second */
- }
- }
- #endif
ucos中对CPU使用率查看
下面你就可以在自己的任务中输出相关信息了:
- printf("\r\n\r\n Micrium uC/OS-II \r\n");
- printf(" ST STM32 (Cortex-M3)\r\n\r\n");
- printf(" uC/OS-II: V%ld.%ld%ld\r\n",OSVersion()/100,(OSVersion() % 100) / 10,(OSVersion() % 10));
- printf(" TickRate: %ld \r\n",OS_TICKS_PER_SEC);
- printf(" CPU Usage: %ld% \r\n",OSCPUUsage);
- printf(" CPU Speed: %ld MHz \r\n",BSP_CPU_ClkFreq() / 1000000L);
- printf(" #Ticks: %ld \r\n",OSTime);
- printf(" #CtxSw: %ld \r\n",OSCtxSwCtr);
依次为:版本号,CPU使用率,主频,任务切换次数等!
http://blog.youkuaiyun.com/l545045612/article/details/7823046