blob: 6ac9741c30b8cf8f1bb79cb60f0786fe648a55ab [file] [log] [blame]
Mark Salyzyn12bac902014-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 Cherry1647cd42020-05-04 12:53:36 -070017#include "LogReaderThread.h"
18
Mark Salyzync959c642015-11-30 11:35:56 -080019#include <errno.h>
Mark Salyzynbec7b2d2017-03-31 10:48:39 -070020#include <string.h>
Mark Salyzyn4e5efdc2014-04-28 14:07:23 -070021#include <sys/prctl.h>
22
Tom Cherry7e3bc962020-05-04 17:10:16 -070023#include <thread>
24
Mark Salyzyn12bac902014-02-26 09:50:16 -080025#include "LogBuffer.h"
Tom Cherryadf2e442020-05-14 19:25:05 -070026#include "LogReaderList.h"
Mark Salyzyn12bac902014-02-26 09:50:16 -080027
Tom Cherryadf2e442020-05-14 19:25:05 -070028LogReaderThread::LogReaderThread(LogBuffer* log_buffer, LogReaderList* reader_list,
29 std::unique_ptr<LogWriter> writer, bool non_block,
Tom Cherryd444ab42020-05-28 12:38:21 -070030 unsigned long tail, LogMask log_mask, pid_t pid,
Tom Cherryadf2e442020-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 Cherry5ecfbf02020-05-11 16:29:29 -070034 reader_list_(reader_list),
Tom Cherryadf2e442020-05-14 19:25:05 -070035 writer_(std::move(writer)),
Tom Cherry7e3bc962020-05-04 17:10:16 -070036 pid_(pid),
37 tail_(tail),
38 count_(0),
39 index_(0),
Tom Cherry7e3bc962020-05-04 17:10:16 -070040 start_time_(start_time),
Tom Cherry5ecfbf02020-05-11 16:29:29 -070041 deadline_(deadline),
Tom Cherryadf2e442020-05-14 19:25:05 -070042 non_block_(non_block) {
Mark Salyzyncd766f92015-05-12 15:21:31 -070043 cleanSkip_Locked();
Tom Cherryd444ab42020-05-28 12:38:21 -070044 flush_to_state_ = log_buffer_->CreateFlushToState(start, log_mask);
Tom Cherry7e3bc962020-05-04 17:10:16 -070045 auto thread = std::thread{&LogReaderThread::ThreadFunction, this};
46 thread.detach();
Mark Salyzyn12bac902014-02-26 09:50:16 -080047}
48
Tom Cherry7e3bc962020-05-04 17:10:16 -070049void LogReaderThread::ThreadFunction() {
Mark Salyzyn4e5efdc2014-04-28 14:07:23 -070050 prctl(PR_SET_NAME, "logd.reader.per");
51
Tom Cherryadf2e442020-05-14 19:25:05 -070052 auto lock = std::unique_lock{reader_list_->reader_threads_lock()};
Mark Salyzyn12bac902014-02-26 09:50:16 -080053
Tom Cherry7e3bc962020-05-04 17:10:16 -070054 while (!release_) {
Tom Cherry5ecfbf02020-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 Salyzync959c642015-11-30 11:35:56 -080059 }
Tom Cherry7e3bc962020-05-04 17:10:16 -070060 if (release_) {
Mark Salyzync959c642015-11-30 11:35:56 -080061 break;
62 }
63 }
64
Tom Cherry5ecfbf02020-05-11 16:29:29 -070065 lock.unlock();
Mark Salyzyn12bac902014-02-26 09:50:16 -080066
Tom Cherry7e3bc962020-05-04 17:10:16 -070067 if (tail_) {
Tom Cherryd444ab42020-05-28 12:38:21 -070068 auto first_pass_state = log_buffer_->CreateFlushToState(flush_to_state_->start(),
69 flush_to_state_->log_mask());
Tom Cherryca4b25d2020-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 });
Tom Cherry154b63a2020-10-05 11:35:59 -070075 log_buffer_->DeleteFlushToState(std::move(first_pass_state));
Mark Salyzyn12bac902014-02-26 09:50:16 -080076 }
Tom Cherryd444ab42020-05-28 12:38:21 -070077 bool flush_success = log_buffer_->FlushTo(
78 writer_.get(), *flush_to_state_,
Tom Cherryca4b25d2020-05-28 20:02:42 -070079 [this](log_id_t log_id, pid_t pid, uint64_t sequence, log_time realtime) {
80 return FilterSecondPass(log_id, pid, sequence, realtime);
Tom Cherryd444ab42020-05-28 12:38:21 -070081 });
Mark Salyzyn12bac902014-02-26 09:50:16 -080082
Tom Cherrye8d20892019-08-21 14:53:06 -070083 // We only ignore entries before the original start time for the first flushTo(), if we
84 // get entries after this first flush before the original start time, then the client
85 // wouldn't have seen them.
86 // Note: this is still racy and may skip out of order events that came in since the last
87 // time the client disconnected and then reconnected with the new start time. The long term
88 // solution here is that clients must request events since a specific sequence number.
Tom Cherry7e3bc962020-05-04 17:10:16 -070089 start_time_.tv_sec = 0;
90 start_time_.tv_nsec = 0;
Tom Cherrye8d20892019-08-21 14:53:06 -070091
Tom Cherry5ecfbf02020-05-11 16:29:29 -070092 lock.lock();
Mark Salyzyn740d7c22014-08-07 08:16:52 -070093
Tom Cherryd444ab42020-05-28 12:38:21 -070094 if (!flush_success) {
Mark Salyzyn28cb69a2015-09-16 15:34:00 -070095 break;
Mark Salyzyn12bac902014-02-26 09:50:16 -080096 }
97
Tom Cherry7e3bc962020-05-04 17:10:16 -070098 if (non_block_ || release_) {
Mark Salyzyn12bac902014-02-26 09:50:16 -080099 break;
100 }
101
Tom Cherry7e3bc962020-05-04 17:10:16 -0700102 cleanSkip_Locked();
TraianX Schiau6ba427e2014-12-17 10:53:41 +0200103
Tom Cherry5ecfbf02020-05-11 16:29:29 -0700104 if (deadline_.time_since_epoch().count() == 0) {
105 thread_triggered_condition_.wait(lock);
Mark Salyzync959c642015-11-30 11:35:56 -0800106 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800107 }
108
Tom Cherry154b63a2020-10-05 11:35:59 -0700109 lock.unlock();
110 log_buffer_->DeleteFlushToState(std::move(flush_to_state_));
111 lock.lock();
112
Tom Cherryadf2e442020-05-14 19:25:05 -0700113 writer_->Release();
Tom Cherry06e478b2018-10-08 17:33:50 -0700114
Tom Cherryadf2e442020-05-14 19:25:05 -0700115 auto& log_reader_threads = reader_list_->reader_threads();
Tom Cherry5ecfbf02020-05-11 16:29:29 -0700116 auto it = std::find_if(log_reader_threads.begin(), log_reader_threads.end(),
Tom Cherry7e3bc962020-05-04 17:10:16 -0700117 [this](const auto& other) { return other.get() == this; });
Tom Cherry06e478b2018-10-08 17:33:50 -0700118
Tom Cherry5ecfbf02020-05-11 16:29:29 -0700119 if (it != log_reader_threads.end()) {
120 log_reader_threads.erase(it);
Tom Cherry06e478b2018-10-08 17:33:50 -0700121 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800122}
123
124// A first pass to count the number of elements
Tom Cherryca4b25d2020-05-28 20:02:42 -0700125FilterResult LogReaderThread::FilterFirstPass(log_id_t, pid_t pid, uint64_t, log_time realtime) {
Tom Cherryadf2e442020-05-14 19:25:05 -0700126 auto lock = std::lock_guard{reader_list_->reader_threads_lock()};
Mark Salyzyn12bac902014-02-26 09:50:16 -0800127
Tom Cherryd444ab42020-05-28 12:38:21 -0700128 if ((!pid_ || pid_ == pid) && (start_time_ == log_time::EPOCH || start_time_ <= realtime)) {
Tom Cherry7e3bc962020-05-04 17:10:16 -0700129 ++count_;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800130 }
131
Tom Cherryc92cbf62020-05-27 10:46:37 -0700132 return FilterResult::kSkip;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800133}
134
135// A second pass to send the selected elements
Tom Cherryd444ab42020-05-28 12:38:21 -0700136FilterResult LogReaderThread::FilterSecondPass(log_id_t log_id, pid_t pid, uint64_t,
Tom Cherryca4b25d2020-05-28 20:02:42 -0700137 log_time realtime) {
Tom Cherryadf2e442020-05-14 19:25:05 -0700138 auto lock = std::lock_guard{reader_list_->reader_threads_lock()};
Mark Salyzyn12bac902014-02-26 09:50:16 -0800139
Tom Cherrybaa25a22020-05-27 14:43:19 -0700140 if (skip_ahead_[log_id]) {
141 skip_ahead_[log_id]--;
Tom Cherryc92cbf62020-05-27 10:46:37 -0700142 return FilterResult::kSkip;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800143 }
144
Mark Salyzyn12bac902014-02-26 09:50:16 -0800145 // Truncate to close race between first and second pass
Tom Cherry7e3bc962020-05-04 17:10:16 -0700146 if (non_block_ && tail_ && index_ >= count_) {
Tom Cherryc92cbf62020-05-27 10:46:37 -0700147 return FilterResult::kStop;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800148 }
149
Tom Cherrybaa25a22020-05-27 14:43:19 -0700150 if (pid_ && pid_ != pid) {
Tom Cherryc92cbf62020-05-27 10:46:37 -0700151 return FilterResult::kSkip;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800152 }
153
Tom Cherrybaa25a22020-05-27 14:43:19 -0700154 if (start_time_ != log_time::EPOCH && realtime <= start_time_) {
Tom Cherryc92cbf62020-05-27 10:46:37 -0700155 return FilterResult::kSkip;
Tom Cherrye8d20892019-08-21 14:53:06 -0700156 }
157
Tom Cherry7e3bc962020-05-04 17:10:16 -0700158 if (release_) {
Tom Cherryc92cbf62020-05-27 10:46:37 -0700159 return FilterResult::kStop;
Jintao_Zhu3e6f9d82018-11-11 03:13:24 -0800160 }
161
Tom Cherry7e3bc962020-05-04 17:10:16 -0700162 if (!tail_) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800163 goto ok;
164 }
165
Tom Cherry7e3bc962020-05-04 17:10:16 -0700166 ++index_;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800167
Tom Cherry7e3bc962020-05-04 17:10:16 -0700168 if (count_ > tail_ && index_ <= (count_ - tail_)) {
Tom Cherryc92cbf62020-05-27 10:46:37 -0700169 return FilterResult::kSkip;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800170 }
171
Tom Cherry7e3bc962020-05-04 17:10:16 -0700172 if (!non_block_) {
173 tail_ = 0;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800174 }
175
176ok:
Tom Cherrybaa25a22020-05-27 14:43:19 -0700177 if (!skip_ahead_[log_id]) {
Tom Cherryc92cbf62020-05-27 10:46:37 -0700178 return FilterResult::kWrite;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800179 }
Tom Cherryc92cbf62020-05-27 10:46:37 -0700180 return FilterResult::kSkip;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800181}
TraianX Schiau6ba427e2014-12-17 10:53:41 +0200182
Tom Cherry1647cd42020-05-04 12:53:36 -0700183void LogReaderThread::cleanSkip_Locked(void) {
Tom Cherry7e3bc962020-05-04 17:10:16 -0700184 memset(skip_ahead_, 0, sizeof(skip_ahead_));
TraianX Schiau6ba427e2014-12-17 10:53:41 +0200185}