Nios® II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
Intel Support hours are Monday-Fridays, 8am-5pm PST, except Holidays. Thanks to our community members who provide support during our down time or before we get to your questions. We appreciate you!

Need Forum Guidance? Click here
Search our FPGA Knowledge Articles here.

OSCPUUsage higher than 100?

Honored Contributor II

I wanted to measure maximal and average CPU usage in my design by checking the global variable OSCPUUsage. Therefore, I made a simple application containing 3 tasks doing some dummy array reading/writing/copying + one main task that displays CPU load values on a LCD screen. I use an INK board with MicroC/OS-II. 


The trouble is that the maximal CPU usage value is higher than 100! While debugging the design and putting a breakpoint inside OSTaskStat, I can see that the first non-zero value of OSCPUUsage is higher than 100. All the next values calculated in this task are much lower. 


My questions are: what causes such high OSCPUUsage values? Should I change the measurement method I use or treat it as a mistake? 


#include <stdio.h># include "includes.h"# include "system.h"# include "altera_avalon_lcd_16207_regs.h"# include "altera_avalon_pio_regs.h" /* Definition of Task Stacks */# define TASK_STACKSIZE 2048 OS_STK task0_stk; OS_STK task1_stk; OS_STK task2_stk; OS_STK task3_stk; /* Definition of Task Priorities */# define TASK0_PRIORITY 1# define TASK1_PRIORITY 2# define TASK2_PRIORITY 3# define TASK3_PRIORITY 4 /*Adjustable parameters*/# define ARRAY_NUM 2000# define DELAY_MS 200 //delay of the task that displays OSCPUUsage on LCD screen# define AVG_SAMPLES 100 /* SRAM data */ int CopyArray1 ; int CopyArray2 ; /*CPU load statistics*/ int CPUUsageMax=0; int CPUUsageAvg=0; int CPUUsageSum=0; int CPUUsageDiv=0; /*Task prototypes*/ void task0 (void* pdata); void task1 (void* pdata); void task2 (void* pdata); void task3 (void* pdata); FILE* lcd; void task0(void*pdata) { OSStatInit(); OSTaskCreateExt(task1, NULL, &task1_stk, TASK1_PRIORITY, TASK1_PRIORITY, task1_stk, TASK_STACKSIZE, NULL, 0); OSTaskCreateExt(task2, NULL, &task2_stk, TASK2_PRIORITY, TASK2_PRIORITY, task2_stk, TASK_STACKSIZE, NULL, 0); OSTaskCreateExt(task3, NULL, &task3_stk, TASK3_PRIORITY, TASK3_PRIORITY, task3_stk, TASK_STACKSIZE, NULL, 0); while(1) { IOWR_ALTERA_AVALON_PIO_DATA(APPL_SUBSYSTEM_PIO_LED_RED_BASE, 0x1); //Display the maximal CPU usage and the average CPU usage from last AVG_SAMPLES measurements if (OSCPUUsage>CPUUsageMax) { //Maximal CPU usage: CPUUsageMax=OSCPUUsage; } if (CPUUsageDiv>=AVG_SAMPLES) //Average value of (AVG_SAMPLES) samples: { CPUUsageDiv=0; CPUUsageSum=0; CPUUsageAvg=0; } else { CPUUsageDiv++; CPUUsageSum+=OSCPUUsage; CPUUsageAvg=CPUUsageSum/CPUUsageDiv; } fprintf(lcd, "Max:%u Avg:%u \n", CPUUsageMax, CPUUsageAvg); OSTimeDlyHMSM(0, 0, 0, DELAY_MS); } } int main(void) { IOWR_ALTERA_AVALON_PIO_DATA(APPL_SUBSYSTEM_PIO_LED_RED_BASE, 0x0); lcd = fopen(APPL_SUBSYSTEM_LCD_NAME, "w"); OSInit(); OSTaskCreateExt(task0, NULL, &task0_stk, TASK0_PRIORITY, TASK0_PRIORITY, task0_stk, TASK_STACKSIZE, NULL, 0); OSStart(); fclose(lcd); return 0; } void task1(void* pdata) { while (1) { for (int i=0;i<ARRAY_NUM;i++) { //Some array operations performed just to check the CPU load CopyArray1=1; CopyArray2=CopyArray1; (void) CopyArray2; } //Sanity check: blinking LEDs IOWR_ALTERA_AVALON_PIO_DATA(APPL_SUBSYSTEM_PIO_LED_RED_BASE, 0x2); OSTimeDlyHMSM(0, 0, 0, 100); } } void task2(void* pdata) { while (1) { for (int i=0;i<ARRAY_NUM;i++) { CopyArray1=2; CopyArray2=CopyArray1; (void) CopyArray2; } IOWR_ALTERA_AVALON_PIO_DATA(APPL_SUBSYSTEM_PIO_LED_RED_BASE, 0x4); OSTimeDlyHMSM(0, 0, 0, 10); } } void task3(void*pdata) { while (1) { for (int i=0;i<ARRAY_NUM;i++) { CopyArray1=3; CopyArray2=CopyArray1; (void) CopyArray2; } IOWR_ALTERA_AVALON_PIO_DATA(APPL_SUBSYSTEM_PIO_LED_RED_BASE, 0x8); OSTimeDlyHMSM(0, 0, 0, 1); } }  



0 Kudos
0 Replies