src/os_cpu/linux_x86/vm/threadLS_linux_x86.cpp

Thu, 22 Apr 2010 13:23:15 -0700

author
jcoomes
date
Thu, 22 Apr 2010 13:23:15 -0700
changeset 1845
f03d0a26bf83
parent 435
a61af66fc99e
child 1907
c18cbe5936b8
permissions
-rw-r--r--

6888954: argument formatting for assert() and friends
Reviewed-by: kvn, twisti, apetrusenko, never, dcubed

duke@435 1 /*
duke@435 2 * Copyright 1999-2003 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 #include "incls/_precompiled.incl"
duke@435 26 #include "incls/_threadLS_linux_x86.cpp.incl"
duke@435 27
duke@435 28 // Map stack pointer (%esp) to thread pointer for faster TLS access
duke@435 29 //
duke@435 30 // Here we use a flat table for better performance. Getting current thread
duke@435 31 // is down to one memory access (read _sp_map[%esp>>12]) in generated code
duke@435 32 // and two in runtime code (-fPIC code needs an extra load for _sp_map).
duke@435 33 //
duke@435 34 // This code assumes stack page is not shared by different threads. It works
duke@435 35 // in 32-bit VM when page size is 4K (or a multiple of 4K, if that matters).
duke@435 36 //
duke@435 37 // Notice that _sp_map is allocated in the bss segment, which is ZFOD
duke@435 38 // (zero-fill-on-demand). While it reserves 4M address space upfront,
duke@435 39 // actual memory pages are committed on demand.
duke@435 40 //
duke@435 41 // If an application creates and destroys a lot of threads, usually the
duke@435 42 // stack space freed by a thread will soon get reused by new thread
duke@435 43 // (this is especially true in NPTL or LinuxThreads in fixed-stack mode).
duke@435 44 // No memory page in _sp_map is wasted.
duke@435 45 //
duke@435 46 // However, it's still possible that we might end up populating &
duke@435 47 // committing a large fraction of the 4M table over time, but the actual
duke@435 48 // amount of live data in the table could be quite small. The max wastage
duke@435 49 // is less than 4M bytes. If it becomes an issue, we could use madvise()
duke@435 50 // with MADV_DONTNEED to reclaim unused (i.e. all-zero) pages in _sp_map.
duke@435 51 // MADV_DONTNEED on Linux keeps the virtual memory mapping, but zaps the
duke@435 52 // physical memory page (i.e. similar to MADV_FREE on Solaris).
duke@435 53
duke@435 54 #ifndef AMD64
duke@435 55 Thread* ThreadLocalStorage::_sp_map[1UL << (SP_BITLENGTH - PAGE_SHIFT)];
duke@435 56 #endif // !AMD64
duke@435 57
duke@435 58 void ThreadLocalStorage::generate_code_for_get_thread() {
duke@435 59 // nothing we can do here for user-level thread
duke@435 60 }
duke@435 61
duke@435 62 void ThreadLocalStorage::pd_init() {
duke@435 63 #ifndef AMD64
duke@435 64 assert(align_size_down(os::vm_page_size(), PAGE_SIZE) == os::vm_page_size(),
duke@435 65 "page size must be multiple of PAGE_SIZE");
duke@435 66 #endif // !AMD64
duke@435 67 }
duke@435 68
duke@435 69 void ThreadLocalStorage::pd_set_thread(Thread* thread) {
duke@435 70 os::thread_local_storage_at_put(ThreadLocalStorage::thread_index(), thread);
duke@435 71
duke@435 72 #ifndef AMD64
duke@435 73 address stack_top = os::current_stack_base();
duke@435 74 size_t stack_size = os::current_stack_size();
duke@435 75
duke@435 76 for (address p = stack_top - stack_size; p < stack_top; p += PAGE_SIZE) {
duke@435 77 // pd_set_thread() is called with non-NULL value when a new thread is
duke@435 78 // created/attached, or with NULL value when a thread is about to exit.
duke@435 79 // If both "thread" and the corresponding _sp_map[] entry are non-NULL,
duke@435 80 // they should have the same value. Otherwise it might indicate that the
duke@435 81 // stack page is shared by multiple threads. However, a more likely cause
duke@435 82 // for this assertion to fail is that an attached thread exited without
duke@435 83 // detaching itself from VM, which is a program error and could cause VM
duke@435 84 // to crash.
duke@435 85 assert(thread == NULL || _sp_map[(uintptr_t)p >> PAGE_SHIFT] == NULL ||
duke@435 86 thread == _sp_map[(uintptr_t)p >> PAGE_SHIFT],
duke@435 87 "thread exited without detaching from VM??");
duke@435 88 _sp_map[(uintptr_t)p >> PAGE_SHIFT] = thread;
duke@435 89 }
duke@435 90 #endif // !AMD64
duke@435 91 }

mercurial