timer52brl.c

Go to the documentation of this file.
00001 /*                    T I M E R 5 2 B R L . C
00002  * BRL-CAD
00003  *
00004  * Copyright (c) 1985-2006 United States Government as represented by
00005  * the U.S. Army Research Laboratory.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License
00009  * as published by the Free Software Foundation; either version 2 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this file; see the file named COPYING for more
00019  * information.
00020  */
00021 
00022 /** @addtogroup timer */
00023 
00024 /*@{*/
00025 
00026 /** @file timer52brl.c
00027  * To provide timing information for RT.
00028  *      This version for System V, Release TWO, under 4.2 BSD,
00029  *      using Doug Gwyn's System-V-under-4.2 emulation.
00030  *
00031  *  Source -
00032  *      SECAD/VLD Computing Consortium, Bldg 394
00033  *      The U. S. Army Ballistic Research Laboratory
00034  *      Aberdeen Proving Ground, Maryland  21005
00035  *
00036  */
00037 
00038 #ifndef lint
00039 static const char RCStimer[] = "@(#)$Header: /cvsroot/brlcad/brlcad/src/librt/timer52brl.c,v 14.10 2006/09/16 02:04:26 lbutler Exp $ (BRL)";
00040 #endif
00041 
00042 #include <stdio.h>
00043 #include <memory.h>
00044 
00045 /* BRL's System V - under 4.2 BSD version */
00046 
00047 #include "/sys/h/types.h"
00048 #include "/sys/h/time.h"
00049 #include "/sys/h/resource.h"
00050 
00051 static struct   timeval time0;  /* Time at which timeing started */
00052 static struct   rusage ru0;     /* Resource utilization at the start */
00053 
00054 static void prusage();
00055 static void tvadd();
00056 
00057 /*
00058  *                      P R E P _ T I M E R
00059  */
00060 void
00061 rt_prep_timer()
00062 {
00063         _gettimeofday(&time0, (struct timezone *)0);
00064         _getrusage(RUSAGE_SELF, &ru0);
00065 }
00066 
00067 static void
00068 tvsub(tdiff, t1, t0)
00069         struct timeval *tdiff, *t1, *t0;
00070 {
00071 
00072         tdiff->tv_sec = t1->tv_sec - t0->tv_sec;
00073         tdiff->tv_usec = t1->tv_usec - t0->tv_usec;
00074         if (tdiff->tv_usec < 0)
00075                 tdiff->tv_sec--, tdiff->tv_usec += 1000000;
00076 }
00077 
00078 /*
00079  *                      R E A D _ T I M E R
00080  *
00081  */
00082 double
00083 rt_read_timer(str,len)
00084 char *str;
00085 {
00086         struct timeval timedol;
00087         struct rusage ru1;
00088         struct timeval td;
00089         double usert;
00090         char line[132];
00091 
00092         _getrusage(RUSAGE_SELF, &ru1);
00093         _gettimeofday(&timedol, (struct timezone *)0);
00094         prusage(&ru0, &ru1, &timedol, &time0, line);
00095         (void)strncpy( str, line, len );
00096         tvsub( &td, &ru1.ru_utime, &ru0.ru_utime );
00097         usert = td.tv_sec + ((double)td.tv_usec) / 1000000;
00098         if( usert < 0.00001 )  usert = 0.00001;
00099         return( usert );
00100 }
00101 
00102 static void
00103 psecs(l,cp)
00104 long l;
00105 register char *cp;
00106 {
00107         register int i;
00108 
00109         i = l / 3600;
00110         if (i) {
00111                 sprintf(cp,"%d:", i);
00112                 END(cp);
00113                 i = l % 3600;
00114                 sprintf(cp,"%d%d", (i/60) / 10, (i/60) % 10);
00115                 END(cp);
00116         } else {
00117                 i = l;
00118                 sprintf(cp,"%d", i / 60);
00119                 END(cp);
00120         }
00121         i %= 60;
00122         *cp++ = ':';
00123         sprintf(cp,"%d%d", i / 10, i % 10);
00124 }
00125 
00126 static void
00127 prusage(r0, r1, e, b, outp)
00128         register struct rusage *r0, *r1;
00129         struct timeval *e, *b;
00130         char *outp;
00131 {
00132         struct timeval tdiff;
00133         register time_t t;
00134         register char *cp;
00135         register int i;
00136         int ms;
00137 
00138         t = (r1->ru_utime.tv_sec-r0->ru_utime.tv_sec)*100+
00139             (r1->ru_utime.tv_usec-r0->ru_utime.tv_usec)/10000+
00140             (r1->ru_stime.tv_sec-r0->ru_stime.tv_sec)*100+
00141             (r1->ru_stime.tv_usec-r0->ru_stime.tv_usec)/10000;
00142         ms =  (e->tv_sec-b->tv_sec)*100 + (e->tv_usec-b->tv_usec)/10000;
00143 
00144 #define END(x)  {while(*x) x++;}
00145         cp = "%Uuser %Ssys %Ereal %P %Xi+%Dd %Mmaxrss %F+%Rpf %Ccsw";
00146         for (; *cp; cp++)  {
00147                 if (*cp != '%')
00148                         *outp++ = *cp;
00149                 else if (cp[1]) switch(*++cp) {
00150 
00151                 case 'U':
00152                         tvsub(&tdiff, &r1->ru_utime, &r0->ru_utime);
00153                         sprintf(outp,"%d.%01d", tdiff.tv_sec, tdiff.tv_usec/100000);
00154                         END(outp);
00155                         break;
00156 
00157                 case 'S':
00158                         tvsub(&tdiff, &r1->ru_stime, &r0->ru_stime);
00159                         sprintf(outp,"%d.%01d", tdiff.tv_sec, tdiff.tv_usec/100000);
00160                         END(outp);
00161                         break;
00162 
00163                 case 'E':
00164                         psecs(ms / 100, outp);
00165                         END(outp);
00166                         break;
00167 
00168                 case 'P':
00169                         sprintf(outp,"%d%%", (int) (t*100 / ((ms ? ms : 1))));
00170                         END(outp);
00171                         break;
00172 
00173                 case 'W':
00174                         i = r1->ru_nswap - r0->ru_nswap;
00175                         sprintf(outp,"%d", i);
00176                         END(outp);
00177                         break;
00178 
00179                 case 'X':
00180                         sprintf(outp,"%d", t == 0 ? 0 : (r1->ru_ixrss-r0->ru_ixrss)/t);
00181                         END(outp);
00182                         break;
00183 
00184                 case 'D':
00185                         sprintf(outp,"%d", t == 0 ? 0 :
00186                             (r1->ru_idrss+r1->ru_isrss-(r0->ru_idrss+r0->ru_isrss))/t);
00187                         END(outp);
00188                         break;
00189 
00190                 case 'K':
00191                         sprintf(outp,"%d", t == 0 ? 0 :
00192                             ((r1->ru_ixrss+r1->ru_isrss+r1->ru_idrss) -
00193                             (r0->ru_ixrss+r0->ru_idrss+r0->ru_isrss))/t);
00194                         END(outp);
00195                         break;
00196 
00197                 case 'M':
00198                         sprintf(outp,"%d", r1->ru_maxrss/2);
00199                         END(outp);
00200                         break;
00201 
00202                 case 'F':
00203                         sprintf(outp,"%d", r1->ru_majflt-r0->ru_majflt);
00204                         END(outp);
00205                         break;
00206 
00207                 case 'R':
00208                         sprintf(outp,"%d", r1->ru_minflt-r0->ru_minflt);
00209                         END(outp);
00210                         break;
00211 
00212                 case 'I':
00213                         sprintf(outp,"%d", r1->ru_inblock-r0->ru_inblock);
00214                         END(outp);
00215                         break;
00216 
00217                 case 'O':
00218                         sprintf(outp,"%d", r1->ru_oublock-r0->ru_oublock);
00219                         END(outp);
00220                         break;
00221                 case 'C':
00222                         sprintf(outp,"%d+%d", r1->ru_nvcsw-r0->ru_nvcsw,
00223                                 r1->ru_nivcsw-r0->ru_nivcsw );
00224                         END(outp);
00225                         break;
00226                 }
00227         }
00228         *outp = '\0';
00229 }
00230 
00231 static void
00232 tvadd(tsum, t0)
00233         struct timeval *tsum, *t0;
00234 {
00235 
00236         tsum->tv_sec += t0->tv_sec;
00237         tsum->tv_usec += t0->tv_usec;
00238         if (tsum->tv_usec > 1000000)
00239                 tsum->tv_sec++, tsum->tv_usec -= 1000000;
00240 }
00241 
00242 
00243 /*@}*/
00244 /*
00245  * Local Variables:
00246  * mode: C
00247  * tab-width: 8
00248  * c-basic-offset: 4
00249  * indent-tabs-mode: t
00250  * End:
00251  * ex: shiftwidth=4 tabstop=8
00252  */

Generated on Mon Sep 18 01:24:57 2006 for BRL-CAD by  doxygen 1.4.6