Annotation of embedaddon/lrzsz/src/timing.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:   timing.c - Timing routines for computing elapsed wall time
                      3:   Copyright (C) 1994 Michael D. Black
                      4:   Copyright (C) 1996, 1997 Uwe Ohse
                      5: 
                      6:   This program is free software; you can redistribute it and/or modify
                      7:   it under the terms of the GNU General Public License as published by
                      8:   the Free Software Foundation; either version 2, or (at your option)
                      9:   any later version.
                     10: 
                     11:   This program is distributed in the hope that it will be useful,
                     12:   but WITHOUT ANY WARRANTY; without even the implied warranty of
                     13:   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     14:   GNU General Public License for more details.
                     15: 
                     16:   You should have received a copy of the GNU General Public License
                     17:   along with this program; if not, write to the Free Software
                     18:   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
                     19:   02111-1307, USA.
                     20: 
                     21:   originally written by Michael D. Black, mblack@csihq.com
                     22: */
                     23: 
                     24: #include "zglobal.h"
                     25: 
                     26: #include "timing.h"
                     27: 
                     28: #if HAVE_SYS_PARAM_H
                     29: #include <sys/param.h>
                     30: #endif
                     31: 
                     32: #if !defined(TIME_WITH_SYS_TIME) && !defined(HAVE_SYS_TIME_H)
                     33:    /* can't use gettimeofday without struct timeval */
                     34: #  undef HAVE_GETTIMEOFDAY
                     35: #endif
                     36: 
                     37: /* Prefer gettimeofday to ftime to times.  */
                     38: #if defined(HAVE_GETTIMEOFDAY)
                     39: #  undef HAVE_FTIME
                     40: #  undef HAVE_TIMES
                     41: #else
                     42: #  if defined(HAVE_FTIME)
                     43: #    undef HAVE_TIMES
                     44: #  endif
                     45: #endif
                     46: 
                     47: #ifdef HAVE_FTIME
                     48: #  include <sys/timeb.h>
                     49: #endif
                     50: 
                     51: #ifdef HAVE_TIMES
                     52: #  if HAVE_SYS_TIMES_H
                     53: #    include <sys/times.h>
                     54: #  endif
                     55: #  ifdef _SC_CLK_TCK
                     56: #    define HAVE_SC_CLK_TCK 1
                     57: #  else
                     58: #    define HAVE_SC_CLK_TCK 0
                     59: #  endif
                     60: /* TIMES_TICK may have been set in policy.h, or we may be able to get
                     61:    it using sysconf.  If neither is the case, try to find a useful
                     62:    definition from the system header files.  */
                     63: #  if !defined(TIMES_TICK) && (!defined(HAVE_SYSCONF) || !defined(HAVE_SC_CLK_TCK))
                     64: #    ifdef CLK_TCK
                     65: #      define TIMES_TICK CLK_TCK
                     66: #    else /* ! defined (CLK_TCK) */
                     67: #      ifdef HZ
                     68: #        define TIMES_TICK HZ
                     69: #      endif /* defined (HZ) */
                     70: #    endif /* ! defined (CLK_TCK) */
                     71: #else
                     72: #  endif /* TIMES_TICK == 0 && (! HAVE_SYSCONF || ! HAVE_SC_CLK_TCK) */
                     73: #  ifndef TIMES_TICK
                     74: #    define TIMES_TICK 0
                     75: #  endif
                     76: #endif /* HAVE_TIMES */
                     77: 
                     78: #ifdef HAVE_GETTIMEOFDAY
                     79: /* collides with Solaris 2.5 prototype? */
                     80: /* int gettimeofday (struct timeval *tv, struct timezone *tz); */
                     81: #endif
                     82: 
                     83: double 
                     84: timing (int reset, time_t *nowp)
                     85: {
                     86:   static double elaptime, starttime, stoptime;
                     87:   double yet;
                     88: #define NEED_TIME
                     89: #ifdef HAVE_GETTIMEOFDAY
                     90:   struct timeval tv;
                     91:   struct timezone tz;
                     92: 
                     93: #ifdef DST_NONE
                     94:   tz.tz_dsttime = DST_NONE;
                     95: #else
                     96:   tz.tz_dsttime = 0;
                     97: #endif
                     98:   gettimeofday (&tv, &tz);
                     99:   yet=tv.tv_sec + tv.tv_usec/1000000.0;
                    100: #undef NEED_TIME
                    101: #endif
                    102: #ifdef HAVE_FTIME
                    103:        static int fbad=0;
                    104: 
                    105:        if (! fbad)
                    106:        {
                    107:                struct timeb stime;
                    108:                static struct timeb slast;
                    109: 
                    110:                (void) ftime (&stime);
                    111: 
                    112:                /* On some systems, such as SCO 3.2.2, ftime can go backwards in
                    113:                   time.  If we detect this, we switch to using time.  */
                    114:                if (slast.time != 0
                    115:                        && (stime.time < slast.time
                    116:                        || (stime.time == slast.time && stime.millitm < slast.millitm)))
                    117:                        fbad = 1;
                    118:                else
                    119:                {
                    120:                        yet = stime.millitm / 1000.0  + stime.time;
                    121:                        slast = stime;
                    122:                }
                    123:        }
                    124:        if (fbad)
                    125:                yet=(double) time(NULL);
                    126: #undef NEED_TIME
                    127: #endif
                    128: 
                    129: #ifdef HAVE_TIMES
                    130:   struct tms s;
                    131:   long i;
                    132:   static int itick;
                    133: 
                    134:   if (itick == 0)
                    135:     {
                    136: #if TIMES_TICK == 0
                    137: #if HAVE_SYSCONF && HAVE_SC_CLK_TCK
                    138:       itick = (int) sysconf (_SC_CLK_TCK);
                    139: #else /* ! HAVE_SYSCONF || ! HAVE_SC_CLK_TCK */
                    140:       const char *z;
                    141: 
                    142:       z = getenv ("HZ");
                    143:       if (z != NULL)
                    144:         itick = (int) strtol (z, (char **) NULL, 10);
                    145: 
                    146:       /* If we really couldn't get anything, just use 60.  */
                    147:       if (itick == 0)
                    148:         itick = 60;
                    149: #endif /* ! HAVE_SYSCONF || ! HAVE_SC_CLK_TCK */
                    150: #else /* TIMES_TICK != 0 */
                    151:       itick = TIMES_TICK;
                    152: #endif /* TIMES_TICK == 0 */
                    153:     }
                    154:   yet = ((double) times (&s)) / itick;
                    155: #undef NEED_TIME
                    156: #endif
                    157: 
                    158: #ifdef NEED_TIME
                    159:        yet=(double) time(NULL);
                    160: #endif
                    161:   if (nowp)
                    162:     *nowp=(time_t) yet;
                    163:   if (reset) {
                    164:     starttime = yet;
                    165:     return starttime;
                    166:   }
                    167:   else {
                    168:     stoptime = yet;
                    169:     elaptime = stoptime - starttime;
                    170:     return elaptime;
                    171:   }
                    172: }
                    173: 
                    174: /*#define TEST*/
                    175: #ifdef TEST
                    176: main()
                    177: {
                    178:        int i;
                    179:        printf("timing %g\n",timing(1));
                    180:        printf("timing %g\n",timing(0));
                    181:        for(i=0;i<20;i++){
                    182:        sleep(1);
                    183:        printf("timing %g\n",timing(0));
                    184:        }
                    185: }
                    186: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>