timerunix.c

Go to the documentation of this file.
00001 /*                     T I M E R U N I X . 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 /** @file timerunix.c
00025  * To provide timing information for RT.
00026  *      This version for any non-BSD UNIX system, including
00027  *      System III, Vr1, Vr2.
00028  *      Version 6 & 7 should also be able to use this (untested).
00029  *      The time() and times() sys-calls are used for all timing.
00030  *
00031  *  Author -
00032  *      Michael John Muuss
00033  *
00034  *  Source -
00035  *      SECAD/VLD Computing Consortium, Bldg 394
00036  *      The U. S. Army Ballistic Research Laboratory
00037  *      Aberdeen Proving Ground, Maryland  21005
00038  *
00039  */
00040 
00041 #ifndef lint
00042 static const char RCStimer[] = "@(#)$Header: /cvsroot/brlcad/brlcad/src/librt/timerunix.c,v 14.10 2006/09/16 02:04:26 lbutler Exp $ (BRL)";
00043 #endif
00044 
00045 #include "common.h"
00046 
00047 
00048 
00049 #include <stdio.h>
00050 #ifdef HAVE_MEMORY_H
00051 #include <memory.h>
00052 #endif
00053 #include <sys/types.h>
00054 #include <sys/times.h>
00055 #include <sys/param.h>
00056 
00057 #ifdef HAVE_SYS_MACHD_H
00058 # include <sys/machd.h>
00059 #endif
00060 
00061 #ifndef HZ
00062         /* It's not always in sys/param.h;  if not, guess */
00063 #       define  HZ              60
00064 #       define  DEFAULT_HZ      yes
00065 #endif
00066 
00067 #include "machine.h"
00068 #include "bu.h"
00069 
00070 /* Standard System V stuff */
00071 static long time0;
00072 static struct tms tms0;
00073 
00074 /*
00075  *                      R T _ P R E P _ T I M E R
00076  */
00077 void
00078 rt_prep_timer(void)
00079 {
00080         (void)time(&time0);
00081         (void)times(&tms0);
00082 }
00083 
00084 /*
00085  *                      R T _ G E T _ T I M E R
00086  *
00087  *  Reports on the passage of time, since rt_prep_timer() was called.
00088  *  Explicit return is number of CPU seconds.
00089  *  String return is descriptive.
00090  *  If "elapsed" pointer is non-null, number of elapsed seconds are returned.
00091  *  Times returned will never be zero.
00092  */
00093 double
00094 rt_get_timer(struct bu_vls *vp, double *elapsed)
00095 {
00096         long    now;
00097         double  user_cpu_secs;
00098         double  sys_cpu_secs;
00099         double  elapsed_secs;
00100         double  percent;
00101         struct tms tmsnow;
00102 
00103         /* Real time.  1 second resolution. */
00104         (void)time(&now);
00105         elapsed_secs = now-time0;
00106 
00107         /* CPU time */
00108         (void)times(&tmsnow);
00109         user_cpu_secs = (tmsnow.tms_utime + tmsnow.tms_cutime) -
00110                 (tms0.tms_utime + tms0.tms_cutime );
00111         user_cpu_secs /= HZ;
00112         sys_cpu_secs = (tmsnow.tms_stime + tmsnow.tms_cstime) -
00113                 (tms0.tms_stime + tms0.tms_cstime );
00114         sys_cpu_secs /= HZ;
00115         if( user_cpu_secs < 0.00001 )  user_cpu_secs = 0.00001;
00116         if( elapsed_secs < 0.00001 )  elapsed_secs = user_cpu_secs;     /* It can't be any less! */
00117 
00118         if( elapsed )  *elapsed = elapsed_secs;
00119 
00120         if( vp )  {
00121                 percent = user_cpu_secs/elapsed_secs*100.0;
00122                 BU_CK_VLS(vp);
00123 #ifdef DEFAULT_HZ
00124                 bu_vls_printf( vp,
00125 "%g user + %g sys in %g elapsed secs (%g%%) WARNING: HZ=60 assumed, fix librt/timerunix.c",
00126                         user_cpu_secs, sys_cpu_secs, elapsed_secs, percent );
00127 #else
00128                 bu_vls_printf( vp,
00129                         "%g user + %g sys in %g elapsed secs (%g%%)",
00130                         user_cpu_secs, sys_cpu_secs, elapsed_secs, percent );
00131 #endif
00132         }
00133         return( user_cpu_secs );
00134 }
00135 
00136 /*
00137  *                      R T _ R E A D _ T I M E R
00138  *
00139  *  Compatability routine
00140  */
00141 double
00142 rt_read_timer(char *str, int len)
00143 {
00144         struct bu_vls   vls;
00145         double          cpu;
00146         int             todo;
00147 
00148         if( !str )  return  rt_get_timer( (struct bu_vls *)0, (double *)0 );
00149 
00150         bu_vls_init( &vls );
00151         cpu = rt_get_timer( &vls, (double *)0 );
00152         todo = bu_vls_strlen( &vls );
00153         if( todo > len )  todo = len-1;
00154         strncpy( str, bu_vls_addr(&vls), todo );
00155         str[todo] = '\0';
00156         return cpu;
00157 }
00158 
00159 /*@}*/
00160 /*
00161  * Local Variables:
00162  * mode: C
00163  * tab-width: 8
00164  * c-basic-offset: 4
00165  * indent-tabs-mode: t
00166  * End:
00167  * ex: shiftwidth=4 tabstop=8
00168  */

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