1 /* 2 * Copyright (c) 1997, 2017, 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 */ 24 25 #ifndef SHARE_VM_UTILITIES_EVENTS_HPP 26 #define SHARE_VM_UTILITIES_EVENTS_HPP 27 28 #include "memory/allocation.hpp" 29 #include "runtime/mutexLocker.hpp" 30 #include "runtime/thread.hpp" 31 #include "utilities/top.hpp" 32 #include "utilities/vmError.hpp" 33 34 // Events and EventMark provide interfaces to log events taking place in the vm. 35 // This facility is extremly useful for post-mortem debugging. The eventlog 36 // often provides crucial information about events leading up to the crash. 37 // 38 // Abstractly the logs can record whatever they way but normally they 39 // would record at least a timestamp and the current Thread, along 40 // with whatever data they need in a ring buffer. Commonly fixed 41 // length text messages are recorded for simplicity but other 42 // strategies could be used. Several logs are provided by default but 43 // new instances can be created as needed. 44 45 // The base event log dumping class that is registered for dumping at 46 // crash time. This is a very generic interface that is mainly here 47 // for completeness. Normally the templated EventLogBase would be 48 // subclassed to provide different log types. 49 class EventLog : public CHeapObj<mtInternal> { 50 friend class Events; 51 52 private: 53 EventLog* _next; 54 55 EventLog* next() const { return _next; } 56 57 public: 58 // Automatically registers the log so that it will be printed during 59 // crashes. 60 EventLog(); 61 62 virtual void print_log_on(outputStream* out) = 0; 63 }; 64 65 66 // A templated subclass of EventLog that provides basic ring buffer 67 // functionality. Most event loggers should subclass this, possibly 68 // providing a more featureful log function if the existing copy 69 // semantics aren't appropriate. The name is used as the label of the 70 // log when it is dumped during a crash. 71 template <class T> class EventLogBase : public EventLog { 72 template <class X> class EventRecord : public CHeapObj<mtInternal> { 73 public: 74 double timestamp; 75 Thread* thread; 76 X data; 77 }; 78 79 protected: 80 Mutex _mutex; 81 const char* _name; 82 int _length; 83 int _index; 84 int _count; 85 EventRecord<T>* _records; 86 87 public: 88 EventLogBase<T>(const char* name, int length = LogEventsBufferEntries): 89 _name(name), 90 _length(length), 91 _count(0), 92 _index(0), 93 _mutex(Mutex::event, name) { 94 _records = new EventRecord<T>[length]; 95 } 96 97 double fetch_timestamp() { 98 return os::elapsedTime(); 99 } 100 101 // move the ring buffer to next open slot and return the index of 102 // the slot to use for the current message. Should only be called 103 // while mutex is held. 104 int compute_log_index() { 105 int index = _index; 106 if (_count < _length) _count++; 107 _index++; 108 if (_index >= _length) _index = 0; 109 return index; 110 } 111 112 bool should_log() { 113 // Don't bother adding new entries when we're crashing. This also 114 // avoids mutating the ring buffer when printing the log. 115 return !VMError::fatal_error_in_progress(); 116 } 117 118 // Print the contents of the log 119 void print_log_on(outputStream* out); 120 121 private: 122 void print_log_impl(outputStream* out); 123 124 // Print a single element. A templated implementation might need to 125 // be declared by subclasses. 126 void print(outputStream* out, T& e); 127 128 void print(outputStream* out, EventRecord<T>& e) { 129 out->print("Event: %.3f ", e.timestamp); 130 if (e.thread != NULL) { 131 out->print("Thread " INTPTR_FORMAT " ", p2i(e.thread)); 132 } 133 print(out, e.data); 134 } 135 }; 136 137 // A simple wrapper class for fixed size text messages. 138 class StringLogMessage : public FormatBuffer<256> { 139 public: 140 // Wrap this buffer in a stringStream. 141 stringStream stream() { 142 return stringStream(_buf, size()); 143 } 144 }; 145 146 // A simple ring buffer of fixed size text messages. 147 class StringEventLog : public EventLogBase<StringLogMessage> { 148 public: 149 StringEventLog(const char* name, int count = LogEventsBufferEntries) : EventLogBase<StringLogMessage>(name, count) {} 150 151 void logv(Thread* thread, const char* format, va_list ap) ATTRIBUTE_PRINTF(3, 0) { 152 if (!should_log()) return; 153 154 double timestamp = fetch_timestamp(); 155 MutexLockerEx ml(&_mutex, Mutex::_no_safepoint_check_flag); 156 int index = compute_log_index(); 157 _records[index].thread = thread; 158 _records[index].timestamp = timestamp; 159 _records[index].data.printv(format, ap); 160 } 161 162 void log(Thread* thread, const char* format, ...) ATTRIBUTE_PRINTF(3, 4) { 163 va_list ap; 164 va_start(ap, format); 165 logv(thread, format, ap); 166 va_end(ap); 167 } 168 169 }; 170 171 class GCLogMessage { 172 friend class EventLogBase<GCLogMessage>; 173 const static size_t GCLOG_MAX_BUFSIZE = 128; 174 char buf[GCLOG_MAX_BUFSIZE]; 175 size_t size; 176 177 public: 178 void write(const char* s, size_t len) { 179 size = MIN2(len, GCLOG_MAX_BUFSIZE - 1); 180 memcpy(buf, s, size); 181 } 182 }; 183 184 class GCLogEvent: public EventLogBase<GCLogMessage> { 185 private: 186 static uint _buffer_overflow_count; 187 188 public: 189 GCLogEvent(const char* name, int count = LogEventsBufferEntries) 190 : EventLogBase<GCLogMessage> (name, count) {} 191 192 void log(Thread* thread, const char* s, size_t len) { 193 if (!should_log()) return; 194 if (full()) { 195 _buffer_overflow_count++; 196 } 197 198 double timestamp = fetch_timestamp(); 199 int index = compute_log_index(); 200 _records[index].thread = thread; 201 _records[index].timestamp = timestamp; 202 _records[index].data.write(s, len); 203 } 204 205 bool full() const { 206 return _count >= _length; 207 } 208 209 bool empty() const { 210 return 0 == _count; 211 } 212 213 void reset() { 214 _index = _count = 0; 215 } 216 217 static uint buffer_overflow_count() { return _buffer_overflow_count; } 218 }; 219 220 class Events : AllStatic { 221 friend class EventLog; 222 223 private: 224 static EventLog* _logs; 225 226 // A log for generic messages that aren't well categorized. 227 static StringEventLog* _messages; 228 229 // A log for internal exception related messages, like internal 230 // throws and implicit exceptions. 231 static StringEventLog* _exceptions; 232 233 // Deoptization related messages 234 static StringEventLog* _deopt_messages; 235 236 // Redefinition related messages 237 static StringEventLog* _redefinitions; 238 239 public: 240 static void print_all(outputStream* out); 241 242 // Dump all events to the tty 243 static void print(); 244 245 // Logs a generic message with timestamp and format as printf. 246 static void log(Thread* thread, const char* format, ...) ATTRIBUTE_PRINTF(2, 3); 247 248 // Log exception related message 249 static void log_exception(Thread* thread, const char* format, ...) ATTRIBUTE_PRINTF(2, 3); 250 251 static void log_redefinition(Thread* thread, const char* format, ...) ATTRIBUTE_PRINTF(2, 3); 252 253 static void log_deopt_message(Thread* thread, const char* format, ...) ATTRIBUTE_PRINTF(2, 3); 254 255 // Register default loggers 256 static void init(); 257 }; 258 259 inline void Events::log(Thread* thread, const char* format, ...) { 260 if (LogEvents) { 261 va_list ap; 262 va_start(ap, format); 263 _messages->logv(thread, format, ap); 264 va_end(ap); 265 } 266 } 267 268 inline void Events::log_exception(Thread* thread, const char* format, ...) { 269 if (LogEvents) { 270 va_list ap; 271 va_start(ap, format); 272 _exceptions->logv(thread, format, ap); 273 va_end(ap); 274 } 275 } 276 277 inline void Events::log_redefinition(Thread* thread, const char* format, ...) { 278 if (LogEvents) { 279 va_list ap; 280 va_start(ap, format); 281 _redefinitions->logv(thread, format, ap); 282 va_end(ap); 283 } 284 } 285 286 inline void Events::log_deopt_message(Thread* thread, const char* format, ...) { 287 if (LogEvents) { 288 va_list ap; 289 va_start(ap, format); 290 _deopt_messages->logv(thread, format, ap); 291 va_end(ap); 292 } 293 } 294 295 296 template <class T> 297 inline void EventLogBase<T>::print_log_on(outputStream* out) { 298 if (ThreadLocalStorage::get_thread_slow() == NULL) { 299 // Not a regular Java thread so don't bother locking 300 print_log_impl(out); 301 } else { 302 MutexLockerEx ml(&_mutex, Mutex::_no_safepoint_check_flag); 303 print_log_impl(out); 304 } 305 } 306 307 // Dump the ring buffer entries that current have entries. 308 template <class T> 309 inline void EventLogBase<T>::print_log_impl(outputStream* out) { 310 out->print_cr("%s (%d events):", _name, _count); 311 if (_count == 0) { 312 out->print_cr("No events"); 313 out->cr(); 314 return; 315 } 316 317 if (_count < _length) { 318 for (int i = 0; i < _count; i++) { 319 print(out, _records[i]); 320 } 321 } else { 322 for (int i = _index; i < _length; i++) { 323 print(out, _records[i]); 324 } 325 for (int i = 0; i < _index; i++) { 326 print(out, _records[i]); 327 } 328 } 329 out->cr(); 330 } 331 332 // Implement a printing routine for the StringLogMessage 333 template <> 334 inline void EventLogBase<StringLogMessage>::print(outputStream* out, StringLogMessage& lm) { 335 out->print_raw(lm); 336 out->cr(); 337 } 338 339 template<> 340 inline void EventLogBase<GCLogMessage>::print(outputStream* out, GCLogMessage& lm) { 341 gcLogFileStream* gclog = static_cast<gcLogFileStream* >(out); 342 gclog->write_blocking(lm.buf, lm.size); 343 } 344 345 template <> 346 inline void EventLogBase<GCLogMessage>::print_log_impl(outputStream* out) { 347 if (_count == 0) { 348 return; 349 } 350 351 if (_count < _length) { 352 for (int i = 0; i < _count; i++) { 353 print(out, _records[i].data); 354 } 355 } else { 356 for (int i = _index; i < _length; i++) { 357 print(out, _records[i].data); 358 } 359 for (int i = 0; i < _index; i++) { 360 print(out, _records[i].data); 361 } 362 } 363 } 364 365 366 // Place markers for the beginning and end up of a set of events. 367 // These end up in the default log. 368 class EventMark : public StackObj { 369 StringLogMessage _buffer; 370 371 public: 372 // log a begin event, format as printf 373 EventMark(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); 374 // log an end event 375 ~EventMark(); 376 }; 377 378 #endif // SHARE_VM_UTILITIES_EVENTS_HPP