blob: 3a83f3f5f6362786c0fde8f0bf1ba98f5cf45ac7 [file] [log] [blame]
Mark Salyzyn0175b072014-02-26 09:50:16 -08001/*
2 * Copyright (C) 2014 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
Tom Cherry6ec71e92020-05-04 12:53:36 -070017#include "LogReaderThread.h"
18
Mark Salyzynb75cce02015-11-30 11:35:56 -080019#include <errno.h>
Mark Salyzynae2abf12017-03-31 10:48:39 -070020#include <string.h>
Mark Salyzyn8daa9af2014-04-28 14:07:23 -070021#include <sys/prctl.h>
22
Tom Cherrycef47bb2020-05-04 17:10:16 -070023#include <thread>
24
Mark Salyzyn0175b072014-02-26 09:50:16 -080025#include "LogBuffer.h"
Tom Cherry283c9a12020-05-14 19:25:05 -070026#include "LogReaderList.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080027
Tom Cherry320f5962020-05-04 17:25:34 -070028using namespace std::placeholders;
29
Tom Cherry283c9a12020-05-14 19:25:05 -070030LogReaderThread::LogReaderThread(LogBuffer* log_buffer, LogReaderList* reader_list,
31 std::unique_ptr<LogWriter> writer, bool non_block,
32 unsigned long tail, unsigned int log_mask, pid_t pid,
33 log_time start_time, uint64_t start,
34 std::chrono::steady_clock::time_point deadline)
35 : log_buffer_(log_buffer),
Tom Cherry68630a02020-05-11 16:29:29 -070036 reader_list_(reader_list),
Tom Cherry283c9a12020-05-14 19:25:05 -070037 writer_(std::move(writer)),
38 leading_dropped_(false),
Tom Cherrycef47bb2020-05-04 17:10:16 -070039 log_mask_(log_mask),
40 pid_(pid),
41 tail_(tail),
42 count_(0),
43 index_(0),
Tom Cherrycef47bb2020-05-04 17:10:16 -070044 start_time_(start_time),
45 start_(start),
Tom Cherry68630a02020-05-11 16:29:29 -070046 deadline_(deadline),
Tom Cherry283c9a12020-05-14 19:25:05 -070047 non_block_(non_block) {
Tom Cherrycef47bb2020-05-04 17:10:16 -070048 memset(last_tid_, 0, sizeof(last_tid_));
Mark Salyzyn77187782015-05-12 15:21:31 -070049 cleanSkip_Locked();
Tom Cherrycef47bb2020-05-04 17:10:16 -070050 auto thread = std::thread{&LogReaderThread::ThreadFunction, this};
51 thread.detach();
Mark Salyzyn0175b072014-02-26 09:50:16 -080052}
53
Tom Cherrycef47bb2020-05-04 17:10:16 -070054void LogReaderThread::ThreadFunction() {
Mark Salyzyn8daa9af2014-04-28 14:07:23 -070055 prctl(PR_SET_NAME, "logd.reader.per");
56
Tom Cherrycef47bb2020-05-04 17:10:16 -070057 leading_dropped_ = true;
Mark Salyzyn047cc072015-06-04 13:35:30 -070058
Tom Cherry283c9a12020-05-14 19:25:05 -070059 auto lock = std::unique_lock{reader_list_->reader_threads_lock()};
Mark Salyzyn0175b072014-02-26 09:50:16 -080060
Tom Cherrycef47bb2020-05-04 17:10:16 -070061 uint64_t start = start_;
Mark Salyzyn0175b072014-02-26 09:50:16 -080062
Tom Cherrycef47bb2020-05-04 17:10:16 -070063 while (!release_) {
Tom Cherry68630a02020-05-11 16:29:29 -070064 if (deadline_.time_since_epoch().count() != 0) {
65 if (thread_triggered_condition_.wait_until(lock, deadline_) ==
66 std::cv_status::timeout) {
67 deadline_ = {};
Mark Salyzynb75cce02015-11-30 11:35:56 -080068 }
Tom Cherrycef47bb2020-05-04 17:10:16 -070069 if (release_) {
Mark Salyzynb75cce02015-11-30 11:35:56 -080070 break;
71 }
72 }
73
Tom Cherry68630a02020-05-11 16:29:29 -070074 lock.unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -080075
Tom Cherrycef47bb2020-05-04 17:10:16 -070076 if (tail_) {
Tom Cherry283c9a12020-05-14 19:25:05 -070077 log_buffer_->FlushTo(writer_.get(), start, nullptr,
Tom Cherry70fadea2020-05-27 14:43:19 -070078 [this](log_id_t log_id, pid_t pid, uint64_t sequence,
79 log_time realtime, uint16_t dropped_count) {
80 return FilterFirstPass(log_id, pid, sequence, realtime,
81 dropped_count);
82 });
Tom Cherrycef47bb2020-05-04 17:10:16 -070083 leading_dropped_ =
84 true; // TODO: Likely a bug, if leading_dropped_ was not true before calling
85 // flushTo(), then it should not be reset to true after.
Mark Salyzyn0175b072014-02-26 09:50:16 -080086 }
Tom Cherry283c9a12020-05-14 19:25:05 -070087 start = log_buffer_->FlushTo(writer_.get(), start, last_tid_,
Tom Cherry70fadea2020-05-27 14:43:19 -070088 [this](log_id_t log_id, pid_t pid, uint64_t sequence,
89 log_time realtime, uint16_t dropped_count) {
90 return FilterSecondPass(log_id, pid, sequence, realtime,
91 dropped_count);
92 });
Mark Salyzyn0175b072014-02-26 09:50:16 -080093
Tom Cherry65ab7fe2019-08-21 14:53:06 -070094 // We only ignore entries before the original start time for the first flushTo(), if we
95 // get entries after this first flush before the original start time, then the client
96 // wouldn't have seen them.
97 // Note: this is still racy and may skip out of order events that came in since the last
98 // time the client disconnected and then reconnected with the new start time. The long term
99 // solution here is that clients must request events since a specific sequence number.
Tom Cherrycef47bb2020-05-04 17:10:16 -0700100 start_time_.tv_sec = 0;
101 start_time_.tv_nsec = 0;
Tom Cherry65ab7fe2019-08-21 14:53:06 -0700102
Tom Cherry68630a02020-05-11 16:29:29 -0700103 lock.lock();
Mark Salyzyna16f7612014-08-07 08:16:52 -0700104
Tom Cherry283c9a12020-05-14 19:25:05 -0700105 if (start == LogBuffer::FLUSH_ERROR) {
Mark Salyzynde4bb9c2015-09-16 15:34:00 -0700106 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800107 }
108
Tom Cherrycef47bb2020-05-04 17:10:16 -0700109 start_ = start + 1;
Mark Salyzynde4bb9c2015-09-16 15:34:00 -0700110
Tom Cherrycef47bb2020-05-04 17:10:16 -0700111 if (non_block_ || release_) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800112 break;
113 }
114
Tom Cherrycef47bb2020-05-04 17:10:16 -0700115 cleanSkip_Locked();
TraianX Schiauda6495d2014-12-17 10:53:41 +0200116
Tom Cherry68630a02020-05-11 16:29:29 -0700117 if (deadline_.time_since_epoch().count() == 0) {
118 thread_triggered_condition_.wait(lock);
Mark Salyzynb75cce02015-11-30 11:35:56 -0800119 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800120 }
121
Tom Cherry283c9a12020-05-14 19:25:05 -0700122 writer_->Release();
Tom Cherry4f227862018-10-08 17:33:50 -0700123
Tom Cherry283c9a12020-05-14 19:25:05 -0700124 auto& log_reader_threads = reader_list_->reader_threads();
Tom Cherry68630a02020-05-11 16:29:29 -0700125 auto it = std::find_if(log_reader_threads.begin(), log_reader_threads.end(),
Tom Cherrycef47bb2020-05-04 17:10:16 -0700126 [this](const auto& other) { return other.get() == this; });
Tom Cherry4f227862018-10-08 17:33:50 -0700127
Tom Cherry68630a02020-05-11 16:29:29 -0700128 if (it != log_reader_threads.end()) {
129 log_reader_threads.erase(it);
Tom Cherry4f227862018-10-08 17:33:50 -0700130 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800131}
132
133// A first pass to count the number of elements
Tom Cherry70fadea2020-05-27 14:43:19 -0700134FilterResult LogReaderThread::FilterFirstPass(log_id_t log_id, pid_t pid, uint64_t sequence,
135 log_time realtime, uint16_t dropped_count) {
Tom Cherry283c9a12020-05-14 19:25:05 -0700136 auto lock = std::lock_guard{reader_list_->reader_threads_lock()};
Mark Salyzyn0175b072014-02-26 09:50:16 -0800137
Tom Cherrycef47bb2020-05-04 17:10:16 -0700138 if (leading_dropped_) {
Tom Cherry70fadea2020-05-27 14:43:19 -0700139 if (dropped_count) {
Tom Cherry3e61a132020-05-27 10:46:37 -0700140 return FilterResult::kSkip;
Mark Salyzyn047cc072015-06-04 13:35:30 -0700141 }
Tom Cherrycef47bb2020-05-04 17:10:16 -0700142 leading_dropped_ = false;
Mark Salyzyn047cc072015-06-04 13:35:30 -0700143 }
144
Tom Cherrycef47bb2020-05-04 17:10:16 -0700145 if (count_ == 0) {
Tom Cherry70fadea2020-05-27 14:43:19 -0700146 start_ = sequence;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800147 }
148
Tom Cherry70fadea2020-05-27 14:43:19 -0700149 if ((!pid_ || pid_ == pid) && IsWatching(log_id) &&
150 (start_time_ == log_time::EPOCH || start_time_ <= realtime)) {
Tom Cherrycef47bb2020-05-04 17:10:16 -0700151 ++count_;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800152 }
153
Tom Cherry3e61a132020-05-27 10:46:37 -0700154 return FilterResult::kSkip;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800155}
156
157// A second pass to send the selected elements
Tom Cherry70fadea2020-05-27 14:43:19 -0700158FilterResult LogReaderThread::FilterSecondPass(log_id_t log_id, pid_t pid, uint64_t sequence,
159 log_time realtime, uint16_t dropped_count) {
Tom Cherry283c9a12020-05-14 19:25:05 -0700160 auto lock = std::lock_guard{reader_list_->reader_threads_lock()};
Mark Salyzyn0175b072014-02-26 09:50:16 -0800161
Tom Cherry70fadea2020-05-27 14:43:19 -0700162 start_ = sequence;
TraianX Schiauda6495d2014-12-17 10:53:41 +0200163
Tom Cherry70fadea2020-05-27 14:43:19 -0700164 if (skip_ahead_[log_id]) {
165 skip_ahead_[log_id]--;
Tom Cherry3e61a132020-05-27 10:46:37 -0700166 return FilterResult::kSkip;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800167 }
168
Tom Cherrycef47bb2020-05-04 17:10:16 -0700169 if (leading_dropped_) {
Tom Cherry70fadea2020-05-27 14:43:19 -0700170 if (dropped_count) {
Tom Cherry3e61a132020-05-27 10:46:37 -0700171 return FilterResult::kSkip;
Mark Salyzyn047cc072015-06-04 13:35:30 -0700172 }
Tom Cherrycef47bb2020-05-04 17:10:16 -0700173 leading_dropped_ = false;
Mark Salyzyn047cc072015-06-04 13:35:30 -0700174 }
175
Mark Salyzyn0175b072014-02-26 09:50:16 -0800176 // Truncate to close race between first and second pass
Tom Cherrycef47bb2020-05-04 17:10:16 -0700177 if (non_block_ && tail_ && index_ >= count_) {
Tom Cherry3e61a132020-05-27 10:46:37 -0700178 return FilterResult::kStop;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800179 }
180
Tom Cherry70fadea2020-05-27 14:43:19 -0700181 if (!IsWatching(log_id)) {
Tom Cherry3e61a132020-05-27 10:46:37 -0700182 return FilterResult::kSkip;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800183 }
184
Tom Cherry70fadea2020-05-27 14:43:19 -0700185 if (pid_ && pid_ != pid) {
Tom Cherry3e61a132020-05-27 10:46:37 -0700186 return FilterResult::kSkip;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800187 }
188
Tom Cherry70fadea2020-05-27 14:43:19 -0700189 if (start_time_ != log_time::EPOCH && realtime <= start_time_) {
Tom Cherry3e61a132020-05-27 10:46:37 -0700190 return FilterResult::kSkip;
Tom Cherry65ab7fe2019-08-21 14:53:06 -0700191 }
192
Tom Cherrycef47bb2020-05-04 17:10:16 -0700193 if (release_) {
Tom Cherry3e61a132020-05-27 10:46:37 -0700194 return FilterResult::kStop;
Jintao_Zhu5f930722018-11-11 03:13:24 -0800195 }
196
Tom Cherrycef47bb2020-05-04 17:10:16 -0700197 if (!tail_) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800198 goto ok;
199 }
200
Tom Cherrycef47bb2020-05-04 17:10:16 -0700201 ++index_;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800202
Tom Cherrycef47bb2020-05-04 17:10:16 -0700203 if (count_ > tail_ && index_ <= (count_ - tail_)) {
Tom Cherry3e61a132020-05-27 10:46:37 -0700204 return FilterResult::kSkip;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800205 }
206
Tom Cherrycef47bb2020-05-04 17:10:16 -0700207 if (!non_block_) {
208 tail_ = 0;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800209 }
210
211ok:
Tom Cherry70fadea2020-05-27 14:43:19 -0700212 if (!skip_ahead_[log_id]) {
Tom Cherry3e61a132020-05-27 10:46:37 -0700213 return FilterResult::kWrite;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800214 }
Tom Cherry3e61a132020-05-27 10:46:37 -0700215 return FilterResult::kSkip;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800216}
TraianX Schiauda6495d2014-12-17 10:53:41 +0200217
Tom Cherry6ec71e92020-05-04 12:53:36 -0700218void LogReaderThread::cleanSkip_Locked(void) {
Tom Cherrycef47bb2020-05-04 17:10:16 -0700219 memset(skip_ahead_, 0, sizeof(skip_ahead_));
TraianX Schiauda6495d2014-12-17 10:53:41 +0200220}