Mon, 03 Jan 2011 14:09:11 -0500
6302804: Hotspot VM dies ungraceful death when C heap is exhausted in various places.
Summary: enhance the error reporting mechanism to help user to fix the problem rather than making it look like a VM error.
Reviewed-by: kvn, kamg
duke@435 | 1 | /* |
stefank@2314 | 2 | * Copyright (c) 1997, 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 | #include "precompiled.hpp" |
stefank@2314 | 26 | #include "memory/allocation.inline.hpp" |
stefank@2314 | 27 | #include "runtime/mutexLocker.hpp" |
stefank@2314 | 28 | #include "runtime/osThread.hpp" |
stefank@2314 | 29 | #include "runtime/threadLocalStorage.hpp" |
stefank@2314 | 30 | #include "runtime/timer.hpp" |
stefank@2314 | 31 | #include "utilities/events.hpp" |
stefank@2314 | 32 | #ifdef TARGET_OS_FAMILY_linux |
stefank@2314 | 33 | # include "thread_linux.inline.hpp" |
stefank@2314 | 34 | #endif |
stefank@2314 | 35 | #ifdef TARGET_OS_FAMILY_solaris |
stefank@2314 | 36 | # include "thread_solaris.inline.hpp" |
stefank@2314 | 37 | #endif |
stefank@2314 | 38 | #ifdef TARGET_OS_FAMILY_windows |
stefank@2314 | 39 | # include "thread_windows.inline.hpp" |
stefank@2314 | 40 | #endif |
duke@435 | 41 | |
duke@435 | 42 | |
duke@435 | 43 | #ifndef PRODUCT |
duke@435 | 44 | |
duke@435 | 45 | //////////////////////////////////////////////////////////////////////////// |
duke@435 | 46 | // Event |
duke@435 | 47 | |
duke@435 | 48 | typedef u4 EventID; |
duke@435 | 49 | |
duke@435 | 50 | class Event VALUE_OBJ_CLASS_SPEC { |
duke@435 | 51 | private: |
duke@435 | 52 | jlong _time_tick; |
duke@435 | 53 | intx _thread_id; |
duke@435 | 54 | const char* _format; |
duke@435 | 55 | int _indent; |
duke@435 | 56 | intptr_t _arg_1; |
duke@435 | 57 | intptr_t _arg_2; |
duke@435 | 58 | intptr_t _arg_3; |
duke@435 | 59 | |
duke@435 | 60 | // only EventBuffer::add_event() can assign event id |
duke@435 | 61 | friend class EventBuffer; |
duke@435 | 62 | EventID _id; |
duke@435 | 63 | |
duke@435 | 64 | public: |
duke@435 | 65 | |
duke@435 | 66 | void clear() { _format = NULL; } |
duke@435 | 67 | |
duke@435 | 68 | EventID id() const { return _id; } |
duke@435 | 69 | |
duke@435 | 70 | void fill(int indent, const char* format, intptr_t arg_1, intptr_t arg_2, intptr_t arg_3) { |
duke@435 | 71 | _format = format; |
duke@435 | 72 | _arg_1 = arg_1; |
duke@435 | 73 | _arg_2 = arg_2; |
duke@435 | 74 | _arg_3 = arg_3; |
duke@435 | 75 | |
duke@435 | 76 | _indent = indent; |
duke@435 | 77 | |
duke@435 | 78 | _thread_id = os::current_thread_id(); |
duke@435 | 79 | _time_tick = os::elapsed_counter(); |
duke@435 | 80 | } |
duke@435 | 81 | |
duke@435 | 82 | void print_on(outputStream *st) { |
duke@435 | 83 | if (_format == NULL) return; |
duke@435 | 84 | st->print(" %d", _thread_id); |
duke@435 | 85 | st->print(" %3.2g ", (double)_time_tick / os::elapsed_frequency()); |
duke@435 | 86 | st->fill_to(20); |
duke@435 | 87 | for (int index = 0; index < _indent; index++) { |
duke@435 | 88 | st->print("| "); |
duke@435 | 89 | } |
duke@435 | 90 | st->print_cr(_format, _arg_1, _arg_2, _arg_3); |
duke@435 | 91 | } |
duke@435 | 92 | }; |
duke@435 | 93 | |
duke@435 | 94 | //////////////////////////////////////////////////////////////////////////// |
duke@435 | 95 | // EventBuffer |
duke@435 | 96 | // |
duke@435 | 97 | // Simple lock-free event queue. Every event has a unique 32-bit id. |
duke@435 | 98 | // It's fine if two threads add events at the same time, because they |
duke@435 | 99 | // will get different event id, and then write to different buffer location. |
duke@435 | 100 | // However, it is assumed that add_event() is quick enough (or buffer size |
duke@435 | 101 | // is big enough), so when one thread is adding event, there can't be more |
duke@435 | 102 | // than "size" events created by other threads; otherwise we'll end up having |
duke@435 | 103 | // two threads writing to the same location. |
duke@435 | 104 | |
duke@435 | 105 | class EventBuffer : AllStatic { |
duke@435 | 106 | private: |
duke@435 | 107 | static Event* buffer; |
duke@435 | 108 | static int size; |
duke@435 | 109 | static jint indent; |
duke@435 | 110 | static volatile EventID _current_event_id; |
duke@435 | 111 | |
duke@435 | 112 | static EventID get_next_event_id() { |
duke@435 | 113 | return (EventID)Atomic::add(1, (jint*)&_current_event_id); |
duke@435 | 114 | } |
duke@435 | 115 | |
duke@435 | 116 | public: |
duke@435 | 117 | static void inc_indent() { Atomic::inc(&indent); } |
duke@435 | 118 | static void dec_indent() { Atomic::dec(&indent); } |
duke@435 | 119 | |
duke@435 | 120 | static bool get_event(EventID id, Event* event) { |
duke@435 | 121 | int index = (int)(id % size); |
duke@435 | 122 | if (buffer[index].id() == id) { |
duke@435 | 123 | memcpy(event, &buffer[index], sizeof(Event)); |
duke@435 | 124 | // check id again; if buffer[index] is being updated by another thread, |
duke@435 | 125 | // event->id() will contain different value. |
duke@435 | 126 | return (event->id() == id); |
duke@435 | 127 | } else { |
duke@435 | 128 | // id does not match - id is invalid, or event is overwritten |
duke@435 | 129 | return false; |
duke@435 | 130 | } |
duke@435 | 131 | } |
duke@435 | 132 | |
duke@435 | 133 | // add a new event to the queue; if EventBuffer is full, this call will |
duke@435 | 134 | // overwrite the oldest event in the queue |
duke@435 | 135 | static EventID add_event(const char* format, |
duke@435 | 136 | intptr_t arg_1, intptr_t arg_2, intptr_t arg_3) { |
duke@435 | 137 | // assign a unique id |
duke@435 | 138 | EventID id = get_next_event_id(); |
duke@435 | 139 | |
duke@435 | 140 | // event will be copied to buffer[index] |
duke@435 | 141 | int index = (int)(id % size); |
duke@435 | 142 | |
duke@435 | 143 | // first, invalidate id, buffer[index] can't have event with id = index + 2 |
duke@435 | 144 | buffer[index]._id = index + 2; |
duke@435 | 145 | |
duke@435 | 146 | // make sure everyone has seen that buffer[index] is invalid |
duke@435 | 147 | OrderAccess::fence(); |
duke@435 | 148 | |
duke@435 | 149 | // ... before updating its value |
duke@435 | 150 | buffer[index].fill(indent, format, arg_1, arg_2, arg_3); |
duke@435 | 151 | |
duke@435 | 152 | // finally, set up real event id, now buffer[index] contains valid event |
duke@435 | 153 | OrderAccess::release_store(&(buffer[index]._id), id); |
duke@435 | 154 | |
duke@435 | 155 | return id; |
duke@435 | 156 | } |
duke@435 | 157 | |
duke@435 | 158 | static void print_last(outputStream *st, int number) { |
duke@435 | 159 | st->print_cr("[Last %d events in the event buffer]", number); |
duke@435 | 160 | st->print_cr("-<thd>-<elapsed sec>-<description>---------------------"); |
duke@435 | 161 | |
duke@435 | 162 | int count = 0; |
duke@435 | 163 | EventID id = _current_event_id; |
duke@435 | 164 | while (count < number) { |
duke@435 | 165 | Event event; |
duke@435 | 166 | if (get_event(id, &event)) { |
duke@435 | 167 | event.print_on(st); |
duke@435 | 168 | } |
duke@435 | 169 | id--; |
duke@435 | 170 | count++; |
duke@435 | 171 | } |
duke@435 | 172 | } |
duke@435 | 173 | |
duke@435 | 174 | static void print_all(outputStream* st) { |
duke@435 | 175 | print_last(st, size); |
duke@435 | 176 | } |
duke@435 | 177 | |
duke@435 | 178 | static void init() { |
duke@435 | 179 | // Allocate the event buffer |
duke@435 | 180 | size = EventLogLength; |
duke@435 | 181 | buffer = NEW_C_HEAP_ARRAY(Event, size); |
duke@435 | 182 | |
duke@435 | 183 | _current_event_id = 0; |
duke@435 | 184 | |
duke@435 | 185 | // Clear the event buffer |
duke@435 | 186 | for (int index = 0; index < size; index++) { |
duke@435 | 187 | buffer[index]._id = index + 1; // index + 1 is invalid id |
duke@435 | 188 | buffer[index].clear(); |
duke@435 | 189 | } |
duke@435 | 190 | } |
duke@435 | 191 | }; |
duke@435 | 192 | |
duke@435 | 193 | Event* EventBuffer::buffer; |
duke@435 | 194 | int EventBuffer::size; |
duke@435 | 195 | volatile EventID EventBuffer::_current_event_id; |
duke@435 | 196 | int EventBuffer::indent; |
duke@435 | 197 | |
duke@435 | 198 | //////////////////////////////////////////////////////////////////////////// |
duke@435 | 199 | // Events |
duke@435 | 200 | |
duke@435 | 201 | // Events::log() is safe for signal handlers |
duke@435 | 202 | void Events::log(const char* format, ...) { |
duke@435 | 203 | if (LogEvents) { |
duke@435 | 204 | va_list ap; |
duke@435 | 205 | va_start(ap, format); |
duke@435 | 206 | intptr_t arg_1 = va_arg(ap, intptr_t); |
duke@435 | 207 | intptr_t arg_2 = va_arg(ap, intptr_t); |
duke@435 | 208 | intptr_t arg_3 = va_arg(ap, intptr_t); |
duke@435 | 209 | va_end(ap); |
duke@435 | 210 | |
duke@435 | 211 | EventBuffer::add_event(format, arg_1, arg_2, arg_3); |
duke@435 | 212 | } |
duke@435 | 213 | } |
duke@435 | 214 | |
duke@435 | 215 | void Events::print_all(outputStream *st) { |
duke@435 | 216 | EventBuffer::print_all(st); |
duke@435 | 217 | } |
duke@435 | 218 | |
duke@435 | 219 | void Events::print_last(outputStream *st, int number) { |
duke@435 | 220 | EventBuffer::print_last(st, number); |
duke@435 | 221 | } |
duke@435 | 222 | |
duke@435 | 223 | /////////////////////////////////////////////////////////////////////////// |
duke@435 | 224 | // EventMark |
duke@435 | 225 | |
duke@435 | 226 | EventMark::EventMark(const char* format, ...) { |
duke@435 | 227 | if (LogEvents) { |
duke@435 | 228 | va_list ap; |
duke@435 | 229 | va_start(ap, format); |
duke@435 | 230 | intptr_t arg_1 = va_arg(ap, intptr_t); |
duke@435 | 231 | intptr_t arg_2 = va_arg(ap, intptr_t); |
duke@435 | 232 | intptr_t arg_3 = va_arg(ap, intptr_t); |
duke@435 | 233 | va_end(ap); |
duke@435 | 234 | |
duke@435 | 235 | EventBuffer::add_event(format, arg_1, arg_2, arg_3); |
duke@435 | 236 | EventBuffer::inc_indent(); |
duke@435 | 237 | } |
duke@435 | 238 | } |
duke@435 | 239 | |
duke@435 | 240 | EventMark::~EventMark() { |
duke@435 | 241 | if (LogEvents) { |
duke@435 | 242 | EventBuffer::dec_indent(); |
duke@435 | 243 | EventBuffer::add_event("done", 0, 0, 0); |
duke@435 | 244 | } |
duke@435 | 245 | } |
duke@435 | 246 | |
duke@435 | 247 | /////////////////////////////////////////////////////////////////////////// |
duke@435 | 248 | |
duke@435 | 249 | void eventlog_init() { |
duke@435 | 250 | EventBuffer::init(); |
duke@435 | 251 | } |
duke@435 | 252 | |
duke@435 | 253 | int print_all_events(outputStream *st) { |
duke@435 | 254 | EventBuffer::print_all(st); |
duke@435 | 255 | return 1; |
duke@435 | 256 | } |
duke@435 | 257 | |
duke@435 | 258 | #else |
duke@435 | 259 | |
duke@435 | 260 | void eventlog_init() {} |
duke@435 | 261 | int print_all_events(outputStream *st) { return 0; } |
duke@435 | 262 | |
duke@435 | 263 | #endif // PRODUCT |