|
31 | 31 | #include "runtime/atomic.hpp"
|
32 | 32 | #include "runtime/os.inline.hpp"
|
33 | 33 |
|
| 34 | +DEBUG_ONLY(bool AsyncLogWriter::ignore_recursive_logging = false;) |
| 35 | + |
34 | 36 | class AsyncLogWriter::AsyncLogLocker : public StackObj {
|
35 |
| - public: |
| 37 | + static Thread* _holder; |
| 38 | +public: |
| 39 | + static Thread* current_holder() { return _holder; } |
36 | 40 | AsyncLogLocker() {
|
37 | 41 | assert(_instance != nullptr, "AsyncLogWriter::_lock is unavailable");
|
38 | 42 | _instance->_lock.lock();
|
| 43 | + _holder = Thread::current_or_null(); |
39 | 44 | }
|
40 | 45 |
|
41 | 46 | ~AsyncLogLocker() {
|
| 47 | + assert(_holder == Thread::current_or_null(), "must be"); |
| 48 | + _holder = nullptr; |
42 | 49 | _instance->_lock.unlock();
|
43 | 50 | }
|
| 51 | + |
| 52 | + void wait() { |
| 53 | + Thread* saved_holder = _holder; |
| 54 | + _holder = nullptr; |
| 55 | + _instance->_lock.wait(0/* no timeout */); |
| 56 | + _holder = saved_holder; |
| 57 | + } |
44 | 58 | };
|
45 | 59 |
|
| 60 | +Thread* AsyncLogWriter::AsyncLogLocker::_holder = nullptr; |
| 61 | + |
46 | 62 | // LogDecorator::None applies to 'constant initialization' because of its constexpr constructor.
|
47 | 63 | const LogDecorations& AsyncLogWriter::None = LogDecorations(LogLevel::Warning, LogTagSetMapping<LogTag::__NO_TAG>::tagset(),
|
48 | 64 | LogDecorators::None);
|
@@ -84,19 +100,67 @@ void AsyncLogWriter::enqueue_locked(LogFileStreamOutput* output, const LogDecora
|
84 | 100 | _lock.notify();
|
85 | 101 | }
|
86 | 102 |
|
87 |
| -void AsyncLogWriter::enqueue(LogFileStreamOutput& output, const LogDecorations& decorations, const char* msg) { |
| 103 | +// This function checks for cases where continuing with asynchronous logging may lead to stability issues, such as a deadlock. |
| 104 | +// If this returns false then we give up on logging asynchronously and do so synchronously instead. |
| 105 | +bool AsyncLogWriter::is_enqueue_allowed() { |
| 106 | + AsyncLogWriter* alw = AsyncLogWriter::instance(); |
| 107 | + Thread* holding_thread = AsyncLogWriter::AsyncLogLocker::current_holder(); |
| 108 | + Thread* this_thread = Thread::current_or_null(); |
| 109 | + if (this_thread == nullptr) { |
| 110 | + // The current thread is unattached. |
| 111 | + return false; |
| 112 | + } |
| 113 | + |
| 114 | + if (holding_thread == this_thread) { |
| 115 | + // A thread, while enqueuing a message, has attempted to log something. |
| 116 | + // Do not log while holding the Async log lock. |
| 117 | + // Try to catch possible occurrences in debug builds. |
| 118 | +#ifdef ASSERT |
| 119 | + if (!AsyncLogWriter::ignore_recursive_logging) { |
| 120 | + ShouldNotReachHere(); |
| 121 | + } |
| 122 | +#endif // ASSERT |
| 123 | + return false; |
| 124 | + } |
| 125 | + |
| 126 | + if (alw == nullptr) { |
| 127 | + // There is no AsyncLogWriter instance yet. |
| 128 | + return false; |
| 129 | + } |
| 130 | + |
| 131 | + if (this_thread == alw) { |
| 132 | + // The async log producer is attempting to log, leading to recursive logging. |
| 133 | + return false; |
| 134 | + } |
| 135 | + |
| 136 | + return true; |
| 137 | +} |
| 138 | + |
| 139 | +bool AsyncLogWriter::enqueue(LogFileStreamOutput& output, const LogDecorations& decorations, const char* msg) { |
| 140 | + if (!is_enqueue_allowed()) { |
| 141 | + return false; |
| 142 | + } |
| 143 | + |
88 | 144 | AsyncLogLocker locker;
|
89 |
| - enqueue_locked(&output, decorations, msg); |
| 145 | + DEBUG_ONLY(log_debug(deathtest)("Induce a recursive log for testing (for crashing)");) |
| 146 | + DEBUG_ONLY(log_debug(deathtest2)("Induce a recursive log for testing");) |
| 147 | + AsyncLogWriter::instance()->enqueue_locked(&output, decorations, msg); |
| 148 | + return true; |
90 | 149 | }
|
91 | 150 |
|
92 | 151 | // LogMessageBuffer consists of a multiple-part/multiple-line message.
|
93 | 152 | // The lock here guarantees its integrity.
|
94 |
| -void AsyncLogWriter::enqueue(LogFileStreamOutput& output, LogMessageBuffer::Iterator msg_iterator) { |
95 |
| - AsyncLogLocker locker; |
| 153 | +bool AsyncLogWriter::enqueue(LogFileStreamOutput& output, LogMessageBuffer::Iterator msg_iterator) { |
| 154 | + if (!is_enqueue_allowed()) { |
| 155 | + return false; |
| 156 | + } |
96 | 157 |
|
| 158 | + // If we get here we know the AsyncLogWriter is initialized. |
| 159 | + AsyncLogLocker locker; |
97 | 160 | for (; !msg_iterator.is_at_end(); msg_iterator++) {
|
98 |
| - enqueue_locked(&output, msg_iterator.decorations(), msg_iterator.message()); |
| 161 | + AsyncLogWriter::instance()->enqueue_locked(&output, msg_iterator.decorations(), msg_iterator.message()); |
99 | 162 | }
|
| 163 | + return true; |
100 | 164 | }
|
101 | 165 |
|
102 | 166 | AsyncLogWriter::AsyncLogWriter()
|
@@ -155,7 +219,7 @@ void AsyncLogWriter::run() {
|
155 | 219 | AsyncLogLocker locker;
|
156 | 220 |
|
157 | 221 | while (!_data_available) {
|
158 |
| - _lock.wait(0/* no timeout */); |
| 222 | + locker.wait(); |
159 | 223 | }
|
160 | 224 | // Only doing a swap and statistics under the lock to
|
161 | 225 | // guarantee that I/O jobs don't block logsites.
|
|
0 commit comments