src/share/vm/utilities/events.hpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6680
78bbf4d43a14
parent 0
f90c822e73f8
child 8856
ac27a9c85bea
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_UTILITIES_EVENTS_HPP
aoqi@0 26 #define SHARE_VM_UTILITIES_EVENTS_HPP
aoqi@0 27
aoqi@0 28 #include "memory/allocation.hpp"
aoqi@0 29 #include "runtime/mutexLocker.hpp"
aoqi@0 30 #include "runtime/thread.hpp"
aoqi@0 31 #include "utilities/top.hpp"
aoqi@0 32 #include "utilities/vmError.hpp"
aoqi@0 33
aoqi@0 34 // Events and EventMark provide interfaces to log events taking place in the vm.
aoqi@0 35 // This facility is extremly useful for post-mortem debugging. The eventlog
aoqi@0 36 // often provides crucial information about events leading up to the crash.
aoqi@0 37 //
aoqi@0 38 // Abstractly the logs can record whatever they way but normally they
aoqi@0 39 // would record at least a timestamp and the current Thread, along
aoqi@0 40 // with whatever data they need in a ring buffer. Commonly fixed
aoqi@0 41 // length text messages are recorded for simplicity but other
aoqi@0 42 // strategies could be used. Several logs are provided by default but
aoqi@0 43 // new instances can be created as needed.
aoqi@0 44
aoqi@0 45 // The base event log dumping class that is registered for dumping at
aoqi@0 46 // crash time. This is a very generic interface that is mainly here
aoqi@0 47 // for completeness. Normally the templated EventLogBase would be
aoqi@0 48 // subclassed to provide different log types.
aoqi@0 49 class EventLog : public CHeapObj<mtInternal> {
aoqi@0 50 friend class Events;
aoqi@0 51
aoqi@0 52 private:
aoqi@0 53 EventLog* _next;
aoqi@0 54
aoqi@0 55 EventLog* next() const { return _next; }
aoqi@0 56
aoqi@0 57 public:
aoqi@0 58 // Automatically registers the log so that it will be printed during
aoqi@0 59 // crashes.
aoqi@0 60 EventLog();
aoqi@0 61
aoqi@0 62 virtual void print_log_on(outputStream* out) = 0;
aoqi@0 63 };
aoqi@0 64
aoqi@0 65
aoqi@0 66 // A templated subclass of EventLog that provides basic ring buffer
aoqi@0 67 // functionality. Most event loggers should subclass this, possibly
aoqi@0 68 // providing a more featureful log function if the existing copy
aoqi@0 69 // semantics aren't appropriate. The name is used as the label of the
aoqi@0 70 // log when it is dumped during a crash.
aoqi@0 71 template <class T> class EventLogBase : public EventLog {
aoqi@0 72 template <class X> class EventRecord : public CHeapObj<mtInternal> {
aoqi@0 73 public:
aoqi@0 74 double timestamp;
aoqi@0 75 Thread* thread;
aoqi@0 76 X data;
aoqi@0 77 };
aoqi@0 78
aoqi@0 79 protected:
aoqi@0 80 Mutex _mutex;
aoqi@0 81 const char* _name;
aoqi@0 82 int _length;
aoqi@0 83 int _index;
aoqi@0 84 int _count;
aoqi@0 85 EventRecord<T>* _records;
aoqi@0 86
aoqi@0 87 public:
aoqi@0 88 EventLogBase<T>(const char* name, int length = LogEventsBufferEntries):
aoqi@0 89 _name(name),
aoqi@0 90 _length(length),
aoqi@0 91 _count(0),
aoqi@0 92 _index(0),
aoqi@0 93 _mutex(Mutex::event, name) {
aoqi@0 94 _records = new EventRecord<T>[length];
aoqi@0 95 }
aoqi@0 96
aoqi@0 97 double fetch_timestamp() {
aoqi@0 98 return os::elapsedTime();
aoqi@0 99 }
aoqi@0 100
aoqi@0 101 // move the ring buffer to next open slot and return the index of
aoqi@0 102 // the slot to use for the current message. Should only be called
aoqi@0 103 // while mutex is held.
aoqi@0 104 int compute_log_index() {
aoqi@0 105 int index = _index;
aoqi@0 106 if (_count < _length) _count++;
aoqi@0 107 _index++;
aoqi@0 108 if (_index >= _length) _index = 0;
aoqi@0 109 return index;
aoqi@0 110 }
aoqi@0 111
aoqi@0 112 bool should_log() {
aoqi@0 113 // Don't bother adding new entries when we're crashing. This also
aoqi@0 114 // avoids mutating the ring buffer when printing the log.
aoqi@0 115 return !VMError::fatal_error_in_progress();
aoqi@0 116 }
aoqi@0 117
aoqi@0 118 // Print the contents of the log
aoqi@0 119 void print_log_on(outputStream* out);
aoqi@0 120
aoqi@0 121 private:
aoqi@0 122 void print_log_impl(outputStream* out);
aoqi@0 123
aoqi@0 124 // Print a single element. A templated implementation might need to
aoqi@0 125 // be declared by subclasses.
aoqi@0 126 void print(outputStream* out, T& e);
aoqi@0 127
aoqi@0 128 void print(outputStream* out, EventRecord<T>& e) {
aoqi@0 129 out->print("Event: %.3f ", e.timestamp);
aoqi@0 130 if (e.thread != NULL) {
aoqi@0 131 out->print("Thread " INTPTR_FORMAT " ", p2i(e.thread));
aoqi@0 132 }
aoqi@0 133 print(out, e.data);
aoqi@0 134 }
aoqi@0 135 };
aoqi@0 136
aoqi@0 137 // A simple wrapper class for fixed size text messages.
aoqi@0 138 class StringLogMessage : public FormatBuffer<256> {
aoqi@0 139 public:
aoqi@0 140 // Wrap this buffer in a stringStream.
aoqi@0 141 stringStream stream() {
aoqi@0 142 return stringStream(_buf, size());
aoqi@0 143 }
aoqi@0 144 };
aoqi@0 145
aoqi@0 146 // A simple ring buffer of fixed size text messages.
aoqi@0 147 class StringEventLog : public EventLogBase<StringLogMessage> {
aoqi@0 148 public:
aoqi@0 149 StringEventLog(const char* name, int count = LogEventsBufferEntries) : EventLogBase<StringLogMessage>(name, count) {}
aoqi@0 150
aoqi@0 151 void logv(Thread* thread, const char* format, va_list ap) ATTRIBUTE_PRINTF(3, 0) {
aoqi@0 152 if (!should_log()) return;
aoqi@0 153
aoqi@0 154 double timestamp = fetch_timestamp();
aoqi@0 155 MutexLockerEx ml(&_mutex, Mutex::_no_safepoint_check_flag);
aoqi@0 156 int index = compute_log_index();
aoqi@0 157 _records[index].thread = thread;
aoqi@0 158 _records[index].timestamp = timestamp;
aoqi@0 159 _records[index].data.printv(format, ap);
aoqi@0 160 }
aoqi@0 161
aoqi@0 162 void log(Thread* thread, const char* format, ...) ATTRIBUTE_PRINTF(3, 4) {
aoqi@0 163 va_list ap;
aoqi@0 164 va_start(ap, format);
aoqi@0 165 logv(thread, format, ap);
aoqi@0 166 va_end(ap);
aoqi@0 167 }
aoqi@0 168
aoqi@0 169 };
aoqi@0 170
aoqi@0 171
aoqi@0 172
aoqi@0 173 class Events : AllStatic {
aoqi@0 174 friend class EventLog;
aoqi@0 175
aoqi@0 176 private:
aoqi@0 177 static EventLog* _logs;
aoqi@0 178
aoqi@0 179 // A log for generic messages that aren't well categorized.
aoqi@0 180 static StringEventLog* _messages;
aoqi@0 181
aoqi@0 182 // A log for internal exception related messages, like internal
aoqi@0 183 // throws and implicit exceptions.
aoqi@0 184 static StringEventLog* _exceptions;
aoqi@0 185
aoqi@0 186 // Deoptization related messages
aoqi@0 187 static StringEventLog* _deopt_messages;
aoqi@0 188
aoqi@0 189 public:
aoqi@0 190 static void print_all(outputStream* out);
aoqi@0 191
aoqi@0 192 // Dump all events to the tty
aoqi@0 193 static void print();
aoqi@0 194
aoqi@0 195 // Logs a generic message with timestamp and format as printf.
aoqi@0 196 static void log(Thread* thread, const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
aoqi@0 197
aoqi@0 198 // Log exception related message
aoqi@0 199 static void log_exception(Thread* thread, const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
aoqi@0 200
aoqi@0 201 static void log_deopt_message(Thread* thread, const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
aoqi@0 202
aoqi@0 203 // Register default loggers
aoqi@0 204 static void init();
aoqi@0 205 };
aoqi@0 206
aoqi@0 207 inline void Events::log(Thread* thread, const char* format, ...) {
aoqi@0 208 if (LogEvents) {
aoqi@0 209 va_list ap;
aoqi@0 210 va_start(ap, format);
aoqi@0 211 _messages->logv(thread, format, ap);
aoqi@0 212 va_end(ap);
aoqi@0 213 }
aoqi@0 214 }
aoqi@0 215
aoqi@0 216 inline void Events::log_exception(Thread* thread, const char* format, ...) {
aoqi@0 217 if (LogEvents) {
aoqi@0 218 va_list ap;
aoqi@0 219 va_start(ap, format);
aoqi@0 220 _exceptions->logv(thread, format, ap);
aoqi@0 221 va_end(ap);
aoqi@0 222 }
aoqi@0 223 }
aoqi@0 224
aoqi@0 225 inline void Events::log_deopt_message(Thread* thread, const char* format, ...) {
aoqi@0 226 if (LogEvents) {
aoqi@0 227 va_list ap;
aoqi@0 228 va_start(ap, format);
aoqi@0 229 _deopt_messages->logv(thread, format, ap);
aoqi@0 230 va_end(ap);
aoqi@0 231 }
aoqi@0 232 }
aoqi@0 233
aoqi@0 234
aoqi@0 235 template <class T>
aoqi@0 236 inline void EventLogBase<T>::print_log_on(outputStream* out) {
aoqi@0 237 if (ThreadLocalStorage::get_thread_slow() == NULL) {
aoqi@0 238 // Not a regular Java thread so don't bother locking
aoqi@0 239 print_log_impl(out);
aoqi@0 240 } else {
aoqi@0 241 MutexLockerEx ml(&_mutex, Mutex::_no_safepoint_check_flag);
aoqi@0 242 print_log_impl(out);
aoqi@0 243 }
aoqi@0 244 }
aoqi@0 245
aoqi@0 246 // Dump the ring buffer entries that current have entries.
aoqi@0 247 template <class T>
aoqi@0 248 inline void EventLogBase<T>::print_log_impl(outputStream* out) {
aoqi@0 249 out->print_cr("%s (%d events):", _name, _count);
aoqi@0 250 if (_count == 0) {
aoqi@0 251 out->print_cr("No events");
aoqi@0 252 out->cr();
aoqi@0 253 return;
aoqi@0 254 }
aoqi@0 255
aoqi@0 256 if (_count < _length) {
aoqi@0 257 for (int i = 0; i < _count; i++) {
aoqi@0 258 print(out, _records[i]);
aoqi@0 259 }
aoqi@0 260 } else {
aoqi@0 261 for (int i = _index; i < _length; i++) {
aoqi@0 262 print(out, _records[i]);
aoqi@0 263 }
aoqi@0 264 for (int i = 0; i < _index; i++) {
aoqi@0 265 print(out, _records[i]);
aoqi@0 266 }
aoqi@0 267 }
aoqi@0 268 out->cr();
aoqi@0 269 }
aoqi@0 270
aoqi@0 271 // Implement a printing routine for the StringLogMessage
aoqi@0 272 template <>
aoqi@0 273 inline void EventLogBase<StringLogMessage>::print(outputStream* out, StringLogMessage& lm) {
aoqi@0 274 out->print_raw(lm);
aoqi@0 275 out->cr();
aoqi@0 276 }
aoqi@0 277
aoqi@0 278 // Place markers for the beginning and end up of a set of events.
aoqi@0 279 // These end up in the default log.
aoqi@0 280 class EventMark : public StackObj {
aoqi@0 281 StringLogMessage _buffer;
aoqi@0 282
aoqi@0 283 public:
aoqi@0 284 // log a begin event, format as printf
aoqi@0 285 EventMark(const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
aoqi@0 286 // log an end event
aoqi@0 287 ~EventMark();
aoqi@0 288 };
aoqi@0 289
aoqi@0 290 #endif // SHARE_VM_UTILITIES_EVENTS_HPP

mercurial