blob: 257c8b3a61c70ce778a06fec777b904d01493f9b [file] [log] [blame]
Tom Cherryd5b38382020-05-12 13:16:41 -07001/*
2 * Copyright (C) 2012-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
17#pragma once
18
19#include <sys/types.h>
20
Tom Cherrya3c5ff52020-05-21 13:56:33 -070021#include <atomic>
Tom Cherryd5b38382020-05-12 13:16:41 -070022#include <list>
23#include <optional>
24#include <string>
25
Tom Cherry0b01ff02020-05-21 10:37:22 -070026#include <android-base/thread_annotations.h>
Tom Cherryd5b38382020-05-12 13:16:41 -070027#include <android/log.h>
28#include <private/android_filesystem_config.h>
29#include <sysutils/SocketClient.h>
30
31#include "LogBuffer.h"
32#include "LogBufferElement.h"
Tom Cherry283c9a12020-05-14 19:25:05 -070033#include "LogReaderList.h"
34#include "LogReaderThread.h"
Tom Cherryd5b38382020-05-12 13:16:41 -070035#include "LogStatistics.h"
36#include "LogTags.h"
37#include "LogWhiteBlackList.h"
Tom Cherry283c9a12020-05-14 19:25:05 -070038#include "LogWriter.h"
Tom Cherry0b01ff02020-05-21 10:37:22 -070039#include "rwlock.h"
Tom Cherryd5b38382020-05-12 13:16:41 -070040
Tom Cherry13224722020-05-19 18:02:00 -070041typedef std::list<LogBufferElement> LogBufferElementCollection;
Tom Cherryd5b38382020-05-12 13:16:41 -070042
Tom Cherryd5b38382020-05-12 13:16:41 -070043class ChattyLogBuffer : public LogBuffer {
Tom Cherry0b01ff02020-05-21 10:37:22 -070044 LogBufferElementCollection mLogElements GUARDED_BY(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -070045
46 // watermark of any worst/chatty uid processing
47 typedef std::unordered_map<uid_t, LogBufferElementCollection::iterator> LogBufferIteratorMap;
Tom Cherry0b01ff02020-05-21 10:37:22 -070048 LogBufferIteratorMap mLastWorst[LOG_ID_MAX] GUARDED_BY(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -070049 // watermark of any worst/chatty pid of system processing
50 typedef std::unordered_map<pid_t, LogBufferElementCollection::iterator> LogBufferPidIteratorMap;
Tom Cherry0b01ff02020-05-21 10:37:22 -070051 LogBufferPidIteratorMap mLastWorstPidOfSystem[LOG_ID_MAX] GUARDED_BY(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -070052
Tom Cherry0b01ff02020-05-21 10:37:22 -070053 unsigned long mMaxSize[LOG_ID_MAX] GUARDED_BY(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -070054
Tom Cherryd5b38382020-05-12 13:16:41 -070055 public:
56 ChattyLogBuffer(LogReaderList* reader_list, LogTags* tags, PruneList* prune,
57 LogStatistics* stats);
58 ~ChattyLogBuffer();
59 void Init() override;
60
61 int Log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid, const char* msg,
62 uint16_t len) override;
63 uint64_t FlushTo(
Tom Cherry283c9a12020-05-14 19:25:05 -070064 LogWriter* writer, uint64_t start, pid_t* lastTid,
Tom Cherryd5b38382020-05-12 13:16:41 -070065 const std::function<FlushToResult(const LogBufferElement* element)>& filter) override;
66
67 bool Clear(log_id_t id, uid_t uid = AID_ROOT) override;
68 unsigned long GetSize(log_id_t id) override;
69 int SetSize(log_id_t id, unsigned long size) override;
70
Tom Cherrya3c5ff52020-05-21 13:56:33 -070071 uint64_t sequence() const override { return sequence_.load(std::memory_order_relaxed); }
72
Tom Cherryd5b38382020-05-12 13:16:41 -070073 private:
Tom Cherry0b01ff02020-05-21 10:37:22 -070074 void maybePrune(log_id_t id) REQUIRES(lock_);
75 void kickMe(LogReaderThread* me, log_id_t id, unsigned long pruneRows) REQUIRES_SHARED(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -070076
Tom Cherry0b01ff02020-05-21 10:37:22 -070077 bool prune(log_id_t id, unsigned long pruneRows, uid_t uid = AID_ROOT) REQUIRES(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -070078 LogBufferElementCollection::iterator erase(LogBufferElementCollection::iterator it,
Tom Cherry0b01ff02020-05-21 10:37:22 -070079 bool coalesce = false) REQUIRES(lock_);
Tom Cherrya26f7df2020-05-19 17:48:42 -070080 bool ShouldLog(log_id_t log_id, const char* msg, uint16_t len);
Tom Cherry13224722020-05-19 18:02:00 -070081 void Log(LogBufferElement&& elem) REQUIRES(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -070082
83 // Returns an iterator to the oldest element for a given log type, or mLogElements.end() if
84 // there are no logs for the given log type. Requires mLogElementsLock to be held.
Tom Cherry0b01ff02020-05-21 10:37:22 -070085 LogBufferElementCollection::iterator GetOldest(log_id_t log_id) REQUIRES(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -070086
87 LogReaderList* reader_list_;
88 LogTags* tags_;
89 PruneList* prune_;
90 LogStatistics* stats_;
91
92 // Keeps track of the iterator to the oldest log message of a given log type, as an
93 // optimization when pruning logs. Use GetOldest() to retrieve.
94 std::optional<LogBufferElementCollection::iterator> oldest_[LOG_ID_MAX];
Tom Cherry0b01ff02020-05-21 10:37:22 -070095
96 RwLock lock_;
Tom Cherrya26f7df2020-05-19 17:48:42 -070097
Tom Cherrya3c5ff52020-05-21 13:56:33 -070098 std::atomic<uint64_t> sequence_ = 1;
99
Tom Cherrya26f7df2020-05-19 17:48:42 -0700100 // This always contains a copy of the last message logged, for deduplication.
Tom Cherry13224722020-05-19 18:02:00 -0700101 std::optional<LogBufferElement> last_logged_elements_[LOG_ID_MAX] GUARDED_BY(lock_);
Tom Cherrya26f7df2020-05-19 17:48:42 -0700102 // This contains an element if duplicate messages are seen.
103 // Its `dropped` count is `duplicates seen - 1`.
Tom Cherry13224722020-05-19 18:02:00 -0700104 std::optional<LogBufferElement> duplicate_elements_[LOG_ID_MAX] GUARDED_BY(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -0700105};