Measuring execution time with timing.h

Hi, there i want to measure code execution time of a function. I came across https://docs.zephyrproject.org/latest/reference/timing_functions/index.html. I have enabled CONFIG_TIMING_FUNCTIONS on my prj.conf file. However the build cannot link to the timing functions. I get undefined references to both board_timing functions and soc_timing functions. How can i fix this. Additionally is there a better way to measure code execution time ?

Parents
  • anybidy got the answer? I also trrying to use this function but got error undefined references to timer function.

    For example

     

    C:\nrf\example\button\src\main.c:21: undefined reference to `soc_timing_init'
     

    I already add some c++ in configuration files.

    Below is my configuration files.

    # Include C++ Support
    CONFIG_CPLUSPLUS=y
    
    #include std c/c++, do not remove else standard design patterns will not work
    CONFIG_NEWLIB_LIBC=y
    CONFIG_LIB_CPLUSPLUS=y
    
    # Log
    CONFIG_LOG=y
    CONFIG_LOG_PRINTK=y
    CONFIG_LOG_MODE_DEFERRED=y
    CONFIG_USE_SEGGER_RTT=y
    CONFIG_RTT_CONSOLE=y
    CONFIG_LOG_BACKEND_UART=y
    
    # Timer
    CONFIG_TIMING_FUNCTIONS=y
    
    # GPIO
    CONFIG_GPIO=y
    
    

    Anybody have solution for this? thanks.

  • Have you included timing.h in your source file?
    #include <zephyr/timing/timing.h>

    Have you enabled the Zephyr subsystem timing library?

    soc_timing_init is defined in zephyr/soc/arm/nordic_nrf/timing.c, which is included by zephyr/soc/arm/nordic_nrf/CMakeLists.txt:

    if(CONFIG_SOC_HAS_TIMING_FUNCTIONS AND NOT CONFIG_BOARD_HAS_TIMING_FUNCTIONS)
      if(CONFIG_TIMING_FUNCTIONS)
        # Use nRF-specific timing calculations only if DWT is not present
        if(NOT CONFIG_CORTEX_M_DWT)
          zephyr_library_sources(timing.c)
        endif()
      endif()
    endif()

    Verify the dependencies listed above.

  • I have already done all those steps, but now I get the following error: "zephyr/timing/timing.h: No such a file or directory". 

    Eventhough I have in my source file the next line without any error: 

    #include <timing/timing.h>

Reply Children
  • Have you satisfied the dependencies listed in the snippet from the CMakeList.txt in my previous post?

  • Yes. Of course. Finally I got the compilation right. I saw in zephyr website than I have to use: “timing_init()” instead of “soc_timing_init()”. 
    But now I have a question if can solve it. When I use the function “timing_cycles_to_ns(total_cycles)” I always have the same value which is 240. 
    In the previous step I coded this: “total_cycles = timing_cycles_get(&start_time, &end_time);” and the result is correctly calculated and consistent

  • Can you share build/zephyr/.config? 

    Can you share a code snippet of your timing code? 

  • prj.config:

    CONFIG_BT=y
    CONFIG_BT_DEBUG_LOG=y
    CONFIG_BT_CENTRAL=y
    CONFIG_BT_PRIVACY=y
    CONFIG_BT_SCAN_WITH_IDENTITY=y

    CONFIG_BT_CTLR_TX_PWR_PLUS_3=y

    CONFIG_BT_SCAN=y
    CONFIG_BT_SCAN_FILTER_ENABLE=y
    CONFIG_BT_SCAN_UUID_CNT=1

    CONFIG_BT_BROADCASTER=y
    CONFIG_BT_OBSERVER=y

    CONFIG_BT_EXT_ADV=y
    CONFIG_BT_CTLR_ADV_EXT=y
    CONFIG_BT_CTLR_PHY_CODED=y
    CONFIG_BT_USER_PHY_UPDATE=y

    CONFIG_CONSOLE_HANDLER=y

    CONFIG_BT_EXT_ADV_MAX_ADV_SET=2

    CONFIG_TINYCRYPT=y
    CONFIG_TINYCRYPT_SHA256=y
    CONFIG_TINYCRYPT_ECC_DSA=y

    CONFIG_ENTROPY_GENERATOR=y
    CONFIG_STDOUT_CONSOLE=y
    CONFIG_ENTROPY_DEVICE_RANDOM_GENERATOR=y

    #
    # C Library
    #
    CONFIG_NEWLIB_LIBC=y
    CONFIG_TIMING_FUNCTIONS=y
    # end of C Library

    #Timer functions
    #CONFIG_SOC_HAS_TIMING_FUNCTIONS=y
    #CONFIG_SOC_SERIES_NRF53X=y
    #CONFIG_SOC_COMPATIBLE_NRF=y

    main (source file):

    // Timers
    timing_t start_time1, end_time1;
    uint64_t total_cycles;
    uint64_t total_ns;

    timing_init();
    timing_start();

    start_time1 = timing_counter_get();

    // code_execution_to_be_measured();


    sign_message_rsa(); //Encrypted RSA
    bt_le_ext_adv_set_data(adv, buffer, ARRAY_SIZE(buffer), NULL, 0);
    end_time1 = timing_counter_get();
    total_cycles = timing_cycles_get(&start_time1, &end_time1);
    total_ns = timing_cycles_to_ns(total_cycles);

    timing_stop();

    printk("#;%d;1;%x\n",_rssi,time_stamp);
  • I need the generated build/zephyr/.config, not prj.conf. 

    PS:
    How are you reading out the values of total_cycles and total_ns ?

Related