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 | 4e756fb | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 31 | #include "LogReader.h" |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 32 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 33 | // Default |
Mark Salyzyn | 12bac90 | 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 | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 35 | #define log_buffer_size(id) mMaxSize[id] |
Mark Salyzyn | 7b85c59 | 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 | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 64 | |
Mark Salyzyn | 4e756fb | 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 | 7b85c59 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 88 | if (!valid_size(value)) { |
| 89 | value = 0; |
| 90 | } |
| 91 | |
Mark Salyzyn | 4e756fb | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 92 | return value; |
| 93 | } |
| 94 | |
Mark Salyzyn | 3fe2593 | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 95 | void LogBuffer::init() { |
Mark Salyzyn | 7b85c59 | 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 | 4e756fb | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 103 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 104 | log_id_for_each(i) { |
Mark Salyzyn | 4e756fb | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 105 | char key[PROP_NAME_MAX]; |
Mark Salyzyn | 4e756fb | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 106 | |
Mark Salyzyn | 7b85c59 | 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 | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 128 | } |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Mark Salyzyn | cd766f9 | 2015-05-12 15:21:31 -0700 | [diff] [blame] | 131 | LogBuffer::LogBuffer(LastLogTimes *times) : mTimes(*times) { |
Mark Salyzyn | 3fe2593 | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 132 | pthread_mutex_init(&mLogElementsLock, NULL); |
| 133 | |
| 134 | init(); |
| 135 | } |
| 136 | |
Mark Salyzyn | 88fe7cc | 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 | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 140 | if ((log_id >= LOG_ID_MAX) || (log_id < 0)) { |
Mark Salyzyn | 88fe7cc | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 141 | return -EINVAL; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 142 | } |
Mark Salyzyn | 0dcfae6 | 2014-10-02 13:07:05 -0700 | [diff] [blame] | 143 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 144 | LogBufferElement *elem = new LogBufferElement(log_id, realtime, |
Mark Salyzyn | 3661dd0 | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 145 | uid, pid, tid, msg, len); |
Mark Salyzyn | 0dcfae6 | 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 | 12bac90 | 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 | a222a77 | 2014-10-13 16:49:47 -0700 | [diff] [blame] | 170 | while (last != mLogElements.begin()) { |
| 171 | --it; |
Mark Salyzyn | 5b7a8d8 | 2014-02-18 11:23:53 -0800 | [diff] [blame] | 172 | if ((*it)->getRealTime() <= realtime) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 173 | break; |
| 174 | } |
| 175 | last = it; |
| 176 | } |
Mark Salyzyn | 5b7a8d8 | 2014-02-18 11:23:53 -0800 | [diff] [blame] | 177 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 178 | if (last == mLogElements.end()) { |
| 179 | mLogElements.push_back(elem); |
| 180 | } else { |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 181 | uint64_t end = 1; |
Mark Salyzyn | 12bac90 | 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 | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 204 | || (end_set && (end >= (*last)->getSequence()))) { |
Mark Salyzyn | 12bac90 | 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 | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 213 | stats.add(elem); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 214 | maybePrune(log_id); |
| 215 | pthread_mutex_unlock(&mLogElementsLock); |
Mark Salyzyn | 88fe7cc | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 216 | |
| 217 | return len; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | // If we're using more than 256K of memory for log entries, prune |
Mark Salyzyn | 5f19031 | 2015-08-10 10:23:56 -0700 | [diff] [blame^] | 221 | // at least 10% of the log entries. For sizes above 1M, prune at |
| 222 | // least 1% of the log entries. |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 223 | // |
| 224 | // mLogElementsLock must be held when this function is called. |
| 225 | void LogBuffer::maybePrune(log_id_t id) { |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 226 | size_t sizes = stats.sizes(id); |
Mark Salyzyn | 5f19031 | 2015-08-10 10:23:56 -0700 | [diff] [blame^] | 227 | unsigned long maxSize = log_buffer_size(id); |
| 228 | if (sizes > maxSize) { |
| 229 | size_t sizeOver, minElements, elements = stats.elements(id); |
| 230 | if (maxSize > (4 * LOG_BUFFER_SIZE)) { |
| 231 | sizeOver = sizes - ((maxSize * 99) / 100); |
| 232 | minElements = elements / 100; |
| 233 | } else { |
| 234 | sizeOver = sizes - ((maxSize * 9) / 10); |
| 235 | minElements = elements / 10; |
| 236 | } |
| 237 | unsigned long pruneRows = elements * sizeOver / sizes; |
| 238 | if (pruneRows <= minElements) { |
| 239 | pruneRows = minElements; |
Mark Salyzyn | 9d4e34e | 2014-01-13 16:37:51 -0800 | [diff] [blame] | 240 | } |
| 241 | prune(id, pruneRows); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 245 | LogBufferElementCollection::iterator LogBuffer::erase(LogBufferElementCollection::iterator it) { |
| 246 | LogBufferElement *e = *it; |
| 247 | |
| 248 | it = mLogElements.erase(it); |
| 249 | stats.subtract(e); |
| 250 | delete e; |
| 251 | |
| 252 | return it; |
| 253 | } |
| 254 | |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 255 | // Define a temporary mechanism to report the last LogBufferElement pointer |
| 256 | // for the specified uid, pid and tid. Used below to help merge-sort when |
| 257 | // pruning for worst UID. |
| 258 | class LogBufferElementKey { |
| 259 | const union { |
| 260 | struct { |
| 261 | uint16_t uid; |
| 262 | uint16_t pid; |
| 263 | uint16_t tid; |
| 264 | uint16_t padding; |
| 265 | } __packed; |
| 266 | uint64_t value; |
| 267 | } __packed; |
| 268 | |
| 269 | public: |
| 270 | LogBufferElementKey(uid_t u, pid_t p, pid_t t):uid(u),pid(p),tid(t),padding(0) { } |
| 271 | LogBufferElementKey(uint64_t k):value(k) { } |
| 272 | |
| 273 | uint64_t getKey() { return value; } |
| 274 | }; |
| 275 | |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 276 | class LogBufferElementLast { |
| 277 | |
| 278 | typedef std::unordered_map<uint64_t, LogBufferElement *> LogBufferElementMap; |
| 279 | LogBufferElementMap map; |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 280 | |
| 281 | public: |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 282 | |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 283 | bool merge(LogBufferElement *e, unsigned short dropped) { |
| 284 | LogBufferElementKey key(e->getUid(), e->getPid(), e->getTid()); |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 285 | LogBufferElementMap::iterator it = map.find(key.getKey()); |
| 286 | if (it != map.end()) { |
| 287 | LogBufferElement *l = it->second; |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 288 | unsigned short d = l->getDropped(); |
| 289 | if ((dropped + d) > USHRT_MAX) { |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 290 | map.erase(it); |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 291 | } else { |
| 292 | l->setDropped(dropped + d); |
| 293 | return true; |
| 294 | } |
| 295 | } |
| 296 | return false; |
| 297 | } |
| 298 | |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 299 | void add(LogBufferElement *e) { |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 300 | LogBufferElementKey key(e->getUid(), e->getPid(), e->getTid()); |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 301 | map[key.getKey()] = e; |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Mark Salyzyn | be1a30c | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 304 | inline void clear() { |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 305 | map.clear(); |
Mark Salyzyn | be1a30c | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | void clear(LogBufferElement *e) { |
Mark Salyzyn | 80c1fc6 | 2015-06-04 13:35:30 -0700 | [diff] [blame] | 309 | uint64_t current = e->getRealTime().nsec() |
| 310 | - (EXPIRE_RATELIMIT * NS_PER_SEC); |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 311 | for(LogBufferElementMap::iterator it = map.begin(); it != map.end();) { |
| 312 | LogBufferElement *l = it->second; |
Mark Salyzyn | 4f199f3 | 2015-05-15 15:58:17 -0700 | [diff] [blame] | 313 | if ((l->getDropped() >= EXPIRE_THRESHOLD) |
| 314 | && (current > l->getRealTime().nsec())) { |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 315 | it = map.erase(it); |
| 316 | } else { |
| 317 | ++it; |
Mark Salyzyn | be1a30c | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 322 | }; |
| 323 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 324 | // prune "pruneRows" of type "id" from the buffer. |
| 325 | // |
| 326 | // mLogElementsLock must be held when this function is called. |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 327 | void 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] | 328 | LogTimeEntry *oldest = NULL; |
| 329 | |
| 330 | LogTimeEntry::lock(); |
| 331 | |
| 332 | // Region locked? |
| 333 | LastLogTimes::iterator t = mTimes.begin(); |
| 334 | while(t != mTimes.end()) { |
| 335 | LogTimeEntry *entry = (*t); |
TraianX Schiau | 6ba427e | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 336 | if (entry->owned_Locked() && entry->isWatching(id) |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 337 | && (!oldest || (oldest->mStart > entry->mStart))) { |
| 338 | oldest = entry; |
| 339 | } |
| 340 | t++; |
| 341 | } |
| 342 | |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 343 | LogBufferElementCollection::iterator it; |
| 344 | |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 345 | if (caller_uid != AID_ROOT) { |
| 346 | for(it = mLogElements.begin(); it != mLogElements.end();) { |
| 347 | LogBufferElement *e = *it; |
| 348 | |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 349 | if (oldest && (oldest->mStart <= e->getSequence())) { |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 350 | break; |
| 351 | } |
| 352 | |
| 353 | if (e->getLogId() != id) { |
| 354 | ++it; |
| 355 | continue; |
| 356 | } |
| 357 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 358 | if (e->getUid() == caller_uid) { |
| 359 | it = erase(it); |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 360 | pruneRows--; |
| 361 | if (pruneRows == 0) { |
| 362 | break; |
| 363 | } |
| 364 | } else { |
| 365 | ++it; |
| 366 | } |
| 367 | } |
| 368 | LogTimeEntry::unlock(); |
| 369 | return; |
| 370 | } |
| 371 | |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 372 | // prune by worst offender by uid |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 373 | bool hasBlacklist = mPrune.naughty(); |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 374 | while (pruneRows > 0) { |
| 375 | // recalculate the worst offender on every batched pass |
| 376 | uid_t worst = (uid_t) -1; |
| 377 | size_t worst_sizes = 0; |
| 378 | size_t second_worst_sizes = 0; |
| 379 | |
Mark Salyzyn | 0e765c6 | 2015-03-17 17:17:25 -0700 | [diff] [blame] | 380 | if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) { |
Mark Salyzyn | 1435b47 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 381 | std::unique_ptr<const UidEntry *[]> sorted = stats.sort(2, id); |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 382 | |
Mark Salyzyn | 1435b47 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 383 | if (sorted.get()) { |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 384 | if (sorted[0] && sorted[1]) { |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 385 | worst_sizes = sorted[0]->getSizes(); |
Mark Salyzyn | 84a8398 | 2015-04-20 15:36:12 -0700 | [diff] [blame] | 386 | // Calculate threshold as 12.5% of available storage |
| 387 | size_t threshold = log_buffer_size(id) / 8; |
| 388 | if (worst_sizes > threshold) { |
| 389 | worst = sorted[0]->getKey(); |
| 390 | second_worst_sizes = sorted[1]->getSizes(); |
| 391 | if (second_worst_sizes < threshold) { |
| 392 | second_worst_sizes = threshold; |
| 393 | } |
| 394 | } |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 395 | } |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 399 | // skip if we have neither worst nor naughty filters |
| 400 | if ((worst == (uid_t) -1) && !hasBlacklist) { |
| 401 | break; |
| 402 | } |
| 403 | |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 404 | bool kick = false; |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 405 | bool leading = true; |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 406 | LogBufferElementLast last; |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 407 | for(it = mLogElements.begin(); it != mLogElements.end();) { |
| 408 | LogBufferElement *e = *it; |
| 409 | |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 410 | if (oldest && (oldest->mStart <= e->getSequence())) { |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 411 | break; |
| 412 | } |
| 413 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 414 | if (e->getLogId() != id) { |
| 415 | ++it; |
| 416 | continue; |
| 417 | } |
| 418 | |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 419 | unsigned short dropped = e->getDropped(); |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 420 | |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 421 | // remove any leading drops |
| 422 | if (leading && dropped) { |
| 423 | it = erase(it); |
| 424 | continue; |
| 425 | } |
| 426 | |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 427 | // merge any drops |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 428 | if (dropped && last.merge(e, dropped)) { |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 429 | it = mLogElements.erase(it); |
| 430 | stats.erase(e); |
| 431 | delete e; |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 432 | continue; |
| 433 | } |
| 434 | |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 435 | if (hasBlacklist && mPrune.naughty(e)) { |
Mark Salyzyn | be1a30c | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 436 | last.clear(e); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 437 | it = erase(it); |
| 438 | if (dropped) { |
| 439 | continue; |
| 440 | } |
| 441 | |
| 442 | pruneRows--; |
| 443 | if (pruneRows == 0) { |
| 444 | break; |
| 445 | } |
| 446 | |
| 447 | if (e->getUid() == worst) { |
| 448 | kick = true; |
| 449 | if (worst_sizes < second_worst_sizes) { |
| 450 | break; |
| 451 | } |
| 452 | worst_sizes -= e->getMsgLen(); |
| 453 | } |
| 454 | continue; |
| 455 | } |
| 456 | |
| 457 | if (dropped) { |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 458 | last.add(e); |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 459 | ++it; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 460 | continue; |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 461 | } |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 462 | |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 463 | if (e->getUid() != worst) { |
Mark Salyzyn | fd318a8 | 2015-06-01 09:41:19 -0700 | [diff] [blame] | 464 | if (leading) { |
Mark Salyzyn | 4f199f3 | 2015-05-15 15:58:17 -0700 | [diff] [blame] | 465 | static const timespec too_old = { |
| 466 | EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 |
| 467 | }; |
Mark Salyzyn | fd318a8 | 2015-06-01 09:41:19 -0700 | [diff] [blame] | 468 | LogBufferElementCollection::iterator last; |
| 469 | last = mLogElements.end(); |
| 470 | --last; |
| 471 | if ((e->getRealTime() < ((*last)->getRealTime() - too_old)) |
| 472 | || (e->getRealTime() > (*last)->getRealTime())) { |
| 473 | break; |
| 474 | } |
Mark Salyzyn | 4f199f3 | 2015-05-15 15:58:17 -0700 | [diff] [blame] | 475 | } |
Mark Salyzyn | fd318a8 | 2015-06-01 09:41:19 -0700 | [diff] [blame] | 476 | leading = false; |
Mark Salyzyn | be1a30c | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 477 | last.clear(e); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 478 | ++it; |
| 479 | continue; |
| 480 | } |
| 481 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 482 | pruneRows--; |
| 483 | if (pruneRows == 0) { |
| 484 | break; |
| 485 | } |
| 486 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 487 | kick = true; |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 488 | |
| 489 | unsigned short len = e->getMsgLen(); |
Mark Salyzyn | 65ec860 | 2015-05-22 10:03:31 -0700 | [diff] [blame] | 490 | |
| 491 | // do not create any leading drops |
| 492 | if (leading) { |
| 493 | it = erase(it); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 494 | } else { |
Mark Salyzyn | 65ec860 | 2015-05-22 10:03:31 -0700 | [diff] [blame] | 495 | stats.drop(e); |
| 496 | e->setDropped(1); |
| 497 | if (last.merge(e, 1)) { |
| 498 | it = mLogElements.erase(it); |
| 499 | stats.erase(e); |
| 500 | delete e; |
| 501 | } else { |
| 502 | last.add(e); |
| 503 | ++it; |
| 504 | } |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 505 | } |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 506 | if (worst_sizes < second_worst_sizes) { |
| 507 | break; |
| 508 | } |
| 509 | worst_sizes -= len; |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 510 | } |
Mark Salyzyn | d84b27e | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 511 | last.clear(); |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 512 | |
Mark Salyzyn | c402bc7 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 513 | if (!kick || !mPrune.worstUidEnabled()) { |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 514 | break; // the following loop will ask bad clients to skip/drop |
| 515 | } |
| 516 | } |
| 517 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 518 | bool whitelist = false; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 519 | bool hasWhitelist = mPrune.nice(); |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 520 | it = mLogElements.begin(); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 521 | while((pruneRows > 0) && (it != mLogElements.end())) { |
| 522 | LogBufferElement *e = *it; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 523 | |
| 524 | if (e->getLogId() != id) { |
| 525 | it++; |
| 526 | continue; |
| 527 | } |
| 528 | |
| 529 | if (oldest && (oldest->mStart <= e->getSequence())) { |
| 530 | if (whitelist) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 531 | break; |
| 532 | } |
Mark Salyzyn | c402bc7 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 533 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 534 | if (stats.sizes(id) > (2 * log_buffer_size(id))) { |
| 535 | // kick a misbehaving log reader client off the island |
| 536 | oldest->release_Locked(); |
| 537 | } else { |
| 538 | oldest->triggerSkip_Locked(id, pruneRows); |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 539 | } |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 540 | break; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 541 | } |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 542 | |
Mark Salyzyn | d910ca9 | 2015-05-21 12:50:31 -0700 | [diff] [blame] | 543 | if (hasWhitelist && !e->getDropped() && mPrune.nice(e)) { // WhiteListed |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 544 | whitelist = true; |
| 545 | it++; |
| 546 | continue; |
| 547 | } |
| 548 | |
| 549 | it = erase(it); |
| 550 | pruneRows--; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 551 | } |
| 552 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 553 | // Do not save the whitelist if we are reader range limited |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 554 | if (whitelist && (pruneRows > 0)) { |
| 555 | it = mLogElements.begin(); |
| 556 | while((it != mLogElements.end()) && (pruneRows > 0)) { |
| 557 | LogBufferElement *e = *it; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 558 | |
| 559 | if (e->getLogId() != id) { |
| 560 | ++it; |
| 561 | continue; |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 562 | } |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 563 | |
| 564 | if (oldest && (oldest->mStart <= e->getSequence())) { |
| 565 | if (stats.sizes(id) > (2 * log_buffer_size(id))) { |
| 566 | // kick a misbehaving log reader client off the island |
| 567 | oldest->release_Locked(); |
| 568 | } else { |
| 569 | oldest->triggerSkip_Locked(id, pruneRows); |
| 570 | } |
| 571 | break; |
| 572 | } |
| 573 | |
| 574 | it = erase(it); |
| 575 | pruneRows--; |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 576 | } |
| 577 | } |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 578 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 579 | LogTimeEntry::unlock(); |
| 580 | } |
| 581 | |
| 582 | // clear all rows of type "id" from the buffer. |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 583 | void LogBuffer::clear(log_id_t id, uid_t uid) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 584 | pthread_mutex_lock(&mLogElementsLock); |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 585 | prune(id, ULONG_MAX, uid); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 586 | pthread_mutex_unlock(&mLogElementsLock); |
| 587 | } |
| 588 | |
| 589 | // get the used space associated with "id". |
| 590 | unsigned long LogBuffer::getSizeUsed(log_id_t id) { |
| 591 | pthread_mutex_lock(&mLogElementsLock); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 592 | size_t retval = stats.sizes(id); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 593 | pthread_mutex_unlock(&mLogElementsLock); |
| 594 | return retval; |
| 595 | } |
| 596 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 597 | // set the total space allocated to "id" |
| 598 | int LogBuffer::setSize(log_id_t id, unsigned long size) { |
| 599 | // Reasonable limits ... |
Mark Salyzyn | 7b85c59 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 600 | if (!valid_size(size)) { |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 601 | return -1; |
| 602 | } |
| 603 | pthread_mutex_lock(&mLogElementsLock); |
| 604 | log_buffer_size(id) = size; |
| 605 | pthread_mutex_unlock(&mLogElementsLock); |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | // get the total space allocated to "id" |
| 610 | unsigned long LogBuffer::getSize(log_id_t id) { |
| 611 | pthread_mutex_lock(&mLogElementsLock); |
| 612 | size_t retval = log_buffer_size(id); |
| 613 | pthread_mutex_unlock(&mLogElementsLock); |
| 614 | return retval; |
| 615 | } |
| 616 | |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 617 | uint64_t LogBuffer::flushTo( |
| 618 | SocketClient *reader, const uint64_t start, bool privileged, |
| 619 | int (*filter)(const LogBufferElement *element, void *arg), void *arg) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 620 | LogBufferElementCollection::iterator it; |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 621 | uint64_t max = start; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 622 | uid_t uid = reader->getUid(); |
| 623 | |
| 624 | pthread_mutex_lock(&mLogElementsLock); |
Dragoslav Mitrinovic | 81624f2 | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 625 | |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 626 | if (start <= 1) { |
Dragoslav Mitrinovic | 81624f2 | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 627 | // client wants to start from the beginning |
| 628 | it = mLogElements.begin(); |
| 629 | } else { |
| 630 | // Client wants to start from some specified time. Chances are |
| 631 | // we are better off starting from the end of the time sorted list. |
| 632 | for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) { |
| 633 | --it; |
| 634 | LogBufferElement *element = *it; |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 635 | if (element->getSequence() <= start) { |
Dragoslav Mitrinovic | 81624f2 | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 636 | it++; |
| 637 | break; |
| 638 | } |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | for (; it != mLogElements.end(); ++it) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 643 | LogBufferElement *element = *it; |
| 644 | |
| 645 | if (!privileged && (element->getUid() != uid)) { |
| 646 | continue; |
| 647 | } |
| 648 | |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 649 | if (element->getSequence() <= start) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 650 | continue; |
| 651 | } |
| 652 | |
| 653 | // NB: calling out to another object with mLogElementsLock held (safe) |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 654 | if (filter) { |
| 655 | int ret = (*filter)(element, arg); |
| 656 | if (ret == false) { |
| 657 | continue; |
| 658 | } |
| 659 | if (ret != true) { |
| 660 | break; |
| 661 | } |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | pthread_mutex_unlock(&mLogElementsLock); |
| 665 | |
| 666 | // range locking in LastLogTimes looks after us |
Mark Salyzyn | bf91832 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 667 | max = element->flushTo(reader, this); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 668 | |
| 669 | if (max == element->FLUSH_ERROR) { |
| 670 | return max; |
| 671 | } |
| 672 | |
| 673 | pthread_mutex_lock(&mLogElementsLock); |
| 674 | } |
| 675 | pthread_mutex_unlock(&mLogElementsLock); |
| 676 | |
| 677 | return max; |
| 678 | } |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 679 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 680 | void LogBuffer::formatStatistics(char **strp, uid_t uid, unsigned int logMask) { |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 681 | pthread_mutex_lock(&mLogElementsLock); |
| 682 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 683 | stats.format(strp, uid, logMask); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 684 | |
| 685 | pthread_mutex_unlock(&mLogElementsLock); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 686 | } |