blob: d307a5e08a030e914062d14784cae2da25cad6ce [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
21#include <list>
22#include <optional>
23#include <string>
24
Tom Cherry0b01ff02020-05-21 10:37:22 -070025#include <android-base/thread_annotations.h>
Tom Cherryd5b38382020-05-12 13:16:41 -070026#include <android/log.h>
27#include <private/android_filesystem_config.h>
28#include <sysutils/SocketClient.h>
29
30#include "LogBuffer.h"
31#include "LogBufferElement.h"
Tom Cherry283c9a12020-05-14 19:25:05 -070032#include "LogReaderList.h"
33#include "LogReaderThread.h"
Tom Cherryd5b38382020-05-12 13:16:41 -070034#include "LogStatistics.h"
35#include "LogTags.h"
36#include "LogWhiteBlackList.h"
Tom Cherry283c9a12020-05-14 19:25:05 -070037#include "LogWriter.h"
Tom Cherry0b01ff02020-05-21 10:37:22 -070038#include "rwlock.h"
Tom Cherryd5b38382020-05-12 13:16:41 -070039
40typedef std::list<LogBufferElement*> LogBufferElementCollection;
41
Tom Cherryd5b38382020-05-12 13:16:41 -070042class ChattyLogBuffer : public LogBuffer {
Tom Cherry0b01ff02020-05-21 10:37:22 -070043 LogBufferElementCollection mLogElements GUARDED_BY(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -070044
45 // watermark of any worst/chatty uid processing
46 typedef std::unordered_map<uid_t, LogBufferElementCollection::iterator> LogBufferIteratorMap;
Tom Cherry0b01ff02020-05-21 10:37:22 -070047 LogBufferIteratorMap mLastWorst[LOG_ID_MAX] GUARDED_BY(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -070048 // watermark of any worst/chatty pid of system processing
49 typedef std::unordered_map<pid_t, LogBufferElementCollection::iterator> LogBufferPidIteratorMap;
Tom Cherry0b01ff02020-05-21 10:37:22 -070050 LogBufferPidIteratorMap mLastWorstPidOfSystem[LOG_ID_MAX] GUARDED_BY(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -070051
Tom Cherry0b01ff02020-05-21 10:37:22 -070052 unsigned long mMaxSize[LOG_ID_MAX] GUARDED_BY(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -070053
Tom Cherryd5b38382020-05-12 13:16:41 -070054 public:
55 ChattyLogBuffer(LogReaderList* reader_list, LogTags* tags, PruneList* prune,
56 LogStatistics* stats);
57 ~ChattyLogBuffer();
58 void Init() override;
59
60 int Log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid, const char* msg,
61 uint16_t len) override;
62 uint64_t FlushTo(
Tom Cherry283c9a12020-05-14 19:25:05 -070063 LogWriter* writer, uint64_t start, pid_t* lastTid,
Tom Cherryd5b38382020-05-12 13:16:41 -070064 const std::function<FlushToResult(const LogBufferElement* element)>& filter) override;
65
66 bool Clear(log_id_t id, uid_t uid = AID_ROOT) override;
67 unsigned long GetSize(log_id_t id) override;
68 int SetSize(log_id_t id, unsigned long size) override;
69
70 private:
Tom Cherry0b01ff02020-05-21 10:37:22 -070071 void maybePrune(log_id_t id) REQUIRES(lock_);
72 void kickMe(LogReaderThread* me, log_id_t id, unsigned long pruneRows) REQUIRES_SHARED(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -070073
Tom Cherry0b01ff02020-05-21 10:37:22 -070074 bool prune(log_id_t id, unsigned long pruneRows, uid_t uid = AID_ROOT) REQUIRES(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -070075 LogBufferElementCollection::iterator erase(LogBufferElementCollection::iterator it,
Tom Cherry0b01ff02020-05-21 10:37:22 -070076 bool coalesce = false) REQUIRES(lock_);
Tom Cherrya26f7df2020-05-19 17:48:42 -070077 bool ShouldLog(log_id_t log_id, const char* msg, uint16_t len);
78 void Log(std::unique_ptr<LogBufferElement> elem) REQUIRES(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -070079
80 // Returns an iterator to the oldest element for a given log type, or mLogElements.end() if
81 // there are no logs for the given log type. Requires mLogElementsLock to be held.
Tom Cherry0b01ff02020-05-21 10:37:22 -070082 LogBufferElementCollection::iterator GetOldest(log_id_t log_id) REQUIRES(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -070083
84 LogReaderList* reader_list_;
85 LogTags* tags_;
86 PruneList* prune_;
87 LogStatistics* stats_;
88
89 // Keeps track of the iterator to the oldest log message of a given log type, as an
90 // optimization when pruning logs. Use GetOldest() to retrieve.
91 std::optional<LogBufferElementCollection::iterator> oldest_[LOG_ID_MAX];
Tom Cherry0b01ff02020-05-21 10:37:22 -070092
93 RwLock lock_;
Tom Cherrya26f7df2020-05-19 17:48:42 -070094
95 // This always contains a copy of the last message logged, for deduplication.
96 std::unique_ptr<LogBufferElement> last_logged_elements_[LOG_ID_MAX] GUARDED_BY(lock_);
97 // This contains an element if duplicate messages are seen.
98 // Its `dropped` count is `duplicates seen - 1`.
99 std::unique_ptr<LogBufferElement> duplicate_elements_[LOG_ID_MAX] GUARDED_BY(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -0700100};