This is the mail archive of the glibc-bugs@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libc/16996] get_nprocs caching breaks when 'time (NULL) == 0'


https://sourceware.org/bugzilla/show_bug.cgi?id=16996

--- Comment #1 from cvs-commit at gcc dot gnu.org <cvs-commit at gcc dot gnu.org> ---
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GNU C Library master sources".

The branch, master has been updated
       via  995a46bbfba9964e328e3947130919d8bd3cd62a (commit)
      from  febf6cc58c36cd5a978b8b5faedb5a932eb44c98 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=995a46bbfba9964e328e3947130919d8bd3cd62a

commit 995a46bbfba9964e328e3947130919d8bd3cd62a
Author: Meador Inge <meadori@codesourcery.com>
Date:   Fri Jun 13 14:02:04 2014 +0530

    get_nprocs: Only return explictly set cache values (BZ #16996)

    The implementation of __get_nprocs uses a stactic variable to cache
    the value of the current number of processors.  The caching breaks when
    'time (NULL) == 0':

      $ cat nproc.c
      #include <stdio.h>
      #include <time.h>
      #include <sys/time.h>

      int main(int argc, char *argv[])
      {
        time_t t;
        struct timeval tv = {0, 0};
        printf("settimeofday({0, 0}, NULL) = %d\n", settimeofday(&tv, NULL));
        t = time(NULL);
        printf("Time: %d, CPUs: %d\n", (unsigned int)t, get_nprocs());
        return 0;
      }
      $ gcc -O3 nproc.c
      $ ./a.out
      settimeofday({0, 0}, NULL) = -1
      Time: 1401311578, CPUs: 4
      $ sudo ./a.out
      settimeofday({0, 0}, NULL) = 0
      Time: 0, CPUs: 0

    The problem is with the condition used to check whether a cached
    value should be returned or not:

      static int cached_result;
      static time_t timestamp;

      time_t now = time (NULL);
      time_t prev = timestamp;
      atomic_read_barrier ();
      if (now == prev)
        return cached_result;

    This patch fixes the problem by ensuring that 'cached_result' has
    been set at least once before returning it.

-----------------------------------------------------------------------

Summary of changes:
 ChangeLog                             |    6 ++++++
 NEWS                                  |    2 +-
 sysdeps/unix/sysv/linux/getsysstats.c |    4 ++--
 3 files changed, 9 insertions(+), 3 deletions(-)

-- 
You are receiving this mail because:
You are on the CC list for the bug.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]