blob: 6f6027213a211b9d3d97b9d5ab765d35b328edef [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 Cherry8f613462020-05-12 12:46:43 -070038#include "SimpleLogBuffer.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 Cherry8f613462020-05-12 12:46:43 -070043class ChattyLogBuffer : public SimpleLogBuffer {
Tom Cherryd5b38382020-05-12 13:16:41 -070044 // watermark of any worst/chatty uid processing
45 typedef std::unordered_map<uid_t, LogBufferElementCollection::iterator> LogBufferIteratorMap;
Tom Cherry0b01ff02020-05-21 10:37:22 -070046 LogBufferIteratorMap mLastWorst[LOG_ID_MAX] GUARDED_BY(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -070047 // watermark of any worst/chatty pid of system processing
48 typedef std::unordered_map<pid_t, LogBufferElementCollection::iterator> LogBufferPidIteratorMap;
Tom Cherry0b01ff02020-05-21 10:37:22 -070049 LogBufferPidIteratorMap mLastWorstPidOfSystem[LOG_ID_MAX] GUARDED_BY(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -070050
Tom Cherryd5b38382020-05-12 13:16:41 -070051 public:
52 ChattyLogBuffer(LogReaderList* reader_list, LogTags* tags, PruneList* prune,
53 LogStatistics* stats);
54 ~ChattyLogBuffer();
Tom Cherryd5b38382020-05-12 13:16:41 -070055
Tom Cherry8f613462020-05-12 12:46:43 -070056 protected:
57 bool Prune(log_id_t id, unsigned long pruneRows, uid_t uid) REQUIRES(lock_) override;
58 void LogInternal(LogBufferElement&& elem) REQUIRES(lock_) override;
Tom Cherrya3c5ff52020-05-21 13:56:33 -070059
Tom Cherryd5b38382020-05-12 13:16:41 -070060 private:
Tom Cherry8f613462020-05-12 12:46:43 -070061 LogBufferElementCollection::iterator Erase(LogBufferElementCollection::iterator it,
Tom Cherry0b01ff02020-05-21 10:37:22 -070062 bool coalesce = false) REQUIRES(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -070063
Tom Cherryd5b38382020-05-12 13:16:41 -070064 PruneList* prune_;
Tom Cherrya3c5ff52020-05-21 13:56:33 -070065
Tom Cherrya26f7df2020-05-19 17:48:42 -070066 // This always contains a copy of the last message logged, for deduplication.
Tom Cherry13224722020-05-19 18:02:00 -070067 std::optional<LogBufferElement> last_logged_elements_[LOG_ID_MAX] GUARDED_BY(lock_);
Tom Cherrya26f7df2020-05-19 17:48:42 -070068 // This contains an element if duplicate messages are seen.
69 // Its `dropped` count is `duplicates seen - 1`.
Tom Cherry13224722020-05-19 18:02:00 -070070 std::optional<LogBufferElement> duplicate_elements_[LOG_ID_MAX] GUARDED_BY(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -070071};