Mark Salyzyn | 0175b07 | 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 | |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 17 | #include <ctype.h> |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 18 | #include <errno.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <string.h> |
Mark Salyzyn | 8fcfd85 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 21 | #include <sys/cdefs.h> |
Mark Salyzyn | 57a0af9 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 22 | #include <sys/user.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 23 | #include <time.h> |
| 24 | #include <unistd.h> |
| 25 | |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 26 | #include <unordered_map> |
| 27 | |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 28 | #include <cutils/properties.h> |
Mark Salyzyn | f10e273 | 2016-09-27 13:08:23 -0700 | [diff] [blame] | 29 | #include <private/android_logger.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 30 | |
| 31 | #include "LogBuffer.h" |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 32 | #include "LogKlog.h" |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 33 | #include "LogReader.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 34 | |
Mark Salyzyn | 8fcfd85 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 35 | #ifndef __predict_false |
| 36 | #define __predict_false(exp) __builtin_expect((exp) != 0, 0) |
| 37 | #endif |
| 38 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 39 | // Default |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 40 | #define log_buffer_size(id) mMaxSize[id] |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 41 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 42 | void LogBuffer::init() { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 43 | log_id_for_each(i) { |
Mark Salyzyn | 507eb9f | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 44 | mLastSet[i] = false; |
| 45 | mLast[i] = mLogElements.begin(); |
| 46 | |
Mark Salyzyn | f10e273 | 2016-09-27 13:08:23 -0700 | [diff] [blame] | 47 | if (setSize(i, __android_logger_get_buffer_size(i))) { |
Mark Salyzyn | 57a0af9 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 48 | setSize(i, LOG_BUFFER_MIN_SIZE); |
| 49 | } |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 50 | } |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 51 | bool lastMonotonic = monotonic; |
Mark Salyzyn | ba7a9a0 | 2015-12-01 15:57:25 -0800 | [diff] [blame] | 52 | monotonic = android_log_clockid() == CLOCK_MONOTONIC; |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 53 | if (lastMonotonic != monotonic) { |
| 54 | // |
| 55 | // Fixup all timestamps, may not be 100% accurate, but better than |
| 56 | // throwing what we have away when we get 'surprised' by a change. |
| 57 | // In-place element fixup so no need to check reader-lock. Entries |
| 58 | // should already be in timestamp order, but we could end up with a |
| 59 | // few out-of-order entries if new monotonics come in before we |
| 60 | // are notified of the reinit change in status. A Typical example would |
| 61 | // be: |
| 62 | // --------- beginning of system |
| 63 | // 10.494082 184 201 D Cryptfs : Just triggered post_fs_data |
| 64 | // --------- beginning of kernel |
| 65 | // 0.000000 0 0 I : Initializing cgroup subsys |
| 66 | // as the act of mounting /data would trigger persist.logd.timestamp to |
| 67 | // be corrected. 1/30 corner case YMMV. |
| 68 | // |
| 69 | pthread_mutex_lock(&mLogElementsLock); |
| 70 | LogBufferElementCollection::iterator it = mLogElements.begin(); |
| 71 | while((it != mLogElements.end())) { |
| 72 | LogBufferElement *e = *it; |
| 73 | if (monotonic) { |
| 74 | if (!android::isMonotonic(e->mRealTime)) { |
| 75 | LogKlog::convertRealToMonotonic(e->mRealTime); |
| 76 | } |
| 77 | } else { |
| 78 | if (android::isMonotonic(e->mRealTime)) { |
| 79 | LogKlog::convertMonotonicToReal(e->mRealTime); |
| 80 | } |
| 81 | } |
| 82 | ++it; |
| 83 | } |
| 84 | pthread_mutex_unlock(&mLogElementsLock); |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 87 | // We may have been triggered by a SIGHUP. Release any sleeping reader |
| 88 | // threads to dump their current content. |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 89 | // |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 90 | // NB: this is _not_ performed in the context of a SIGHUP, it is |
| 91 | // performed during startup, and in context of reinit administrative thread |
| 92 | LogTimeEntry::lock(); |
| 93 | |
| 94 | LastLogTimes::iterator times = mTimes.begin(); |
| 95 | while(times != mTimes.end()) { |
| 96 | LogTimeEntry *entry = (*times); |
| 97 | if (entry->owned_Locked()) { |
| 98 | entry->triggerReader_Locked(); |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 99 | } |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 100 | times++; |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 101 | } |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 102 | |
| 103 | LogTimeEntry::unlock(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 106 | LogBuffer::LogBuffer(LastLogTimes *times): |
Mark Salyzyn | ba7a9a0 | 2015-12-01 15:57:25 -0800 | [diff] [blame] | 107 | monotonic(android_log_clockid() == CLOCK_MONOTONIC), |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 108 | mTimes(*times) { |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 109 | pthread_mutex_init(&mLogElementsLock, NULL); |
| 110 | |
| 111 | init(); |
| 112 | } |
| 113 | |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 114 | int LogBuffer::log(log_id_t log_id, log_time realtime, |
| 115 | uid_t uid, pid_t pid, pid_t tid, |
| 116 | const char *msg, unsigned short len) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 117 | if ((log_id >= LOG_ID_MAX) || (log_id < 0)) { |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 118 | return -EINVAL; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 119 | } |
Mark Salyzyn | e59c469 | 2014-10-02 13:07:05 -0700 | [diff] [blame] | 120 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 121 | LogBufferElement *elem = new LogBufferElement(log_id, realtime, |
Mark Salyzyn | b992d0d | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 122 | uid, pid, tid, msg, len); |
Mark Salyzyn | 0ee8de3 | 2016-01-06 21:17:43 +0000 | [diff] [blame] | 123 | if (log_id != LOG_ID_SECURITY) { |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 124 | int prio = ANDROID_LOG_INFO; |
Mark Salyzyn | 0ee8de3 | 2016-01-06 21:17:43 +0000 | [diff] [blame] | 125 | const char *tag = NULL; |
Mark Salyzyn | 807e40e | 2016-09-22 09:56:51 -0700 | [diff] [blame] | 126 | size_t len = 0; |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 127 | if (log_id == LOG_ID_EVENTS) { |
Mark Salyzyn | 807e40e | 2016-09-22 09:56:51 -0700 | [diff] [blame] | 128 | tag = android::tagToName(&len, elem->getTag()); |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 129 | } else { |
| 130 | prio = *msg; |
| 131 | tag = msg + 1; |
Mark Salyzyn | 807e40e | 2016-09-22 09:56:51 -0700 | [diff] [blame] | 132 | len = strlen(tag); |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 133 | } |
Mark Salyzyn | 807e40e | 2016-09-22 09:56:51 -0700 | [diff] [blame] | 134 | if (!__android_log_is_loggable_len(prio, tag, len, ANDROID_LOG_VERBOSE)) { |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 135 | // Log traffic received to total |
| 136 | pthread_mutex_lock(&mLogElementsLock); |
| 137 | stats.add(elem); |
| 138 | stats.subtract(elem); |
| 139 | pthread_mutex_unlock(&mLogElementsLock); |
| 140 | delete elem; |
| 141 | return -EACCES; |
| 142 | } |
Mark Salyzyn | e59c469 | 2014-10-02 13:07:05 -0700 | [diff] [blame] | 143 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 144 | |
| 145 | pthread_mutex_lock(&mLogElementsLock); |
| 146 | |
| 147 | // Insert elements in time sorted order if possible |
| 148 | // NB: if end is region locked, place element at end of list |
| 149 | LogBufferElementCollection::iterator it = mLogElements.end(); |
| 150 | LogBufferElementCollection::iterator last = it; |
Mark Salyzyn | eae155e | 2014-10-13 16:49:47 -0700 | [diff] [blame] | 151 | while (last != mLogElements.begin()) { |
| 152 | --it; |
Mark Salyzyn | c03e72c | 2014-02-18 11:23:53 -0800 | [diff] [blame] | 153 | if ((*it)->getRealTime() <= realtime) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 154 | break; |
| 155 | } |
| 156 | last = it; |
| 157 | } |
Mark Salyzyn | c03e72c | 2014-02-18 11:23:53 -0800 | [diff] [blame] | 158 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 159 | if (last == mLogElements.end()) { |
| 160 | mLogElements.push_back(elem); |
| 161 | } else { |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 162 | uint64_t end = 1; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 163 | bool end_set = false; |
| 164 | bool end_always = false; |
| 165 | |
| 166 | LogTimeEntry::lock(); |
| 167 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 168 | LastLogTimes::iterator times = mTimes.begin(); |
| 169 | while(times != mTimes.end()) { |
| 170 | LogTimeEntry *entry = (*times); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 171 | if (entry->owned_Locked()) { |
| 172 | if (!entry->mNonBlock) { |
| 173 | end_always = true; |
| 174 | break; |
| 175 | } |
| 176 | if (!end_set || (end <= entry->mEnd)) { |
| 177 | end = entry->mEnd; |
| 178 | end_set = true; |
| 179 | } |
| 180 | } |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 181 | times++; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | if (end_always |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 185 | || (end_set && (end >= (*last)->getSequence()))) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 186 | mLogElements.push_back(elem); |
| 187 | } else { |
| 188 | mLogElements.insert(last,elem); |
| 189 | } |
| 190 | |
| 191 | LogTimeEntry::unlock(); |
| 192 | } |
| 193 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 194 | stats.add(elem); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 195 | maybePrune(log_id); |
| 196 | pthread_mutex_unlock(&mLogElementsLock); |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 197 | |
| 198 | return len; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 201 | // Prune at most 10% of the log entries or maxPrune, whichever is less. |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 202 | // |
| 203 | // mLogElementsLock must be held when this function is called. |
| 204 | void LogBuffer::maybePrune(log_id_t id) { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 205 | size_t sizes = stats.sizes(id); |
Mark Salyzyn | 62ab0fd | 2015-08-10 10:23:56 -0700 | [diff] [blame] | 206 | unsigned long maxSize = log_buffer_size(id); |
| 207 | if (sizes > maxSize) { |
Mark Salyzyn | b39ed0c | 2015-08-19 12:20:36 -0700 | [diff] [blame] | 208 | size_t sizeOver = sizes - ((maxSize * 9) / 10); |
Mark Salyzyn | 58b8be8 | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 209 | size_t elements = stats.realElements(id); |
| 210 | size_t minElements = elements / 100; |
| 211 | if (minElements < minPrune) { |
| 212 | minElements = minPrune; |
| 213 | } |
Mark Salyzyn | 62ab0fd | 2015-08-10 10:23:56 -0700 | [diff] [blame] | 214 | unsigned long pruneRows = elements * sizeOver / sizes; |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 215 | if (pruneRows < minElements) { |
Mark Salyzyn | 62ab0fd | 2015-08-10 10:23:56 -0700 | [diff] [blame] | 216 | pruneRows = minElements; |
Mark Salyzyn | 740f9b4 | 2014-01-13 16:37:51 -0800 | [diff] [blame] | 217 | } |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 218 | if (pruneRows > maxPrune) { |
| 219 | pruneRows = maxPrune; |
Mark Salyzyn | b39ed0c | 2015-08-19 12:20:36 -0700 | [diff] [blame] | 220 | } |
Mark Salyzyn | 740f9b4 | 2014-01-13 16:37:51 -0800 | [diff] [blame] | 221 | prune(id, pruneRows); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Mark Salyzyn | 831aa29 | 2015-09-03 16:08:50 -0700 | [diff] [blame] | 225 | LogBufferElementCollection::iterator LogBuffer::erase( |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 226 | LogBufferElementCollection::iterator it, bool coalesce) { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 227 | LogBufferElement *element = *it; |
| 228 | log_id_t id = element->getLogId(); |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 229 | |
Mark Salyzyn | fa07f9d | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 230 | // Remove iterator references in the various lists that will become stale |
| 231 | // after the element is erased from the main logging list. |
| 232 | |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 233 | { // start of scope for found iterator |
| 234 | int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) ? |
| 235 | element->getTag() : element->getUid(); |
| 236 | LogBufferIteratorMap::iterator found = mLastWorst[id].find(key); |
| 237 | if ((found != mLastWorst[id].end()) && (it == found->second)) { |
| 238 | mLastWorst[id].erase(found); |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 239 | } |
Mark Salyzyn | c892ea3 | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 240 | } |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 241 | |
Mark Salyzyn | 8fcfd85 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 242 | { // start of scope for pid found iterator |
Mark Salyzyn | fa07f9d | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 243 | // element->getUid() may not be AID_SYSTEM for next-best-watermark. |
Mark Salyzyn | 8fcfd85 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 244 | // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and |
| 245 | // long term code stability, find() check should be fast for those ids. |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 246 | LogBufferPidIteratorMap::iterator found = |
| 247 | mLastWorstPidOfSystem[id].find(element->getPid()); |
| 248 | if ((found != mLastWorstPidOfSystem[id].end()) |
| 249 | && (it == found->second)) { |
| 250 | mLastWorstPidOfSystem[id].erase(found); |
| 251 | } |
| 252 | } |
| 253 | |
Mark Salyzyn | 7fd6c5c | 2016-01-19 16:04:41 -0800 | [diff] [blame] | 254 | bool setLast[LOG_ID_MAX]; |
| 255 | bool doSetLast = false; |
| 256 | log_id_for_each(i) { |
| 257 | doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]); |
| 258 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 259 | it = mLogElements.erase(it); |
Mark Salyzyn | 7fd6c5c | 2016-01-19 16:04:41 -0800 | [diff] [blame] | 260 | if (doSetLast) { |
| 261 | log_id_for_each(i) { |
| 262 | if (setLast[i]) { |
Mark Salyzyn | 8fcfd85 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 263 | if (__predict_false(it == mLogElements.end())) { // impossible |
Mark Salyzyn | 7fd6c5c | 2016-01-19 16:04:41 -0800 | [diff] [blame] | 264 | mLastSet[i] = false; |
Mark Salyzyn | 8fcfd85 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 265 | mLast[i] = mLogElements.begin(); |
Mark Salyzyn | 7fd6c5c | 2016-01-19 16:04:41 -0800 | [diff] [blame] | 266 | } else { |
Mark Salyzyn | 8fcfd85 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 267 | mLast[i] = it; // push down the road as next-best-watermark |
Mark Salyzyn | 7fd6c5c | 2016-01-19 16:04:41 -0800 | [diff] [blame] | 268 | } |
| 269 | } |
Mark Salyzyn | 507eb9f | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 270 | } |
| 271 | } |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 272 | if (coalesce) { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 273 | stats.erase(element); |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 274 | } else { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 275 | stats.subtract(element); |
Mark Salyzyn | 831aa29 | 2015-09-03 16:08:50 -0700 | [diff] [blame] | 276 | } |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 277 | delete element; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 278 | |
| 279 | return it; |
| 280 | } |
| 281 | |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 282 | // Define a temporary mechanism to report the last LogBufferElement pointer |
| 283 | // for the specified uid, pid and tid. Used below to help merge-sort when |
| 284 | // pruning for worst UID. |
| 285 | class LogBufferElementKey { |
| 286 | const union { |
| 287 | struct { |
| 288 | uint16_t uid; |
| 289 | uint16_t pid; |
| 290 | uint16_t tid; |
| 291 | uint16_t padding; |
| 292 | } __packed; |
| 293 | uint64_t value; |
| 294 | } __packed; |
| 295 | |
| 296 | public: |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 297 | LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid): |
| 298 | uid(uid), |
| 299 | pid(pid), |
| 300 | tid(tid), |
| 301 | padding(0) { |
| 302 | } |
Chih-Hung Hsieh | 1cc82ce | 2016-04-25 13:49:46 -0700 | [diff] [blame] | 303 | explicit LogBufferElementKey(uint64_t key):value(key) { } |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 304 | |
| 305 | uint64_t getKey() { return value; } |
| 306 | }; |
| 307 | |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 308 | class LogBufferElementLast { |
| 309 | |
| 310 | typedef std::unordered_map<uint64_t, LogBufferElement *> LogBufferElementMap; |
| 311 | LogBufferElementMap map; |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 312 | |
| 313 | public: |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 314 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 315 | bool coalesce(LogBufferElement *element, unsigned short dropped) { |
| 316 | LogBufferElementKey key(element->getUid(), |
| 317 | element->getPid(), |
| 318 | element->getTid()); |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 319 | LogBufferElementMap::iterator it = map.find(key.getKey()); |
| 320 | if (it != map.end()) { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 321 | LogBufferElement *found = it->second; |
| 322 | unsigned short moreDropped = found->getDropped(); |
| 323 | if ((dropped + moreDropped) > USHRT_MAX) { |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 324 | map.erase(it); |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 325 | } else { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 326 | found->setDropped(dropped + moreDropped); |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 327 | return true; |
| 328 | } |
| 329 | } |
| 330 | return false; |
| 331 | } |
| 332 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 333 | void add(LogBufferElement *element) { |
| 334 | LogBufferElementKey key(element->getUid(), |
| 335 | element->getPid(), |
| 336 | element->getTid()); |
| 337 | map[key.getKey()] = element; |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Mark Salyzyn | e06a6e0 | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 340 | inline void clear() { |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 341 | map.clear(); |
Mark Salyzyn | e06a6e0 | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 344 | void clear(LogBufferElement *element) { |
| 345 | uint64_t current = element->getRealTime().nsec() |
Mark Salyzyn | 047cc07 | 2015-06-04 13:35:30 -0700 | [diff] [blame] | 346 | - (EXPIRE_RATELIMIT * NS_PER_SEC); |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 347 | for(LogBufferElementMap::iterator it = map.begin(); it != map.end();) { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 348 | LogBufferElement *mapElement = it->second; |
| 349 | if ((mapElement->getDropped() >= EXPIRE_THRESHOLD) |
| 350 | && (current > mapElement->getRealTime().nsec())) { |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 351 | it = map.erase(it); |
| 352 | } else { |
| 353 | ++it; |
Mark Salyzyn | e06a6e0 | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 358 | }; |
| 359 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 360 | // prune "pruneRows" of type "id" from the buffer. |
| 361 | // |
Mark Salyzyn | 5bb2972 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 362 | // This garbage collection task is used to expire log entries. It is called to |
| 363 | // remove all logs (clear), all UID logs (unprivileged clear), or every |
| 364 | // 256 or 10% of the total logs (whichever is less) to prune the logs. |
| 365 | // |
| 366 | // First there is a prep phase where we discover the reader region lock that |
| 367 | // acts as a backstop to any pruning activity to stop there and go no further. |
| 368 | // |
| 369 | // There are three major pruning loops that follow. All expire from the oldest |
| 370 | // entries. Since there are multiple log buffers, the Android logging facility |
| 371 | // will appear to drop entries 'in the middle' when looking at multiple log |
| 372 | // sources and buffers. This effect is slightly more prominent when we prune |
| 373 | // the worst offender by logging source. Thus the logs slowly loose content |
| 374 | // and value as you move back in time. This is preferred since chatty sources |
| 375 | // invariably move the logs value down faster as less chatty sources would be |
| 376 | // expired in the noise. |
| 377 | // |
| 378 | // The first loop performs blacklisting and worst offender pruning. Falling |
| 379 | // through when there are no notable worst offenders and have not hit the |
| 380 | // region lock preventing further worst offender pruning. This loop also looks |
| 381 | // after managing the chatty log entries and merging to help provide |
| 382 | // statistical basis for blame. The chatty entries are not a notification of |
| 383 | // how much logs you may have, but instead represent how much logs you would |
| 384 | // have had in a virtual log buffer that is extended to cover all the in-memory |
| 385 | // logs without loss. They last much longer than the represented pruned logs |
| 386 | // since they get multiplied by the gains in the non-chatty log sources. |
| 387 | // |
| 388 | // The second loop get complicated because an algorithm of watermarks and |
| 389 | // history is maintained to reduce the order and keep processing time |
| 390 | // down to a minimum at scale. These algorithms can be costly in the face |
| 391 | // of larger log buffers, or severly limited processing time granted to a |
| 392 | // background task at lowest priority. |
| 393 | // |
| 394 | // This second loop does straight-up expiration from the end of the logs |
| 395 | // (again, remember for the specified log buffer id) but does some whitelist |
| 396 | // preservation. Thus whitelist is a Hail Mary low priority, blacklists and |
| 397 | // spam filtration all take priority. This second loop also checks if a region |
| 398 | // lock is causing us to buffer too much in the logs to help the reader(s), |
| 399 | // and will tell the slowest reader thread to skip log entries, and if |
| 400 | // persistent and hits a further threshold, kill the reader thread. |
| 401 | // |
| 402 | // The third thread is optional, and only gets hit if there was a whitelist |
| 403 | // and more needs to be pruned against the backstop of the region lock. |
| 404 | // |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 405 | // mLogElementsLock must be held when this function is called. |
Mark Salyzyn | 5bb2972 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 406 | // |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 407 | bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 408 | LogTimeEntry *oldest = NULL; |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 409 | bool busy = false; |
Mark Salyzyn | 2b25c66 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 410 | bool clearAll = pruneRows == ULONG_MAX; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 411 | |
| 412 | LogTimeEntry::lock(); |
| 413 | |
| 414 | // Region locked? |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 415 | LastLogTimes::iterator times = mTimes.begin(); |
| 416 | while(times != mTimes.end()) { |
| 417 | LogTimeEntry *entry = (*times); |
TraianX Schiau | da6495d | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 418 | if (entry->owned_Locked() && entry->isWatching(id) |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 419 | && (!oldest || |
| 420 | (oldest->mStart > entry->mStart) || |
| 421 | ((oldest->mStart == entry->mStart) && |
| 422 | (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 423 | oldest = entry; |
| 424 | } |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 425 | times++; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 426 | } |
| 427 | |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 428 | LogBufferElementCollection::iterator it; |
| 429 | |
Mark Salyzyn | 8fcfd85 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 430 | if (__predict_false(caller_uid != AID_ROOT)) { // unlikely |
Mark Salyzyn | 43a5f31 | 2016-09-01 15:48:36 -0700 | [diff] [blame] | 431 | // Only here if clear all request from non system source, so chatty |
| 432 | // filter logistics is not required. |
Mark Salyzyn | 507eb9f | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 433 | it = mLastSet[id] ? mLast[id] : mLogElements.begin(); |
| 434 | while (it != mLogElements.end()) { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 435 | LogBufferElement *element = *it; |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 436 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 437 | if ((element->getLogId() != id) || (element->getUid() != caller_uid)) { |
Mark Salyzyn | 2b25c66 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 438 | ++it; |
| 439 | continue; |
| 440 | } |
| 441 | |
Mark Salyzyn | 507eb9f | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 442 | if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) { |
| 443 | mLast[id] = it; |
| 444 | mLastSet[id] = true; |
| 445 | } |
| 446 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 447 | if (oldest && (oldest->mStart <= element->getSequence())) { |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 448 | busy = true; |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 449 | if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) { |
| 450 | oldest->triggerReader_Locked(); |
| 451 | } else { |
| 452 | oldest->triggerSkip_Locked(id, pruneRows); |
| 453 | } |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 454 | break; |
| 455 | } |
| 456 | |
Mark Salyzyn | 2b25c66 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 457 | it = erase(it); |
Mark Salyzyn | 43a5f31 | 2016-09-01 15:48:36 -0700 | [diff] [blame] | 458 | if (--pruneRows == 0) { |
| 459 | break; |
| 460 | } |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 461 | } |
| 462 | LogTimeEntry::unlock(); |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 463 | return busy; |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 466 | // prune by worst offenders; by blacklist, UID, and by PID of system UID |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 467 | bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty(); |
Mark Salyzyn | 2b25c66 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 468 | while (!clearAll && (pruneRows > 0)) { |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 469 | // recalculate the worst offender on every batched pass |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 470 | int worst = -1; // not valid for getUid() or getKey() |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 471 | size_t worst_sizes = 0; |
| 472 | size_t second_worst_sizes = 0; |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 473 | pid_t worstPid = 0; // POSIX guarantees PID != 0 |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 474 | |
Mark Salyzyn | ae76923 | 2015-03-17 17:17:25 -0700 | [diff] [blame] | 475 | if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) { |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 476 | // Calculate threshold as 12.5% of available storage |
| 477 | size_t threshold = log_buffer_size(id) / 8; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 478 | |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 479 | if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) { |
| 480 | stats.sortTags(AID_ROOT, (pid_t)0, 2, id).findWorst( |
| 481 | worst, worst_sizes, second_worst_sizes, threshold); |
Mark Salyzyn | 8fcfd85 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 482 | // per-pid filter for AID_SYSTEM sources is too complex |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 483 | } else { |
| 484 | stats.sort(AID_ROOT, (pid_t)0, 2, id).findWorst( |
| 485 | worst, worst_sizes, second_worst_sizes, threshold); |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 486 | |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 487 | if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) { |
| 488 | stats.sortPids(worst, (pid_t)0, 2, id).findWorst( |
| 489 | worstPid, worst_sizes, second_worst_sizes); |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 490 | } |
| 491 | } |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 492 | } |
| 493 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 494 | // skip if we have neither worst nor naughty filters |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 495 | if ((worst == -1) && !hasBlacklist) { |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 496 | break; |
| 497 | } |
| 498 | |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 499 | bool kick = false; |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 500 | bool leading = true; |
Mark Salyzyn | 507eb9f | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 501 | it = mLastSet[id] ? mLast[id] : mLogElements.begin(); |
Mark Salyzyn | 5bb2972 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 502 | // Perform at least one mandatory garbage collection cycle in following |
| 503 | // - clear leading chatty tags |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 504 | // - coalesce chatty tags |
Mark Salyzyn | 5bb2972 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 505 | // - check age-out of preserved logs |
| 506 | bool gc = pruneRows <= 1; |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 507 | if (!gc && (worst != -1)) { |
| 508 | { // begin scope for worst found iterator |
| 509 | LogBufferIteratorMap::iterator found = mLastWorst[id].find(worst); |
| 510 | if ((found != mLastWorst[id].end()) |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 511 | && (found->second != mLogElements.end())) { |
| 512 | leading = false; |
| 513 | it = found->second; |
| 514 | } |
| 515 | } |
Mark Salyzyn | 8fcfd85 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 516 | if (worstPid) { // begin scope for pid worst found iterator |
| 517 | // FYI: worstPid only set if !LOG_ID_EVENTS and |
| 518 | // !LOG_ID_SECURITY, not going to make that assumption ... |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 519 | LogBufferPidIteratorMap::iterator found |
| 520 | = mLastWorstPidOfSystem[id].find(worstPid); |
| 521 | if ((found != mLastWorstPidOfSystem[id].end()) |
| 522 | && (found->second != mLogElements.end())) { |
| 523 | leading = false; |
| 524 | it = found->second; |
| 525 | } |
Mark Salyzyn | c892ea3 | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 526 | } |
| 527 | } |
Mark Salyzyn | ccfe844 | 2015-08-24 13:43:27 -0700 | [diff] [blame] | 528 | static const timespec too_old = { |
| 529 | EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 |
| 530 | }; |
| 531 | LogBufferElementCollection::iterator lastt; |
| 532 | lastt = mLogElements.end(); |
| 533 | --lastt; |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 534 | LogBufferElementLast last; |
Mark Salyzyn | c892ea3 | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 535 | while (it != mLogElements.end()) { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 536 | LogBufferElement *element = *it; |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 537 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 538 | if (oldest && (oldest->mStart <= element->getSequence())) { |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 539 | busy = true; |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 540 | if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) { |
| 541 | oldest->triggerReader_Locked(); |
| 542 | } |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 543 | break; |
| 544 | } |
| 545 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 546 | if (element->getLogId() != id) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 547 | ++it; |
| 548 | continue; |
| 549 | } |
Mark Salyzyn | fa07f9d | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 550 | // below this point element->getLogId() == id |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 551 | |
Mark Salyzyn | 507eb9f | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 552 | if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) { |
| 553 | mLast[id] = it; |
| 554 | mLastSet[id] = true; |
| 555 | } |
| 556 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 557 | unsigned short dropped = element->getDropped(); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 558 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 559 | // remove any leading drops |
| 560 | if (leading && dropped) { |
| 561 | it = erase(it); |
| 562 | continue; |
| 563 | } |
| 564 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 565 | if (dropped && last.coalesce(element, dropped)) { |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 566 | it = erase(it, true); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 567 | continue; |
| 568 | } |
| 569 | |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 570 | int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) ? |
| 571 | element->getTag() : |
| 572 | element->getUid(); |
| 573 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 574 | if (hasBlacklist && mPrune.naughty(element)) { |
| 575 | last.clear(element); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 576 | it = erase(it); |
| 577 | if (dropped) { |
| 578 | continue; |
| 579 | } |
| 580 | |
| 581 | pruneRows--; |
| 582 | if (pruneRows == 0) { |
| 583 | break; |
| 584 | } |
| 585 | |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 586 | if (key == worst) { |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 587 | kick = true; |
| 588 | if (worst_sizes < second_worst_sizes) { |
| 589 | break; |
| 590 | } |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 591 | worst_sizes -= element->getMsgLen(); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 592 | } |
| 593 | continue; |
| 594 | } |
| 595 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 596 | if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) |
| 597 | || (element->getRealTime() > (*lastt)->getRealTime())) { |
Mark Salyzyn | ccfe844 | 2015-08-24 13:43:27 -0700 | [diff] [blame] | 598 | break; |
| 599 | } |
| 600 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 601 | if (dropped) { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 602 | last.add(element); |
| 603 | if (worstPid |
| 604 | && ((!gc && (element->getPid() == worstPid)) |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 605 | || (mLastWorstPidOfSystem[id].find(element->getPid()) |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 606 | == mLastWorstPidOfSystem[id].end()))) { |
Mark Salyzyn | fa07f9d | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 607 | // element->getUid() may not be AID_SYSTEM, next best |
Mark Salyzyn | 8fcfd85 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 608 | // watermark if current one empty. id is not LOG_ID_EVENTS |
| 609 | // or LOG_ID_SECURITY because of worstPid check. |
Mark Salyzyn | 1eefca2 | 2016-09-01 07:28:44 -0700 | [diff] [blame] | 610 | mLastWorstPidOfSystem[id][element->getPid()] = it; |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 611 | } |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 612 | if ((!gc && !worstPid && (key == worst)) |
| 613 | || (mLastWorst[id].find(key) == mLastWorst[id].end())) { |
| 614 | mLastWorst[id][key] = it; |
Mark Salyzyn | 49afe0d | 2015-08-24 13:43:27 -0700 | [diff] [blame] | 615 | } |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 616 | ++it; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 617 | continue; |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 618 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 619 | |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 620 | if ((key != worst) |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 621 | || (worstPid && (element->getPid() != worstPid))) { |
Mark Salyzyn | 5921276 | 2015-06-01 09:41:19 -0700 | [diff] [blame] | 622 | leading = false; |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 623 | last.clear(element); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 624 | ++it; |
| 625 | continue; |
| 626 | } |
Mark Salyzyn | fa07f9d | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 627 | // key == worst below here |
| 628 | // If worstPid set, then element->getPid() == worstPid below here |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 629 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 630 | pruneRows--; |
| 631 | if (pruneRows == 0) { |
| 632 | break; |
| 633 | } |
| 634 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 635 | kick = true; |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 636 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 637 | unsigned short len = element->getMsgLen(); |
Mark Salyzyn | 5392aac | 2015-05-22 10:03:31 -0700 | [diff] [blame] | 638 | |
| 639 | // do not create any leading drops |
| 640 | if (leading) { |
| 641 | it = erase(it); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 642 | } else { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 643 | stats.drop(element); |
| 644 | element->setDropped(1); |
| 645 | if (last.coalesce(element, 1)) { |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 646 | it = erase(it, true); |
Mark Salyzyn | 5392aac | 2015-05-22 10:03:31 -0700 | [diff] [blame] | 647 | } else { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 648 | last.add(element); |
| 649 | if (worstPid && (!gc |
| 650 | || (mLastWorstPidOfSystem[id].find(worstPid) |
| 651 | == mLastWorstPidOfSystem[id].end()))) { |
Mark Salyzyn | fa07f9d | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 652 | // element->getUid() may not be AID_SYSTEM, next best |
Mark Salyzyn | 8fcfd85 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 653 | // watermark if current one empty. id is not |
| 654 | // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid. |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 655 | mLastWorstPidOfSystem[id][worstPid] = it; |
| 656 | } |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 657 | if ((!gc && !worstPid) || |
| 658 | (mLastWorst[id].find(worst) == mLastWorst[id].end())) { |
| 659 | mLastWorst[id][worst] = it; |
Mark Salyzyn | 5bb2972 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 660 | } |
Mark Salyzyn | 5392aac | 2015-05-22 10:03:31 -0700 | [diff] [blame] | 661 | ++it; |
| 662 | } |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 663 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 664 | if (worst_sizes < second_worst_sizes) { |
| 665 | break; |
| 666 | } |
| 667 | worst_sizes -= len; |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 668 | } |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 669 | last.clear(); |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 670 | |
Mark Salyzyn | 1c95047 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 671 | if (!kick || !mPrune.worstUidEnabled()) { |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 672 | break; // the following loop will ask bad clients to skip/drop |
| 673 | } |
| 674 | } |
| 675 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 676 | bool whitelist = false; |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 677 | bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll; |
Mark Salyzyn | 507eb9f | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 678 | it = mLastSet[id] ? mLast[id] : mLogElements.begin(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 679 | while((pruneRows > 0) && (it != mLogElements.end())) { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 680 | LogBufferElement *element = *it; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 681 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 682 | if (element->getLogId() != id) { |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 683 | it++; |
| 684 | continue; |
| 685 | } |
| 686 | |
Mark Salyzyn | 507eb9f | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 687 | if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) { |
| 688 | mLast[id] = it; |
| 689 | mLastSet[id] = true; |
| 690 | } |
| 691 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 692 | if (oldest && (oldest->mStart <= element->getSequence())) { |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 693 | busy = true; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 694 | if (whitelist) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 695 | break; |
| 696 | } |
Mark Salyzyn | 1c95047 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 697 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 698 | if (stats.sizes(id) > (2 * log_buffer_size(id))) { |
| 699 | // kick a misbehaving log reader client off the island |
| 700 | oldest->release_Locked(); |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 701 | } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) { |
| 702 | oldest->triggerReader_Locked(); |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 703 | } else { |
| 704 | oldest->triggerSkip_Locked(id, pruneRows); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 705 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 706 | break; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 707 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 708 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 709 | if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) { |
| 710 | // WhiteListed |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 711 | whitelist = true; |
| 712 | it++; |
| 713 | continue; |
| 714 | } |
| 715 | |
| 716 | it = erase(it); |
| 717 | pruneRows--; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 718 | } |
| 719 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 720 | // Do not save the whitelist if we are reader range limited |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 721 | if (whitelist && (pruneRows > 0)) { |
Mark Salyzyn | 507eb9f | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 722 | it = mLastSet[id] ? mLast[id] : mLogElements.begin(); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 723 | while((it != mLogElements.end()) && (pruneRows > 0)) { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 724 | LogBufferElement *element = *it; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 725 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 726 | if (element->getLogId() != id) { |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 727 | ++it; |
| 728 | continue; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 729 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 730 | |
Mark Salyzyn | 507eb9f | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 731 | if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) { |
| 732 | mLast[id] = it; |
| 733 | mLastSet[id] = true; |
| 734 | } |
| 735 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 736 | if (oldest && (oldest->mStart <= element->getSequence())) { |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 737 | busy = true; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 738 | if (stats.sizes(id) > (2 * log_buffer_size(id))) { |
| 739 | // kick a misbehaving log reader client off the island |
| 740 | oldest->release_Locked(); |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 741 | } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) { |
| 742 | oldest->triggerReader_Locked(); |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 743 | } else { |
| 744 | oldest->triggerSkip_Locked(id, pruneRows); |
| 745 | } |
| 746 | break; |
| 747 | } |
| 748 | |
| 749 | it = erase(it); |
| 750 | pruneRows--; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 751 | } |
| 752 | } |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 753 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 754 | LogTimeEntry::unlock(); |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 755 | |
| 756 | return (pruneRows > 0) && busy; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | // clear all rows of type "id" from the buffer. |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 760 | bool LogBuffer::clear(log_id_t id, uid_t uid) { |
| 761 | bool busy = true; |
| 762 | // If it takes more than 4 tries (seconds) to clear, then kill reader(s) |
| 763 | for (int retry = 4;;) { |
| 764 | if (retry == 1) { // last pass |
| 765 | // Check if it is still busy after the sleep, we say prune |
| 766 | // one entry, not another clear run, so we are looking for |
| 767 | // the quick side effect of the return value to tell us if |
| 768 | // we have a _blocked_ reader. |
| 769 | pthread_mutex_lock(&mLogElementsLock); |
| 770 | busy = prune(id, 1, uid); |
| 771 | pthread_mutex_unlock(&mLogElementsLock); |
| 772 | // It is still busy, blocked reader(s), lets kill them all! |
| 773 | // otherwise, lets be a good citizen and preserve the slow |
| 774 | // readers and let the clear run (below) deal with determining |
| 775 | // if we are still blocked and return an error code to caller. |
| 776 | if (busy) { |
| 777 | LogTimeEntry::lock(); |
| 778 | LastLogTimes::iterator times = mTimes.begin(); |
| 779 | while (times != mTimes.end()) { |
| 780 | LogTimeEntry *entry = (*times); |
| 781 | // Killer punch |
| 782 | if (entry->owned_Locked() && entry->isWatching(id)) { |
| 783 | entry->release_Locked(); |
| 784 | } |
| 785 | times++; |
| 786 | } |
| 787 | LogTimeEntry::unlock(); |
| 788 | } |
| 789 | } |
| 790 | pthread_mutex_lock(&mLogElementsLock); |
| 791 | busy = prune(id, ULONG_MAX, uid); |
| 792 | pthread_mutex_unlock(&mLogElementsLock); |
| 793 | if (!busy || !--retry) { |
| 794 | break; |
| 795 | } |
| 796 | sleep (1); // Let reader(s) catch up after notification |
| 797 | } |
| 798 | return busy; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | // get the used space associated with "id". |
| 802 | unsigned long LogBuffer::getSizeUsed(log_id_t id) { |
| 803 | pthread_mutex_lock(&mLogElementsLock); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 804 | size_t retval = stats.sizes(id); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 805 | pthread_mutex_unlock(&mLogElementsLock); |
| 806 | return retval; |
| 807 | } |
| 808 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 809 | // set the total space allocated to "id" |
| 810 | int LogBuffer::setSize(log_id_t id, unsigned long size) { |
| 811 | // Reasonable limits ... |
Mark Salyzyn | f10e273 | 2016-09-27 13:08:23 -0700 | [diff] [blame] | 812 | if (!__android_logger_valid_buffer_size(size)) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 813 | return -1; |
| 814 | } |
| 815 | pthread_mutex_lock(&mLogElementsLock); |
| 816 | log_buffer_size(id) = size; |
| 817 | pthread_mutex_unlock(&mLogElementsLock); |
| 818 | return 0; |
| 819 | } |
| 820 | |
| 821 | // get the total space allocated to "id" |
| 822 | unsigned long LogBuffer::getSize(log_id_t id) { |
| 823 | pthread_mutex_lock(&mLogElementsLock); |
| 824 | size_t retval = log_buffer_size(id); |
| 825 | pthread_mutex_unlock(&mLogElementsLock); |
| 826 | return retval; |
| 827 | } |
| 828 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 829 | uint64_t LogBuffer::flushTo( |
Mark Salyzyn | 8fa8896 | 2016-01-26 14:32:35 -0800 | [diff] [blame] | 830 | SocketClient *reader, const uint64_t start, |
| 831 | bool privileged, bool security, |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 832 | int (*filter)(const LogBufferElement *element, void *arg), void *arg) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 833 | LogBufferElementCollection::iterator it; |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 834 | uint64_t max = start; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 835 | uid_t uid = reader->getUid(); |
| 836 | |
| 837 | pthread_mutex_lock(&mLogElementsLock); |
Dragoslav Mitrinovic | 8e8e8db | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 838 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 839 | if (start <= 1) { |
Dragoslav Mitrinovic | 8e8e8db | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 840 | // client wants to start from the beginning |
| 841 | it = mLogElements.begin(); |
| 842 | } else { |
| 843 | // Client wants to start from some specified time. Chances are |
| 844 | // we are better off starting from the end of the time sorted list. |
| 845 | for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) { |
| 846 | --it; |
| 847 | LogBufferElement *element = *it; |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 848 | if (element->getSequence() <= start) { |
Dragoslav Mitrinovic | 8e8e8db | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 849 | it++; |
| 850 | break; |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | for (; it != mLogElements.end(); ++it) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 856 | LogBufferElement *element = *it; |
| 857 | |
| 858 | if (!privileged && (element->getUid() != uid)) { |
| 859 | continue; |
| 860 | } |
| 861 | |
Mark Salyzyn | 8fa8896 | 2016-01-26 14:32:35 -0800 | [diff] [blame] | 862 | if (!security && (element->getLogId() == LOG_ID_SECURITY)) { |
| 863 | continue; |
| 864 | } |
| 865 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 866 | if (element->getSequence() <= start) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 867 | continue; |
| 868 | } |
| 869 | |
| 870 | // NB: calling out to another object with mLogElementsLock held (safe) |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 871 | if (filter) { |
| 872 | int ret = (*filter)(element, arg); |
| 873 | if (ret == false) { |
| 874 | continue; |
| 875 | } |
| 876 | if (ret != true) { |
| 877 | break; |
| 878 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | pthread_mutex_unlock(&mLogElementsLock); |
| 882 | |
| 883 | // range locking in LastLogTimes looks after us |
Mark Salyzyn | 7b87365 | 2015-12-03 15:38:35 -0800 | [diff] [blame] | 884 | max = element->flushTo(reader, this, privileged); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 885 | |
| 886 | if (max == element->FLUSH_ERROR) { |
| 887 | return max; |
| 888 | } |
| 889 | |
| 890 | pthread_mutex_lock(&mLogElementsLock); |
| 891 | } |
| 892 | pthread_mutex_unlock(&mLogElementsLock); |
| 893 | |
| 894 | return max; |
| 895 | } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 896 | |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 897 | std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid, |
| 898 | unsigned int logMask) { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 899 | pthread_mutex_lock(&mLogElementsLock); |
| 900 | |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 901 | std::string ret = stats.format(uid, pid, logMask); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 902 | |
| 903 | pthread_mutex_unlock(&mLogElementsLock); |
Mark Salyzyn | 73160ac | 2015-08-20 10:01:44 -0700 | [diff] [blame] | 904 | |
| 905 | return ret; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 906 | } |