blob: 2763b4a0a1af7bbba3573d0f161ee35a6aea4c4e [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);
Wenhao Wangb1f6c6c2021-11-19 16:42:43 -080046}
47
48void LogReaderThread::Run() {
Tom Cherry7e3bc962020-05-04 17:10:16 -070049 auto thread = std::thread{&LogReaderThread::ThreadFunction, this};
50 thread.detach();
Mark Salyzyn12bac902014-02-26 09:50:16 -080051}
52
Tom Cherry7e3bc962020-05-04 17:10:16 -070053void LogReaderThread::ThreadFunction() {
Mark Salyzyn4e5efdc2014-04-28 14:07:23 -070054 prctl(PR_SET_NAME, "logd.reader.per");
55
Tom Cherryc5c9eba2020-10-06 10:22:35 -070056 auto lock = std::unique_lock{logd_lock};
57 auto lock_assertion = android::base::ScopedLockAssertion{logd_lock};
Mark Salyzyn12bac902014-02-26 09:50:16 -080058
Tom Cherry7e3bc962020-05-04 17:10:16 -070059 while (!release_) {
Tom Cherry5ecfbf02020-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 Salyzync959c642015-11-30 11:35:56 -080064 }
Tom Cherry7e3bc962020-05-04 17:10:16 -070065 if (release_) {
Mark Salyzync959c642015-11-30 11:35:56 -080066 break;
67 }
68 }
69
Tom Cherry7e3bc962020-05-04 17:10:16 -070070 if (tail_) {
Tom Cherryd444ab42020-05-28 12:38:21 -070071 auto first_pass_state = log_buffer_->CreateFlushToState(flush_to_state_->start(),
72 flush_to_state_->log_mask());
Tom Cherryc5c9eba2020-10-06 10:22:35 -070073 log_buffer_->FlushTo(writer_.get(), *first_pass_state,
74 [this](log_id_t log_id, pid_t pid, uint64_t sequence,
75 log_time realtime) REQUIRES(logd_lock) {
76 return FilterFirstPass(log_id, pid, sequence, realtime);
77 });
Mark Salyzyn12bac902014-02-26 09:50:16 -080078 }
Tom Cherryd444ab42020-05-28 12:38:21 -070079 bool flush_success = log_buffer_->FlushTo(
80 writer_.get(), *flush_to_state_,
Tom Cherryc5c9eba2020-10-06 10:22:35 -070081 [this](log_id_t log_id, pid_t pid, uint64_t sequence, log_time realtime) REQUIRES(
82 logd_lock) { return FilterSecondPass(log_id, pid, sequence, realtime); });
Mark Salyzyn12bac902014-02-26 09:50:16 -080083
Tom Cherrye8d20892019-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 Cherry7e3bc962020-05-04 17:10:16 -070090 start_time_.tv_sec = 0;
91 start_time_.tv_nsec = 0;
Tom Cherrye8d20892019-08-21 14:53:06 -070092
Tom Cherryd444ab42020-05-28 12:38:21 -070093 if (!flush_success) {
Mark Salyzyn28cb69a2015-09-16 15:34:00 -070094 break;
Mark Salyzyn12bac902014-02-26 09:50:16 -080095 }
96
Tom Cherry7e3bc962020-05-04 17:10:16 -070097 if (non_block_ || release_) {
Mark Salyzyn12bac902014-02-26 09:50:16 -080098 break;
99 }
100
Tom Cherryc5c9eba2020-10-06 10:22:35 -0700101 CleanSkip();
TraianX Schiau6ba427e2014-12-17 10:53:41 +0200102
Tom Cherry5ecfbf02020-05-11 16:29:29 -0700103 if (deadline_.time_since_epoch().count() == 0) {
104 thread_triggered_condition_.wait(lock);
Mark Salyzync959c642015-11-30 11:35:56 -0800105 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800106 }
107
Tom Cherryadf2e442020-05-14 19:25:05 -0700108 writer_->Release();
Wenhao Wangb1f6c6c2021-11-19 16:42:43 -0800109 reader_list_->RemoveRunningThread(this);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800110}
111
112// A first pass to count the number of elements
Tom Cherryca4b25d2020-05-28 20:02:42 -0700113FilterResult LogReaderThread::FilterFirstPass(log_id_t, pid_t pid, uint64_t, log_time realtime) {
Tom Cherryd444ab42020-05-28 12:38:21 -0700114 if ((!pid_ || pid_ == pid) && (start_time_ == log_time::EPOCH || start_time_ <= realtime)) {
Tom Cherry7e3bc962020-05-04 17:10:16 -0700115 ++count_;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800116 }
117
Tom Cherryc92cbf62020-05-27 10:46:37 -0700118 return FilterResult::kSkip;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800119}
120
121// A second pass to send the selected elements
Tom Cherryd444ab42020-05-28 12:38:21 -0700122FilterResult LogReaderThread::FilterSecondPass(log_id_t log_id, pid_t pid, uint64_t,
Tom Cherryca4b25d2020-05-28 20:02:42 -0700123 log_time realtime) {
Tom Cherrybaa25a22020-05-27 14:43:19 -0700124 if (skip_ahead_[log_id]) {
125 skip_ahead_[log_id]--;
Tom Cherryc92cbf62020-05-27 10:46:37 -0700126 return FilterResult::kSkip;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800127 }
128
Mark Salyzyn12bac902014-02-26 09:50:16 -0800129 // Truncate to close race between first and second pass
Tom Cherry7e3bc962020-05-04 17:10:16 -0700130 if (non_block_ && tail_ && index_ >= count_) {
Tom Cherryc92cbf62020-05-27 10:46:37 -0700131 return FilterResult::kStop;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800132 }
133
Tom Cherrybaa25a22020-05-27 14:43:19 -0700134 if (pid_ && pid_ != pid) {
Tom Cherryc92cbf62020-05-27 10:46:37 -0700135 return FilterResult::kSkip;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800136 }
137
Tom Cherrybaa25a22020-05-27 14:43:19 -0700138 if (start_time_ != log_time::EPOCH && realtime <= start_time_) {
Tom Cherryc92cbf62020-05-27 10:46:37 -0700139 return FilterResult::kSkip;
Tom Cherrye8d20892019-08-21 14:53:06 -0700140 }
141
Tom Cherry7e3bc962020-05-04 17:10:16 -0700142 if (release_) {
Tom Cherryc92cbf62020-05-27 10:46:37 -0700143 return FilterResult::kStop;
Jintao_Zhu3e6f9d82018-11-11 03:13:24 -0800144 }
145
Tom Cherry7e3bc962020-05-04 17:10:16 -0700146 if (!tail_) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800147 goto ok;
148 }
149
Tom Cherry7e3bc962020-05-04 17:10:16 -0700150 ++index_;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800151
Tom Cherry7e3bc962020-05-04 17:10:16 -0700152 if (count_ > tail_ && index_ <= (count_ - tail_)) {
Tom Cherryc92cbf62020-05-27 10:46:37 -0700153 return FilterResult::kSkip;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800154 }
155
Tom Cherry7e3bc962020-05-04 17:10:16 -0700156 if (!non_block_) {
157 tail_ = 0;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800158 }
159
160ok:
Tom Cherrybaa25a22020-05-27 14:43:19 -0700161 if (!skip_ahead_[log_id]) {
Tom Cherryc92cbf62020-05-27 10:46:37 -0700162 return FilterResult::kWrite;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800163 }
Tom Cherryc92cbf62020-05-27 10:46:37 -0700164 return FilterResult::kSkip;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800165}