blob: cf769bafb51f6dfc2ef425eca3d8bb3f41113674 [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"
Tom Cherryc5c9eba2020-10-06 10:22:35 -070027#include "SerializedFlushToState.h"
Mark Salyzyn12bac902014-02-26 09:50:16 -080028
Tom Cherryadf2e442020-05-14 19:25:05 -070029LogReaderThread::LogReaderThread(LogBuffer* log_buffer, LogReaderList* reader_list,
30 std::unique_ptr<LogWriter> writer, bool non_block,
Tom Cherryd444ab42020-05-28 12:38:21 -070031 unsigned long tail, LogMask log_mask, pid_t pid,
Tom Cherryadf2e442020-05-14 19:25:05 -070032 log_time start_time, uint64_t start,
33 std::chrono::steady_clock::time_point deadline)
34 : log_buffer_(log_buffer),
Tom Cherry5ecfbf02020-05-11 16:29:29 -070035 reader_list_(reader_list),
Tom Cherryadf2e442020-05-14 19:25:05 -070036 writer_(std::move(writer)),
Tom Cherry7e3bc962020-05-04 17:10:16 -070037 pid_(pid),
38 tail_(tail),
39 count_(0),
40 index_(0),
Tom Cherry7e3bc962020-05-04 17:10:16 -070041 start_time_(start_time),
Tom Cherry5ecfbf02020-05-11 16:29:29 -070042 deadline_(deadline),
Tom Cherryadf2e442020-05-14 19:25:05 -070043 non_block_(non_block) {
Tom Cherryc5c9eba2020-10-06 10:22:35 -070044 CleanSkip();
Tom Cherryd444ab42020-05-28 12:38:21 -070045 flush_to_state_ = log_buffer_->CreateFlushToState(start, log_mask);
Tom Cherry7e3bc962020-05-04 17:10:16 -070046 auto thread = std::thread{&LogReaderThread::ThreadFunction, this};
47 thread.detach();
Mark Salyzyn12bac902014-02-26 09:50:16 -080048}
49
Tom Cherry7e3bc962020-05-04 17:10:16 -070050void LogReaderThread::ThreadFunction() {
Mark Salyzyn4e5efdc2014-04-28 14:07:23 -070051 prctl(PR_SET_NAME, "logd.reader.per");
52
Tom Cherryc5c9eba2020-10-06 10:22:35 -070053 auto lock = std::unique_lock{logd_lock};
54 auto lock_assertion = android::base::ScopedLockAssertion{logd_lock};
Mark Salyzyn12bac902014-02-26 09:50:16 -080055
Tom Cherry7e3bc962020-05-04 17:10:16 -070056 while (!release_) {
Tom Cherry5ecfbf02020-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 Salyzync959c642015-11-30 11:35:56 -080061 }
Tom Cherry7e3bc962020-05-04 17:10:16 -070062 if (release_) {
Mark Salyzync959c642015-11-30 11:35:56 -080063 break;
64 }
65 }
66
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 Cherryc5c9eba2020-10-06 10:22:35 -070070 log_buffer_->FlushTo(writer_.get(), *first_pass_state,
71 [this](log_id_t log_id, pid_t pid, uint64_t sequence,
72 log_time realtime) REQUIRES(logd_lock) {
73 return FilterFirstPass(log_id, pid, sequence, realtime);
74 });
Mark Salyzyn12bac902014-02-26 09:50:16 -080075 }
Tom Cherryd444ab42020-05-28 12:38:21 -070076 bool flush_success = log_buffer_->FlushTo(
77 writer_.get(), *flush_to_state_,
Tom Cherryc5c9eba2020-10-06 10:22:35 -070078 [this](log_id_t log_id, pid_t pid, uint64_t sequence, log_time realtime) REQUIRES(
79 logd_lock) { return FilterSecondPass(log_id, pid, sequence, realtime); });
Mark Salyzyn12bac902014-02-26 09:50:16 -080080
Tom Cherrye8d20892019-08-21 14:53:06 -070081 // We only ignore entries before the original start time for the first flushTo(), if we
82 // get entries after this first flush before the original start time, then the client
83 // wouldn't have seen them.
84 // Note: this is still racy and may skip out of order events that came in since the last
85 // time the client disconnected and then reconnected with the new start time. The long term
86 // solution here is that clients must request events since a specific sequence number.
Tom Cherry7e3bc962020-05-04 17:10:16 -070087 start_time_.tv_sec = 0;
88 start_time_.tv_nsec = 0;
Tom Cherrye8d20892019-08-21 14:53:06 -070089
Tom Cherryd444ab42020-05-28 12:38:21 -070090 if (!flush_success) {
Mark Salyzyn28cb69a2015-09-16 15:34:00 -070091 break;
Mark Salyzyn12bac902014-02-26 09:50:16 -080092 }
93
Tom Cherry7e3bc962020-05-04 17:10:16 -070094 if (non_block_ || release_) {
Mark Salyzyn12bac902014-02-26 09:50:16 -080095 break;
96 }
97
Tom Cherryc5c9eba2020-10-06 10:22:35 -070098 CleanSkip();
TraianX Schiau6ba427e2014-12-17 10:53:41 +020099
Tom Cherry5ecfbf02020-05-11 16:29:29 -0700100 if (deadline_.time_since_epoch().count() == 0) {
101 thread_triggered_condition_.wait(lock);
Mark Salyzync959c642015-11-30 11:35:56 -0800102 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800103 }
104
Tom Cherryadf2e442020-05-14 19:25:05 -0700105 writer_->Release();
Tom Cherry06e478b2018-10-08 17:33:50 -0700106
Tom Cherryadf2e442020-05-14 19:25:05 -0700107 auto& log_reader_threads = reader_list_->reader_threads();
Tom Cherry5ecfbf02020-05-11 16:29:29 -0700108 auto it = std::find_if(log_reader_threads.begin(), log_reader_threads.end(),
Tom Cherry7e3bc962020-05-04 17:10:16 -0700109 [this](const auto& other) { return other.get() == this; });
Tom Cherry06e478b2018-10-08 17:33:50 -0700110
Tom Cherry5ecfbf02020-05-11 16:29:29 -0700111 if (it != log_reader_threads.end()) {
112 log_reader_threads.erase(it);
Tom Cherry06e478b2018-10-08 17:33:50 -0700113 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800114}
115
116// A first pass to count the number of elements
Tom Cherryca4b25d2020-05-28 20:02:42 -0700117FilterResult LogReaderThread::FilterFirstPass(log_id_t, pid_t pid, uint64_t, log_time realtime) {
Tom Cherryd444ab42020-05-28 12:38:21 -0700118 if ((!pid_ || pid_ == pid) && (start_time_ == log_time::EPOCH || start_time_ <= realtime)) {
Tom Cherry7e3bc962020-05-04 17:10:16 -0700119 ++count_;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800120 }
121
Tom Cherryc92cbf62020-05-27 10:46:37 -0700122 return FilterResult::kSkip;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800123}
124
125// A second pass to send the selected elements
Tom Cherryd444ab42020-05-28 12:38:21 -0700126FilterResult LogReaderThread::FilterSecondPass(log_id_t log_id, pid_t pid, uint64_t,
Tom Cherryca4b25d2020-05-28 20:02:42 -0700127 log_time realtime) {
Tom Cherrybaa25a22020-05-27 14:43:19 -0700128 if (skip_ahead_[log_id]) {
129 skip_ahead_[log_id]--;
Tom Cherryc92cbf62020-05-27 10:46:37 -0700130 return FilterResult::kSkip;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800131 }
132
Mark Salyzyn12bac902014-02-26 09:50:16 -0800133 // Truncate to close race between first and second pass
Tom Cherry7e3bc962020-05-04 17:10:16 -0700134 if (non_block_ && tail_ && index_ >= count_) {
Tom Cherryc92cbf62020-05-27 10:46:37 -0700135 return FilterResult::kStop;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800136 }
137
Tom Cherrybaa25a22020-05-27 14:43:19 -0700138 if (pid_ && pid_ != pid) {
Tom Cherryc92cbf62020-05-27 10:46:37 -0700139 return FilterResult::kSkip;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800140 }
141
Tom Cherrybaa25a22020-05-27 14:43:19 -0700142 if (start_time_ != log_time::EPOCH && realtime <= start_time_) {
Tom Cherryc92cbf62020-05-27 10:46:37 -0700143 return FilterResult::kSkip;
Tom Cherrye8d20892019-08-21 14:53:06 -0700144 }
145
Tom Cherry7e3bc962020-05-04 17:10:16 -0700146 if (release_) {
Tom Cherryc92cbf62020-05-27 10:46:37 -0700147 return FilterResult::kStop;
Jintao_Zhu3e6f9d82018-11-11 03:13:24 -0800148 }
149
Tom Cherry7e3bc962020-05-04 17:10:16 -0700150 if (!tail_) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800151 goto ok;
152 }
153
Tom Cherry7e3bc962020-05-04 17:10:16 -0700154 ++index_;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800155
Tom Cherry7e3bc962020-05-04 17:10:16 -0700156 if (count_ > tail_ && index_ <= (count_ - tail_)) {
Tom Cherryc92cbf62020-05-27 10:46:37 -0700157 return FilterResult::kSkip;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800158 }
159
Tom Cherry7e3bc962020-05-04 17:10:16 -0700160 if (!non_block_) {
161 tail_ = 0;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800162 }
163
164ok:
Tom Cherrybaa25a22020-05-27 14:43:19 -0700165 if (!skip_ahead_[log_id]) {
Tom Cherryc92cbf62020-05-27 10:46:37 -0700166 return FilterResult::kWrite;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800167 }
Tom Cherryc92cbf62020-05-27 10:46:37 -0700168 return FilterResult::kSkip;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800169}