2014-02-26 17:50:16 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012-2013 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-05-04 19:53:36 +00:00
|
|
|
#pragma once
|
2014-02-26 17:50:16 +00:00
|
|
|
|
|
|
|
#include <pthread.h>
|
logd: improve logd prune
upon memory usage high(>log_buffer_size), logd will try to prune(erase) all those old log elements which have been read by all readers for reclaiming the memory. As such, a too slow reader will be a hinder to the success of the prune. Logd has to try to kick-out the slow-est reader when memory usage is really too high(>2 * log_buffer_size). But the kick-out operation is just a request to the reader and at what time the reader will exit is always uncertain, beyond control. Furthermore, if you kick-out reader-A, waiting for A to exit; then, another reader-B may come in; then A exit; and then you kick-out-B, waiting for B to exit; and then, ...loop for ever: yes, logd may find that there seems to be always a slow reader hinder its pruning. As we all know, that, logd will probably kick-out a slow reader(logcat), hence, indeed, almost all log capturing tools will try to re-connect logd immediately after it being kick-out-ed. Such retry makes the issue easy to happen. And, we observed that the reader thread may often be blocked by socket write operation, which hindering its exiting and hereby hindering the prune progress. We need gracefully shutdown socket to relieve it from blocking and eventually stop such disaster from happening.
Test: monkey test for one day and one night
Change-Id: I5496ff74168b71e261914b91c145aa44814a5def
2018-12-20 15:10:41 +00:00
|
|
|
#include <sys/socket.h>
|
2014-02-26 17:50:16 +00:00
|
|
|
#include <sys/types.h>
|
2017-03-10 22:31:54 +00:00
|
|
|
#include <time.h>
|
2015-08-19 20:10:14 +00:00
|
|
|
|
2020-05-11 23:29:29 +00:00
|
|
|
#include <chrono>
|
|
|
|
#include <condition_variable>
|
2015-08-19 20:10:14 +00:00
|
|
|
#include <list>
|
2018-10-09 00:33:50 +00:00
|
|
|
#include <memory>
|
2015-08-19 20:10:14 +00:00
|
|
|
|
2016-09-30 20:30:33 +00:00
|
|
|
#include <log/log.h>
|
2014-02-26 17:50:16 +00:00
|
|
|
#include <sysutils/SocketClient.h>
|
|
|
|
|
2020-05-11 23:29:29 +00:00
|
|
|
#include "LogBuffer.h"
|
|
|
|
|
2014-02-26 17:50:16 +00:00
|
|
|
class LogReader;
|
2016-02-23 16:55:43 +00:00
|
|
|
class LogBufferElement;
|
2014-02-26 17:50:16 +00:00
|
|
|
|
2020-05-04 19:53:36 +00:00
|
|
|
class LogReaderThread {
|
2019-08-21 21:16:34 +00:00
|
|
|
public:
|
2020-05-11 23:29:29 +00:00
|
|
|
LogReaderThread(LogReader& reader, LogReaderList& reader_list, SocketClient* client,
|
|
|
|
bool non_block, unsigned long tail, unsigned int log_mask, pid_t pid,
|
|
|
|
log_time start_time, uint64_t sequence,
|
|
|
|
std::chrono::steady_clock::time_point deadline, bool privileged,
|
|
|
|
bool can_read_security_logs);
|
2014-02-26 17:50:16 +00:00
|
|
|
|
2018-10-09 00:33:50 +00:00
|
|
|
bool startReader_Locked();
|
2014-02-26 17:50:16 +00:00
|
|
|
|
2020-05-11 23:29:29 +00:00
|
|
|
void triggerReader_Locked() { thread_triggered_condition_.notify_all(); }
|
2014-08-07 15:16:52 +00:00
|
|
|
|
2020-05-05 00:10:16 +00:00
|
|
|
void triggerSkip_Locked(log_id_t id, unsigned int skip) { skip_ahead_[id] = skip; }
|
2020-05-04 19:53:36 +00:00
|
|
|
void cleanSkip_Locked();
|
2014-02-26 17:50:16 +00:00
|
|
|
|
2020-05-04 19:53:36 +00:00
|
|
|
void release_Locked() {
|
logd: improve logd prune
upon memory usage high(>log_buffer_size), logd will try to prune(erase) all those old log elements which have been read by all readers for reclaiming the memory. As such, a too slow reader will be a hinder to the success of the prune. Logd has to try to kick-out the slow-est reader when memory usage is really too high(>2 * log_buffer_size). But the kick-out operation is just a request to the reader and at what time the reader will exit is always uncertain, beyond control. Furthermore, if you kick-out reader-A, waiting for A to exit; then, another reader-B may come in; then A exit; and then you kick-out-B, waiting for B to exit; and then, ...loop for ever: yes, logd may find that there seems to be always a slow reader hinder its pruning. As we all know, that, logd will probably kick-out a slow reader(logcat), hence, indeed, almost all log capturing tools will try to re-connect logd immediately after it being kick-out-ed. Such retry makes the issue easy to happen. And, we observed that the reader thread may often be blocked by socket write operation, which hindering its exiting and hereby hindering the prune progress. We need gracefully shutdown socket to relieve it from blocking and eventually stop such disaster from happening.
Test: monkey test for one day and one night
Change-Id: I5496ff74168b71e261914b91c145aa44814a5def
2018-12-20 15:10:41 +00:00
|
|
|
// gracefully shut down the socket.
|
2020-05-05 00:10:16 +00:00
|
|
|
shutdown(client_->getSocket(), SHUT_RDWR);
|
|
|
|
release_ = true;
|
2020-05-11 23:29:29 +00:00
|
|
|
thread_triggered_condition_.notify_all();
|
2017-03-10 22:31:54 +00:00
|
|
|
}
|
2014-02-26 17:50:16 +00:00
|
|
|
|
2020-05-05 00:10:16 +00:00
|
|
|
bool IsWatching(log_id_t id) const { return log_mask_ & (1 << id); }
|
|
|
|
bool IsWatchingMultiple(unsigned int log_mask) const { return log_mask_ & log_mask; }
|
|
|
|
|
|
|
|
const SocketClient* client() const { return client_; }
|
|
|
|
uint64_t start() const { return start_; }
|
2020-05-11 23:29:29 +00:00
|
|
|
std::chrono::steady_clock::time_point deadline() const { return deadline_; }
|
2020-05-04 18:13:55 +00:00
|
|
|
|
|
|
|
private:
|
2020-05-05 00:10:16 +00:00
|
|
|
void ThreadFunction();
|
|
|
|
// flushTo filter callbacks
|
2020-05-11 23:29:29 +00:00
|
|
|
FlushToResult FilterFirstPass(const LogBufferElement* element);
|
|
|
|
FlushToResult FilterSecondPass(const LogBufferElement* element);
|
2020-05-05 00:10:16 +00:00
|
|
|
|
|
|
|
// Set to true to cause the thread to end and the LogReaderThread to delete itself.
|
|
|
|
bool release_ = false;
|
|
|
|
// Indicates whether or not 'leading' (first logs seen starting from start_) 'dropped' (chatty)
|
|
|
|
// messages should be ignored.
|
|
|
|
bool leading_dropped_;
|
|
|
|
|
|
|
|
// Condition variable for waking the reader thread if there are messages pending for its client.
|
2020-05-11 23:29:29 +00:00
|
|
|
std::condition_variable thread_triggered_condition_;
|
2020-05-05 00:10:16 +00:00
|
|
|
|
|
|
|
// Reference to the parent thread that manages log reader sockets.
|
|
|
|
LogReader& reader_;
|
2020-05-11 23:29:29 +00:00
|
|
|
// Reference to the parent list that shares its lock with each instance
|
|
|
|
LogReaderList& reader_list_;
|
2020-05-05 00:10:16 +00:00
|
|
|
// A mask of the logs buffers that are read by this reader.
|
|
|
|
const unsigned int log_mask_;
|
|
|
|
// If set to non-zero, only pids equal to this are read by the reader.
|
|
|
|
const pid_t pid_;
|
|
|
|
// When a reader is referencing (via start_) old elements in the log buffer, and the log
|
|
|
|
// buffer's size grows past its memory limit, the log buffer may request the reader to skip
|
|
|
|
// ahead a specified number of logs.
|
|
|
|
unsigned int skip_ahead_[LOG_ID_MAX];
|
|
|
|
// Used for distinguishing 'dropped' messages for duplicate logs vs chatty drops
|
|
|
|
pid_t last_tid_[LOG_ID_MAX];
|
|
|
|
|
|
|
|
// These next three variables are used for reading only the most recent lines aka `adb logcat
|
|
|
|
// -t` / `adb logcat -T`.
|
|
|
|
// tail_ is the number of most recent lines to print.
|
|
|
|
unsigned long tail_;
|
|
|
|
// count_ is the result of a first pass through the log buffer to determine how many total
|
|
|
|
// messages there are.
|
|
|
|
unsigned long count_;
|
|
|
|
// index_ is used along with count_ to only start sending lines once index_ > (count_ - tail_)
|
|
|
|
// and to disconnect the reader (if it is dumpAndClose, `adb logcat -t`), when index_ >= count_.
|
|
|
|
unsigned long index_;
|
|
|
|
|
|
|
|
// A pointer to the socket for this reader.
|
|
|
|
SocketClient* client_;
|
|
|
|
// When a reader requests logs starting from a given timestamp, its stored here for the first
|
|
|
|
// pass, such that logs before this time stamp that are accumulated in the buffer are ignored.
|
|
|
|
log_time start_time_;
|
|
|
|
// The point from which the reader will read logs once awoken.
|
|
|
|
uint64_t start_;
|
2020-05-11 23:29:29 +00:00
|
|
|
// CLOCK_MONOTONIC based deadline used for log wrapping. If this deadline expires before logs
|
2020-05-05 00:10:16 +00:00
|
|
|
// wrap, then wake up and send the logs to the reader anyway.
|
2020-05-11 23:29:29 +00:00
|
|
|
std::chrono::steady_clock::time_point deadline_;
|
2020-05-05 00:10:16 +00:00
|
|
|
// If this reader is 'dumpAndClose' and will disconnect once it has read its intended logs.
|
|
|
|
const bool non_block_;
|
|
|
|
|
|
|
|
// Whether or not this reader can read logs from all UIDs or only its own UID. See
|
|
|
|
// clientHasLogCredentials().
|
2020-05-04 18:13:55 +00:00
|
|
|
bool privileged_;
|
2020-05-05 00:10:16 +00:00
|
|
|
// Whether or not this reader can read security logs. See CanReadSecurityLogs().
|
2020-05-04 18:13:55 +00:00
|
|
|
bool can_read_security_logs_;
|
2014-02-26 17:50:16 +00:00
|
|
|
};
|