From da905beac4579a2e3292bbdf4592a513d12a2e80 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Thu, 11 Oct 2018 17:54:35 +0200 Subject: [PATCH] timevar: expect that getrusage is available Don't keep both times and getrusage as backend: both are guaranteed by gnulib, a single one suffices. Using getrusage is open to possibly tracking other types of resources in the future. * modules/timevar (Depends-on): Add getrusage. (configure.ac): Remove gl_TIMEVAR. (Files): Remove m4/timevar.m4. * m4/timevar.m4: Remove, rely on gnulib for getrusage. * lib/timevar.h (timevar_enabled): Clarify documentation. * lib/timevar.c: Remove all the code about times. Remove all the CPP guards about getrusage: expect it to be present (courtesy of gnulib). --- ChangeLog | 15 +++++++++ lib/timevar.c | 89 +++++-------------------------------------------- lib/timevar.h | 4 ++- m4/timevar.m4 | 47 -------------------------- modules/timevar | 5 +-- 5 files changed, 28 insertions(+), 132 deletions(-) delete mode 100644 m4/timevar.m4 diff --git a/ChangeLog b/ChangeLog index 0879c07785..22695f8f44 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,18 @@ +2018-10-13 Akim Demaille + + timevar: expect that getrusage is available. + Don't keep both times and getrusage as backend: both are guaranteed by + gnulib, a single one suffices. Using getrusage is open to possibly + tracking other types of resources in the future. + * modules/timevar (Depends-on): Add getrusage. + (configure.ac): Remove gl_TIMEVAR. + (Files): Remove m4/timevar.m4. + * m4/timevar.m4: Remove, rely on gnulib for getrusage. + * lib/timevar.h (timevar_enabled): Clarify documentation. + * lib/timevar.c: Remove all the code about times. + Remove all the CPP guards about getrusage: expect it to be present + (courtesy of gnulib). + 2018-10-12 Bruno Haible mountlist: Improve support for Solaris in 64-bit mode. diff --git a/lib/timevar.c b/lib/timevar.c index 391802838b..469db9cb10 100644 --- a/lib/timevar.c +++ b/lib/timevar.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -33,50 +34,6 @@ #define _(msgid) gettext (msgid) #include "xalloc.h" -#ifdef HAVE_SYS_RESOURCE_H -# include -#endif - -/* Calculation of scale factor to convert ticks to microseconds. - We mustn't use CLOCKS_PER_SEC except with clock(). */ -#if HAVE_SYSCONF && defined _SC_CLK_TCK -# define TICKS_PER_SECOND sysconf (_SC_CLK_TCK) /* POSIX 1003.1-1996 */ -#elif defined CLK_TCK -# define TICKS_PER_SECOND CLK_TCK /* POSIX 1003.1-1988; obsolescent */ -#elif defined HZ -# define TICKS_PER_SECOND HZ /* traditional UNIX */ -#else -# define TICKS_PER_SECOND 100 /* often the correct value */ -#endif - -/* Prefer times to getrusage to clock (each gives successively less - information). */ -#if defined HAVE_TIMES -# define USE_TIMES -# define HAVE_USER_TIME -# define HAVE_SYS_TIME -# define HAVE_WALL_TIME -#elif defined HAVE_GETRUSAGE -# define USE_GETRUSAGE -# define HAVE_USER_TIME -# define HAVE_SYS_TIME -#endif - -#if defined USE_TIMES && !defined HAVE_DECL_TIMES -clock_t times (struct tms *); -#elif defined USE_GETRUSAGE && !defined HAVE_DECL_GETRUSAGE -int getrusage (int, struct rusage *); -#endif - -/* libc is very likely to have snuck a call to sysconf() into one of - the underlying constants, and that can be very slow, so we have to - precompute them. Whose wonderful idea was it to make all those - _constants_ variable at run time, anyway? */ -#ifdef USE_TIMES -static float ticks_to_msec; -# define TICKS_TO_MSEC (1.0 / TICKS_PER_SECOND) -#endif - /* See timevar.h for an explanation of timing variables. */ int timevar_enabled = 0; @@ -132,9 +89,7 @@ static struct timevar_stack_def *unused_stack_instances; element. */ static struct timevar_time_def start_time; -/* Fill the current times into TIME. The definition of this function - also defines any or all of the HAVE_USER_TIME, HAVE_SYS_TIME, and - HAVE_WALL_TIME macros. */ +/* Fill the current times into TIME. */ static void set_to_current_time (struct timevar_time_def *now) @@ -146,19 +101,13 @@ set_to_current_time (struct timevar_time_def *now) if (!timevar_enabled) return; - { -#ifdef USE_TIMES - struct tms tms; - now->wall = times (&tms) * ticks_to_msec; - now->user = (tms.tms_utime + tms.tms_cutime) * ticks_to_msec; - now->sys = (tms.tms_stime + tms.tms_cstime) * ticks_to_msec; -#elif defined USE_GETRUSAGE - struct rusage rusage; - getrusage (RUSAGE_CHILDREN, &rusage); - now->user = rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec * 1e-6; - now->sys = rusage.ru_stime.tv_sec + rusage.ru_stime.tv_usec * 1e-6; -#endif - } + struct rusage rusage; + getrusage (RUSAGE_SELF, &rusage); + now->user = rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec * 1e-6; + now->sys = rusage.ru_stime.tv_sec + rusage.ru_stime.tv_usec * 1e-6; + getrusage (RUSAGE_CHILDREN, &rusage); + now->user += rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec * 1e-6; + now->sys += rusage.ru_stime.tv_sec + rusage.ru_stime.tv_usec * 1e-6; } /* Return the current time. */ @@ -197,10 +146,6 @@ timevar_init () timevars[identifier__].name = name__; #include "timevar.def" #undef DEFTIMEVAR - -#if defined USE_TIMES - ticks_to_msec = TICKS_TO_MSEC; -#endif } void @@ -337,8 +282,6 @@ timevar_get (timevar_id_t timevar, void timevar_print (FILE *fp) { - /* Only print stuff if we have some sort of time information. */ -#if defined HAVE_USER_TIME || defined HAVE_SYS_TIME || defined HAVE_WALL_TIME if (!timevar_enabled) return; @@ -386,41 +329,27 @@ timevar_print (FILE *fp) /* The timing variable name. */ fprintf (fp, " %-22s:", tv->name); -# ifdef HAVE_USER_TIME /* Print user-mode time for this process. */ fprintf (fp, "%7.2f (%2.0f%%) usr", tv->elapsed.user, (total->user == 0 ? 0 : tv->elapsed.user / total->user) * 100); -# endif -# ifdef HAVE_SYS_TIME /* Print system-mode time for this process. */ fprintf (fp, "%7.2f (%2.0f%%) sys", tv->elapsed.sys, (total->sys == 0 ? 0 : tv->elapsed.sys / total->sys) * 100); -# endif -# ifdef HAVE_WALL_TIME /* Print wall clock time elapsed. */ fprintf (fp, "%7.2f (%2.0f%%) wall", tv->elapsed.wall, (total->wall == 0 ? 0 : tv->elapsed.wall / total->wall) * 100); -# endif putc ('\n', fp); } /* Print total time. */ fprintf (fp, " %-22s:", timevars[tv_total].name); -#ifdef HAVE_USER_TIME fprintf (fp, "%7.2f ", total->user); -#endif -#ifdef HAVE_SYS_TIME fprintf (fp, "%7.2f ", total->sys); -#endif -#ifdef HAVE_WALL_TIME fprintf (fp, "%7.2f\n", total->wall); -#endif - -#endif /* defined HAVE_USER_TIME || defined HAVE_SYS_TIME || defined HAVE_WALL_TIME */ } diff --git a/lib/timevar.h b/lib/timevar.h index 7c9258409b..ff443fed6b 100644 --- a/lib/timevar.h +++ b/lib/timevar.h @@ -124,7 +124,9 @@ void timevar_get (timevar_id_t timevar, struct timevar_time_def *elapsed); void timevar_print (FILE *fp); -/* Set to to nonzero to enable timing variables. */ +/* Set to to nonzero to enable timing variables. All the timevar + functions make an early exit if timevar is disabled. */ + extern int timevar_enabled; # ifdef __cplusplus diff --git a/m4/timevar.m4 b/m4/timevar.m4 deleted file mode 100644 index c790ed0a9a..0000000000 --- a/m4/timevar.m4 +++ /dev/null @@ -1,47 +0,0 @@ -# -*- Autoconf -*- -# Checks required to run `timevar', a time tracker. -# -# Copyright (C) 2002-2003, 2009-2015, 2018 Free Software Foundation, -# Inc. -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program 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 General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -# serial 3 - -AC_DEFUN([gl_TIMEVAR], -[AC_CHECK_HEADERS([sys/time.h sys/times.h]) - AC_CHECK_HEADERS([sys/resource.h], [], [], - [$ac_includes_default -#if HAVE_SYS_TIME_H -# include -#endif -#ifdef HAVE_SYS_TIMES_H -# include -#endif -]) -AC_CHECK_FUNCS([times]) - -AC_CHECK_DECLS([getrusage, times, clock, sysconf], [], [], -[$ac_includes_default -#if HAVE_SYS_TIME_H -# include -#endif -#if HAVE_SYS_TIMES_H -# include -#endif -#if HAVE_SYS_RESOURCE_H -# include -#endif -]) -]) diff --git a/modules/timevar b/modules/timevar index f82b7e90a0..354d1d211f 100644 --- a/modules/timevar +++ b/modules/timevar @@ -4,9 +4,9 @@ A simple self-profiling module based on timers. Files: lib/timevar.h lib/timevar.c -m4/timevar.m4 Depends-on: +getrusage gettext-h stdlib sys_time @@ -14,9 +14,6 @@ sys_times times xalloc -configure.ac: -gl_TIMEVAR - Makefile.am: lib_SOURCES += timevar.c timevar.def -- 2.39.5