blob: dc8582d1a4d710af1c16ed23e1aca7b549dec8a2 [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)),
Tom Cherrycef47bb2020-05-04 17:10:16 -070038 pid_(pid),
39 tail_(tail),
40 count_(0),
41 index_(0),
Tom Cherrycef47bb2020-05-04 17:10:16 -070042 start_time_(start_time),
Tom Cherry68630a02020-05-11 16:29:29 -070043 deadline_(deadline),
Tom Cherry283c9a12020-05-14 19:25:05 -070044 non_block_(non_block) {
Mark Salyzyn77187782015-05-12 15:21:31 -070045 cleanSkip_Locked();
Tom Cherry855c7c82020-05-28 12:38:21 -070046 flush_to_state_ = log_buffer_->CreateFlushToState(start, log_mask);
Tom Cherrycef47bb2020-05-04 17:10:16 -070047 auto thread = std::thread{&LogReaderThread::ThreadFunction, this};
48 thread.detach();
Mark Salyzyn0175b072014-02-26 09:50:16 -080049}
50
Tom Cherrycef47bb2020-05-04 17:10:16 -070051void LogReaderThread::ThreadFunction() {
Mark Salyzyn8daa9af2014-04-28 14:07:23 -070052 prctl(PR_SET_NAME, "logd.reader.per");
53
Tom Cherry283c9a12020-05-14 19:25:05 -070054 auto lock = std::unique_lock{reader_list_->reader_threads_lock()};
Mark Salyzyn0175b072014-02-26 09:50:16 -080055
Tom Cherrycef47bb2020-05-04 17:10:16 -070056 while (!release_) {
Tom Cherry68630a02020-05-11 16:29:29 -070057 if (deadline_.time_since_epoch().count() != 0) {
58 if (thread_triggered_condition_.wait_until(lock, deadline_) ==
59 std::cv_status::timeout) {
60 deadline_ = {};
Mark Salyzynb75cce02015-11-30 11:35:56 -080061 }
Tom Cherrycef47bb2020-05-04 17:10:16 -070062 if (release_) {
Mark Salyzynb75cce02015-11-30 11:35:56 -080063 break;
64 }
65 }
66
Tom Cherry68630a02020-05-11 16:29:29 -070067 lock.unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -080068
Tom Cherrycef47bb2020-05-04 17:10:16 -070069 if (tail_) {
Tom Cherry855c7c82020-05-28 12:38:21 -070070 auto first_pass_state = log_buffer_->CreateFlushToState(flush_to_state_->start(),
71 flush_to_state_->log_mask());
Tom Cherryb3e16332020-05-28 20:02:42 -070072 log_buffer_->FlushTo(
73 writer_.get(), *first_pass_state,
74 [this](log_id_t log_id, pid_t pid, uint64_t sequence, log_time realtime) {
75 return FilterFirstPass(log_id, pid, sequence, realtime);
76 });
Mark Salyzyn0175b072014-02-26 09:50:16 -080077 }
Tom Cherry855c7c82020-05-28 12:38:21 -070078 bool flush_success = log_buffer_->FlushTo(
79 writer_.get(), *flush_to_state_,
Tom Cherryb3e16332020-05-28 20:02:42 -070080 [this](log_id_t log_id, pid_t pid, uint64_t sequence, log_time realtime) {
81 return FilterSecondPass(log_id, pid, sequence, realtime);
Tom Cherry855c7c82020-05-28 12:38:21 -070082 });
Mark Salyzyn0175b072014-02-26 09:50:16 -080083
Tom Cherry65ab7fe2019-08-21 14:53:06 -070084 // We only ignore entries before the original start time for the first flushTo(), if we
85 // get entries after this first flush before the original start time, then the client
86 // wouldn't have seen them.
87 // Note: this is still racy and may skip out of order events that came in since the last
88 // time the client disconnected and then reconnected with the new start time. The long term
89 // solution here is that clients must request events since a specific sequence number.
Tom Cherrycef47bb2020-05-04 17:10:16 -070090 start_time_.tv_sec = 0;
91 start_time_.tv_nsec = 0;
Tom Cherry65ab7fe2019-08-21 14:53:06 -070092
Tom Cherry68630a02020-05-11 16:29:29 -070093 lock.lock();
Mark Salyzyna16f7612014-08-07 08:16:52 -070094
Tom Cherry855c7c82020-05-28 12:38:21 -070095 if (!flush_success) {
Mark Salyzynde4bb9c2015-09-16 15:34:00 -070096 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -080097 }
98
Tom Cherrycef47bb2020-05-04 17:10:16 -070099 if (non_block_ || release_) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800100 break;
101 }
102
Tom Cherrycef47bb2020-05-04 17:10:16 -0700103 cleanSkip_Locked();
TraianX Schiauda6495d2014-12-17 10:53:41 +0200104
Tom Cherry68630a02020-05-11 16:29:29 -0700105 if (deadline_.time_since_epoch().count() == 0) {
106 thread_triggered_condition_.wait(lock);
Mark Salyzynb75cce02015-11-30 11:35:56 -0800107 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800108 }
109
Tom Cherry283c9a12020-05-14 19:25:05 -0700110 writer_->Release();
Tom Cherry4f227862018-10-08 17:33:50 -0700111
Tom Cherry283c9a12020-05-14 19:25:05 -0700112 auto& log_reader_threads = reader_list_->reader_threads();
Tom Cherry68630a02020-05-11 16:29:29 -0700113 auto it = std::find_if(log_reader_threads.begin(), log_reader_threads.end(),
Tom Cherrycef47bb2020-05-04 17:10:16 -0700114 [this](const auto& other) { return other.get() == this; });
Tom Cherry4f227862018-10-08 17:33:50 -0700115
Tom Cherry68630a02020-05-11 16:29:29 -0700116 if (it != log_reader_threads.end()) {
117 log_reader_threads.erase(it);
Tom Cherry4f227862018-10-08 17:33:50 -0700118 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800119}
120
121// A first pass to count the number of elements
Tom Cherryb3e16332020-05-28 20:02:42 -0700122FilterResult LogReaderThread::FilterFirstPass(log_id_t, pid_t pid, uint64_t, log_time realtime) {
Tom Cherry283c9a12020-05-14 19:25:05 -0700123 auto lock = std::lock_guard{reader_list_->reader_threads_lock()};
Mark Salyzyn0175b072014-02-26 09:50:16 -0800124
Tom Cherry855c7c82020-05-28 12:38:21 -0700125 if ((!pid_ || pid_ == pid) && (start_time_ == log_time::EPOCH || start_time_ <= realtime)) {
Tom Cherrycef47bb2020-05-04 17:10:16 -0700126 ++count_;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800127 }
128
Tom Cherry3e61a132020-05-27 10:46:37 -0700129 return FilterResult::kSkip;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800130}
131
132// A second pass to send the selected elements
Tom Cherry855c7c82020-05-28 12:38:21 -0700133FilterResult LogReaderThread::FilterSecondPass(log_id_t log_id, pid_t pid, uint64_t,
Tom Cherryb3e16332020-05-28 20:02:42 -0700134 log_time realtime) {
Tom Cherry283c9a12020-05-14 19:25:05 -0700135 auto lock = std::lock_guard{reader_list_->reader_threads_lock()};
Mark Salyzyn0175b072014-02-26 09:50:16 -0800136
Tom Cherry70fadea2020-05-27 14:43:19 -0700137 if (skip_ahead_[log_id]) {
138 skip_ahead_[log_id]--;
Tom Cherry3e61a132020-05-27 10:46:37 -0700139 return FilterResult::kSkip;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800140 }
141
Mark Salyzyn0175b072014-02-26 09:50:16 -0800142 // Truncate to close race between first and second pass
Tom Cherrycef47bb2020-05-04 17:10:16 -0700143 if (non_block_ && tail_ && index_ >= count_) {
Tom Cherry3e61a132020-05-27 10:46:37 -0700144 return FilterResult::kStop;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800145 }
146
Tom Cherry70fadea2020-05-27 14:43:19 -0700147 if (pid_ && pid_ != pid) {
Tom Cherry3e61a132020-05-27 10:46:37 -0700148 return FilterResult::kSkip;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800149 }
150
Tom Cherry70fadea2020-05-27 14:43:19 -0700151 if (start_time_ != log_time::EPOCH && realtime <= start_time_) {
Tom Cherry3e61a132020-05-27 10:46:37 -0700152 return FilterResult::kSkip;
Tom Cherry65ab7fe2019-08-21 14:53:06 -0700153 }
154
Tom Cherrycef47bb2020-05-04 17:10:16 -0700155 if (release_) {
Tom Cherry3e61a132020-05-27 10:46:37 -0700156 return FilterResult::kStop;
Jintao_Zhu5f930722018-11-11 03:13:24 -0800157 }
158
Tom Cherrycef47bb2020-05-04 17:10:16 -0700159 if (!tail_) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800160 goto ok;
161 }
162
Tom Cherrycef47bb2020-05-04 17:10:16 -0700163 ++index_;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800164
Tom Cherrycef47bb2020-05-04 17:10:16 -0700165 if (count_ > tail_ && index_ <= (count_ - tail_)) {
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 (!non_block_) {
170 tail_ = 0;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800171 }
172
173ok:
Tom Cherry70fadea2020-05-27 14:43:19 -0700174 if (!skip_ahead_[log_id]) {
Tom Cherry3e61a132020-05-27 10:46:37 -0700175 return FilterResult::kWrite;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800176 }
Tom Cherry3e61a132020-05-27 10:46:37 -0700177 return FilterResult::kSkip;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800178}
TraianX Schiauda6495d2014-12-17 10:53:41 +0200179
Tom Cherry6ec71e92020-05-04 12:53:36 -0700180void LogReaderThread::cleanSkip_Locked(void) {
Tom Cherrycef47bb2020-05-04 17:10:16 -0700181 memset(skip_ahead_, 0, sizeof(skip_ahead_));
TraianX Schiauda6495d2014-12-17 10:53:41 +0200182}