]> sourceware.org Git - glibc.git/commitdiff
Rate limit expensive _SC_NPROCESSORS_ONLN computation
authorUlrich Drepper <drepper@gmail.com>
Wed, 22 Jun 2011 13:50:39 +0000 (09:50 -0400)
committerUlrich Drepper <drepper@gmail.com>
Wed, 22 Jun 2011 13:50:39 +0000 (09:50 -0400)
ChangeLog
sysdeps/unix/sysv/linux/getsysstats.c

index 2901e2e67ce1e2105779b6c15f3bb5fbcc2de8d1..9cef65ae0242f176d96aa046ef8b83054e1c701c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2011-06-22  Ulrich Drepper  <drepper@gmail.com>
+
+       * sysdeps/unix/sysv/linux/getsysstats.c (__get_nprocs): Rate limit
+       reading the information from the /proc filesystem to once a second.
+
 2011-06-21  Andreas Jaeger  <aj@suse.de>
 
        * sysdeps/unix/sysv/linux/bits/sigcontext.h: Fix definition of
index af454b650de92e4910a5196c283ae04050e6e07f..a13b6e3e37afbb5a5b26e07478baac13f18e21cf 100644 (file)
@@ -1,5 +1,5 @@
 /* Determine various system internal values, Linux version.
-   Copyright (C) 1996-2003,2006,2007,2009,2010 Free Software Foundation, Inc.
+   Copyright (C) 1996-2003,2006,2007,2009,2010,2011 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
 
 
 #include <atomic.h>
 #include <not-cancel.h>
+#include <kernel-features.h>
+
+#ifndef HAVE_CLOCK_GETTIME_VSYSCALL
+# undef INTERNAL_VSYSCALL
+# define INTERNAL_VSYSCALL INTERNAL_SYSCALL
+# undef INLINE_VSYSCALL
+# define INLINE_VSYSCALL INLINE_SYSCALL
+#else
+# include <bits/libc-vdso.h>
+#endif
 
 
 /* How we can determine the number of available processors depends on
@@ -128,6 +138,22 @@ next_line (int fd, char *const buffer, char **cp, char **re,
 int
 __get_nprocs ()
 {
+  static int cached_result;
+  static time_t timestamp;
+
+#ifdef __ASSUME_POSIX_TIMERS
+  struct timespec ts;
+  INTERNAL_SYSCALL_DECL (err);
+  INTERNAL_VSYSCALL (clock_gettime, err, 2, CLOCK_REALTIME, &ts);
+#else
+  struct timeval ts;
+  gettimeofday (&ts, NULL);
+#endif
+  time_t prev = timestamp;
+  atomic_read_barrier ();
+  if (ts.tv_sec == prev)
+    return cached_result;
+
   /* XXX Here will come a test for the new system call.  */
 
   const size_t buffer_size = __libc_use_alloca (8192) ? 8192 : 512;
@@ -169,6 +195,10 @@ __get_nprocs ()
        }
     }
 
+  cached_result = result;
+  atomic_write_barrier ();
+  timestamp = ts.tv_sec;
+
   return result;
 }
 weak_alias (__get_nprocs, get_nprocs)
This page took 0.044006 seconds and 5 git commands to generate.