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 | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 25 | #include <cutils/properties.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 26 | #include <log/logger.h> |
| 27 | |
| 28 | #include "LogBuffer.h" |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 29 | #include "LogReader.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 30 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 31 | // Default |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 32 | #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] | 33 | #define log_buffer_size(id) mMaxSize[id] |
Mark Salyzyn | 57a0af9 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 34 | #define LOG_BUFFER_MIN_SIZE (64 * 1024UL) |
| 35 | #define LOG_BUFFER_MAX_SIZE (256 * 1024 * 1024UL) |
| 36 | |
| 37 | static bool valid_size(unsigned long value) { |
| 38 | if ((value < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < value)) { |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | long pages = sysconf(_SC_PHYS_PAGES); |
| 43 | if (pages < 1) { |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | long pagesize = sysconf(_SC_PAGESIZE); |
| 48 | if (pagesize <= 1) { |
| 49 | pagesize = PAGE_SIZE; |
| 50 | } |
| 51 | |
| 52 | // maximum memory impact a somewhat arbitrary ~3% |
| 53 | pages = (pages + 31) / 32; |
| 54 | unsigned long maximum = pages * pagesize; |
| 55 | |
| 56 | if ((maximum < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < maximum)) { |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | return value <= maximum; |
| 61 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 62 | |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 63 | static unsigned long property_get_size(const char *key) { |
| 64 | char property[PROPERTY_VALUE_MAX]; |
| 65 | property_get(key, property, ""); |
| 66 | |
| 67 | char *cp; |
| 68 | unsigned long value = strtoul(property, &cp, 10); |
| 69 | |
| 70 | switch(*cp) { |
| 71 | case 'm': |
| 72 | case 'M': |
| 73 | value *= 1024; |
| 74 | /* FALLTHRU */ |
| 75 | case 'k': |
| 76 | case 'K': |
| 77 | value *= 1024; |
| 78 | /* FALLTHRU */ |
| 79 | case '\0': |
| 80 | break; |
| 81 | |
| 82 | default: |
| 83 | value = 0; |
| 84 | } |
| 85 | |
Mark Salyzyn | 57a0af9 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 86 | if (!valid_size(value)) { |
| 87 | value = 0; |
| 88 | } |
| 89 | |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 90 | return value; |
| 91 | } |
| 92 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 93 | void LogBuffer::init() { |
Mark Salyzyn | 57a0af9 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 94 | static const char global_tuneable[] = "persist.logd.size"; // Settings App |
| 95 | static const char global_default[] = "ro.logd.size"; // BoardConfig.mk |
| 96 | |
| 97 | unsigned long default_size = property_get_size(global_tuneable); |
| 98 | if (!default_size) { |
| 99 | default_size = property_get_size(global_default); |
| 100 | } |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 101 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 102 | log_id_for_each(i) { |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 103 | char key[PROP_NAME_MAX]; |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 104 | |
Mark Salyzyn | 57a0af9 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 105 | snprintf(key, sizeof(key), "%s.%s", |
| 106 | global_tuneable, android_log_id_to_name(i)); |
| 107 | unsigned long property_size = property_get_size(key); |
| 108 | |
| 109 | if (!property_size) { |
| 110 | snprintf(key, sizeof(key), "%s.%s", |
| 111 | global_default, android_log_id_to_name(i)); |
| 112 | property_size = property_get_size(key); |
| 113 | } |
| 114 | |
| 115 | if (!property_size) { |
| 116 | property_size = default_size; |
| 117 | } |
| 118 | |
| 119 | if (!property_size) { |
| 120 | property_size = LOG_BUFFER_SIZE; |
| 121 | } |
| 122 | |
| 123 | if (setSize(i, property_size)) { |
| 124 | setSize(i, LOG_BUFFER_MIN_SIZE); |
| 125 | } |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 126 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Mark Salyzyn | 7718778 | 2015-05-12 15:21:31 -0700 | [diff] [blame^] | 129 | LogBuffer::LogBuffer(LastLogTimes *times) : mTimes(*times) { |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 130 | pthread_mutex_init(&mLogElementsLock, NULL); |
| 131 | |
| 132 | init(); |
| 133 | } |
| 134 | |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 135 | int LogBuffer::log(log_id_t log_id, log_time realtime, |
| 136 | uid_t uid, pid_t pid, pid_t tid, |
| 137 | const char *msg, unsigned short len) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 138 | if ((log_id >= LOG_ID_MAX) || (log_id < 0)) { |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 139 | return -EINVAL; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 140 | } |
| 141 | LogBufferElement *elem = new LogBufferElement(log_id, realtime, |
Mark Salyzyn | b992d0d | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 142 | uid, pid, tid, msg, len); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 143 | |
| 144 | pthread_mutex_lock(&mLogElementsLock); |
| 145 | |
| 146 | // Insert elements in time sorted order if possible |
| 147 | // NB: if end is region locked, place element at end of list |
| 148 | LogBufferElementCollection::iterator it = mLogElements.end(); |
| 149 | LogBufferElementCollection::iterator last = it; |
Mark Salyzyn | eae155e | 2014-10-13 16:49:47 -0700 | [diff] [blame] | 150 | while (last != mLogElements.begin()) { |
| 151 | --it; |
Mark Salyzyn | c03e72c | 2014-02-18 11:23:53 -0800 | [diff] [blame] | 152 | if ((*it)->getRealTime() <= realtime) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 153 | break; |
| 154 | } |
| 155 | last = it; |
| 156 | } |
Mark Salyzyn | c03e72c | 2014-02-18 11:23:53 -0800 | [diff] [blame] | 157 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 158 | if (last == mLogElements.end()) { |
| 159 | mLogElements.push_back(elem); |
| 160 | } else { |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 161 | uint64_t end = 1; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 162 | bool end_set = false; |
| 163 | bool end_always = false; |
| 164 | |
| 165 | LogTimeEntry::lock(); |
| 166 | |
| 167 | LastLogTimes::iterator t = mTimes.begin(); |
| 168 | while(t != mTimes.end()) { |
| 169 | LogTimeEntry *entry = (*t); |
| 170 | if (entry->owned_Locked()) { |
| 171 | if (!entry->mNonBlock) { |
| 172 | end_always = true; |
| 173 | break; |
| 174 | } |
| 175 | if (!end_set || (end <= entry->mEnd)) { |
| 176 | end = entry->mEnd; |
| 177 | end_set = true; |
| 178 | } |
| 179 | } |
| 180 | t++; |
| 181 | } |
| 182 | |
| 183 | if (end_always |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 184 | || (end_set && (end >= (*last)->getSequence()))) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 185 | mLogElements.push_back(elem); |
| 186 | } else { |
| 187 | mLogElements.insert(last,elem); |
| 188 | } |
| 189 | |
| 190 | LogTimeEntry::unlock(); |
| 191 | } |
| 192 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 193 | stats.add(elem); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 194 | maybePrune(log_id); |
| 195 | pthread_mutex_unlock(&mLogElementsLock); |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 196 | |
| 197 | return len; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | // If we're using more than 256K of memory for log entries, prune |
Mark Salyzyn | 740f9b4 | 2014-01-13 16:37:51 -0800 | [diff] [blame] | 201 | // at least 10% of the log entries. |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 202 | // |
| 203 | // mLogElementsLock must be held when this function is called. |
| 204 | void LogBuffer::maybePrune(log_id_t id) { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 205 | size_t sizes = stats.sizes(id); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 206 | if (sizes > log_buffer_size(id)) { |
| 207 | size_t sizeOver90Percent = sizes - ((log_buffer_size(id) * 9) / 10); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 208 | size_t elements = stats.elements(id); |
Mark Salyzyn | 740f9b4 | 2014-01-13 16:37:51 -0800 | [diff] [blame] | 209 | unsigned long pruneRows = elements * sizeOver90Percent / sizes; |
| 210 | elements /= 10; |
| 211 | if (pruneRows <= elements) { |
| 212 | pruneRows = elements; |
| 213 | } |
| 214 | prune(id, pruneRows); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 218 | LogBufferElementCollection::iterator LogBuffer::erase(LogBufferElementCollection::iterator it) { |
| 219 | LogBufferElement *e = *it; |
| 220 | |
| 221 | it = mLogElements.erase(it); |
| 222 | stats.subtract(e); |
| 223 | delete e; |
| 224 | |
| 225 | return it; |
| 226 | } |
| 227 | |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 228 | // Define a temporary mechanism to report the last LogBufferElement pointer |
| 229 | // for the specified uid, pid and tid. Used below to help merge-sort when |
| 230 | // pruning for worst UID. |
| 231 | class LogBufferElementKey { |
| 232 | const union { |
| 233 | struct { |
| 234 | uint16_t uid; |
| 235 | uint16_t pid; |
| 236 | uint16_t tid; |
| 237 | uint16_t padding; |
| 238 | } __packed; |
| 239 | uint64_t value; |
| 240 | } __packed; |
| 241 | |
| 242 | public: |
| 243 | LogBufferElementKey(uid_t u, pid_t p, pid_t t):uid(u),pid(p),tid(t),padding(0) { } |
| 244 | LogBufferElementKey(uint64_t k):value(k) { } |
| 245 | |
| 246 | uint64_t getKey() { return value; } |
| 247 | }; |
| 248 | |
Mark Salyzyn | e06a6e0 | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 249 | class LogBufferElementEntry { |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 250 | const uint64_t key; |
| 251 | LogBufferElement *last; |
| 252 | |
| 253 | public: |
| 254 | LogBufferElementEntry(const uint64_t &k, LogBufferElement *e):key(k),last(e) { } |
| 255 | |
| 256 | const uint64_t&getKey() const { return key; } |
| 257 | |
| 258 | LogBufferElement *getLast() { return last; } |
| 259 | }; |
| 260 | |
Mark Salyzyn | e06a6e0 | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 261 | class LogBufferElementLast : public android::BasicHashtable<uint64_t, LogBufferElementEntry> { |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 262 | |
Mark Salyzyn | e06a6e0 | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 263 | public: |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 264 | bool merge(LogBufferElement *e, unsigned short dropped) { |
| 265 | LogBufferElementKey key(e->getUid(), e->getPid(), e->getTid()); |
| 266 | android::hash_t hash = android::hash_type(key.getKey()); |
| 267 | ssize_t index = find(-1, hash, key.getKey()); |
| 268 | if (index != -1) { |
| 269 | LogBufferElementEntry &entry = editEntryAt(index); |
| 270 | LogBufferElement *l = entry.getLast(); |
| 271 | unsigned short d = l->getDropped(); |
| 272 | if ((dropped + d) > USHRT_MAX) { |
| 273 | removeAt(index); |
| 274 | } else { |
| 275 | l->setDropped(dropped + d); |
| 276 | return true; |
| 277 | } |
| 278 | } |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | size_t add(LogBufferElement *e) { |
| 283 | LogBufferElementKey key(e->getUid(), e->getPid(), e->getTid()); |
| 284 | android::hash_t hash = android::hash_type(key.getKey()); |
| 285 | return android::BasicHashtable<uint64_t, LogBufferElementEntry>:: |
| 286 | add(hash, LogBufferElementEntry(key.getKey(), e)); |
| 287 | } |
| 288 | |
Mark Salyzyn | e06a6e0 | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 289 | inline void clear() { |
| 290 | android::BasicHashtable<uint64_t, LogBufferElementEntry>::clear(); |
| 291 | } |
| 292 | |
| 293 | void clear(LogBufferElement *e) { |
| 294 | uint64_t current = e->getRealTime().nsec() - NS_PER_SEC; |
| 295 | ssize_t index = -1; |
| 296 | while((index = next(index)) >= 0) { |
Mark Salyzyn | 35173a9 | 2015-04-23 07:47:54 -0700 | [diff] [blame] | 297 | LogBufferElement *l = editEntryAt(index).getLast(); |
| 298 | if ((l->getDropped() >= 4) && (current > l->getRealTime().nsec())) { |
Mark Salyzyn | e06a6e0 | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 299 | removeAt(index); |
| 300 | index = -1; |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 305 | }; |
| 306 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 307 | // prune "pruneRows" of type "id" from the buffer. |
| 308 | // |
| 309 | // mLogElementsLock must be held when this function is called. |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 310 | void 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] | 311 | LogTimeEntry *oldest = NULL; |
| 312 | |
| 313 | LogTimeEntry::lock(); |
| 314 | |
| 315 | // Region locked? |
| 316 | LastLogTimes::iterator t = mTimes.begin(); |
| 317 | while(t != mTimes.end()) { |
| 318 | LogTimeEntry *entry = (*t); |
TraianX Schiau | da6495d | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 319 | if (entry->owned_Locked() && entry->isWatching(id) |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 320 | && (!oldest || (oldest->mStart > entry->mStart))) { |
| 321 | oldest = entry; |
| 322 | } |
| 323 | t++; |
| 324 | } |
| 325 | |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 326 | LogBufferElementCollection::iterator it; |
| 327 | |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 328 | if (caller_uid != AID_ROOT) { |
| 329 | for(it = mLogElements.begin(); it != mLogElements.end();) { |
| 330 | LogBufferElement *e = *it; |
| 331 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 332 | if (oldest && (oldest->mStart <= e->getSequence())) { |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 333 | break; |
| 334 | } |
| 335 | |
| 336 | if (e->getLogId() != id) { |
| 337 | ++it; |
| 338 | continue; |
| 339 | } |
| 340 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 341 | if (e->getUid() == caller_uid) { |
| 342 | it = erase(it); |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 343 | pruneRows--; |
| 344 | if (pruneRows == 0) { |
| 345 | break; |
| 346 | } |
| 347 | } else { |
| 348 | ++it; |
| 349 | } |
| 350 | } |
| 351 | LogTimeEntry::unlock(); |
| 352 | return; |
| 353 | } |
| 354 | |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 355 | // prune by worst offender by uid |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 356 | bool hasBlacklist = mPrune.naughty(); |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 357 | while (pruneRows > 0) { |
| 358 | // recalculate the worst offender on every batched pass |
| 359 | uid_t worst = (uid_t) -1; |
| 360 | size_t worst_sizes = 0; |
| 361 | size_t second_worst_sizes = 0; |
| 362 | |
Mark Salyzyn | ae76923 | 2015-03-17 17:17:25 -0700 | [diff] [blame] | 363 | if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) { |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 364 | std::unique_ptr<const UidEntry *[]> sorted = stats.sort(2, id); |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 365 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 366 | if (sorted.get()) { |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 367 | if (sorted[0] && sorted[1]) { |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 368 | worst_sizes = sorted[0]->getSizes(); |
Mark Salyzyn | d717d80 | 2015-04-20 15:36:12 -0700 | [diff] [blame] | 369 | // Calculate threshold as 12.5% of available storage |
| 370 | size_t threshold = log_buffer_size(id) / 8; |
| 371 | if (worst_sizes > threshold) { |
| 372 | worst = sorted[0]->getKey(); |
| 373 | second_worst_sizes = sorted[1]->getSizes(); |
| 374 | if (second_worst_sizes < threshold) { |
| 375 | second_worst_sizes = threshold; |
| 376 | } |
| 377 | } |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 378 | } |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 382 | // skip if we have neither worst nor naughty filters |
| 383 | if ((worst == (uid_t) -1) && !hasBlacklist) { |
| 384 | break; |
| 385 | } |
| 386 | |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 387 | bool kick = false; |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 388 | bool leading = true; |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 389 | LogBufferElementLast last; |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 390 | for(it = mLogElements.begin(); it != mLogElements.end();) { |
| 391 | LogBufferElement *e = *it; |
| 392 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 393 | if (oldest && (oldest->mStart <= e->getSequence())) { |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 394 | break; |
| 395 | } |
| 396 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 397 | if (e->getLogId() != id) { |
| 398 | ++it; |
| 399 | continue; |
| 400 | } |
| 401 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 402 | unsigned short dropped = e->getDropped(); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 403 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 404 | // remove any leading drops |
| 405 | if (leading && dropped) { |
| 406 | it = erase(it); |
| 407 | continue; |
| 408 | } |
| 409 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 410 | // merge any drops |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 411 | if (dropped && last.merge(e, dropped)) { |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 412 | it = mLogElements.erase(it); |
| 413 | stats.erase(e); |
| 414 | delete e; |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 415 | continue; |
| 416 | } |
| 417 | |
| 418 | leading = false; |
| 419 | |
| 420 | if (hasBlacklist && mPrune.naughty(e)) { |
Mark Salyzyn | e06a6e0 | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 421 | last.clear(e); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 422 | it = erase(it); |
| 423 | if (dropped) { |
| 424 | continue; |
| 425 | } |
| 426 | |
| 427 | pruneRows--; |
| 428 | if (pruneRows == 0) { |
| 429 | break; |
| 430 | } |
| 431 | |
| 432 | if (e->getUid() == worst) { |
| 433 | kick = true; |
| 434 | if (worst_sizes < second_worst_sizes) { |
| 435 | break; |
| 436 | } |
| 437 | worst_sizes -= e->getMsgLen(); |
| 438 | } |
| 439 | continue; |
| 440 | } |
| 441 | |
| 442 | if (dropped) { |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 443 | last.add(e); |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 444 | ++it; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 445 | continue; |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 446 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 447 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 448 | if (e->getUid() != worst) { |
Mark Salyzyn | e06a6e0 | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 449 | last.clear(e); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 450 | ++it; |
| 451 | continue; |
| 452 | } |
| 453 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 454 | pruneRows--; |
| 455 | if (pruneRows == 0) { |
| 456 | break; |
| 457 | } |
| 458 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 459 | kick = true; |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 460 | |
| 461 | unsigned short len = e->getMsgLen(); |
| 462 | stats.drop(e); |
| 463 | e->setDropped(1); |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 464 | if (last.merge(e, 1)) { |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 465 | it = mLogElements.erase(it); |
| 466 | stats.erase(e); |
| 467 | delete e; |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 468 | } else { |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 469 | last.add(e); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 470 | ++it; |
| 471 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 472 | if (worst_sizes < second_worst_sizes) { |
| 473 | break; |
| 474 | } |
| 475 | worst_sizes -= len; |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 476 | } |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 477 | last.clear(); |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 478 | |
Mark Salyzyn | 1c95047 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 479 | if (!kick || !mPrune.worstUidEnabled()) { |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 480 | break; // the following loop will ask bad clients to skip/drop |
| 481 | } |
| 482 | } |
| 483 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 484 | bool whitelist = false; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 485 | bool hasWhitelist = mPrune.nice(); |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 486 | it = mLogElements.begin(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 487 | while((pruneRows > 0) && (it != mLogElements.end())) { |
| 488 | LogBufferElement *e = *it; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 489 | |
| 490 | if (e->getLogId() != id) { |
| 491 | it++; |
| 492 | continue; |
| 493 | } |
| 494 | |
| 495 | if (oldest && (oldest->mStart <= e->getSequence())) { |
| 496 | if (whitelist) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 497 | break; |
| 498 | } |
Mark Salyzyn | 1c95047 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 499 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 500 | if (stats.sizes(id) > (2 * log_buffer_size(id))) { |
| 501 | // kick a misbehaving log reader client off the island |
| 502 | oldest->release_Locked(); |
| 503 | } else { |
| 504 | oldest->triggerSkip_Locked(id, pruneRows); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 505 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 506 | break; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 507 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 508 | |
| 509 | if (hasWhitelist && mPrune.nice(e)) { // WhiteListed |
| 510 | whitelist = true; |
| 511 | it++; |
| 512 | continue; |
| 513 | } |
| 514 | |
| 515 | it = erase(it); |
| 516 | pruneRows--; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 517 | } |
| 518 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 519 | // Do not save the whitelist if we are reader range limited |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 520 | if (whitelist && (pruneRows > 0)) { |
| 521 | it = mLogElements.begin(); |
| 522 | while((it != mLogElements.end()) && (pruneRows > 0)) { |
| 523 | LogBufferElement *e = *it; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 524 | |
| 525 | if (e->getLogId() != id) { |
| 526 | ++it; |
| 527 | continue; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 528 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 529 | |
| 530 | if (oldest && (oldest->mStart <= e->getSequence())) { |
| 531 | if (stats.sizes(id) > (2 * log_buffer_size(id))) { |
| 532 | // kick a misbehaving log reader client off the island |
| 533 | oldest->release_Locked(); |
| 534 | } else { |
| 535 | oldest->triggerSkip_Locked(id, pruneRows); |
| 536 | } |
| 537 | break; |
| 538 | } |
| 539 | |
| 540 | it = erase(it); |
| 541 | pruneRows--; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 542 | } |
| 543 | } |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 544 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 545 | LogTimeEntry::unlock(); |
| 546 | } |
| 547 | |
| 548 | // clear all rows of type "id" from the buffer. |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 549 | void LogBuffer::clear(log_id_t id, uid_t uid) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 550 | pthread_mutex_lock(&mLogElementsLock); |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 551 | prune(id, ULONG_MAX, uid); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 552 | pthread_mutex_unlock(&mLogElementsLock); |
| 553 | } |
| 554 | |
| 555 | // get the used space associated with "id". |
| 556 | unsigned long LogBuffer::getSizeUsed(log_id_t id) { |
| 557 | pthread_mutex_lock(&mLogElementsLock); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 558 | size_t retval = stats.sizes(id); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 559 | pthread_mutex_unlock(&mLogElementsLock); |
| 560 | return retval; |
| 561 | } |
| 562 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 563 | // set the total space allocated to "id" |
| 564 | int LogBuffer::setSize(log_id_t id, unsigned long size) { |
| 565 | // Reasonable limits ... |
Mark Salyzyn | 57a0af9 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 566 | if (!valid_size(size)) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 567 | return -1; |
| 568 | } |
| 569 | pthread_mutex_lock(&mLogElementsLock); |
| 570 | log_buffer_size(id) = size; |
| 571 | pthread_mutex_unlock(&mLogElementsLock); |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | // get the total space allocated to "id" |
| 576 | unsigned long LogBuffer::getSize(log_id_t id) { |
| 577 | pthread_mutex_lock(&mLogElementsLock); |
| 578 | size_t retval = log_buffer_size(id); |
| 579 | pthread_mutex_unlock(&mLogElementsLock); |
| 580 | return retval; |
| 581 | } |
| 582 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 583 | uint64_t LogBuffer::flushTo( |
| 584 | SocketClient *reader, const uint64_t start, bool privileged, |
| 585 | int (*filter)(const LogBufferElement *element, void *arg), void *arg) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 586 | LogBufferElementCollection::iterator it; |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 587 | uint64_t max = start; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 588 | uid_t uid = reader->getUid(); |
| 589 | |
| 590 | pthread_mutex_lock(&mLogElementsLock); |
Dragoslav Mitrinovic | 8e8e8db | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 591 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 592 | if (start <= 1) { |
Dragoslav Mitrinovic | 8e8e8db | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 593 | // client wants to start from the beginning |
| 594 | it = mLogElements.begin(); |
| 595 | } else { |
| 596 | // Client wants to start from some specified time. Chances are |
| 597 | // we are better off starting from the end of the time sorted list. |
| 598 | for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) { |
| 599 | --it; |
| 600 | LogBufferElement *element = *it; |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 601 | if (element->getSequence() <= start) { |
Dragoslav Mitrinovic | 8e8e8db | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 602 | it++; |
| 603 | break; |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | for (; it != mLogElements.end(); ++it) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 609 | LogBufferElement *element = *it; |
| 610 | |
| 611 | if (!privileged && (element->getUid() != uid)) { |
| 612 | continue; |
| 613 | } |
| 614 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 615 | if (element->getSequence() <= start) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 616 | continue; |
| 617 | } |
| 618 | |
| 619 | // NB: calling out to another object with mLogElementsLock held (safe) |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 620 | if (filter) { |
| 621 | int ret = (*filter)(element, arg); |
| 622 | if (ret == false) { |
| 623 | continue; |
| 624 | } |
| 625 | if (ret != true) { |
| 626 | break; |
| 627 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | pthread_mutex_unlock(&mLogElementsLock); |
| 631 | |
| 632 | // range locking in LastLogTimes looks after us |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 633 | max = element->flushTo(reader, this); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 634 | |
| 635 | if (max == element->FLUSH_ERROR) { |
| 636 | return max; |
| 637 | } |
| 638 | |
| 639 | pthread_mutex_lock(&mLogElementsLock); |
| 640 | } |
| 641 | pthread_mutex_unlock(&mLogElementsLock); |
| 642 | |
| 643 | return max; |
| 644 | } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 645 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 646 | void LogBuffer::formatStatistics(char **strp, uid_t uid, unsigned int logMask) { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 647 | pthread_mutex_lock(&mLogElementsLock); |
| 648 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 649 | stats.format(strp, uid, logMask); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 650 | |
| 651 | pthread_mutex_unlock(&mLogElementsLock); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 652 | } |