This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

CPU Usage Monitor using RTX

I'm developing a CPU usage monitor on a ADuC7126 using RTX that will later be incorporated into other projects but have ran into a subtle issue that I hope someone can shed some light into.

The mechanism is to use a recurring 1 second timer inside a task to calculate the CPU usage and a incremental counter in os_idle_demon(). Since this project has only 2 tasks running, tskCpuUsageCalc and os_idle_demon, I would assume the counter would have the same ballpark value each time the timer expires since it's idling consistently but it's not. Most of the time the counter value is ~125000 but occasionally (1 out of ~5) it would be ~250000 and very rarely it would be ~375000. It seems like it's always 1X, 2X, or 3X. I tried to define the counter variable as volatile to prevent caching but the result is still the same.

Here is the snippit in RTX_Conf_ADuC712x.c:

extern OS_TID tidCpuUsageCalc;
extern volatile U32 cpuIdleCnt;
...
__task void os_idle_demon (void)
{
    for (;;)
    {
        /* HERE: include optional user code to be executed when no task runs.*/
        cpuIdleCnt++;
    }
}

void os_tmr_call (U16 info)
{
    switch (info)
    {
        case TMR_1_SECOND:
            isr_evt_set(EVT_TMSTMP_PROC, tidCpuUsageCalc);
            break;
    }
}

Here is the snippit in main.c:

volatile U32 cpuIdleCnt = 0;
int cpuUsage = 0;
...
__task void tskCpuUsageCalc(void)
{
    // Create a 1sec timer
    oidTmr1sec = os_tmr_create (100, TMR_1_SECOND);

    while (1)
    {
        os_evt_wait_or(EVT_TMSTMP_PROC, 0xFFFF);

        // Calculate CPU usage
        cpuUsage = 100 - ((cpuIdleCnt * 100) / MAX_IDLE_CNT);

        // Initialize cpuIdleCnt before starting the timer again
        cpuIdleCnt = 0;

        // Recreate 1sec timer when it expires since RTX only has 1-shot timers
        oidTmr1sec = os_tmr_create (100, TMR_1_SECOND);
    }
}

__task void init_task(void)
{
    tidCpuUsageCalc = os_tsk_create_user(tskCpuUsageCalc, 50, &CpuUsage_stack,    sizeof(CpuUsage_stack));
    os_tsk_delete_self();
}

int main (void)
{
    os_sys_init(init_task);                   /* Initialize RTX */
}

Any advice would be appreciated.

0