src/os/linux/vm/os_linux.hpp

Thu, 28 Jun 2012 17:03:16 -0400

author
zgu
date
Thu, 28 Jun 2012 17:03:16 -0400
changeset 3900
d2a62e0f25eb
parent 3783
7432b9db36ff
child 4017
fa9253dcd4df
permissions
-rw-r--r--

6995781: Native Memory Tracking (Phase 1)
7151532: DCmd for hotspot native memory tracking
Summary: Implementation of native memory tracking phase 1, which tracks VM native memory usage, and related DCmd
Reviewed-by: acorn, coleenp, fparain

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

mercurial