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