LogBuffer.cpp revision 63c15d50615a4c9e9ad25f601ef9dcb1161d8990
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#include <stdio.h>
18#include <string.h>
19#include <time.h>
20#include <unistd.h>
21
22#include <log/logger.h>
23
24#include "LogBuffer.h"
25#include "LogReader.h"
26
27#define LOG_BUFFER_SIZE (256 * 1024) // Tuned on a per-platform basis here?
28
29LogBuffer::LogBuffer(LastLogTimes *times)
30        : mTimes(*times) {
31    int i;
32    for (i = 0; i < LOG_ID_MAX; i++) {
33        mSizes[i] = 0;
34        mElements[i] = 0;
35    }
36    pthread_mutex_init(&mLogElementsLock, NULL);
37}
38
39void LogBuffer::log(log_id_t log_id, struct timespec realtime,
40                    uid_t uid, pid_t pid, const char *msg,
41                    unsigned short len) {
42    if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
43        return;
44    }
45    LogBufferElement *elem = new LogBufferElement(log_id, realtime,
46                                                  uid, pid, msg, len);
47
48    pthread_mutex_lock(&mLogElementsLock);
49
50    // Insert elements in time sorted order if possible
51    //  NB: if end is region locked, place element at end of list
52    LogBufferElementCollection::iterator it = mLogElements.end();
53    LogBufferElementCollection::iterator last = it;
54    while (--it != mLogElements.begin()) {
55        if ((*it)->getRealTime() <= elem->getRealTime()) {
56            break;
57        }
58        last = it;
59    }
60    if (last == mLogElements.end()) {
61        mLogElements.push_back(elem);
62    } else {
63        log_time end;
64        bool end_set = false;
65        bool end_always = false;
66
67        LogTimeEntry::lock();
68
69        LastLogTimes::iterator t = mTimes.begin();
70        while(t != mTimes.end()) {
71            LogTimeEntry *entry = (*t);
72            if (entry->owned_Locked()) {
73                if (!entry->mNonBlock) {
74                    end_always = true;
75                    break;
76                }
77                if (!end_set || (end <= entry->mEnd)) {
78                    end = entry->mEnd;
79                    end_set = true;
80                }
81            }
82            t++;
83        }
84
85        if (end_always
86         || (end_set && (end >= (*last)->getMonotonicTime()))) {
87            mLogElements.push_back(elem);
88        } else {
89            mLogElements.insert(last,elem);
90        }
91
92        LogTimeEntry::unlock();
93    }
94
95    mSizes[log_id] += len;
96    mElements[log_id]++;
97    maybePrune(log_id);
98    pthread_mutex_unlock(&mLogElementsLock);
99}
100
101// If we're using more than 256K of memory for log entries, prune
102// at least 10% of the log entries.
103//
104// mLogElementsLock must be held when this function is called.
105void LogBuffer::maybePrune(log_id_t id) {
106    unsigned long sizes = mSizes[id];
107    if (sizes > LOG_BUFFER_SIZE) {
108        unsigned long sizeOver90Percent = sizes - ((LOG_BUFFER_SIZE * 9) / 10);
109        unsigned long elements = mElements[id];
110        unsigned long pruneRows = elements * sizeOver90Percent / sizes;
111        elements /= 10;
112        if (pruneRows <= elements) {
113            pruneRows = elements;
114        }
115        prune(id, pruneRows);
116    }
117}
118
119// prune "pruneRows" of type "id" from the buffer.
120//
121// mLogElementsLock must be held when this function is called.
122void LogBuffer::prune(log_id_t id, unsigned long pruneRows) {
123    LogTimeEntry *oldest = NULL;
124
125    LogTimeEntry::lock();
126
127    // Region locked?
128    LastLogTimes::iterator t = mTimes.begin();
129    while(t != mTimes.end()) {
130        LogTimeEntry *entry = (*t);
131        if (entry->owned_Locked()
132                && (!oldest || (oldest->mStart > entry->mStart))) {
133            oldest = entry;
134        }
135        t++;
136    }
137
138    LogBufferElementCollection::iterator it = mLogElements.begin();
139    while((pruneRows > 0) && (it != mLogElements.end())) {
140        LogBufferElement *e = *it;
141        if (e->getLogId() == id) {
142            if (oldest && (oldest->mStart <= e->getMonotonicTime())) {
143                if (mSizes[id] > (2 * LOG_BUFFER_SIZE)) {
144                    // kick a misbehaving log reader client off the island
145                    oldest->release_Locked();
146                } else {
147                    oldest->triggerSkip_Locked(pruneRows);
148                }
149                break;
150            }
151            it = mLogElements.erase(it);
152            mSizes[id] -= e->getMsgLen();
153            mElements[id]--;
154            delete e;
155            pruneRows--;
156        } else {
157            it++;
158        }
159    }
160
161    LogTimeEntry::unlock();
162}
163
164// clear all rows of type "id" from the buffer.
165void LogBuffer::clear(log_id_t id) {
166    pthread_mutex_lock(&mLogElementsLock);
167    prune(id, ULONG_MAX);
168    pthread_mutex_unlock(&mLogElementsLock);
169}
170
171// get the used space associated with "id".
172unsigned long LogBuffer::getSizeUsed(log_id_t id) {
173    pthread_mutex_lock(&mLogElementsLock);
174    unsigned long retval = mSizes[id];
175    pthread_mutex_unlock(&mLogElementsLock);
176    return retval;
177}
178
179// get the total space allocated to "id"
180unsigned long LogBuffer::getSize(log_id_t /*id*/) {
181    return LOG_BUFFER_SIZE;
182}
183
184struct timespec LogBuffer::flushTo(
185        SocketClient *reader, const struct timespec start, bool privileged,
186        bool (*filter)(const LogBufferElement *element, void *arg), void *arg) {
187    LogBufferElementCollection::iterator it;
188    log_time max = start;
189    uid_t uid = reader->getUid();
190
191    pthread_mutex_lock(&mLogElementsLock);
192    for (it = mLogElements.begin(); it != mLogElements.end(); ++it) {
193        LogBufferElement *element = *it;
194
195        if (!privileged && (element->getUid() != uid)) {
196            continue;
197        }
198
199        if (element->getMonotonicTime() <= start) {
200            continue;
201        }
202
203        // NB: calling out to another object with mLogElementsLock held (safe)
204        if (filter && !(*filter)(element, arg)) {
205            continue;
206        }
207
208        pthread_mutex_unlock(&mLogElementsLock);
209
210        // range locking in LastLogTimes looks after us
211        max = element->flushTo(reader);
212
213        if (max == element->FLUSH_ERROR) {
214            return max;
215        }
216
217        pthread_mutex_lock(&mLogElementsLock);
218    }
219    pthread_mutex_unlock(&mLogElementsLock);
220
221    return max;
222}
223