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 | 0e2fe42 | 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 | ec20776 | 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 | b0e8386 | 2015-12-14 13:07:12 -0800 | [diff] [blame] | 103 | if (!default_size) { |
Mark Salyzyn | 8510ef2 | 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 | b0e8386 | 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 | b7dd96e | 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 | 0e2fe42 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 139 | bool lastMonotonic = monotonic; |
Mark Salyzyn | 11a3e70 | 2015-12-01 15:57:25 -0800 | [diff] [blame] | 140 | monotonic = android_log_clockid() == CLOCK_MONOTONIC; |
Mark Salyzyn | c959c64 | 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 | 0e2fe42 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Mark Salyzyn | c959c64 | 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 | 0e2fe42 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 177 | // |
Mark Salyzyn | c959c64 | 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 | 0e2fe42 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 187 | } |
Mark Salyzyn | c959c64 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 188 | times++; |
Mark Salyzyn | 0e2fe42 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 189 | } |
Mark Salyzyn | c959c64 | 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 | 0e2fe42 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 194 | LogBuffer::LogBuffer(LastLogTimes *times): |
Mark Salyzyn | 11a3e70 | 2015-12-01 15:57:25 -0800 | [diff] [blame] | 195 | monotonic(android_log_clockid() == CLOCK_MONOTONIC), |
Mark Salyzyn | 0e2fe42 | 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 | 0dcfae6 | 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 | c55cc11 | 2016-01-06 21:17:43 +0000 | [diff] [blame] | 211 | if (log_id != LOG_ID_SECURITY) { |
Mark Salyzyn | c969086 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 212 | int prio = ANDROID_LOG_INFO; |
Mark Salyzyn | c55cc11 | 2016-01-06 21:17:43 +0000 | [diff] [blame] | 213 | const char *tag = NULL; |
Mark Salyzyn | c969086 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 214 | if (log_id == LOG_ID_EVENTS) { |
Mark Salyzyn | c55cc11 | 2016-01-06 21:17:43 +0000 | [diff] [blame] | 215 | tag = android::tagToName(elem->getTag()); |
Mark Salyzyn | c969086 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 216 | } else { |
| 217 | prio = *msg; |
| 218 | tag = msg + 1; |
| 219 | } |
Mark Salyzyn | c55cc11 | 2016-01-06 21:17:43 +0000 | [diff] [blame] | 220 | if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) { |
Mark Salyzyn | c969086 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 221 | // Log traffic received to total |
| 222 | pthread_mutex_lock(&mLogElementsLock); |
| 223 | stats.add(elem); |
| 224 | stats.subtract(elem); |
| 225 | pthread_mutex_unlock(&mLogElementsLock); |
| 226 | delete elem; |
| 227 | return -EACCES; |
| 228 | } |
Mark Salyzyn | 0dcfae6 | 2014-10-02 13:07:05 -0700 | [diff] [blame] | 229 | } |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 230 | |
| 231 | pthread_mutex_lock(&mLogElementsLock); |
| 232 | |
| 233 | // Insert elements in time sorted order if possible |
| 234 | // NB: if end is region locked, place element at end of list |
| 235 | LogBufferElementCollection::iterator it = mLogElements.end(); |
| 236 | LogBufferElementCollection::iterator last = it; |
Mark Salyzyn | a222a77 | 2014-10-13 16:49:47 -0700 | [diff] [blame] | 237 | while (last != mLogElements.begin()) { |
| 238 | --it; |
Mark Salyzyn | 5b7a8d8 | 2014-02-18 11:23:53 -0800 | [diff] [blame] | 239 | if ((*it)->getRealTime() <= realtime) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 240 | break; |
| 241 | } |
| 242 | last = it; |
| 243 | } |
Mark Salyzyn | 5b7a8d8 | 2014-02-18 11:23:53 -0800 | [diff] [blame] | 244 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 245 | if (last == mLogElements.end()) { |
| 246 | mLogElements.push_back(elem); |
| 247 | } else { |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 248 | uint64_t end = 1; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 249 | bool end_set = false; |
| 250 | bool end_always = false; |
| 251 | |
| 252 | LogTimeEntry::lock(); |
| 253 | |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 254 | LastLogTimes::iterator times = mTimes.begin(); |
| 255 | while(times != mTimes.end()) { |
| 256 | LogTimeEntry *entry = (*times); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 257 | if (entry->owned_Locked()) { |
| 258 | if (!entry->mNonBlock) { |
| 259 | end_always = true; |
| 260 | break; |
| 261 | } |
| 262 | if (!end_set || (end <= entry->mEnd)) { |
| 263 | end = entry->mEnd; |
| 264 | end_set = true; |
| 265 | } |
| 266 | } |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 267 | times++; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | if (end_always |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 271 | || (end_set && (end >= (*last)->getSequence()))) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 272 | mLogElements.push_back(elem); |
| 273 | } else { |
| 274 | mLogElements.insert(last,elem); |
| 275 | } |
| 276 | |
| 277 | LogTimeEntry::unlock(); |
| 278 | } |
| 279 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 280 | stats.add(elem); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 281 | maybePrune(log_id); |
| 282 | pthread_mutex_unlock(&mLogElementsLock); |
Mark Salyzyn | 88fe7cc | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 283 | |
| 284 | return len; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 285 | } |
| 286 | |
Mark Salyzyn | 5c5189a | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 287 | // 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] | 288 | // |
| 289 | // mLogElementsLock must be held when this function is called. |
| 290 | void LogBuffer::maybePrune(log_id_t id) { |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 291 | size_t sizes = stats.sizes(id); |
Mark Salyzyn | 5f19031 | 2015-08-10 10:23:56 -0700 | [diff] [blame] | 292 | unsigned long maxSize = log_buffer_size(id); |
| 293 | if (sizes > maxSize) { |
Mark Salyzyn | 185d8da | 2015-08-19 12:20:36 -0700 | [diff] [blame] | 294 | size_t sizeOver = sizes - ((maxSize * 9) / 10); |
Mark Salyzyn | 49858cc | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 295 | size_t elements = stats.realElements(id); |
| 296 | size_t minElements = elements / 100; |
| 297 | if (minElements < minPrune) { |
| 298 | minElements = minPrune; |
| 299 | } |
Mark Salyzyn | 5f19031 | 2015-08-10 10:23:56 -0700 | [diff] [blame] | 300 | unsigned long pruneRows = elements * sizeOver / sizes; |
Mark Salyzyn | 5c5189a | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 301 | if (pruneRows < minElements) { |
Mark Salyzyn | 5f19031 | 2015-08-10 10:23:56 -0700 | [diff] [blame] | 302 | pruneRows = minElements; |
Mark Salyzyn | 9d4e34e | 2014-01-13 16:37:51 -0800 | [diff] [blame] | 303 | } |
Mark Salyzyn | 5c5189a | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 304 | if (pruneRows > maxPrune) { |
| 305 | pruneRows = maxPrune; |
Mark Salyzyn | 185d8da | 2015-08-19 12:20:36 -0700 | [diff] [blame] | 306 | } |
Mark Salyzyn | 9d4e34e | 2014-01-13 16:37:51 -0800 | [diff] [blame] | 307 | prune(id, pruneRows); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
Mark Salyzyn | 1fdf71f | 2015-09-03 16:08:50 -0700 | [diff] [blame] | 311 | LogBufferElementCollection::iterator LogBuffer::erase( |
Mark Salyzyn | 5c5189a | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 312 | LogBufferElementCollection::iterator it, bool coalesce) { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 313 | LogBufferElement *element = *it; |
| 314 | log_id_t id = element->getLogId(); |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 315 | |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 316 | { // start of scope for uid found iterator |
| 317 | LogBufferIteratorMap::iterator found = |
| 318 | mLastWorstUid[id].find(element->getUid()); |
| 319 | if ((found != mLastWorstUid[id].end()) |
| 320 | && (it == found->second)) { |
| 321 | mLastWorstUid[id].erase(found); |
| 322 | } |
Mark Salyzyn | 5af07be | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 323 | } |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 324 | |
| 325 | if (element->getUid() == AID_SYSTEM) { |
| 326 | // start of scope for pid found iterator |
| 327 | LogBufferPidIteratorMap::iterator found = |
| 328 | mLastWorstPidOfSystem[id].find(element->getPid()); |
| 329 | if ((found != mLastWorstPidOfSystem[id].end()) |
| 330 | && (it == found->second)) { |
| 331 | mLastWorstPidOfSystem[id].erase(found); |
| 332 | } |
| 333 | } |
| 334 | |
Mark Salyzyn | b7dd96e | 2016-01-11 10:58:09 -0800 | [diff] [blame^] | 335 | bool setLast = mLastSet[id] && (it == mLast[id]); |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 336 | it = mLogElements.erase(it); |
Mark Salyzyn | b7dd96e | 2016-01-11 10:58:09 -0800 | [diff] [blame^] | 337 | if (setLast) { |
| 338 | if (it == mLogElements.end()) { // unlikely |
| 339 | mLastSet[id] = false; |
| 340 | } else { |
| 341 | mLast[id] = it; |
| 342 | } |
| 343 | } |
Mark Salyzyn | 5c5189a | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 344 | if (coalesce) { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 345 | stats.erase(element); |
Mark Salyzyn | 5c5189a | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 346 | } else { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 347 | stats.subtract(element); |
Mark Salyzyn | 1fdf71f | 2015-09-03 16:08:50 -0700 | [diff] [blame] | 348 | } |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 349 | delete element; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 350 | |
| 351 | return it; |
| 352 | } |
| 353 | |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 354 | // Define a temporary mechanism to report the last LogBufferElement pointer |
| 355 | // for the specified uid, pid and tid. Used below to help merge-sort when |
| 356 | // pruning for worst UID. |
| 357 | class LogBufferElementKey { |
| 358 | const union { |
| 359 | struct { |
| 360 | uint16_t uid; |
| 361 | uint16_t pid; |
| 362 | uint16_t tid; |
| 363 | uint16_t padding; |
| 364 | } __packed; |
| 365 | uint64_t value; |
| 366 | } __packed; |
| 367 | |
| 368 | public: |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 369 | LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid): |
| 370 | uid(uid), |
| 371 | pid(pid), |
| 372 | tid(tid), |
| 373 | padding(0) { |
| 374 | } |
| 375 | LogBufferElementKey(uint64_t key):value(key) { } |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 376 | |
| 377 | uint64_t getKey() { return value; } |
| 378 | }; |
| 379 | |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 380 | class LogBufferElementLast { |
| 381 | |
| 382 | typedef std::unordered_map<uint64_t, LogBufferElement *> LogBufferElementMap; |
| 383 | LogBufferElementMap map; |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 384 | |
| 385 | public: |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 386 | |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 387 | bool coalesce(LogBufferElement *element, unsigned short dropped) { |
| 388 | LogBufferElementKey key(element->getUid(), |
| 389 | element->getPid(), |
| 390 | element->getTid()); |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 391 | LogBufferElementMap::iterator it = map.find(key.getKey()); |
| 392 | if (it != map.end()) { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 393 | LogBufferElement *found = it->second; |
| 394 | unsigned short moreDropped = found->getDropped(); |
| 395 | if ((dropped + moreDropped) > USHRT_MAX) { |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 396 | map.erase(it); |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 397 | } else { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 398 | found->setDropped(dropped + moreDropped); |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 399 | return true; |
| 400 | } |
| 401 | } |
| 402 | return false; |
| 403 | } |
| 404 | |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 405 | void add(LogBufferElement *element) { |
| 406 | LogBufferElementKey key(element->getUid(), |
| 407 | element->getPid(), |
| 408 | element->getTid()); |
| 409 | map[key.getKey()] = element; |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Mark Salyzyn | be1a30c | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 412 | inline void clear() { |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 413 | map.clear(); |
Mark Salyzyn | be1a30c | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 416 | void clear(LogBufferElement *element) { |
| 417 | uint64_t current = element->getRealTime().nsec() |
Mark Salyzyn | 80c1fc6 | 2015-06-04 13:35:30 -0700 | [diff] [blame] | 418 | - (EXPIRE_RATELIMIT * NS_PER_SEC); |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 419 | for(LogBufferElementMap::iterator it = map.begin(); it != map.end();) { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 420 | LogBufferElement *mapElement = it->second; |
| 421 | if ((mapElement->getDropped() >= EXPIRE_THRESHOLD) |
| 422 | && (current > mapElement->getRealTime().nsec())) { |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 423 | it = map.erase(it); |
| 424 | } else { |
| 425 | ++it; |
Mark Salyzyn | be1a30c | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | } |
| 429 | |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 430 | }; |
| 431 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 432 | // prune "pruneRows" of type "id" from the buffer. |
| 433 | // |
Mark Salyzyn | e2827d5 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 434 | // This garbage collection task is used to expire log entries. It is called to |
| 435 | // remove all logs (clear), all UID logs (unprivileged clear), or every |
| 436 | // 256 or 10% of the total logs (whichever is less) to prune the logs. |
| 437 | // |
| 438 | // First there is a prep phase where we discover the reader region lock that |
| 439 | // acts as a backstop to any pruning activity to stop there and go no further. |
| 440 | // |
| 441 | // There are three major pruning loops that follow. All expire from the oldest |
| 442 | // entries. Since there are multiple log buffers, the Android logging facility |
| 443 | // will appear to drop entries 'in the middle' when looking at multiple log |
| 444 | // sources and buffers. This effect is slightly more prominent when we prune |
| 445 | // the worst offender by logging source. Thus the logs slowly loose content |
| 446 | // and value as you move back in time. This is preferred since chatty sources |
| 447 | // invariably move the logs value down faster as less chatty sources would be |
| 448 | // expired in the noise. |
| 449 | // |
| 450 | // The first loop performs blacklisting and worst offender pruning. Falling |
| 451 | // through when there are no notable worst offenders and have not hit the |
| 452 | // region lock preventing further worst offender pruning. This loop also looks |
| 453 | // after managing the chatty log entries and merging to help provide |
| 454 | // statistical basis for blame. The chatty entries are not a notification of |
| 455 | // how much logs you may have, but instead represent how much logs you would |
| 456 | // have had in a virtual log buffer that is extended to cover all the in-memory |
| 457 | // logs without loss. They last much longer than the represented pruned logs |
| 458 | // since they get multiplied by the gains in the non-chatty log sources. |
| 459 | // |
| 460 | // The second loop get complicated because an algorithm of watermarks and |
| 461 | // history is maintained to reduce the order and keep processing time |
| 462 | // down to a minimum at scale. These algorithms can be costly in the face |
| 463 | // of larger log buffers, or severly limited processing time granted to a |
| 464 | // background task at lowest priority. |
| 465 | // |
| 466 | // This second loop does straight-up expiration from the end of the logs |
| 467 | // (again, remember for the specified log buffer id) but does some whitelist |
| 468 | // preservation. Thus whitelist is a Hail Mary low priority, blacklists and |
| 469 | // spam filtration all take priority. This second loop also checks if a region |
| 470 | // lock is causing us to buffer too much in the logs to help the reader(s), |
| 471 | // and will tell the slowest reader thread to skip log entries, and if |
| 472 | // persistent and hits a further threshold, kill the reader thread. |
| 473 | // |
| 474 | // The third thread is optional, and only gets hit if there was a whitelist |
| 475 | // and more needs to be pruned against the backstop of the region lock. |
| 476 | // |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 477 | // mLogElementsLock must be held when this function is called. |
Mark Salyzyn | e2827d5 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 478 | // |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 479 | 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] | 480 | LogTimeEntry *oldest = NULL; |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 481 | bool busy = false; |
Mark Salyzyn | 71541f3 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 482 | bool clearAll = pruneRows == ULONG_MAX; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 483 | |
| 484 | LogTimeEntry::lock(); |
| 485 | |
| 486 | // Region locked? |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 487 | LastLogTimes::iterator times = mTimes.begin(); |
| 488 | while(times != mTimes.end()) { |
| 489 | LogTimeEntry *entry = (*times); |
TraianX Schiau | 6ba427e | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 490 | if (entry->owned_Locked() && entry->isWatching(id) |
Mark Salyzyn | c959c64 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 491 | && (!oldest || |
| 492 | (oldest->mStart > entry->mStart) || |
| 493 | ((oldest->mStart == entry->mStart) && |
| 494 | (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 495 | oldest = entry; |
| 496 | } |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 497 | times++; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 498 | } |
| 499 | |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 500 | LogBufferElementCollection::iterator it; |
| 501 | |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 502 | if (caller_uid != AID_ROOT) { |
Mark Salyzyn | 71541f3 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 503 | // Only here if clearAll condition (pruneRows == ULONG_MAX) |
Mark Salyzyn | b7dd96e | 2016-01-11 10:58:09 -0800 | [diff] [blame^] | 504 | it = mLastSet[id] ? mLast[id] : mLogElements.begin(); |
| 505 | while (it != mLogElements.end()) { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 506 | LogBufferElement *element = *it; |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 507 | |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 508 | if ((element->getLogId() != id) || (element->getUid() != caller_uid)) { |
Mark Salyzyn | 71541f3 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 509 | ++it; |
| 510 | continue; |
| 511 | } |
| 512 | |
Mark Salyzyn | b7dd96e | 2016-01-11 10:58:09 -0800 | [diff] [blame^] | 513 | if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) { |
| 514 | mLast[id] = it; |
| 515 | mLastSet[id] = true; |
| 516 | } |
| 517 | |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 518 | if (oldest && (oldest->mStart <= element->getSequence())) { |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 519 | busy = true; |
Mark Salyzyn | c959c64 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 520 | if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) { |
| 521 | oldest->triggerReader_Locked(); |
| 522 | } else { |
| 523 | oldest->triggerSkip_Locked(id, pruneRows); |
| 524 | } |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 525 | break; |
| 526 | } |
| 527 | |
Mark Salyzyn | 71541f3 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 528 | it = erase(it); |
| 529 | pruneRows--; |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 530 | } |
| 531 | LogTimeEntry::unlock(); |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 532 | return busy; |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 535 | // prune by worst offenders; by blacklist, UID, and by PID of system UID |
Mark Salyzyn | c969086 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 536 | bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty(); |
Mark Salyzyn | 71541f3 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 537 | while (!clearAll && (pruneRows > 0)) { |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 538 | // recalculate the worst offender on every batched pass |
| 539 | uid_t worst = (uid_t) -1; |
| 540 | size_t worst_sizes = 0; |
| 541 | size_t second_worst_sizes = 0; |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 542 | pid_t worstPid = 0; // POSIX guarantees PID != 0 |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 543 | |
Mark Salyzyn | 0e765c6 | 2015-03-17 17:17:25 -0700 | [diff] [blame] | 544 | if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 545 | { // begin scope for UID sorted list |
| 546 | std::unique_ptr<const UidEntry *[]> sorted = stats.sort( |
| 547 | AID_ROOT, (pid_t)0, 2, id); |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 548 | |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 549 | if (sorted.get() && sorted[0] && sorted[1]) { |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 550 | worst_sizes = sorted[0]->getSizes(); |
Mark Salyzyn | 84a8398 | 2015-04-20 15:36:12 -0700 | [diff] [blame] | 551 | // Calculate threshold as 12.5% of available storage |
| 552 | size_t threshold = log_buffer_size(id) / 8; |
Mark Salyzyn | 8b3b07f | 2015-10-12 13:45:51 -0700 | [diff] [blame] | 553 | if ((worst_sizes > threshold) |
| 554 | // Allow time horizon to extend roughly tenfold, assume |
| 555 | // average entry length is 100 characters. |
| 556 | && (worst_sizes > (10 * sorted[0]->getDropped()))) { |
Mark Salyzyn | 84a8398 | 2015-04-20 15:36:12 -0700 | [diff] [blame] | 557 | worst = sorted[0]->getKey(); |
| 558 | second_worst_sizes = sorted[1]->getSizes(); |
| 559 | if (second_worst_sizes < threshold) { |
| 560 | second_worst_sizes = threshold; |
| 561 | } |
| 562 | } |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 563 | } |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 564 | } |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 565 | |
| 566 | if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) { |
| 567 | // begin scope of PID sorted list |
| 568 | std::unique_ptr<const PidEntry *[]> sorted = stats.sort( |
| 569 | worst, (pid_t)0, 2, id, worst); |
| 570 | if (sorted.get() && sorted[0] && sorted[1]) { |
| 571 | worstPid = sorted[0]->getKey(); |
| 572 | second_worst_sizes = worst_sizes |
| 573 | - sorted[0]->getSizes() |
| 574 | + sorted[1]->getSizes(); |
| 575 | } |
| 576 | } |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 577 | } |
| 578 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 579 | // skip if we have neither worst nor naughty filters |
| 580 | if ((worst == (uid_t) -1) && !hasBlacklist) { |
| 581 | break; |
| 582 | } |
| 583 | |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 584 | bool kick = false; |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 585 | bool leading = true; |
Mark Salyzyn | b7dd96e | 2016-01-11 10:58:09 -0800 | [diff] [blame^] | 586 | it = mLastSet[id] ? mLast[id] : mLogElements.begin(); |
Mark Salyzyn | e2827d5 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 587 | // Perform at least one mandatory garbage collection cycle in following |
| 588 | // - clear leading chatty tags |
Mark Salyzyn | 5c5189a | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 589 | // - coalesce chatty tags |
Mark Salyzyn | e2827d5 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 590 | // - check age-out of preserved logs |
| 591 | bool gc = pruneRows <= 1; |
| 592 | if (!gc && (worst != (uid_t) -1)) { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 593 | { // begin scope for uid worst found iterator |
| 594 | LogBufferIteratorMap::iterator found = mLastWorstUid[id].find(worst); |
| 595 | if ((found != mLastWorstUid[id].end()) |
| 596 | && (found->second != mLogElements.end())) { |
| 597 | leading = false; |
| 598 | it = found->second; |
| 599 | } |
| 600 | } |
| 601 | if (worstPid) { |
| 602 | // begin scope for pid worst found iterator |
| 603 | LogBufferPidIteratorMap::iterator found |
| 604 | = mLastWorstPidOfSystem[id].find(worstPid); |
| 605 | if ((found != mLastWorstPidOfSystem[id].end()) |
| 606 | && (found->second != mLogElements.end())) { |
| 607 | leading = false; |
| 608 | it = found->second; |
| 609 | } |
Mark Salyzyn | 5af07be | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 610 | } |
| 611 | } |
Mark Salyzyn | 7f62d6b | 2015-08-24 13:43:27 -0700 | [diff] [blame] | 612 | static const timespec too_old = { |
| 613 | EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 |
| 614 | }; |
| 615 | LogBufferElementCollection::iterator lastt; |
| 616 | lastt = mLogElements.end(); |
| 617 | --lastt; |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 618 | LogBufferElementLast last; |
Mark Salyzyn | 5af07be | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 619 | while (it != mLogElements.end()) { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 620 | LogBufferElement *element = *it; |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 621 | |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 622 | if (oldest && (oldest->mStart <= element->getSequence())) { |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 623 | busy = true; |
Mark Salyzyn | c959c64 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 624 | if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) { |
| 625 | oldest->triggerReader_Locked(); |
| 626 | } |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 627 | break; |
| 628 | } |
| 629 | |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 630 | if (element->getLogId() != id) { |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 631 | ++it; |
| 632 | continue; |
| 633 | } |
| 634 | |
Mark Salyzyn | b7dd96e | 2016-01-11 10:58:09 -0800 | [diff] [blame^] | 635 | if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) { |
| 636 | mLast[id] = it; |
| 637 | mLastSet[id] = true; |
| 638 | } |
| 639 | |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 640 | unsigned short dropped = element->getDropped(); |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 641 | |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 642 | // remove any leading drops |
| 643 | if (leading && dropped) { |
| 644 | it = erase(it); |
| 645 | continue; |
| 646 | } |
| 647 | |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 648 | if (dropped && last.coalesce(element, dropped)) { |
Mark Salyzyn | 5c5189a | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 649 | it = erase(it, true); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 650 | continue; |
| 651 | } |
| 652 | |
Mark Salyzyn | edcbdfa | 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 | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 665 | if (element->getUid() == 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 | edcbdfa | 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 | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 675 | if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) |
| 676 | || (element->getRealTime() > (*lastt)->getRealTime())) { |
Mark Salyzyn | 7f62d6b | 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 | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 681 | last.add(element); |
| 682 | if (worstPid |
| 683 | && ((!gc && (element->getPid() == worstPid)) |
| 684 | || (mLastWorstPidOfSystem[id].find(element->getPid()) |
| 685 | == mLastWorstPidOfSystem[id].end()))) { |
| 686 | mLastWorstPidOfSystem[id][element->getUid()] = it; |
| 687 | } |
| 688 | if ((!gc && !worstPid && (element->getUid() == worst)) |
| 689 | || (mLastWorstUid[id].find(element->getUid()) |
Mark Salyzyn | 76aab49 | 2015-08-24 13:43:27 -0700 | [diff] [blame] | 690 | == mLastWorstUid[id].end())) { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 691 | mLastWorstUid[id][element->getUid()] = it; |
Mark Salyzyn | 76aab49 | 2015-08-24 13:43:27 -0700 | [diff] [blame] | 692 | } |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 693 | ++it; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 694 | continue; |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 695 | } |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 696 | |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 697 | if ((element->getUid() != worst) |
| 698 | || (worstPid && (element->getPid() != worstPid))) { |
Mark Salyzyn | fd318a8 | 2015-06-01 09:41:19 -0700 | [diff] [blame] | 699 | leading = false; |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 700 | last.clear(element); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 701 | ++it; |
| 702 | continue; |
| 703 | } |
| 704 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 705 | pruneRows--; |
| 706 | if (pruneRows == 0) { |
| 707 | break; |
| 708 | } |
| 709 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 710 | kick = true; |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 711 | |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 712 | unsigned short len = element->getMsgLen(); |
Mark Salyzyn | 65ec860 | 2015-05-22 10:03:31 -0700 | [diff] [blame] | 713 | |
| 714 | // do not create any leading drops |
| 715 | if (leading) { |
| 716 | it = erase(it); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 717 | } else { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 718 | stats.drop(element); |
| 719 | element->setDropped(1); |
| 720 | if (last.coalesce(element, 1)) { |
Mark Salyzyn | 5c5189a | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 721 | it = erase(it, true); |
Mark Salyzyn | 65ec860 | 2015-05-22 10:03:31 -0700 | [diff] [blame] | 722 | } else { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 723 | last.add(element); |
| 724 | if (worstPid && (!gc |
| 725 | || (mLastWorstPidOfSystem[id].find(worstPid) |
| 726 | == mLastWorstPidOfSystem[id].end()))) { |
| 727 | mLastWorstPidOfSystem[id][worstPid] = it; |
| 728 | } |
| 729 | if ((!gc && !worstPid) || (mLastWorstUid[id].find(worst) |
Mark Salyzyn | e2827d5 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 730 | == mLastWorstUid[id].end())) { |
| 731 | mLastWorstUid[id][worst] = it; |
| 732 | } |
Mark Salyzyn | 65ec860 | 2015-05-22 10:03:31 -0700 | [diff] [blame] | 733 | ++it; |
| 734 | } |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 735 | } |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 736 | if (worst_sizes < second_worst_sizes) { |
| 737 | break; |
| 738 | } |
| 739 | worst_sizes -= len; |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 740 | } |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 741 | last.clear(); |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 742 | |
Mark Salyzyn | c402bc7 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 743 | if (!kick || !mPrune.worstUidEnabled()) { |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 744 | break; // the following loop will ask bad clients to skip/drop |
| 745 | } |
| 746 | } |
| 747 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 748 | bool whitelist = false; |
Mark Salyzyn | c969086 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 749 | bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll; |
Mark Salyzyn | b7dd96e | 2016-01-11 10:58:09 -0800 | [diff] [blame^] | 750 | it = mLastSet[id] ? mLast[id] : mLogElements.begin(); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 751 | while((pruneRows > 0) && (it != mLogElements.end())) { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 752 | LogBufferElement *element = *it; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 753 | |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 754 | if (element->getLogId() != id) { |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 755 | it++; |
| 756 | continue; |
| 757 | } |
| 758 | |
Mark Salyzyn | b7dd96e | 2016-01-11 10:58:09 -0800 | [diff] [blame^] | 759 | if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) { |
| 760 | mLast[id] = it; |
| 761 | mLastSet[id] = true; |
| 762 | } |
| 763 | |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 764 | if (oldest && (oldest->mStart <= element->getSequence())) { |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 765 | busy = true; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 766 | if (whitelist) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 767 | break; |
| 768 | } |
Mark Salyzyn | c402bc7 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 769 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 770 | if (stats.sizes(id) > (2 * log_buffer_size(id))) { |
| 771 | // kick a misbehaving log reader client off the island |
| 772 | oldest->release_Locked(); |
Mark Salyzyn | c959c64 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 773 | } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) { |
| 774 | oldest->triggerReader_Locked(); |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 775 | } else { |
| 776 | oldest->triggerSkip_Locked(id, pruneRows); |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 777 | } |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 778 | break; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 779 | } |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 780 | |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 781 | if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) { |
| 782 | // WhiteListed |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 783 | whitelist = true; |
| 784 | it++; |
| 785 | continue; |
| 786 | } |
| 787 | |
| 788 | it = erase(it); |
| 789 | pruneRows--; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 790 | } |
| 791 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 792 | // Do not save the whitelist if we are reader range limited |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 793 | if (whitelist && (pruneRows > 0)) { |
Mark Salyzyn | b7dd96e | 2016-01-11 10:58:09 -0800 | [diff] [blame^] | 794 | it = mLastSet[id] ? mLast[id] : mLogElements.begin(); |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 795 | while((it != mLogElements.end()) && (pruneRows > 0)) { |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 796 | LogBufferElement *element = *it; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 797 | |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 798 | if (element->getLogId() != id) { |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 799 | ++it; |
| 800 | continue; |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 801 | } |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 802 | |
Mark Salyzyn | b7dd96e | 2016-01-11 10:58:09 -0800 | [diff] [blame^] | 803 | if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) { |
| 804 | mLast[id] = it; |
| 805 | mLastSet[id] = true; |
| 806 | } |
| 807 | |
Mark Salyzyn | edcbdfa | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 808 | if (oldest && (oldest->mStart <= element->getSequence())) { |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 809 | busy = true; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 810 | if (stats.sizes(id) > (2 * log_buffer_size(id))) { |
| 811 | // kick a misbehaving log reader client off the island |
| 812 | oldest->release_Locked(); |
Mark Salyzyn | c959c64 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 813 | } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) { |
| 814 | oldest->triggerReader_Locked(); |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 815 | } else { |
| 816 | oldest->triggerSkip_Locked(id, pruneRows); |
| 817 | } |
| 818 | break; |
| 819 | } |
| 820 | |
| 821 | it = erase(it); |
| 822 | pruneRows--; |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 823 | } |
| 824 | } |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 825 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 826 | LogTimeEntry::unlock(); |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 827 | |
| 828 | return (pruneRows > 0) && busy; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | // clear all rows of type "id" from the buffer. |
Mark Salyzyn | 8cf0d03 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 832 | bool LogBuffer::clear(log_id_t id, uid_t uid) { |
| 833 | bool busy = true; |
| 834 | // If it takes more than 4 tries (seconds) to clear, then kill reader(s) |
| 835 | for (int retry = 4;;) { |
| 836 | if (retry == 1) { // last pass |
| 837 | // Check if it is still busy after the sleep, we say prune |
| 838 | // one entry, not another clear run, so we are looking for |
| 839 | // the quick side effect of the return value to tell us if |
| 840 | // we have a _blocked_ reader. |
| 841 | pthread_mutex_lock(&mLogElementsLock); |
| 842 | busy = prune(id, 1, uid); |
| 843 | pthread_mutex_unlock(&mLogElementsLock); |
| 844 | // It is still busy, blocked reader(s), lets kill them all! |
| 845 | // otherwise, lets be a good citizen and preserve the slow |
| 846 | // readers and let the clear run (below) deal with determining |
| 847 | // if we are still blocked and return an error code to caller. |
| 848 | if (busy) { |
| 849 | LogTimeEntry::lock(); |
| 850 | LastLogTimes::iterator times = mTimes.begin(); |
| 851 | while (times != mTimes.end()) { |
| 852 | LogTimeEntry *entry = (*times); |
| 853 | // Killer punch |
| 854 | if (entry->owned_Locked() && entry->isWatching(id)) { |
| 855 | entry->release_Locked(); |
| 856 | } |
| 857 | times++; |
| 858 | } |
| 859 | LogTimeEntry::unlock(); |
| 860 | } |
| 861 | } |
| 862 | pthread_mutex_lock(&mLogElementsLock); |
| 863 | busy = prune(id, ULONG_MAX, uid); |
| 864 | pthread_mutex_unlock(&mLogElementsLock); |
| 865 | if (!busy || !--retry) { |
| 866 | break; |
| 867 | } |
| 868 | sleep (1); // Let reader(s) catch up after notification |
| 869 | } |
| 870 | return busy; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | // get the used space associated with "id". |
| 874 | unsigned long LogBuffer::getSizeUsed(log_id_t id) { |
| 875 | pthread_mutex_lock(&mLogElementsLock); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 876 | size_t retval = stats.sizes(id); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 877 | pthread_mutex_unlock(&mLogElementsLock); |
| 878 | return retval; |
| 879 | } |
| 880 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 881 | // set the total space allocated to "id" |
| 882 | int LogBuffer::setSize(log_id_t id, unsigned long size) { |
| 883 | // Reasonable limits ... |
Mark Salyzyn | 7b85c59 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 884 | if (!valid_size(size)) { |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 885 | return -1; |
| 886 | } |
| 887 | pthread_mutex_lock(&mLogElementsLock); |
| 888 | log_buffer_size(id) = size; |
| 889 | pthread_mutex_unlock(&mLogElementsLock); |
| 890 | return 0; |
| 891 | } |
| 892 | |
| 893 | // get the total space allocated to "id" |
| 894 | unsigned long LogBuffer::getSize(log_id_t id) { |
| 895 | pthread_mutex_lock(&mLogElementsLock); |
| 896 | size_t retval = log_buffer_size(id); |
| 897 | pthread_mutex_unlock(&mLogElementsLock); |
| 898 | return retval; |
| 899 | } |
| 900 | |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 901 | uint64_t LogBuffer::flushTo( |
| 902 | SocketClient *reader, const uint64_t start, bool privileged, |
| 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 | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 933 | if (element->getSequence() <= start) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 934 | continue; |
| 935 | } |
| 936 | |
| 937 | // NB: calling out to another object with mLogElementsLock held (safe) |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 938 | if (filter) { |
| 939 | int ret = (*filter)(element, arg); |
| 940 | if (ret == false) { |
| 941 | continue; |
| 942 | } |
| 943 | if (ret != true) { |
| 944 | break; |
| 945 | } |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | pthread_mutex_unlock(&mLogElementsLock); |
| 949 | |
| 950 | // range locking in LastLogTimes looks after us |
Mark Salyzyn | 069c25d | 2015-12-03 15:38:35 -0800 | [diff] [blame] | 951 | max = element->flushTo(reader, this, privileged); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 952 | |
| 953 | if (max == element->FLUSH_ERROR) { |
| 954 | return max; |
| 955 | } |
| 956 | |
| 957 | pthread_mutex_lock(&mLogElementsLock); |
| 958 | } |
| 959 | pthread_mutex_unlock(&mLogElementsLock); |
| 960 | |
| 961 | return max; |
| 962 | } |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 963 | |
Mark Salyzyn | 4a2fc1c | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 964 | std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid, |
| 965 | unsigned int logMask) { |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 966 | pthread_mutex_lock(&mLogElementsLock); |
| 967 | |
Mark Salyzyn | 4a2fc1c | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 968 | std::string ret = stats.format(uid, pid, logMask); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 969 | |
| 970 | pthread_mutex_unlock(&mLogElementsLock); |
Mark Salyzyn | 7c528d9 | 2015-08-20 10:01:44 -0700 | [diff] [blame] | 971 | |
| 972 | return ret; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 973 | } |