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 | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 20 | #include <endian.h> |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 21 | #include <errno.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <string.h> |
Mark Salyzyn | 8fcfd85 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 24 | #include <sys/cdefs.h> |
Mark Salyzyn | 57a0af9 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 25 | #include <sys/user.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 26 | #include <time.h> |
| 27 | #include <unistd.h> |
| 28 | |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 29 | #include <unordered_map> |
| 30 | |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 31 | #include <cutils/properties.h> |
Mark Salyzyn | f10e273 | 2016-09-27 13:08:23 -0700 | [diff] [blame] | 32 | #include <private/android_logger.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 33 | |
| 34 | #include "LogBuffer.h" |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 35 | #include "LogKlog.h" |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 36 | #include "LogReader.h" |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 37 | #include "LogUtils.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 38 | |
Mark Salyzyn | 8fcfd85 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 39 | #ifndef __predict_false |
| 40 | #define __predict_false(exp) __builtin_expect((exp) != 0, 0) |
| 41 | #endif |
| 42 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 43 | // Default |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 44 | #define log_buffer_size(id) mMaxSize[id] |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 45 | |
Mark Salyzyn | 5836379 | 2017-04-17 12:46:12 -0700 | [diff] [blame] | 46 | const log_time LogBuffer::pruneMargin(3, 0); |
| 47 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 48 | void LogBuffer::init() { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 49 | log_id_for_each(i) { |
Mark Salyzyn | 507eb9f | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 50 | mLastSet[i] = false; |
| 51 | mLast[i] = mLogElements.begin(); |
| 52 | |
Mark Salyzyn | f10e273 | 2016-09-27 13:08:23 -0700 | [diff] [blame] | 53 | if (setSize(i, __android_logger_get_buffer_size(i))) { |
Mark Salyzyn | 57a0af9 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 54 | setSize(i, LOG_BUFFER_MIN_SIZE); |
| 55 | } |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 56 | } |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 57 | bool lastMonotonic = monotonic; |
Mark Salyzyn | ba7a9a0 | 2015-12-01 15:57:25 -0800 | [diff] [blame] | 58 | monotonic = android_log_clockid() == CLOCK_MONOTONIC; |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 59 | if (lastMonotonic != monotonic) { |
| 60 | // |
| 61 | // Fixup all timestamps, may not be 100% accurate, but better than |
| 62 | // throwing what we have away when we get 'surprised' by a change. |
| 63 | // In-place element fixup so no need to check reader-lock. Entries |
| 64 | // should already be in timestamp order, but we could end up with a |
| 65 | // few out-of-order entries if new monotonics come in before we |
| 66 | // are notified of the reinit change in status. A Typical example would |
| 67 | // be: |
| 68 | // --------- beginning of system |
| 69 | // 10.494082 184 201 D Cryptfs : Just triggered post_fs_data |
| 70 | // --------- beginning of kernel |
| 71 | // 0.000000 0 0 I : Initializing cgroup subsys |
| 72 | // as the act of mounting /data would trigger persist.logd.timestamp to |
| 73 | // be corrected. 1/30 corner case YMMV. |
| 74 | // |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 75 | rdlock(); |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 76 | LogBufferElementCollection::iterator it = mLogElements.begin(); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 77 | while ((it != mLogElements.end())) { |
| 78 | LogBufferElement* e = *it; |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 79 | if (monotonic) { |
| 80 | if (!android::isMonotonic(e->mRealTime)) { |
| 81 | LogKlog::convertRealToMonotonic(e->mRealTime); |
Mark Salyzyn | 206ed8e | 2017-05-18 10:06:00 -0700 | [diff] [blame] | 82 | if ((e->mRealTime.tv_nsec % 1000) == 0) { |
| 83 | e->mRealTime.tv_nsec++; |
| 84 | } |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 85 | } |
| 86 | } else { |
| 87 | if (android::isMonotonic(e->mRealTime)) { |
| 88 | LogKlog::convertMonotonicToReal(e->mRealTime); |
Mark Salyzyn | 206ed8e | 2017-05-18 10:06:00 -0700 | [diff] [blame] | 89 | if ((e->mRealTime.tv_nsec % 1000) == 0) { |
| 90 | e->mRealTime.tv_nsec++; |
| 91 | } |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | ++it; |
| 95 | } |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 96 | unlock(); |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 99 | // We may have been triggered by a SIGHUP. Release any sleeping reader |
| 100 | // threads to dump their current content. |
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 | // NB: this is _not_ performed in the context of a SIGHUP, it is |
| 103 | // performed during startup, and in context of reinit administrative thread |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 104 | LogTimeEntry::wrlock(); |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 105 | |
| 106 | LastLogTimes::iterator times = mTimes.begin(); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 107 | while (times != mTimes.end()) { |
| 108 | LogTimeEntry* entry = (*times); |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 109 | if (entry->owned_Locked()) { |
| 110 | entry->triggerReader_Locked(); |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 111 | } |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 112 | times++; |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 113 | } |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 114 | |
| 115 | LogTimeEntry::unlock(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 118 | LogBuffer::LogBuffer(LastLogTimes* times) |
| 119 | : monotonic(android_log_clockid() == CLOCK_MONOTONIC), mTimes(*times) { |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 120 | pthread_rwlock_init(&mLogElementsLock, nullptr); |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 121 | |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 122 | log_id_for_each(i) { |
Mark Salyzyn | ae2abf1 | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 123 | lastLoggedElements[i] = nullptr; |
| 124 | droppedElements[i] = nullptr; |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 127 | init(); |
| 128 | } |
| 129 | |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 130 | LogBuffer::~LogBuffer() { |
| 131 | log_id_for_each(i) { |
| 132 | delete lastLoggedElements[i]; |
| 133 | delete droppedElements[i]; |
| 134 | } |
| 135 | } |
| 136 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 137 | enum match_type { DIFFERENT, SAME, SAME_LIBLOG }; |
Mark Salyzyn | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 138 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 139 | static enum match_type identical(LogBufferElement* elem, |
| 140 | LogBufferElement* last) { |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 141 | // is it mostly identical? |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 142 | // if (!elem) return DIFFERENT; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 143 | ssize_t lenl = elem->getMsgLen(); |
| 144 | if (lenl <= 0) return DIFFERENT; // value if this represents a chatty elem |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 145 | // if (!last) return DIFFERENT; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 146 | ssize_t lenr = last->getMsgLen(); |
| 147 | if (lenr <= 0) return DIFFERENT; // value if this represents a chatty elem |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 148 | // if (elem->getLogId() != last->getLogId()) return DIFFERENT; |
Mark Salyzyn | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 149 | if (elem->getUid() != last->getUid()) return DIFFERENT; |
| 150 | if (elem->getPid() != last->getPid()) return DIFFERENT; |
| 151 | if (elem->getTid() != last->getTid()) return DIFFERENT; |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 152 | |
| 153 | // last is more than a minute old, stop squashing identical messages |
| 154 | if (elem->getRealTime().nsec() > |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 155 | (last->getRealTime().nsec() + 60 * NS_PER_SEC)) |
| 156 | return DIFFERENT; |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 157 | |
| 158 | // Identical message |
| 159 | const char* msgl = elem->getMsg(); |
| 160 | const char* msgr = last->getMsg(); |
Mark Salyzyn | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 161 | if (lenl == lenr) { |
| 162 | if (!fastcmp<memcmp>(msgl, msgr, lenl)) return SAME; |
| 163 | // liblog tagged messages (content gets summed) |
| 164 | if ((elem->getLogId() == LOG_ID_EVENTS) && |
| 165 | (lenl == sizeof(android_log_event_int_t)) && |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 166 | !fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_int_t) - |
| 167 | sizeof(int32_t)) && |
Mark Salyzyn | 1598fe0 | 2017-03-13 15:41:59 -0700 | [diff] [blame] | 168 | (elem->getTag() == LIBLOG_LOG_TAG)) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 169 | return SAME_LIBLOG; |
Mark Salyzyn | 1598fe0 | 2017-03-13 15:41:59 -0700 | [diff] [blame] | 170 | } |
Mark Salyzyn | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 171 | } |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 172 | |
| 173 | // audit message (except sequence number) identical? |
Mark Salyzyn | 2271242 | 2018-03-13 11:06:38 -0700 | [diff] [blame] | 174 | if (last->isBinary() && |
| 175 | (lenl > static_cast<ssize_t>(sizeof(android_log_event_string_t))) && |
| 176 | (lenr > static_cast<ssize_t>(sizeof(android_log_event_string_t)))) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 177 | if (fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_string_t) - |
Mark Salyzyn | 1598fe0 | 2017-03-13 15:41:59 -0700 | [diff] [blame] | 178 | sizeof(int32_t))) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 179 | return DIFFERENT; |
Mark Salyzyn | 1598fe0 | 2017-03-13 15:41:59 -0700 | [diff] [blame] | 180 | } |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 181 | msgl += sizeof(android_log_event_string_t); |
| 182 | lenl -= sizeof(android_log_event_string_t); |
| 183 | msgr += sizeof(android_log_event_string_t); |
| 184 | lenr -= sizeof(android_log_event_string_t); |
| 185 | } |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 186 | static const char avc[] = "): avc: "; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 187 | const char* avcl = android::strnstr(msgl, lenl, avc); |
Mark Salyzyn | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 188 | if (!avcl) return DIFFERENT; |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 189 | lenl -= avcl - msgl; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 190 | const char* avcr = android::strnstr(msgr, lenr, avc); |
Mark Salyzyn | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 191 | if (!avcr) return DIFFERENT; |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 192 | lenr -= avcr - msgr; |
Mark Salyzyn | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 193 | if (lenl != lenr) return DIFFERENT; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 194 | if (fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc), |
Mark Salyzyn | 1598fe0 | 2017-03-13 15:41:59 -0700 | [diff] [blame] | 195 | lenl - strlen(avc))) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 196 | return DIFFERENT; |
Mark Salyzyn | 1598fe0 | 2017-03-13 15:41:59 -0700 | [diff] [blame] | 197 | } |
Mark Salyzyn | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 198 | return SAME; |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 201 | int LogBuffer::log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, |
| 202 | pid_t tid, const char* msg, unsigned short len) { |
Yi Kong | 141ccee | 2018-03-01 17:48:53 -0500 | [diff] [blame] | 203 | if (log_id >= LOG_ID_MAX) { |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 204 | return -EINVAL; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 205 | } |
Mark Salyzyn | e59c469 | 2014-10-02 13:07:05 -0700 | [diff] [blame] | 206 | |
Mark Salyzyn | 206ed8e | 2017-05-18 10:06:00 -0700 | [diff] [blame] | 207 | // Slip the time by 1 nsec if the incoming lands on xxxxxx000 ns. |
| 208 | // This prevents any chance that an outside source can request an |
| 209 | // exact entry with time specified in ms or us precision. |
| 210 | if ((realtime.tv_nsec % 1000) == 0) ++realtime.tv_nsec; |
| 211 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 212 | LogBufferElement* elem = |
| 213 | new LogBufferElement(log_id, realtime, uid, pid, tid, msg, len); |
Mark Salyzyn | 0ee8de3 | 2016-01-06 21:17:43 +0000 | [diff] [blame] | 214 | if (log_id != LOG_ID_SECURITY) { |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 215 | int prio = ANDROID_LOG_INFO; |
Mark Salyzyn | ae2abf1 | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 216 | const char* tag = nullptr; |
Paul Elliott | c6ed8f3 | 2017-11-07 11:11:41 +0000 | [diff] [blame] | 217 | size_t tag_len = 0; |
Yao Chen | 025f05a | 2017-12-01 15:48:19 -0800 | [diff] [blame] | 218 | if (log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS) { |
Mark Salyzyn | 61e9ce6 | 2016-09-12 14:51:54 -0700 | [diff] [blame] | 219 | tag = tagToName(elem->getTag()); |
Paul Elliott | c6ed8f3 | 2017-11-07 11:11:41 +0000 | [diff] [blame] | 220 | if (tag) { |
| 221 | tag_len = strlen(tag); |
| 222 | } |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 223 | } else { |
| 224 | prio = *msg; |
| 225 | tag = msg + 1; |
Paul Elliott | c6ed8f3 | 2017-11-07 11:11:41 +0000 | [diff] [blame] | 226 | tag_len = strnlen(tag, len - 1); |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 227 | } |
Paul Elliott | c6ed8f3 | 2017-11-07 11:11:41 +0000 | [diff] [blame] | 228 | if (!__android_log_is_loggable_len(prio, tag, tag_len, |
| 229 | ANDROID_LOG_VERBOSE)) { |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 230 | // Log traffic received to total |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 231 | wrlock(); |
Mark Salyzyn | 02dd2f4 | 2017-04-14 09:46:57 -0700 | [diff] [blame] | 232 | stats.addTotal(elem); |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 233 | unlock(); |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 234 | delete elem; |
| 235 | return -EACCES; |
| 236 | } |
Mark Salyzyn | e59c469 | 2014-10-02 13:07:05 -0700 | [diff] [blame] | 237 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 238 | |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 239 | wrlock(); |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 240 | LogBufferElement* currentLast = lastLoggedElements[log_id]; |
| 241 | if (currentLast) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 242 | LogBufferElement* dropped = droppedElements[log_id]; |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 243 | unsigned short count = dropped ? dropped->getDropped() : 0; |
Mark Salyzyn | 8f83a35 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 244 | // |
| 245 | // State Init |
| 246 | // incoming: |
Mark Salyzyn | ae2abf1 | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 247 | // dropped = nullptr |
| 248 | // currentLast = nullptr; |
Mark Salyzyn | 8f83a35 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 249 | // elem = incoming message |
| 250 | // outgoing: |
Mark Salyzyn | ae2abf1 | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 251 | // dropped = nullptr -> State 0 |
Mark Salyzyn | 8f83a35 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 252 | // currentLast = copy of elem |
| 253 | // log elem |
| 254 | // State 0 |
| 255 | // incoming: |
| 256 | // count = 0 |
Mark Salyzyn | ae2abf1 | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 257 | // dropped = nullptr |
Mark Salyzyn | 8f83a35 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 258 | // currentLast = copy of last message |
| 259 | // elem = incoming message |
Mark Salyzyn | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 260 | // outgoing: if match != DIFFERENT |
Mark Salyzyn | 8f83a35 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 261 | // dropped = copy of first identical message -> State 1 |
| 262 | // currentLast = reference to elem |
Mark Salyzyn | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 263 | // break: if match == DIFFERENT |
Mark Salyzyn | ae2abf1 | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 264 | // dropped = nullptr -> State 0 |
Mark Salyzyn | 8f83a35 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 265 | // delete copy of last message (incoming currentLast) |
| 266 | // currentLast = copy of elem |
| 267 | // log elem |
| 268 | // State 1 |
| 269 | // incoming: |
| 270 | // count = 0 |
| 271 | // dropped = copy of first identical message |
| 272 | // currentLast = reference to last held-back incoming |
| 273 | // message |
| 274 | // elem = incoming message |
Mark Salyzyn | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 275 | // outgoing: if match == SAME |
Mark Salyzyn | 8f83a35 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 276 | // delete copy of first identical message (dropped) |
| 277 | // dropped = reference to last held-back incoming |
| 278 | // message set to chatty count of 1 -> State 2 |
| 279 | // currentLast = reference to elem |
Mark Salyzyn | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 280 | // outgoing: if match == SAME_LIBLOG |
| 281 | // dropped = copy of first identical message -> State 1 |
| 282 | // take sum of currentLast and elem |
| 283 | // if sum overflows: |
| 284 | // log currentLast |
| 285 | // currentLast = reference to elem |
| 286 | // else |
| 287 | // delete currentLast |
| 288 | // currentLast = reference to elem, sum liblog. |
| 289 | // break: if match == DIFFERENT |
Mark Salyzyn | 8f83a35 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 290 | // delete dropped |
Mark Salyzyn | ae2abf1 | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 291 | // dropped = nullptr -> State 0 |
Mark Salyzyn | 8f83a35 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 292 | // log reference to last held-back (currentLast) |
| 293 | // currentLast = copy of elem |
| 294 | // log elem |
| 295 | // State 2 |
| 296 | // incoming: |
| 297 | // count = chatty count |
| 298 | // dropped = chatty message holding count |
| 299 | // currentLast = reference to last held-back incoming |
| 300 | // message. |
| 301 | // dropped = chatty message holding count |
| 302 | // elem = incoming message |
Mark Salyzyn | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 303 | // outgoing: if match != DIFFERENT |
Mark Salyzyn | 8f83a35 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 304 | // delete chatty message holding count |
| 305 | // dropped = reference to last held-back incoming |
| 306 | // message, set to chatty count + 1 |
| 307 | // currentLast = reference to elem |
Mark Salyzyn | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 308 | // break: if match == DIFFERENT |
Mark Salyzyn | 8f83a35 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 309 | // log dropped (chatty message) |
Mark Salyzyn | ae2abf1 | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 310 | // dropped = nullptr -> State 0 |
Mark Salyzyn | 8f83a35 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 311 | // log reference to last held-back (currentLast) |
| 312 | // currentLast = copy of elem |
| 313 | // log elem |
| 314 | // |
Mark Salyzyn | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 315 | enum match_type match = identical(elem, currentLast); |
| 316 | if (match != DIFFERENT) { |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 317 | if (dropped) { |
Mark Salyzyn | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 318 | // Sum up liblog tag messages? |
| 319 | if ((count == 0) /* at Pass 1 */ && (match == SAME_LIBLOG)) { |
| 320 | android_log_event_int_t* event = |
| 321 | reinterpret_cast<android_log_event_int_t*>( |
| 322 | const_cast<char*>(currentLast->getMsg())); |
| 323 | // |
| 324 | // To unit test, differentiate with something like: |
| 325 | // event->header.tag = htole32(CHATTY_LOG_TAG); |
| 326 | // here, then instead of delete currentLast below, |
| 327 | // log(currentLast) to see the incremental sums form. |
| 328 | // |
| 329 | uint32_t swab = event->payload.data; |
| 330 | unsigned long long total = htole32(swab); |
| 331 | event = reinterpret_cast<android_log_event_int_t*>( |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 332 | const_cast<char*>(elem->getMsg())); |
Mark Salyzyn | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 333 | swab = event->payload.data; |
| 334 | |
| 335 | lastLoggedElements[LOG_ID_EVENTS] = elem; |
| 336 | total += htole32(swab); |
| 337 | // check for overflow |
| 338 | if (total >= UINT32_MAX) { |
| 339 | log(currentLast); |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 340 | unlock(); |
Mark Salyzyn | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 341 | return len; |
| 342 | } |
Mark Salyzyn | 02dd2f4 | 2017-04-14 09:46:57 -0700 | [diff] [blame] | 343 | stats.addTotal(currentLast); |
Mark Salyzyn | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 344 | delete currentLast; |
| 345 | swab = total; |
| 346 | event->payload.data = htole32(swab); |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 347 | unlock(); |
Mark Salyzyn | 1dfb4de | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 348 | return len; |
| 349 | } |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 350 | if (count == USHRT_MAX) { |
| 351 | log(dropped); |
| 352 | count = 1; |
| 353 | } else { |
| 354 | delete dropped; |
| 355 | ++count; |
| 356 | } |
| 357 | } |
| 358 | if (count) { |
Mark Salyzyn | 02dd2f4 | 2017-04-14 09:46:57 -0700 | [diff] [blame] | 359 | stats.addTotal(currentLast); |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 360 | currentLast->setDropped(count); |
| 361 | } |
| 362 | droppedElements[log_id] = currentLast; |
| 363 | lastLoggedElements[log_id] = elem; |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 364 | unlock(); |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 365 | return len; |
| 366 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 367 | if (dropped) { // State 1 or 2 |
| 368 | if (count) { // State 2 |
| 369 | log(dropped); // report chatty |
| 370 | } else { // State 1 |
| 371 | delete dropped; |
Mark Salyzyn | 8f83a35 | 2016-12-16 16:09:15 -0800 | [diff] [blame] | 372 | } |
Mark Salyzyn | ae2abf1 | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 373 | droppedElements[log_id] = nullptr; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 374 | log(currentLast); // report last message in the series |
| 375 | } else { // State 0 |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 376 | delete currentLast; |
| 377 | } |
| 378 | } |
| 379 | lastLoggedElements[log_id] = new LogBufferElement(*elem); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 380 | |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 381 | log(elem); |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 382 | unlock(); |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 383 | |
| 384 | return len; |
| 385 | } |
| 386 | |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 387 | // assumes LogBuffer::wrlock() held, owns elem, look after garbage collection |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 388 | void LogBuffer::log(LogBufferElement* elem) { |
Mark Salyzyn | 09d6632 | 2017-03-14 13:11:12 -0700 | [diff] [blame] | 389 | // cap on how far back we will sort in-place, otherwise append |
| 390 | static uint32_t too_far_back = 5; // five seconds |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 391 | // Insert elements in time sorted order if possible |
| 392 | // NB: if end is region locked, place element at end of list |
| 393 | LogBufferElementCollection::iterator it = mLogElements.end(); |
| 394 | LogBufferElementCollection::iterator last = it; |
Mark Salyzyn | 1d84f0b | 2017-03-03 10:21:23 -0800 | [diff] [blame] | 395 | if (__predict_true(it != mLogElements.begin())) --it; |
| 396 | if (__predict_false(it == mLogElements.begin()) || |
Mark Salyzyn | 09d6632 | 2017-03-14 13:11:12 -0700 | [diff] [blame] | 397 | __predict_true((*it)->getRealTime() <= elem->getRealTime()) || |
| 398 | __predict_false((((*it)->getRealTime().tv_sec - too_far_back) > |
| 399 | elem->getRealTime().tv_sec) && |
| 400 | (elem->getLogId() != LOG_ID_KERNEL) && |
| 401 | ((*it)->getLogId() != LOG_ID_KERNEL))) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 402 | mLogElements.push_back(elem); |
| 403 | } else { |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 404 | log_time end = log_time::EPOCH; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 405 | bool end_set = false; |
| 406 | bool end_always = false; |
| 407 | |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 408 | LogTimeEntry::rdlock(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 409 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 410 | LastLogTimes::iterator times = mTimes.begin(); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 411 | while (times != mTimes.end()) { |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 412 | LogTimeEntry* entry = (*times); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 413 | if (entry->owned_Locked()) { |
| 414 | if (!entry->mNonBlock) { |
| 415 | end_always = true; |
| 416 | break; |
| 417 | } |
Mark Salyzyn | 1d84f0b | 2017-03-03 10:21:23 -0800 | [diff] [blame] | 418 | // it passing mEnd is blocked by the following checks. |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 419 | if (!end_set || (end <= entry->mEnd)) { |
| 420 | end = entry->mEnd; |
| 421 | end_set = true; |
| 422 | } |
| 423 | } |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 424 | times++; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 425 | } |
| 426 | |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 427 | if (end_always || (end_set && (end > (*it)->getRealTime()))) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 428 | mLogElements.push_back(elem); |
| 429 | } else { |
Mark Salyzyn | 1d84f0b | 2017-03-03 10:21:23 -0800 | [diff] [blame] | 430 | // should be short as timestamps are localized near end() |
| 431 | do { |
| 432 | last = it; |
| 433 | if (__predict_false(it == mLogElements.begin())) { |
| 434 | break; |
| 435 | } |
Mark Salyzyn | 1d84f0b | 2017-03-03 10:21:23 -0800 | [diff] [blame] | 436 | --it; |
| 437 | } while (((*it)->getRealTime() > elem->getRealTime()) && |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 438 | (!end_set || (end <= (*it)->getRealTime()))); |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 439 | mLogElements.insert(last, elem); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 440 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 441 | LogTimeEntry::unlock(); |
| 442 | } |
| 443 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 444 | stats.add(elem); |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 445 | maybePrune(elem->getLogId()); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 446 | } |
| 447 | |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 448 | // 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] | 449 | // |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 450 | // LogBuffer::wrlock() must be held when this function is called. |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 451 | void LogBuffer::maybePrune(log_id_t id) { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 452 | size_t sizes = stats.sizes(id); |
Mark Salyzyn | 62ab0fd | 2015-08-10 10:23:56 -0700 | [diff] [blame] | 453 | unsigned long maxSize = log_buffer_size(id); |
| 454 | if (sizes > maxSize) { |
Mark Salyzyn | b39ed0c | 2015-08-19 12:20:36 -0700 | [diff] [blame] | 455 | size_t sizeOver = sizes - ((maxSize * 9) / 10); |
Mark Salyzyn | 58b8be8 | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 456 | size_t elements = stats.realElements(id); |
| 457 | size_t minElements = elements / 100; |
| 458 | if (minElements < minPrune) { |
| 459 | minElements = minPrune; |
| 460 | } |
Mark Salyzyn | 62ab0fd | 2015-08-10 10:23:56 -0700 | [diff] [blame] | 461 | unsigned long pruneRows = elements * sizeOver / sizes; |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 462 | if (pruneRows < minElements) { |
Mark Salyzyn | 62ab0fd | 2015-08-10 10:23:56 -0700 | [diff] [blame] | 463 | pruneRows = minElements; |
Mark Salyzyn | 740f9b4 | 2014-01-13 16:37:51 -0800 | [diff] [blame] | 464 | } |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 465 | if (pruneRows > maxPrune) { |
| 466 | pruneRows = maxPrune; |
Mark Salyzyn | b39ed0c | 2015-08-19 12:20:36 -0700 | [diff] [blame] | 467 | } |
Mark Salyzyn | 740f9b4 | 2014-01-13 16:37:51 -0800 | [diff] [blame] | 468 | prune(id, pruneRows); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
Mark Salyzyn | 831aa29 | 2015-09-03 16:08:50 -0700 | [diff] [blame] | 472 | LogBufferElementCollection::iterator LogBuffer::erase( |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 473 | LogBufferElementCollection::iterator it, bool coalesce) { |
| 474 | LogBufferElement* element = *it; |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 475 | log_id_t id = element->getLogId(); |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 476 | |
Mark Salyzyn | fa07f9d | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 477 | // Remove iterator references in the various lists that will become stale |
| 478 | // after the element is erased from the main logging list. |
| 479 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 480 | { // start of scope for found iterator |
| 481 | int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) |
| 482 | ? element->getTag() |
| 483 | : element->getUid(); |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 484 | LogBufferIteratorMap::iterator found = mLastWorst[id].find(key); |
| 485 | if ((found != mLastWorst[id].end()) && (it == found->second)) { |
| 486 | mLastWorst[id].erase(found); |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 487 | } |
Mark Salyzyn | c892ea3 | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 488 | } |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 489 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 490 | { // start of scope for pid found iterator |
Mark Salyzyn | fa07f9d | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 491 | // element->getUid() may not be AID_SYSTEM for next-best-watermark. |
Mark Salyzyn | 8fcfd85 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 492 | // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and |
| 493 | // long term code stability, find() check should be fast for those ids. |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 494 | LogBufferPidIteratorMap::iterator found = |
| 495 | mLastWorstPidOfSystem[id].find(element->getPid()); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 496 | if ((found != mLastWorstPidOfSystem[id].end()) && |
| 497 | (it == found->second)) { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 498 | mLastWorstPidOfSystem[id].erase(found); |
| 499 | } |
| 500 | } |
| 501 | |
Mark Salyzyn | 7fd6c5c | 2016-01-19 16:04:41 -0800 | [diff] [blame] | 502 | bool setLast[LOG_ID_MAX]; |
| 503 | bool doSetLast = false; |
| 504 | log_id_for_each(i) { |
| 505 | doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]); |
| 506 | } |
Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 507 | #ifdef DEBUG_CHECK_FOR_STALE_ENTRIES |
| 508 | LogBufferElementCollection::iterator bad = it; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 509 | int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) |
| 510 | ? element->getTag() |
| 511 | : element->getUid(); |
Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 512 | #endif |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 513 | it = mLogElements.erase(it); |
Mark Salyzyn | 7fd6c5c | 2016-01-19 16:04:41 -0800 | [diff] [blame] | 514 | if (doSetLast) { |
| 515 | log_id_for_each(i) { |
| 516 | if (setLast[i]) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 517 | if (__predict_false(it == mLogElements.end())) { // impossible |
Mark Salyzyn | 7fd6c5c | 2016-01-19 16:04:41 -0800 | [diff] [blame] | 518 | mLastSet[i] = false; |
Mark Salyzyn | 8fcfd85 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 519 | mLast[i] = mLogElements.begin(); |
Mark Salyzyn | 7fd6c5c | 2016-01-19 16:04:41 -0800 | [diff] [blame] | 520 | } else { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 521 | mLast[i] = it; // push down the road as next-best-watermark |
Mark Salyzyn | 7fd6c5c | 2016-01-19 16:04:41 -0800 | [diff] [blame] | 522 | } |
| 523 | } |
Mark Salyzyn | 507eb9f | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 524 | } |
| 525 | } |
Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 526 | #ifdef DEBUG_CHECK_FOR_STALE_ENTRIES |
| 527 | log_id_for_each(i) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 528 | for (auto b : mLastWorst[i]) { |
Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 529 | if (bad == b.second) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 530 | android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i, |
| 531 | b.first, key); |
Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 532 | } |
| 533 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 534 | for (auto b : mLastWorstPidOfSystem[i]) { |
Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 535 | if (bad == b.second) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 536 | android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i, |
| 537 | b.first); |
Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | if (mLastSet[i] && (bad == mLast[i])) { |
| 541 | android::prdebug("stale mLast[%d]\n", i); |
| 542 | mLastSet[i] = false; |
| 543 | mLast[i] = mLogElements.begin(); |
| 544 | } |
| 545 | } |
| 546 | #endif |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 547 | if (coalesce) { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 548 | stats.erase(element); |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 549 | } else { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 550 | stats.subtract(element); |
Mark Salyzyn | 831aa29 | 2015-09-03 16:08:50 -0700 | [diff] [blame] | 551 | } |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 552 | delete element; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 553 | |
| 554 | return it; |
| 555 | } |
| 556 | |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 557 | // Define a temporary mechanism to report the last LogBufferElement pointer |
| 558 | // for the specified uid, pid and tid. Used below to help merge-sort when |
| 559 | // pruning for worst UID. |
| 560 | class LogBufferElementKey { |
| 561 | const union { |
| 562 | struct { |
Mark Salyzyn | 684bdb5 | 2016-12-13 12:44:20 -0800 | [diff] [blame] | 563 | uint32_t uid; |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 564 | uint16_t pid; |
| 565 | uint16_t tid; |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 566 | } __packed; |
| 567 | uint64_t value; |
| 568 | } __packed; |
| 569 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 570 | public: |
| 571 | LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid) |
| 572 | : uid(uid), pid(pid), tid(tid) { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 573 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 574 | explicit LogBufferElementKey(uint64_t key) : value(key) { |
| 575 | } |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 576 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 577 | uint64_t getKey() { |
| 578 | return value; |
| 579 | } |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 580 | }; |
| 581 | |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 582 | class LogBufferElementLast { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 583 | typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap; |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 584 | LogBufferElementMap map; |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 585 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 586 | public: |
| 587 | bool coalesce(LogBufferElement* element, unsigned short dropped) { |
| 588 | LogBufferElementKey key(element->getUid(), element->getPid(), |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 589 | element->getTid()); |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 590 | LogBufferElementMap::iterator it = map.find(key.getKey()); |
| 591 | if (it != map.end()) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 592 | LogBufferElement* found = it->second; |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 593 | unsigned short moreDropped = found->getDropped(); |
| 594 | if ((dropped + moreDropped) > USHRT_MAX) { |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 595 | map.erase(it); |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 596 | } else { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 597 | found->setDropped(dropped + moreDropped); |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 598 | return true; |
| 599 | } |
| 600 | } |
| 601 | return false; |
| 602 | } |
| 603 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 604 | void add(LogBufferElement* element) { |
| 605 | LogBufferElementKey key(element->getUid(), element->getPid(), |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 606 | element->getTid()); |
| 607 | map[key.getKey()] = element; |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Mark Salyzyn | e06a6e0 | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 610 | inline void clear() { |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 611 | map.clear(); |
Mark Salyzyn | e06a6e0 | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 614 | void clear(LogBufferElement* element) { |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 615 | log_time current = |
| 616 | element->getRealTime() - log_time(EXPIRE_RATELIMIT, 0); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 617 | for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) { |
| 618 | LogBufferElement* mapElement = it->second; |
| 619 | if ((mapElement->getDropped() >= EXPIRE_THRESHOLD) && |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 620 | (current > mapElement->getRealTime())) { |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 621 | it = map.erase(it); |
| 622 | } else { |
| 623 | ++it; |
Mark Salyzyn | e06a6e0 | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | } |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 627 | }; |
| 628 | |
Mark Salyzyn | 0878a7c | 2017-05-11 13:28:33 -0700 | [diff] [blame] | 629 | // Determine if watermark is within pruneMargin + 1s from the end of the list, |
| 630 | // the caller will use this result to set an internal busy flag indicating |
| 631 | // the prune operation could not be completed because a reader is blocking |
| 632 | // the request. |
| 633 | bool LogBuffer::isBusy(log_time watermark) { |
| 634 | LogBufferElementCollection::iterator ei = mLogElements.end(); |
| 635 | --ei; |
| 636 | return watermark < ((*ei)->getRealTime() - pruneMargin - log_time(1, 0)); |
| 637 | } |
| 638 | |
| 639 | // If the selected reader is blocking our pruning progress, decide on |
| 640 | // what kind of mitigation is necessary to unblock the situation. |
| 641 | void LogBuffer::kickMe(LogTimeEntry* me, log_id_t id, unsigned long pruneRows) { |
| 642 | if (stats.sizes(id) > (2 * log_buffer_size(id))) { // +100% |
| 643 | // A misbehaving or slow reader has its connection |
| 644 | // dropped if we hit too much memory pressure. |
| 645 | me->release_Locked(); |
| 646 | } else if (me->mTimeout.tv_sec || me->mTimeout.tv_nsec) { |
| 647 | // Allow a blocked WRAP timeout reader to |
| 648 | // trigger and start reporting the log data. |
| 649 | me->triggerReader_Locked(); |
| 650 | } else { |
| 651 | // tell slow reader to skip entries to catch up |
| 652 | me->triggerSkip_Locked(id, pruneRows); |
| 653 | } |
| 654 | } |
| 655 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 656 | // prune "pruneRows" of type "id" from the buffer. |
| 657 | // |
Mark Salyzyn | 5bb2972 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 658 | // This garbage collection task is used to expire log entries. It is called to |
| 659 | // remove all logs (clear), all UID logs (unprivileged clear), or every |
| 660 | // 256 or 10% of the total logs (whichever is less) to prune the logs. |
| 661 | // |
| 662 | // First there is a prep phase where we discover the reader region lock that |
| 663 | // acts as a backstop to any pruning activity to stop there and go no further. |
| 664 | // |
| 665 | // There are three major pruning loops that follow. All expire from the oldest |
| 666 | // entries. Since there are multiple log buffers, the Android logging facility |
| 667 | // will appear to drop entries 'in the middle' when looking at multiple log |
| 668 | // sources and buffers. This effect is slightly more prominent when we prune |
| 669 | // the worst offender by logging source. Thus the logs slowly loose content |
| 670 | // and value as you move back in time. This is preferred since chatty sources |
| 671 | // invariably move the logs value down faster as less chatty sources would be |
| 672 | // expired in the noise. |
| 673 | // |
| 674 | // The first loop performs blacklisting and worst offender pruning. Falling |
| 675 | // through when there are no notable worst offenders and have not hit the |
| 676 | // region lock preventing further worst offender pruning. This loop also looks |
| 677 | // after managing the chatty log entries and merging to help provide |
| 678 | // statistical basis for blame. The chatty entries are not a notification of |
| 679 | // how much logs you may have, but instead represent how much logs you would |
| 680 | // have had in a virtual log buffer that is extended to cover all the in-memory |
| 681 | // logs without loss. They last much longer than the represented pruned logs |
| 682 | // since they get multiplied by the gains in the non-chatty log sources. |
| 683 | // |
| 684 | // The second loop get complicated because an algorithm of watermarks and |
| 685 | // history is maintained to reduce the order and keep processing time |
| 686 | // down to a minimum at scale. These algorithms can be costly in the face |
| 687 | // of larger log buffers, or severly limited processing time granted to a |
| 688 | // background task at lowest priority. |
| 689 | // |
| 690 | // This second loop does straight-up expiration from the end of the logs |
| 691 | // (again, remember for the specified log buffer id) but does some whitelist |
| 692 | // preservation. Thus whitelist is a Hail Mary low priority, blacklists and |
| 693 | // spam filtration all take priority. This second loop also checks if a region |
| 694 | // lock is causing us to buffer too much in the logs to help the reader(s), |
| 695 | // and will tell the slowest reader thread to skip log entries, and if |
| 696 | // persistent and hits a further threshold, kill the reader thread. |
| 697 | // |
| 698 | // The third thread is optional, and only gets hit if there was a whitelist |
| 699 | // and more needs to be pruned against the backstop of the region lock. |
| 700 | // |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 701 | // LogBuffer::wrlock() must be held when this function is called. |
Mark Salyzyn | 5bb2972 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 702 | // |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 703 | bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) { |
Mark Salyzyn | ae2abf1 | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 704 | LogTimeEntry* oldest = nullptr; |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 705 | bool busy = false; |
Mark Salyzyn | 2b25c66 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 706 | bool clearAll = pruneRows == ULONG_MAX; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 707 | |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 708 | LogTimeEntry::rdlock(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 709 | |
| 710 | // Region locked? |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 711 | LastLogTimes::iterator times = mTimes.begin(); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 712 | while (times != mTimes.end()) { |
| 713 | LogTimeEntry* entry = (*times); |
| 714 | if (entry->owned_Locked() && entry->isWatching(id) && |
| 715 | (!oldest || (oldest->mStart > entry->mStart) || |
| 716 | ((oldest->mStart == entry->mStart) && |
| 717 | (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 718 | oldest = entry; |
| 719 | } |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 720 | times++; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 721 | } |
Mark Salyzyn | 5836379 | 2017-04-17 12:46:12 -0700 | [diff] [blame] | 722 | log_time watermark(log_time::tv_sec_max, log_time::tv_nsec_max); |
| 723 | if (oldest) watermark = oldest->mStart - pruneMargin; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 724 | |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 725 | LogBufferElementCollection::iterator it; |
| 726 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 727 | if (__predict_false(caller_uid != AID_ROOT)) { // unlikely |
Mark Salyzyn | 43a5f31 | 2016-09-01 15:48:36 -0700 | [diff] [blame] | 728 | // Only here if clear all request from non system source, so chatty |
| 729 | // filter logistics is not required. |
Mark Salyzyn | 507eb9f | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 730 | it = mLastSet[id] ? mLast[id] : mLogElements.begin(); |
| 731 | while (it != mLogElements.end()) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 732 | LogBufferElement* element = *it; |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 733 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 734 | if ((element->getLogId() != id) || |
| 735 | (element->getUid() != caller_uid)) { |
Mark Salyzyn | 2b25c66 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 736 | ++it; |
| 737 | continue; |
| 738 | } |
| 739 | |
Mark Salyzyn | 507eb9f | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 740 | if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) { |
| 741 | mLast[id] = it; |
| 742 | mLastSet[id] = true; |
| 743 | } |
| 744 | |
Mark Salyzyn | 5836379 | 2017-04-17 12:46:12 -0700 | [diff] [blame] | 745 | if (oldest && (watermark <= element->getRealTime())) { |
Mark Salyzyn | 0878a7c | 2017-05-11 13:28:33 -0700 | [diff] [blame] | 746 | busy = isBusy(watermark); |
| 747 | if (busy) kickMe(oldest, id, pruneRows); |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 748 | break; |
| 749 | } |
| 750 | |
Mark Salyzyn | 2b25c66 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 751 | it = erase(it); |
Mark Salyzyn | 43a5f31 | 2016-09-01 15:48:36 -0700 | [diff] [blame] | 752 | if (--pruneRows == 0) { |
| 753 | break; |
| 754 | } |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 755 | } |
| 756 | LogTimeEntry::unlock(); |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 757 | return busy; |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 758 | } |
| 759 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 760 | // 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] | 761 | bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty(); |
Mark Salyzyn | 2b25c66 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 762 | while (!clearAll && (pruneRows > 0)) { |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 763 | // recalculate the worst offender on every batched pass |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 764 | int worst = -1; // not valid for getUid() or getKey() |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 765 | size_t worst_sizes = 0; |
| 766 | size_t second_worst_sizes = 0; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 767 | pid_t worstPid = 0; // POSIX guarantees PID != 0 |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 768 | |
Mark Salyzyn | ae76923 | 2015-03-17 17:17:25 -0700 | [diff] [blame] | 769 | if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) { |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 770 | // Calculate threshold as 12.5% of available storage |
| 771 | size_t threshold = log_buffer_size(id) / 8; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 772 | |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 773 | if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 774 | stats.sortTags(AID_ROOT, (pid_t)0, 2, id) |
| 775 | .findWorst(worst, worst_sizes, second_worst_sizes, |
| 776 | threshold); |
Mark Salyzyn | 8fcfd85 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 777 | // per-pid filter for AID_SYSTEM sources is too complex |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 778 | } else { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 779 | stats.sort(AID_ROOT, (pid_t)0, 2, id) |
| 780 | .findWorst(worst, worst_sizes, second_worst_sizes, |
| 781 | threshold); |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 782 | |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 783 | if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 784 | stats.sortPids(worst, (pid_t)0, 2, id) |
| 785 | .findWorst(worstPid, worst_sizes, second_worst_sizes); |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 786 | } |
| 787 | } |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 788 | } |
| 789 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 790 | // skip if we have neither worst nor naughty filters |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 791 | if ((worst == -1) && !hasBlacklist) { |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 792 | break; |
| 793 | } |
| 794 | |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 795 | bool kick = false; |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 796 | bool leading = true; |
Mark Salyzyn | 507eb9f | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 797 | it = mLastSet[id] ? mLast[id] : mLogElements.begin(); |
Mark Salyzyn | 5bb2972 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 798 | // Perform at least one mandatory garbage collection cycle in following |
| 799 | // - clear leading chatty tags |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 800 | // - coalesce chatty tags |
Mark Salyzyn | 5bb2972 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 801 | // - check age-out of preserved logs |
| 802 | bool gc = pruneRows <= 1; |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 803 | if (!gc && (worst != -1)) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 804 | { // begin scope for worst found iterator |
| 805 | LogBufferIteratorMap::iterator found = |
| 806 | mLastWorst[id].find(worst); |
| 807 | if ((found != mLastWorst[id].end()) && |
| 808 | (found->second != mLogElements.end())) { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 809 | leading = false; |
| 810 | it = found->second; |
| 811 | } |
| 812 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 813 | if (worstPid) { // begin scope for pid worst found iterator |
Mark Salyzyn | 8fcfd85 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 814 | // FYI: worstPid only set if !LOG_ID_EVENTS and |
| 815 | // !LOG_ID_SECURITY, not going to make that assumption ... |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 816 | LogBufferPidIteratorMap::iterator found = |
| 817 | mLastWorstPidOfSystem[id].find(worstPid); |
| 818 | if ((found != mLastWorstPidOfSystem[id].end()) && |
| 819 | (found->second != mLogElements.end())) { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 820 | leading = false; |
| 821 | it = found->second; |
| 822 | } |
Mark Salyzyn | c892ea3 | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 823 | } |
| 824 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 825 | static const timespec too_old = { EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 }; |
Mark Salyzyn | ccfe844 | 2015-08-24 13:43:27 -0700 | [diff] [blame] | 826 | LogBufferElementCollection::iterator lastt; |
| 827 | lastt = mLogElements.end(); |
| 828 | --lastt; |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 829 | LogBufferElementLast last; |
Mark Salyzyn | c892ea3 | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 830 | while (it != mLogElements.end()) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 831 | LogBufferElement* element = *it; |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 832 | |
Mark Salyzyn | 5836379 | 2017-04-17 12:46:12 -0700 | [diff] [blame] | 833 | if (oldest && (watermark <= element->getRealTime())) { |
Mark Salyzyn | 0878a7c | 2017-05-11 13:28:33 -0700 | [diff] [blame] | 834 | busy = isBusy(watermark); |
| 835 | // Do not let chatty eliding trigger any reader mitigation |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 836 | break; |
| 837 | } |
| 838 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 839 | if (element->getLogId() != id) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 840 | ++it; |
| 841 | continue; |
| 842 | } |
Mark Salyzyn | fa07f9d | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 843 | // below this point element->getLogId() == id |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 844 | |
Mark Salyzyn | 507eb9f | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 845 | if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) { |
| 846 | mLast[id] = it; |
| 847 | mLastSet[id] = true; |
| 848 | } |
| 849 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 850 | unsigned short dropped = element->getDropped(); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 851 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 852 | // remove any leading drops |
| 853 | if (leading && dropped) { |
| 854 | it = erase(it); |
| 855 | continue; |
| 856 | } |
| 857 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 858 | if (dropped && last.coalesce(element, dropped)) { |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 859 | it = erase(it, true); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 860 | continue; |
| 861 | } |
| 862 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 863 | int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) |
| 864 | ? element->getTag() |
| 865 | : element->getUid(); |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 866 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 867 | if (hasBlacklist && mPrune.naughty(element)) { |
| 868 | last.clear(element); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 869 | it = erase(it); |
| 870 | if (dropped) { |
| 871 | continue; |
| 872 | } |
| 873 | |
| 874 | pruneRows--; |
| 875 | if (pruneRows == 0) { |
| 876 | break; |
| 877 | } |
| 878 | |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 879 | if (key == worst) { |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 880 | kick = true; |
| 881 | if (worst_sizes < second_worst_sizes) { |
| 882 | break; |
| 883 | } |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 884 | worst_sizes -= element->getMsgLen(); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 885 | } |
| 886 | continue; |
| 887 | } |
| 888 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 889 | if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) || |
| 890 | (element->getRealTime() > (*lastt)->getRealTime())) { |
Mark Salyzyn | ccfe844 | 2015-08-24 13:43:27 -0700 | [diff] [blame] | 891 | break; |
| 892 | } |
| 893 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 894 | if (dropped) { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 895 | last.add(element); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 896 | if (worstPid && |
| 897 | ((!gc && (element->getPid() == worstPid)) || |
| 898 | (mLastWorstPidOfSystem[id].find(element->getPid()) == |
| 899 | mLastWorstPidOfSystem[id].end()))) { |
Mark Salyzyn | fa07f9d | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 900 | // element->getUid() may not be AID_SYSTEM, next best |
Mark Salyzyn | 8fcfd85 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 901 | // watermark if current one empty. id is not LOG_ID_EVENTS |
| 902 | // or LOG_ID_SECURITY because of worstPid check. |
Mark Salyzyn | 1eefca2 | 2016-09-01 07:28:44 -0700 | [diff] [blame] | 903 | mLastWorstPidOfSystem[id][element->getPid()] = it; |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 904 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 905 | if ((!gc && !worstPid && (key == worst)) || |
| 906 | (mLastWorst[id].find(key) == mLastWorst[id].end())) { |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 907 | mLastWorst[id][key] = it; |
Mark Salyzyn | 49afe0d | 2015-08-24 13:43:27 -0700 | [diff] [blame] | 908 | } |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 909 | ++it; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 910 | continue; |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 911 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 912 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 913 | if ((key != worst) || |
| 914 | (worstPid && (element->getPid() != worstPid))) { |
Mark Salyzyn | 5921276 | 2015-06-01 09:41:19 -0700 | [diff] [blame] | 915 | leading = false; |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 916 | last.clear(element); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 917 | ++it; |
| 918 | continue; |
| 919 | } |
Mark Salyzyn | fa07f9d | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 920 | // key == worst below here |
| 921 | // If worstPid set, then element->getPid() == worstPid below here |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 922 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 923 | pruneRows--; |
| 924 | if (pruneRows == 0) { |
| 925 | break; |
| 926 | } |
| 927 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 928 | kick = true; |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 929 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 930 | unsigned short len = element->getMsgLen(); |
Mark Salyzyn | 5392aac | 2015-05-22 10:03:31 -0700 | [diff] [blame] | 931 | |
| 932 | // do not create any leading drops |
| 933 | if (leading) { |
| 934 | it = erase(it); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 935 | } else { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 936 | stats.drop(element); |
| 937 | element->setDropped(1); |
| 938 | if (last.coalesce(element, 1)) { |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 939 | it = erase(it, true); |
Mark Salyzyn | 5392aac | 2015-05-22 10:03:31 -0700 | [diff] [blame] | 940 | } else { |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 941 | last.add(element); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 942 | if (worstPid && |
| 943 | (!gc || (mLastWorstPidOfSystem[id].find(worstPid) == |
| 944 | mLastWorstPidOfSystem[id].end()))) { |
Mark Salyzyn | fa07f9d | 2016-10-21 09:46:42 -0700 | [diff] [blame] | 945 | // element->getUid() may not be AID_SYSTEM, next best |
Mark Salyzyn | 8fcfd85 | 2016-10-24 08:20:26 -0700 | [diff] [blame] | 946 | // watermark if current one empty. id is not |
| 947 | // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid. |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 948 | mLastWorstPidOfSystem[id][worstPid] = it; |
| 949 | } |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 950 | if ((!gc && !worstPid) || |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 951 | (mLastWorst[id].find(worst) == mLastWorst[id].end())) { |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 952 | mLastWorst[id][worst] = it; |
Mark Salyzyn | 5bb2972 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 953 | } |
Mark Salyzyn | 5392aac | 2015-05-22 10:03:31 -0700 | [diff] [blame] | 954 | ++it; |
| 955 | } |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 956 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 957 | if (worst_sizes < second_worst_sizes) { |
| 958 | break; |
| 959 | } |
| 960 | worst_sizes -= len; |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 961 | } |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 962 | last.clear(); |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 963 | |
Mark Salyzyn | 1c95047 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 964 | if (!kick || !mPrune.worstUidEnabled()) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 965 | break; // the following loop will ask bad clients to skip/drop |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 969 | bool whitelist = false; |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 970 | bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll; |
Mark Salyzyn | 507eb9f | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 971 | it = mLastSet[id] ? mLast[id] : mLogElements.begin(); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 972 | while ((pruneRows > 0) && (it != mLogElements.end())) { |
| 973 | LogBufferElement* element = *it; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 974 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 975 | if (element->getLogId() != id) { |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 976 | it++; |
| 977 | continue; |
| 978 | } |
| 979 | |
Mark Salyzyn | 507eb9f | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 980 | if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) { |
| 981 | mLast[id] = it; |
| 982 | mLastSet[id] = true; |
| 983 | } |
| 984 | |
Mark Salyzyn | 5836379 | 2017-04-17 12:46:12 -0700 | [diff] [blame] | 985 | if (oldest && (watermark <= element->getRealTime())) { |
Mark Salyzyn | 0878a7c | 2017-05-11 13:28:33 -0700 | [diff] [blame] | 986 | busy = isBusy(watermark); |
| 987 | if (!whitelist && busy) kickMe(oldest, id, pruneRows); |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 988 | break; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 989 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 990 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 991 | if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) { |
| 992 | // WhiteListed |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 993 | whitelist = true; |
| 994 | it++; |
| 995 | continue; |
| 996 | } |
| 997 | |
| 998 | it = erase(it); |
| 999 | pruneRows--; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1000 | } |
| 1001 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 1002 | // Do not save the whitelist if we are reader range limited |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1003 | if (whitelist && (pruneRows > 0)) { |
Mark Salyzyn | 507eb9f | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 1004 | it = mLastSet[id] ? mLast[id] : mLogElements.begin(); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 1005 | while ((it != mLogElements.end()) && (pruneRows > 0)) { |
| 1006 | LogBufferElement* element = *it; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 1007 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 1008 | if (element->getLogId() != id) { |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 1009 | ++it; |
| 1010 | continue; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1011 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 1012 | |
Mark Salyzyn | 507eb9f | 2016-01-11 10:58:09 -0800 | [diff] [blame] | 1013 | if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) { |
| 1014 | mLast[id] = it; |
| 1015 | mLastSet[id] = true; |
| 1016 | } |
| 1017 | |
Mark Salyzyn | 5836379 | 2017-04-17 12:46:12 -0700 | [diff] [blame] | 1018 | if (oldest && (watermark <= element->getRealTime())) { |
Mark Salyzyn | 0878a7c | 2017-05-11 13:28:33 -0700 | [diff] [blame] | 1019 | busy = isBusy(watermark); |
| 1020 | if (busy) kickMe(oldest, id, pruneRows); |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 1021 | break; |
| 1022 | } |
| 1023 | |
| 1024 | it = erase(it); |
| 1025 | pruneRows--; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1026 | } |
| 1027 | } |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1028 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1029 | LogTimeEntry::unlock(); |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1030 | |
| 1031 | return (pruneRows > 0) && busy; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | // clear all rows of type "id" from the buffer. |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1035 | bool LogBuffer::clear(log_id_t id, uid_t uid) { |
| 1036 | bool busy = true; |
| 1037 | // If it takes more than 4 tries (seconds) to clear, then kill reader(s) |
| 1038 | for (int retry = 4;;) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 1039 | if (retry == 1) { // last pass |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1040 | // Check if it is still busy after the sleep, we say prune |
| 1041 | // one entry, not another clear run, so we are looking for |
| 1042 | // the quick side effect of the return value to tell us if |
| 1043 | // we have a _blocked_ reader. |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1044 | wrlock(); |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1045 | busy = prune(id, 1, uid); |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1046 | unlock(); |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1047 | // It is still busy, blocked reader(s), lets kill them all! |
| 1048 | // otherwise, lets be a good citizen and preserve the slow |
| 1049 | // readers and let the clear run (below) deal with determining |
| 1050 | // if we are still blocked and return an error code to caller. |
| 1051 | if (busy) { |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1052 | LogTimeEntry::wrlock(); |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1053 | LastLogTimes::iterator times = mTimes.begin(); |
| 1054 | while (times != mTimes.end()) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 1055 | LogTimeEntry* entry = (*times); |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1056 | // Killer punch |
| 1057 | if (entry->owned_Locked() && entry->isWatching(id)) { |
| 1058 | entry->release_Locked(); |
| 1059 | } |
| 1060 | times++; |
| 1061 | } |
| 1062 | LogTimeEntry::unlock(); |
| 1063 | } |
| 1064 | } |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1065 | wrlock(); |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1066 | busy = prune(id, ULONG_MAX, uid); |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1067 | unlock(); |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1068 | if (!busy || !--retry) { |
| 1069 | break; |
| 1070 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 1071 | sleep(1); // Let reader(s) catch up after notification |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1072 | } |
| 1073 | return busy; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | // get the used space associated with "id". |
| 1077 | unsigned long LogBuffer::getSizeUsed(log_id_t id) { |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1078 | rdlock(); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1079 | size_t retval = stats.sizes(id); |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1080 | unlock(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1081 | return retval; |
| 1082 | } |
| 1083 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1084 | // set the total space allocated to "id" |
| 1085 | int LogBuffer::setSize(log_id_t id, unsigned long size) { |
| 1086 | // Reasonable limits ... |
Mark Salyzyn | f10e273 | 2016-09-27 13:08:23 -0700 | [diff] [blame] | 1087 | if (!__android_logger_valid_buffer_size(size)) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1088 | return -1; |
| 1089 | } |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1090 | wrlock(); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1091 | log_buffer_size(id) = size; |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1092 | unlock(); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1093 | return 0; |
| 1094 | } |
| 1095 | |
| 1096 | // get the total space allocated to "id" |
| 1097 | unsigned long LogBuffer::getSize(log_id_t id) { |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1098 | rdlock(); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1099 | size_t retval = log_buffer_size(id); |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1100 | unlock(); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1101 | return retval; |
| 1102 | } |
| 1103 | |
Mark Salyzyn | ae2abf1 | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 1104 | log_time LogBuffer::flushTo(SocketClient* reader, const log_time& start, |
| 1105 | pid_t* lastTid, bool privileged, bool security, |
| 1106 | int (*filter)(const LogBufferElement* element, |
| 1107 | void* arg), |
| 1108 | void* arg) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1109 | LogBufferElementCollection::iterator it; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1110 | uid_t uid = reader->getUid(); |
| 1111 | |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1112 | rdlock(); |
Dragoslav Mitrinovic | 8e8e8db | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 1113 | |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 1114 | if (start == log_time::EPOCH) { |
Dragoslav Mitrinovic | 8e8e8db | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 1115 | // client wants to start from the beginning |
| 1116 | it = mLogElements.begin(); |
| 1117 | } else { |
Mark Salyzyn | 0f92fdc | 2017-04-04 10:57:41 -0700 | [diff] [blame] | 1118 | // 3 second limit to continue search for out-of-order entries. |
Mark Salyzyn | 5836379 | 2017-04-17 12:46:12 -0700 | [diff] [blame] | 1119 | log_time min = start - pruneMargin; |
| 1120 | |
Mark Salyzyn | 0f92fdc | 2017-04-04 10:57:41 -0700 | [diff] [blame] | 1121 | // Cap to 300 iterations we look back for out-of-order entries. |
| 1122 | size_t count = 300; |
Mark Salyzyn | 5836379 | 2017-04-17 12:46:12 -0700 | [diff] [blame] | 1123 | |
Dragoslav Mitrinovic | 8e8e8db | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 1124 | // Client wants to start from some specified time. Chances are |
| 1125 | // we are better off starting from the end of the time sorted list. |
Mark Salyzyn | 5836379 | 2017-04-17 12:46:12 -0700 | [diff] [blame] | 1126 | LogBufferElementCollection::iterator last; |
Mark Salyzyn | 1f46716 | 2017-03-27 13:12:41 -0700 | [diff] [blame] | 1127 | for (last = it = mLogElements.end(); it != mLogElements.begin(); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 1128 | /* do nothing */) { |
Dragoslav Mitrinovic | 8e8e8db | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 1129 | --it; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 1130 | LogBufferElement* element = *it; |
Mark Salyzyn | 3b941d4 | 2017-03-10 10:31:48 -0800 | [diff] [blame] | 1131 | if (element->getRealTime() > start) { |
| 1132 | last = it; |
Mark Salyzyn | 206ed8e | 2017-05-18 10:06:00 -0700 | [diff] [blame] | 1133 | } else if (element->getRealTime() == start) { |
| 1134 | last = ++it; |
| 1135 | break; |
Mark Salyzyn | 0f92fdc | 2017-04-04 10:57:41 -0700 | [diff] [blame] | 1136 | } else if (!--count || (element->getRealTime() < min)) { |
Dragoslav Mitrinovic | 8e8e8db | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 1137 | break; |
| 1138 | } |
| 1139 | } |
Mark Salyzyn | 3b941d4 | 2017-03-10 10:31:48 -0800 | [diff] [blame] | 1140 | it = last; |
Dragoslav Mitrinovic | 8e8e8db | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 1141 | } |
| 1142 | |
Mark Salyzyn | 206ed8e | 2017-05-18 10:06:00 -0700 | [diff] [blame] | 1143 | log_time curr = start; |
Mark Salyzyn | b5b8796 | 2017-01-23 14:20:31 -0800 | [diff] [blame] | 1144 | |
Mark Salyzyn | 3614a0c | 2017-04-17 12:46:12 -0700 | [diff] [blame] | 1145 | LogBufferElement* lastElement = nullptr; // iterator corruption paranoia |
| 1146 | static const size_t maxSkip = 4194304; // maximum entries to skip |
| 1147 | size_t skip = maxSkip; |
Dragoslav Mitrinovic | 8e8e8db | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 1148 | for (; it != mLogElements.end(); ++it) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 1149 | LogBufferElement* element = *it; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1150 | |
Mark Salyzyn | 3614a0c | 2017-04-17 12:46:12 -0700 | [diff] [blame] | 1151 | if (!--skip) { |
| 1152 | android::prdebug("reader.per: too many elements skipped"); |
| 1153 | break; |
| 1154 | } |
| 1155 | if (element == lastElement) { |
| 1156 | android::prdebug("reader.per: identical elements"); |
| 1157 | break; |
| 1158 | } |
| 1159 | lastElement = element; |
| 1160 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1161 | if (!privileged && (element->getUid() != uid)) { |
| 1162 | continue; |
| 1163 | } |
| 1164 | |
Mark Salyzyn | 8fa8896 | 2016-01-26 14:32:35 -0800 | [diff] [blame] | 1165 | if (!security && (element->getLogId() == LOG_ID_SECURITY)) { |
| 1166 | continue; |
| 1167 | } |
| 1168 | |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1169 | // NB: calling out to another object with wrlock() held (safe) |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 1170 | if (filter) { |
| 1171 | int ret = (*filter)(element, arg); |
| 1172 | if (ret == false) { |
| 1173 | continue; |
| 1174 | } |
| 1175 | if (ret != true) { |
| 1176 | break; |
| 1177 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1178 | } |
| 1179 | |
Mark Salyzyn | ae2abf1 | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 1180 | bool sameTid = false; |
| 1181 | if (lastTid) { |
| 1182 | sameTid = lastTid[element->getLogId()] == element->getTid(); |
| 1183 | // Dropped (chatty) immediately following a valid log from the |
| 1184 | // same source in the same log buffer indicates we have a |
| 1185 | // multiple identical squash. chatty that differs source |
| 1186 | // is due to spam filter. chatty to chatty of different |
| 1187 | // source is also due to spam filter. |
| 1188 | lastTid[element->getLogId()] = |
| 1189 | (element->getDropped() && !sameTid) ? 0 : element->getTid(); |
| 1190 | } |
Mark Salyzyn | b5b8796 | 2017-01-23 14:20:31 -0800 | [diff] [blame] | 1191 | |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1192 | unlock(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1193 | |
| 1194 | // range locking in LastLogTimes looks after us |
Mark Salyzyn | 206ed8e | 2017-05-18 10:06:00 -0700 | [diff] [blame] | 1195 | curr = element->flushTo(reader, this, privileged, sameTid); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1196 | |
Mark Salyzyn | 206ed8e | 2017-05-18 10:06:00 -0700 | [diff] [blame] | 1197 | if (curr == element->FLUSH_ERROR) { |
| 1198 | return curr; |
Mark Salyzyn | eb45db2 | 2017-05-17 19:55:12 +0000 | [diff] [blame] | 1199 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1200 | |
Mark Salyzyn | 3614a0c | 2017-04-17 12:46:12 -0700 | [diff] [blame] | 1201 | skip = maxSkip; |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1202 | rdlock(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1203 | } |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1204 | unlock(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1205 | |
Mark Salyzyn | 206ed8e | 2017-05-18 10:06:00 -0700 | [diff] [blame] | 1206 | return curr; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1207 | } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1208 | |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 1209 | std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid, |
| 1210 | unsigned int logMask) { |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1211 | wrlock(); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1212 | |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 1213 | std::string ret = stats.format(uid, pid, logMask); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1214 | |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 1215 | unlock(); |
Mark Salyzyn | 73160ac | 2015-08-20 10:01:44 -0700 | [diff] [blame] | 1216 | |
| 1217 | return ret; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1218 | } |