events.hpp 8.4 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28
#ifndef SHARE_VM_UTILITIES_EVENTS_HPP
#define SHARE_VM_UTILITIES_EVENTS_HPP

#include "memory/allocation.hpp"
29 30
#include "runtime/mutexLocker.hpp"
#include "runtime/thread.hpp"
31
#include "utilities/top.hpp"
32
#include "utilities/vmError.hpp"
33

D
duke 已提交
34 35 36 37
// Events and EventMark provide interfaces to log events taking place in the vm.
// This facility is extremly useful for post-mortem debugging. The eventlog
// often provides crucial information about events leading up to the crash.
//
N
never 已提交
38 39 40 41 42 43
// Abstractly the logs can record whatever they way but normally they
// would record at least a timestamp and the current Thread, along
// with whatever data they need in a ring buffer.  Commonly fixed
// length text messages are recorded for simplicity but other
// strategies could be used.  Several logs are provided by default but
// new instances can be created as needed.
D
duke 已提交
44

45 46 47 48
// The base event log dumping class that is registered for dumping at
// crash time.  This is a very generic interface that is mainly here
// for completeness.  Normally the templated EventLogBase would be
// subclassed to provide different log types.
Z
zgu 已提交
49
class EventLog : public CHeapObj<mtInternal> {
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  friend class Events;

 private:
  EventLog* _next;

  EventLog* next() const { return _next; }

 public:
  // Automatically registers the log so that it will be printed during
  // crashes.
  EventLog();

  virtual void print_log_on(outputStream* out) = 0;
};


// A templated subclass of EventLog that provides basic ring buffer
// functionality.  Most event loggers should subclass this, possibly
// providing a more featureful log function if the existing copy
// semantics aren't appropriate.  The name is used as the label of the
// log when it is dumped during a crash.
template <class T> class EventLogBase : public EventLog {
72
  template <class X> class EventRecord : public CHeapObj<mtInternal> {
73
   public:
N
never 已提交
74
    double  timestamp;
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    Thread* thread;
    X       data;
  };

 protected:
  Mutex           _mutex;
  const char*     _name;
  int             _length;
  int             _index;
  int             _count;
  EventRecord<T>* _records;

 public:
  EventLogBase<T>(const char* name, int length = LogEventsBufferEntries):
    _name(name),
    _length(length),
    _count(0),
    _index(0),
    _mutex(Mutex::event, name) {
    _records = new EventRecord<T>[length];
  }

N
never 已提交
97
  double fetch_timestamp() {
98
    return os::elapsedTime();
N
never 已提交
99 100
  }

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
  // move the ring buffer to next open slot and return the index of
  // the slot to use for the current message.  Should only be called
  // while mutex is held.
  int compute_log_index() {
    int index = _index;
    if (_count < _length) _count++;
    _index++;
    if (_index >= _length) _index = 0;
    return index;
  }

  bool should_log() {
    // Don't bother adding new entries when we're crashing.  This also
    // avoids mutating the ring buffer when printing the log.
    return !VMError::fatal_error_in_progress();
  }

  // Print the contents of the log
  void print_log_on(outputStream* out);

 private:
  void print_log_impl(outputStream* out);

  // Print a single element.  A templated implementation might need to
  // be declared by subclasses.
  void print(outputStream* out, T& e);

  void print(outputStream* out, EventRecord<T>& e) {
N
never 已提交
129
    out->print("Event: %.3f ", e.timestamp);
130
    if (e.thread != NULL) {
131
      out->print("Thread " INTPTR_FORMAT " ", p2i(e.thread));
132 133 134 135 136 137
    }
    print(out, e.data);
  }
};

// A simple wrapper class for fixed size text messages.
138
class StringLogMessage : public FormatBuffer<256> {
139 140 141
 public:
  // Wrap this buffer in a stringStream.
  stringStream stream() {
142
    return stringStream(_buf, size());
143 144 145 146 147 148 149 150
  }
};

// A simple ring buffer of fixed size text messages.
class StringEventLog : public EventLogBase<StringLogMessage> {
 public:
  StringEventLog(const char* name, int count = LogEventsBufferEntries) : EventLogBase<StringLogMessage>(name, count) {}

151
  void logv(Thread* thread, const char* format, va_list ap) ATTRIBUTE_PRINTF(3, 0) {
152 153
    if (!should_log()) return;

N
never 已提交
154
    double timestamp = fetch_timestamp();
155 156 157 158 159 160 161
    MutexLockerEx ml(&_mutex, Mutex::_no_safepoint_check_flag);
    int index = compute_log_index();
    _records[index].thread = thread;
    _records[index].timestamp = timestamp;
    _records[index].data.printv(format, ap);
  }

162
  void log(Thread* thread, const char* format, ...) ATTRIBUTE_PRINTF(3, 4) {
163 164 165 166 167 168 169 170 171 172
    va_list ap;
    va_start(ap, format);
    logv(thread, format, ap);
    va_end(ap);
  }

};



D
duke 已提交
173
class Events : AllStatic {
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
  friend class EventLog;

 private:
  static EventLog* _logs;

  // A log for generic messages that aren't well categorized.
  static StringEventLog* _messages;

  // A log for internal exception related messages, like internal
  // throws and implicit exceptions.
  static StringEventLog* _exceptions;

  // Deoptization related messages
  static StringEventLog* _deopt_messages;

D
duke 已提交
189
 public:
190 191
  static void print_all(outputStream* out);

N
never 已提交
192 193
  // Dump all events to the tty
  static void print();
194 195

  // Logs a generic message with timestamp and format as printf.
196
  static void log(Thread* thread, const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
D
duke 已提交
197

198
  // Log exception related message
199
  static void log_exception(Thread* thread, const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
D
duke 已提交
200

201
  static void log_deopt_message(Thread* thread, const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
202 203 204

  // Register default loggers
  static void init();
D
duke 已提交
205 206
};

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
inline void Events::log(Thread* thread, const char* format, ...) {
  if (LogEvents) {
    va_list ap;
    va_start(ap, format);
    _messages->logv(thread, format, ap);
    va_end(ap);
  }
}

inline void Events::log_exception(Thread* thread, const char* format, ...) {
  if (LogEvents) {
    va_list ap;
    va_start(ap, format);
    _exceptions->logv(thread, format, ap);
    va_end(ap);
  }
}

inline void Events::log_deopt_message(Thread* thread, const char* format, ...) {
  if (LogEvents) {
    va_list ap;
    va_start(ap, format);
    _deopt_messages->logv(thread, format, ap);
    va_end(ap);
  }
}


template <class T>
inline void EventLogBase<T>::print_log_on(outputStream* out) {
  if (ThreadLocalStorage::get_thread_slow() == NULL) {
    // Not a regular Java thread so don't bother locking
    print_log_impl(out);
  } else {
    MutexLockerEx ml(&_mutex, Mutex::_no_safepoint_check_flag);
    print_log_impl(out);
  }
}

// Dump the ring buffer entries that current have entries.
template <class T>
inline void EventLogBase<T>::print_log_impl(outputStream* out) {
  out->print_cr("%s (%d events):", _name, _count);
  if (_count == 0) {
    out->print_cr("No events");
N
never 已提交
252
    out->cr();
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
    return;
  }

  if (_count < _length) {
    for (int i = 0; i < _count; i++) {
      print(out, _records[i]);
    }
  } else {
    for (int i = _index; i < _length; i++) {
      print(out, _records[i]);
    }
    for (int i = 0; i < _index; i++) {
      print(out, _records[i]);
    }
  }
  out->cr();
}

// Implement a printing routine for the StringLogMessage
template <>
inline void EventLogBase<StringLogMessage>::print(outputStream* out, StringLogMessage& lm) {
  out->print_raw(lm);
  out->cr();
}

// Place markers for the beginning and end up of a set of events.
// These end up in the default log.
D
duke 已提交
280
class EventMark : public StackObj {
281 282
  StringLogMessage _buffer;

D
duke 已提交
283 284
 public:
  // log a begin event, format as printf
285
  EventMark(const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
D
duke 已提交
286
  // log an end event
287
  ~EventMark();
D
duke 已提交
288 289
};

290
#endif // SHARE_VM_UTILITIES_EVENTS_HPP