comparison src/os/linux/vm/os_linux.cpp @ 24176:e21dd2c95cf0

6515172: Runtime.availableProcessors() ignores Linux taskset command Summary: extract processor count from sched_getaffinity mask Reviewed-by: dholmes, gthornbr
author shshahma
date Thu, 22 Sep 2016 02:04:40 -0700
parents 776cb7cbe2e4
children 75021e6fe108
comparison
equal deleted inserted replaced
24175:12b06fba6e5b 24176:e21dd2c95cf0
1 /* 1 /*
2 * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
101 # include <stdint.h> 101 # include <stdint.h>
102 # include <inttypes.h> 102 # include <inttypes.h>
103 # include <sys/ioctl.h> 103 # include <sys/ioctl.h>
104 104
105 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 105 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
106
107 #ifndef _GNU_SOURCE
108 #define _GNU_SOURCE
109 #include <sched.h>
110 #undef _GNU_SOURCE
111 #else
112 #include <sched.h>
113 #endif
106 114
107 // if RUSAGE_THREAD for getrusage() has not been defined, do it here. The code calling 115 // if RUSAGE_THREAD for getrusage() has not been defined, do it here. The code calling
108 // getrusage() is prepared to handle the associated failure. 116 // getrusage() is prepared to handle the associated failure.
109 #ifndef RUSAGE_THREAD 117 #ifndef RUSAGE_THREAD
110 #define RUSAGE_THREAD (1) /* only the calling thread */ 118 #define RUSAGE_THREAD (1) /* only the calling thread */
5014 if( !linux_mprotect((char *)_polling_page, Linux::page_size(), PROT_READ)) { 5022 if( !linux_mprotect((char *)_polling_page, Linux::page_size(), PROT_READ)) {
5015 fatal("Could not enable polling page"); 5023 fatal("Could not enable polling page");
5016 } 5024 }
5017 }; 5025 };
5018 5026
5027 static int os_cpu_count(const cpu_set_t* cpus) {
5028 int count = 0;
5029 // only look up to the number of configured processors
5030 for (int i = 0; i < os::processor_count(); i++) {
5031 if (CPU_ISSET(i, cpus)) {
5032 count++;
5033 }
5034 }
5035 return count;
5036 }
5037
5038 // Get the current number of available processors for this process.
5039 // This value can change at any time during a process's lifetime.
5040 // sched_getaffinity gives an accurate answer as it accounts for cpusets.
5041 // If anything goes wrong we fallback to returning the number of online
5042 // processors - which can be greater than the number available to the process.
5019 int os::active_processor_count() { 5043 int os::active_processor_count() {
5020 // Linux doesn't yet have a (official) notion of processor sets, 5044 cpu_set_t cpus; // can represent at most 1024 (CPU_SETSIZE) processors
5021 // so just return the number of online processors. 5045 int cpus_size = sizeof(cpu_set_t);
5022 int online_cpus = ::sysconf(_SC_NPROCESSORS_ONLN); 5046 int cpu_count = 0;
5023 assert(online_cpus > 0 && online_cpus <= processor_count(), "sanity check"); 5047
5024 return online_cpus; 5048 // pid 0 means the current thread - which we have to assume represents the process
5049 if (sched_getaffinity(0, cpus_size, &cpus) == 0) {
5050 cpu_count = os_cpu_count(&cpus);
5051 if (PrintActiveCpus) {
5052 tty->print_cr("active_processor_count: sched_getaffinity processor count: %d", cpu_count);
5053 }
5054 }
5055 else {
5056 cpu_count = ::sysconf(_SC_NPROCESSORS_ONLN);
5057 warning("sched_getaffinity failed (%s)- using online processor count (%d) "
5058 "which may exceed available processors", strerror(errno), cpu_count);
5059 }
5060
5061 assert(cpu_count > 0 && cpu_count <= processor_count(), "sanity check");
5062 return cpu_count;
5025 } 5063 }
5026 5064
5027 void os::set_native_thread_name(const char *name) { 5065 void os::set_native_thread_name(const char *name) {
5028 // Not yet implemented. 5066 // Not yet implemented.
5029 return; 5067 return;