home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Source / GNU / uucp / Uucp.framework / unix.subproj / proctm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-09  |  4.7 KB  |  198 lines

  1. /* proctm.c
  2.    Get the time spent in the process.
  3.  
  4.    Copyright (C) 1992, 1993 Ian Lance Taylor
  5.  
  6.    This file is part of the Taylor UUCP package.
  7.  
  8.    This program is free software; you can redistribute it and/or
  9.    modify it under the terms of the GNU General Public License as
  10.    published by the Free Software Foundation; either version 2 of the
  11.    License, or (at your option) any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful, but
  14.    WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22.    The author of the program may be contacted at ian@airs.com or
  23.    c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
  24.    */
  25.  
  26. #include "uucp.h"
  27.  
  28. #include "sysdep.h"
  29. #include "system.h"
  30.  
  31. #if HAVE_SYS_PARAM_H
  32. #include <sys/param.h>
  33. #endif
  34.  
  35. #if HAVE_LIMITS_H
  36. #include <limits.h>
  37. #endif
  38.  
  39. /* Prefer gettimeofday to ftime to times.  */
  40.  
  41. #if HAVE_GETTIMEOFDAY || HAVE_FTIME
  42. #undef HAVE_TIMES
  43. #define HAVE_TIMES 0
  44. #endif
  45.  
  46. #if HAVE_GETTIMEOFDAY
  47. #undef HAVE_FTIME
  48. #define HAVE_FTIME 0
  49. #endif
  50.  
  51. #if HAVE_TIME_H && (TIME_WITH_SYS_TIME || ! HAVE_GETTIMEOFDAY)
  52. #include <time.h>
  53. #endif
  54.  
  55. #if HAVE_GETTIMEOFDAY
  56. #include <sys/time.h>
  57. #endif
  58.  
  59. #if HAVE_FTIME
  60. #include <sys/timeb.h>
  61. #endif
  62.  
  63. #if HAVE_TIMES
  64.  
  65. #if HAVE_SYS_TIMES_H
  66. #include <sys/times.h>
  67. #endif
  68.  
  69. #if TIMES_DECLARATION_OK
  70. /* We use a macro to protect this because times really returns clock_t
  71.    and on some systems, such as Ultrix 4.0, clock_t is int.  We don't
  72.    leave it out entirely because on some systems, such as System III,
  73.    the declaration is necessary for correct compilation.  */
  74. #ifndef times
  75. extern long times ();
  76. #endif
  77. #endif /* TIMES_DECLARATION_OK */
  78.  
  79. #ifdef _SC_CLK_TCK
  80. #define HAVE_SC_CLK_TCK 1
  81. #else
  82. #define HAVE_SC_CLK_TCK 0
  83. #endif
  84.  
  85. /* TIMES_TICK may have been set in policy.h, or we may be able to get
  86.    it using sysconf.  If neither is the case, try to find a useful
  87.    definition from the system header files.  */
  88. #if TIMES_TICK == 0 && (! HAVE_SYSCONF || ! HAVE_SC_CLK_TCK)
  89. #ifdef CLK_TCK
  90. #undef TIMES_TICK
  91. #define TIMES_TICK CLK_TCK
  92. #else /* ! defined (CLK_TCK) */
  93. #ifdef HZ
  94. #undef TIMES_TICK
  95. #define TIMES_TICK HZ
  96. #endif /* defined (HZ) */
  97. #endif /* ! defined (CLK_TCK) */
  98. #endif /* TIMES_TICK == 0 && (! HAVE_SYSCONF || ! HAVE_SC_CLK_TCK) */
  99.  
  100. #endif /* HAVE_TIMES */
  101.  
  102. #ifndef time
  103. extern time_t time ();
  104. #endif
  105. #if HAVE_SYSCONF
  106. #ifndef sysconf
  107. extern long sysconf ();
  108. #endif
  109. #endif
  110.  
  111. /* Get the time in seconds and microseconds; this need only work
  112.    within the process when called from the system independent code.
  113.    It is also called by ixsysdep_time.  */
  114.  
  115. long
  116. ixsysdep_process_time (pimicros)
  117.      long *pimicros;
  118. {
  119. #if HAVE_GETTIMEOFDAY
  120.   struct timeval stime;
  121.   struct timezone stz;
  122.  
  123.   (void) gettimeofday (&stime, &stz);
  124.   if (pimicros != NULL)
  125.     *pimicros = (long) stime.tv_usec;
  126.   return (long) stime.tv_sec;
  127. #endif /* HAVE_GETTIMEOFDAY */
  128.  
  129. #if HAVE_FTIME
  130.   static boolean fbad;
  131.  
  132.   if (! fbad)
  133.     {
  134.       struct timeb stime;
  135.       static struct timeb slast;
  136.  
  137.       (void) ftime (&stime);
  138.  
  139.       /* On some systems, such as SCO 3.2.2, ftime can go backwards in
  140.      time.  If we detect this, we switch to using time.  */
  141.       if (slast.time != 0
  142.       && (stime.time < slast.time
  143.           || (stime.time == slast.time &&
  144.           stime.millitm < slast.millitm)))
  145.     fbad = TRUE;
  146.       else
  147.     {
  148.       slast = stime;
  149.       if (pimicros != NULL)
  150.         *pimicros = (long) stime.millitm * (long) 1000;
  151.       return (long) stime.time;
  152.     }
  153.     }
  154.  
  155.   if (pimicros != NULL)
  156.     *pimicros = 0;
  157.   return (long) time ((time_t *) NULL);
  158. #endif /* HAVE_FTIME */
  159.  
  160. #if HAVE_TIMES
  161.   struct tms s;
  162.   long i;
  163.   static int itick;
  164.  
  165.   if (itick == 0)
  166.     {
  167. #if TIMES_TICK == 0
  168. #if HAVE_SYSCONF && HAVE_SC_CLK_TCK
  169.       itick = (int) sysconf (_SC_CLK_TCK);
  170. #else /* ! HAVE_SYSCONF || ! HAVE_SC_CLK_TCK */
  171.       const char *z;
  172.  
  173.       z = getenv ("HZ");
  174.       if (z != NULL)
  175.     itick = (int) strtol (z, (char **) NULL, 10);
  176.  
  177.       /* If we really couldn't get anything, just use 60.  */
  178.       if (itick == 0)
  179.     itick = 60;
  180. #endif /* ! HAVE_SYSCONF || ! HAVE_SC_CLK_TCK */
  181. #else /* TIMES_TICK != 0 */
  182.       itick = TIMES_TICK;
  183. #endif /* TIMES_TICK == 0 */
  184.     }
  185.  
  186.   i = (long) times (&s);
  187.   if (pimicros != NULL)
  188.     *pimicros = (i % (long) itick) * ((long) 1000000 / (long) itick);
  189.   return i / (long) itick;
  190. #endif /* HAVE_TIMES */
  191.  
  192. #if ! HAVE_GETTIMEOFDAY && ! HAVE_FTIME && ! HAVE_TIMES
  193.   if (pimicros != NULL)
  194.     *pimicros = 0;
  195.   return (long) time ((time_t *) NULL);
  196. #endif /* ! HAVE_GETTIMEOFDAY && ! HAVE_FTIME && ! HAVE_TIMES  */
  197. }
  198.