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