• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 #ifndef _LOGD_LOG_BUFFER_H__
18 #define _LOGD_LOG_BUFFER_H__
19 
20 #include <sys/types.h>
21 
22 #include <list>
23 #include <string>
24 
25 #include <android/log.h>
26 #include <private/android_filesystem_config.h>
27 #include <sysutils/SocketClient.h>
28 
29 #include "LogBufferElement.h"
30 #include "LogBufferInterface.h"
31 #include "LogStatistics.h"
32 #include "LogTags.h"
33 #include "LogTimes.h"
34 #include "LogWhiteBlackList.h"
35 
36 //
37 // We are either in 1970ish (MONOTONIC) or 2016+ish (REALTIME) so to
38 // differentiate without prejudice, we use 1972 to delineate, earlier
39 // is likely monotonic, later is real. Otherwise we start using a
40 // dividing line between monotonic and realtime if more than a minute
41 // difference between them.
42 //
43 namespace android {
44 
isMonotonic(const log_time & mono)45 static bool isMonotonic(const log_time& mono) {
46     static const uint32_t EPOCH_PLUS_2_YEARS = 2 * 24 * 60 * 60 * 1461 / 4;
47     static const uint32_t EPOCH_PLUS_MINUTE = 60;
48 
49     if (mono.tv_sec >= EPOCH_PLUS_2_YEARS) {
50         return false;
51     }
52 
53     log_time now(CLOCK_REALTIME);
54 
55     /* Timezone and ntp time setup? */
56     if (now.tv_sec >= EPOCH_PLUS_2_YEARS) {
57         return true;
58     }
59 
60     /* no way to differentiate realtime from monotonic time */
61     if (now.tv_sec < EPOCH_PLUS_MINUTE) {
62         return false;
63     }
64 
65     log_time cpu(CLOCK_MONOTONIC);
66     /* too close to call to differentiate monotonic times from realtime */
67     if ((cpu.tv_sec + EPOCH_PLUS_MINUTE) >= now.tv_sec) {
68         return false;
69     }
70 
71     /* dividing line half way between monotonic and realtime */
72     return mono.tv_sec < ((cpu.tv_sec + now.tv_sec) / 2);
73 }
74 }
75 
76 typedef std::list<LogBufferElement*> LogBufferElementCollection;
77 
78 class LogBuffer : public LogBufferInterface {
79     LogBufferElementCollection mLogElements;
80     pthread_rwlock_t mLogElementsLock;
81 
82     LogStatistics stats;
83 
84     PruneList mPrune;
85     // watermark for last per log id
86     LogBufferElementCollection::iterator mLast[LOG_ID_MAX];
87     bool mLastSet[LOG_ID_MAX];
88     // watermark of any worst/chatty uid processing
89     typedef std::unordered_map<uid_t, LogBufferElementCollection::iterator>
90         LogBufferIteratorMap;
91     LogBufferIteratorMap mLastWorst[LOG_ID_MAX];
92     // watermark of any worst/chatty pid of system processing
93     typedef std::unordered_map<pid_t, LogBufferElementCollection::iterator>
94         LogBufferPidIteratorMap;
95     LogBufferPidIteratorMap mLastWorstPidOfSystem[LOG_ID_MAX];
96 
97     unsigned long mMaxSize[LOG_ID_MAX];
98 
99     bool monotonic;
100 
101     LogTags tags;
102 
103     LogBufferElement* lastLoggedElements[LOG_ID_MAX];
104     LogBufferElement* droppedElements[LOG_ID_MAX];
105     void log(LogBufferElement* elem);
106 
107    public:
108     LastLogTimes& mTimes;
109 
110     explicit LogBuffer(LastLogTimes* times);
111     ~LogBuffer() override;
112     void init();
isMonotonic()113     bool isMonotonic() {
114         return monotonic;
115     }
116 
117     int log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid,
118             const char* msg, uint16_t len) override;
119     // lastTid is an optional context to help detect if the last previous
120     // valid message was from the same source so we can differentiate chatty
121     // filter types (identical or expired)
122     log_time flushTo(SocketClient* writer, const log_time& start,
123                      pid_t* lastTid,  // &lastTid[LOG_ID_MAX] or nullptr
124                      bool privileged, bool security,
125                      int (*filter)(const LogBufferElement* element,
126                                    void* arg) = nullptr,
127                      void* arg = nullptr);
128 
129     bool clear(log_id_t id, uid_t uid = AID_ROOT);
130     unsigned long getSize(log_id_t id);
131     int setSize(log_id_t id, unsigned long size);
132     unsigned long getSizeUsed(log_id_t id);
133 
134     std::string formatStatistics(uid_t uid, pid_t pid, unsigned int logMask);
135 
enableStatistics()136     void enableStatistics() {
137         stats.enableStatistics();
138     }
139 
initPrune(const char * cp)140     int initPrune(const char* cp) {
141         return mPrune.init(cp);
142     }
formatPrune()143     std::string formatPrune() {
144         return mPrune.format();
145     }
146 
formatGetEventTag(uid_t uid,const char * name,const char * format)147     std::string formatGetEventTag(uid_t uid, const char* name,
148                                   const char* format) {
149         return tags.formatGetEventTag(uid, name, format);
150     }
formatEntry(uint32_t tag,uid_t uid)151     std::string formatEntry(uint32_t tag, uid_t uid) {
152         return tags.formatEntry(tag, uid);
153     }
tagToName(uint32_t tag)154     const char* tagToName(uint32_t tag) {
155         return tags.tagToName(tag);
156     }
157 
158     // helper must be protected directly or implicitly by wrlock()/unlock()
pidToName(pid_t pid)159     const char* pidToName(pid_t pid) {
160         return stats.pidToName(pid);
161     }
pidToUid(pid_t pid)162     virtual uid_t pidToUid(pid_t pid) override {
163         return stats.pidToUid(pid);
164     }
tidToPid(pid_t tid)165     virtual pid_t tidToPid(pid_t tid) override {
166         return stats.tidToPid(tid);
167     }
uidToName(uid_t uid)168     const char* uidToName(uid_t uid) {
169         return stats.uidToName(uid);
170     }
wrlock()171     void wrlock() {
172         pthread_rwlock_wrlock(&mLogElementsLock);
173     }
rdlock()174     void rdlock() {
175         pthread_rwlock_rdlock(&mLogElementsLock);
176     }
unlock()177     void unlock() {
178         pthread_rwlock_unlock(&mLogElementsLock);
179     }
180 
181    private:
182     static constexpr size_t minPrune = 4;
183     static constexpr size_t maxPrune = 256;
184     static const log_time pruneMargin;
185 
186     void maybePrune(log_id_t id);
187     bool isBusy(log_time watermark);
188     void kickMe(LogTimeEntry* me, log_id_t id, unsigned long pruneRows);
189 
190     bool prune(log_id_t id, unsigned long pruneRows, uid_t uid = AID_ROOT);
191     LogBufferElementCollection::iterator erase(
192         LogBufferElementCollection::iterator it, bool coalesce = false);
193 };
194 
195 #endif  // _LOGD_LOG_BUFFER_H__
196