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