src/os/linux/vm/os_linux.cpp

changeset 8923
82f3ae5b4190
parent 8619
3a38e441474d
child 8620
65847ffbff14
child 8748
75021e6fe108
child 8940
eb9e617d6f64
     1.1 --- a/src/os/linux/vm/os_linux.cpp	Tue Jan 03 08:51:36 2017 -0800
     1.2 +++ b/src/os/linux/vm/os_linux.cpp	Thu Sep 22 02:04:40 2016 -0700
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
     1.6 + * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
     1.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.8   *
     1.9   * This code is free software; you can redistribute it and/or modify it
    1.10 @@ -104,6 +104,14 @@
    1.11  
    1.12  PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
    1.13  
    1.14 +#ifndef _GNU_SOURCE
    1.15 +  #define _GNU_SOURCE
    1.16 +  #include <sched.h>
    1.17 +  #undef _GNU_SOURCE
    1.18 +#else
    1.19 +  #include <sched.h>
    1.20 +#endif
    1.21 +
    1.22  // if RUSAGE_THREAD for getrusage() has not been defined, do it here. The code calling
    1.23  // getrusage() is prepared to handle the associated failure.
    1.24  #ifndef RUSAGE_THREAD
    1.25 @@ -5016,12 +5024,42 @@
    1.26    }
    1.27  };
    1.28  
    1.29 +static int os_cpu_count(const cpu_set_t* cpus) {
    1.30 +  int count = 0;
    1.31 +  // only look up to the number of configured processors
    1.32 +  for (int i = 0; i < os::processor_count(); i++) {
    1.33 +    if (CPU_ISSET(i, cpus)) {
    1.34 +      count++;
    1.35 +    }
    1.36 +  }
    1.37 +  return count;
    1.38 +}
    1.39 +
    1.40 +// Get the current number of available processors for this process.
    1.41 +// This value can change at any time during a process's lifetime.
    1.42 +// sched_getaffinity gives an accurate answer as it accounts for cpusets.
    1.43 +// If anything goes wrong we fallback to returning the number of online
    1.44 +// processors - which can be greater than the number available to the process.
    1.45  int os::active_processor_count() {
    1.46 -  // Linux doesn't yet have a (official) notion of processor sets,
    1.47 -  // so just return the number of online processors.
    1.48 -  int online_cpus = ::sysconf(_SC_NPROCESSORS_ONLN);
    1.49 -  assert(online_cpus > 0 && online_cpus <= processor_count(), "sanity check");
    1.50 -  return online_cpus;
    1.51 +  cpu_set_t cpus;  // can represent at most 1024 (CPU_SETSIZE) processors
    1.52 +  int cpus_size = sizeof(cpu_set_t);
    1.53 +  int cpu_count = 0;
    1.54 +
    1.55 +  // pid 0 means the current thread - which we have to assume represents the process
    1.56 +  if (sched_getaffinity(0, cpus_size, &cpus) == 0) {
    1.57 +    cpu_count = os_cpu_count(&cpus);
    1.58 +    if (PrintActiveCpus) {
    1.59 +      tty->print_cr("active_processor_count: sched_getaffinity processor count: %d", cpu_count);
    1.60 +    }
    1.61 +  }
    1.62 +  else {
    1.63 +    cpu_count = ::sysconf(_SC_NPROCESSORS_ONLN);
    1.64 +    warning("sched_getaffinity failed (%s)- using online processor count (%d) "
    1.65 +            "which may exceed available processors", strerror(errno), cpu_count);
    1.66 +  }
    1.67 +
    1.68 +  assert(cpu_count > 0 && cpu_count <= processor_count(), "sanity check");
    1.69 +  return cpu_count;
    1.70  }
    1.71  
    1.72  void os::set_native_thread_name(const char *name) {

mercurial