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