blob: c6e60feff67f39b3fc4f1e1c7cfe19ab31031a6a [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,
Tom Cherry855c7c82020-05-28 12:38:21 -070032 unsigned long tail, LogMask log_mask, pid_t pid,
Tom Cherry283c9a12020-05-14 19:25:05 -070033 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 pid_(pid),
40 tail_(tail),
41 count_(0),
42 index_(0),
Tom Cherrycef47bb2020-05-04 17:10:16 -070043 start_time_(start_time),
Tom Cherry68630a02020-05-11 16:29:29 -070044 deadline_(deadline),
Tom Cherry283c9a12020-05-14 19:25:05 -070045 non_block_(non_block) {
Mark Salyzyn77187782015-05-12 15:21:31 -070046 cleanSkip_Locked();
Tom Cherry855c7c82020-05-28 12:38:21 -070047 flush_to_state_ = log_buffer_->CreateFlushToState(start, log_mask);
Tom Cherrycef47bb2020-05-04 17:10:16 -070048 auto thread = std::thread{&LogReaderThread::ThreadFunction, this};
49 thread.detach();
Mark Salyzyn0175b072014-02-26 09:50:16 -080050}
51
Tom Cherrycef47bb2020-05-04 17:10:16 -070052void LogReaderThread::ThreadFunction() {
Mark Salyzyn8daa9af2014-04-28 14:07:23 -070053 prctl(PR_SET_NAME, "logd.reader.per");
54
Tom Cherrycef47bb2020-05-04 17:10:16 -070055 leading_dropped_ = true;
Mark Salyzyn047cc072015-06-04 13:35:30 -070056
Tom Cherry283c9a12020-05-14 19:25:05 -070057 auto lock = std::unique_lock{reader_list_->reader_threads_lock()};
Mark Salyzyn0175b072014-02-26 09:50:16 -080058
Tom Cherrycef47bb2020-05-04 17:10:16 -070059 while (!release_) {
Tom Cherry68630a02020-05-11 16:29:29 -070060 if (deadline_.time_since_epoch().count() != 0) {
61 if (thread_triggered_condition_.wait_until(lock, deadline_) ==
62 std::cv_status::timeout) {
63 deadline_ = {};
Mark Salyzynb75cce02015-11-30 11:35:56 -080064 }
Tom Cherrycef47bb2020-05-04 17:10:16 -070065 if (release_) {
Mark Salyzynb75cce02015-11-30 11:35:56 -080066 break;
67 }
68 }
69
Tom Cherry68630a02020-05-11 16:29:29 -070070 lock.unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -080071
Tom Cherrycef47bb2020-05-04 17:10:16 -070072 if (tail_) {
Tom Cherry855c7c82020-05-28 12:38:21 -070073 auto first_pass_state = log_buffer_->CreateFlushToState(flush_to_state_->start(),
74 flush_to_state_->log_mask());
75 log_buffer_->FlushTo(writer_.get(), *first_pass_state,
Tom Cherry70fadea2020-05-27 14:43:19 -070076 [this](log_id_t log_id, pid_t pid, uint64_t sequence,
77 log_time realtime, uint16_t dropped_count) {
78 return FilterFirstPass(log_id, pid, sequence, realtime,
79 dropped_count);
80 });
Tom Cherrycef47bb2020-05-04 17:10:16 -070081 leading_dropped_ =
82 true; // TODO: Likely a bug, if leading_dropped_ was not true before calling
83 // flushTo(), then it should not be reset to true after.
Mark Salyzyn0175b072014-02-26 09:50:16 -080084 }
Tom Cherry855c7c82020-05-28 12:38:21 -070085 bool flush_success = log_buffer_->FlushTo(
86 writer_.get(), *flush_to_state_,
87 [this](log_id_t log_id, pid_t pid, uint64_t sequence, log_time realtime,
88 uint16_t dropped_count) {
89 return FilterSecondPass(log_id, pid, sequence, realtime, dropped_count);
90 });
Mark Salyzyn0175b072014-02-26 09:50:16 -080091
Tom Cherry65ab7fe2019-08-21 14:53:06 -070092 // We only ignore entries before the original start time for the first flushTo(), if we
93 // get entries after this first flush before the original start time, then the client
94 // wouldn't have seen them.
95 // Note: this is still racy and may skip out of order events that came in since the last
96 // time the client disconnected and then reconnected with the new start time. The long term
97 // solution here is that clients must request events since a specific sequence number.
Tom Cherrycef47bb2020-05-04 17:10:16 -070098 start_time_.tv_sec = 0;
99 start_time_.tv_nsec = 0;
Tom Cherry65ab7fe2019-08-21 14:53:06 -0700100
Tom Cherry68630a02020-05-11 16:29:29 -0700101 lock.lock();
Mark Salyzyna16f7612014-08-07 08:16:52 -0700102
Tom Cherry855c7c82020-05-28 12:38:21 -0700103 if (!flush_success) {
Mark Salyzynde4bb9c2015-09-16 15:34:00 -0700104 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800105 }
106
Tom Cherrycef47bb2020-05-04 17:10:16 -0700107 if (non_block_ || release_) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800108 break;
109 }
110
Tom Cherrycef47bb2020-05-04 17:10:16 -0700111 cleanSkip_Locked();
TraianX Schiauda6495d2014-12-17 10:53:41 +0200112
Tom Cherry68630a02020-05-11 16:29:29 -0700113 if (deadline_.time_since_epoch().count() == 0) {
114 thread_triggered_condition_.wait(lock);
Mark Salyzynb75cce02015-11-30 11:35:56 -0800115 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800116 }
117
Tom Cherry283c9a12020-05-14 19:25:05 -0700118 writer_->Release();
Tom Cherry4f227862018-10-08 17:33:50 -0700119
Tom Cherry283c9a12020-05-14 19:25:05 -0700120 auto& log_reader_threads = reader_list_->reader_threads();
Tom Cherry68630a02020-05-11 16:29:29 -0700121 auto it = std::find_if(log_reader_threads.begin(), log_reader_threads.end(),
Tom Cherrycef47bb2020-05-04 17:10:16 -0700122 [this](const auto& other) { return other.get() == this; });
Tom Cherry4f227862018-10-08 17:33:50 -0700123
Tom Cherry68630a02020-05-11 16:29:29 -0700124 if (it != log_reader_threads.end()) {
125 log_reader_threads.erase(it);
Tom Cherry4f227862018-10-08 17:33:50 -0700126 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800127}
128
129// A first pass to count the number of elements
Tom Cherry855c7c82020-05-28 12:38:21 -0700130FilterResult LogReaderThread::FilterFirstPass(log_id_t, pid_t pid, uint64_t, log_time realtime,
131 uint16_t dropped_count) {
Tom Cherry283c9a12020-05-14 19:25:05 -0700132 auto lock = std::lock_guard{reader_list_->reader_threads_lock()};
Mark Salyzyn0175b072014-02-26 09:50:16 -0800133
Tom Cherrycef47bb2020-05-04 17:10:16 -0700134 if (leading_dropped_) {
Tom Cherry70fadea2020-05-27 14:43:19 -0700135 if (dropped_count) {
Tom Cherry3e61a132020-05-27 10:46:37 -0700136 return FilterResult::kSkip;
Mark Salyzyn047cc072015-06-04 13:35:30 -0700137 }
Tom Cherrycef47bb2020-05-04 17:10:16 -0700138 leading_dropped_ = false;
Mark Salyzyn047cc072015-06-04 13:35:30 -0700139 }
140
Tom Cherry855c7c82020-05-28 12:38:21 -0700141 if ((!pid_ || pid_ == pid) && (start_time_ == log_time::EPOCH || start_time_ <= realtime)) {
Tom Cherrycef47bb2020-05-04 17:10:16 -0700142 ++count_;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800143 }
144
Tom Cherry3e61a132020-05-27 10:46:37 -0700145 return FilterResult::kSkip;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800146}
147
148// A second pass to send the selected elements
Tom Cherry855c7c82020-05-28 12:38:21 -0700149FilterResult LogReaderThread::FilterSecondPass(log_id_t log_id, pid_t pid, uint64_t,
Tom Cherry70fadea2020-05-27 14:43:19 -0700150 log_time realtime, uint16_t dropped_count) {
Tom Cherry283c9a12020-05-14 19:25:05 -0700151 auto lock = std::lock_guard{reader_list_->reader_threads_lock()};
Mark Salyzyn0175b072014-02-26 09:50:16 -0800152
Tom Cherry70fadea2020-05-27 14:43:19 -0700153 if (skip_ahead_[log_id]) {
154 skip_ahead_[log_id]--;
Tom Cherry3e61a132020-05-27 10:46:37 -0700155 return FilterResult::kSkip;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800156 }
157
Tom Cherrycef47bb2020-05-04 17:10:16 -0700158 if (leading_dropped_) {
Tom Cherry70fadea2020-05-27 14:43:19 -0700159 if (dropped_count) {
Tom Cherry3e61a132020-05-27 10:46:37 -0700160 return FilterResult::kSkip;
Mark Salyzyn047cc072015-06-04 13:35:30 -0700161 }
Tom Cherrycef47bb2020-05-04 17:10:16 -0700162 leading_dropped_ = false;
Mark Salyzyn047cc072015-06-04 13:35:30 -0700163 }
164
Mark Salyzyn0175b072014-02-26 09:50:16 -0800165 // Truncate to close race between first and second pass
Tom Cherrycef47bb2020-05-04 17:10:16 -0700166 if (non_block_ && tail_ && index_ >= count_) {
Tom Cherry3e61a132020-05-27 10:46:37 -0700167 return FilterResult::kStop;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800168 }
169
Tom Cherry70fadea2020-05-27 14:43:19 -0700170 if (pid_ && pid_ != pid) {
Tom Cherry3e61a132020-05-27 10:46:37 -0700171 return FilterResult::kSkip;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800172 }
173
Tom Cherry70fadea2020-05-27 14:43:19 -0700174 if (start_time_ != log_time::EPOCH && realtime <= start_time_) {
Tom Cherry3e61a132020-05-27 10:46:37 -0700175 return FilterResult::kSkip;
Tom Cherry65ab7fe2019-08-21 14:53:06 -0700176 }
177
Tom Cherrycef47bb2020-05-04 17:10:16 -0700178 if (release_) {
Tom Cherry3e61a132020-05-27 10:46:37 -0700179 return FilterResult::kStop;
Jintao_Zhu5f930722018-11-11 03:13:24 -0800180 }
181
Tom Cherrycef47bb2020-05-04 17:10:16 -0700182 if (!tail_) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800183 goto ok;
184 }
185
Tom Cherrycef47bb2020-05-04 17:10:16 -0700186 ++index_;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800187
Tom Cherrycef47bb2020-05-04 17:10:16 -0700188 if (count_ > tail_ && index_ <= (count_ - tail_)) {
Tom Cherry3e61a132020-05-27 10:46:37 -0700189 return FilterResult::kSkip;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800190 }
191
Tom Cherrycef47bb2020-05-04 17:10:16 -0700192 if (!non_block_) {
193 tail_ = 0;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800194 }
195
196ok:
Tom Cherry70fadea2020-05-27 14:43:19 -0700197 if (!skip_ahead_[log_id]) {
Tom Cherry3e61a132020-05-27 10:46:37 -0700198 return FilterResult::kWrite;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800199 }
Tom Cherry3e61a132020-05-27 10:46:37 -0700200 return FilterResult::kSkip;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800201}
TraianX Schiauda6495d2014-12-17 10:53:41 +0200202
Tom Cherry6ec71e92020-05-04 12:53:36 -0700203void LogReaderThread::cleanSkip_Locked(void) {
Tom Cherrycef47bb2020-05-04 17:10:16 -0700204 memset(skip_ahead_, 0, sizeof(skip_ahead_));
TraianX Schiauda6495d2014-12-17 10:53:41 +0200205}