blob: 4a8be01fa31c62575a70b2363996aab00790f8e8 [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 Cherry283c9a12020-05-14 19:25:05 -070028LogReaderThread::LogReaderThread(LogBuffer* log_buffer, LogReaderList* reader_list,
29 std::unique_ptr<LogWriter> writer, bool non_block,
Tom Cherry855c7c82020-05-28 12:38:21 -070030 unsigned long tail, LogMask log_mask, pid_t pid,
Tom Cherry283c9a12020-05-14 19:25:05 -070031 log_time start_time, uint64_t start,
32 std::chrono::steady_clock::time_point deadline)
33 : log_buffer_(log_buffer),
Tom Cherry68630a02020-05-11 16:29:29 -070034 reader_list_(reader_list),
Tom Cherry283c9a12020-05-14 19:25:05 -070035 writer_(std::move(writer)),
Tom Cherrycef47bb2020-05-04 17:10:16 -070036 pid_(pid),
37 tail_(tail),
38 count_(0),
39 index_(0),
Tom Cherrycef47bb2020-05-04 17:10:16 -070040 start_time_(start_time),
Tom Cherry68630a02020-05-11 16:29:29 -070041 deadline_(deadline),
Tom Cherry283c9a12020-05-14 19:25:05 -070042 non_block_(non_block) {
Mark Salyzyn77187782015-05-12 15:21:31 -070043 cleanSkip_Locked();
Tom Cherry855c7c82020-05-28 12:38:21 -070044 flush_to_state_ = log_buffer_->CreateFlushToState(start, log_mask);
Tom Cherrycef47bb2020-05-04 17:10:16 -070045 auto thread = std::thread{&LogReaderThread::ThreadFunction, this};
46 thread.detach();
Mark Salyzyn0175b072014-02-26 09:50:16 -080047}
48
Tom Cherrycef47bb2020-05-04 17:10:16 -070049void LogReaderThread::ThreadFunction() {
Mark Salyzyn8daa9af2014-04-28 14:07:23 -070050 prctl(PR_SET_NAME, "logd.reader.per");
51
Tom Cherry283c9a12020-05-14 19:25:05 -070052 auto lock = std::unique_lock{reader_list_->reader_threads_lock()};
Mark Salyzyn0175b072014-02-26 09:50:16 -080053
Tom Cherrycef47bb2020-05-04 17:10:16 -070054 while (!release_) {
Tom Cherry68630a02020-05-11 16:29:29 -070055 if (deadline_.time_since_epoch().count() != 0) {
56 if (thread_triggered_condition_.wait_until(lock, deadline_) ==
57 std::cv_status::timeout) {
58 deadline_ = {};
Mark Salyzynb75cce02015-11-30 11:35:56 -080059 }
Tom Cherrycef47bb2020-05-04 17:10:16 -070060 if (release_) {
Mark Salyzynb75cce02015-11-30 11:35:56 -080061 break;
62 }
63 }
64
Tom Cherry68630a02020-05-11 16:29:29 -070065 lock.unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -080066
Tom Cherrycef47bb2020-05-04 17:10:16 -070067 if (tail_) {
Tom Cherry855c7c82020-05-28 12:38:21 -070068 auto first_pass_state = log_buffer_->CreateFlushToState(flush_to_state_->start(),
69 flush_to_state_->log_mask());
Tom Cherryb3e16332020-05-28 20:02:42 -070070 log_buffer_->FlushTo(
71 writer_.get(), *first_pass_state,
72 [this](log_id_t log_id, pid_t pid, uint64_t sequence, log_time realtime) {
73 return FilterFirstPass(log_id, pid, sequence, realtime);
74 });
Mark Salyzyn0175b072014-02-26 09:50:16 -080075 }
Tom Cherry855c7c82020-05-28 12:38:21 -070076 bool flush_success = log_buffer_->FlushTo(
77 writer_.get(), *flush_to_state_,
Tom Cherryb3e16332020-05-28 20:02:42 -070078 [this](log_id_t log_id, pid_t pid, uint64_t sequence, log_time realtime) {
79 return FilterSecondPass(log_id, pid, sequence, realtime);
Tom Cherry855c7c82020-05-28 12:38:21 -070080 });
Mark Salyzyn0175b072014-02-26 09:50:16 -080081
Tom Cherry65ab7fe2019-08-21 14:53:06 -070082 // We only ignore entries before the original start time for the first flushTo(), if we
83 // get entries after this first flush before the original start time, then the client
84 // wouldn't have seen them.
85 // Note: this is still racy and may skip out of order events that came in since the last
86 // time the client disconnected and then reconnected with the new start time. The long term
87 // solution here is that clients must request events since a specific sequence number.
Tom Cherrycef47bb2020-05-04 17:10:16 -070088 start_time_.tv_sec = 0;
89 start_time_.tv_nsec = 0;
Tom Cherry65ab7fe2019-08-21 14:53:06 -070090
Tom Cherry68630a02020-05-11 16:29:29 -070091 lock.lock();
Mark Salyzyna16f7612014-08-07 08:16:52 -070092
Tom Cherry855c7c82020-05-28 12:38:21 -070093 if (!flush_success) {
Mark Salyzynde4bb9c2015-09-16 15:34:00 -070094 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -080095 }
96
Tom Cherrycef47bb2020-05-04 17:10:16 -070097 if (non_block_ || release_) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080098 break;
99 }
100
Tom Cherrycef47bb2020-05-04 17:10:16 -0700101 cleanSkip_Locked();
TraianX Schiauda6495d2014-12-17 10:53:41 +0200102
Tom Cherry68630a02020-05-11 16:29:29 -0700103 if (deadline_.time_since_epoch().count() == 0) {
104 thread_triggered_condition_.wait(lock);
Mark Salyzynb75cce02015-11-30 11:35:56 -0800105 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800106 }
107
Tom Cherry283c9a12020-05-14 19:25:05 -0700108 writer_->Release();
Tom Cherry4f227862018-10-08 17:33:50 -0700109
Tom Cherry283c9a12020-05-14 19:25:05 -0700110 auto& log_reader_threads = reader_list_->reader_threads();
Tom Cherry68630a02020-05-11 16:29:29 -0700111 auto it = std::find_if(log_reader_threads.begin(), log_reader_threads.end(),
Tom Cherrycef47bb2020-05-04 17:10:16 -0700112 [this](const auto& other) { return other.get() == this; });
Tom Cherry4f227862018-10-08 17:33:50 -0700113
Tom Cherry68630a02020-05-11 16:29:29 -0700114 if (it != log_reader_threads.end()) {
115 log_reader_threads.erase(it);
Tom Cherry4f227862018-10-08 17:33:50 -0700116 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800117}
118
119// A first pass to count the number of elements
Tom Cherryb3e16332020-05-28 20:02:42 -0700120FilterResult LogReaderThread::FilterFirstPass(log_id_t, pid_t pid, uint64_t, log_time realtime) {
Tom Cherry283c9a12020-05-14 19:25:05 -0700121 auto lock = std::lock_guard{reader_list_->reader_threads_lock()};
Mark Salyzyn0175b072014-02-26 09:50:16 -0800122
Tom Cherry855c7c82020-05-28 12:38:21 -0700123 if ((!pid_ || pid_ == pid) && (start_time_ == log_time::EPOCH || start_time_ <= realtime)) {
Tom Cherrycef47bb2020-05-04 17:10:16 -0700124 ++count_;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800125 }
126
Tom Cherry3e61a132020-05-27 10:46:37 -0700127 return FilterResult::kSkip;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800128}
129
130// A second pass to send the selected elements
Tom Cherry855c7c82020-05-28 12:38:21 -0700131FilterResult LogReaderThread::FilterSecondPass(log_id_t log_id, pid_t pid, uint64_t,
Tom Cherryb3e16332020-05-28 20:02:42 -0700132 log_time realtime) {
Tom Cherry283c9a12020-05-14 19:25:05 -0700133 auto lock = std::lock_guard{reader_list_->reader_threads_lock()};
Mark Salyzyn0175b072014-02-26 09:50:16 -0800134
Tom Cherry70fadea2020-05-27 14:43:19 -0700135 if (skip_ahead_[log_id]) {
136 skip_ahead_[log_id]--;
Tom Cherry3e61a132020-05-27 10:46:37 -0700137 return FilterResult::kSkip;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800138 }
139
Mark Salyzyn0175b072014-02-26 09:50:16 -0800140 // Truncate to close race between first and second pass
Tom Cherrycef47bb2020-05-04 17:10:16 -0700141 if (non_block_ && tail_ && index_ >= count_) {
Tom Cherry3e61a132020-05-27 10:46:37 -0700142 return FilterResult::kStop;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800143 }
144
Tom Cherry70fadea2020-05-27 14:43:19 -0700145 if (pid_ && pid_ != pid) {
Tom Cherry3e61a132020-05-27 10:46:37 -0700146 return FilterResult::kSkip;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800147 }
148
Tom Cherry70fadea2020-05-27 14:43:19 -0700149 if (start_time_ != log_time::EPOCH && realtime <= start_time_) {
Tom Cherry3e61a132020-05-27 10:46:37 -0700150 return FilterResult::kSkip;
Tom Cherry65ab7fe2019-08-21 14:53:06 -0700151 }
152
Tom Cherrycef47bb2020-05-04 17:10:16 -0700153 if (release_) {
Tom Cherry3e61a132020-05-27 10:46:37 -0700154 return FilterResult::kStop;
Jintao_Zhu5f930722018-11-11 03:13:24 -0800155 }
156
Tom Cherrycef47bb2020-05-04 17:10:16 -0700157 if (!tail_) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800158 goto ok;
159 }
160
Tom Cherrycef47bb2020-05-04 17:10:16 -0700161 ++index_;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800162
Tom Cherrycef47bb2020-05-04 17:10:16 -0700163 if (count_ > tail_ && index_ <= (count_ - tail_)) {
Tom Cherry3e61a132020-05-27 10:46:37 -0700164 return FilterResult::kSkip;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800165 }
166
Tom Cherrycef47bb2020-05-04 17:10:16 -0700167 if (!non_block_) {
168 tail_ = 0;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800169 }
170
171ok:
Tom Cherry70fadea2020-05-27 14:43:19 -0700172 if (!skip_ahead_[log_id]) {
Tom Cherry3e61a132020-05-27 10:46:37 -0700173 return FilterResult::kWrite;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800174 }
Tom Cherry3e61a132020-05-27 10:46:37 -0700175 return FilterResult::kSkip;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800176}
TraianX Schiauda6495d2014-12-17 10:53:41 +0200177
Tom Cherry6ec71e92020-05-04 12:53:36 -0700178void LogReaderThread::cleanSkip_Locked(void) {
Tom Cherrycef47bb2020-05-04 17:10:16 -0700179 memset(skip_ahead_, 0, sizeof(skip_ahead_));
TraianX Schiauda6495d2014-12-17 10:53:41 +0200180}