Kernel FreeRTOSv202311.00 on a c66x TI DSP processor, task never has CPU

Hi there,

I ported several years ago FreeRTOS to a c66x DSP, it is working well.

However, I tried a little program an have a strange behavior for one task, always the same.

In the test, I create 32 tasks that are showing informations on the console with prints.

Each task just increment a counter and I see them in Trace32 FreeRTOS awareness.

The problem is that the task number 1 (tid==2) is never gets the CPU so no messages.

All the other tasks are displaying information and gets the CPU.

I tried this test on a ARM A15 port I did also and it works perfectly well.

Any help ?

Regards,

Hugo.

Does the counter for task number 1 increase? Can you remove prints to isolate if the issue is with the port or with prints? Do all the tasks block or are those always ready?

I put a flag initialized to 0 and set to 1 at the beginning of the task.

With prints, task01 flag is not set to 1 so the task never gets the CPU.

If I remove prints, all odd tasks are never scheduled for CPU, half of the tasks are running.

I created the 32 tasks in reverse order and now task30 never gets the CPU

Can you share your task definition?

Please let us see your task code. My suspicion is that your tasks are CPU bound, leaving only priorities for tasks to have a chance to run. Possibly you fall into the vTaskDelay(1) trap.

static void prvTask(void *p)
{
uint32_t n;

    n=(uint32_t)p;
    started[n]=1;

    counters[n]=0;
    for(;;)
    {
        if((counters[n]%10000000)==0)
        {
            osdep_console_lock();
            printf("\033[%d;0HTask%s%d(): counter==0x\033[K%08lx", 8+n, n<10?"0":"", n, counters[n]);
            osdep_console_unlock();
        }
        counters[n]+=1;
    }
}
SemaphoreHandle_t osdep_console_semaphore;

void osdep_console_lock(void)
{
    xSemaphoreTake(osdep_console_semaphore,portMAX_DELAY);
}

void osdep_console_unlock(void)
{
    xSemaphoreGive(osdep_console_semaphore);
}

int32_t osdep_console_lock_create(void)
{
int32_t r;

    osdep_console_semaphore=xSemaphoreCreateBinary();
    if(osdep_console_semaphore!=NULL)
    {
        if(xSemaphoreGive(osdep_console_semaphore)!=pdPASS) r=(-1);
        else r=0;
    }
    else { r=(-1); }
    return(r);
}

Tasks creation :

for(i=(N_TASKS-1);i>=0;i--)
{
    sprintf(tn,"Task%s%d",i<10?"0":"",i);
    if(xTaskCreate(prvTask,tn,configMINIMAL_STACK_SIZE,(void *)i,0,NULL)!=pdPASS)
    {
        for(;;);
    }
}

Hint: you could simply use sprintf(tn,"Task%02d",i); for 2 digit task number with leading 0

Also using (default ?) configMINIMAL_STACK_SIZE for your tasks might by not sufficient due to usage of rather stack hungry printf.
Note that all printf-family functions need a larger amount of stack.
It’s strongly recommended to enable FreeRTOS stack checking during development.

Great improvement, thanks a lot, another ?

ok, your problem is that your tasks are CPU bound (unless your printf does some implicit delaying or waiting), so what your semaphore does is pass the baton in very quick succession from one task to another which may end in a lockout. Iow, your low pri tasks are being starved by the hi pri ones that pass the batons back and forth between each other.

For multitasking to work as intended in an RTOS, each task must periodically yield the CPU to allow lower priority tasks to execute.

I have an ARM A15 port on the SOC (keystoneII) with the same kernel and application code, everything works fine

I have an ARM A72 port on a Jacinto SOC, with the same kernel and application stuff, it works perfectly well without any yield(), I’m NOT in cooperative mode.

The fact that a different processor doesn’t have the problem just says that it is faster and the higher priority task complete their work with spare time that gets passed down to the lower priority tasks.

As @RAc and @richard-damon already pointed that the likely cause is that your other tasks are exhausting the CPU. Would you please try by adding an explicit delay:

static void prvTask(void *p)
{
uint32_t n;

    n=(uint32_t)p;
    started[n]=1;

    counters[n]=0;
    for(;;)
    {
        if((counters[n]%10000000)==0)
        {
            osdep_console_lock();
            printf("\033[%d;0HTask%s%d(): counter==0x\033[K%08lx", 8+n, n<10?"0":"", n, counters[n]);
            osdep_console_unlock();
        }
        counters[n]+=1;
        vTaskDelay( pdMS_TO_TICKS( 1000 ) );
    }
}

If you have a way to examine counter without prints, I’d suggest to try the following:

static void prvTask(void *p)
{
uint32_t n;

    n=(uint32_t)p;
    started[n]=1;

    counters[n]=0;
    for(;;)
    {
        counters[n]+=1;
        vTaskDelay( pdMS_TO_TICKS( 1000 ) );
    }
}

No delays or yields must be used.

well in that case your only chance is to use an mcu that is fast enough, as Richard mentioned. Be aware, though, that this requirement pretty much precludes multitasking in the first place because if your highest priority task is completly cpu bound, it must starve all tasks below it; in the cases where it must yield (eg waiting for an interrupt), your next pri task will likely use up most of the remaining cycles, starving the ones below it… repeat. very likely your lowest pri tasks will starve to death sooner or later.

If your use case truly requires no cpu relinquishing, you need to reconsider your system design. Either don’t use an RTOS at all or run all tasks at the same priority with time slicing enabled, but that will likely break your real time requirements.

The whole purpose of an RTOS is to smartly distribute cpu cycles between mostly independent threads of execution, relying on those threads to be inactive frequently enough to allow other threads to fill in the gaps.

Also note that @aggarg did not propose a solution to your problem, just a strategy to verify the starvation hypothesis. If the code change he proposed resolves your issue, it means that you truly will have to rethink your system design and replace the waits by meaningful (in the context of your system) other ways to prevent your tasks from starving others.