Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012-2013 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 Cherry | 6ec71e9 | 2020-05-04 12:53:36 -0700 | [diff] [blame] | 17 | #pragma once |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 18 | |
| 19 | #include <pthread.h> |
Jintao Zhu | d3987a9 | 2018-12-20 23:10:41 +0800 | [diff] [blame] | 20 | #include <sys/socket.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 21 | #include <sys/types.h> |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 22 | #include <time.h> |
Mark Salyzyn | 98dca2d | 2015-08-19 13:10:14 -0700 | [diff] [blame] | 23 | |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 24 | #include <chrono> |
| 25 | #include <condition_variable> |
Mark Salyzyn | 98dca2d | 2015-08-19 13:10:14 -0700 | [diff] [blame] | 26 | #include <list> |
Tom Cherry | 4f22786 | 2018-10-08 17:33:50 -0700 | [diff] [blame] | 27 | #include <memory> |
Mark Salyzyn | 98dca2d | 2015-08-19 13:10:14 -0700 | [diff] [blame] | 28 | |
Mark Salyzyn | aeaaf81 | 2016-09-30 13:30:33 -0700 | [diff] [blame] | 29 | #include <log/log.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 30 | #include <sysutils/SocketClient.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 31 | |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 32 | #include "LogBuffer.h" |
Tom Cherry | 283c9a1 | 2020-05-14 19:25:05 -0700 | [diff] [blame] | 33 | #include "LogWriter.h" |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 34 | |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 35 | class LogReaderList; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 36 | |
Tom Cherry | 6ec71e9 | 2020-05-04 12:53:36 -0700 | [diff] [blame] | 37 | class LogReaderThread { |
Tom Cherry | 10d086e | 2019-08-21 14:16:34 -0700 | [diff] [blame] | 38 | public: |
Tom Cherry | 283c9a1 | 2020-05-14 19:25:05 -0700 | [diff] [blame] | 39 | LogReaderThread(LogBuffer* log_buffer, LogReaderList* reader_list, |
| 40 | std::unique_ptr<LogWriter> writer, bool non_block, unsigned long tail, |
Tom Cherry | 855c7c8 | 2020-05-28 12:38:21 -0700 | [diff] [blame] | 41 | LogMask log_mask, pid_t pid, log_time start_time, uint64_t sequence, |
Tom Cherry | 283c9a1 | 2020-05-14 19:25:05 -0700 | [diff] [blame] | 42 | std::chrono::steady_clock::time_point deadline); |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 43 | void triggerReader_Locked() { thread_triggered_condition_.notify_all(); } |
Mark Salyzyn | a16f761 | 2014-08-07 08:16:52 -0700 | [diff] [blame] | 44 | |
Tom Cherry | cef47bb | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 45 | void triggerSkip_Locked(log_id_t id, unsigned int skip) { skip_ahead_[id] = skip; } |
Tom Cherry | 6ec71e9 | 2020-05-04 12:53:36 -0700 | [diff] [blame] | 46 | void cleanSkip_Locked(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 47 | |
Tom Cherry | 6ec71e9 | 2020-05-04 12:53:36 -0700 | [diff] [blame] | 48 | void release_Locked() { |
Jintao Zhu | d3987a9 | 2018-12-20 23:10:41 +0800 | [diff] [blame] | 49 | // gracefully shut down the socket. |
Tom Cherry | 283c9a1 | 2020-05-14 19:25:05 -0700 | [diff] [blame] | 50 | writer_->Shutdown(); |
Tom Cherry | cef47bb | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 51 | release_ = true; |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 52 | thread_triggered_condition_.notify_all(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Tom Cherry | 855c7c8 | 2020-05-28 12:38:21 -0700 | [diff] [blame] | 55 | bool IsWatching(log_id_t id) const { return flush_to_state_->log_mask() & (1 << id); } |
| 56 | bool IsWatchingMultiple(LogMask log_mask) const { |
| 57 | return flush_to_state_->log_mask() & log_mask; |
| 58 | } |
Tom Cherry | cef47bb | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 59 | |
Tom Cherry | 283c9a1 | 2020-05-14 19:25:05 -0700 | [diff] [blame] | 60 | std::string name() const { return writer_->name(); } |
Tom Cherry | 855c7c8 | 2020-05-28 12:38:21 -0700 | [diff] [blame] | 61 | uint64_t start() const { return flush_to_state_->start(); } |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 62 | std::chrono::steady_clock::time_point deadline() const { return deadline_; } |
Tom Cherry | 79d54f7 | 2020-05-04 11:13:55 -0700 | [diff] [blame] | 63 | |
| 64 | private: |
Tom Cherry | cef47bb | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 65 | void ThreadFunction(); |
| 66 | // flushTo filter callbacks |
Tom Cherry | b3e1633 | 2020-05-28 20:02:42 -0700 | [diff] [blame^] | 67 | FilterResult FilterFirstPass(log_id_t log_id, pid_t pid, uint64_t sequence, log_time realtime); |
| 68 | FilterResult FilterSecondPass(log_id_t log_id, pid_t pid, uint64_t sequence, log_time realtime); |
Tom Cherry | cef47bb | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 69 | |
Tom Cherry | 283c9a1 | 2020-05-14 19:25:05 -0700 | [diff] [blame] | 70 | std::condition_variable thread_triggered_condition_; |
| 71 | LogBuffer* log_buffer_; |
| 72 | LogReaderList* reader_list_; |
| 73 | std::unique_ptr<LogWriter> writer_; |
| 74 | |
Tom Cherry | cef47bb | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 75 | // Set to true to cause the thread to end and the LogReaderThread to delete itself. |
| 76 | bool release_ = false; |
Tom Cherry | cef47bb | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 77 | |
Tom Cherry | cef47bb | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 78 | // If set to non-zero, only pids equal to this are read by the reader. |
| 79 | const pid_t pid_; |
| 80 | // When a reader is referencing (via start_) old elements in the log buffer, and the log |
| 81 | // buffer's size grows past its memory limit, the log buffer may request the reader to skip |
| 82 | // ahead a specified number of logs. |
| 83 | unsigned int skip_ahead_[LOG_ID_MAX]; |
Tom Cherry | 855c7c8 | 2020-05-28 12:38:21 -0700 | [diff] [blame] | 84 | // LogBuffer::FlushTo() needs to store state across subsequent calls. |
| 85 | std::unique_ptr<FlushToState> flush_to_state_; |
Tom Cherry | cef47bb | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 86 | |
| 87 | // These next three variables are used for reading only the most recent lines aka `adb logcat |
| 88 | // -t` / `adb logcat -T`. |
| 89 | // tail_ is the number of most recent lines to print. |
| 90 | unsigned long tail_; |
| 91 | // count_ is the result of a first pass through the log buffer to determine how many total |
| 92 | // messages there are. |
| 93 | unsigned long count_; |
| 94 | // index_ is used along with count_ to only start sending lines once index_ > (count_ - tail_) |
| 95 | // and to disconnect the reader (if it is dumpAndClose, `adb logcat -t`), when index_ >= count_. |
| 96 | unsigned long index_; |
| 97 | |
Tom Cherry | cef47bb | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 98 | // When a reader requests logs starting from a given timestamp, its stored here for the first |
| 99 | // pass, such that logs before this time stamp that are accumulated in the buffer are ignored. |
| 100 | log_time start_time_; |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 101 | // CLOCK_MONOTONIC based deadline used for log wrapping. If this deadline expires before logs |
Tom Cherry | cef47bb | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 102 | // wrap, then wake up and send the logs to the reader anyway. |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 103 | std::chrono::steady_clock::time_point deadline_; |
Tom Cherry | cef47bb | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 104 | // If this reader is 'dumpAndClose' and will disconnect once it has read its intended logs. |
| 105 | const bool non_block_; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 106 | }; |