Thu, 28 Jun 2012 17:03:16 -0400
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
1 /*
2 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
25 #ifndef OS_LINUX_VM_OS_LINUX_INLINE_HPP
26 #define OS_LINUX_VM_OS_LINUX_INLINE_HPP
28 #include "runtime/atomic.hpp"
29 #include "runtime/os.hpp"
30 #ifdef TARGET_OS_ARCH_linux_x86
31 # include "atomic_linux_x86.inline.hpp"
32 # include "orderAccess_linux_x86.inline.hpp"
33 #endif
34 #ifdef TARGET_OS_ARCH_linux_sparc
35 # include "atomic_linux_sparc.inline.hpp"
36 # include "orderAccess_linux_sparc.inline.hpp"
37 #endif
38 #ifdef TARGET_OS_ARCH_linux_zero
39 # include "atomic_linux_zero.inline.hpp"
40 # include "orderAccess_linux_zero.inline.hpp"
41 #endif
42 #ifdef TARGET_OS_ARCH_linux_arm
43 # include "atomic_linux_arm.inline.hpp"
44 # include "orderAccess_linux_arm.inline.hpp"
45 #endif
46 #ifdef TARGET_OS_ARCH_linux_ppc
47 # include "atomic_linux_ppc.inline.hpp"
48 # include "orderAccess_linux_ppc.inline.hpp"
49 #endif
51 // System includes
53 #include <unistd.h>
54 #include <sys/socket.h>
55 #include <sys/poll.h>
56 #include <netdb.h>
58 inline void* os::thread_local_storage_at(int index) {
59 return pthread_getspecific((pthread_key_t)index);
60 }
62 inline const char* os::file_separator() {
63 return "/";
64 }
66 inline const char* os::line_separator() {
67 return "\n";
68 }
70 inline const char* os::path_separator() {
71 return ":";
72 }
74 inline const char* os::jlong_format_specifier() {
75 return "%lld";
76 }
78 inline const char* os::julong_format_specifier() {
79 return "%llu";
80 }
82 // File names are case-sensitive on windows only
83 inline int os::file_name_strcmp(const char* s1, const char* s2) {
84 return strcmp(s1, s2);
85 }
87 inline bool os::obsolete_option(const JavaVMOption *option) {
88 return false;
89 }
91 inline bool os::uses_stack_guard_pages() {
92 return true;
93 }
95 inline bool os::allocate_stack_guard_pages() {
96 assert(uses_stack_guard_pages(), "sanity check");
97 return true;
98 }
101 // On Linux, reservations are made on a page by page basis, nothing to do.
102 inline void os::pd_split_reserved_memory(char *base, size_t size,
103 size_t split, bool realloc) {
104 }
107 // Bang the shadow pages if they need to be touched to be mapped.
108 inline void os::bang_stack_shadow_pages() {
109 }
111 inline void os::dll_unload(void *lib) {
112 ::dlclose(lib);
113 }
115 inline const int os::default_file_open_flags() { return 0;}
117 inline DIR* os::opendir(const char* dirname)
118 {
119 assert(dirname != NULL, "just checking");
120 return ::opendir(dirname);
121 }
123 inline int os::readdir_buf_size(const char *path)
124 {
125 return NAME_MAX + sizeof(dirent) + 1;
126 }
128 inline jlong os::lseek(int fd, jlong offset, int whence) {
129 return (jlong) ::lseek64(fd, offset, whence);
130 }
132 inline int os::fsync(int fd) {
133 return ::fsync(fd);
134 }
136 inline char* os::native_path(char *path) {
137 return path;
138 }
140 inline int os::ftruncate(int fd, jlong length) {
141 return ::ftruncate64(fd, length);
142 }
144 inline struct dirent* os::readdir(DIR* dirp, dirent *dbuf)
145 {
146 dirent* p;
147 int status;
148 assert(dirp != NULL, "just checking");
150 // NOTE: Linux readdir_r (on RH 6.2 and 7.2 at least) is NOT like the POSIX
151 // version. Here is the doc for this function:
152 // http://www.gnu.org/manual/glibc-2.2.3/html_node/libc_262.html
154 if((status = ::readdir_r(dirp, dbuf, &p)) != 0) {
155 errno = status;
156 return NULL;
157 } else
158 return p;
159 }
161 inline int os::closedir(DIR *dirp) {
162 assert(dirp != NULL, "argument is NULL");
163 return ::closedir(dirp);
164 }
166 // macros for restartable system calls
168 #define RESTARTABLE(_cmd, _result) do { \
169 _result = _cmd; \
170 } while(((int)_result == OS_ERR) && (errno == EINTR))
172 #define RESTARTABLE_RETURN_INT(_cmd) do { \
173 int _result; \
174 RESTARTABLE(_cmd, _result); \
175 return _result; \
176 } while(false)
178 inline bool os::numa_has_static_binding() { return true; }
179 inline bool os::numa_has_group_homing() { return false; }
181 inline size_t os::restartable_read(int fd, void *buf, unsigned int nBytes) {
182 size_t res;
183 RESTARTABLE( (size_t) ::read(fd, buf, (size_t) nBytes), res);
184 return res;
185 }
187 inline size_t os::write(int fd, const void *buf, unsigned int nBytes) {
188 size_t res;
189 RESTARTABLE((size_t) ::write(fd, buf, (size_t) nBytes), res);
190 return res;
191 }
193 inline int os::close(int fd) {
194 return ::close(fd);
195 }
197 inline int os::socket_close(int fd) {
198 return ::close(fd);
199 }
201 inline int os::socket(int domain, int type, int protocol) {
202 return ::socket(domain, type, protocol);
203 }
205 inline int os::recv(int fd, char* buf, size_t nBytes, uint flags) {
206 RESTARTABLE_RETURN_INT(::recv(fd, buf, nBytes, flags));
207 }
209 inline int os::send(int fd, char* buf, size_t nBytes, uint flags) {
210 RESTARTABLE_RETURN_INT(::send(fd, buf, nBytes, flags));
211 }
213 inline int os::raw_send(int fd, char* buf, size_t nBytes, uint flags) {
214 return os::send(fd, buf, nBytes, flags);
215 }
217 inline int os::timeout(int fd, long timeout) {
218 julong prevtime,newtime;
219 struct timeval t;
221 gettimeofday(&t, NULL);
222 prevtime = ((julong)t.tv_sec * 1000) + t.tv_usec / 1000;
224 for(;;) {
225 struct pollfd pfd;
227 pfd.fd = fd;
228 pfd.events = POLLIN | POLLERR;
230 int res = ::poll(&pfd, 1, timeout);
232 if (res == OS_ERR && errno == EINTR) {
234 // On Linux any value < 0 means "forever"
236 if(timeout >= 0) {
237 gettimeofday(&t, NULL);
238 newtime = ((julong)t.tv_sec * 1000) + t.tv_usec / 1000;
239 timeout -= newtime - prevtime;
240 if(timeout <= 0)
241 return OS_OK;
242 prevtime = newtime;
243 }
244 } else
245 return res;
246 }
247 }
249 inline int os::listen(int fd, int count) {
250 return ::listen(fd, count);
251 }
253 inline int os::connect(int fd, struct sockaddr* him, socklen_t len) {
254 RESTARTABLE_RETURN_INT(::connect(fd, him, len));
255 }
257 inline int os::accept(int fd, struct sockaddr* him, socklen_t* len) {
258 // Linux doc says this can't return EINTR, unlike accept() on Solaris.
259 // But see attachListener_linux.cpp, LinuxAttachListener::dequeue().
260 return (int)::accept(fd, him, len);
261 }
263 inline int os::recvfrom(int fd, char* buf, size_t nBytes, uint flags,
264 sockaddr* from, socklen_t* fromlen) {
265 RESTARTABLE_RETURN_INT((int)::recvfrom(fd, buf, nBytes, flags, from, fromlen));
266 }
268 inline int os::sendto(int fd, char* buf, size_t len, uint flags,
269 struct sockaddr* to, socklen_t tolen) {
270 RESTARTABLE_RETURN_INT((int)::sendto(fd, buf, len, flags, to, tolen));
271 }
273 inline int os::socket_shutdown(int fd, int howto) {
274 return ::shutdown(fd, howto);
275 }
277 inline int os::bind(int fd, struct sockaddr* him, socklen_t len) {
278 return ::bind(fd, him, len);
279 }
281 inline int os::get_sock_name(int fd, struct sockaddr* him, socklen_t* len) {
282 return ::getsockname(fd, him, len);
283 }
285 inline int os::get_host_name(char* name, int namelen) {
286 return ::gethostname(name, namelen);
287 }
289 inline struct hostent* os::get_host_by_name(char* name) {
290 return ::gethostbyname(name);
291 }
293 inline int os::get_sock_opt(int fd, int level, int optname,
294 char* optval, socklen_t* optlen) {
295 return ::getsockopt(fd, level, optname, optval, optlen);
296 }
298 inline int os::set_sock_opt(int fd, int level, int optname,
299 const char* optval, socklen_t optlen) {
300 return ::setsockopt(fd, level, optname, optval, optlen);
301 }
302 #endif // OS_LINUX_VM_OS_LINUX_INLINE_HPP