From 66de54f8a195583a3e324df9b8072faab7b76561 Mon Sep 17 00:00:00 2001 From: Simon Josefsson Date: Tue, 28 Oct 2008 18:19:49 +0100 Subject: [PATCH] Add modules for sys/times.h header and times function. --- ChangeLog | 14 +++++++++ MODULES.html.sh | 2 ++ doc/posix-functions/times.texi | 10 ++++-- doc/posix-headers/sys_times.texi | 8 ++--- lib/sys_times.in.h | 59 +++++++++++++++++++++++++++++++++++ lib/times.c | 67 ++++++++++++++++++++++++++++++++++++++++ m4/sys_times_h.m4 | 33 ++++++++++++++++++++ modules/sys_times | 39 +++++++++++++++++++++++ modules/sys_times-tests | 6 ++++ modules/times | 26 ++++++++++++++++ modules/times-tests | 6 ++++ 11 files changed, 263 insertions(+), 7 deletions(-) create mode 100644 lib/sys_times.in.h create mode 100644 lib/times.c create mode 100644 m4/sys_times_h.m4 create mode 100644 modules/sys_times create mode 100644 modules/sys_times-tests create mode 100644 modules/times create mode 100644 modules/times-tests diff --git a/ChangeLog b/ChangeLog index e83fc3deb..a35a54bdd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +2008-10-28 Simon Josefsson + + * MODULES.html.sh (Support for systems lacking POSIX:2001): + Mention times and sys_times. + * modules/sys_times, modules/sys_times-tests: New modules. + * modules/times, modules/times-tests: Likewise + * m4/sys_times_h.m4: New file. + * lib/sys_times.in.h: Likewise + * lib/times.c: Likewise. + * tests/test-sys_times.c: Likewise. + * tests/test-times.c: Likewise. + * doc/posix-headers/sys_times.texi: Update. + * doc/posix-functions/times.texi: Update. + 2008-10-28 Jim Meyering * modules/tempname (Depends-on): Add lstat. diff --git a/MODULES.html.sh b/MODULES.html.sh index cf79994d4..e61a64459 100755 --- a/MODULES.html.sh +++ b/MODULES.html.sh @@ -2162,6 +2162,7 @@ func_all_modules () func_module tempname func_module time func_module time_r + func_module times func_module timespec func_module nanosleep func_module regex @@ -2178,6 +2179,7 @@ func_all_modules () func_module sys_socket func_module sys_stat func_module sys_time + func_module sys_times func_module tsearch func_module unistd func_module utime diff --git a/doc/posix-functions/times.texi b/doc/posix-functions/times.texi index d55f78f5f..7a1657fdd 100644 --- a/doc/posix-functions/times.texi +++ b/doc/posix-functions/times.texi @@ -4,15 +4,19 @@ POSIX specification: @url{http://www.opengroup.org/susv3xsh/times.html} -Gnulib module: --- +Gnulib module: times Portability problems fixed by Gnulib: @itemize +@item +This function is missing on some platforms: +mingw. @end itemize Portability problems not fixed by Gnulib: @itemize @item -This function is missing on some platforms: -mingw. +There is no function on Windows to measure consumed process child +times, thus the @code{tms_cutime} and @code{tms_cstime} will always be +0 when the module is used. @end itemize diff --git a/doc/posix-headers/sys_times.texi b/doc/posix-headers/sys_times.texi index eee4125a5..140f71ef4 100644 --- a/doc/posix-headers/sys_times.texi +++ b/doc/posix-headers/sys_times.texi @@ -3,15 +3,15 @@ POSIX specification: @url{http://www.opengroup.org/susv3xbd/sys/times.h.html} -Gnulib module: --- +Gnulib module: sys_times Portability problems fixed by Gnulib: @itemize +@item +This header file is missing on some platforms: +mingw. @end itemize Portability problems not fixed by Gnulib: @itemize -@item -This header file is missing on some platforms: -mingw. @end itemize diff --git a/lib/sys_times.in.h b/lib/sys_times.in.h new file mode 100644 index 000000000..56ac45313 --- /dev/null +++ b/lib/sys_times.in.h @@ -0,0 +1,59 @@ +/* Provide a sys/times.h header file. + Copyright (C) 2008 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 2, 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, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +/* Written by Simon Josefsson , 2008. */ + +/* This file is supposed to be used on platforms where + is missing. */ + +#ifndef _GL_SYS_TIMES_H +# define _GL_SYS_TIMES_H + +/* Get clock_t. */ +# include + +/* The definition of GL_LINK_WARNING is copied here. */ + +# ifdef __cplusplus +extern "C" { +# endif + + /* Structure describing CPU time used by a process and its children. */ + struct tms + { + clock_t tms_utime; /* User CPU time. */ + clock_t tms_stime; /* System CPU time. */ + + clock_t tms_cutime; /* User CPU time of dead children. */ + clock_t tms_cstime; /* System CPU time of dead children. */ + }; + +# if @GNULIB_TIMES@ + extern clock_t times (struct tms *buffer); +# elif defined GNULIB_POSIXCHECK +# undef times +# define times(s) \ + (GL_LINK_WARNING ("times is unportable - " \ + "use gnulib module times for portability"), \ + times (s)) +# endif + +# ifdef __cplusplus +} +# endif + +#endif /* _GL_SYS_TIMES_H */ diff --git a/lib/times.c b/lib/times.c new file mode 100644 index 000000000..f71889a43 --- /dev/null +++ b/lib/times.c @@ -0,0 +1,67 @@ +/* Get process times + + Copyright (C) 2008 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 2, 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, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +/* Written by Simon Josefsson , 2008. */ + +#include + +/* Get times prototype. */ +#include + +/* Get round. */ +#include + +/* Get GetProcessTimes etc. */ +#include + +static clock_t +filetime2clock (FILETIME time) +{ + float f; + + /* We have a 64-bit value, in the form of two DWORDS aka unsigned + int, counting the number of 100-nanosecond intervals. We need to + convert these to clock ticks. Older POSIX uses CLK_TCK to + indicate the number of clock ticks per second while modern POSIX + uses sysconf(_SC_CLK_TCK). Mingw32 does not appear to have + sysconf(_SC_CLK_TCK), but appears to have CLK_TCK = 1000 so we + use it. Note that CLOCKS_PER_SEC constant does not apply here, + it is for use with the clock function. */ + + f = (unsigned long long) time.dwHighDateTime << 32; + f += time.dwLowDateTime; + f = f * CLK_TCK / 10000000; + return (clock_t) round (f); +} + +clock_t +times (struct tms * buffer) +{ + FILETIME creation_time, exit_time, kernel_time, user_time; + + if (GetProcessTimes (GetCurrentProcess (), &creation_time, &exit_time, + &kernel_time, &user_time) == 0) + return (clock_t) -1; + + buffer->tms_utime = filetime2clock (user_time); + buffer->tms_stime = filetime2clock (kernel_time); + buffer->tms_cutime = 0; + buffer->tms_cstime = 0; + + return filetime2clock (creation_time); +} diff --git a/m4/sys_times_h.m4 b/m4/sys_times_h.m4 new file mode 100644 index 000000000..9a45c427d --- /dev/null +++ b/m4/sys_times_h.m4 @@ -0,0 +1,33 @@ +# Configure a replacement for . + +# Copyright (C) 2008 Free Software Foundation, Inc. +# This file is free software; the Free Software Foundation +# gives unlimited permission to copy and/or distribute it, +# with or without modifications, as long as this notice is preserved. + +# Written by Simon Josefsson. + +AC_DEFUN([gl_SYS_TIMES_H], +[ + AC_REQUIRE([gl_SYS_TIMES_H_DEFAULTS]) + + AC_CHECK_HEADERS_ONCE([sys/times.h]) + if test $ac_cv_header_sys_times_h = yes; then + SYS_TIMES_H= + else + SYS_TIMES_H=sys/times.h + fi + AC_SUBST([SYS_TIMES_H]) +]) + +AC_DEFUN([gl_SYS_TIMES_MODULE_INDICATOR], +[ + dnl Use AC_REQUIRE here, so that the default settings are expanded once only. + AC_REQUIRE([gl_SYS_TIMES_H_DEFAULTS]) + GNULIB_[]m4_translit([$1],[abcdefghijklmnopqrstuvwxyz./-],[ABCDEFGHIJKLMNOPQRSTUVWXYZ___])=1 +]) + +AC_DEFUN([gl_SYS_TIMES_H_DEFAULTS], +[ + GNULIB_TIMES=0; AC_SUBST([GNULIB_TIMES]) +]) diff --git a/modules/sys_times b/modules/sys_times new file mode 100644 index 000000000..1b6f8375b --- /dev/null +++ b/modules/sys_times @@ -0,0 +1,39 @@ +Description: +A for systems lacking it. + +Files: +lib/sys_times.in.h +m4/sys_times_h.m4 + +Depends-on: +link-warning + +configure.ac: +gl_SYS_TIMES_H +AC_PROG_MKDIR_P + +Makefile.am: +BUILT_SOURCES += $(SYS_TIMES_H) + +# We need the following in order to create when the system +# doesn't have one that works with the given compiler. +sys/times.h: sys_times.in.h + @MKDIR_P@ sys + rm -f $@-t $@ + { echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \ + sed -e 's|@''GNULIB_TIMES''@|$(GNULIB_TIMES)|g' \ + -e '/definition of GL_LINK_WARNING/r $(LINK_WARNING_H)' \ + < $(srcdir)/sys_times.in.h; \ + } > $@-t + mv $@-t $@ +MOSTLYCLEANFILES += sys/times.h sys/times.h-t +MOSTLYCLEANDIRS += sys + +Include: +#include + +License: +LGPLv2+ + +Maintainer: +Simon Josefsson diff --git a/modules/sys_times-tests b/modules/sys_times-tests new file mode 100644 index 000000000..f62215703 --- /dev/null +++ b/modules/sys_times-tests @@ -0,0 +1,6 @@ +Files: +tests/test-sys_times.c + +Makefile.am: +TESTS += test-sys_times +check_PROGRAMS += test-sys_times diff --git a/modules/times b/modules/times new file mode 100644 index 000000000..249931bb0 --- /dev/null +++ b/modules/times @@ -0,0 +1,26 @@ +Description: +times() function: get process times + +Files: +lib/times.c + +Depends-on: +sys_times + +configure.ac: +AC_CHECK_FUNCS_ONCE([times]) +if test $ac_cv_func_times = no; then + AC_LIBOBJ([times]) +fi +gl_SYS_TIMES_MODULE_INDICATOR([times]) + +Makefile.am: + +Include: +#include + +License: +LGPLv2+ + +Maintainer: +Simon Josefsson diff --git a/modules/times-tests b/modules/times-tests new file mode 100644 index 000000000..838eac001 --- /dev/null +++ b/modules/times-tests @@ -0,0 +1,6 @@ +Files: +tests/test-times.c + +Makefile.am: +TESTS += test-times +check_PROGRAMS += test-times -- 2.11.0