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 | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 18 | #include <stdio.h> |
| 19 | #include <string.h> |
Mark Salyzyn | 57a0af9 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 20 | #include <sys/user.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 21 | #include <time.h> |
| 22 | #include <unistd.h> |
| 23 | |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 24 | #include <cutils/properties.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 25 | #include <log/logger.h> |
| 26 | |
| 27 | #include "LogBuffer.h" |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 28 | #include "LogReader.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 29 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 30 | // Default |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 31 | #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] | 32 | #define log_buffer_size(id) mMaxSize[id] |
Mark Salyzyn | 57a0af9 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 33 | #define LOG_BUFFER_MIN_SIZE (64 * 1024UL) |
| 34 | #define LOG_BUFFER_MAX_SIZE (256 * 1024 * 1024UL) |
| 35 | |
| 36 | static bool valid_size(unsigned long value) { |
| 37 | if ((value < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < value)) { |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | long pages = sysconf(_SC_PHYS_PAGES); |
| 42 | if (pages < 1) { |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | long pagesize = sysconf(_SC_PAGESIZE); |
| 47 | if (pagesize <= 1) { |
| 48 | pagesize = PAGE_SIZE; |
| 49 | } |
| 50 | |
| 51 | // maximum memory impact a somewhat arbitrary ~3% |
| 52 | pages = (pages + 31) / 32; |
| 53 | unsigned long maximum = pages * pagesize; |
| 54 | |
| 55 | if ((maximum < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < maximum)) { |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | return value <= maximum; |
| 60 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 61 | |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 62 | static unsigned long property_get_size(const char *key) { |
| 63 | char property[PROPERTY_VALUE_MAX]; |
| 64 | property_get(key, property, ""); |
| 65 | |
| 66 | char *cp; |
| 67 | unsigned long value = strtoul(property, &cp, 10); |
| 68 | |
| 69 | switch(*cp) { |
| 70 | case 'm': |
| 71 | case 'M': |
| 72 | value *= 1024; |
| 73 | /* FALLTHRU */ |
| 74 | case 'k': |
| 75 | case 'K': |
| 76 | value *= 1024; |
| 77 | /* FALLTHRU */ |
| 78 | case '\0': |
| 79 | break; |
| 80 | |
| 81 | default: |
| 82 | value = 0; |
| 83 | } |
| 84 | |
Mark Salyzyn | 57a0af9 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 85 | if (!valid_size(value)) { |
| 86 | value = 0; |
| 87 | } |
| 88 | |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 89 | return value; |
| 90 | } |
| 91 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 92 | void LogBuffer::init() { |
Mark Salyzyn | 57a0af9 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 93 | static const char global_tuneable[] = "persist.logd.size"; // Settings App |
| 94 | static const char global_default[] = "ro.logd.size"; // BoardConfig.mk |
| 95 | |
| 96 | unsigned long default_size = property_get_size(global_tuneable); |
| 97 | if (!default_size) { |
| 98 | default_size = property_get_size(global_default); |
| 99 | } |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 100 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 101 | log_id_for_each(i) { |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 102 | char key[PROP_NAME_MAX]; |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 103 | |
Mark Salyzyn | 57a0af9 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 104 | snprintf(key, sizeof(key), "%s.%s", |
| 105 | global_tuneable, android_log_id_to_name(i)); |
| 106 | unsigned long property_size = property_get_size(key); |
| 107 | |
| 108 | if (!property_size) { |
| 109 | snprintf(key, sizeof(key), "%s.%s", |
| 110 | global_default, android_log_id_to_name(i)); |
| 111 | property_size = property_get_size(key); |
| 112 | } |
| 113 | |
| 114 | if (!property_size) { |
| 115 | property_size = default_size; |
| 116 | } |
| 117 | |
| 118 | if (!property_size) { |
| 119 | property_size = LOG_BUFFER_SIZE; |
| 120 | } |
| 121 | |
| 122 | if (setSize(i, property_size)) { |
| 123 | setSize(i, LOG_BUFFER_MIN_SIZE); |
| 124 | } |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 125 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 128 | LogBuffer::LogBuffer(LastLogTimes *times) |
| 129 | : mTimes(*times) { |
| 130 | pthread_mutex_init(&mLogElementsLock, NULL); |
| 131 | |
| 132 | init(); |
| 133 | } |
| 134 | |
Mark Salyzyn | 7e2f83c | 2014-03-05 07:41:49 -0800 | [diff] [blame] | 135 | void LogBuffer::log(log_id_t log_id, log_time realtime, |
Mark Salyzyn | b992d0d | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 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)) { |
| 139 | return; |
| 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); |
| 196 | } |
| 197 | |
| 198 | // 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] | 199 | // at least 10% of the log entries. |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 200 | // |
| 201 | // mLogElementsLock must be held when this function is called. |
| 202 | void LogBuffer::maybePrune(log_id_t id) { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 203 | size_t sizes = stats.sizes(id); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 204 | if (sizes > log_buffer_size(id)) { |
| 205 | size_t sizeOver90Percent = sizes - ((log_buffer_size(id) * 9) / 10); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 206 | size_t elements = stats.elements(id); |
Mark Salyzyn | 740f9b4 | 2014-01-13 16:37:51 -0800 | [diff] [blame] | 207 | unsigned long pruneRows = elements * sizeOver90Percent / sizes; |
| 208 | elements /= 10; |
| 209 | if (pruneRows <= elements) { |
| 210 | pruneRows = elements; |
| 211 | } |
| 212 | prune(id, pruneRows); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 216 | LogBufferElementCollection::iterator LogBuffer::erase(LogBufferElementCollection::iterator it) { |
| 217 | LogBufferElement *e = *it; |
| 218 | |
| 219 | it = mLogElements.erase(it); |
| 220 | stats.subtract(e); |
| 221 | delete e; |
| 222 | |
| 223 | return it; |
| 224 | } |
| 225 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 226 | // prune "pruneRows" of type "id" from the buffer. |
| 227 | // |
| 228 | // mLogElementsLock must be held when this function is called. |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 229 | 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] | 230 | LogTimeEntry *oldest = NULL; |
| 231 | |
| 232 | LogTimeEntry::lock(); |
| 233 | |
| 234 | // Region locked? |
| 235 | LastLogTimes::iterator t = mTimes.begin(); |
| 236 | while(t != mTimes.end()) { |
| 237 | LogTimeEntry *entry = (*t); |
TraianX Schiau | da6495d | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 238 | if (entry->owned_Locked() && entry->isWatching(id) |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 239 | && (!oldest || (oldest->mStart > entry->mStart))) { |
| 240 | oldest = entry; |
| 241 | } |
| 242 | t++; |
| 243 | } |
| 244 | |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 245 | LogBufferElementCollection::iterator it; |
| 246 | |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 247 | if (caller_uid != AID_ROOT) { |
| 248 | for(it = mLogElements.begin(); it != mLogElements.end();) { |
| 249 | LogBufferElement *e = *it; |
| 250 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 251 | if (oldest && (oldest->mStart <= e->getSequence())) { |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 252 | break; |
| 253 | } |
| 254 | |
| 255 | if (e->getLogId() != id) { |
| 256 | ++it; |
| 257 | continue; |
| 258 | } |
| 259 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 260 | if (e->getUid() == caller_uid) { |
| 261 | it = erase(it); |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 262 | pruneRows--; |
| 263 | if (pruneRows == 0) { |
| 264 | break; |
| 265 | } |
| 266 | } else { |
| 267 | ++it; |
| 268 | } |
| 269 | } |
| 270 | LogTimeEntry::unlock(); |
| 271 | return; |
| 272 | } |
| 273 | |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 274 | // prune by worst offender by uid |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 275 | bool hasBlacklist = mPrune.naughty(); |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 276 | while (pruneRows > 0) { |
| 277 | // recalculate the worst offender on every batched pass |
| 278 | uid_t worst = (uid_t) -1; |
| 279 | size_t worst_sizes = 0; |
| 280 | size_t second_worst_sizes = 0; |
| 281 | |
Mark Salyzyn | 99f47a9 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 282 | if ((id != LOG_ID_CRASH) && mPrune.worstUidEnabled()) { |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 283 | std::unique_ptr<const UidEntry *[]> sorted = stats.sort(2, id); |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 284 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 285 | if (sorted.get()) { |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 286 | if (sorted[0] && sorted[1]) { |
| 287 | worst = sorted[0]->getKey(); |
| 288 | worst_sizes = sorted[0]->getSizes(); |
| 289 | second_worst_sizes = sorted[1]->getSizes(); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 290 | } |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 294 | // skip if we have neither worst nor naughty filters |
| 295 | if ((worst == (uid_t) -1) && !hasBlacklist) { |
| 296 | break; |
| 297 | } |
| 298 | |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 299 | bool kick = false; |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame^] | 300 | bool leading = true; |
| 301 | LogBufferElement *last = NULL; |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 302 | for(it = mLogElements.begin(); it != mLogElements.end();) { |
| 303 | LogBufferElement *e = *it; |
| 304 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 305 | if (oldest && (oldest->mStart <= e->getSequence())) { |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 306 | break; |
| 307 | } |
| 308 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 309 | if (e->getLogId() != id) { |
| 310 | ++it; |
| 311 | continue; |
| 312 | } |
| 313 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame^] | 314 | unsigned short dropped = e->getDropped(); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 315 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame^] | 316 | // remove any leading drops |
| 317 | if (leading && dropped) { |
| 318 | it = erase(it); |
| 319 | continue; |
| 320 | } |
| 321 | |
| 322 | pid_t pid = e->getPid(); |
| 323 | |
| 324 | // merge any drops |
| 325 | if (last && dropped |
| 326 | && ((dropped + last->getDropped()) < USHRT_MAX) |
| 327 | && (last->getPid() == pid) |
| 328 | && (last->getTid() == e->getTid())) { |
| 329 | it = mLogElements.erase(it); |
| 330 | stats.erase(e); |
| 331 | delete e; |
| 332 | last->setDropped(dropped + last->getDropped()); |
| 333 | continue; |
| 334 | } |
| 335 | |
| 336 | leading = false; |
| 337 | |
| 338 | if (hasBlacklist && mPrune.naughty(e)) { |
| 339 | last = NULL; |
| 340 | it = erase(it); |
| 341 | if (dropped) { |
| 342 | continue; |
| 343 | } |
| 344 | |
| 345 | pruneRows--; |
| 346 | if (pruneRows == 0) { |
| 347 | break; |
| 348 | } |
| 349 | |
| 350 | if (e->getUid() == worst) { |
| 351 | kick = true; |
| 352 | if (worst_sizes < second_worst_sizes) { |
| 353 | break; |
| 354 | } |
| 355 | worst_sizes -= e->getMsgLen(); |
| 356 | } |
| 357 | continue; |
| 358 | } |
| 359 | |
| 360 | if (dropped) { |
| 361 | last = e; |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 362 | ++it; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 363 | continue; |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 364 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 365 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame^] | 366 | if (e->getUid() != worst) { |
| 367 | last = NULL; |
| 368 | ++it; |
| 369 | continue; |
| 370 | } |
| 371 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 372 | pruneRows--; |
| 373 | if (pruneRows == 0) { |
| 374 | break; |
| 375 | } |
| 376 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 377 | kick = true; |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame^] | 378 | |
| 379 | unsigned short len = e->getMsgLen(); |
| 380 | stats.drop(e); |
| 381 | e->setDropped(1); |
| 382 | // merge any drops |
| 383 | if (last |
| 384 | && (last->getDropped() < (USHRT_MAX - 1)) |
| 385 | && (last->getPid() == pid) |
| 386 | && (last->getTid() == e->getTid())) { |
| 387 | it = mLogElements.erase(it); |
| 388 | stats.erase(e); |
| 389 | delete e; |
| 390 | last->setDropped(last->getDropped() + 1); |
| 391 | } else { |
| 392 | last = e; |
| 393 | ++it; |
| 394 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 395 | if (worst_sizes < second_worst_sizes) { |
| 396 | break; |
| 397 | } |
| 398 | worst_sizes -= len; |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 399 | } |
| 400 | |
Mark Salyzyn | 1c95047 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 401 | if (!kick || !mPrune.worstUidEnabled()) { |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 402 | break; // the following loop will ask bad clients to skip/drop |
| 403 | } |
| 404 | } |
| 405 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 406 | bool whitelist = false; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 407 | bool hasWhitelist = mPrune.nice(); |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 408 | it = mLogElements.begin(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 409 | while((pruneRows > 0) && (it != mLogElements.end())) { |
| 410 | LogBufferElement *e = *it; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 411 | |
| 412 | if (e->getLogId() != id) { |
| 413 | it++; |
| 414 | continue; |
| 415 | } |
| 416 | |
| 417 | if (oldest && (oldest->mStart <= e->getSequence())) { |
| 418 | if (whitelist) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 419 | break; |
| 420 | } |
Mark Salyzyn | 1c95047 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 421 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 422 | if (stats.sizes(id) > (2 * log_buffer_size(id))) { |
| 423 | // kick a misbehaving log reader client off the island |
| 424 | oldest->release_Locked(); |
| 425 | } else { |
| 426 | oldest->triggerSkip_Locked(id, pruneRows); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 427 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 428 | break; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 429 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 430 | |
| 431 | if (hasWhitelist && mPrune.nice(e)) { // WhiteListed |
| 432 | whitelist = true; |
| 433 | it++; |
| 434 | continue; |
| 435 | } |
| 436 | |
| 437 | it = erase(it); |
| 438 | pruneRows--; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 439 | } |
| 440 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 441 | // Do not save the whitelist if we are reader range limited |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 442 | if (whitelist && (pruneRows > 0)) { |
| 443 | it = mLogElements.begin(); |
| 444 | while((it != mLogElements.end()) && (pruneRows > 0)) { |
| 445 | LogBufferElement *e = *it; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 446 | |
| 447 | if (e->getLogId() != id) { |
| 448 | ++it; |
| 449 | continue; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 450 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 451 | |
| 452 | if (oldest && (oldest->mStart <= e->getSequence())) { |
| 453 | if (stats.sizes(id) > (2 * log_buffer_size(id))) { |
| 454 | // kick a misbehaving log reader client off the island |
| 455 | oldest->release_Locked(); |
| 456 | } else { |
| 457 | oldest->triggerSkip_Locked(id, pruneRows); |
| 458 | } |
| 459 | break; |
| 460 | } |
| 461 | |
| 462 | it = erase(it); |
| 463 | pruneRows--; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 464 | } |
| 465 | } |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 466 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 467 | LogTimeEntry::unlock(); |
| 468 | } |
| 469 | |
| 470 | // clear all rows of type "id" from the buffer. |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 471 | void LogBuffer::clear(log_id_t id, uid_t uid) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 472 | pthread_mutex_lock(&mLogElementsLock); |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 473 | prune(id, ULONG_MAX, uid); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 474 | pthread_mutex_unlock(&mLogElementsLock); |
| 475 | } |
| 476 | |
| 477 | // get the used space associated with "id". |
| 478 | unsigned long LogBuffer::getSizeUsed(log_id_t id) { |
| 479 | pthread_mutex_lock(&mLogElementsLock); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 480 | size_t retval = stats.sizes(id); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 481 | pthread_mutex_unlock(&mLogElementsLock); |
| 482 | return retval; |
| 483 | } |
| 484 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 485 | // set the total space allocated to "id" |
| 486 | int LogBuffer::setSize(log_id_t id, unsigned long size) { |
| 487 | // Reasonable limits ... |
Mark Salyzyn | 57a0af9 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 488 | if (!valid_size(size)) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 489 | return -1; |
| 490 | } |
| 491 | pthread_mutex_lock(&mLogElementsLock); |
| 492 | log_buffer_size(id) = size; |
| 493 | pthread_mutex_unlock(&mLogElementsLock); |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | // get the total space allocated to "id" |
| 498 | unsigned long LogBuffer::getSize(log_id_t id) { |
| 499 | pthread_mutex_lock(&mLogElementsLock); |
| 500 | size_t retval = log_buffer_size(id); |
| 501 | pthread_mutex_unlock(&mLogElementsLock); |
| 502 | return retval; |
| 503 | } |
| 504 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 505 | uint64_t LogBuffer::flushTo( |
| 506 | SocketClient *reader, const uint64_t start, bool privileged, |
| 507 | int (*filter)(const LogBufferElement *element, void *arg), void *arg) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 508 | LogBufferElementCollection::iterator it; |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 509 | uint64_t max = start; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 510 | uid_t uid = reader->getUid(); |
| 511 | |
| 512 | pthread_mutex_lock(&mLogElementsLock); |
Dragoslav Mitrinovic | 8e8e8db | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 513 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 514 | if (start <= 1) { |
Dragoslav Mitrinovic | 8e8e8db | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 515 | // client wants to start from the beginning |
| 516 | it = mLogElements.begin(); |
| 517 | } else { |
| 518 | // Client wants to start from some specified time. Chances are |
| 519 | // we are better off starting from the end of the time sorted list. |
| 520 | for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) { |
| 521 | --it; |
| 522 | LogBufferElement *element = *it; |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 523 | if (element->getSequence() <= start) { |
Dragoslav Mitrinovic | 8e8e8db | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 524 | it++; |
| 525 | break; |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | for (; it != mLogElements.end(); ++it) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 531 | LogBufferElement *element = *it; |
| 532 | |
| 533 | if (!privileged && (element->getUid() != uid)) { |
| 534 | continue; |
| 535 | } |
| 536 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 537 | if (element->getSequence() <= start) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 538 | continue; |
| 539 | } |
| 540 | |
| 541 | // NB: calling out to another object with mLogElementsLock held (safe) |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 542 | if (filter) { |
| 543 | int ret = (*filter)(element, arg); |
| 544 | if (ret == false) { |
| 545 | continue; |
| 546 | } |
| 547 | if (ret != true) { |
| 548 | break; |
| 549 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | pthread_mutex_unlock(&mLogElementsLock); |
| 553 | |
| 554 | // range locking in LastLogTimes looks after us |
| 555 | max = element->flushTo(reader); |
| 556 | |
| 557 | if (max == element->FLUSH_ERROR) { |
| 558 | return max; |
| 559 | } |
| 560 | |
| 561 | pthread_mutex_lock(&mLogElementsLock); |
| 562 | } |
| 563 | pthread_mutex_unlock(&mLogElementsLock); |
| 564 | |
| 565 | return max; |
| 566 | } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 567 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 568 | void LogBuffer::formatStatistics(char **strp, uid_t uid, unsigned int logMask) { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 569 | pthread_mutex_lock(&mLogElementsLock); |
| 570 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 571 | stats.format(strp, uid, logMask); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 572 | |
| 573 | pthread_mutex_unlock(&mLogElementsLock); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 574 | } |