src/os/linux/vm/os_linux.hpp

Tue, 28 Jul 2009 12:12:40 -0700

author
xdono
date
Tue, 28 Jul 2009 12:12:40 -0700
changeset 1279
bd02caa94611
parent 1198
cf71f149d7ae
child 1907
c18cbe5936b8
permissions
-rw-r--r--

6862919: Update copyright year
Summary: Update copyright for files that have been modified in 2009, up to 07/09
Reviewed-by: tbell, ohair

duke@435 1 /*
xdono@1279 2 * Copyright 1999-2009 Sun Microsystems, Inc. All Rights Reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 // Linux_OS defines the interface to Linux operating systems
duke@435 26
duke@435 27 /* pthread_getattr_np comes with LinuxThreads-0.9-7 on RedHat 7.1 */
duke@435 28 typedef int (*pthread_getattr_func_type) (pthread_t, pthread_attr_t *);
duke@435 29
duke@435 30 class Linux {
duke@435 31 friend class os;
duke@435 32
duke@435 33 // For signal-chaining
duke@435 34 #define MAXSIGNUM 32
duke@435 35 static struct sigaction sigact[MAXSIGNUM]; // saved preinstalled sigactions
duke@435 36 static unsigned int sigs; // mask of signals that have
duke@435 37 // preinstalled signal handlers
duke@435 38 static bool libjsig_is_loaded; // libjsig that interposes sigaction(),
duke@435 39 // __sigaction(), signal() is loaded
duke@435 40 static struct sigaction *(*get_signal_action)(int);
duke@435 41 static struct sigaction *get_preinstalled_handler(int);
duke@435 42 static void save_preinstalled_handler(int, struct sigaction&);
duke@435 43
duke@435 44 static void check_signal_handler(int sig);
duke@435 45
duke@435 46 // For signal flags diagnostics
duke@435 47 static int sigflags[MAXSIGNUM];
duke@435 48
duke@435 49 static int (*_clock_gettime)(clockid_t, struct timespec *);
duke@435 50 static int (*_pthread_getcpuclockid)(pthread_t, clockid_t *);
duke@435 51
duke@435 52 static address _initial_thread_stack_bottom;
duke@435 53 static uintptr_t _initial_thread_stack_size;
duke@435 54
xlu@634 55 static const char *_glibc_version;
xlu@634 56 static const char *_libpthread_version;
duke@435 57
duke@435 58 static bool _is_floating_stack;
duke@435 59 static bool _is_NPTL;
duke@435 60 static bool _supports_fast_thread_cpu_time;
duke@435 61
iveresov@576 62 static GrowableArray<int>* _cpu_to_node;
iveresov@576 63
duke@435 64 protected:
duke@435 65
duke@435 66 static julong _physical_memory;
duke@435 67 static pthread_t _main_thread;
duke@435 68 static Mutex* _createThread_lock;
duke@435 69 static int _page_size;
duke@435 70
duke@435 71 static julong available_memory();
duke@435 72 static julong physical_memory() { return _physical_memory; }
duke@435 73 static void initialize_system_info();
duke@435 74
xlu@634 75 static void set_glibc_version(const char *s) { _glibc_version = s; }
xlu@634 76 static void set_libpthread_version(const char *s) { _libpthread_version = s; }
duke@435 77
duke@435 78 static bool supports_variable_stack_size();
duke@435 79
duke@435 80 static void set_is_NPTL() { _is_NPTL = true; }
duke@435 81 static void set_is_LinuxThreads() { _is_NPTL = false; }
duke@435 82 static void set_is_floating_stack() { _is_floating_stack = true; }
duke@435 83
iveresov@576 84 static void rebuild_cpu_to_node_map();
iveresov@576 85 static GrowableArray<int>* cpu_to_node() { return _cpu_to_node; }
duke@435 86 public:
duke@435 87 static void init_thread_fpu_state();
duke@435 88 static int get_fpu_control_word();
duke@435 89 static void set_fpu_control_word(int fpu_control);
duke@435 90 static pthread_t main_thread(void) { return _main_thread; }
duke@435 91 // returns kernel thread id (similar to LWP id on Solaris), which can be
duke@435 92 // used to access /proc
duke@435 93 static pid_t gettid();
duke@435 94 static void set_createThread_lock(Mutex* lk) { _createThread_lock = lk; }
duke@435 95 static Mutex* createThread_lock(void) { return _createThread_lock; }
duke@435 96 static void hotspot_sigmask(Thread* thread);
duke@435 97
duke@435 98 static address initial_thread_stack_bottom(void) { return _initial_thread_stack_bottom; }
duke@435 99 static uintptr_t initial_thread_stack_size(void) { return _initial_thread_stack_size; }
duke@435 100 static bool is_initial_thread(void);
duke@435 101
duke@435 102 static int page_size(void) { return _page_size; }
duke@435 103 static void set_page_size(int val) { _page_size = val; }
duke@435 104
duke@435 105 static address ucontext_get_pc(ucontext_t* uc);
duke@435 106 static intptr_t* ucontext_get_sp(ucontext_t* uc);
duke@435 107 static intptr_t* ucontext_get_fp(ucontext_t* uc);
duke@435 108
duke@435 109 // For Analyzer Forte AsyncGetCallTrace profiling support:
duke@435 110 //
duke@435 111 // This interface should be declared in os_linux_i486.hpp, but
duke@435 112 // that file provides extensions to the os class and not the
duke@435 113 // Linux class.
duke@435 114 static ExtendedPC fetch_frame_from_ucontext(Thread* thread, ucontext_t* uc,
duke@435 115 intptr_t** ret_sp, intptr_t** ret_fp);
duke@435 116
duke@435 117 // This boolean allows users to forward their own non-matching signals
duke@435 118 // to JVM_handle_linux_signal, harmlessly.
duke@435 119 static bool signal_handlers_are_installed;
duke@435 120
duke@435 121 static int get_our_sigflags(int);
duke@435 122 static void set_our_sigflags(int, int);
duke@435 123 static void signal_sets_init();
duke@435 124 static void install_signal_handlers();
duke@435 125 static void set_signal_handler(int, bool);
duke@435 126 static bool is_sig_ignored(int sig);
duke@435 127
duke@435 128 static sigset_t* unblocked_signals();
duke@435 129 static sigset_t* vm_signals();
duke@435 130 static sigset_t* allowdebug_blocked_signals();
duke@435 131
duke@435 132 // For signal-chaining
duke@435 133 static struct sigaction *get_chained_signal_action(int sig);
duke@435 134 static bool chained_handler(int sig, siginfo_t* siginfo, void* context);
duke@435 135
duke@435 136 // GNU libc and libpthread version strings
xlu@634 137 static const char *glibc_version() { return _glibc_version; }
xlu@634 138 static const char *libpthread_version() { return _libpthread_version; }
duke@435 139
duke@435 140 // NPTL or LinuxThreads?
duke@435 141 static bool is_LinuxThreads() { return !_is_NPTL; }
duke@435 142 static bool is_NPTL() { return _is_NPTL; }
duke@435 143
duke@435 144 // NPTL is always floating stack. LinuxThreads could be using floating
duke@435 145 // stack or fixed stack.
duke@435 146 static bool is_floating_stack() { return _is_floating_stack; }
duke@435 147
duke@435 148 static void libpthread_init();
iveresov@897 149 static bool libnuma_init();
iveresov@1198 150 static void* libnuma_dlsym(void* handle, const char* name);
duke@435 151 // Minimum stack size a thread can be created with (allowing
duke@435 152 // the VM to completely create the thread and enter user code)
duke@435 153 static size_t min_stack_allowed;
duke@435 154
duke@435 155 // Return default stack size or guard size for the specified thread type
duke@435 156 static size_t default_stack_size(os::ThreadType thr_type);
duke@435 157 static size_t default_guard_size(os::ThreadType thr_type);
duke@435 158
duke@435 159 static void capture_initial_stack(size_t max_size);
duke@435 160
duke@435 161 // Stack overflow handling
duke@435 162 static bool manually_expand_stack(JavaThread * t, address addr);
duke@435 163 static int max_register_window_saves_before_flushing();
duke@435 164
duke@435 165 // Real-time clock functions
duke@435 166 static void clock_init(void);
duke@435 167
duke@435 168 // fast POSIX clocks support
duke@435 169 static void fast_thread_clock_init(void);
duke@435 170
duke@435 171 static bool supports_monotonic_clock() {
duke@435 172 return _clock_gettime != NULL;
duke@435 173 }
duke@435 174
duke@435 175 static int clock_gettime(clockid_t clock_id, struct timespec *tp) {
duke@435 176 return _clock_gettime ? _clock_gettime(clock_id, tp) : -1;
duke@435 177 }
duke@435 178
duke@435 179 static int pthread_getcpuclockid(pthread_t tid, clockid_t *clock_id) {
duke@435 180 return _pthread_getcpuclockid ? _pthread_getcpuclockid(tid, clock_id) : -1;
duke@435 181 }
duke@435 182
duke@435 183 static bool supports_fast_thread_cpu_time() {
duke@435 184 return _supports_fast_thread_cpu_time;
duke@435 185 }
duke@435 186
duke@435 187 static jlong fast_thread_cpu_time(clockid_t clockid);
duke@435 188
duke@435 189 // Stack repair handling
duke@435 190
duke@435 191 // none present
duke@435 192
duke@435 193 // LinuxThreads work-around for 6292965
duke@435 194 static int safe_cond_timedwait(pthread_cond_t *_cond, pthread_mutex_t *_mutex, const struct timespec *_abstime);
duke@435 195
duke@435 196
duke@435 197 // Linux suspend/resume support - this helper is a shadow of its former
duke@435 198 // self now that low-level suspension is barely used, and old workarounds
duke@435 199 // for LinuxThreads are no longer needed.
duke@435 200 class SuspendResume {
duke@435 201 private:
duke@435 202 volatile int _suspend_action;
duke@435 203 // values for suspend_action:
duke@435 204 #define SR_NONE (0x00)
duke@435 205 #define SR_SUSPEND (0x01) // suspend request
duke@435 206 #define SR_CONTINUE (0x02) // resume request
duke@435 207
duke@435 208 volatile jint _state;
duke@435 209 // values for _state: + SR_NONE
duke@435 210 #define SR_SUSPENDED (0x20)
duke@435 211 public:
duke@435 212 SuspendResume() { _suspend_action = SR_NONE; _state = SR_NONE; }
duke@435 213
duke@435 214 int suspend_action() const { return _suspend_action; }
duke@435 215 void set_suspend_action(int x) { _suspend_action = x; }
duke@435 216
duke@435 217 // atomic updates for _state
duke@435 218 void set_suspended() {
duke@435 219 jint temp, temp2;
duke@435 220 do {
duke@435 221 temp = _state;
duke@435 222 temp2 = Atomic::cmpxchg(temp | SR_SUSPENDED, &_state, temp);
duke@435 223 } while (temp2 != temp);
duke@435 224 }
duke@435 225 void clear_suspended() {
duke@435 226 jint temp, temp2;
duke@435 227 do {
duke@435 228 temp = _state;
duke@435 229 temp2 = Atomic::cmpxchg(temp & ~SR_SUSPENDED, &_state, temp);
duke@435 230 } while (temp2 != temp);
duke@435 231 }
duke@435 232 bool is_suspended() { return _state & SR_SUSPENDED; }
duke@435 233
duke@435 234 #undef SR_SUSPENDED
duke@435 235 };
iveresov@576 236
iveresov@576 237 private:
iveresov@576 238 typedef int (*sched_getcpu_func_t)(void);
iveresov@576 239 typedef int (*numa_node_to_cpus_func_t)(int node, unsigned long *buffer, int bufferlen);
iveresov@576 240 typedef int (*numa_max_node_func_t)(void);
iveresov@576 241 typedef int (*numa_available_func_t)(void);
iveresov@576 242 typedef int (*numa_tonode_memory_func_t)(void *start, size_t size, int node);
iveresov@897 243 typedef void (*numa_interleave_memory_func_t)(void *start, size_t size, unsigned long *nodemask);
iveresov@576 244
iveresov@576 245 static sched_getcpu_func_t _sched_getcpu;
iveresov@576 246 static numa_node_to_cpus_func_t _numa_node_to_cpus;
iveresov@576 247 static numa_max_node_func_t _numa_max_node;
iveresov@576 248 static numa_available_func_t _numa_available;
iveresov@576 249 static numa_tonode_memory_func_t _numa_tonode_memory;
iveresov@897 250 static numa_interleave_memory_func_t _numa_interleave_memory;
iveresov@897 251 static unsigned long* _numa_all_nodes;
iveresov@576 252
iveresov@576 253 static void set_sched_getcpu(sched_getcpu_func_t func) { _sched_getcpu = func; }
iveresov@576 254 static void set_numa_node_to_cpus(numa_node_to_cpus_func_t func) { _numa_node_to_cpus = func; }
iveresov@576 255 static void set_numa_max_node(numa_max_node_func_t func) { _numa_max_node = func; }
iveresov@576 256 static void set_numa_available(numa_available_func_t func) { _numa_available = func; }
iveresov@576 257 static void set_numa_tonode_memory(numa_tonode_memory_func_t func) { _numa_tonode_memory = func; }
iveresov@897 258 static void set_numa_interleave_memory(numa_interleave_memory_func_t func) { _numa_interleave_memory = func; }
iveresov@897 259 static void set_numa_all_nodes(unsigned long* ptr) { _numa_all_nodes = ptr; }
iveresov@576 260 public:
iveresov@576 261 static int sched_getcpu() { return _sched_getcpu != NULL ? _sched_getcpu() : -1; }
iveresov@576 262 static int numa_node_to_cpus(int node, unsigned long *buffer, int bufferlen) {
iveresov@576 263 return _numa_node_to_cpus != NULL ? _numa_node_to_cpus(node, buffer, bufferlen) : -1;
iveresov@576 264 }
iveresov@576 265 static int numa_max_node() { return _numa_max_node != NULL ? _numa_max_node() : -1; }
iveresov@576 266 static int numa_available() { return _numa_available != NULL ? _numa_available() : -1; }
iveresov@576 267 static int numa_tonode_memory(void *start, size_t size, int node) {
iveresov@576 268 return _numa_tonode_memory != NULL ? _numa_tonode_memory(start, size, node) : -1;
iveresov@576 269 }
iveresov@897 270 static void numa_interleave_memory(void *start, size_t size) {
iveresov@897 271 if (_numa_interleave_memory != NULL && _numa_all_nodes != NULL) {
iveresov@897 272 _numa_interleave_memory(start, size, _numa_all_nodes);
iveresov@897 273 }
iveresov@897 274 }
iveresov@576 275 static int get_node_by_cpu(int cpu_id);
duke@435 276 };
duke@435 277
duke@435 278
duke@435 279 class PlatformEvent : public CHeapObj {
duke@435 280 private:
duke@435 281 double CachePad [4] ; // increase odds that _mutex is sole occupant of cache line
duke@435 282 volatile int _Event ;
duke@435 283 volatile int _nParked ;
duke@435 284 pthread_mutex_t _mutex [1] ;
duke@435 285 pthread_cond_t _cond [1] ;
duke@435 286 double PostPad [2] ;
duke@435 287 Thread * _Assoc ;
duke@435 288
duke@435 289 public: // TODO-FIXME: make dtor private
duke@435 290 ~PlatformEvent() { guarantee (0, "invariant") ; }
duke@435 291
duke@435 292 public:
duke@435 293 PlatformEvent() {
duke@435 294 int status;
duke@435 295 status = pthread_cond_init (_cond, NULL);
duke@435 296 assert_status(status == 0, status, "cond_init");
duke@435 297 status = pthread_mutex_init (_mutex, NULL);
duke@435 298 assert_status(status == 0, status, "mutex_init");
duke@435 299 _Event = 0 ;
duke@435 300 _nParked = 0 ;
duke@435 301 _Assoc = NULL ;
duke@435 302 }
duke@435 303
duke@435 304 // Use caution with reset() and fired() -- they may require MEMBARs
duke@435 305 void reset() { _Event = 0 ; }
duke@435 306 int fired() { return _Event; }
duke@435 307 void park () ;
duke@435 308 void unpark () ;
duke@435 309 int TryPark () ;
duke@435 310 int park (jlong millis) ;
duke@435 311 void SetAssociation (Thread * a) { _Assoc = a ; }
duke@435 312 } ;
duke@435 313
duke@435 314 class PlatformParker : public CHeapObj {
duke@435 315 protected:
duke@435 316 pthread_mutex_t _mutex [1] ;
duke@435 317 pthread_cond_t _cond [1] ;
duke@435 318
duke@435 319 public: // TODO-FIXME: make dtor private
duke@435 320 ~PlatformParker() { guarantee (0, "invariant") ; }
duke@435 321
duke@435 322 public:
duke@435 323 PlatformParker() {
duke@435 324 int status;
duke@435 325 status = pthread_cond_init (_cond, NULL);
duke@435 326 assert_status(status == 0, status, "cond_init");
duke@435 327 status = pthread_mutex_init (_mutex, NULL);
duke@435 328 assert_status(status == 0, status, "mutex_init");
duke@435 329 }
duke@435 330 } ;

mercurial