LogBuffer.cpp revision 3e76e0a49760c4970b7cda6153e51026af98e4f3
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// 10% of the log entries. 103// 104// mLogElementsLock must be held when this function is called. 105void LogBuffer::maybePrune(log_id_t id) { 106 if (mSizes[id] > LOG_BUFFER_SIZE) { 107 prune(id, mElements[id] / 10); 108 } 109} 110 111// prune "pruneRows" of type "id" from the buffer. 112// 113// mLogElementsLock must be held when this function is called. 114void LogBuffer::prune(log_id_t id, unsigned long pruneRows) { 115 LogTimeEntry *oldest = NULL; 116 117 LogTimeEntry::lock(); 118 119 // Region locked? 120 LastLogTimes::iterator t = mTimes.begin(); 121 while(t != mTimes.end()) { 122 LogTimeEntry *entry = (*t); 123 if (entry->owned_Locked() 124 && (!oldest || (oldest->mStart > entry->mStart))) { 125 oldest = entry; 126 } 127 t++; 128 } 129 130 LogBufferElementCollection::iterator it = mLogElements.begin(); 131 while((pruneRows > 0) && (it != mLogElements.end())) { 132 LogBufferElement *e = *it; 133 if (e->getLogId() == id) { 134 if (oldest && (oldest->mStart <= e->getMonotonicTime())) { 135 if (mSizes[id] > (2 * LOG_BUFFER_SIZE)) { 136 // kick a misbehaving log reader client off the island 137 oldest->release_Locked(); 138 } else { 139 oldest->triggerSkip_Locked(pruneRows); 140 } 141 break; 142 } 143 it = mLogElements.erase(it); 144 mSizes[id] -= e->getMsgLen(); 145 mElements[id]--; 146 delete e; 147 pruneRows--; 148 } else { 149 it++; 150 } 151 } 152 153 LogTimeEntry::unlock(); 154} 155 156// clear all rows of type "id" from the buffer. 157void LogBuffer::clear(log_id_t id) { 158 pthread_mutex_lock(&mLogElementsLock); 159 prune(id, ULONG_MAX); 160 pthread_mutex_unlock(&mLogElementsLock); 161} 162 163// get the used space associated with "id". 164unsigned long LogBuffer::getSizeUsed(log_id_t id) { 165 pthread_mutex_lock(&mLogElementsLock); 166 unsigned long retval = mSizes[id]; 167 pthread_mutex_unlock(&mLogElementsLock); 168 return retval; 169} 170 171// get the total space allocated to "id" 172unsigned long LogBuffer::getSize(log_id_t /*id*/) { 173 return LOG_BUFFER_SIZE; 174} 175 176struct timespec LogBuffer::flushTo( 177 SocketClient *reader, const struct timespec start, bool privileged, 178 bool (*filter)(const LogBufferElement *element, void *arg), void *arg) { 179 LogBufferElementCollection::iterator it; 180 log_time max = start; 181 uid_t uid = reader->getUid(); 182 183 pthread_mutex_lock(&mLogElementsLock); 184 for (it = mLogElements.begin(); it != mLogElements.end(); ++it) { 185 LogBufferElement *element = *it; 186 187 if (!privileged && (element->getUid() != uid)) { 188 continue; 189 } 190 191 if (element->getMonotonicTime() <= start) { 192 continue; 193 } 194 195 // NB: calling out to another object with mLogElementsLock held (safe) 196 if (filter && !(*filter)(element, arg)) { 197 continue; 198 } 199 200 pthread_mutex_unlock(&mLogElementsLock); 201 202 // range locking in LastLogTimes looks after us 203 max = element->flushTo(reader); 204 205 if (max == element->FLUSH_ERROR) { 206 return max; 207 } 208 209 pthread_mutex_lock(&mLogElementsLock); 210 } 211 pthread_mutex_unlock(&mLogElementsLock); 212 213 return max; 214} 215