CPU time in Windows

Discussion of chess software programming and technical issues.

Moderators: hgm, Harvey Williamson, bob

User avatar
hgm
Posts: 22083
Joined: Fri Mar 10, 2006 9:06 am
Location: Amsterdam
Contact:

CPU time in Windows

Post by hgm » Mon Feb 13, 2012 6:18 pm

Does anyone know how to get the CPU time used by the current process in Windows? I thought that clock() was supposed to do that, but it seems to always return a value identical to the wall-clock time from GetTickCount().

User avatar
Jim Ablett
Posts: 1327
Joined: Fri Jul 14, 2006 5:56 am
Location: London, England
Contact:

Re: CPU time in Windows

Post by Jim Ablett » Mon Feb 13, 2012 6:50 pm

hgm wrote:Does anyone know how to get the CPU time used by the current process in Windows? I thought that clock() was supposed to do that, but it seems to always return a value identical to the wall-clock time from GetTickCount().
How about getrusage >

Code: Select all

#include <sys/resource.h>
#include <errno.h>

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

int __cdecl getrusage &#40;int who, struct rusage *r_usage&#41;
&#123;
  FILETIME dummy;
  ULARGE_INTEGER KernelTime;
  ULARGE_INTEGER UserTime;

  if (!rusage&#41; 
    &#123;
      errno = EFAULT;
      return -1;
    &#125;

  if &#40;who != RUSAGE_SELF&#41;
    &#123;
      errno = EINVAL;
      return -1;
    &#125;

  GetProcessTimes &#40;GetCurrentProcess (), &dummy, &dummy,
                   &#40;LPFILETIME&#41; &KernelTime, &#40;LPFILETIME&#41; &UserTime&#41;;

  r_usage->ru_stime.tv_sec = &#40;long&#41; &#40;0x7fffffff
				     & &#40;unsigned long&#41; KernelTime.QuadPart / 10000000&#41;;
  r_usage->ru_utime.tv_sec = &#40;long&#41; &#40;0x7fffffff
				     & &#40;unsigned long&#41; UserTime.QuadPart / 10000000&#41;;
  r_usage->ru_stime.tv_usec = &#40;long&#41;&#40;0x7fffffff
				     & &#40;unsigned long&#41; (&#40;KernelTime.QuadPart % 10000000&#41; / 10&#41;);
  r_usage->ru_utime.tv_usec = &#40;long&#41;&#40;0x7fffffff
				     & &#40;unsigned long&#41; (&#40;UserTime.QuadPart % 10000000&#41; / 10&#41;);

  return 0;
&#125;
Jim.

User avatar
hgm
Posts: 22083
Joined: Fri Mar 10, 2006 9:06 am
Location: Amsterdam
Contact:

Re: CPU time in Windows

Post by hgm » Mon Feb 13, 2012 7:27 pm

Thanks!

A big request:

could you make a Fruit 2.1 Windows compile for me that prints the real time and the user time? I don't manage to compile it with gcc in -mno-cygwin mode, it produces tons of error messages. (On Linux it compiled fine, and cpu_now() and real_now() also worked with good precision.) What I need is a version that prints

Code: Select all

 startTime = WallClockTime&#40;); // time stamp &#40;millisec&#41;
cpuTime = CPUtime&#40;); // Total CPU used upto now &#40;millisec&#41;
printf&#40;"info string times @ %u\n", startTime&#41;;
immediately after it receives a "go" command (this code is in protocol.cpp), and

Code: Select all

stopTime = WallClockTime&#40;); // time stamp
cpuTime = CPUtime&#40;) - cpuTime; // Consumed CPU time
printf&#40;"info string times @ %u&#58; real=%u cpu=%u\n", stopTime, stopTime - startTime, cpuTime&#41;;
just before it sends "bestmove". (Also in protocol.cpp.)

I have written a small program that extracts such messages from the debug file, and calculates from them the delay between sending a move and the opponent receiving it, and CPU time stolen by the GUI.

User avatar
Jim Ablett
Posts: 1327
Joined: Fri Jul 14, 2006 5:56 am
Location: London, England
Contact:

Re: CPU time in Windows

Post by Jim Ablett » Tue Feb 14, 2012 1:11 am

hgm wrote:Thanks!

A big request:

could you make a Fruit 2.1 Windows compile for me that prints the real time and the user time? I don't manage to compile it with gcc in -mno-cygwin mode, it produces tons of error messages. (On Linux it compiled fine, and cpu_now() and real_now() also worked with good precision.) What I need is a version that prints

Code: Select all

 startTime = WallClockTime&#40;); // time stamp &#40;millisec&#41;
cpuTime = CPUtime&#40;); // Total CPU used upto now &#40;millisec&#41;
printf&#40;"info string times @ %u\n", startTime&#41;;
immediately after it receives a "go" command (this code is in protocol.cpp), and

Code: Select all

stopTime = WallClockTime&#40;); // time stamp
cpuTime = CPUtime&#40;) - cpuTime; // Consumed CPU time
printf&#40;"info string times @ %u&#58; real=%u cpu=%u\n", stopTime, stopTime - startTime, cpuTime&#41;;
just before it sends "bestmove". (Also in protocol.cpp.)

I have written a small program that extracts such messages from the debug file, and calculates from them the delay between sending a move and the opponent receiving it, and CPU time stolen by the GUI.

http://dl.dropbox.com/u/5047625/fruit-2 ... est%29.zip


Jim.

User avatar
Jim Ablett
Posts: 1327
Joined: Fri Jul 14, 2006 5:56 am
Location: London, England
Contact:

Re: CPU time in Windows

Post by Jim Ablett » Tue Feb 14, 2012 11:34 am

This is a better solution to Mingw32 cpu time bug >

Code: Select all

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

static void
__winnt_cpu_time &#40;long *sec, long *usec&#41;
&#123;
  union &#123;
    FILETIME ft;
    unsigned long long ulltime;
  &#125; kernel_time,  user_time;

  FILETIME unused1, unused2;
  unsigned long long total_time;

  /* No support for Win9x.  The high order bit of the DWORD
     returned by GetVersion is 0 for NT and higher. */
  if &#40;GetVersion () >= 0x80000000&#41;
    &#123;
      *sec = -1;
      *usec = 0;
      return;
    &#125;

  /* The FILETIME structs filled in by GetProcessTimes represent
     time in 100 nanosecond units. */
  GetProcessTimes &#40;GetCurrentProcess (), &unused1, &unused2,
              	   &kernel_time.ft, &user_time.ft&#41;;
      
  total_time = &#40;kernel_time.ulltime + user_time.ulltime&#41;/10; 
  *sec = total_time / 1000000;
  *usec = total_time % 1000000;
&#125;

Jim.

Daniel Shawul
Posts: 3429
Joined: Tue Mar 14, 2006 10:34 am
Location: Ethiopia
Contact:

Re: CPU time in Windows

Post by Daniel Shawul » Tue Feb 14, 2012 12:41 pm

I am surprized though why unix clock() measures the cpu time. C++ seems to define clock() as a measure of wall clock time http://www.cplusplus.com/reference/clib ... ime/clock/. It is mentioned that the reference could be different on different systems but nothing about some systems excluding I/O time.

User avatar
hgm
Posts: 22083
Joined: Fri Mar 10, 2006 9:06 am
Location: Amsterdam
Contact:

Re: CPU time in Windows

Post by hgm » Sun Feb 19, 2012 12:17 pm

Jim, could you make another Win-32 compile of Fruit for me? I have now increased the precision of the timers to 0.1 msec, for both wall-clock and CPU time.

Just use the file at http://hgm.nubati.net/protocol.cpp in stead of the normal one, that should do it.

I plan to distribute this version of Fruit 2.1 with the WinBoard binary package, and perhaps also make a separate 'timing kit' for people wanting to evaluate GUI performance, with this Fruit, a similarly adapted Fairy-Max and a tool to extract the timing lines from a debug file, and print the timing stats.


User avatar
hgm
Posts: 22083
Joined: Fri Mar 10, 2006 9:06 am
Location: Amsterdam
Contact:

Re: CPU time in Windows

Post by hgm » Sun Feb 19, 2012 1:00 pm

Wow, you are fast! :D Thanks!

rbarreira
Posts: 900
Joined: Tue Apr 27, 2010 1:48 pm

Re: CPU time in Windows

Post by rbarreira » Sun Feb 19, 2012 1:07 pm

Daniel Shawul wrote:I am surprized though why unix clock() measures the cpu time. C++ seems to define clock() as a measure of wall clock time http://www.cplusplus.com/reference/clib ... ime/clock/. It is mentioned that the reference could be different on different systems but nothing about some systems excluding I/O time.
Actually that page is even more confusing, as it talks about "processing times" as well.

Post Reply