1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
/* timer.c -- Timer support
* Created: Sat Oct 7 13:05:31 1995 by faith@cs.unc.edu
* Revised: Sun Jul 5 17:34:48 1998 by faith@acm.org
* Copyright 1995, 1996, 1997, 1998 Rickard E. Faith (faith@acm.org)
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Library General Public License as published
* by the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*
* $Id: timer.c,v 1.17 1998/07/05 22:10:12 faith Exp $
*
* \section{Timer Support}
*
* \intro These routines provide access to a microsecond-resolution time
* that can be used for profiling.
*
*/
#include "maaP.h"
static hsh_HashTable _tim_Hash;
typedef struct tim_Entry {
double real; /* wall time in sec */
double self_user; /* user time in sec */
double self_system; /* system time in sec */
double children_user; /* user time in sec */
double children_system; /* system time in sec */
struct timeval real_mark;
struct rusage self_mark;
struct rusage children_mark;
} *tim_Entry;
static void _tim_check( void )
{
if (!_tim_Hash) _tim_Hash = hsh_create( NULL, NULL );
}
/* \doc Start the named timer. */
void tim_start( const char *name )
{
tim_Entry entry;
_tim_check();
if (!(entry = (tim_Entry)hsh_retrieve( _tim_Hash, name ))) {
entry = xmalloc( sizeof( struct tim_Entry ) );
entry->real = 0.0;
entry->self_user = 0.0;
entry->self_system = 0.0;
entry->children_user = 0.0;
entry->children_system = 0.0;
hsh_insert( _tim_Hash, name, entry );
}
#if HAVE_GETTIMEOFDAY
gettimeofday( &entry->real_mark, NULL );
#else
time( &entry->real_mark.tv_sec );
entry->real_mark.tv_usec = 0;
#endif
getrusage( RUSAGE_SELF, &entry->self_mark );
getrusage( RUSAGE_CHILDREN, &entry->children_mark );
}
/* \doc Stop the named timer. */
void tim_stop( const char *name )
{
tim_Entry entry;
struct timeval real;
struct rusage rusage;
#define DIFFTIME(now,then)\
(((now).tv_sec - (then).tv_sec) \
+ ((now).tv_usec - (then).tv_usec)/1000)
_tim_check();
#if HAVE_GETTIMEOFDAY
gettimeofday( &real, NULL );
#else
time( &real.tv_sec );
real.tv_usec = 0;
#endif
if (!(entry = (tim_Entry)hsh_retrieve( _tim_Hash, name ) ))
err_internal ( __FUNCTION__, "No timer: %s\n", name );
entry->real = DIFFTIME( real, entry->real_mark );
getrusage( RUSAGE_SELF, &rusage );
entry->self_user = DIFFTIME( rusage.ru_utime, entry->self_mark.ru_utime );
entry->self_system = DIFFTIME( rusage.ru_stime, entry->self_mark.ru_stime );
getrusage( RUSAGE_CHILDREN, &rusage );
entry->children_user
= DIFFTIME( rusage.ru_utime, entry->children_mark.ru_utime );
entry->children_system
= DIFFTIME( rusage.ru_stime, entry->children_mark.ru_stime );
}
/* \doc Reset the named timer to zero elapsed time. Use |tim_start| to reset
the start time. */
void tim_reset( const char *name )
{
tim_Entry entry;
_tim_check();
if (!(entry = (tim_Entry)hsh_retrieve( _tim_Hash, name ) ))
err_internal ( __FUNCTION__, "No timer: %s\n", name );
entry->real = 0.0;
entry->self_user = 0.0;
entry->self_system = 0.0;
entry->children_user = 0.0;
entry->children_system = 0.0;
}
/* \doc Get the wall time in seconds from the named timer. The return
value is a |double| and has microsecond resolution if the current system
provides that accuracy (most don't). */
double tim_get_real( const char *name )
{
tim_Entry entry;
_tim_check();
if (!(entry = (tim_Entry)hsh_retrieve( _tim_Hash, name ) ))
err_internal ( __FUNCTION__, "No timer: %s\n", name );
return entry->real;
}
/* \doc Get the number of seconds of user CPU time. */
double tim_get_user( const char *name )
{
tim_Entry entry;
_tim_check();
if (!(entry = (tim_Entry)hsh_retrieve( _tim_Hash, name ) ))
err_internal ( __FUNCTION__, "No timer: %s\n", name );
#if 0
printf( "self: maxrss %ld ixrss %ld idrss %ld isrss %ld minflt %ld"
" majflt %ld nswap %ld inblock %ld outblock %ld msgsnd %ld"
" msgrcv %ld nsignals %ld nvcwm %ld nivcsm %ld\n",
entry->self_mark.ru_maxrss,
entry->self_mark.ru_ixrss,
entry->self_mark.ru_idrss,
entry->self_mark.ru_isrss,
entry->self_mark.ru_minflt,
entry->self_mark.ru_majflt,
entry->self_mark.ru_nswap,
entry->self_mark.ru_inblock,
entry->self_mark.ru_oublock,
entry->self_mark.ru_msgsnd,
entry->self_mark.ru_msgrcv,
entry->self_mark.ru_nsignals,
entry->self_mark.ru_nvcsw,
entry->self_mark.ru_nivcsw );
printf( "chld: maxrss %ld ixrss %ld idrss %ld isrss %ld minflt %ld"
" majflt %ld nswap %ld inblock %ld outblock %ld msgsnd %ld"
" msgrcv %ld nsignals %ld nvcwm %ld nivcsm %ld\n",
entry->children_mark.ru_maxrss,
entry->children_mark.ru_ixrss,
entry->children_mark.ru_idrss,
entry->children_mark.ru_isrss,
entry->children_mark.ru_minflt,
entry->children_mark.ru_majflt,
entry->children_mark.ru_nswap,
entry->children_mark.ru_inblock,
entry->children_mark.ru_oublock,
entry->children_mark.ru_msgsnd,
entry->children_mark.ru_msgrcv,
entry->children_mark.ru_nsignals,
entry->children_mark.ru_nvcsw,
entry->children_mark.ru_nivcsw );
#endif
return (entry->self_user + entry->children_user);
}
/* \doc Get the number of seconds of system CPU time. */
double tim_get_system( const char *name )
{
tim_Entry entry;
_tim_check();
if (!(entry = (tim_Entry)hsh_retrieve( _tim_Hash, name ) ))
err_internal ( __FUNCTION__, "No timer: %s\n", name );
return (entry->self_system + entry->children_system);
}
/* \doc Print the named timer values to |str|. The format is similar to
"time(1)". */
void tim_print_timer( FILE *str, const char *name )
{
fprintf( str, "%-20s %0.3fr %0.3fu %0.3fs\n",
name,
tim_get_real( name ),
tim_get_user( name ),
tim_get_system( name ) );
}
static int _tim_iterator( const void *key, const void *datum, void *arg )
{
FILE *str = (FILE *)arg;
tim_print_timer( str, key );
return 0;
}
/* \doc Print all the timers to |str|. The order is arbitary. */
void tim_print_timers( FILE *str )
{
if (_tim_Hash) hsh_iterate_arg( _tim_Hash, _tim_iterator, str );
}
static int _tim_freer( const void *key, const void *datum )
{
xfree( (void *)datum ); /* Discard const */
return 0;
}
/* \doc Free all memory associated with the timers. This function is
called automatically at program termination. There should never be a
need to call this function in user-level code. */
void _tim_shutdown( void )
{
if (_tim_Hash) {
hsh_iterate( _tim_Hash, _tim_freer );
hsh_destroy( _tim_Hash );
}
_tim_Hash = NULL;
}
|