This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Same code / different time

Other Parts Discussed in Thread: SYSBIOS


Hi all,

I have a piece of code that I want to run periodically on my C6678EVM. It is very important for me to control the time it takes this code. The problem is that I have observed that, sometimes, it takes longer than usual. I have managed to reduce the code to the minimum to try to find the problem. Now, it looks like this:

#include <stdio.h>
#include <ti/sysbios/BIOS.h>
#include <ti/csl/csl_chip.h>

uint32_t guiProfileMin=0xFFFFFFFF;
uint32_t guiProfileMax=0;
uint64_t guiProfileTotal=0;
uint32_t guiProfileNumIter=0;
uint32_t guiProfileAvg=0;

#define NUM_MEASURES 100000000

void MyFunction()
{
    uint64_t uiTimeOrigin = 0;
    uint32_t uiTimeSpent = 0;
    
    TSCH = 0;
    TSCL = 0;

    while (guiProfileNumIter < NUM_MEASURES)
    {
        uiTimeOrigin = _itoll(TSCH, TSCL);        /* TIME 1 */

        // Future additional code here

        uiTimeSpent = (uint32_t)(_itoll(TSCH, TSCL) - uiTimeOrigin);    /* TIME 2 */
        
        // Update min, max and avg
        if (uiTimeSpent < guiProfileMin)
            guiProfileMin = uiTimeSpent;
        if (uiTimeSpent > guiProfileMax)
            guiProfileMax = uiTimeSpent;
        guiProfileTotal += uiTimeSpent;
        guiProfileNumIter ++;
        guiProfileAvg = (uint32_t)(guiProfileTotal / guiProfileNumIter);
    }
    
    printf("guiProfileMin: %4d\n", guiProfileMin);
    printf("guiProfileMax: %4d\n", guiProfileMax);
    printf("guiProfileAvg: %4d\n", guiProfileAvg);
}

void core3_taskMain(void)
{
    MyFunction();
}

int main()
{
    BIOS_start ();
}

Basically, I measure the time between TIME1 and TIME2. In this code, it should be near 0 cycles (0 ns), since there is no code between those points. The problem is that, after running it in CCCS, I am getting the following output:

[C66xx_0] guiProfileMin:    8
[C66xx_0] guiProfileMax:  331
[C66xx_0] guiProfileAvg:    8

This means that, although in most of the iteration the time lapse is 8 ns (ok), sometimes it takes up to 331 ns between those points. Am I missing something? I think it should take always the same time...

The only idea that comes to my mind is that some kind of Task Scheduler is running periodically, affecting the times. If it was the reason, how could I disable it?

By the way, I am using bios_6_33_06_50

Best regards,
Alejandro


  • Hi Alejandro,

    The guiProfileMax is 331 cycles most likely because the cache is cold when the loop is executed for the very first time. I bet the second, third and all subsequent iterations take only 8 cycles.

    If the code that is being executed between TIME1 and TIME2 is critical, you could disable tasking to prevent any task switches by protecting the code block with a Task_disable/restore. The code can still be interrupted by hardware interrupts though. You could also disable interrupts using Hwi_disable/restore.

    If you plan to use Hwi_disable/restore to disable interrupts, I dont think you need to use Task_disable/restore unless the critical code block tries to trigger the task scheduler using a Semaphore_post/pend or some other similar event.

    Best,

    Ashish

  • Dear Ashish,

    thank you very much for your suggestions! Using Task_disable has no effect in my case, but using Hwi_disable makes min=max so I will disable interrupts.