Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012-2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 17 | #include <ctype.h> |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 18 | #include <errno.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <string.h> |
Mark Salyzyn | 57a0af9 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 21 | #include <sys/user.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 22 | #include <time.h> |
| 23 | #include <unistd.h> |
| 24 | |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 25 | #include <unordered_map> |
| 26 | |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 27 | #include <cutils/properties.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 28 | #include <log/logger.h> |
| 29 | |
| 30 | #include "LogBuffer.h" |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 31 | #include "LogReader.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 32 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 33 | // Default |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 34 | #define LOG_BUFFER_SIZE (256 * 1024) // Tuned on a per-platform basis here? |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 35 | #define log_buffer_size(id) mMaxSize[id] |
Mark Salyzyn | 57a0af9 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 36 | #define LOG_BUFFER_MIN_SIZE (64 * 1024UL) |
| 37 | #define LOG_BUFFER_MAX_SIZE (256 * 1024 * 1024UL) |
| 38 | |
| 39 | static bool valid_size(unsigned long value) { |
| 40 | if ((value < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < value)) { |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | long pages = sysconf(_SC_PHYS_PAGES); |
| 45 | if (pages < 1) { |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | long pagesize = sysconf(_SC_PAGESIZE); |
| 50 | if (pagesize <= 1) { |
| 51 | pagesize = PAGE_SIZE; |
| 52 | } |
| 53 | |
| 54 | // maximum memory impact a somewhat arbitrary ~3% |
| 55 | pages = (pages + 31) / 32; |
| 56 | unsigned long maximum = pages * pagesize; |
| 57 | |
| 58 | if ((maximum < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < maximum)) { |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | return value <= maximum; |
| 63 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 64 | |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 65 | static unsigned long property_get_size(const char *key) { |
| 66 | char property[PROPERTY_VALUE_MAX]; |
| 67 | property_get(key, property, ""); |
| 68 | |
| 69 | char *cp; |
| 70 | unsigned long value = strtoul(property, &cp, 10); |
| 71 | |
| 72 | switch(*cp) { |
| 73 | case 'm': |
| 74 | case 'M': |
| 75 | value *= 1024; |
| 76 | /* FALLTHRU */ |
| 77 | case 'k': |
| 78 | case 'K': |
| 79 | value *= 1024; |
| 80 | /* FALLTHRU */ |
| 81 | case '\0': |
| 82 | break; |
| 83 | |
| 84 | default: |
| 85 | value = 0; |
| 86 | } |
| 87 | |
Mark Salyzyn | 57a0af9 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 88 | if (!valid_size(value)) { |
| 89 | value = 0; |
| 90 | } |
| 91 | |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 92 | return value; |
| 93 | } |
| 94 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 95 | void LogBuffer::init() { |
Mark Salyzyn | 57a0af9 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 96 | static const char global_tuneable[] = "persist.logd.size"; // Settings App |
| 97 | static const char global_default[] = "ro.logd.size"; // BoardConfig.mk |
| 98 | |
| 99 | unsigned long default_size = property_get_size(global_tuneable); |
| 100 | if (!default_size) { |
| 101 | default_size = property_get_size(global_default); |
| 102 | } |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 103 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 104 | log_id_for_each(i) { |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 105 | char key[PROP_NAME_MAX]; |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 106 | |
Mark Salyzyn | 57a0af9 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 107 | snprintf(key, sizeof(key), "%s.%s", |
| 108 | global_tuneable, android_log_id_to_name(i)); |
| 109 | unsigned long property_size = property_get_size(key); |
| 110 | |
| 111 | if (!property_size) { |
| 112 | snprintf(key, sizeof(key), "%s.%s", |
| 113 | global_default, android_log_id_to_name(i)); |
| 114 | property_size = property_get_size(key); |
| 115 | } |
| 116 | |
| 117 | if (!property_size) { |
| 118 | property_size = default_size; |
| 119 | } |
| 120 | |
| 121 | if (!property_size) { |
| 122 | property_size = LOG_BUFFER_SIZE; |
| 123 | } |
| 124 | |
| 125 | if (setSize(i, property_size)) { |
| 126 | setSize(i, LOG_BUFFER_MIN_SIZE); |
| 127 | } |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 128 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Mark Salyzyn | 7718778 | 2015-05-12 15:21:31 -0700 | [diff] [blame] | 131 | LogBuffer::LogBuffer(LastLogTimes *times) : mTimes(*times) { |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 132 | pthread_mutex_init(&mLogElementsLock, NULL); |
| 133 | |
| 134 | init(); |
| 135 | } |
| 136 | |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 137 | int LogBuffer::log(log_id_t log_id, log_time realtime, |
| 138 | uid_t uid, pid_t pid, pid_t tid, |
| 139 | const char *msg, unsigned short len) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 140 | if ((log_id >= LOG_ID_MAX) || (log_id < 0)) { |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 141 | return -EINVAL; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 142 | } |
Mark Salyzyn | e59c469 | 2014-10-02 13:07:05 -0700 | [diff] [blame] | 143 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 144 | LogBufferElement *elem = new LogBufferElement(log_id, realtime, |
Mark Salyzyn | b992d0d | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 145 | uid, pid, tid, msg, len); |
Mark Salyzyn | e59c469 | 2014-10-02 13:07:05 -0700 | [diff] [blame] | 146 | int prio = ANDROID_LOG_INFO; |
| 147 | const char *tag = NULL; |
| 148 | if (log_id == LOG_ID_EVENTS) { |
| 149 | tag = android::tagToName(elem->getTag()); |
| 150 | } else { |
| 151 | prio = *msg; |
| 152 | tag = msg + 1; |
| 153 | } |
| 154 | if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) { |
| 155 | // Log traffic received to total |
| 156 | pthread_mutex_lock(&mLogElementsLock); |
| 157 | stats.add(elem); |
| 158 | stats.subtract(elem); |
| 159 | pthread_mutex_unlock(&mLogElementsLock); |
| 160 | delete elem; |
| 161 | return -EACCES; |
| 162 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 163 | |
| 164 | pthread_mutex_lock(&mLogElementsLock); |
| 165 | |
| 166 | // Insert elements in time sorted order if possible |
| 167 | // NB: if end is region locked, place element at end of list |
| 168 | LogBufferElementCollection::iterator it = mLogElements.end(); |
| 169 | LogBufferElementCollection::iterator last = it; |
Mark Salyzyn | eae155e | 2014-10-13 16:49:47 -0700 | [diff] [blame] | 170 | while (last != mLogElements.begin()) { |
| 171 | --it; |
Mark Salyzyn | c03e72c | 2014-02-18 11:23:53 -0800 | [diff] [blame] | 172 | if ((*it)->getRealTime() <= realtime) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 173 | break; |
| 174 | } |
| 175 | last = it; |
| 176 | } |
Mark Salyzyn | c03e72c | 2014-02-18 11:23:53 -0800 | [diff] [blame] | 177 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 178 | if (last == mLogElements.end()) { |
| 179 | mLogElements.push_back(elem); |
| 180 | } else { |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 181 | uint64_t end = 1; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 182 | bool end_set = false; |
| 183 | bool end_always = false; |
| 184 | |
| 185 | LogTimeEntry::lock(); |
| 186 | |
| 187 | LastLogTimes::iterator t = mTimes.begin(); |
| 188 | while(t != mTimes.end()) { |
| 189 | LogTimeEntry *entry = (*t); |
| 190 | if (entry->owned_Locked()) { |
| 191 | if (!entry->mNonBlock) { |
| 192 | end_always = true; |
| 193 | break; |
| 194 | } |
| 195 | if (!end_set || (end <= entry->mEnd)) { |
| 196 | end = entry->mEnd; |
| 197 | end_set = true; |
| 198 | } |
| 199 | } |
| 200 | t++; |
| 201 | } |
| 202 | |
| 203 | if (end_always |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 204 | || (end_set && (end >= (*last)->getSequence()))) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 205 | mLogElements.push_back(elem); |
| 206 | } else { |
| 207 | mLogElements.insert(last,elem); |
| 208 | } |
| 209 | |
| 210 | LogTimeEntry::unlock(); |
| 211 | } |
| 212 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 213 | stats.add(elem); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 214 | maybePrune(log_id); |
| 215 | pthread_mutex_unlock(&mLogElementsLock); |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 216 | |
| 217 | return len; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 220 | // Prune at most 10% of the log entries or maxPrune, whichever is less. |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 221 | // |
| 222 | // mLogElementsLock must be held when this function is called. |
| 223 | void LogBuffer::maybePrune(log_id_t id) { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 224 | size_t sizes = stats.sizes(id); |
Mark Salyzyn | 62ab0fd | 2015-08-10 10:23:56 -0700 | [diff] [blame] | 225 | unsigned long maxSize = log_buffer_size(id); |
| 226 | if (sizes > maxSize) { |
Mark Salyzyn | b39ed0c | 2015-08-19 12:20:36 -0700 | [diff] [blame] | 227 | size_t sizeOver = sizes - ((maxSize * 9) / 10); |
Mark Salyzyn | 58b8be8 | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 228 | size_t elements = stats.realElements(id); |
| 229 | size_t minElements = elements / 100; |
| 230 | if (minElements < minPrune) { |
| 231 | minElements = minPrune; |
| 232 | } |
Mark Salyzyn | 62ab0fd | 2015-08-10 10:23:56 -0700 | [diff] [blame] | 233 | unsigned long pruneRows = elements * sizeOver / sizes; |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 234 | if (pruneRows < minElements) { |
Mark Salyzyn | 62ab0fd | 2015-08-10 10:23:56 -0700 | [diff] [blame] | 235 | pruneRows = minElements; |
Mark Salyzyn | 740f9b4 | 2014-01-13 16:37:51 -0800 | [diff] [blame] | 236 | } |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 237 | if (pruneRows > maxPrune) { |
| 238 | pruneRows = maxPrune; |
Mark Salyzyn | b39ed0c | 2015-08-19 12:20:36 -0700 | [diff] [blame] | 239 | } |
Mark Salyzyn | 740f9b4 | 2014-01-13 16:37:51 -0800 | [diff] [blame] | 240 | prune(id, pruneRows); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
Mark Salyzyn | 831aa29 | 2015-09-03 16:08:50 -0700 | [diff] [blame] | 244 | LogBufferElementCollection::iterator LogBuffer::erase( |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 245 | LogBufferElementCollection::iterator it, bool coalesce) { |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 246 | LogBufferElement *e = *it; |
Mark Salyzyn | c892ea3 | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 247 | log_id_t id = e->getLogId(); |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 248 | |
Mark Salyzyn | 5bb2972 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 249 | LogBufferIteratorMap::iterator f = mLastWorstUid[id].find(e->getUid()); |
Mark Salyzyn | c892ea3 | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 250 | if ((f != mLastWorstUid[id].end()) && (it == f->second)) { |
| 251 | mLastWorstUid[id].erase(f); |
| 252 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 253 | it = mLogElements.erase(it); |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 254 | if (coalesce) { |
Mark Salyzyn | 831aa29 | 2015-09-03 16:08:50 -0700 | [diff] [blame] | 255 | stats.erase(e); |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 256 | } else { |
| 257 | stats.subtract(e); |
Mark Salyzyn | 831aa29 | 2015-09-03 16:08:50 -0700 | [diff] [blame] | 258 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 259 | delete e; |
| 260 | |
| 261 | return it; |
| 262 | } |
| 263 | |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 264 | // Define a temporary mechanism to report the last LogBufferElement pointer |
| 265 | // for the specified uid, pid and tid. Used below to help merge-sort when |
| 266 | // pruning for worst UID. |
| 267 | class LogBufferElementKey { |
| 268 | const union { |
| 269 | struct { |
| 270 | uint16_t uid; |
| 271 | uint16_t pid; |
| 272 | uint16_t tid; |
| 273 | uint16_t padding; |
| 274 | } __packed; |
| 275 | uint64_t value; |
| 276 | } __packed; |
| 277 | |
| 278 | public: |
| 279 | LogBufferElementKey(uid_t u, pid_t p, pid_t t):uid(u),pid(p),tid(t),padding(0) { } |
| 280 | LogBufferElementKey(uint64_t k):value(k) { } |
| 281 | |
| 282 | uint64_t getKey() { return value; } |
| 283 | }; |
| 284 | |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 285 | class LogBufferElementLast { |
| 286 | |
| 287 | typedef std::unordered_map<uint64_t, LogBufferElement *> LogBufferElementMap; |
| 288 | LogBufferElementMap map; |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 289 | |
| 290 | public: |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 291 | |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 292 | bool coalesce(LogBufferElement *e, unsigned short dropped) { |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 293 | LogBufferElementKey key(e->getUid(), e->getPid(), e->getTid()); |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 294 | LogBufferElementMap::iterator it = map.find(key.getKey()); |
| 295 | if (it != map.end()) { |
| 296 | LogBufferElement *l = it->second; |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 297 | unsigned short d = l->getDropped(); |
| 298 | if ((dropped + d) > USHRT_MAX) { |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 299 | map.erase(it); |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 300 | } else { |
| 301 | l->setDropped(dropped + d); |
| 302 | return true; |
| 303 | } |
| 304 | } |
| 305 | return false; |
| 306 | } |
| 307 | |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 308 | void add(LogBufferElement *e) { |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 309 | LogBufferElementKey key(e->getUid(), e->getPid(), e->getTid()); |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 310 | map[key.getKey()] = e; |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Mark Salyzyn | e06a6e0 | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 313 | inline void clear() { |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 314 | map.clear(); |
Mark Salyzyn | e06a6e0 | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | void clear(LogBufferElement *e) { |
Mark Salyzyn | 047cc07 | 2015-06-04 13:35:30 -0700 | [diff] [blame] | 318 | uint64_t current = e->getRealTime().nsec() |
| 319 | - (EXPIRE_RATELIMIT * NS_PER_SEC); |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 320 | for(LogBufferElementMap::iterator it = map.begin(); it != map.end();) { |
| 321 | LogBufferElement *l = it->second; |
Mark Salyzyn | 833a9b1 | 2015-05-15 15:58:17 -0700 | [diff] [blame] | 322 | if ((l->getDropped() >= EXPIRE_THRESHOLD) |
| 323 | && (current > l->getRealTime().nsec())) { |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 324 | it = map.erase(it); |
| 325 | } else { |
| 326 | ++it; |
Mark Salyzyn | e06a6e0 | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 331 | }; |
| 332 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 333 | // prune "pruneRows" of type "id" from the buffer. |
| 334 | // |
Mark Salyzyn | 5bb2972 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 335 | // This garbage collection task is used to expire log entries. It is called to |
| 336 | // remove all logs (clear), all UID logs (unprivileged clear), or every |
| 337 | // 256 or 10% of the total logs (whichever is less) to prune the logs. |
| 338 | // |
| 339 | // First there is a prep phase where we discover the reader region lock that |
| 340 | // acts as a backstop to any pruning activity to stop there and go no further. |
| 341 | // |
| 342 | // There are three major pruning loops that follow. All expire from the oldest |
| 343 | // entries. Since there are multiple log buffers, the Android logging facility |
| 344 | // will appear to drop entries 'in the middle' when looking at multiple log |
| 345 | // sources and buffers. This effect is slightly more prominent when we prune |
| 346 | // the worst offender by logging source. Thus the logs slowly loose content |
| 347 | // and value as you move back in time. This is preferred since chatty sources |
| 348 | // invariably move the logs value down faster as less chatty sources would be |
| 349 | // expired in the noise. |
| 350 | // |
| 351 | // The first loop performs blacklisting and worst offender pruning. Falling |
| 352 | // through when there are no notable worst offenders and have not hit the |
| 353 | // region lock preventing further worst offender pruning. This loop also looks |
| 354 | // after managing the chatty log entries and merging to help provide |
| 355 | // statistical basis for blame. The chatty entries are not a notification of |
| 356 | // how much logs you may have, but instead represent how much logs you would |
| 357 | // have had in a virtual log buffer that is extended to cover all the in-memory |
| 358 | // logs without loss. They last much longer than the represented pruned logs |
| 359 | // since they get multiplied by the gains in the non-chatty log sources. |
| 360 | // |
| 361 | // The second loop get complicated because an algorithm of watermarks and |
| 362 | // history is maintained to reduce the order and keep processing time |
| 363 | // down to a minimum at scale. These algorithms can be costly in the face |
| 364 | // of larger log buffers, or severly limited processing time granted to a |
| 365 | // background task at lowest priority. |
| 366 | // |
| 367 | // This second loop does straight-up expiration from the end of the logs |
| 368 | // (again, remember for the specified log buffer id) but does some whitelist |
| 369 | // preservation. Thus whitelist is a Hail Mary low priority, blacklists and |
| 370 | // spam filtration all take priority. This second loop also checks if a region |
| 371 | // lock is causing us to buffer too much in the logs to help the reader(s), |
| 372 | // and will tell the slowest reader thread to skip log entries, and if |
| 373 | // persistent and hits a further threshold, kill the reader thread. |
| 374 | // |
| 375 | // The third thread is optional, and only gets hit if there was a whitelist |
| 376 | // and more needs to be pruned against the backstop of the region lock. |
| 377 | // |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 378 | // mLogElementsLock must be held when this function is called. |
Mark Salyzyn | 5bb2972 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 379 | // |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 380 | bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 381 | LogTimeEntry *oldest = NULL; |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 382 | bool busy = false; |
Mark Salyzyn | 2b25c66 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 383 | bool clearAll = pruneRows == ULONG_MAX; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 384 | |
| 385 | LogTimeEntry::lock(); |
| 386 | |
| 387 | // Region locked? |
| 388 | LastLogTimes::iterator t = mTimes.begin(); |
| 389 | while(t != mTimes.end()) { |
| 390 | LogTimeEntry *entry = (*t); |
TraianX Schiau | da6495d | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 391 | if (entry->owned_Locked() && entry->isWatching(id) |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 392 | && (!oldest || (oldest->mStart > entry->mStart))) { |
| 393 | oldest = entry; |
| 394 | } |
| 395 | t++; |
| 396 | } |
| 397 | |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 398 | LogBufferElementCollection::iterator it; |
| 399 | |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 400 | if (caller_uid != AID_ROOT) { |
Mark Salyzyn | 2b25c66 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 401 | // Only here if clearAll condition (pruneRows == ULONG_MAX) |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 402 | for(it = mLogElements.begin(); it != mLogElements.end();) { |
| 403 | LogBufferElement *e = *it; |
| 404 | |
Mark Salyzyn | 2b25c66 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 405 | if ((e->getLogId() != id) || (e->getUid() != caller_uid)) { |
| 406 | ++it; |
| 407 | continue; |
| 408 | } |
| 409 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 410 | if (oldest && (oldest->mStart <= e->getSequence())) { |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 411 | oldest->triggerSkip_Locked(id, pruneRows); |
| 412 | busy = true; |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 413 | break; |
| 414 | } |
| 415 | |
Mark Salyzyn | 2b25c66 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 416 | it = erase(it); |
| 417 | pruneRows--; |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 418 | } |
| 419 | LogTimeEntry::unlock(); |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 420 | return busy; |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 423 | // prune by worst offender by uid |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 424 | bool hasBlacklist = mPrune.naughty(); |
Mark Salyzyn | 2b25c66 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 425 | while (!clearAll && (pruneRows > 0)) { |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 426 | // recalculate the worst offender on every batched pass |
| 427 | uid_t worst = (uid_t) -1; |
| 428 | size_t worst_sizes = 0; |
| 429 | size_t second_worst_sizes = 0; |
| 430 | |
Mark Salyzyn | ae76923 | 2015-03-17 17:17:25 -0700 | [diff] [blame] | 431 | if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) { |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 432 | std::unique_ptr<const UidEntry *[]> sorted = stats.sort(2, id); |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 433 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 434 | if (sorted.get()) { |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 435 | if (sorted[0] && sorted[1]) { |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 436 | worst_sizes = sorted[0]->getSizes(); |
Mark Salyzyn | d717d80 | 2015-04-20 15:36:12 -0700 | [diff] [blame] | 437 | // Calculate threshold as 12.5% of available storage |
| 438 | size_t threshold = log_buffer_size(id) / 8; |
| 439 | if (worst_sizes > threshold) { |
| 440 | worst = sorted[0]->getKey(); |
| 441 | second_worst_sizes = sorted[1]->getSizes(); |
| 442 | if (second_worst_sizes < threshold) { |
| 443 | second_worst_sizes = threshold; |
| 444 | } |
| 445 | } |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 446 | } |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 450 | // skip if we have neither worst nor naughty filters |
| 451 | if ((worst == (uid_t) -1) && !hasBlacklist) { |
| 452 | break; |
| 453 | } |
| 454 | |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 455 | bool kick = false; |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 456 | bool leading = true; |
Mark Salyzyn | c892ea3 | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 457 | it = mLogElements.begin(); |
Mark Salyzyn | 5bb2972 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 458 | // Perform at least one mandatory garbage collection cycle in following |
| 459 | // - clear leading chatty tags |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 460 | // - coalesce chatty tags |
Mark Salyzyn | 5bb2972 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 461 | // - check age-out of preserved logs |
| 462 | bool gc = pruneRows <= 1; |
| 463 | if (!gc && (worst != (uid_t) -1)) { |
Mark Salyzyn | c892ea3 | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 464 | LogBufferIteratorMap::iterator f = mLastWorstUid[id].find(worst); |
| 465 | if ((f != mLastWorstUid[id].end()) |
| 466 | && (f->second != mLogElements.end())) { |
| 467 | leading = false; |
| 468 | it = f->second; |
| 469 | } |
| 470 | } |
Mark Salyzyn | ccfe844 | 2015-08-24 13:43:27 -0700 | [diff] [blame] | 471 | static const timespec too_old = { |
| 472 | EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 |
| 473 | }; |
| 474 | LogBufferElementCollection::iterator lastt; |
| 475 | lastt = mLogElements.end(); |
| 476 | --lastt; |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 477 | LogBufferElementLast last; |
Mark Salyzyn | c892ea3 | 2015-08-19 17:06:11 -0700 | [diff] [blame] | 478 | while (it != mLogElements.end()) { |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 479 | LogBufferElement *e = *it; |
| 480 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 481 | if (oldest && (oldest->mStart <= e->getSequence())) { |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 482 | busy = true; |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 483 | break; |
| 484 | } |
| 485 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 486 | if (e->getLogId() != id) { |
| 487 | ++it; |
| 488 | continue; |
| 489 | } |
| 490 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 491 | unsigned short dropped = e->getDropped(); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 492 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 493 | // remove any leading drops |
| 494 | if (leading && dropped) { |
| 495 | it = erase(it); |
| 496 | continue; |
| 497 | } |
| 498 | |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 499 | if (dropped && last.coalesce(e, dropped)) { |
| 500 | it = erase(it, true); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 501 | continue; |
| 502 | } |
| 503 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 504 | if (hasBlacklist && mPrune.naughty(e)) { |
Mark Salyzyn | e06a6e0 | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 505 | last.clear(e); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 506 | it = erase(it); |
| 507 | if (dropped) { |
| 508 | continue; |
| 509 | } |
| 510 | |
| 511 | pruneRows--; |
| 512 | if (pruneRows == 0) { |
| 513 | break; |
| 514 | } |
| 515 | |
| 516 | if (e->getUid() == worst) { |
| 517 | kick = true; |
| 518 | if (worst_sizes < second_worst_sizes) { |
| 519 | break; |
| 520 | } |
| 521 | worst_sizes -= e->getMsgLen(); |
| 522 | } |
| 523 | continue; |
| 524 | } |
| 525 | |
Mark Salyzyn | ccfe844 | 2015-08-24 13:43:27 -0700 | [diff] [blame] | 526 | if ((e->getRealTime() < ((*lastt)->getRealTime() - too_old)) |
| 527 | || (e->getRealTime() > (*lastt)->getRealTime())) { |
| 528 | break; |
| 529 | } |
| 530 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 531 | if (dropped) { |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 532 | last.add(e); |
Mark Salyzyn | 5bb2972 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 533 | if ((!gc && (e->getUid() == worst)) |
Mark Salyzyn | 49afe0d | 2015-08-24 13:43:27 -0700 | [diff] [blame] | 534 | || (mLastWorstUid[id].find(e->getUid()) |
| 535 | == mLastWorstUid[id].end())) { |
| 536 | mLastWorstUid[id][e->getUid()] = it; |
| 537 | } |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 538 | ++it; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 539 | continue; |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 540 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 541 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 542 | if (e->getUid() != worst) { |
Mark Salyzyn | 5921276 | 2015-06-01 09:41:19 -0700 | [diff] [blame] | 543 | leading = false; |
Mark Salyzyn | e06a6e0 | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 544 | last.clear(e); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 545 | ++it; |
| 546 | continue; |
| 547 | } |
| 548 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 549 | pruneRows--; |
| 550 | if (pruneRows == 0) { |
| 551 | break; |
| 552 | } |
| 553 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 554 | kick = true; |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 555 | |
| 556 | unsigned short len = e->getMsgLen(); |
Mark Salyzyn | 5392aac | 2015-05-22 10:03:31 -0700 | [diff] [blame] | 557 | |
| 558 | // do not create any leading drops |
| 559 | if (leading) { |
| 560 | it = erase(it); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 561 | } else { |
Mark Salyzyn | 5392aac | 2015-05-22 10:03:31 -0700 | [diff] [blame] | 562 | stats.drop(e); |
| 563 | e->setDropped(1); |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 564 | if (last.coalesce(e, 1)) { |
| 565 | it = erase(it, true); |
Mark Salyzyn | 5392aac | 2015-05-22 10:03:31 -0700 | [diff] [blame] | 566 | } else { |
| 567 | last.add(e); |
Mark Salyzyn | 5bb2972 | 2015-09-08 09:12:51 -0700 | [diff] [blame] | 568 | if (!gc || (mLastWorstUid[id].find(worst) |
| 569 | == mLastWorstUid[id].end())) { |
| 570 | mLastWorstUid[id][worst] = it; |
| 571 | } |
Mark Salyzyn | 5392aac | 2015-05-22 10:03:31 -0700 | [diff] [blame] | 572 | ++it; |
| 573 | } |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 574 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 575 | if (worst_sizes < second_worst_sizes) { |
| 576 | break; |
| 577 | } |
| 578 | worst_sizes -= len; |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 579 | } |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 580 | last.clear(); |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 581 | |
Mark Salyzyn | 1c95047 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 582 | if (!kick || !mPrune.worstUidEnabled()) { |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 583 | break; // the following loop will ask bad clients to skip/drop |
| 584 | } |
| 585 | } |
| 586 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 587 | bool whitelist = false; |
Mark Salyzyn | 2b25c66 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 588 | bool hasWhitelist = mPrune.nice() && !clearAll; |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 589 | it = mLogElements.begin(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 590 | while((pruneRows > 0) && (it != mLogElements.end())) { |
| 591 | LogBufferElement *e = *it; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 592 | |
| 593 | if (e->getLogId() != id) { |
| 594 | it++; |
| 595 | continue; |
| 596 | } |
| 597 | |
| 598 | if (oldest && (oldest->mStart <= e->getSequence())) { |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 599 | busy = true; |
| 600 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 601 | if (whitelist) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 602 | break; |
| 603 | } |
Mark Salyzyn | 1c95047 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 604 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 605 | if (stats.sizes(id) > (2 * log_buffer_size(id))) { |
| 606 | // kick a misbehaving log reader client off the island |
| 607 | oldest->release_Locked(); |
| 608 | } else { |
| 609 | oldest->triggerSkip_Locked(id, pruneRows); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 610 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 611 | break; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 612 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 613 | |
Mark Salyzyn | c5bf3b8 | 2015-05-21 12:50:31 -0700 | [diff] [blame] | 614 | if (hasWhitelist && !e->getDropped() && mPrune.nice(e)) { // WhiteListed |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 615 | whitelist = true; |
| 616 | it++; |
| 617 | continue; |
| 618 | } |
| 619 | |
| 620 | it = erase(it); |
| 621 | pruneRows--; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 622 | } |
| 623 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 624 | // Do not save the whitelist if we are reader range limited |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 625 | if (whitelist && (pruneRows > 0)) { |
| 626 | it = mLogElements.begin(); |
| 627 | while((it != mLogElements.end()) && (pruneRows > 0)) { |
| 628 | LogBufferElement *e = *it; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 629 | |
| 630 | if (e->getLogId() != id) { |
| 631 | ++it; |
| 632 | continue; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 633 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 634 | |
| 635 | if (oldest && (oldest->mStart <= e->getSequence())) { |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 636 | busy = true; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 637 | if (stats.sizes(id) > (2 * log_buffer_size(id))) { |
| 638 | // kick a misbehaving log reader client off the island |
| 639 | oldest->release_Locked(); |
| 640 | } else { |
| 641 | oldest->triggerSkip_Locked(id, pruneRows); |
| 642 | } |
| 643 | break; |
| 644 | } |
| 645 | |
| 646 | it = erase(it); |
| 647 | pruneRows--; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 648 | } |
| 649 | } |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 650 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 651 | LogTimeEntry::unlock(); |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 652 | |
| 653 | return (pruneRows > 0) && busy; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | // clear all rows of type "id" from the buffer. |
Mark Salyzyn | c5dc970 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 657 | bool LogBuffer::clear(log_id_t id, uid_t uid) { |
| 658 | bool busy = true; |
| 659 | // If it takes more than 4 tries (seconds) to clear, then kill reader(s) |
| 660 | for (int retry = 4;;) { |
| 661 | if (retry == 1) { // last pass |
| 662 | // Check if it is still busy after the sleep, we say prune |
| 663 | // one entry, not another clear run, so we are looking for |
| 664 | // the quick side effect of the return value to tell us if |
| 665 | // we have a _blocked_ reader. |
| 666 | pthread_mutex_lock(&mLogElementsLock); |
| 667 | busy = prune(id, 1, uid); |
| 668 | pthread_mutex_unlock(&mLogElementsLock); |
| 669 | // It is still busy, blocked reader(s), lets kill them all! |
| 670 | // otherwise, lets be a good citizen and preserve the slow |
| 671 | // readers and let the clear run (below) deal with determining |
| 672 | // if we are still blocked and return an error code to caller. |
| 673 | if (busy) { |
| 674 | LogTimeEntry::lock(); |
| 675 | LastLogTimes::iterator times = mTimes.begin(); |
| 676 | while (times != mTimes.end()) { |
| 677 | LogTimeEntry *entry = (*times); |
| 678 | // Killer punch |
| 679 | if (entry->owned_Locked() && entry->isWatching(id)) { |
| 680 | entry->release_Locked(); |
| 681 | } |
| 682 | times++; |
| 683 | } |
| 684 | LogTimeEntry::unlock(); |
| 685 | } |
| 686 | } |
| 687 | pthread_mutex_lock(&mLogElementsLock); |
| 688 | busy = prune(id, ULONG_MAX, uid); |
| 689 | pthread_mutex_unlock(&mLogElementsLock); |
| 690 | if (!busy || !--retry) { |
| 691 | break; |
| 692 | } |
| 693 | sleep (1); // Let reader(s) catch up after notification |
| 694 | } |
| 695 | return busy; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | // get the used space associated with "id". |
| 699 | unsigned long LogBuffer::getSizeUsed(log_id_t id) { |
| 700 | pthread_mutex_lock(&mLogElementsLock); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 701 | size_t retval = stats.sizes(id); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 702 | pthread_mutex_unlock(&mLogElementsLock); |
| 703 | return retval; |
| 704 | } |
| 705 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 706 | // set the total space allocated to "id" |
| 707 | int LogBuffer::setSize(log_id_t id, unsigned long size) { |
| 708 | // Reasonable limits ... |
Mark Salyzyn | 57a0af9 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 709 | if (!valid_size(size)) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 710 | return -1; |
| 711 | } |
| 712 | pthread_mutex_lock(&mLogElementsLock); |
| 713 | log_buffer_size(id) = size; |
| 714 | pthread_mutex_unlock(&mLogElementsLock); |
| 715 | return 0; |
| 716 | } |
| 717 | |
| 718 | // get the total space allocated to "id" |
| 719 | unsigned long LogBuffer::getSize(log_id_t id) { |
| 720 | pthread_mutex_lock(&mLogElementsLock); |
| 721 | size_t retval = log_buffer_size(id); |
| 722 | pthread_mutex_unlock(&mLogElementsLock); |
| 723 | return retval; |
| 724 | } |
| 725 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 726 | uint64_t LogBuffer::flushTo( |
| 727 | SocketClient *reader, const uint64_t start, bool privileged, |
| 728 | int (*filter)(const LogBufferElement *element, void *arg), void *arg) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 729 | LogBufferElementCollection::iterator it; |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 730 | uint64_t max = start; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 731 | uid_t uid = reader->getUid(); |
| 732 | |
| 733 | pthread_mutex_lock(&mLogElementsLock); |
Dragoslav Mitrinovic | 8e8e8db | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 734 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 735 | if (start <= 1) { |
Dragoslav Mitrinovic | 8e8e8db | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 736 | // client wants to start from the beginning |
| 737 | it = mLogElements.begin(); |
| 738 | } else { |
| 739 | // Client wants to start from some specified time. Chances are |
| 740 | // we are better off starting from the end of the time sorted list. |
| 741 | for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) { |
| 742 | --it; |
| 743 | LogBufferElement *element = *it; |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 744 | if (element->getSequence() <= start) { |
Dragoslav Mitrinovic | 8e8e8db | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 745 | it++; |
| 746 | break; |
| 747 | } |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | for (; it != mLogElements.end(); ++it) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 752 | LogBufferElement *element = *it; |
| 753 | |
| 754 | if (!privileged && (element->getUid() != uid)) { |
| 755 | continue; |
| 756 | } |
| 757 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 758 | if (element->getSequence() <= start) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 759 | continue; |
| 760 | } |
| 761 | |
| 762 | // NB: calling out to another object with mLogElementsLock held (safe) |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 763 | if (filter) { |
| 764 | int ret = (*filter)(element, arg); |
| 765 | if (ret == false) { |
| 766 | continue; |
| 767 | } |
| 768 | if (ret != true) { |
| 769 | break; |
| 770 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | pthread_mutex_unlock(&mLogElementsLock); |
| 774 | |
| 775 | // range locking in LastLogTimes looks after us |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 776 | max = element->flushTo(reader, this); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 777 | |
| 778 | if (max == element->FLUSH_ERROR) { |
| 779 | return max; |
| 780 | } |
| 781 | |
| 782 | pthread_mutex_lock(&mLogElementsLock); |
| 783 | } |
| 784 | pthread_mutex_unlock(&mLogElementsLock); |
| 785 | |
| 786 | return max; |
| 787 | } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 788 | |
Mark Salyzyn | 73160ac | 2015-08-20 10:01:44 -0700 | [diff] [blame] | 789 | std::string LogBuffer::formatStatistics(uid_t uid, unsigned int logMask) { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 790 | pthread_mutex_lock(&mLogElementsLock); |
| 791 | |
Mark Salyzyn | 73160ac | 2015-08-20 10:01:44 -0700 | [diff] [blame] | 792 | std::string ret = stats.format(uid, logMask); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 793 | |
| 794 | pthread_mutex_unlock(&mLogElementsLock); |
Mark Salyzyn | 73160ac | 2015-08-20 10:01:44 -0700 | [diff] [blame] | 795 | |
| 796 | return ret; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 797 | } |