/**CFile*********************************************************************** FileName [ cpu_time.c ] PackageName [ util ] Synopsis [ System time calls ] Description [ The problem is that all unix systems have a different notion of how fast time goes (i.e., the units returned by). This returns a consistent result. ] Author [ Stephen Edwards and others ] Copyright [Copyright (c) 1994-1996 The Regents of the Univ. of California. All rights reserved. Permission is hereby granted, without written agreement and without license or royalty fees, to use, copy, modify, and distribute this software and its documentation for any purpose, provided that the above copyright notice and the following two paragraphs appear in all copies of this software. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.] ******************************************************************************/ #include "util.h" //#if HAVE_SYS_TYPES_H # include //#endif //#if HAVE_SYS_TIMES_H # include //#endif /**AutomaticStart*************************************************************/ /*---------------------------------------------------------------------------*/ /* Static function prototypes */ /*---------------------------------------------------------------------------*/ /**AutomaticEnd***************************************************************/ /**Function******************************************************************** Synopsis [ Return elapsed time in milliseconds ] Description [ Return a long which represents the elapsed time in milliseconds since some constant reference.

There are two possibilities:

  1. The system is non-POSIX compliant, so unistd.h and hence sysconf() can't tell us the clock tick speed. At this point, we have to resort to using the user-settable CLOCK_RESOLUTION definition to get the right speed
  2. The system is POSIX-compliant. unistd.h gives us sysconf(), which tells us the clock rate.
] SideEffects [ none ] ******************************************************************************/ long util_cpu_time(void) { long t = 0; #if HAVE_SYSCONF == 1 /* Code for POSIX systems */ struct tms buffer; long nticks; /* number of clock ticks per second */ nticks = sysconf(_SC_CLK_TCK); times(&buffer); t = (long) (buffer.tms_utime * (1000.0/nticks)); #else # ifndef vms /* Code for non-POSIX systems */ struct tms buffer; time(&buffer); t = buffer.tms_utime * 1000.0 / CLOCK_RESOLUTION; # else /* Code for VMS (?) */ struct {int p1, p2, p3, p4;} buffer; static long ref_time; times(&buffer); t = buffer.p1 * 10; if (ref_time == 0) ref_time = t; t = t - ref_time; # endif /* vms */ #endif /* _POSIX_VERSION */ return t; } /**Function******************************************************************** Synopsis [ Return elapsed time in milliseconds. It includes waited- for terminated children. ] Description [ Return a long which represents the elapsed time in milliseconds since some constant reference. This time includes the CPU time spent executing instructions of the calling process and the time this process waited for its children to be terminated

There are two possibilities:

  1. The system is non-POSIX compliant, so unistd.h and hence sysconf() can't tell us the clock tick speed. At this point, we have to resort to using the user-settable CLOCK_RESOLUTION definition to get the right speed
  2. The system is POSIX-compliant. unistd.h gives us sysconf(), which tells us the clock rate.
] SideEffects [ none ] ******************************************************************************/ long util_cpu_ctime(void) { long t = 0; #if HAVE_SYSCONF == 1 /* Code for POSIX systems */ struct tms buffer; long nticks; /* number of clock ticks per second */ nticks = sysconf(_SC_CLK_TCK); times(&buffer); t = (long) ((buffer.tms_utime + buffer.tms_cutime) * (1000.0/nticks)); #else # ifndef vms /* Code for non-POSIX systems */ struct tms buffer; time(&buffer); t = (buffer.tms_utime + buffer.tms_cutime) * 1000.0 / CLOCK_RESOLUTION; # else /* Code for VMS (?) */ struct {int p1, p2, p3, p4;} buffer; static long ref_time; times(&buffer); t = (buffer.p1 + buffer.p3) * 10; if (ref_time == 0) ref_time = t; t = t - ref_time; # endif /* vms */ #endif /* _POSIX_VERSION */ return t; }