Fri, 11 Jul 2008 01:14:44 -0700
Merge
1 /*
2 * Copyright 1999-2008 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
25 // Linux_OS defines the interface to Linux operating systems
27 /* pthread_getattr_np comes with LinuxThreads-0.9-7 on RedHat 7.1 */
28 typedef int (*pthread_getattr_func_type) (pthread_t, pthread_attr_t *);
30 class Linux {
31 friend class os;
33 // For signal-chaining
34 #define MAXSIGNUM 32
35 static struct sigaction sigact[MAXSIGNUM]; // saved preinstalled sigactions
36 static unsigned int sigs; // mask of signals that have
37 // preinstalled signal handlers
38 static bool libjsig_is_loaded; // libjsig that interposes sigaction(),
39 // __sigaction(), signal() is loaded
40 static struct sigaction *(*get_signal_action)(int);
41 static struct sigaction *get_preinstalled_handler(int);
42 static void save_preinstalled_handler(int, struct sigaction&);
44 static void check_signal_handler(int sig);
46 // For signal flags diagnostics
47 static int sigflags[MAXSIGNUM];
49 static int (*_clock_gettime)(clockid_t, struct timespec *);
50 static int (*_pthread_getcpuclockid)(pthread_t, clockid_t *);
52 static address _initial_thread_stack_bottom;
53 static uintptr_t _initial_thread_stack_size;
55 static const char *_glibc_version;
56 static const char *_libpthread_version;
58 static bool _is_floating_stack;
59 static bool _is_NPTL;
60 static bool _supports_fast_thread_cpu_time;
62 static GrowableArray<int>* _cpu_to_node;
64 protected:
66 static julong _physical_memory;
67 static pthread_t _main_thread;
68 static Mutex* _createThread_lock;
69 static int _page_size;
71 static julong available_memory();
72 static julong physical_memory() { return _physical_memory; }
73 static void initialize_system_info();
75 static void set_glibc_version(const char *s) { _glibc_version = s; }
76 static void set_libpthread_version(const char *s) { _libpthread_version = s; }
78 static bool supports_variable_stack_size();
80 static void set_is_NPTL() { _is_NPTL = true; }
81 static void set_is_LinuxThreads() { _is_NPTL = false; }
82 static void set_is_floating_stack() { _is_floating_stack = true; }
84 static void rebuild_cpu_to_node_map();
85 static GrowableArray<int>* cpu_to_node() { return _cpu_to_node; }
86 public:
87 static void init_thread_fpu_state();
88 static int get_fpu_control_word();
89 static void set_fpu_control_word(int fpu_control);
90 static pthread_t main_thread(void) { return _main_thread; }
91 // returns kernel thread id (similar to LWP id on Solaris), which can be
92 // used to access /proc
93 static pid_t gettid();
94 static void set_createThread_lock(Mutex* lk) { _createThread_lock = lk; }
95 static Mutex* createThread_lock(void) { return _createThread_lock; }
96 static void hotspot_sigmask(Thread* thread);
98 static address initial_thread_stack_bottom(void) { return _initial_thread_stack_bottom; }
99 static uintptr_t initial_thread_stack_size(void) { return _initial_thread_stack_size; }
100 static bool is_initial_thread(void);
102 static int page_size(void) { return _page_size; }
103 static void set_page_size(int val) { _page_size = val; }
105 static address ucontext_get_pc(ucontext_t* uc);
106 static intptr_t* ucontext_get_sp(ucontext_t* uc);
107 static intptr_t* ucontext_get_fp(ucontext_t* uc);
109 // For Analyzer Forte AsyncGetCallTrace profiling support:
110 //
111 // This interface should be declared in os_linux_i486.hpp, but
112 // that file provides extensions to the os class and not the
113 // Linux class.
114 static ExtendedPC fetch_frame_from_ucontext(Thread* thread, ucontext_t* uc,
115 intptr_t** ret_sp, intptr_t** ret_fp);
117 // This boolean allows users to forward their own non-matching signals
118 // to JVM_handle_linux_signal, harmlessly.
119 static bool signal_handlers_are_installed;
121 static int get_our_sigflags(int);
122 static void set_our_sigflags(int, int);
123 static void signal_sets_init();
124 static void install_signal_handlers();
125 static void set_signal_handler(int, bool);
126 static bool is_sig_ignored(int sig);
128 static sigset_t* unblocked_signals();
129 static sigset_t* vm_signals();
130 static sigset_t* allowdebug_blocked_signals();
132 // For signal-chaining
133 static struct sigaction *get_chained_signal_action(int sig);
134 static bool chained_handler(int sig, siginfo_t* siginfo, void* context);
136 // GNU libc and libpthread version strings
137 static const char *glibc_version() { return _glibc_version; }
138 static const char *libpthread_version() { return _libpthread_version; }
140 // NPTL or LinuxThreads?
141 static bool is_LinuxThreads() { return !_is_NPTL; }
142 static bool is_NPTL() { return _is_NPTL; }
144 // NPTL is always floating stack. LinuxThreads could be using floating
145 // stack or fixed stack.
146 static bool is_floating_stack() { return _is_floating_stack; }
148 static void libpthread_init();
149 static void libnuma_init();
151 // Minimum stack size a thread can be created with (allowing
152 // the VM to completely create the thread and enter user code)
153 static size_t min_stack_allowed;
155 // Return default stack size or guard size for the specified thread type
156 static size_t default_stack_size(os::ThreadType thr_type);
157 static size_t default_guard_size(os::ThreadType thr_type);
159 static void capture_initial_stack(size_t max_size);
161 // Stack overflow handling
162 static bool manually_expand_stack(JavaThread * t, address addr);
163 static int max_register_window_saves_before_flushing();
165 // Real-time clock functions
166 static void clock_init(void);
168 // fast POSIX clocks support
169 static void fast_thread_clock_init(void);
171 static bool supports_monotonic_clock() {
172 return _clock_gettime != NULL;
173 }
175 static int clock_gettime(clockid_t clock_id, struct timespec *tp) {
176 return _clock_gettime ? _clock_gettime(clock_id, tp) : -1;
177 }
179 static int pthread_getcpuclockid(pthread_t tid, clockid_t *clock_id) {
180 return _pthread_getcpuclockid ? _pthread_getcpuclockid(tid, clock_id) : -1;
181 }
183 static bool supports_fast_thread_cpu_time() {
184 return _supports_fast_thread_cpu_time;
185 }
187 static jlong fast_thread_cpu_time(clockid_t clockid);
189 // Stack repair handling
191 // none present
193 // LinuxThreads work-around for 6292965
194 static int safe_cond_timedwait(pthread_cond_t *_cond, pthread_mutex_t *_mutex, const struct timespec *_abstime);
197 // Linux suspend/resume support - this helper is a shadow of its former
198 // self now that low-level suspension is barely used, and old workarounds
199 // for LinuxThreads are no longer needed.
200 class SuspendResume {
201 private:
202 volatile int _suspend_action;
203 // values for suspend_action:
204 #define SR_NONE (0x00)
205 #define SR_SUSPEND (0x01) // suspend request
206 #define SR_CONTINUE (0x02) // resume request
208 volatile jint _state;
209 // values for _state: + SR_NONE
210 #define SR_SUSPENDED (0x20)
211 public:
212 SuspendResume() { _suspend_action = SR_NONE; _state = SR_NONE; }
214 int suspend_action() const { return _suspend_action; }
215 void set_suspend_action(int x) { _suspend_action = x; }
217 // atomic updates for _state
218 void set_suspended() {
219 jint temp, temp2;
220 do {
221 temp = _state;
222 temp2 = Atomic::cmpxchg(temp | SR_SUSPENDED, &_state, temp);
223 } while (temp2 != temp);
224 }
225 void clear_suspended() {
226 jint temp, temp2;
227 do {
228 temp = _state;
229 temp2 = Atomic::cmpxchg(temp & ~SR_SUSPENDED, &_state, temp);
230 } while (temp2 != temp);
231 }
232 bool is_suspended() { return _state & SR_SUSPENDED; }
234 #undef SR_SUSPENDED
235 };
237 private:
238 typedef int (*sched_getcpu_func_t)(void);
239 typedef int (*numa_node_to_cpus_func_t)(int node, unsigned long *buffer, int bufferlen);
240 typedef int (*numa_max_node_func_t)(void);
241 typedef int (*numa_available_func_t)(void);
242 typedef int (*numa_tonode_memory_func_t)(void *start, size_t size, int node);
245 static sched_getcpu_func_t _sched_getcpu;
246 static numa_node_to_cpus_func_t _numa_node_to_cpus;
247 static numa_max_node_func_t _numa_max_node;
248 static numa_available_func_t _numa_available;
249 static numa_tonode_memory_func_t _numa_tonode_memory;
251 static void set_sched_getcpu(sched_getcpu_func_t func) { _sched_getcpu = func; }
252 static void set_numa_node_to_cpus(numa_node_to_cpus_func_t func) { _numa_node_to_cpus = func; }
253 static void set_numa_max_node(numa_max_node_func_t func) { _numa_max_node = func; }
254 static void set_numa_available(numa_available_func_t func) { _numa_available = func; }
255 static void set_numa_tonode_memory(numa_tonode_memory_func_t func) { _numa_tonode_memory = func; }
257 public:
258 static int sched_getcpu() { return _sched_getcpu != NULL ? _sched_getcpu() : -1; }
259 static int numa_node_to_cpus(int node, unsigned long *buffer, int bufferlen) {
260 return _numa_node_to_cpus != NULL ? _numa_node_to_cpus(node, buffer, bufferlen) : -1;
261 }
262 static int numa_max_node() { return _numa_max_node != NULL ? _numa_max_node() : -1; }
263 static int numa_available() { return _numa_available != NULL ? _numa_available() : -1; }
264 static int numa_tonode_memory(void *start, size_t size, int node) {
265 return _numa_tonode_memory != NULL ? _numa_tonode_memory(start, size, node) : -1;
266 }
267 static int get_node_by_cpu(int cpu_id);
268 };
271 class PlatformEvent : public CHeapObj {
272 private:
273 double CachePad [4] ; // increase odds that _mutex is sole occupant of cache line
274 volatile int _Event ;
275 volatile int _nParked ;
276 pthread_mutex_t _mutex [1] ;
277 pthread_cond_t _cond [1] ;
278 double PostPad [2] ;
279 Thread * _Assoc ;
281 public: // TODO-FIXME: make dtor private
282 ~PlatformEvent() { guarantee (0, "invariant") ; }
284 public:
285 PlatformEvent() {
286 int status;
287 status = pthread_cond_init (_cond, NULL);
288 assert_status(status == 0, status, "cond_init");
289 status = pthread_mutex_init (_mutex, NULL);
290 assert_status(status == 0, status, "mutex_init");
291 _Event = 0 ;
292 _nParked = 0 ;
293 _Assoc = NULL ;
294 }
296 // Use caution with reset() and fired() -- they may require MEMBARs
297 void reset() { _Event = 0 ; }
298 int fired() { return _Event; }
299 void park () ;
300 void unpark () ;
301 int TryPark () ;
302 int park (jlong millis) ;
303 void SetAssociation (Thread * a) { _Assoc = a ; }
304 } ;
306 class PlatformParker : public CHeapObj {
307 protected:
308 pthread_mutex_t _mutex [1] ;
309 pthread_cond_t _cond [1] ;
311 public: // TODO-FIXME: make dtor private
312 ~PlatformParker() { guarantee (0, "invariant") ; }
314 public:
315 PlatformParker() {
316 int status;
317 status = pthread_cond_init (_cond, NULL);
318 assert_status(status == 0, status, "cond_init");
319 status = pthread_mutex_init (_mutex, NULL);
320 assert_status(status == 0, status, "mutex_init");
321 }
322 } ;