src/os/linux/vm/os_linux.hpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/os/linux/vm/os_linux.hpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,353 @@
     1.4 +/*
     1.5 + * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#ifndef OS_LINUX_VM_OS_LINUX_HPP
    1.29 +#define OS_LINUX_VM_OS_LINUX_HPP
    1.30 +
    1.31 +// Linux_OS defines the interface to Linux operating systems
    1.32 +
    1.33 +/* pthread_getattr_np comes with LinuxThreads-0.9-7 on RedHat 7.1 */
    1.34 +typedef int (*pthread_getattr_func_type) (pthread_t, pthread_attr_t *);
    1.35 +
    1.36 +// Information about the protection of the page at address '0' on this os.
    1.37 +static bool zero_page_read_protected() { return true; }
    1.38 +
    1.39 +class Linux {
    1.40 +  friend class os;
    1.41 +  friend class TestReserveMemorySpecial;
    1.42 +
    1.43 +  // For signal-chaining
    1.44 +#define MAXSIGNUM 32
    1.45 +  static struct sigaction sigact[MAXSIGNUM]; // saved preinstalled sigactions
    1.46 +  static unsigned int sigs;             // mask of signals that have
    1.47 +                                        // preinstalled signal handlers
    1.48 +  static bool libjsig_is_loaded;        // libjsig that interposes sigaction(),
    1.49 +                                        // __sigaction(), signal() is loaded
    1.50 +  static struct sigaction *(*get_signal_action)(int);
    1.51 +  static struct sigaction *get_preinstalled_handler(int);
    1.52 +  static void save_preinstalled_handler(int, struct sigaction&);
    1.53 +
    1.54 +  static void check_signal_handler(int sig);
    1.55 +
    1.56 +  // For signal flags diagnostics
    1.57 +  static int sigflags[MAXSIGNUM];
    1.58 +
    1.59 +  static int (*_clock_gettime)(clockid_t, struct timespec *);
    1.60 +  static int (*_pthread_getcpuclockid)(pthread_t, clockid_t *);
    1.61 +
    1.62 +  static address   _initial_thread_stack_bottom;
    1.63 +  static uintptr_t _initial_thread_stack_size;
    1.64 +
    1.65 +  static const char *_glibc_version;
    1.66 +  static const char *_libpthread_version;
    1.67 +
    1.68 +  static bool _is_floating_stack;
    1.69 +  static bool _is_NPTL;
    1.70 +  static bool _supports_fast_thread_cpu_time;
    1.71 +
    1.72 +  static GrowableArray<int>* _cpu_to_node;
    1.73 +
    1.74 + protected:
    1.75 +
    1.76 +  static julong _physical_memory;
    1.77 +  static pthread_t _main_thread;
    1.78 +  static Mutex* _createThread_lock;
    1.79 +  static int _page_size;
    1.80 +  static const int _vm_default_page_size;
    1.81 +
    1.82 +  static julong available_memory();
    1.83 +  static julong physical_memory() { return _physical_memory; }
    1.84 +  static void initialize_system_info();
    1.85 +
    1.86 +  static int commit_memory_impl(char* addr, size_t bytes, bool exec);
    1.87 +  static int commit_memory_impl(char* addr, size_t bytes,
    1.88 +                                size_t alignment_hint, bool exec);
    1.89 +
    1.90 +  static void set_glibc_version(const char *s)      { _glibc_version = s; }
    1.91 +  static void set_libpthread_version(const char *s) { _libpthread_version = s; }
    1.92 +
    1.93 +  static bool supports_variable_stack_size();
    1.94 +
    1.95 +  static void set_is_NPTL()                   { _is_NPTL = true;  }
    1.96 +  static void set_is_LinuxThreads()           { _is_NPTL = false; }
    1.97 +  static void set_is_floating_stack()         { _is_floating_stack = true; }
    1.98 +
    1.99 +  static void rebuild_cpu_to_node_map();
   1.100 +  static GrowableArray<int>* cpu_to_node()    { return _cpu_to_node; }
   1.101 +
   1.102 +  static size_t find_large_page_size();
   1.103 +  static size_t setup_large_page_size();
   1.104 +
   1.105 +  static bool setup_large_page_type(size_t page_size);
   1.106 +  static bool transparent_huge_pages_sanity_check(bool warn, size_t pages_size);
   1.107 +  static bool hugetlbfs_sanity_check(bool warn, size_t page_size);
   1.108 +
   1.109 +  static char* reserve_memory_special_shm(size_t bytes, size_t alignment, char* req_addr, bool exec);
   1.110 +  static char* reserve_memory_special_huge_tlbfs(size_t bytes, size_t alignment, char* req_addr, bool exec);
   1.111 +  static char* reserve_memory_special_huge_tlbfs_only(size_t bytes, char* req_addr, bool exec);
   1.112 +  static char* reserve_memory_special_huge_tlbfs_mixed(size_t bytes, size_t alignment, char* req_addr, bool exec);
   1.113 +
   1.114 +  static bool release_memory_special_shm(char* base, size_t bytes);
   1.115 +  static bool release_memory_special_huge_tlbfs(char* base, size_t bytes);
   1.116 +
   1.117 +  static void print_full_memory_info(outputStream* st);
   1.118 +  static void print_distro_info(outputStream* st);
   1.119 +  static void print_libversion_info(outputStream* st);
   1.120 +
   1.121 + public:
   1.122 +  static bool _stack_is_executable;
   1.123 +  static void *dlopen_helper(const char *name, char *ebuf, int ebuflen);
   1.124 +  static void *dll_load_in_vmthread(const char *name, char *ebuf, int ebuflen);
   1.125 +
   1.126 +  static void init_thread_fpu_state();
   1.127 +  static int  get_fpu_control_word();
   1.128 +  static void set_fpu_control_word(int fpu_control);
   1.129 +  static pthread_t main_thread(void)                                { return _main_thread; }
   1.130 +  // returns kernel thread id (similar to LWP id on Solaris), which can be
   1.131 +  // used to access /proc
   1.132 +  static pid_t gettid();
   1.133 +  static void set_createThread_lock(Mutex* lk)                      { _createThread_lock = lk; }
   1.134 +  static Mutex* createThread_lock(void)                             { return _createThread_lock; }
   1.135 +  static void hotspot_sigmask(Thread* thread);
   1.136 +
   1.137 +  static address   initial_thread_stack_bottom(void)                { return _initial_thread_stack_bottom; }
   1.138 +  static uintptr_t initial_thread_stack_size(void)                  { return _initial_thread_stack_size; }
   1.139 +  static bool is_initial_thread(void);
   1.140 +
   1.141 +  static int page_size(void)                                        { return _page_size; }
   1.142 +  static void set_page_size(int val)                                { _page_size = val; }
   1.143 +
   1.144 +  static int vm_default_page_size(void)                             { return _vm_default_page_size; }
   1.145 +
   1.146 +  static address   ucontext_get_pc(ucontext_t* uc);
   1.147 +  static intptr_t* ucontext_get_sp(ucontext_t* uc);
   1.148 +  static intptr_t* ucontext_get_fp(ucontext_t* uc);
   1.149 +
   1.150 +  // For Analyzer Forte AsyncGetCallTrace profiling support:
   1.151 +  //
   1.152 +  // This interface should be declared in os_linux_i486.hpp, but
   1.153 +  // that file provides extensions to the os class and not the
   1.154 +  // Linux class.
   1.155 +  static ExtendedPC fetch_frame_from_ucontext(Thread* thread, ucontext_t* uc,
   1.156 +    intptr_t** ret_sp, intptr_t** ret_fp);
   1.157 +
   1.158 +  // This boolean allows users to forward their own non-matching signals
   1.159 +  // to JVM_handle_linux_signal, harmlessly.
   1.160 +  static bool signal_handlers_are_installed;
   1.161 +
   1.162 +  static int get_our_sigflags(int);
   1.163 +  static void set_our_sigflags(int, int);
   1.164 +  static void signal_sets_init();
   1.165 +  static void install_signal_handlers();
   1.166 +  static void set_signal_handler(int, bool);
   1.167 +  static bool is_sig_ignored(int sig);
   1.168 +
   1.169 +  static sigset_t* unblocked_signals();
   1.170 +  static sigset_t* vm_signals();
   1.171 +  static sigset_t* allowdebug_blocked_signals();
   1.172 +
   1.173 +  // For signal-chaining
   1.174 +  static struct sigaction *get_chained_signal_action(int sig);
   1.175 +  static bool chained_handler(int sig, siginfo_t* siginfo, void* context);
   1.176 +
   1.177 +  // GNU libc and libpthread version strings
   1.178 +  static const char *glibc_version()          { return _glibc_version; }
   1.179 +  static const char *libpthread_version()     { return _libpthread_version; }
   1.180 +
   1.181 +  // NPTL or LinuxThreads?
   1.182 +  static bool is_LinuxThreads()               { return !_is_NPTL; }
   1.183 +  static bool is_NPTL()                       { return _is_NPTL;  }
   1.184 +
   1.185 +  // NPTL is always floating stack. LinuxThreads could be using floating
   1.186 +  // stack or fixed stack.
   1.187 +  static bool is_floating_stack()             { return _is_floating_stack; }
   1.188 +
   1.189 +  static void libpthread_init();
   1.190 +  static bool libnuma_init();
   1.191 +  static void* libnuma_dlsym(void* handle, const char* name);
   1.192 +  // Minimum stack size a thread can be created with (allowing
   1.193 +  // the VM to completely create the thread and enter user code)
   1.194 +  static size_t min_stack_allowed;
   1.195 +
   1.196 +  // Return default stack size or guard size for the specified thread type
   1.197 +  static size_t default_stack_size(os::ThreadType thr_type);
   1.198 +  static size_t default_guard_size(os::ThreadType thr_type);
   1.199 +
   1.200 +  static void capture_initial_stack(size_t max_size);
   1.201 +
   1.202 +  // Stack overflow handling
   1.203 +  static bool manually_expand_stack(JavaThread * t, address addr);
   1.204 +  static int max_register_window_saves_before_flushing();
   1.205 +
   1.206 +  // Real-time clock functions
   1.207 +  static void clock_init(void);
   1.208 +
   1.209 +  // fast POSIX clocks support
   1.210 +  static void fast_thread_clock_init(void);
   1.211 +
   1.212 +  static inline bool supports_monotonic_clock() {
   1.213 +    return _clock_gettime != NULL;
   1.214 +  }
   1.215 +
   1.216 +  static int clock_gettime(clockid_t clock_id, struct timespec *tp) {
   1.217 +    return _clock_gettime ? _clock_gettime(clock_id, tp) : -1;
   1.218 +  }
   1.219 +
   1.220 +  static int pthread_getcpuclockid(pthread_t tid, clockid_t *clock_id) {
   1.221 +    return _pthread_getcpuclockid ? _pthread_getcpuclockid(tid, clock_id) : -1;
   1.222 +  }
   1.223 +
   1.224 +  static bool supports_fast_thread_cpu_time() {
   1.225 +    return _supports_fast_thread_cpu_time;
   1.226 +  }
   1.227 +
   1.228 +  static jlong fast_thread_cpu_time(clockid_t clockid);
   1.229 +
   1.230 +  // pthread_cond clock suppport
   1.231 +  private:
   1.232 +  static pthread_condattr_t _condattr[1];
   1.233 +
   1.234 +  public:
   1.235 +  static pthread_condattr_t* condAttr() { return _condattr; }
   1.236 +
   1.237 +  // Stack repair handling
   1.238 +
   1.239 +  // none present
   1.240 +
   1.241 +  // LinuxThreads work-around for 6292965
   1.242 +  static int safe_cond_timedwait(pthread_cond_t *_cond, pthread_mutex_t *_mutex, const struct timespec *_abstime);
   1.243 +
   1.244 +private:
   1.245 +  typedef int (*sched_getcpu_func_t)(void);
   1.246 +  typedef int (*numa_node_to_cpus_func_t)(int node, unsigned long *buffer, int bufferlen);
   1.247 +  typedef int (*numa_max_node_func_t)(void);
   1.248 +  typedef int (*numa_available_func_t)(void);
   1.249 +  typedef int (*numa_tonode_memory_func_t)(void *start, size_t size, int node);
   1.250 +  typedef void (*numa_interleave_memory_func_t)(void *start, size_t size, unsigned long *nodemask);
   1.251 +  typedef void (*numa_set_bind_policy_func_t)(int policy);
   1.252 +
   1.253 +  static sched_getcpu_func_t _sched_getcpu;
   1.254 +  static numa_node_to_cpus_func_t _numa_node_to_cpus;
   1.255 +  static numa_max_node_func_t _numa_max_node;
   1.256 +  static numa_available_func_t _numa_available;
   1.257 +  static numa_tonode_memory_func_t _numa_tonode_memory;
   1.258 +  static numa_interleave_memory_func_t _numa_interleave_memory;
   1.259 +  static numa_set_bind_policy_func_t _numa_set_bind_policy;
   1.260 +  static unsigned long* _numa_all_nodes;
   1.261 +
   1.262 +  static void set_sched_getcpu(sched_getcpu_func_t func) { _sched_getcpu = func; }
   1.263 +  static void set_numa_node_to_cpus(numa_node_to_cpus_func_t func) { _numa_node_to_cpus = func; }
   1.264 +  static void set_numa_max_node(numa_max_node_func_t func) { _numa_max_node = func; }
   1.265 +  static void set_numa_available(numa_available_func_t func) { _numa_available = func; }
   1.266 +  static void set_numa_tonode_memory(numa_tonode_memory_func_t func) { _numa_tonode_memory = func; }
   1.267 +  static void set_numa_interleave_memory(numa_interleave_memory_func_t func) { _numa_interleave_memory = func; }
   1.268 +  static void set_numa_set_bind_policy(numa_set_bind_policy_func_t func) { _numa_set_bind_policy = func; }
   1.269 +  static void set_numa_all_nodes(unsigned long* ptr) { _numa_all_nodes = ptr; }
   1.270 +  static int sched_getcpu_syscall(void);
   1.271 +public:
   1.272 +  static int sched_getcpu()  { return _sched_getcpu != NULL ? _sched_getcpu() : -1; }
   1.273 +  static int numa_node_to_cpus(int node, unsigned long *buffer, int bufferlen) {
   1.274 +    return _numa_node_to_cpus != NULL ? _numa_node_to_cpus(node, buffer, bufferlen) : -1;
   1.275 +  }
   1.276 +  static int numa_max_node() { return _numa_max_node != NULL ? _numa_max_node() : -1; }
   1.277 +  static int numa_available() { return _numa_available != NULL ? _numa_available() : -1; }
   1.278 +  static int numa_tonode_memory(void *start, size_t size, int node) {
   1.279 +    return _numa_tonode_memory != NULL ? _numa_tonode_memory(start, size, node) : -1;
   1.280 +  }
   1.281 +  static void numa_interleave_memory(void *start, size_t size) {
   1.282 +    if (_numa_interleave_memory != NULL && _numa_all_nodes != NULL) {
   1.283 +      _numa_interleave_memory(start, size, _numa_all_nodes);
   1.284 +    }
   1.285 +  }
   1.286 +  static void numa_set_bind_policy(int policy) {
   1.287 +    if (_numa_set_bind_policy != NULL) {
   1.288 +      _numa_set_bind_policy(policy);
   1.289 +    }
   1.290 +  }
   1.291 +  static int get_node_by_cpu(int cpu_id);
   1.292 +};
   1.293 +
   1.294 +
   1.295 +class PlatformEvent : public CHeapObj<mtInternal> {
   1.296 +  private:
   1.297 +    double CachePad [4] ;   // increase odds that _mutex is sole occupant of cache line
   1.298 +    volatile int _Event ;
   1.299 +    volatile int _nParked ;
   1.300 +    pthread_mutex_t _mutex  [1] ;
   1.301 +    pthread_cond_t  _cond   [1] ;
   1.302 +    double PostPad  [2] ;
   1.303 +    Thread * _Assoc ;
   1.304 +
   1.305 +  public:       // TODO-FIXME: make dtor private
   1.306 +    ~PlatformEvent() { guarantee (0, "invariant") ; }
   1.307 +
   1.308 +  public:
   1.309 +    PlatformEvent() {
   1.310 +      int status;
   1.311 +      status = pthread_cond_init (_cond, os::Linux::condAttr());
   1.312 +      assert_status(status == 0, status, "cond_init");
   1.313 +      status = pthread_mutex_init (_mutex, NULL);
   1.314 +      assert_status(status == 0, status, "mutex_init");
   1.315 +      _Event   = 0 ;
   1.316 +      _nParked = 0 ;
   1.317 +      _Assoc   = NULL ;
   1.318 +    }
   1.319 +
   1.320 +    // Use caution with reset() and fired() -- they may require MEMBARs
   1.321 +    void reset() { _Event = 0 ; }
   1.322 +    int  fired() { return _Event; }
   1.323 +    void park () ;
   1.324 +    void unpark () ;
   1.325 +    int  TryPark () ;
   1.326 +    int  park (jlong millis) ; // relative timed-wait only
   1.327 +    void SetAssociation (Thread * a) { _Assoc = a ; }
   1.328 +} ;
   1.329 +
   1.330 +class PlatformParker : public CHeapObj<mtInternal> {
   1.331 +  protected:
   1.332 +    enum {
   1.333 +        REL_INDEX = 0,
   1.334 +        ABS_INDEX = 1
   1.335 +    };
   1.336 +    int _cur_index;  // which cond is in use: -1, 0, 1
   1.337 +    pthread_mutex_t _mutex [1] ;
   1.338 +    pthread_cond_t  _cond  [2] ; // one for relative times and one for abs.
   1.339 +
   1.340 +  public:       // TODO-FIXME: make dtor private
   1.341 +    ~PlatformParker() { guarantee (0, "invariant") ; }
   1.342 +
   1.343 +  public:
   1.344 +    PlatformParker() {
   1.345 +      int status;
   1.346 +      status = pthread_cond_init (&_cond[REL_INDEX], os::Linux::condAttr());
   1.347 +      assert_status(status == 0, status, "cond_init rel");
   1.348 +      status = pthread_cond_init (&_cond[ABS_INDEX], NULL);
   1.349 +      assert_status(status == 0, status, "cond_init abs");
   1.350 +      status = pthread_mutex_init (_mutex, NULL);
   1.351 +      assert_status(status == 0, status, "mutex_init");
   1.352 +      _cur_index = -1; // mark as unused
   1.353 +    }
   1.354 +};
   1.355 +
   1.356 +#endif // OS_LINUX_VM_OS_LINUX_HPP

mercurial