< prev index next >

src/os/linux/vm/os_linux.cpp

Print this page


   1 /*
   2  * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 // no precompiled headers
  26 #include "classfile/classLoader.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "classfile/vmSymbols.hpp"
  29 #include "code/icBuffer.hpp"
  30 #include "code/vtableStubs.hpp"
  31 #include "compiler/compileBroker.hpp"
  32 #include "compiler/disassembler.hpp"
  33 #include "interpreter/interpreter.hpp"
  34 #include "jvm_linux.h"

  35 #include "memory/allocation.inline.hpp"
  36 #include "memory/filemap.hpp"
  37 #include "mutex_linux.inline.hpp"
  38 #include "oops/oop.inline.hpp"
  39 #include "os_linux.inline.hpp"
  40 #include "os_share_linux.hpp"
  41 #include "prims/jniFastGetField.hpp"
  42 #include "prims/jvm.h"
  43 #include "prims/jvm_misc.hpp"
  44 #include "runtime/arguments.hpp"
  45 #include "runtime/atomic.inline.hpp"
  46 #include "runtime/extendedPC.hpp"
  47 #include "runtime/globals.hpp"
  48 #include "runtime/interfaceSupport.hpp"
  49 #include "runtime/init.hpp"
  50 #include "runtime/java.hpp"
  51 #include "runtime/javaCalls.hpp"
  52 #include "runtime/mutexLocker.hpp"
  53 #include "runtime/objectMonitor.hpp"
  54 #include "runtime/orderAccess.inline.hpp"


  89 # include <sys/time.h>
  90 # include <sys/times.h>
  91 # include <sys/utsname.h>
  92 # include <sys/socket.h>
  93 # include <sys/wait.h>
  94 # include <pwd.h>
  95 # include <poll.h>
  96 # include <semaphore.h>
  97 # include <fcntl.h>
  98 # include <string.h>
  99 # include <syscall.h>
 100 # include <sys/sysinfo.h>
 101 # include <gnu/libc-version.h>
 102 # include <sys/ipc.h>
 103 # include <sys/shm.h>
 104 # include <link.h>
 105 # include <stdint.h>
 106 # include <inttypes.h>
 107 # include <sys/ioctl.h>
 108 








 109 // if RUSAGE_THREAD for getrusage() has not been defined, do it here. The code calling
 110 // getrusage() is prepared to handle the associated failure.
 111 #ifndef RUSAGE_THREAD
 112   #define RUSAGE_THREAD   (1)               /* only the calling thread */
 113 #endif
 114 
 115 #define MAX_PATH    (2 * K)
 116 
 117 #define MAX_SECS 100000000
 118 
 119 // for timer info max values which include all bits
 120 #define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF)
 121 
 122 #define LARGEPAGES_BIT (1 << 6)
 123 ////////////////////////////////////////////////////////////////////////////////
 124 // global variables
 125 julong os::Linux::_physical_memory = 0;
 126 
 127 address   os::Linux::_initial_thread_stack_bottom = NULL;
 128 uintptr_t os::Linux::_initial_thread_stack_size   = 0;


4745   // initialize thread priority policy
4746   prio_init();
4747 
4748   return JNI_OK;
4749 }
4750 
4751 // Mark the polling page as unreadable
4752 void os::make_polling_page_unreadable(void) {
4753   if (!guard_memory((char*)_polling_page, Linux::page_size())) {
4754     fatal("Could not disable polling page");
4755   }
4756 }
4757 
4758 // Mark the polling page as readable
4759 void os::make_polling_page_readable(void) {
4760   if (!linux_mprotect((char *)_polling_page, Linux::page_size(), PROT_READ)) {
4761     fatal("Could not enable polling page");
4762   }
4763 }
4764 







4765 int os::active_processor_count() {
4766   // Linux doesn't yet have a (official) notion of processor sets,
4767   // so just return the number of online processors.





















4768   int online_cpus = ::sysconf(_SC_NPROCESSORS_ONLN);
4769   assert(online_cpus > 0 && online_cpus <= processor_count(), "sanity check");



4770   return online_cpus;





























4771 }
4772 
4773 void os::set_native_thread_name(const char *name) {
4774   if (Linux::_pthread_setname_np) {
4775     char buf [16]; // according to glibc manpage, 16 chars incl. '/0'
4776     snprintf(buf, sizeof(buf), "%s", name);
4777     buf[sizeof(buf) - 1] = '\0';
4778     const int rc = Linux::_pthread_setname_np(pthread_self(), buf);
4779     // ERANGE should not happen; all other errors should just be ignored.
4780     assert(rc != ERANGE, "pthread_setname_np failed");
4781   }
4782 }
4783 
4784 bool os::distribute_processes(uint length, uint* distribution) {
4785   // Not yet implemented.
4786   return false;
4787 }
4788 
4789 bool os::bind_to_processor(uint processor_id) {
4790   // Not yet implemented.


   1 /*
   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.
   4  *
   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
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 // no precompiled headers
  26 #include "classfile/classLoader.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "classfile/vmSymbols.hpp"
  29 #include "code/icBuffer.hpp"
  30 #include "code/vtableStubs.hpp"
  31 #include "compiler/compileBroker.hpp"
  32 #include "compiler/disassembler.hpp"
  33 #include "interpreter/interpreter.hpp"
  34 #include "jvm_linux.h"
  35 #include "logging/log.hpp"
  36 #include "memory/allocation.inline.hpp"
  37 #include "memory/filemap.hpp"
  38 #include "mutex_linux.inline.hpp"
  39 #include "oops/oop.inline.hpp"
  40 #include "os_linux.inline.hpp"
  41 #include "os_share_linux.hpp"
  42 #include "prims/jniFastGetField.hpp"
  43 #include "prims/jvm.h"
  44 #include "prims/jvm_misc.hpp"
  45 #include "runtime/arguments.hpp"
  46 #include "runtime/atomic.inline.hpp"
  47 #include "runtime/extendedPC.hpp"
  48 #include "runtime/globals.hpp"
  49 #include "runtime/interfaceSupport.hpp"
  50 #include "runtime/init.hpp"
  51 #include "runtime/java.hpp"
  52 #include "runtime/javaCalls.hpp"
  53 #include "runtime/mutexLocker.hpp"
  54 #include "runtime/objectMonitor.hpp"
  55 #include "runtime/orderAccess.inline.hpp"


  90 # include <sys/time.h>
  91 # include <sys/times.h>
  92 # include <sys/utsname.h>
  93 # include <sys/socket.h>
  94 # include <sys/wait.h>
  95 # include <pwd.h>
  96 # include <poll.h>
  97 # include <semaphore.h>
  98 # include <fcntl.h>
  99 # include <string.h>
 100 # include <syscall.h>
 101 # include <sys/sysinfo.h>
 102 # include <gnu/libc-version.h>
 103 # include <sys/ipc.h>
 104 # include <sys/shm.h>
 105 # include <link.h>
 106 # include <stdint.h>
 107 # include <inttypes.h>
 108 # include <sys/ioctl.h>
 109 
 110 #ifndef _GNU_SOURCE
 111   #define _GNU_SOURCE
 112   #include <sched.h>
 113   #undef _GNU_SOURCE
 114 #else
 115   #include <sched.h>
 116 #endif
 117 
 118 // if RUSAGE_THREAD for getrusage() has not been defined, do it here. The code calling
 119 // getrusage() is prepared to handle the associated failure.
 120 #ifndef RUSAGE_THREAD
 121   #define RUSAGE_THREAD   (1)               /* only the calling thread */
 122 #endif
 123 
 124 #define MAX_PATH    (2 * K)
 125 
 126 #define MAX_SECS 100000000
 127 
 128 // for timer info max values which include all bits
 129 #define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF)
 130 
 131 #define LARGEPAGES_BIT (1 << 6)
 132 ////////////////////////////////////////////////////////////////////////////////
 133 // global variables
 134 julong os::Linux::_physical_memory = 0;
 135 
 136 address   os::Linux::_initial_thread_stack_bottom = NULL;
 137 uintptr_t os::Linux::_initial_thread_stack_size   = 0;


4754   // initialize thread priority policy
4755   prio_init();
4756 
4757   return JNI_OK;
4758 }
4759 
4760 // Mark the polling page as unreadable
4761 void os::make_polling_page_unreadable(void) {
4762   if (!guard_memory((char*)_polling_page, Linux::page_size())) {
4763     fatal("Could not disable polling page");
4764   }
4765 }
4766 
4767 // Mark the polling page as readable
4768 void os::make_polling_page_readable(void) {
4769   if (!linux_mprotect((char *)_polling_page, Linux::page_size(), PROT_READ)) {
4770     fatal("Could not enable polling page");
4771   }
4772 }
4773 
4774 // Get the current number of available processors for this process.
4775 // This value can change at any time during a process's lifetime.
4776 // sched_getaffinity gives an accurate answer as it accounts for cpusets.
4777 // If it appears there may be more than 1024 processors then we do a
4778 // dynamic check - see 6515172 for details.
4779 // If anything goes wrong we fallback to returning the number of online
4780 // processors - which can be greater than the number available to the process.
4781 int os::active_processor_count() {
4782   cpu_set_t cpus;  // can represent at most 1024 (CPU_SETSIZE) processors
4783   cpu_set_t* cpus_p = &cpus;
4784   int cpus_size = sizeof(cpu_set_t);
4785 
4786   int configured_cpus = processor_count();  // upper bound on available cpus
4787   int cpu_count = 0;
4788 
4789   // To enable easy testing of the dynamic path on different platforms we
4790   // introduce an experimental flag: UseCpuAllocPath
4791   if (configured_cpus >= CPU_SETSIZE || UseCpuAllocPath) {
4792     // kernel may use a mask bigger than cpu_set_t
4793     log_trace(os)("active_processor_count: using dynamic path %s"
4794                   "- configured processors: %d",
4795                   UseCpuAllocPath ? "(forced) " : "",
4796                   configured_cpus);
4797     cpus_p = CPU_ALLOC(configured_cpus);
4798     if (cpus_p != NULL) {
4799       cpus_size = CPU_ALLOC_SIZE(configured_cpus);
4800       // zero it just to be safe
4801       CPU_ZERO_S(cpus_size, cpus_p);
4802     }
4803     else {
4804        // failed to allocate so fallback to online cpus
4805        int online_cpus = ::sysconf(_SC_NPROCESSORS_ONLN);
4806        log_trace(os)("active_processor_count: "
4807                      "CPU_ALLOC failed (%s) - using "
4808                      "online processor count: %d",
4809                      strerror(errno), online_cpus);
4810        return online_cpus;
4811     }
4812   }
4813   else {
4814     log_trace(os)("active_processor_count: using static path - configured processors: %d",
4815                   configured_cpus);
4816   }
4817        
4818   // pid 0 means the current thread - which we have to assume represents the process
4819   if (sched_getaffinity(0, cpus_size, cpus_p) == 0) {
4820     if (cpus_p != &cpus) {
4821       cpu_count = CPU_COUNT_S(cpus_size, cpus_p);
4822     }
4823     else {
4824       cpu_count = CPU_COUNT(cpus_p);
4825     }
4826     log_trace(os)("active_processor_count: sched_getaffinity processor count: %d", cpu_count);
4827   }
4828   else {
4829     cpu_count = ::sysconf(_SC_NPROCESSORS_ONLN);
4830     warning("sched_getaffinity failed (%s)- using online processor count (%d) "
4831             "which may exceed available processors", strerror(errno), cpu_count);
4832   }
4833 
4834   if (cpus_p != &cpus) {
4835     CPU_FREE(cpus_p);
4836   }  
4837 
4838   assert(cpu_count > 0 && cpu_count <= processor_count(), "sanity check");
4839   return cpu_count;
4840 }
4841 
4842 void os::set_native_thread_name(const char *name) {
4843   if (Linux::_pthread_setname_np) {
4844     char buf [16]; // according to glibc manpage, 16 chars incl. '/0'
4845     snprintf(buf, sizeof(buf), "%s", name);
4846     buf[sizeof(buf) - 1] = '\0';
4847     const int rc = Linux::_pthread_setname_np(pthread_self(), buf);
4848     // ERANGE should not happen; all other errors should just be ignored.
4849     assert(rc != ERANGE, "pthread_setname_np failed");
4850   }
4851 }
4852 
4853 bool os::distribute_processes(uint length, uint* distribution) {
4854   // Not yet implemented.
4855   return false;
4856 }
4857 
4858 bool os::bind_to_processor(uint processor_id) {
4859   // Not yet implemented.


< prev index next >