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.

how to use CLK_gethtime when we set build option as -o3 etc.

I try to use CLK_gethtime() to calculate the running time of a function。 However when I put the build option -O3, it seems not correct, it just return -1 or -2 , I wonder if there is any other method we can apply to get the execution times。 thank you all
  • Miao,

    I'm not aware of a problem with CLK_gethtime() not working with -O3 optimization turned on.

    Have you verified that it works as expected with -O2?

    Have you looked at the disassembly to see if it is still calling CLK_gethtime() correctly?

    The CLK_gethtime() function works by reading the TSCL register.  Can you look to see what the actual value of this register is at the various breakpoints?

    Are you declaring your time value variables s as unsigned int?

    Also, please let us know:

    - What device you are working on.

    - What version of BIOS you are using.

    - What version of CCS/Code Gen Tools version you are using to building your application.

     

    Dave

  • Thank you so much about your help.

    I am working in Davinci 6446, the version of CCS is 3.3, the BIOS version is 5.33.06, the version of CCS/Code Gen is v6.0.8

    My code is like as follow:

     

    for(...)

    {...

    uint64_t    new_time    = 0; // typedef unsigned long uint64_t;
    uint64_t    old_time    = 0;
    uint64_t    delta_time  = 0;

    old_time = CLK_gethtime();

    my_func();

    new_time = CLK_gethtime();

    delta_time = (new_time - old_time);

    printf("encode cycle = %d\n",delta_time);

    ...}// end of for()

     

    Actually, I have 2 main problems, the first is : without any special optimization (-g , -fr )

    because my_func() is in a loop, when it runs several times, the return value of delta_time is like

    encode cycle = -498852525
    encode cycle = 1182426166
    encode cycle = 1278521984
    encode cycle = 1271789978
    encode cycle = 1409855485
    encode cycle = 1107205098
    encode cycle = 1001059091
    encode cycle = 874879279
    encode cycle = 948768678
    encode cycle = 946696466

    I guess the reason of the first negative number is that the running time of my_func() is out of the range of  CLK_gethtime, so it goes back to 0 in some time.

    I want to know if there is any solution to fix this problem, I try to use CLK_getltime, however, it just return 0 everytime.

     

    The second problem is : with the optimization (-o3 -k -mt –mw –pm –op3)

    the results printed are alway negative numbers. I also want some solution to this bug.

     

    So overall, I want to know if there is any other methods I can apply to get the running time of a function or a fragment of code.

    thank you again

  • I think your problem is probably related to CLK_gethtime() returns a 32-bit value, but you are assigning to a uint64_t type.   Do you have a #include <clk.h> in your .c file?


    If you use 32-bit types (uint32_t) instead of 64-bit type, and the code you are benchmarking takes less that 2^32 instruction cycles (4.29 seconds at 1GHz), then the math should work out.   Twos-compliment, unsigned arithmetic will handle the counter wraparound naturally.


    Can you try changing your uint64_t types to uint32_t?


    If you want a 64-bit timestamp, you can also read the TSCH/L registers directly.

    Something like the following should work.

    extern volatile cregister UInt32 TSCL, TSCH;

    /*
     *  ======== get64 ========
     */
    uint64_t get64(void)
    {
        UInt key;

        key = Hwi_disable();
        /* must read TSCL before TSCH */
        result->lo = TSCL;
        result->hi = TSCH;
        Hwi_restore(key);
    }

  • sorry for replying you so late, and I will try your suggestion.

    thank you so much!

  • first I would like to thank you for your advice.

    however, I don't know how to use it correctly. Here is my code:

    #include <clk.h>

    #include <hwi.h>

    extern volatile cregister uint32_t TSCL, TSCH;

    typedef struct {
    uint64_t lo;
    uint64_t hi;
    }t;

    uint64_t get64(void)
    {
        uint32_t key;
        t result;
        key = HWI_disable();
        /* must read TSCL before TSCH */
        result.lo = TSCL;
        result.hi = TSCH;
        HWI_restore(key);
        return result.hi;
    }

    uint64_t    new_time    = 0;
    uint64_t    old_time    = 0;

    uint64_t    delta_time = 0;

    old_time = get64();

    my_function();

    new_time = get64();

    delta_time = (new_time - old_time);

    printf("encode cycle: %ld - %ld = %ld\n",new_time,old_time,delta_time);

     

    But the results printed are not correct at all

    So I want to ask you the correct method of using get64()

    thank you again

  • I think you have some problems with your code.   You are only returning result.hi which is the high 64-bits of the timestamp counter.

    I think you should do something like this:

    #include <stdint.h>

    uint64_t get64()
    {
        extern volatile cregister uint32_t TSCH;
        extern volatile cregister uint32_t TSCL;
        uint64_t low32;
        uint64_t high32;
        uint64_t full64;
        Uns key;

        key = HWI_disable();
        low32 = TSCL;
        high32 = TSCH;
        HWI_restore(key);

        full64 = high32 << 32 | low32;

        return (full64);
    }

  • Hi, Karl

    I use your above methd get64() on DM648, while there is a question puzzle me a long time. I use the fun like this:

    (breakpoint) t1 = get64();
    s32Ret = functions(&handle);
    (breakpoint) t2 = get64();
    printf("time = %ld \n", (t2-t1) / CLK_countspms() );

    run the codec many times, every time the output is : "time = 8402", at the same time, i use a stopwatch, then i got the question, sometimes the stopwatch result consistent with the printf output, while more often the stopwatch results are much large than the printf output "time = 8042". I has tried to replace the get64() with CLK_gethtime(), and got the same result.

    Can you tell me what it is? why?
    Thanks a lot !

  • Hi shmily,

    It sounds like you are relying on the app output to know when to stop your stop watch, correct?

    Can you remove that printf statement and then retry your experiments?  printf statements halt the processor and can throw off your times. 

    You can use variables instead to capture the value of your time computation and the set break points to see the value once your app returns from the "functions()" call.

    (or, alternatively, you could use LOG_printf instead of printf)

    Steve

  • Hi, Steve,

    Sorry for replying you so late.

    You may not understand what I mean, as a matter of fact, it's nothing to do with printf, I have set two breakpoints:

    t1 = get64(); (breakpoint1) program run to there and stop, start the stopwatch: 00:00.0
    s32Ret = functions(&handle);
    t2 = get64(); (breakpoint2) program run to there and stop, stop the stopwatch: 00:23.3
    Log_printf(&trace, "time = %ld ms\n", (t2-t1) / CLK_countspms() ); ---->>>0 trace 795 ms

    Like above, the stopwatch result is much large than the trace output. I have run the program on platfrom DM6467, those two results are consistent.

    Now, i want to know if i configure the DM648 platform wrong , or the evm board itself has some problem.