home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume21 / cloops / part01 / secs.c < prev    next >
C/C++ Source or Header  |  1991-07-25  |  4KB  |  157 lines

  1. /*
  2.  * This file is part of the Livermore Loops transliteration into C.
  3.  * Copyright (C) 1991 by Martin Fouts
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 1, or (at your option)
  8.  * any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. #include <stdio.h>
  21. #define Float double
  22.  
  23. #ifdef BSD_TIME
  24. #include <sys/types.h>
  25. #include <sys/times.h>
  26. #include <sys/time.h>
  27. #define HZ 60
  28. #else
  29. #ifdef CONVEX_TIME
  30. #include <sys/types.h>
  31. #include <sys/times.h>
  32. #include <sys/time.h>
  33. #include <sys/resource.h>
  34. #define HZ 60
  35. #else
  36. #include <sys/types.h>        /* To avoid errors in param.h */
  37. #include <sys/param.h>        /* To obtain the local value for HZ */
  38. #include <sys/times.h>
  39. #endif
  40. #endif
  41.  
  42. #ifdef ETA_BUG
  43. float Hz;
  44. static int hz = 100;
  45. #endif
  46.  
  47. #define MICRO 1000000.0        /* number of microseconds in a second */
  48.  
  49. Float second(oldsec)
  50. Float oldsec;
  51. {
  52.   float mytime, cptime();
  53.   mytime = cptime();
  54.   return((Float)mytime-oldsec);
  55. }
  56.  
  57. Float second_(oldsec)
  58. Float *oldsec;
  59. {
  60.   return second(*oldsec);
  61. }
  62.  
  63. float cptime()
  64. {
  65. #ifdef CONVEX_TIME
  66.   struct rusage rusage;
  67.   float temp1, temp2;
  68.   getrusage(RUSAGE_SELF,&rusage);
  69.   temp1 = (float) rusage.ru_exutime.tv_sec;
  70.   temp2 = (float) rusage.ru_exutime.tv_usec / MICRO;
  71.   return(temp1+temp2);
  72.  
  73. #else
  74.  
  75.   long times();
  76.   struct tms tms;
  77.   float t;
  78.   static oldt;
  79.  
  80.   (void) times(&tms);
  81. #ifdef ETA_BUG
  82.   Hz = hz;
  83.   t = (float) tms.tms_utime / Hz;
  84. #else
  85.   t = (float)(tms.tms_utime /* + tms.tms_stime*/)/(float)HZ;
  86. #endif
  87.   if (t < oldt) {
  88.     printf("cptime, new time (%f) < old!(%f)\n", t, oldt);
  89.     t = oldt;
  90.   }
  91.   oldt = t;
  92.   return(t);
  93. #endif
  94.  
  95. }
  96.  
  97. float secs()            /* returns the number of seconds which have
  98.                  * expired since secs() was last called, to
  99.                  * the highest granularity available on the
  100.                  * system:
  101.                  * SVR2: (vax) 1/60 second
  102.                  * 4.2 BSD: (vax) 1/100 second
  103.                  * Cray 2 1/250 millionth second
  104.                  */
  105. {
  106. #ifdef BSD_TIME            /* There are really two versions, one for
  107.                  * BSD and one for SVR2.  If you have a
  108.                  * different way of getting time, add
  109.                  * another.
  110.                  */
  111.     static struct timeval tpbase;/* time at which secs() first called */
  112.     struct timeval tp;        /* time at current call */
  113.     struct timezone tzp;    /* ignored */
  114.     float temp, temp1, temp2;    /* To simplify the code */
  115.     (void)gettimeofday(&tp,&tzp);    /* find out what time it is */
  116.  
  117.     if (tpbase.tv_sec == 0) {    /* first time, set the base */
  118.     tpbase.tv_sec = tp.tv_sec;
  119.     tpbase.tv_usec = tp.tv_usec;
  120.     }
  121.     /* calculate the time since last called, as floating seconds */
  122.     temp1 = (float) (tp.tv_sec - tpbase.tv_sec);
  123.     temp2 = (float) (tp.tv_usec - tpbase.tv_usec) / MICRO ;
  124.     temp = temp1 + temp2;
  125.     tpbase.tv_sec = tp.tv_sec;
  126.     tpbase.tv_usec = tp.tv_usec;             
  127.     return(temp);
  128.  
  129. #else
  130. #ifdef CRAY_TIME
  131.    static long rtticks;
  132.    long rtclock();
  133.    long rtnow = rtclock();
  134.    float rettime = (float)(rtnow-rtticks) / (float) HZ;
  135.    rtticks = rtnow;
  136.    return(rettime);
  137. #else
  138.     struct tms tms;        /* dummy (as in not used) */
  139.     static long nowtim = 0;    /* time at which secs() first called */
  140.     long thistim;        /* current time (in tics) */
  141.     long times();        /* returns ticks (ticks/HZ = seconds) */
  142.     float rettime;        /* Time to return */
  143.     thistim = times(&tms);    /* get the time */
  144.     if (nowtim == 0) nowtim = thistim;
  145. #ifdef ETA_BUG
  146.     Hz = hz;
  147.     rettime = (float)(thistim-nowtim)/Hz;
  148. #else
  149.     rettime = (float)(thistim-nowtim)/(float)HZ;
  150. #endif ETA_BUG
  151.     nowtim = thistim;        /* first call */
  152.     return(rettime); /* return, converted to secs */
  153. #endif CRAY_TIME
  154. #endif BSD_TIME
  155. }
  156.  
  157.