LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Linux Coding - TSC Register (https://www.linuxquestions.org/questions/linux-software-2/linux-coding-tsc-register-588670/)

RedOctober45 10-01-2007 01:20 PM

Linux Coding - TSC Register
 
Hi guys!

I have an assginment in which code is given to me and I have to modify some errors injected by the professor on purpose, sort of like a warm-up exercise for the course. Anywho, this project involves comparing changes from gettimeofday() function with changes taken from the TSC register. The TSC register differences are converted to microseconds and compared to the gettimeofday() microsecond values, and to see the difference bewteen the exact same time differences (delta) from two different sources and to explain why, yada yada yada.

Part of this assignment is to correct the wrap-around effect of the TSC register, which is 64 bits. Unless I am doing something wrong, this register never wraps around, well it does, just not for a very very long time. I mean, it is 2^64 long. Even a 3GHZ processor would take forever to wrap that register around. My professor is annoyed with me bugging him about it and keeps saying that it wraps around and I am not understanding this right. Can someone clear this up for me? Can the TSC register wrap around, what am I doing wrong?

Here is a snipet of the main code:

Code:

main()
{
  struct timespec dt;
  struct timeval  bt, et;
  tsc_t      tsc_bt, tsc_et;
  double      delta_t, tsc_delta_t;
  int        i;

  printf("CPU clock: %.2f MHz\n", Init_TSC() / 1000000.0);

  for (i = 100; i < 500000; i += 100) {

    dt.tv_sec = 0;
    dt.tv_nsec =i  * 1000;

    gettimeofday( &bt, NULL );
    Read_TSC( tsc_bt );
    nanosleep( &dt, NULL );
    Read_TSC( tsc_et );
    gettimeofday( &et, NULL );

    delta_t  =  (et.tv_sec - bt.tv_sec) ;
    delta_t += (et.tv_usec - bt.tv_usec);
    tsc_delta_t = Convert_TSC( tsc_et - tsc_bt, TSC_Microseconds );

    printf("Requested %6d uS sleep:  actual %8.3f, tsc %8.3f, delta %.6f, %.3f%% err\n",
      i, delta_t, tsc_delta_t, delta_t - tsc_delta_t,
      (fabs(delta_t - tsc_delta_t) / delta_t) * 100.0);

  }
}

Convert_TSC() just converts the value from the TSC register into the modifier specified (TSC_Microseconds), which is microseconds. (Ingore any obvious math errors, I see them, just haven't changed them yet. I am most concerned about the wrap-around issue.)

Thank you so much guys!

studioj 10-01-2007 09:58 PM

perhaps the code was written for a 32 bit tsc ??
a 32 bit counter wrapps around in under a minute.

other wise i guess you just have to work the problem in "theory"
i guess you culd try to manually zero the counter to test your thoeretical solution.


All times are GMT -5. The time now is 12:20 PM.