changeset 30426:692835f5e4a3

Add modules for sys/times.h header and times function.
author Simon Josefsson <simon@josefsson.org>
date Tue, 28 Oct 2008 18:19:49 +0100
parents 6d78154f655f
children cbc784206ff2
files ChangeLog MODULES.html.sh doc/posix-functions/times.texi doc/posix-headers/sys_times.texi lib/sys_times.in.h lib/times.c m4/sys_times_h.m4 modules/sys_times modules/sys_times-tests modules/times modules/times-tests
diffstat 11 files changed, 264 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Tue Oct 28 11:56:54 2008 +0100
+++ b/ChangeLog	Tue Oct 28 18:19:49 2008 +0100
@@ -1,3 +1,17 @@
+2008-10-28  Simon Josefsson  <simon@josefsson.org>
+
+	* 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  <meyering@redhat.com>
 
 	* modules/tempname (Depends-on): Add lstat.
--- a/MODULES.html.sh	Tue Oct 28 11:56:54 2008 +0100
+++ b/MODULES.html.sh	Tue Oct 28 18:19:49 2008 +0100
@@ -2162,6 +2162,7 @@
   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_module sys_socket
   func_module sys_stat
   func_module sys_time
+  func_module sys_times
   func_module tsearch
   func_module unistd
   func_module utime
--- a/doc/posix-functions/times.texi	Tue Oct 28 11:56:54 2008 +0100
+++ b/doc/posix-functions/times.texi	Tue Oct 28 18:19:49 2008 +0100
@@ -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
--- a/doc/posix-headers/sys_times.texi	Tue Oct 28 11:56:54 2008 +0100
+++ b/doc/posix-headers/sys_times.texi	Tue Oct 28 18:19:49 2008 +0100
@@ -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
-@end itemize
-
-Portability problems not fixed by Gnulib:
-@itemize
 @item
 This header file is missing on some platforms:
 mingw.
 @end itemize
+
+Portability problems not fixed by Gnulib:
+@itemize
+@end itemize
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/sys_times.in.h	Tue Oct 28 18:19:49 2008 +0100
@@ -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 <simon@josefsson.org>, 2008.  */
+
+/* This file is supposed to be used on platforms where <sys/times.h>
+   is missing.  */
+
+#ifndef _GL_SYS_TIMES_H
+# define _GL_SYS_TIMES_H
+
+/* Get clock_t. */
+# include <time.h>
+
+/* 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 */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/times.c	Tue Oct 28 18:19:49 2008 +0100
@@ -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 <simon@josefsson.org>, 2008.  */
+
+#include <config.h>
+
+/* Get times prototype. */
+#include <sys/times.h>
+
+/* Get round. */
+#include <math.h>
+
+/* Get GetProcessTimes etc. */
+#include <windows.h>
+
+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);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/m4/sys_times_h.m4	Tue Oct 28 18:19:49 2008 +0100
@@ -0,0 +1,33 @@
+# Configure a replacement for <sys/times.h>.
+
+# 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])
+])
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/sys_times	Tue Oct 28 18:19:49 2008 +0100
@@ -0,0 +1,39 @@
+Description:
+A <sys/times.h> 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 <sys/times.h> 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 <sys/times.h>
+
+License:
+LGPLv2+
+
+Maintainer:
+Simon Josefsson
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/sys_times-tests	Tue Oct 28 18:19:49 2008 +0100
@@ -0,0 +1,6 @@
+Files:
+tests/test-sys_times.c
+
+Makefile.am:
+TESTS += test-sys_times
+check_PROGRAMS += test-sys_times
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/times	Tue Oct 28 18:19:49 2008 +0100
@@ -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 <sys/times.h>
+
+License:
+LGPLv2+
+
+Maintainer:
+Simon Josefsson
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/times-tests	Tue Oct 28 18:19:49 2008 +0100
@@ -0,0 +1,6 @@
+Files:
+tests/test-times.c
+
+Makefile.am:
+TESTS += test-times
+check_PROGRAMS += test-times