bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

new modules: times and sys_times


From: Simon Josefsson
Subject: new modules: times and sys_times
Date: Mon, 27 Oct 2008 18:39:06 +0100
User-agent: Gnus/5.110011 (No Gnus v0.11) Emacs/22.2 (gnu/linux)

This module can be useful for benchmarking in self-tests, for example.
Thoughts?

The patch does not link the modules in MODULES.html.sh, I'll take care
of that in later revisions of the patch (or when pushing it).

/Simon

>From 9789867af6d7ffb3fd362789fe9bbc3fe335df93 Mon Sep 17 00:00:00 2001
From: Simon Josefsson <address@hidden>
Date: Mon, 27 Oct 2008 18:33:07 +0100
Subject: [PATCH] Add modules for the sys/times.h header and the times function.

---
 ChangeLog                        |   12 +++++
 doc/posix-functions/times.texi   |   10 +++-
 doc/posix-headers/sys_times.texi |    8 ++--
 lib/sys_times.in.h               |   60 ++++++++++++++++++++++
 lib/times.c                      |   67 +++++++++++++++++++++++++
 m4/sys_times_h.m4                |   33 ++++++++++++
 modules/sys_times                |   39 +++++++++++++++
 modules/sys_times-tests          |   10 ++++
 modules/times                    |   26 ++++++++++
 modules/times-tests              |    6 ++
 tests/test-sys_times.c           |   29 +++++++++++
 tests/test-times.c               |  101 ++++++++++++++++++++++++++++++++++++++
 12 files changed, 394 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
 create mode 100644 tests/test-sys_times.c
 create mode 100644 tests/test-times.c

diff --git a/ChangeLog b/ChangeLog
index c32c1eb..361e050 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2008-10-27  Simon Josefsson  <address@hidden>
+
+       * 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-24  Simon Josefsson  <address@hidden>
 
        * doc/gnulib.texi (Header files): Add note about C++ problems.
diff --git a/doc/posix-functions/times.texi b/doc/posix-functions/times.texi
index d55f78f..7a1657f 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
address@hidden
+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 eee4125..140f71e 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
address@hidden
+This header file is missing on some platforms:
+mingw.
 @end itemize
 
 Portability problems not fixed by Gnulib:
 @itemize
address@hidden
-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 0000000..847df61
--- /dev/null
+++ b/lib/sys_times.in.h
@@ -0,0 +1,60 @@
+/* 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.  */
+
+/* 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 */
diff --git a/lib/times.c b/lib/times.c
new file mode 100644
index 0000000..f71889a
--- /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 <address@hidden>, 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);
+}
diff --git a/m4/sys_times_h.m4 b/m4/sys_times_h.m4
new file mode 100644
index 0000000..9a45c42
--- /dev/null
+++ b/m4/sys_times_h.m4
@@ -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])
+])
diff --git a/modules/sys_times b/modules/sys_times
new file mode 100644
index 0000000..1b6f837
--- /dev/null
+++ b/modules/sys_times
@@ -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 address@hidden $@
+       { 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; \
+       } > address@hidden
+       mv address@hidden $@
+MOSTLYCLEANFILES += sys/times.h sys/times.h-t
+MOSTLYCLEANDIRS += sys
+
+Include:
+#include <sys/times.h>
+
+License:
+LGPLv2+
+
+Maintainer:
+Simon Josefsson
diff --git a/modules/sys_times-tests b/modules/sys_times-tests
new file mode 100644
index 0000000..3903d96
--- /dev/null
+++ b/modules/sys_times-tests
@@ -0,0 +1,10 @@
+Files:
+tests/test-sys_times.c
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-sys_times
+check_PROGRAMS += test-sys_times
diff --git a/modules/times b/modules/times
new file mode 100644
index 0000000..d989753
--- /dev/null
+++ b/modules/times
@@ -0,0 +1,26 @@
+Description:
+times() function: get process and waited-for child 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
diff --git a/modules/times-tests b/modules/times-tests
new file mode 100644
index 0000000..838eac0
--- /dev/null
+++ b/modules/times-tests
@@ -0,0 +1,6 @@
+Files:
+tests/test-times.c
+
+Makefile.am:
+TESTS += test-times
+check_PROGRAMS += test-times
diff --git a/tests/test-sys_times.c b/tests/test-sys_times.c
new file mode 100644
index 0000000..f312372
--- /dev/null
+++ b/tests/test-sys_times.c
@@ -0,0 +1,29 @@
+/* Test of <sys/times.h> substitute.
+   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 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 <http://www.gnu.org/licenses/>.  */
+
+/* Written by Simon Josefsson <address@hidden>, 2008.  */
+
+#include <config.h>
+
+#include <sys/times.h>
+
+struct tms tms;
+
+int
+main (void)
+{
+  return 0;
+}
diff --git a/tests/test-times.c b/tests/test-times.c
new file mode 100644
index 0000000..377a139
--- /dev/null
+++ b/tests/test-times.c
@@ -0,0 +1,101 @@
+/* Test of times function.
+   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 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 <http://www.gnu.org/licenses/>.  */
+
+/* Written by Simon Josefsson <address@hidden>, 2008.  */
+
+#include <config.h>
+
+#include <sys/times.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <math.h>
+
+static int
+doublecmp (const void *p, const void *q)
+{
+  double a = *(double *) p;
+  double b = *(double *) q;
+
+  return a < b;
+}
+
+int
+main (int argc, char *argv[])
+{
+  struct tms tms;
+  clock_t t;
+#ifndef _SC_CLK_TCK
+  clock_t clk_tck = CLK_TCK;
+#else
+  clock_t clk_tck = sysconf (_SC_CLK_TCK);
+#endif
+
+  t = times (&tms);
+  if (t == (clock_t) -1)
+    {
+      perror ("times");
+      return EXIT_FAILURE;
+    }
+
+  if (argc > 1)
+    {
+      printf ("clk_tck %ld\n", (long int) clk_tck);
+
+      printf ("t %ld\n", t);
+      printf ("tms.tms_utime %ldms\n", (tms.tms_utime * 1000) / clk_tck);
+      printf ("tms.tms_stime %ldms\n", (tms.tms_stime * 1000) / clk_tck);
+      printf ("tms.tms_cutime %ldms\n", (tms.tms_cutime * 1000) / clk_tck);
+      printf ("tms.tms_cstime %ldms\n", (tms.tms_cstime * 1000) / clk_tck);
+    }
+
+  if (argc > 1)
+    {
+      size_t size = atoi (argv[1]);
+      double *base;
+      size_t i;
+
+      base = malloc (size * sizeof (double));
+
+      for (i = 0; i < size; i++)
+       base[i] = i * i;
+
+      qsort (base, size, sizeof (double), doublecmp);
+
+      free (base);
+    }
+
+  t = times (&tms);
+  if (t == (clock_t) -1)
+    {
+      perror ("times");
+      return EXIT_FAILURE;
+    }
+
+  if (argc > 1)
+    {
+      printf ("clk_tck %ld\n", (long int) clk_tck);
+
+      printf ("t %ld\n", t);
+      printf ("tms.tms_utime %ldms\n", (tms.tms_utime * 1000) / clk_tck);
+      printf ("tms.tms_stime %ldms\n", (tms.tms_stime * 1000) / clk_tck);
+      printf ("tms.tms_cutime %ldms\n", (tms.tms_cutime * 1000) / clk_tck);
+      printf ("tms.tms_cstime %ldms\n", (tms.tms_cstime * 1000) / clk_tck);
+    }
+
+  return 0;
+}
-- 
1.5.6.5





reply via email to

[Prev in Thread] Current Thread [Next in Thread]