Mercurial > hg > graal-jvmci-8
comparison src/os/linux/vm/os_linux.cpp @ 2044:06f017f7daa7
Merge.
author | Thomas Wuerthinger <wuerthinger@ssw.jku.at> |
---|---|
date | Fri, 07 Jan 2011 18:18:08 +0100 |
parents | 00bc9eaf0e24 03e1b9fce89d |
children | d25d4ca69222 |
comparison
equal
deleted
inserted
replaced
1942:00bc9eaf0e24 | 2044:06f017f7daa7 |
---|---|
22 * | 22 * |
23 */ | 23 */ |
24 | 24 |
25 # define __STDC_FORMAT_MACROS | 25 # define __STDC_FORMAT_MACROS |
26 | 26 |
27 // do not include precompiled header file | 27 // no precompiled headers |
28 # include "incls/_os_linux.cpp.incl" | 28 #include "classfile/classLoader.hpp" |
29 #include "classfile/systemDictionary.hpp" | |
30 #include "classfile/vmSymbols.hpp" | |
31 #include "code/icBuffer.hpp" | |
32 #include "code/vtableStubs.hpp" | |
33 #include "compiler/compileBroker.hpp" | |
34 #include "interpreter/interpreter.hpp" | |
35 #include "jvm_linux.h" | |
36 #include "memory/allocation.inline.hpp" | |
37 #include "memory/filemap.hpp" | |
38 #include "mutex_linux.inline.hpp" | |
39 #include "oops/oop.inline.hpp" | |
40 #include "os_share_linux.hpp" | |
41 #include "prims/jniFastGetField.hpp" | |
42 #include "prims/jvm.h" | |
43 #include "prims/jvm_misc.hpp" | |
44 #include "runtime/arguments.hpp" | |
45 #include "runtime/extendedPC.hpp" | |
46 #include "runtime/globals.hpp" | |
47 #include "runtime/interfaceSupport.hpp" | |
48 #include "runtime/java.hpp" | |
49 #include "runtime/javaCalls.hpp" | |
50 #include "runtime/mutexLocker.hpp" | |
51 #include "runtime/objectMonitor.hpp" | |
52 #include "runtime/osThread.hpp" | |
53 #include "runtime/perfMemory.hpp" | |
54 #include "runtime/sharedRuntime.hpp" | |
55 #include "runtime/statSampler.hpp" | |
56 #include "runtime/stubRoutines.hpp" | |
57 #include "runtime/threadCritical.hpp" | |
58 #include "runtime/timer.hpp" | |
59 #include "services/attachListener.hpp" | |
60 #include "services/runtimeService.hpp" | |
61 #include "thread_linux.inline.hpp" | |
62 #include "utilities/decoder.hpp" | |
63 #include "utilities/defaultStream.hpp" | |
64 #include "utilities/events.hpp" | |
65 #include "utilities/growableArray.hpp" | |
66 #include "utilities/vmError.hpp" | |
67 #ifdef TARGET_ARCH_x86 | |
68 # include "assembler_x86.inline.hpp" | |
69 # include "nativeInst_x86.hpp" | |
70 #endif | |
71 #ifdef TARGET_ARCH_sparc | |
72 # include "assembler_sparc.inline.hpp" | |
73 # include "nativeInst_sparc.hpp" | |
74 #endif | |
75 #ifdef TARGET_ARCH_zero | |
76 # include "assembler_zero.inline.hpp" | |
77 # include "nativeInst_zero.hpp" | |
78 #endif | |
79 #ifdef COMPILER1 | |
80 #include "c1/c1_Runtime1.hpp" | |
81 #endif | |
82 #ifdef COMPILER2 | |
83 #include "opto/runtime.hpp" | |
84 #endif | |
29 | 85 |
30 // put OS-includes here | 86 // put OS-includes here |
31 # include <sys/types.h> | 87 # include <sys/types.h> |
32 # include <sys/mman.h> | 88 # include <sys/mman.h> |
33 # include <sys/stat.h> | 89 # include <sys/stat.h> |
57 # include <sys/ipc.h> | 113 # include <sys/ipc.h> |
58 # include <sys/shm.h> | 114 # include <sys/shm.h> |
59 # include <link.h> | 115 # include <link.h> |
60 # include <stdint.h> | 116 # include <stdint.h> |
61 # include <inttypes.h> | 117 # include <inttypes.h> |
118 # include <sys/ioctl.h> | |
62 | 119 |
63 #define MAX_PATH (2 * K) | 120 #define MAX_PATH (2 * K) |
64 | 121 |
65 // for timer info max values which include all bits | 122 // for timer info max values which include all bits |
66 #define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF) | 123 #define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF) |
1507 } | 1564 } |
1508 | 1565 |
1509 // unused on linux for now. | 1566 // unused on linux for now. |
1510 void os::set_error_file(const char *logfile) {} | 1567 void os::set_error_file(const char *logfile) {} |
1511 | 1568 |
1569 | |
1570 // This method is a copy of JDK's sysGetLastErrorString | |
1571 // from src/solaris/hpi/src/system_md.c | |
1572 | |
1573 size_t os::lasterror(char *buf, size_t len) { | |
1574 | |
1575 if (errno == 0) return 0; | |
1576 | |
1577 const char *s = ::strerror(errno); | |
1578 size_t n = ::strlen(s); | |
1579 if (n >= len) { | |
1580 n = len - 1; | |
1581 } | |
1582 ::strncpy(buf, s, n); | |
1583 buf[n] = '\0'; | |
1584 return n; | |
1585 } | |
1586 | |
1512 intx os::current_thread_id() { return (intx)pthread_self(); } | 1587 intx os::current_thread_id() { return (intx)pthread_self(); } |
1513 int os::current_process_id() { | 1588 int os::current_process_id() { |
1514 | 1589 |
1515 // Under the old linux thread library, linux gives each thread | 1590 // Under the old linux thread library, linux gives each thread |
1516 // its own process id. Because of this each thread will return | 1591 // its own process id. Because of this each thread will return |
1613 bool os::dll_address_to_function_name(address addr, char *buf, | 1688 bool os::dll_address_to_function_name(address addr, char *buf, |
1614 int buflen, int *offset) { | 1689 int buflen, int *offset) { |
1615 Dl_info dlinfo; | 1690 Dl_info dlinfo; |
1616 | 1691 |
1617 if (dladdr((void*)addr, &dlinfo) && dlinfo.dli_sname != NULL) { | 1692 if (dladdr((void*)addr, &dlinfo) && dlinfo.dli_sname != NULL) { |
1618 if (buf) jio_snprintf(buf, buflen, "%s", dlinfo.dli_sname); | 1693 if (buf != NULL) { |
1619 if (offset) *offset = addr - (address)dlinfo.dli_saddr; | 1694 if(!Decoder::demangle(dlinfo.dli_sname, buf, buflen)) { |
1695 jio_snprintf(buf, buflen, "%s", dlinfo.dli_sname); | |
1696 } | |
1697 } | |
1698 if (offset != NULL) *offset = addr - (address)dlinfo.dli_saddr; | |
1620 return true; | 1699 return true; |
1621 } else { | 1700 } else if (dlinfo.dli_fname != NULL && dlinfo.dli_fbase != 0) { |
1622 if (buf) buf[0] = '\0'; | 1701 if (Decoder::decode((address)(addr - (address)dlinfo.dli_fbase), |
1623 if (offset) *offset = -1; | 1702 dlinfo.dli_fname, buf, buflen, offset) == Decoder::no_error) { |
1624 return false; | 1703 return true; |
1625 } | 1704 } |
1705 } | |
1706 | |
1707 if (buf != NULL) buf[0] = '\0'; | |
1708 if (offset != NULL) *offset = -1; | |
1709 return false; | |
1626 } | 1710 } |
1627 | 1711 |
1628 struct _address_to_library_name { | 1712 struct _address_to_library_name { |
1629 address addr; // input : memory address | 1713 address addr; // input : memory address |
1630 size_t buflen; // size of fname | 1714 size_t buflen; // size of fname |
1871 pthread_mutex_unlock(&dl_mutex); | 1955 pthread_mutex_unlock(&dl_mutex); |
1872 return res; | 1956 return res; |
1873 } | 1957 } |
1874 | 1958 |
1875 | 1959 |
1876 bool _print_ascii_file(const char* filename, outputStream* st) { | 1960 static bool _print_ascii_file(const char* filename, outputStream* st) { |
1877 int fd = open(filename, O_RDONLY); | 1961 int fd = ::open(filename, O_RDONLY); |
1878 if (fd == -1) { | 1962 if (fd == -1) { |
1879 return false; | 1963 return false; |
1880 } | 1964 } |
1881 | 1965 |
1882 char buf[32]; | 1966 char buf[32]; |
1883 int bytes; | 1967 int bytes; |
1884 while ((bytes = read(fd, buf, sizeof(buf))) > 0) { | 1968 while ((bytes = ::read(fd, buf, sizeof(buf))) > 0) { |
1885 st->print_raw(buf, bytes); | 1969 st->print_raw(buf, bytes); |
1886 } | 1970 } |
1887 | 1971 |
1888 close(fd); | 1972 ::close(fd); |
1889 | 1973 |
1890 return true; | 1974 return true; |
1891 } | 1975 } |
1892 | 1976 |
1893 void os::print_dll_info(outputStream *st) { | 1977 void os::print_dll_info(outputStream *st) { |
2161 | 2245 |
2162 if (0 == access(buf, F_OK)) { | 2246 if (0 == access(buf, F_OK)) { |
2163 // Use current module name "libjvm[_g].so" instead of | 2247 // Use current module name "libjvm[_g].so" instead of |
2164 // "libjvm"debug_only("_g")".so" since for fastdebug version | 2248 // "libjvm"debug_only("_g")".so" since for fastdebug version |
2165 // we should have "libjvm.so" but debug_only("_g") adds "_g"! | 2249 // we should have "libjvm.so" but debug_only("_g") adds "_g"! |
2166 // It is used when we are choosing the HPI library's name | |
2167 // "libhpi[_g].so" in hpi::initialize_get_interface(). | |
2168 len = strlen(buf); | 2250 len = strlen(buf); |
2169 snprintf(buf + len, buflen-len, "/hotspot/libjvm%s.so", p); | 2251 snprintf(buf + len, buflen-len, "/hotspot/libjvm%s.so", p); |
2170 } else { | 2252 } else { |
2171 // Go back to path of .so | 2253 // Go back to path of .so |
2172 rp = realpath(dli_fname, buf); | 2254 rp = realpath(dli_fname, buf); |
2346 | 2428 |
2347 snprintf(buf, sizeof(buf), "%s/hs-vm-%d-%d", | 2429 snprintf(buf, sizeof(buf), "%s/hs-vm-%d-%d", |
2348 os::get_temp_directory(), os::current_process_id(), num); | 2430 os::get_temp_directory(), os::current_process_id(), num); |
2349 unlink(buf); | 2431 unlink(buf); |
2350 | 2432 |
2351 int fd = open(buf, O_CREAT | O_RDWR, S_IRWXU); | 2433 int fd = ::open(buf, O_CREAT | O_RDWR, S_IRWXU); |
2352 | 2434 |
2353 if (fd != -1) { | 2435 if (fd != -1) { |
2354 off_t rv = lseek(fd, size-2, SEEK_SET); | 2436 off_t rv = ::lseek(fd, size-2, SEEK_SET); |
2355 if (rv != (off_t)-1) { | 2437 if (rv != (off_t)-1) { |
2356 if (write(fd, "", 1) == 1) { | 2438 if (::write(fd, "", 1) == 1) { |
2357 mmap(base, size, | 2439 mmap(base, size, |
2358 PROT_READ|PROT_WRITE|PROT_EXEC, | 2440 PROT_READ|PROT_WRITE|PROT_EXEC, |
2359 MAP_PRIVATE|MAP_FIXED|MAP_NORESERVE, fd, 0); | 2441 MAP_PRIVATE|MAP_FIXED|MAP_NORESERVE, fd, 0); |
2360 } | 2442 } |
2361 } | 2443 } |
2362 close(fd); | 2444 ::close(fd); |
2363 unlink(buf); | 2445 unlink(buf); |
2364 } | 2446 } |
2365 } | 2447 } |
2366 | 2448 |
2367 // NOTE: Linux kernel does not really reserve the pages for us. | 2449 // NOTE: Linux kernel does not really reserve the pages for us. |
3996 } | 4078 } |
3997 | 4079 |
3998 // Initialize lock used to serialize thread creation (see os::create_thread) | 4080 // Initialize lock used to serialize thread creation (see os::create_thread) |
3999 Linux::set_createThread_lock(new Mutex(Mutex::leaf, "createThread_lock", false)); | 4081 Linux::set_createThread_lock(new Mutex(Mutex::leaf, "createThread_lock", false)); |
4000 | 4082 |
4001 // Initialize HPI. | |
4002 jint hpi_result = hpi::initialize(); | |
4003 if (hpi_result != JNI_OK) { | |
4004 tty->print_cr("There was an error trying to initialize the HPI library."); | |
4005 return hpi_result; | |
4006 } | |
4007 | |
4008 // at-exit methods are called in the reverse order of their registration. | 4083 // at-exit methods are called in the reverse order of their registration. |
4009 // atexit functions are called on return from main or as a result of a | 4084 // atexit functions are called on return from main or as a result of a |
4010 // call to exit(3C). There can be only 32 of these functions registered | 4085 // call to exit(3C). There can be only 32 of these functions registered |
4011 // and atexit() does not set errno. | 4086 // and atexit() does not set errno. |
4012 | 4087 |
4200 char pathbuf[MAX_PATH]; | 4275 char pathbuf[MAX_PATH]; |
4201 if (strlen(path) > MAX_PATH - 1) { | 4276 if (strlen(path) > MAX_PATH - 1) { |
4202 errno = ENAMETOOLONG; | 4277 errno = ENAMETOOLONG; |
4203 return -1; | 4278 return -1; |
4204 } | 4279 } |
4205 hpi::native_path(strcpy(pathbuf, path)); | 4280 os::native_path(strcpy(pathbuf, path)); |
4206 return ::stat(pathbuf, sbuf); | 4281 return ::stat(pathbuf, sbuf); |
4207 } | 4282 } |
4208 | 4283 |
4209 bool os::check_heap(bool force) { | 4284 bool os::check_heap(bool force) { |
4210 return true; | 4285 return true; |
4232 } | 4307 } |
4233 closedir(dir); | 4308 closedir(dir); |
4234 return result; | 4309 return result; |
4235 } | 4310 } |
4236 | 4311 |
4312 // This code originates from JDK's sysOpen and open64_w | |
4313 // from src/solaris/hpi/src/system_md.c | |
4314 | |
4315 #ifndef O_DELETE | |
4316 #define O_DELETE 0x10000 | |
4317 #endif | |
4318 | |
4319 // Open a file. Unlink the file immediately after open returns | |
4320 // if the specified oflag has the O_DELETE flag set. | |
4321 // O_DELETE is used only in j2se/src/share/native/java/util/zip/ZipFile.c | |
4322 | |
4323 int os::open(const char *path, int oflag, int mode) { | |
4324 | |
4325 if (strlen(path) > MAX_PATH - 1) { | |
4326 errno = ENAMETOOLONG; | |
4327 return -1; | |
4328 } | |
4329 int fd; | |
4330 int o_delete = (oflag & O_DELETE); | |
4331 oflag = oflag & ~O_DELETE; | |
4332 | |
4333 fd = ::open64(path, oflag, mode); | |
4334 if (fd == -1) return -1; | |
4335 | |
4336 //If the open succeeded, the file might still be a directory | |
4337 { | |
4338 struct stat64 buf64; | |
4339 int ret = ::fstat64(fd, &buf64); | |
4340 int st_mode = buf64.st_mode; | |
4341 | |
4342 if (ret != -1) { | |
4343 if ((st_mode & S_IFMT) == S_IFDIR) { | |
4344 errno = EISDIR; | |
4345 ::close(fd); | |
4346 return -1; | |
4347 } | |
4348 } else { | |
4349 ::close(fd); | |
4350 return -1; | |
4351 } | |
4352 } | |
4353 | |
4354 /* | |
4355 * All file descriptors that are opened in the JVM and not | |
4356 * specifically destined for a subprocess should have the | |
4357 * close-on-exec flag set. If we don't set it, then careless 3rd | |
4358 * party native code might fork and exec without closing all | |
4359 * appropriate file descriptors (e.g. as we do in closeDescriptors in | |
4360 * UNIXProcess.c), and this in turn might: | |
4361 * | |
4362 * - cause end-of-file to fail to be detected on some file | |
4363 * descriptors, resulting in mysterious hangs, or | |
4364 * | |
4365 * - might cause an fopen in the subprocess to fail on a system | |
4366 * suffering from bug 1085341. | |
4367 * | |
4368 * (Yes, the default setting of the close-on-exec flag is a Unix | |
4369 * design flaw) | |
4370 * | |
4371 * See: | |
4372 * 1085341: 32-bit stdio routines should support file descriptors >255 | |
4373 * 4843136: (process) pipe file descriptor from Runtime.exec not being closed | |
4374 * 6339493: (process) Runtime.exec does not close all file descriptors on Solaris 9 | |
4375 */ | |
4376 #ifdef FD_CLOEXEC | |
4377 { | |
4378 int flags = ::fcntl(fd, F_GETFD); | |
4379 if (flags != -1) | |
4380 ::fcntl(fd, F_SETFD, flags | FD_CLOEXEC); | |
4381 } | |
4382 #endif | |
4383 | |
4384 if (o_delete != 0) { | |
4385 ::unlink(path); | |
4386 } | |
4387 return fd; | |
4388 } | |
4389 | |
4390 | |
4237 // create binary file, rewriting existing file if required | 4391 // create binary file, rewriting existing file if required |
4238 int os::create_binary_file(const char* path, bool rewrite_existing) { | 4392 int os::create_binary_file(const char* path, bool rewrite_existing) { |
4239 int oflags = O_WRONLY | O_CREAT; | 4393 int oflags = O_WRONLY | O_CREAT; |
4240 if (!rewrite_existing) { | 4394 if (!rewrite_existing) { |
4241 oflags |= O_EXCL; | 4395 oflags |= O_EXCL; |
4249 } | 4403 } |
4250 | 4404 |
4251 // move file pointer to the specified offset | 4405 // move file pointer to the specified offset |
4252 jlong os::seek_to_file_offset(int fd, jlong offset) { | 4406 jlong os::seek_to_file_offset(int fd, jlong offset) { |
4253 return (jlong)::lseek64(fd, (off64_t)offset, SEEK_SET); | 4407 return (jlong)::lseek64(fd, (off64_t)offset, SEEK_SET); |
4408 } | |
4409 | |
4410 // This code originates from JDK's sysAvailable | |
4411 // from src/solaris/hpi/src/native_threads/src/sys_api_td.c | |
4412 | |
4413 int os::available(int fd, jlong *bytes) { | |
4414 jlong cur, end; | |
4415 int mode; | |
4416 struct stat64 buf64; | |
4417 | |
4418 if (::fstat64(fd, &buf64) >= 0) { | |
4419 mode = buf64.st_mode; | |
4420 if (S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode)) { | |
4421 /* | |
4422 * XXX: is the following call interruptible? If so, this might | |
4423 * need to go through the INTERRUPT_IO() wrapper as for other | |
4424 * blocking, interruptible calls in this file. | |
4425 */ | |
4426 int n; | |
4427 if (::ioctl(fd, FIONREAD, &n) >= 0) { | |
4428 *bytes = n; | |
4429 return 1; | |
4430 } | |
4431 } | |
4432 } | |
4433 if ((cur = ::lseek64(fd, 0L, SEEK_CUR)) == -1) { | |
4434 return 0; | |
4435 } else if ((end = ::lseek64(fd, 0L, SEEK_END)) == -1) { | |
4436 return 0; | |
4437 } else if (::lseek64(fd, cur, SEEK_SET) == -1) { | |
4438 return 0; | |
4439 } | |
4440 *bytes = end - cur; | |
4441 return 1; | |
4442 } | |
4443 | |
4444 int os::socket_available(int fd, jint *pbytes) { | |
4445 // Linux doc says EINTR not returned, unlike Solaris | |
4446 int ret = ::ioctl(fd, FIONREAD, pbytes); | |
4447 | |
4448 //%% note ioctl can return 0 when successful, JVM_SocketAvailable | |
4449 // is expected to return 0 on failure and 1 on success to the jdk. | |
4450 return (ret < 0) ? 0 : 1; | |
4254 } | 4451 } |
4255 | 4452 |
4256 // Map a block of memory. | 4453 // Map a block of memory. |
4257 char* os::map_memory(int fd, const char* file_name, size_t file_offset, | 4454 char* os::map_memory(int fd, const char* file_name, size_t file_offset, |
4258 char *addr, size_t bytes, bool read_only, | 4455 char *addr, size_t bytes, bool read_only, |
4477 } | 4674 } |
4478 | 4675 |
4479 int fd = ::open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666); | 4676 int fd = ::open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
4480 if (fd != -1) { | 4677 if (fd != -1) { |
4481 struct stat buf; | 4678 struct stat buf; |
4482 close(fd); | 4679 ::close(fd); |
4483 while (::stat(filename, &buf) == 0) { | 4680 while (::stat(filename, &buf) == 0) { |
4484 (void)::poll(NULL, 0, 100); | 4681 (void)::poll(NULL, 0, 100); |
4485 } | 4682 } |
4486 } else { | 4683 } else { |
4487 jio_fprintf(stderr, | 4684 jio_fprintf(stderr, |