blob: 42cdeec815db48158a5e0165b49fa1f006c007e5 [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 Cherry0b01ff02020-05-21 10:37:22 -070054 LogBufferElement* lastLoggedElements[LOG_ID_MAX] GUARDED_BY(lock_);
55 LogBufferElement* droppedElements[LOG_ID_MAX] GUARDED_BY(lock_);
56 void log(LogBufferElement* elem) REQUIRES(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -070057
58 public:
59 ChattyLogBuffer(LogReaderList* reader_list, LogTags* tags, PruneList* prune,
60 LogStatistics* stats);
61 ~ChattyLogBuffer();
62 void Init() override;
63
64 int Log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid, const char* msg,
65 uint16_t len) override;
66 uint64_t FlushTo(
Tom Cherry283c9a12020-05-14 19:25:05 -070067 LogWriter* writer, uint64_t start, pid_t* lastTid,
Tom Cherryd5b38382020-05-12 13:16:41 -070068 const std::function<FlushToResult(const LogBufferElement* element)>& filter) override;
69
70 bool Clear(log_id_t id, uid_t uid = AID_ROOT) override;
71 unsigned long GetSize(log_id_t id) override;
72 int SetSize(log_id_t id, unsigned long size) override;
73
74 private:
Tom Cherry0b01ff02020-05-21 10:37:22 -070075 void maybePrune(log_id_t id) REQUIRES(lock_);
76 void kickMe(LogReaderThread* me, log_id_t id, unsigned long pruneRows) REQUIRES_SHARED(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -070077
Tom Cherry0b01ff02020-05-21 10:37:22 -070078 bool prune(log_id_t id, unsigned long pruneRows, uid_t uid = AID_ROOT) REQUIRES(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -070079 LogBufferElementCollection::iterator erase(LogBufferElementCollection::iterator it,
Tom Cherry0b01ff02020-05-21 10:37:22 -070080 bool coalesce = false) REQUIRES(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -070081
82 // Returns an iterator to the oldest element for a given log type, or mLogElements.end() if
83 // there are no logs for the given log type. Requires mLogElementsLock to be held.
Tom Cherry0b01ff02020-05-21 10:37:22 -070084 LogBufferElementCollection::iterator GetOldest(log_id_t log_id) REQUIRES(lock_);
Tom Cherryd5b38382020-05-12 13:16:41 -070085
86 LogReaderList* reader_list_;
87 LogTags* tags_;
88 PruneList* prune_;
89 LogStatistics* stats_;
90
91 // Keeps track of the iterator to the oldest log message of a given log type, as an
92 // optimization when pruning logs. Use GetOldest() to retrieve.
93 std::optional<LogBufferElementCollection::iterator> oldest_[LOG_ID_MAX];
Tom Cherry0b01ff02020-05-21 10:37:22 -070094
95 RwLock lock_;
Tom Cherryd5b38382020-05-12 13:16:41 -070096};