Thu, 12 Mar 2009 10:37:46 -0700
6791178: Specialize for zero as the compressed oop vm heap base
Summary: Use zero based compressed oops if java heap is below 32gb and unscaled compressed oops if java heap is below 4gb.
Reviewed-by: never, twisti, jcoomes, coleenp
duke@435 | 1 | /* |
duke@435 | 2 | * Copyright 2005-2006 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/_attachListener_solaris.cpp.incl" |
duke@435 | 27 | |
duke@435 | 28 | #include <door.h> |
duke@435 | 29 | #include <string.h> |
duke@435 | 30 | #include <signal.h> |
duke@435 | 31 | #include <sys/types.h> |
duke@435 | 32 | #include <sys/socket.h> |
duke@435 | 33 | #include <sys/stat.h> |
duke@435 | 34 | |
duke@435 | 35 | // stropts.h uses STR in stream ioctl defines |
duke@435 | 36 | #undef STR |
duke@435 | 37 | #include <stropts.h> |
duke@435 | 38 | #undef STR |
duke@435 | 39 | #define STR(a) #a |
duke@435 | 40 | |
duke@435 | 41 | // The attach mechanism on Solaris is implemented using the Doors IPC |
duke@435 | 42 | // mechanism. The first tool to attempt to attach causes the attach |
duke@435 | 43 | // listener thread to startup. This thread creats a door that is |
duke@435 | 44 | // associated with a function that enqueues an operation to the attach |
duke@435 | 45 | // listener. The door is attached to a file in the file system so that |
duke@435 | 46 | // client (tools) can locate it. To enqueue an operation to the VM the |
duke@435 | 47 | // client calls through the door which invokes the enqueue function in |
duke@435 | 48 | // this process. The credentials of the client are checked and if the |
duke@435 | 49 | // effective uid matches this process then the operation is enqueued. |
duke@435 | 50 | // When an operation completes the attach listener is required to send the |
duke@435 | 51 | // operation result and any result data to the client. In this implementation |
duke@435 | 52 | // the result is returned via a UNIX domain socket. A pair of connected |
duke@435 | 53 | // sockets (socketpair) is created in the enqueue function and the file |
duke@435 | 54 | // descriptor for one of the sockets is returned to the client as the |
duke@435 | 55 | // return from the door call. The other end is retained in this process. |
duke@435 | 56 | // When the operation completes the result is sent to the client and |
duke@435 | 57 | // the socket is closed. |
duke@435 | 58 | |
duke@435 | 59 | // forward reference |
duke@435 | 60 | class SolarisAttachOperation; |
duke@435 | 61 | |
duke@435 | 62 | class SolarisAttachListener: AllStatic { |
duke@435 | 63 | private: |
duke@435 | 64 | |
duke@435 | 65 | // the path to which we attach the door file descriptor |
duke@435 | 66 | static char _door_path[PATH_MAX+1]; |
duke@435 | 67 | static volatile bool _has_door_path; |
duke@435 | 68 | |
duke@435 | 69 | // door descriptor returned by door_create |
duke@435 | 70 | static int _door_descriptor; |
duke@435 | 71 | |
duke@435 | 72 | static void set_door_path(char* path) { |
duke@435 | 73 | if (path == NULL) { |
duke@435 | 74 | _has_door_path = false; |
duke@435 | 75 | } else { |
duke@435 | 76 | strncpy(_door_path, path, PATH_MAX); |
duke@435 | 77 | _door_path[PATH_MAX] = '\0'; // ensure it's nul terminated |
duke@435 | 78 | _has_door_path = true; |
duke@435 | 79 | } |
duke@435 | 80 | } |
duke@435 | 81 | |
duke@435 | 82 | static void set_door_descriptor(int dd) { _door_descriptor = dd; } |
duke@435 | 83 | |
duke@435 | 84 | // mutex to protect operation list |
duke@435 | 85 | static mutex_t _mutex; |
duke@435 | 86 | |
duke@435 | 87 | // semaphore to wakeup listener thread |
duke@435 | 88 | static sema_t _wakeup; |
duke@435 | 89 | |
duke@435 | 90 | static mutex_t* mutex() { return &_mutex; } |
duke@435 | 91 | static sema_t* wakeup() { return &_wakeup; } |
duke@435 | 92 | |
duke@435 | 93 | // enqueued operation list |
duke@435 | 94 | static SolarisAttachOperation* _head; |
duke@435 | 95 | static SolarisAttachOperation* _tail; |
duke@435 | 96 | |
duke@435 | 97 | static SolarisAttachOperation* head() { return _head; } |
duke@435 | 98 | static void set_head(SolarisAttachOperation* head) { _head = head; } |
duke@435 | 99 | |
duke@435 | 100 | static SolarisAttachOperation* tail() { return _tail; } |
duke@435 | 101 | static void set_tail(SolarisAttachOperation* tail) { _tail = tail; } |
duke@435 | 102 | |
duke@435 | 103 | // create the door |
duke@435 | 104 | static int create_door(); |
duke@435 | 105 | |
duke@435 | 106 | public: |
duke@435 | 107 | enum { |
duke@435 | 108 | ATTACH_PROTOCOL_VER = 1 // protocol version |
duke@435 | 109 | }; |
duke@435 | 110 | enum { |
duke@435 | 111 | ATTACH_ERROR_BADREQUEST = 100, // error code returned by |
duke@435 | 112 | ATTACH_ERROR_BADVERSION = 101, // the door call |
duke@435 | 113 | ATTACH_ERROR_RESOURCE = 102, |
duke@435 | 114 | ATTACH_ERROR_INTERNAL = 103, |
duke@435 | 115 | ATTACH_ERROR_DENIED = 104 |
duke@435 | 116 | }; |
duke@435 | 117 | |
duke@435 | 118 | // initialize the listener |
duke@435 | 119 | static int init(); |
duke@435 | 120 | |
duke@435 | 121 | static bool has_door_path() { return _has_door_path; } |
duke@435 | 122 | static char* door_path() { return _door_path; } |
duke@435 | 123 | static int door_descriptor() { return _door_descriptor; } |
duke@435 | 124 | |
duke@435 | 125 | // enqueue an operation |
duke@435 | 126 | static void enqueue(SolarisAttachOperation* op); |
duke@435 | 127 | |
duke@435 | 128 | // dequeue an operation |
duke@435 | 129 | static SolarisAttachOperation* dequeue(); |
duke@435 | 130 | }; |
duke@435 | 131 | |
duke@435 | 132 | |
duke@435 | 133 | // SolarisAttachOperation is an AttachOperation that additionally encapsulates |
duke@435 | 134 | // a socket connection to the requesting client/tool. SolarisAttachOperation |
duke@435 | 135 | // can additionally be held in a linked list. |
duke@435 | 136 | |
duke@435 | 137 | class SolarisAttachOperation: public AttachOperation { |
duke@435 | 138 | private: |
duke@435 | 139 | friend class SolarisAttachListener; |
duke@435 | 140 | |
duke@435 | 141 | // connection to client |
duke@435 | 142 | int _socket; |
duke@435 | 143 | |
duke@435 | 144 | // linked list support |
duke@435 | 145 | SolarisAttachOperation* _next; |
duke@435 | 146 | |
duke@435 | 147 | SolarisAttachOperation* next() { return _next; } |
duke@435 | 148 | void set_next(SolarisAttachOperation* next) { _next = next; } |
duke@435 | 149 | |
duke@435 | 150 | public: |
duke@435 | 151 | void complete(jint res, bufferedStream* st); |
duke@435 | 152 | |
duke@435 | 153 | int socket() const { return _socket; } |
duke@435 | 154 | void set_socket(int s) { _socket = s; } |
duke@435 | 155 | |
duke@435 | 156 | SolarisAttachOperation(char* name) : AttachOperation(name) { |
duke@435 | 157 | set_socket(-1); |
duke@435 | 158 | set_next(NULL); |
duke@435 | 159 | } |
duke@435 | 160 | }; |
duke@435 | 161 | |
duke@435 | 162 | // statics |
duke@435 | 163 | char SolarisAttachListener::_door_path[PATH_MAX+1]; |
duke@435 | 164 | volatile bool SolarisAttachListener::_has_door_path; |
duke@435 | 165 | int SolarisAttachListener::_door_descriptor = -1; |
duke@435 | 166 | mutex_t SolarisAttachListener::_mutex; |
duke@435 | 167 | sema_t SolarisAttachListener::_wakeup; |
duke@435 | 168 | SolarisAttachOperation* SolarisAttachListener::_head = NULL; |
duke@435 | 169 | SolarisAttachOperation* SolarisAttachListener::_tail = NULL; |
duke@435 | 170 | |
duke@435 | 171 | // Supporting class to help split a buffer into individual components |
duke@435 | 172 | class ArgumentIterator : public StackObj { |
duke@435 | 173 | private: |
duke@435 | 174 | char* _pos; |
duke@435 | 175 | char* _end; |
duke@435 | 176 | public: |
duke@435 | 177 | ArgumentIterator(char* arg_buffer, size_t arg_size) { |
duke@435 | 178 | _pos = arg_buffer; |
duke@435 | 179 | _end = _pos + arg_size - 1; |
duke@435 | 180 | } |
duke@435 | 181 | char* next() { |
duke@435 | 182 | if (*_pos == '\0') { |
duke@435 | 183 | return NULL; |
duke@435 | 184 | } |
duke@435 | 185 | char* res = _pos; |
duke@435 | 186 | char* next_pos = strchr(_pos, '\0'); |
duke@435 | 187 | if (next_pos < _end) { |
duke@435 | 188 | next_pos++; |
duke@435 | 189 | } |
duke@435 | 190 | _pos = next_pos; |
duke@435 | 191 | return res; |
duke@435 | 192 | } |
duke@435 | 193 | }; |
duke@435 | 194 | |
duke@435 | 195 | // Calls from the door function to check that the client credentials |
duke@435 | 196 | // match this process. Returns 0 if credentials okay, otherwise -1. |
duke@435 | 197 | static int check_credentials() { |
duke@435 | 198 | door_cred_t cred_info; |
duke@435 | 199 | |
duke@435 | 200 | // get client credentials |
duke@435 | 201 | if (door_cred(&cred_info) == -1) { |
duke@435 | 202 | return -1; // unable to get them |
duke@435 | 203 | } |
duke@435 | 204 | |
duke@435 | 205 | // get our euid/eguid (probably could cache these) |
duke@435 | 206 | uid_t euid = geteuid(); |
duke@435 | 207 | gid_t egid = getegid(); |
duke@435 | 208 | |
duke@435 | 209 | // check that the effective uid/gid matches - discuss this with Jeff. |
duke@435 | 210 | if (cred_info.dc_euid == euid && cred_info.dc_egid == egid) { |
duke@435 | 211 | return 0; // okay |
duke@435 | 212 | } else { |
duke@435 | 213 | return -1; // denied |
duke@435 | 214 | } |
duke@435 | 215 | } |
duke@435 | 216 | |
duke@435 | 217 | |
duke@435 | 218 | // Parses the argument buffer to create an AttachOperation that we should |
duke@435 | 219 | // enqueue to the attach listener. |
duke@435 | 220 | // The buffer is expected to be formatted as follows: |
duke@435 | 221 | // <ver>0<cmd>0<arg>0<arg>0<arg>0 |
duke@435 | 222 | // where <ver> is the version number (must be "1"), <cmd> is the command |
duke@435 | 223 | // name ("load, "datadump", ...) and <arg> is an argument. |
duke@435 | 224 | // |
duke@435 | 225 | static SolarisAttachOperation* create_operation(char* argp, size_t arg_size, int* err) { |
duke@435 | 226 | // assume bad request until parsed |
duke@435 | 227 | *err = SolarisAttachListener::ATTACH_ERROR_BADREQUEST; |
duke@435 | 228 | |
duke@435 | 229 | if (arg_size < 2 || argp[arg_size-1] != '\0') { |
duke@435 | 230 | return NULL; // no ver or not null terminated |
duke@435 | 231 | } |
duke@435 | 232 | |
duke@435 | 233 | // Use supporting class to iterate over the buffer |
duke@435 | 234 | ArgumentIterator args(argp, arg_size); |
duke@435 | 235 | |
duke@435 | 236 | // First check the protocol version |
duke@435 | 237 | char* ver = args.next(); |
duke@435 | 238 | if (ver == NULL) { |
duke@435 | 239 | return NULL; |
duke@435 | 240 | } |
duke@435 | 241 | if (atoi(ver) != SolarisAttachListener::ATTACH_PROTOCOL_VER) { |
duke@435 | 242 | *err = SolarisAttachListener::ATTACH_ERROR_BADVERSION; |
duke@435 | 243 | return NULL; |
duke@435 | 244 | } |
duke@435 | 245 | |
duke@435 | 246 | // Get command name and create the operation |
duke@435 | 247 | char* name = args.next(); |
duke@435 | 248 | if (name == NULL || strlen(name) > AttachOperation::name_length_max) { |
duke@435 | 249 | return NULL; |
duke@435 | 250 | } |
duke@435 | 251 | SolarisAttachOperation* op = new SolarisAttachOperation(name); |
duke@435 | 252 | |
duke@435 | 253 | // Iterate over the arguments |
duke@435 | 254 | for (int i=0; i<AttachOperation::arg_count_max; i++) { |
duke@435 | 255 | char* arg = args.next(); |
duke@435 | 256 | if (arg == NULL) { |
duke@435 | 257 | op->set_arg(i, NULL); |
duke@435 | 258 | } else { |
duke@435 | 259 | if (strlen(arg) > AttachOperation::arg_length_max) { |
duke@435 | 260 | delete op; |
duke@435 | 261 | return NULL; |
duke@435 | 262 | } |
duke@435 | 263 | op->set_arg(i, arg); |
duke@435 | 264 | } |
duke@435 | 265 | } |
duke@435 | 266 | |
duke@435 | 267 | // return operation |
duke@435 | 268 | *err = 0; |
duke@435 | 269 | return op; |
duke@435 | 270 | } |
duke@435 | 271 | |
duke@435 | 272 | // create special operation to indicate all clients have detached |
duke@435 | 273 | static SolarisAttachOperation* create_detachall_operation() { |
duke@435 | 274 | return new SolarisAttachOperation(AttachOperation::detachall_operation_name()); |
duke@435 | 275 | } |
duke@435 | 276 | |
duke@435 | 277 | // This is door function which the client executes via a door_call. |
duke@435 | 278 | extern "C" { |
duke@435 | 279 | static void enqueue_proc(void* cookie, char* argp, size_t arg_size, |
duke@435 | 280 | door_desc_t* dt, uint_t n_desc) |
duke@435 | 281 | { |
duke@435 | 282 | int return_fd = -1; |
duke@435 | 283 | SolarisAttachOperation* op = NULL; |
duke@435 | 284 | |
duke@435 | 285 | // no listener |
duke@435 | 286 | jint res = 0; |
duke@435 | 287 | if (!AttachListener::is_initialized()) { |
duke@435 | 288 | // how did we get here? |
duke@435 | 289 | debug_only(warning("door_call when not enabled")); |
duke@435 | 290 | res = (jint)SolarisAttachListener::ATTACH_ERROR_INTERNAL; |
duke@435 | 291 | } |
duke@435 | 292 | |
duke@435 | 293 | // check client credentials |
duke@435 | 294 | if (res == 0) { |
duke@435 | 295 | if (check_credentials() != 0) { |
duke@435 | 296 | res = (jint)SolarisAttachListener::ATTACH_ERROR_DENIED; |
duke@435 | 297 | } |
duke@435 | 298 | } |
duke@435 | 299 | |
duke@435 | 300 | // if we are stopped at ShowMessageBoxOnError then maybe we can |
duke@435 | 301 | // load a diagnostic library |
duke@435 | 302 | if (res == 0 && is_error_reported()) { |
duke@435 | 303 | if (ShowMessageBoxOnError) { |
duke@435 | 304 | // TBD - support loading of diagnostic library here |
duke@435 | 305 | } |
duke@435 | 306 | |
duke@435 | 307 | // can't enqueue operation after fatal error |
duke@435 | 308 | res = (jint)SolarisAttachListener::ATTACH_ERROR_RESOURCE; |
duke@435 | 309 | } |
duke@435 | 310 | |
duke@435 | 311 | // create the operation |
duke@435 | 312 | if (res == 0) { |
duke@435 | 313 | int err; |
duke@435 | 314 | op = create_operation(argp, arg_size, &err); |
duke@435 | 315 | res = (op == NULL) ? (jint)err : 0; |
duke@435 | 316 | } |
duke@435 | 317 | |
duke@435 | 318 | // create a pair of connected sockets. Store the file descriptor |
duke@435 | 319 | // for one end in the operation and enqueue the operation. The |
duke@435 | 320 | // file descriptor for the other end wil be returned to the client. |
duke@435 | 321 | if (res == 0) { |
duke@435 | 322 | int s[2]; |
duke@435 | 323 | if (socketpair(PF_UNIX, SOCK_STREAM, 0, s) < 0) { |
duke@435 | 324 | delete op; |
duke@435 | 325 | res = (jint)SolarisAttachListener::ATTACH_ERROR_RESOURCE; |
duke@435 | 326 | } else { |
duke@435 | 327 | op->set_socket(s[0]); |
duke@435 | 328 | return_fd = s[1]; |
duke@435 | 329 | SolarisAttachListener::enqueue(op); |
duke@435 | 330 | } |
duke@435 | 331 | } |
duke@435 | 332 | |
duke@435 | 333 | // Return 0 (success) + file descriptor, or non-0 (error) |
duke@435 | 334 | if (res == 0) { |
duke@435 | 335 | door_desc_t desc; |
duke@435 | 336 | desc.d_attributes = DOOR_DESCRIPTOR; |
duke@435 | 337 | desc.d_data.d_desc.d_descriptor = return_fd; |
duke@435 | 338 | door_return((char*)&res, sizeof(res), &desc, 1); |
duke@435 | 339 | } else { |
duke@435 | 340 | door_return((char*)&res, sizeof(res), NULL, 0); |
duke@435 | 341 | } |
duke@435 | 342 | } |
duke@435 | 343 | } |
duke@435 | 344 | |
duke@435 | 345 | // atexit hook to detach the door and remove the file |
duke@435 | 346 | extern "C" { |
duke@435 | 347 | static void listener_cleanup() { |
duke@435 | 348 | static int cleanup_done; |
duke@435 | 349 | if (!cleanup_done) { |
duke@435 | 350 | cleanup_done = 1; |
duke@435 | 351 | int dd = SolarisAttachListener::door_descriptor(); |
duke@435 | 352 | if (dd >= 0) { |
duke@435 | 353 | ::close(dd); |
duke@435 | 354 | } |
duke@435 | 355 | if (SolarisAttachListener::has_door_path()) { |
duke@435 | 356 | char* path = SolarisAttachListener::door_path(); |
duke@435 | 357 | ::fdetach(path); |
duke@435 | 358 | ::unlink(path); |
duke@435 | 359 | } |
duke@435 | 360 | } |
duke@435 | 361 | } |
duke@435 | 362 | } |
duke@435 | 363 | |
duke@435 | 364 | // Create the door |
duke@435 | 365 | int SolarisAttachListener::create_door() { |
duke@435 | 366 | char door_path[PATH_MAX+1]; |
duke@435 | 367 | int fd, res; |
duke@435 | 368 | |
duke@435 | 369 | // register exit function |
duke@435 | 370 | ::atexit(listener_cleanup); |
duke@435 | 371 | |
duke@435 | 372 | // create the door descriptor |
duke@435 | 373 | int dd = ::door_create(enqueue_proc, NULL, 0); |
duke@435 | 374 | if (dd < 0) { |
duke@435 | 375 | return -1; |
duke@435 | 376 | } |
duke@435 | 377 | |
duke@435 | 378 | sprintf(door_path, "%s/.java_pid%d", os::get_temp_directory(), os::current_process_id()); |
duke@435 | 379 | RESTARTABLE(::creat(door_path, S_IRUSR | S_IWUSR), fd); |
duke@435 | 380 | |
duke@435 | 381 | if (fd == -1) { |
duke@435 | 382 | debug_only(warning("attempt to create %s failed", door_path)); |
duke@435 | 383 | return -1; |
duke@435 | 384 | } |
duke@435 | 385 | assert(fd >= 0, "bad file descriptor"); |
duke@435 | 386 | set_door_path(door_path); |
duke@435 | 387 | RESTARTABLE(::close(fd), res); |
duke@435 | 388 | |
duke@435 | 389 | // attach the door descriptor to the file |
duke@435 | 390 | if ((res = ::fattach(dd, door_path)) == -1) { |
duke@435 | 391 | // if busy then detach and try again |
duke@435 | 392 | if (errno == EBUSY) { |
duke@435 | 393 | ::fdetach(door_path); |
duke@435 | 394 | res = ::fattach(dd, door_path); |
duke@435 | 395 | } |
duke@435 | 396 | if (res == -1) { |
duke@435 | 397 | ::door_revoke(dd); |
duke@435 | 398 | dd = -1; |
duke@435 | 399 | } |
duke@435 | 400 | } |
duke@435 | 401 | if (dd >= 0) { |
duke@435 | 402 | set_door_descriptor(dd); |
duke@435 | 403 | } else { |
duke@435 | 404 | // unable to create door or attach it to the file |
duke@435 | 405 | ::unlink(door_path); |
duke@435 | 406 | set_door_path(NULL); |
duke@435 | 407 | return -1; |
duke@435 | 408 | } |
duke@435 | 409 | |
duke@435 | 410 | return 0; |
duke@435 | 411 | } |
duke@435 | 412 | |
duke@435 | 413 | // Initialization - create the door, locks, and other initialization |
duke@435 | 414 | int SolarisAttachListener::init() { |
duke@435 | 415 | if (create_door()) { |
duke@435 | 416 | return -1; |
duke@435 | 417 | } |
duke@435 | 418 | |
duke@435 | 419 | int status = os::Solaris::mutex_init(&_mutex); |
duke@435 | 420 | assert_status(status==0, status, "mutex_init"); |
duke@435 | 421 | |
duke@435 | 422 | status = ::sema_init(&_wakeup, 0, NULL, NULL); |
duke@435 | 423 | assert_status(status==0, status, "sema_init"); |
duke@435 | 424 | |
duke@435 | 425 | set_head(NULL); |
duke@435 | 426 | set_tail(NULL); |
duke@435 | 427 | |
duke@435 | 428 | return 0; |
duke@435 | 429 | } |
duke@435 | 430 | |
duke@435 | 431 | // Dequeue an operation |
duke@435 | 432 | SolarisAttachOperation* SolarisAttachListener::dequeue() { |
duke@435 | 433 | for (;;) { |
duke@435 | 434 | int res; |
duke@435 | 435 | |
duke@435 | 436 | // wait for somebody to enqueue something |
duke@435 | 437 | while ((res = ::sema_wait(wakeup())) == EINTR) |
duke@435 | 438 | ; |
duke@435 | 439 | if (res) { |
duke@435 | 440 | warning("sema_wait failed: %s", strerror(res)); |
duke@435 | 441 | return NULL; |
duke@435 | 442 | } |
duke@435 | 443 | |
duke@435 | 444 | // lock the list |
duke@435 | 445 | res = os::Solaris::mutex_lock(mutex()); |
duke@435 | 446 | assert(res == 0, "mutex_lock failed"); |
duke@435 | 447 | |
duke@435 | 448 | // remove the head of the list |
duke@435 | 449 | SolarisAttachOperation* op = head(); |
duke@435 | 450 | if (op != NULL) { |
duke@435 | 451 | set_head(op->next()); |
duke@435 | 452 | if (head() == NULL) { |
duke@435 | 453 | set_tail(NULL); |
duke@435 | 454 | } |
duke@435 | 455 | } |
duke@435 | 456 | |
duke@435 | 457 | // unlock |
duke@435 | 458 | os::Solaris::mutex_unlock(mutex()); |
duke@435 | 459 | |
duke@435 | 460 | // if we got an operation when return it. |
duke@435 | 461 | if (op != NULL) { |
duke@435 | 462 | return op; |
duke@435 | 463 | } |
duke@435 | 464 | } |
duke@435 | 465 | } |
duke@435 | 466 | |
duke@435 | 467 | // Enqueue an operation |
duke@435 | 468 | void SolarisAttachListener::enqueue(SolarisAttachOperation* op) { |
duke@435 | 469 | // lock list |
duke@435 | 470 | int res = os::Solaris::mutex_lock(mutex()); |
duke@435 | 471 | assert(res == 0, "mutex_lock failed"); |
duke@435 | 472 | |
duke@435 | 473 | // enqueue at tail |
duke@435 | 474 | op->set_next(NULL); |
duke@435 | 475 | if (head() == NULL) { |
duke@435 | 476 | set_head(op); |
duke@435 | 477 | } else { |
duke@435 | 478 | tail()->set_next(op); |
duke@435 | 479 | } |
duke@435 | 480 | set_tail(op); |
duke@435 | 481 | |
duke@435 | 482 | // wakeup the attach listener |
duke@435 | 483 | RESTARTABLE(::sema_post(wakeup()), res); |
duke@435 | 484 | assert(res == 0, "sema_post failed"); |
duke@435 | 485 | |
duke@435 | 486 | // unlock |
duke@435 | 487 | os::Solaris::mutex_unlock(mutex()); |
duke@435 | 488 | } |
duke@435 | 489 | |
duke@435 | 490 | |
duke@435 | 491 | // support function - writes the (entire) buffer to a socket |
duke@435 | 492 | static int write_fully(int s, char* buf, int len) { |
duke@435 | 493 | do { |
duke@435 | 494 | int n = ::write(s, buf, len); |
duke@435 | 495 | if (n == -1) { |
duke@435 | 496 | if (errno != EINTR) return -1; |
duke@435 | 497 | } else { |
duke@435 | 498 | buf += n; |
duke@435 | 499 | len -= n; |
duke@435 | 500 | } |
duke@435 | 501 | } |
duke@435 | 502 | while (len > 0); |
duke@435 | 503 | return 0; |
duke@435 | 504 | } |
duke@435 | 505 | |
duke@435 | 506 | // Complete an operation by sending the operation result and any result |
duke@435 | 507 | // output to the client. At this time the socket is in blocking mode so |
duke@435 | 508 | // potentially we can block if there is a lot of data and the client is |
duke@435 | 509 | // non-responsive. For most operations this is a non-issue because the |
duke@435 | 510 | // default send buffer is sufficient to buffer everything. In the future |
duke@435 | 511 | // if there are operations that involves a very big reply then it the |
duke@435 | 512 | // socket could be made non-blocking and a timeout could be used. |
duke@435 | 513 | |
duke@435 | 514 | void SolarisAttachOperation::complete(jint res, bufferedStream* st) { |
duke@435 | 515 | if (this->socket() >= 0) { |
duke@435 | 516 | JavaThread* thread = JavaThread::current(); |
duke@435 | 517 | ThreadBlockInVM tbivm(thread); |
duke@435 | 518 | |
duke@435 | 519 | thread->set_suspend_equivalent(); |
duke@435 | 520 | // cleared by handle_special_suspend_equivalent_condition() or |
duke@435 | 521 | // java_suspend_self() via check_and_wait_while_suspended() |
duke@435 | 522 | |
duke@435 | 523 | // write operation result |
duke@435 | 524 | char msg[32]; |
duke@435 | 525 | sprintf(msg, "%d\n", res); |
duke@435 | 526 | int rc = write_fully(this->socket(), msg, strlen(msg)); |
duke@435 | 527 | |
duke@435 | 528 | // write any result data |
duke@435 | 529 | if (rc == 0) { |
duke@435 | 530 | write_fully(this->socket(), (char*) st->base(), st->size()); |
duke@435 | 531 | ::shutdown(this->socket(), 2); |
duke@435 | 532 | } |
duke@435 | 533 | |
duke@435 | 534 | // close socket and we're done |
duke@435 | 535 | RESTARTABLE(::close(this->socket()), rc); |
duke@435 | 536 | |
duke@435 | 537 | // were we externally suspended while we were waiting? |
duke@435 | 538 | thread->check_and_wait_while_suspended(); |
duke@435 | 539 | } |
duke@435 | 540 | delete this; |
duke@435 | 541 | } |
duke@435 | 542 | |
duke@435 | 543 | |
duke@435 | 544 | // AttachListener functions |
duke@435 | 545 | |
duke@435 | 546 | AttachOperation* AttachListener::dequeue() { |
duke@435 | 547 | JavaThread* thread = JavaThread::current(); |
duke@435 | 548 | ThreadBlockInVM tbivm(thread); |
duke@435 | 549 | |
duke@435 | 550 | thread->set_suspend_equivalent(); |
duke@435 | 551 | // cleared by handle_special_suspend_equivalent_condition() or |
duke@435 | 552 | // java_suspend_self() via check_and_wait_while_suspended() |
duke@435 | 553 | |
duke@435 | 554 | AttachOperation* op = SolarisAttachListener::dequeue(); |
duke@435 | 555 | |
duke@435 | 556 | // were we externally suspended while we were waiting? |
duke@435 | 557 | thread->check_and_wait_while_suspended(); |
duke@435 | 558 | |
duke@435 | 559 | return op; |
duke@435 | 560 | } |
duke@435 | 561 | |
duke@435 | 562 | int AttachListener::pd_init() { |
duke@435 | 563 | JavaThread* thread = JavaThread::current(); |
duke@435 | 564 | ThreadBlockInVM tbivm(thread); |
duke@435 | 565 | |
duke@435 | 566 | thread->set_suspend_equivalent(); |
duke@435 | 567 | // cleared by handle_special_suspend_equivalent_condition() or |
duke@435 | 568 | // java_suspend_self() |
duke@435 | 569 | |
duke@435 | 570 | int ret_code = SolarisAttachListener::init(); |
duke@435 | 571 | |
duke@435 | 572 | // were we externally suspended while we were waiting? |
duke@435 | 573 | thread->check_and_wait_while_suspended(); |
duke@435 | 574 | |
duke@435 | 575 | return ret_code; |
duke@435 | 576 | } |
duke@435 | 577 | |
duke@435 | 578 | // Attach Listener is started lazily except in the case when |
duke@435 | 579 | // +ReduseSignalUsage is used |
duke@435 | 580 | bool AttachListener::init_at_startup() { |
duke@435 | 581 | if (ReduceSignalUsage) { |
duke@435 | 582 | return true; |
duke@435 | 583 | } else { |
duke@435 | 584 | return false; |
duke@435 | 585 | } |
duke@435 | 586 | } |
duke@435 | 587 | |
duke@435 | 588 | // If the file .attach_pid<pid> exists in the working directory |
duke@435 | 589 | // or /tmp then this is the trigger to start the attach mechanism |
duke@435 | 590 | bool AttachListener::is_init_trigger() { |
duke@435 | 591 | if (init_at_startup() || is_initialized()) { |
duke@435 | 592 | return false; // initialized at startup or already initialized |
duke@435 | 593 | } |
duke@435 | 594 | char fn[32]; |
duke@435 | 595 | sprintf(fn, ".attach_pid%d", os::current_process_id()); |
duke@435 | 596 | int ret; |
duke@435 | 597 | struct stat64 st; |
duke@435 | 598 | RESTARTABLE(::stat64(fn, &st), ret); |
duke@435 | 599 | if (ret == -1) { |
duke@435 | 600 | sprintf(fn, "/tmp/.attach_pid%d", os::current_process_id()); |
duke@435 | 601 | RESTARTABLE(::stat64(fn, &st), ret); |
duke@435 | 602 | } |
duke@435 | 603 | if (ret == 0) { |
duke@435 | 604 | // simple check to avoid starting the attach mechanism when |
duke@435 | 605 | // a bogus user creates the file |
duke@435 | 606 | if (st.st_uid == geteuid()) { |
duke@435 | 607 | init(); |
duke@435 | 608 | return true; |
duke@435 | 609 | } |
duke@435 | 610 | } |
duke@435 | 611 | return false; |
duke@435 | 612 | } |
duke@435 | 613 | |
duke@435 | 614 | // if VM aborts then detach/cleanup |
duke@435 | 615 | void AttachListener::abort() { |
duke@435 | 616 | listener_cleanup(); |
duke@435 | 617 | } |
duke@435 | 618 | |
duke@435 | 619 | void AttachListener::pd_data_dump() { |
duke@435 | 620 | os::signal_notify(SIGQUIT); |
duke@435 | 621 | } |
duke@435 | 622 | |
duke@435 | 623 | static jint enable_dprobes(AttachOperation* op, outputStream* out) { |
duke@435 | 624 | const char* probe = op->arg(0); |
duke@435 | 625 | if (probe == NULL || probe[0] == '\0') { |
duke@435 | 626 | out->print_cr("No probe specified"); |
duke@435 | 627 | return JNI_ERR; |
duke@435 | 628 | } else { |
duke@435 | 629 | int probe_typess = atoi(probe); |
duke@435 | 630 | if (errno) { |
duke@435 | 631 | out->print_cr("invalid probe type"); |
duke@435 | 632 | return JNI_ERR; |
duke@435 | 633 | } else { |
duke@435 | 634 | DTrace::enable_dprobes(probe_typess); |
duke@435 | 635 | return JNI_OK; |
duke@435 | 636 | } |
duke@435 | 637 | } |
duke@435 | 638 | } |
duke@435 | 639 | |
duke@435 | 640 | // platform specific operations table |
duke@435 | 641 | static AttachOperationFunctionInfo funcs[] = { |
duke@435 | 642 | { "enabledprobes", enable_dprobes }, |
duke@435 | 643 | { NULL, NULL } |
duke@435 | 644 | }; |
duke@435 | 645 | |
duke@435 | 646 | AttachOperationFunctionInfo* AttachListener::pd_find_operation(const char* name) { |
duke@435 | 647 | int i; |
duke@435 | 648 | for (i = 0; funcs[i].name != NULL; i++) { |
duke@435 | 649 | if (strcmp(funcs[i].name, name) == 0) { |
duke@435 | 650 | return &funcs[i]; |
duke@435 | 651 | } |
duke@435 | 652 | } |
duke@435 | 653 | return NULL; |
duke@435 | 654 | } |
duke@435 | 655 | |
duke@435 | 656 | // Solaris specific global flag set. Currently, we support only |
duke@435 | 657 | // changing ExtendedDTraceProbes flag. |
duke@435 | 658 | jint AttachListener::pd_set_flag(AttachOperation* op, outputStream* out) { |
duke@435 | 659 | const char* name = op->arg(0); |
duke@435 | 660 | assert(name != NULL, "flag name should not be null"); |
duke@435 | 661 | bool flag = true; |
duke@435 | 662 | const char* arg1; |
duke@435 | 663 | if ((arg1 = op->arg(1)) != NULL) { |
duke@435 | 664 | flag = (atoi(arg1) != 0); |
duke@435 | 665 | if (errno) { |
duke@435 | 666 | out->print_cr("flag value has to be an integer"); |
duke@435 | 667 | return JNI_ERR; |
duke@435 | 668 | } |
duke@435 | 669 | } |
duke@435 | 670 | |
duke@435 | 671 | if (strcmp(name, "ExtendedDTraceProbes") != 0) { |
duke@435 | 672 | out->print_cr("flag '%s' cannot be changed", name); |
duke@435 | 673 | return JNI_ERR; |
duke@435 | 674 | } |
duke@435 | 675 | |
duke@435 | 676 | DTrace::set_extended_dprobes(flag); |
duke@435 | 677 | return JNI_OK; |
duke@435 | 678 | } |
duke@435 | 679 | |
duke@435 | 680 | void AttachListener::pd_detachall() { |
duke@435 | 681 | DTrace::detach_all_clients(); |
duke@435 | 682 | } |