os_linux.hpp revision 1472
0N/A/*
1472N/A * Copyright (c) 1999, 2009, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
0N/A// Linux_OS defines the interface to Linux operating systems
0N/A
0N/A/* pthread_getattr_np comes with LinuxThreads-0.9-7 on RedHat 7.1 */
0N/Atypedef int (*pthread_getattr_func_type) (pthread_t, pthread_attr_t *);
0N/A
0N/Aclass Linux {
0N/A friend class os;
0N/A
0N/A // For signal-chaining
0N/A#define MAXSIGNUM 32
0N/A static struct sigaction sigact[MAXSIGNUM]; // saved preinstalled sigactions
0N/A static unsigned int sigs; // mask of signals that have
0N/A // preinstalled signal handlers
0N/A static bool libjsig_is_loaded; // libjsig that interposes sigaction(),
0N/A // __sigaction(), signal() is loaded
0N/A static struct sigaction *(*get_signal_action)(int);
0N/A static struct sigaction *get_preinstalled_handler(int);
0N/A static void save_preinstalled_handler(int, struct sigaction&);
0N/A
0N/A static void check_signal_handler(int sig);
0N/A
0N/A // For signal flags diagnostics
0N/A static int sigflags[MAXSIGNUM];
0N/A
0N/A static int (*_clock_gettime)(clockid_t, struct timespec *);
0N/A static int (*_pthread_getcpuclockid)(pthread_t, clockid_t *);
0N/A
0N/A static address _initial_thread_stack_bottom;
0N/A static uintptr_t _initial_thread_stack_size;
0N/A
199N/A static const char *_glibc_version;
199N/A static const char *_libpthread_version;
0N/A
0N/A static bool _is_floating_stack;
0N/A static bool _is_NPTL;
0N/A static bool _supports_fast_thread_cpu_time;
0N/A
141N/A static GrowableArray<int>* _cpu_to_node;
141N/A
0N/A protected:
0N/A
0N/A static julong _physical_memory;
0N/A static pthread_t _main_thread;
0N/A static Mutex* _createThread_lock;
0N/A static int _page_size;
0N/A
0N/A static julong available_memory();
0N/A static julong physical_memory() { return _physical_memory; }
0N/A static void initialize_system_info();
0N/A
199N/A static void set_glibc_version(const char *s) { _glibc_version = s; }
199N/A static void set_libpthread_version(const char *s) { _libpthread_version = s; }
0N/A
0N/A static bool supports_variable_stack_size();
0N/A
0N/A static void set_is_NPTL() { _is_NPTL = true; }
0N/A static void set_is_LinuxThreads() { _is_NPTL = false; }
0N/A static void set_is_floating_stack() { _is_floating_stack = true; }
0N/A
141N/A static void rebuild_cpu_to_node_map();
141N/A static GrowableArray<int>* cpu_to_node() { return _cpu_to_node; }
0N/A public:
0N/A static void init_thread_fpu_state();
0N/A static int get_fpu_control_word();
0N/A static void set_fpu_control_word(int fpu_control);
0N/A static pthread_t main_thread(void) { return _main_thread; }
0N/A // returns kernel thread id (similar to LWP id on Solaris), which can be
0N/A // used to access /proc
0N/A static pid_t gettid();
0N/A static void set_createThread_lock(Mutex* lk) { _createThread_lock = lk; }
0N/A static Mutex* createThread_lock(void) { return _createThread_lock; }
0N/A static void hotspot_sigmask(Thread* thread);
0N/A
0N/A static address initial_thread_stack_bottom(void) { return _initial_thread_stack_bottom; }
0N/A static uintptr_t initial_thread_stack_size(void) { return _initial_thread_stack_size; }
0N/A static bool is_initial_thread(void);
0N/A
0N/A static int page_size(void) { return _page_size; }
0N/A static void set_page_size(int val) { _page_size = val; }
0N/A
0N/A static address ucontext_get_pc(ucontext_t* uc);
0N/A static intptr_t* ucontext_get_sp(ucontext_t* uc);
0N/A static intptr_t* ucontext_get_fp(ucontext_t* uc);
0N/A
0N/A // For Analyzer Forte AsyncGetCallTrace profiling support:
0N/A //
0N/A // This interface should be declared in os_linux_i486.hpp, but
0N/A // that file provides extensions to the os class and not the
0N/A // Linux class.
0N/A static ExtendedPC fetch_frame_from_ucontext(Thread* thread, ucontext_t* uc,
0N/A intptr_t** ret_sp, intptr_t** ret_fp);
0N/A
0N/A // This boolean allows users to forward their own non-matching signals
0N/A // to JVM_handle_linux_signal, harmlessly.
0N/A static bool signal_handlers_are_installed;
0N/A
0N/A static int get_our_sigflags(int);
0N/A static void set_our_sigflags(int, int);
0N/A static void signal_sets_init();
0N/A static void install_signal_handlers();
0N/A static void set_signal_handler(int, bool);
0N/A static bool is_sig_ignored(int sig);
0N/A
0N/A static sigset_t* unblocked_signals();
0N/A static sigset_t* vm_signals();
0N/A static sigset_t* allowdebug_blocked_signals();
0N/A
0N/A // For signal-chaining
0N/A static struct sigaction *get_chained_signal_action(int sig);
0N/A static bool chained_handler(int sig, siginfo_t* siginfo, void* context);
0N/A
0N/A // GNU libc and libpthread version strings
199N/A static const char *glibc_version() { return _glibc_version; }
199N/A static const char *libpthread_version() { return _libpthread_version; }
0N/A
0N/A // NPTL or LinuxThreads?
0N/A static bool is_LinuxThreads() { return !_is_NPTL; }
0N/A static bool is_NPTL() { return _is_NPTL; }
0N/A
0N/A // NPTL is always floating stack. LinuxThreads could be using floating
0N/A // stack or fixed stack.
0N/A static bool is_floating_stack() { return _is_floating_stack; }
0N/A
0N/A static void libpthread_init();
462N/A static bool libnuma_init();
763N/A static void* libnuma_dlsym(void* handle, const char* name);
0N/A // Minimum stack size a thread can be created with (allowing
0N/A // the VM to completely create the thread and enter user code)
0N/A static size_t min_stack_allowed;
0N/A
0N/A // Return default stack size or guard size for the specified thread type
0N/A static size_t default_stack_size(os::ThreadType thr_type);
0N/A static size_t default_guard_size(os::ThreadType thr_type);
0N/A
0N/A static void capture_initial_stack(size_t max_size);
0N/A
0N/A // Stack overflow handling
0N/A static bool manually_expand_stack(JavaThread * t, address addr);
0N/A static int max_register_window_saves_before_flushing();
0N/A
0N/A // Real-time clock functions
0N/A static void clock_init(void);
0N/A
0N/A // fast POSIX clocks support
0N/A static void fast_thread_clock_init(void);
0N/A
0N/A static bool supports_monotonic_clock() {
0N/A return _clock_gettime != NULL;
0N/A }
0N/A
0N/A static int clock_gettime(clockid_t clock_id, struct timespec *tp) {
0N/A return _clock_gettime ? _clock_gettime(clock_id, tp) : -1;
0N/A }
0N/A
0N/A static int pthread_getcpuclockid(pthread_t tid, clockid_t *clock_id) {
0N/A return _pthread_getcpuclockid ? _pthread_getcpuclockid(tid, clock_id) : -1;
0N/A }
0N/A
0N/A static bool supports_fast_thread_cpu_time() {
0N/A return _supports_fast_thread_cpu_time;
0N/A }
0N/A
0N/A static jlong fast_thread_cpu_time(clockid_t clockid);
0N/A
0N/A // Stack repair handling
0N/A
0N/A // none present
0N/A
0N/A // LinuxThreads work-around for 6292965
0N/A static int safe_cond_timedwait(pthread_cond_t *_cond, pthread_mutex_t *_mutex, const struct timespec *_abstime);
0N/A
0N/A
0N/A // Linux suspend/resume support - this helper is a shadow of its former
0N/A // self now that low-level suspension is barely used, and old workarounds
0N/A // for LinuxThreads are no longer needed.
0N/A class SuspendResume {
0N/A private:
0N/A volatile int _suspend_action;
0N/A // values for suspend_action:
0N/A #define SR_NONE (0x00)
0N/A #define SR_SUSPEND (0x01) // suspend request
0N/A #define SR_CONTINUE (0x02) // resume request
0N/A
0N/A volatile jint _state;
0N/A // values for _state: + SR_NONE
0N/A #define SR_SUSPENDED (0x20)
0N/A public:
0N/A SuspendResume() { _suspend_action = SR_NONE; _state = SR_NONE; }
0N/A
0N/A int suspend_action() const { return _suspend_action; }
0N/A void set_suspend_action(int x) { _suspend_action = x; }
0N/A
0N/A // atomic updates for _state
0N/A void set_suspended() {
0N/A jint temp, temp2;
0N/A do {
0N/A temp = _state;
0N/A temp2 = Atomic::cmpxchg(temp | SR_SUSPENDED, &_state, temp);
0N/A } while (temp2 != temp);
0N/A }
0N/A void clear_suspended() {
0N/A jint temp, temp2;
0N/A do {
0N/A temp = _state;
0N/A temp2 = Atomic::cmpxchg(temp & ~SR_SUSPENDED, &_state, temp);
0N/A } while (temp2 != temp);
0N/A }
0N/A bool is_suspended() { return _state & SR_SUSPENDED; }
0N/A
0N/A #undef SR_SUSPENDED
0N/A };
141N/A
141N/Aprivate:
141N/A typedef int (*sched_getcpu_func_t)(void);
141N/A typedef int (*numa_node_to_cpus_func_t)(int node, unsigned long *buffer, int bufferlen);
141N/A typedef int (*numa_max_node_func_t)(void);
141N/A typedef int (*numa_available_func_t)(void);
141N/A typedef int (*numa_tonode_memory_func_t)(void *start, size_t size, int node);
462N/A typedef void (*numa_interleave_memory_func_t)(void *start, size_t size, unsigned long *nodemask);
141N/A
141N/A static sched_getcpu_func_t _sched_getcpu;
141N/A static numa_node_to_cpus_func_t _numa_node_to_cpus;
141N/A static numa_max_node_func_t _numa_max_node;
141N/A static numa_available_func_t _numa_available;
141N/A static numa_tonode_memory_func_t _numa_tonode_memory;
462N/A static numa_interleave_memory_func_t _numa_interleave_memory;
462N/A static unsigned long* _numa_all_nodes;
141N/A
141N/A static void set_sched_getcpu(sched_getcpu_func_t func) { _sched_getcpu = func; }
141N/A static void set_numa_node_to_cpus(numa_node_to_cpus_func_t func) { _numa_node_to_cpus = func; }
141N/A static void set_numa_max_node(numa_max_node_func_t func) { _numa_max_node = func; }
141N/A static void set_numa_available(numa_available_func_t func) { _numa_available = func; }
141N/A static void set_numa_tonode_memory(numa_tonode_memory_func_t func) { _numa_tonode_memory = func; }
462N/A static void set_numa_interleave_memory(numa_interleave_memory_func_t func) { _numa_interleave_memory = func; }
462N/A static void set_numa_all_nodes(unsigned long* ptr) { _numa_all_nodes = ptr; }
141N/Apublic:
141N/A static int sched_getcpu() { return _sched_getcpu != NULL ? _sched_getcpu() : -1; }
141N/A static int numa_node_to_cpus(int node, unsigned long *buffer, int bufferlen) {
141N/A return _numa_node_to_cpus != NULL ? _numa_node_to_cpus(node, buffer, bufferlen) : -1;
141N/A }
141N/A static int numa_max_node() { return _numa_max_node != NULL ? _numa_max_node() : -1; }
141N/A static int numa_available() { return _numa_available != NULL ? _numa_available() : -1; }
141N/A static int numa_tonode_memory(void *start, size_t size, int node) {
141N/A return _numa_tonode_memory != NULL ? _numa_tonode_memory(start, size, node) : -1;
141N/A }
462N/A static void numa_interleave_memory(void *start, size_t size) {
462N/A if (_numa_interleave_memory != NULL && _numa_all_nodes != NULL) {
462N/A _numa_interleave_memory(start, size, _numa_all_nodes);
462N/A }
462N/A }
141N/A static int get_node_by_cpu(int cpu_id);
0N/A};
0N/A
0N/A
0N/Aclass PlatformEvent : public CHeapObj {
0N/A private:
0N/A double CachePad [4] ; // increase odds that _mutex is sole occupant of cache line
0N/A volatile int _Event ;
0N/A volatile int _nParked ;
0N/A pthread_mutex_t _mutex [1] ;
0N/A pthread_cond_t _cond [1] ;
0N/A double PostPad [2] ;
0N/A Thread * _Assoc ;
0N/A
0N/A public: // TODO-FIXME: make dtor private
0N/A ~PlatformEvent() { guarantee (0, "invariant") ; }
0N/A
0N/A public:
0N/A PlatformEvent() {
0N/A int status;
0N/A status = pthread_cond_init (_cond, NULL);
0N/A assert_status(status == 0, status, "cond_init");
0N/A status = pthread_mutex_init (_mutex, NULL);
0N/A assert_status(status == 0, status, "mutex_init");
0N/A _Event = 0 ;
0N/A _nParked = 0 ;
0N/A _Assoc = NULL ;
0N/A }
0N/A
0N/A // Use caution with reset() and fired() -- they may require MEMBARs
0N/A void reset() { _Event = 0 ; }
0N/A int fired() { return _Event; }
0N/A void park () ;
0N/A void unpark () ;
0N/A int TryPark () ;
0N/A int park (jlong millis) ;
0N/A void SetAssociation (Thread * a) { _Assoc = a ; }
0N/A} ;
0N/A
0N/Aclass PlatformParker : public CHeapObj {
0N/A protected:
0N/A pthread_mutex_t _mutex [1] ;
0N/A pthread_cond_t _cond [1] ;
0N/A
0N/A public: // TODO-FIXME: make dtor private
0N/A ~PlatformParker() { guarantee (0, "invariant") ; }
0N/A
0N/A public:
0N/A PlatformParker() {
0N/A int status;
0N/A status = pthread_cond_init (_cond, NULL);
0N/A assert_status(status == 0, status, "cond_init");
0N/A status = pthread_mutex_init (_mutex, NULL);
0N/A assert_status(status == 0, status, "mutex_init");
0N/A }
0N/A} ;