3

I am trying to measure time take by processes in C++ program with linux and Vxworks. I have noticed that clock_gettime(CLOCK_REALTIME, timespec ) is accurate enough (resolution about 1 ns) to do the job on many Oses. For a portability matter I am using this function and running it on both Vxworks 6.2 and linux 3.7. I ve tried to measure the time taken by a simple print:

    #define <timers.h<
    #define <iostream>
    #define BILLION 1000000000L
    int main(){
       struct timespec start, end; uint32_t diff;
       for(int i=0; i<1000; i++){
         clock_gettime(CLOCK_REALTME, &start);
         std::cout<<"Do stuff"<<std::endl;
         clock_gettime(CLOCK_REALTME, &end);
         diff = BILLION*(end.tv_sec-start.tv_sec)+(end.tv_nsec-start.tv_nsec);
        std::cout<<diff<<std::endl;
       }
       return 0;
    }

I compiled this on linux and vxworks. For linux results seemed logic (average 20 µs). But for Vxworks, I ve got a lot of zeros , then 5000000 ns , then a lot of zeros... PS , for vxwroks, I runned this app on ARM-cortex A8, and results seemed random have anyone seen the same bug before,

sam60
  • 51
  • 2
  • 6
  • [This](https://stackoverflow.com/questions/13474000/arm-performance-counters-vs-linux-clock-gettime) looks similar to me, maybe the problem is with target platform and not vxworks. – Steeve Jul 25 '17 at 12:08

2 Answers2

1

In vxworks, the clock resolution is defined by the system scheduler frequency. By default, this is typically 60Hz, however may be different dependant on BSP, kernel configuration, or runtime configuration.

The VxWorks kernel configuration parameters SYS_CLK_RATE_MAX and SYS_CLK_RATE_MIN define the maximum and minimum values supported, and SYS_CLK_RATE defines the default rate, applied at boot.

The actual clock rate can be modified at runtime using sysClkRateSet, either within your code, or from the shell.

You can check the current rate by using sysClkRateGet.

Given that you are seeing either 0 or 5000000ns - which is 5ms, I would expect that your system clock rate is ~200Hz.

To get greater resolution, you can increase the system clock rate. However, this may have undesired side effects, as this will increase the frequency of certain system operations.

A better method of timing code may be to use sysTimestamp which is typically driven from a high frequency timer, and can be used to perform high-res timing of short-lived activities.

mjs
  • 2,837
  • 4
  • 28
  • 48
-1

I think in vxworks by default the clock resolution is 16.66ms which you can get by calling clock_getres() function. You can change the resolution by calling sysclkrateset() function(max resolution supported is 200us i guess by passing 5000 as argument to sysclkrateset function). You can then calculate the difference between two timestamps using difftime() function

Harry
  • 2,177
  • 1
  • 19
  • 33