blob: 65fedb026c8315f1596991140f059eb45daf53d2 [file] [log] [blame]
Tom Cherry1a796bc2020-05-13 09:28:37 -07001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "SerializedLogBuffer.h"
18
Tom Cherry1fdbdbe2020-06-22 08:28:45 -070019#include <sys/prctl.h>
20
Tom Cherry1a796bc2020-05-13 09:28:37 -070021#include <limits>
Tom Cherry1a796bc2020-05-13 09:28:37 -070022
23#include <android-base/logging.h>
24#include <android-base/scopeguard.h>
25
Tom Cherry39dc2212020-08-05 12:14:45 -070026#include "LogSize.h"
Tom Cherry1a796bc2020-05-13 09:28:37 -070027#include "LogStatistics.h"
28#include "SerializedFlushToState.h"
29
30SerializedLogBuffer::SerializedLogBuffer(LogReaderList* reader_list, LogTags* tags,
31 LogStatistics* stats)
32 : reader_list_(reader_list), tags_(tags), stats_(stats) {
33 Init();
34}
35
Tom Cherry1a796bc2020-05-13 09:28:37 -070036void SerializedLogBuffer::Init() {
37 log_id_for_each(i) {
Tom Cherry39dc2212020-08-05 12:14:45 -070038 if (!SetSize(i, GetBufferSizeFromProperties(i))) {
39 SetSize(i, kLogBufferMinSize);
Tom Cherry1a796bc2020-05-13 09:28:37 -070040 }
41 }
42
43 // Release any sleeping reader threads to dump their current content.
44 auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()};
45 for (const auto& reader_thread : reader_list_->reader_threads()) {
46 reader_thread->triggerReader_Locked();
47 }
48}
49
50bool SerializedLogBuffer::ShouldLog(log_id_t log_id, const char* msg, uint16_t len) {
51 if (log_id == LOG_ID_SECURITY) {
52 return true;
53 }
54
55 int prio = ANDROID_LOG_INFO;
56 const char* tag = nullptr;
57 size_t tag_len = 0;
58 if (IsBinary(log_id)) {
59 int32_t tag_int = MsgToTag(msg, len);
60 tag = tags_->tagToName(tag_int);
61 if (tag) {
62 tag_len = strlen(tag);
63 }
64 } else {
65 prio = *msg;
66 tag = msg + 1;
67 tag_len = strnlen(tag, len - 1);
68 }
69 return __android_log_is_loggable_len(prio, tag, tag_len, ANDROID_LOG_VERBOSE);
70}
71
72int SerializedLogBuffer::Log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid,
73 const char* msg, uint16_t len) {
74 if (log_id >= LOG_ID_MAX || len == 0) {
75 return -EINVAL;
76 }
77
78 if (!ShouldLog(log_id, msg, len)) {
79 stats_->AddTotal(log_id, len);
80 return -EACCES;
81 }
82
83 auto sequence = sequence_.fetch_add(1, std::memory_order_relaxed);
84
85 auto lock = std::lock_guard{lock_};
86
87 if (logs_[log_id].empty()) {
88 logs_[log_id].push_back(SerializedLogChunk(max_size_[log_id] / 4));
89 }
90
91 auto total_len = sizeof(SerializedLogEntry) + len;
92 if (!logs_[log_id].back().CanLog(total_len)) {
93 logs_[log_id].back().FinishWriting();
94 logs_[log_id].push_back(SerializedLogChunk(max_size_[log_id] / 4));
95 }
96
97 auto entry = logs_[log_id].back().Log(sequence, realtime, uid, pid, tid, msg, len);
98 stats_->Add(entry->ToLogStatisticsElement(log_id));
99
100 MaybePrune(log_id);
101
102 reader_list_->NotifyNewLog(1 << log_id);
103 return len;
104}
105
106void SerializedLogBuffer::MaybePrune(log_id_t log_id) {
Tom Cherryf74503d2020-06-19 12:21:21 -0700107 size_t total_size = GetSizeUsed(log_id);
108 size_t after_size = total_size;
Tom Cherry1a796bc2020-05-13 09:28:37 -0700109 if (total_size > max_size_[log_id]) {
110 Prune(log_id, total_size - max_size_[log_id], 0);
Tom Cherryf74503d2020-06-19 12:21:21 -0700111 after_size = GetSizeUsed(log_id);
Tom Cherry1a796bc2020-05-13 09:28:37 -0700112 LOG(INFO) << "Pruned Logs from log_id: " << log_id << ", previous size: " << total_size
Tom Cherryf74503d2020-06-19 12:21:21 -0700113 << " after size: " << after_size;
Tom Cherry1a796bc2020-05-13 09:28:37 -0700114 }
Tom Cherryf74503d2020-06-19 12:21:21 -0700115
116 stats_->set_overhead(log_id, after_size);
Tom Cherry1a796bc2020-05-13 09:28:37 -0700117}
118
Tom Cherry4f8125a2020-07-09 12:12:48 -0700119void SerializedLogBuffer::RemoveChunkFromStats(log_id_t log_id, SerializedLogChunk& chunk) {
120 chunk.IncReaderRefCount();
121 int read_offset = 0;
122 while (read_offset < chunk.write_offset()) {
123 auto* entry = chunk.log_entry(read_offset);
124 stats_->Subtract(entry->ToLogStatisticsElement(log_id));
125 read_offset += entry->total_len();
Tom Cherry1fdbdbe2020-06-22 08:28:45 -0700126 }
Tom Cherry59caa7a2020-07-16 20:46:14 -0700127 chunk.DecReaderRefCount();
Tom Cherry1a796bc2020-05-13 09:28:37 -0700128}
129
130void SerializedLogBuffer::NotifyReadersOfPrune(
131 log_id_t log_id, const std::list<SerializedLogChunk>::iterator& chunk) {
132 for (const auto& reader_thread : reader_list_->reader_threads()) {
133 auto& state = reinterpret_cast<SerializedFlushToState&>(reader_thread->flush_to_state());
134 state.Prune(log_id, chunk);
135 }
136}
137
138bool SerializedLogBuffer::Prune(log_id_t log_id, size_t bytes_to_free, uid_t uid) {
139 // Don't prune logs that are newer than the point at which any reader threads are reading from.
140 LogReaderThread* oldest = nullptr;
141 auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()};
142 for (const auto& reader_thread : reader_list_->reader_threads()) {
143 if (!reader_thread->IsWatching(log_id)) {
144 continue;
145 }
146 if (!oldest || oldest->start() > reader_thread->start() ||
147 (oldest->start() == reader_thread->start() &&
148 reader_thread->deadline().time_since_epoch().count() != 0)) {
149 oldest = reader_thread.get();
150 }
151 }
152
153 auto& log_buffer = logs_[log_id];
Tom Cherry1a796bc2020-05-13 09:28:37 -0700154 auto it = log_buffer.begin();
155 while (it != log_buffer.end()) {
156 if (oldest != nullptr && it->highest_sequence_number() >= oldest->start()) {
157 break;
158 }
159
Tom Cherry4f8125a2020-07-09 12:12:48 -0700160 // Increment ahead of time since we're going to erase this iterator from the list.
Tom Cherry1a796bc2020-05-13 09:28:37 -0700161 auto it_to_prune = it++;
162
163 // The sequence number check ensures that all readers have read all logs in this chunk, but
164 // they may still hold a reference to the chunk to track their last read log_position.
165 // Notify them to delete the reference.
166 NotifyReadersOfPrune(log_id, it_to_prune);
167
168 if (uid != 0) {
169 // Reorder the log buffer to remove logs from the given UID. If there are no logs left
170 // in the buffer after the removal, delete it.
171 if (it_to_prune->ClearUidLogs(uid, log_id, stats_)) {
172 log_buffer.erase(it_to_prune);
173 }
174 } else {
175 size_t buffer_size = it_to_prune->PruneSize();
Tom Cherry4f8125a2020-07-09 12:12:48 -0700176 RemoveChunkFromStats(log_id, *it_to_prune);
177 log_buffer.erase(it_to_prune);
Tom Cherry1a796bc2020-05-13 09:28:37 -0700178 if (buffer_size >= bytes_to_free) {
179 return true;
180 }
181 bytes_to_free -= buffer_size;
182 }
183 }
184
185 // If we've deleted all buffers without bytes_to_free hitting 0, then we're called by Clear()
186 // and should return true.
187 if (it == log_buffer.end()) {
188 return true;
189 }
190
191 // Otherwise we are stuck due to a reader, so mitigate it.
Tom Cherry9b4246d2020-06-17 11:40:55 -0700192 CHECK(oldest != nullptr);
Tom Cherry1a796bc2020-05-13 09:28:37 -0700193 KickReader(oldest, log_id, bytes_to_free);
194 return false;
195}
196
197// If the selected reader is blocking our pruning progress, decide on
198// what kind of mitigation is necessary to unblock the situation.
199void SerializedLogBuffer::KickReader(LogReaderThread* reader, log_id_t id, size_t bytes_to_free) {
200 if (bytes_to_free >= max_size_[id]) { // +100%
201 // A misbehaving or slow reader is dropped if we hit too much memory pressure.
202 LOG(WARNING) << "Kicking blocked reader, " << reader->name()
203 << ", from LogBuffer::kickMe()";
204 reader->release_Locked();
205 } else if (reader->deadline().time_since_epoch().count() != 0) {
206 // Allow a blocked WRAP deadline reader to trigger and start reporting the log data.
207 reader->triggerReader_Locked();
208 } else {
209 // Tell slow reader to skip entries to catch up.
210 unsigned long prune_rows = bytes_to_free / 300;
211 LOG(WARNING) << "Skipping " << prune_rows << " entries from slow reader, " << reader->name()
212 << ", from LogBuffer::kickMe()";
213 reader->triggerSkip_Locked(id, prune_rows);
214 }
215}
216
217std::unique_ptr<FlushToState> SerializedLogBuffer::CreateFlushToState(uint64_t start,
218 LogMask log_mask) {
219 return std::make_unique<SerializedFlushToState>(start, log_mask);
220}
221
222bool SerializedLogBuffer::FlushTo(
223 LogWriter* writer, FlushToState& abstract_state,
224 const std::function<FilterResult(log_id_t log_id, pid_t pid, uint64_t sequence,
225 log_time realtime)>& filter) {
226 auto lock = std::unique_lock{lock_};
227
228 auto& state = reinterpret_cast<SerializedFlushToState&>(abstract_state);
229 state.InitializeLogs(logs_);
Tom Cherry1a796bc2020-05-13 09:28:37 -0700230
231 while (state.HasUnreadLogs()) {
232 MinHeapElement top = state.PopNextUnreadLog();
233 auto* entry = top.entry;
234 auto log_id = top.log_id;
235
236 if (entry->sequence() < state.start()) {
237 continue;
238 }
239 state.set_start(entry->sequence());
240
241 if (!writer->privileged() && entry->uid() != writer->uid()) {
242 continue;
243 }
244
245 if (filter) {
246 auto ret = filter(log_id, entry->pid(), entry->sequence(), entry->realtime());
247 if (ret == FilterResult::kSkip) {
248 continue;
249 }
250 if (ret == FilterResult::kStop) {
251 break;
252 }
253 }
254
255 lock.unlock();
256 // We never prune logs equal to or newer than any LogReaderThreads' `start` value, so the
257 // `entry` pointer is safe here without the lock
258 if (!entry->Flush(writer, log_id)) {
259 return false;
260 }
261 lock.lock();
Tom Cherry1a796bc2020-05-13 09:28:37 -0700262 }
263
264 state.set_start(state.start() + 1);
265 return true;
266}
267
268bool SerializedLogBuffer::Clear(log_id_t id, uid_t uid) {
269 // Try three times to clear, then disconnect the readers and try one final time.
270 for (int retry = 0; retry < 3; ++retry) {
271 {
272 auto lock = std::lock_guard{lock_};
273 bool prune_success = Prune(id, ULONG_MAX, uid);
274 if (prune_success) {
275 return true;
276 }
277 }
278 sleep(1);
279 }
280 // Check if it is still busy after the sleep, we try to prune one entry, not another clear run,
281 // so we are looking for the quick side effect of the return value to tell us if we have a
282 // _blocked_ reader.
283 bool busy = false;
284 {
285 auto lock = std::lock_guard{lock_};
286 busy = !Prune(id, 1, uid);
287 }
288 // It is still busy, disconnect all readers.
289 if (busy) {
290 auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()};
291 for (const auto& reader_thread : reader_list_->reader_threads()) {
292 if (reader_thread->IsWatching(id)) {
293 LOG(WARNING) << "Kicking blocked reader, " << reader_thread->name()
294 << ", from LogBuffer::clear()";
295 reader_thread->release_Locked();
296 }
297 }
298 }
299 auto lock = std::lock_guard{lock_};
300 return Prune(id, ULONG_MAX, uid);
301}
302
Tom Cherry39dc2212020-08-05 12:14:45 -0700303size_t SerializedLogBuffer::GetSizeUsed(log_id_t id) {
Tom Cherryf74503d2020-06-19 12:21:21 -0700304 size_t total_size = 0;
305 for (const auto& chunk : logs_[id]) {
306 total_size += chunk.PruneSize();
307 }
308 return total_size;
309}
310
Tom Cherry39dc2212020-08-05 12:14:45 -0700311size_t SerializedLogBuffer::GetSize(log_id_t id) {
Tom Cherry1a796bc2020-05-13 09:28:37 -0700312 auto lock = std::lock_guard{lock_};
Tom Cherryf74503d2020-06-19 12:21:21 -0700313 return max_size_[id];
Tom Cherry1a796bc2020-05-13 09:28:37 -0700314}
315
316// New SerializedLogChunk objects will be allocated according to the new size, but older one are
317// unchanged. MaybePrune() is called on the log buffer to reduce it to an appropriate size if the
318// new size is lower.
Tom Cherry39dc2212020-08-05 12:14:45 -0700319bool SerializedLogBuffer::SetSize(log_id_t id, size_t size) {
Tom Cherry1a796bc2020-05-13 09:28:37 -0700320 // Reasonable limits ...
Tom Cherry39dc2212020-08-05 12:14:45 -0700321 if (!IsValidBufferSize(size)) {
322 return false;
Tom Cherry1a796bc2020-05-13 09:28:37 -0700323 }
324
325 auto lock = std::lock_guard{lock_};
Tom Cherryf74503d2020-06-19 12:21:21 -0700326 max_size_[id] = size;
Tom Cherry1a796bc2020-05-13 09:28:37 -0700327
328 MaybePrune(id);
329
Tom Cherry39dc2212020-08-05 12:14:45 -0700330 return true;
Tom Cherry1a796bc2020-05-13 09:28:37 -0700331}