blob: b4d3a2ff3248c5c4fd44d3695722d3c7ae55281e [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>
Tom Cherryd5b38382020-05-12 13:16:41 -070028
29#include "LogBuffer.h"
30#include "LogBufferElement.h"
Tom Cherry283c9a12020-05-14 19:25:05 -070031#include "LogReaderList.h"
32#include "LogReaderThread.h"
Tom Cherryd5b38382020-05-12 13:16:41 -070033#include "LogStatistics.h"
34#include "LogTags.h"
Tom Cherry283c9a12020-05-14 19:25:05 -070035#include "LogWriter.h"
Tom Cherry0efb4102020-06-16 10:14:09 -070036#include "PruneList.h"
Tom Cherry8f613462020-05-12 12:46:43 -070037#include "SimpleLogBuffer.h"
Tom Cherry0b01ff02020-05-21 10:37:22 -070038#include "rwlock.h"
Tom Cherryd5b38382020-05-12 13:16:41 -070039
Tom Cherry13224722020-05-19 18:02:00 -070040typedef std::list<LogBufferElement> LogBufferElementCollection;
Tom Cherryd5b38382020-05-12 13:16:41 -070041
Tom Cherry8f613462020-05-12 12:46:43 -070042class ChattyLogBuffer : public SimpleLogBuffer {
Tom Cherryd5b38382020-05-12 13:16:41 -070043 // watermark of any worst/chatty uid processing
44 typedef std::unordered_map<uid_t, LogBufferElementCollection::iterator> LogBufferIteratorMap;
Tom Cherry0b01ff02020-05-21 10:37:22 -070045 LogBufferIteratorMap mLastWorst[LOG_ID_MAX] GUARDED_BY(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -070046 // watermark of any worst/chatty pid of system processing
47 typedef std::unordered_map<pid_t, LogBufferElementCollection::iterator> LogBufferPidIteratorMap;
Tom Cherry0b01ff02020-05-21 10:37:22 -070048 LogBufferPidIteratorMap mLastWorstPidOfSystem[LOG_ID_MAX] GUARDED_BY(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -070049
Tom Cherryd5b38382020-05-12 13:16:41 -070050 public:
51 ChattyLogBuffer(LogReaderList* reader_list, LogTags* tags, PruneList* prune,
52 LogStatistics* stats);
53 ~ChattyLogBuffer();
Tom Cherryd5b38382020-05-12 13:16:41 -070054
Tom Cherry8f613462020-05-12 12:46:43 -070055 protected:
56 bool Prune(log_id_t id, unsigned long pruneRows, uid_t uid) REQUIRES(lock_) override;
57 void LogInternal(LogBufferElement&& elem) REQUIRES(lock_) override;
Tom Cherrya3c5ff52020-05-21 13:56:33 -070058
Tom Cherryd5b38382020-05-12 13:16:41 -070059 private:
Tom Cherry8f613462020-05-12 12:46:43 -070060 LogBufferElementCollection::iterator Erase(LogBufferElementCollection::iterator it,
Tom Cherry0b01ff02020-05-21 10:37:22 -070061 bool coalesce = false) REQUIRES(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -070062
Tom Cherryd5b38382020-05-12 13:16:41 -070063 PruneList* prune_;
Tom Cherrya3c5ff52020-05-21 13:56:33 -070064
Tom Cherrya26f7df2020-05-19 17:48:42 -070065 // This always contains a copy of the last message logged, for deduplication.
Tom Cherry13224722020-05-19 18:02:00 -070066 std::optional<LogBufferElement> last_logged_elements_[LOG_ID_MAX] GUARDED_BY(lock_);
Tom Cherrya26f7df2020-05-19 17:48:42 -070067 // This contains an element if duplicate messages are seen.
68 // Its `dropped` count is `duplicates seen - 1`.
Tom Cherry13224722020-05-19 18:02:00 -070069 std::optional<LogBufferElement> duplicate_elements_[LOG_ID_MAX] GUARDED_BY(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -070070};