LogBuffer.h revision 0dd4431072cce3c62876b728cb20aa5b77b11a8d
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 "LogTimes.h"
31#include "LogStatistics.h"
32#include "LogWhiteBlackList.h"
33
34//
35// We are either in 1970ish (MONOTONIC) or 2016+ish (REALTIME) so to
36// differentiate without prejudice, we use 1972 to delineate, earlier
37// is likely monotonic, later is real. Otherwise we start using a
38// dividing line between monotonic and realtime if more than a minute
39// difference between them.
40//
41namespace android {
42
43static bool isMonotonic(const log_time &mono) {
44    static const uint32_t EPOCH_PLUS_2_YEARS = 2 * 24 * 60 * 60 * 1461 / 4;
45    static const uint32_t EPOCH_PLUS_MINUTE = 60;
46
47    if (mono.tv_sec >= EPOCH_PLUS_2_YEARS) {
48        return false;
49    }
50
51    log_time now(CLOCK_REALTIME);
52
53    /* Timezone and ntp time setup? */
54    if (now.tv_sec >= EPOCH_PLUS_2_YEARS) {
55        return true;
56    }
57
58    /* no way to differentiate realtime from monotonic time */
59    if (now.tv_sec < EPOCH_PLUS_MINUTE) {
60        return false;
61    }
62
63    log_time cpu(CLOCK_MONOTONIC);
64    /* too close to call to differentiate monotonic times from realtime */
65    if ((cpu.tv_sec + EPOCH_PLUS_MINUTE) >= now.tv_sec) {
66        return false;
67    }
68
69    /* dividing line half way between monotonic and realtime */
70    return mono.tv_sec < ((cpu.tv_sec + now.tv_sec) / 2);
71}
72
73}
74
75typedef std::list<LogBufferElement *> LogBufferElementCollection;
76
77class LogBuffer {
78    LogBufferElementCollection mLogElements;
79    pthread_mutex_t mLogElementsLock;
80
81    LogStatistics stats;
82
83    PruneList mPrune;
84    // watermark for last per log id
85    LogBufferElementCollection::iterator mLast[LOG_ID_MAX];
86    bool mLastSet[LOG_ID_MAX];
87    // watermark of any worst/chatty uid processing
88    typedef std::unordered_map<uid_t,
89                               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,
94                               LogBufferElementCollection::iterator>
95                LogBufferPidIteratorMap;
96    LogBufferPidIteratorMap mLastWorstPidOfSystem[LOG_ID_MAX];
97
98    unsigned long mMaxSize[LOG_ID_MAX];
99
100    bool monotonic;
101
102public:
103    LastLogTimes &mTimes;
104
105    explicit LogBuffer(LastLogTimes *times);
106    void init();
107    bool isMonotonic() { return monotonic; }
108
109    int log(log_id_t log_id, log_time realtime,
110            uid_t uid, pid_t pid, pid_t tid,
111            const char *msg, unsigned short len);
112    uint64_t flushTo(SocketClient *writer, const uint64_t start,
113                     bool privileged, bool security,
114                     int (*filter)(const LogBufferElement *element, void *arg) = NULL,
115                     void *arg = NULL);
116
117    bool clear(log_id_t id, uid_t uid = AID_ROOT);
118    unsigned long getSize(log_id_t id);
119    int setSize(log_id_t id, unsigned long size);
120    unsigned long getSizeUsed(log_id_t id);
121
122    std::string formatStatistics(uid_t uid, pid_t pid, unsigned int logMask);
123
124    void enableStatistics() {
125        stats.enableStatistics();
126    }
127
128    int initPrune(const char *cp) { return mPrune.init(cp); }
129    std::string formatPrune() { return mPrune.format(); }
130
131    // helper must be protected directly or implicitly by lock()/unlock()
132    const char *pidToName(pid_t pid) { return stats.pidToName(pid); }
133    uid_t pidToUid(pid_t pid) { return stats.pidToUid(pid); }
134    const char *uidToName(uid_t uid) { return stats.uidToName(uid); }
135    void lock() { pthread_mutex_lock(&mLogElementsLock); }
136    void unlock() { pthread_mutex_unlock(&mLogElementsLock); }
137
138private:
139
140    static constexpr size_t minPrune = 4;
141    static constexpr size_t maxPrune = 256;
142
143    void maybePrune(log_id_t id);
144    bool prune(log_id_t id, unsigned long pruneRows, uid_t uid = AID_ROOT);
145    LogBufferElementCollection::iterator erase(
146        LogBufferElementCollection::iterator it, bool coalesce = false);
147};
148
149#endif // _LOGD_LOG_BUFFER_H__
150