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