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 | |
| 17 | #include <stdio.h> |
| 18 | #include <string.h> |
| 19 | #include <time.h> |
| 20 | #include <unistd.h> |
| 21 | |
| 22 | #include <log/logger.h> |
| 23 | |
| 24 | #include "LogBuffer.h" |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 25 | #include "LogStatistics.h" |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 26 | #include "LogWhiteBlackList.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 27 | #include "LogReader.h" |
| 28 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 29 | // Default |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 30 | #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] | 31 | #ifdef USERDEBUG_BUILD |
| 32 | #define log_buffer_size(id) mMaxSize[id] |
| 33 | #else |
| 34 | #define log_buffer_size(id) LOG_BUFFER_SIZE |
| 35 | #endif |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 36 | |
| 37 | LogBuffer::LogBuffer(LastLogTimes *times) |
| 38 | : mTimes(*times) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 39 | pthread_mutex_init(&mLogElementsLock, NULL); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 40 | #ifdef USERDEBUG_BUILD |
| 41 | log_id_for_each(i) { |
| 42 | mMaxSize[i] = LOG_BUFFER_SIZE; |
| 43 | } |
| 44 | #endif |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Mark Salyzyn | 7e2f83c | 2014-03-05 07:41:49 -0800 | [diff] [blame] | 47 | void LogBuffer::log(log_id_t log_id, log_time realtime, |
Mark Salyzyn | b992d0d | 2014-03-20 16:09:38 -0700 | [diff] [blame^] | 48 | uid_t uid, pid_t pid, pid_t tid, |
| 49 | const char *msg, unsigned short len) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 50 | if ((log_id >= LOG_ID_MAX) || (log_id < 0)) { |
| 51 | return; |
| 52 | } |
| 53 | LogBufferElement *elem = new LogBufferElement(log_id, realtime, |
Mark Salyzyn | b992d0d | 2014-03-20 16:09:38 -0700 | [diff] [blame^] | 54 | uid, pid, tid, msg, len); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 55 | |
| 56 | pthread_mutex_lock(&mLogElementsLock); |
| 57 | |
| 58 | // Insert elements in time sorted order if possible |
| 59 | // NB: if end is region locked, place element at end of list |
| 60 | LogBufferElementCollection::iterator it = mLogElements.end(); |
| 61 | LogBufferElementCollection::iterator last = it; |
| 62 | while (--it != mLogElements.begin()) { |
Mark Salyzyn | c03e72c | 2014-02-18 11:23:53 -0800 | [diff] [blame] | 63 | if ((*it)->getRealTime() <= realtime) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 64 | break; |
| 65 | } |
| 66 | last = it; |
| 67 | } |
Mark Salyzyn | c03e72c | 2014-02-18 11:23:53 -0800 | [diff] [blame] | 68 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 69 | if (last == mLogElements.end()) { |
| 70 | mLogElements.push_back(elem); |
| 71 | } else { |
| 72 | log_time end; |
| 73 | bool end_set = false; |
| 74 | bool end_always = false; |
| 75 | |
| 76 | LogTimeEntry::lock(); |
| 77 | |
| 78 | LastLogTimes::iterator t = mTimes.begin(); |
| 79 | while(t != mTimes.end()) { |
| 80 | LogTimeEntry *entry = (*t); |
| 81 | if (entry->owned_Locked()) { |
| 82 | if (!entry->mNonBlock) { |
| 83 | end_always = true; |
| 84 | break; |
| 85 | } |
| 86 | if (!end_set || (end <= entry->mEnd)) { |
| 87 | end = entry->mEnd; |
| 88 | end_set = true; |
| 89 | } |
| 90 | } |
| 91 | t++; |
| 92 | } |
| 93 | |
| 94 | if (end_always |
Mark Salyzyn | c03e72c | 2014-02-18 11:23:53 -0800 | [diff] [blame] | 95 | || (end_set && (end >= (*last)->getMonotonicTime()))) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 96 | mLogElements.push_back(elem); |
| 97 | } else { |
| 98 | mLogElements.insert(last,elem); |
| 99 | } |
| 100 | |
| 101 | LogTimeEntry::unlock(); |
| 102 | } |
| 103 | |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 104 | stats.add(len, log_id, uid, pid); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 105 | maybePrune(log_id); |
| 106 | pthread_mutex_unlock(&mLogElementsLock); |
| 107 | } |
| 108 | |
| 109 | // 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] | 110 | // at least 10% of the log entries. |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 111 | // |
| 112 | // mLogElementsLock must be held when this function is called. |
| 113 | void LogBuffer::maybePrune(log_id_t id) { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 114 | size_t sizes = stats.sizes(id); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 115 | if (sizes > log_buffer_size(id)) { |
| 116 | size_t sizeOver90Percent = sizes - ((log_buffer_size(id) * 9) / 10); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 117 | size_t elements = stats.elements(id); |
Mark Salyzyn | 740f9b4 | 2014-01-13 16:37:51 -0800 | [diff] [blame] | 118 | unsigned long pruneRows = elements * sizeOver90Percent / sizes; |
| 119 | elements /= 10; |
| 120 | if (pruneRows <= elements) { |
| 121 | pruneRows = elements; |
| 122 | } |
| 123 | prune(id, pruneRows); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | // prune "pruneRows" of type "id" from the buffer. |
| 128 | // |
| 129 | // mLogElementsLock must be held when this function is called. |
| 130 | void LogBuffer::prune(log_id_t id, unsigned long pruneRows) { |
| 131 | LogTimeEntry *oldest = NULL; |
| 132 | |
| 133 | LogTimeEntry::lock(); |
| 134 | |
| 135 | // Region locked? |
| 136 | LastLogTimes::iterator t = mTimes.begin(); |
| 137 | while(t != mTimes.end()) { |
| 138 | LogTimeEntry *entry = (*t); |
| 139 | if (entry->owned_Locked() |
| 140 | && (!oldest || (oldest->mStart > entry->mStart))) { |
| 141 | oldest = entry; |
| 142 | } |
| 143 | t++; |
| 144 | } |
| 145 | |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 146 | LogBufferElementCollection::iterator it; |
| 147 | |
| 148 | // prune by worst offender by uid |
| 149 | while (pruneRows > 0) { |
| 150 | // recalculate the worst offender on every batched pass |
| 151 | uid_t worst = (uid_t) -1; |
| 152 | size_t worst_sizes = 0; |
| 153 | size_t second_worst_sizes = 0; |
| 154 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 155 | #ifdef USERDEBUG_BUILD |
| 156 | if (mPrune.worstUidEnabled()) |
| 157 | #endif |
| 158 | { |
| 159 | LidStatistics &l = stats.id(id); |
| 160 | UidStatisticsCollection::iterator iu; |
| 161 | for (iu = l.begin(); iu != l.end(); ++iu) { |
| 162 | UidStatistics *u = (*iu); |
| 163 | size_t sizes = u->sizes(); |
| 164 | if (worst_sizes < sizes) { |
| 165 | second_worst_sizes = worst_sizes; |
| 166 | worst_sizes = sizes; |
| 167 | worst = u->getUid(); |
| 168 | } |
| 169 | if ((second_worst_sizes < sizes) && (sizes < worst_sizes)) { |
| 170 | second_worst_sizes = sizes; |
| 171 | } |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | bool kick = false; |
| 176 | for(it = mLogElements.begin(); it != mLogElements.end();) { |
| 177 | LogBufferElement *e = *it; |
| 178 | |
| 179 | if (oldest && (oldest->mStart <= e->getMonotonicTime())) { |
| 180 | break; |
| 181 | } |
| 182 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 183 | if (e->getLogId() != id) { |
| 184 | ++it; |
| 185 | continue; |
| 186 | } |
| 187 | |
| 188 | uid_t uid = e->getUid(); |
| 189 | |
| 190 | if (uid == worst) { |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 191 | it = mLogElements.erase(it); |
| 192 | unsigned short len = e->getMsgLen(); |
| 193 | stats.subtract(len, id, worst, e->getPid()); |
| 194 | delete e; |
| 195 | kick = true; |
| 196 | pruneRows--; |
| 197 | if ((pruneRows == 0) || (worst_sizes < second_worst_sizes)) { |
| 198 | break; |
| 199 | } |
| 200 | worst_sizes -= len; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 201 | } |
| 202 | #ifdef USERDEBUG_BUILD |
| 203 | else if (mPrune.naughty(e)) { // BlackListed |
| 204 | it = mLogElements.erase(it); |
| 205 | stats.subtract(e->getMsgLen(), id, uid, e->getPid()); |
| 206 | delete e; |
| 207 | pruneRows--; |
| 208 | if (pruneRows == 0) { |
| 209 | break; |
| 210 | } |
| 211 | } |
| 212 | #endif |
| 213 | else { |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 214 | ++it; |
| 215 | } |
| 216 | } |
| 217 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 218 | if (!kick |
| 219 | #ifdef USERDEBUG_BUILD |
| 220 | || !mPrune.worstUidEnabled() |
| 221 | #endif |
| 222 | ) { |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 223 | break; // the following loop will ask bad clients to skip/drop |
| 224 | } |
| 225 | } |
| 226 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 227 | #ifdef USERDEBUG_BUILD |
| 228 | bool whitelist = false; |
| 229 | #endif |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 230 | it = mLogElements.begin(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 231 | while((pruneRows > 0) && (it != mLogElements.end())) { |
| 232 | LogBufferElement *e = *it; |
| 233 | if (e->getLogId() == id) { |
| 234 | if (oldest && (oldest->mStart <= e->getMonotonicTime())) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 235 | #ifdef USERDEBUG_BUILD |
| 236 | if (!whitelist) |
| 237 | #endif |
| 238 | { |
| 239 | if (stats.sizes(id) > (2 * log_buffer_size(id))) { |
| 240 | // kick a misbehaving log reader client off the island |
| 241 | oldest->release_Locked(); |
| 242 | } else { |
| 243 | oldest->triggerSkip_Locked(pruneRows); |
| 244 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 245 | } |
| 246 | break; |
| 247 | } |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 248 | #ifdef USERDEBUG_BUILD |
| 249 | if (mPrune.nice(e)) { // WhiteListed |
| 250 | whitelist = true; |
| 251 | it++; |
| 252 | continue; |
| 253 | } |
| 254 | #endif |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 255 | it = mLogElements.erase(it); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 256 | stats.subtract(e->getMsgLen(), id, e->getUid(), e->getPid()); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 257 | delete e; |
| 258 | pruneRows--; |
| 259 | } else { |
| 260 | it++; |
| 261 | } |
| 262 | } |
| 263 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 264 | #ifdef USERDEBUG_BUILD |
| 265 | if (whitelist && (pruneRows > 0)) { |
| 266 | it = mLogElements.begin(); |
| 267 | while((it != mLogElements.end()) && (pruneRows > 0)) { |
| 268 | LogBufferElement *e = *it; |
| 269 | if (e->getLogId() == id) { |
| 270 | if (oldest && (oldest->mStart <= e->getMonotonicTime())) { |
| 271 | if (stats.sizes(id) > (2 * log_buffer_size(id))) { |
| 272 | // kick a misbehaving log reader client off the island |
| 273 | oldest->release_Locked(); |
| 274 | } else { |
| 275 | oldest->triggerSkip_Locked(pruneRows); |
| 276 | } |
| 277 | break; |
| 278 | } |
| 279 | it = mLogElements.erase(it); |
| 280 | stats.subtract(e->getMsgLen(), id, e->getUid(), e->getPid()); |
| 281 | delete e; |
| 282 | pruneRows--; |
| 283 | } else { |
| 284 | it++; |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | #endif |
| 289 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 290 | LogTimeEntry::unlock(); |
| 291 | } |
| 292 | |
| 293 | // clear all rows of type "id" from the buffer. |
| 294 | void LogBuffer::clear(log_id_t id) { |
| 295 | pthread_mutex_lock(&mLogElementsLock); |
| 296 | prune(id, ULONG_MAX); |
| 297 | pthread_mutex_unlock(&mLogElementsLock); |
| 298 | } |
| 299 | |
| 300 | // get the used space associated with "id". |
| 301 | unsigned long LogBuffer::getSizeUsed(log_id_t id) { |
| 302 | pthread_mutex_lock(&mLogElementsLock); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 303 | size_t retval = stats.sizes(id); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 304 | pthread_mutex_unlock(&mLogElementsLock); |
| 305 | return retval; |
| 306 | } |
| 307 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 308 | #ifdef USERDEBUG_BUILD |
| 309 | |
| 310 | // set the total space allocated to "id" |
| 311 | int LogBuffer::setSize(log_id_t id, unsigned long size) { |
| 312 | // Reasonable limits ... |
| 313 | if ((size < (64 * 1024)) || ((256 * 1024 * 1024) < size)) { |
| 314 | return -1; |
| 315 | } |
| 316 | pthread_mutex_lock(&mLogElementsLock); |
| 317 | log_buffer_size(id) = size; |
| 318 | pthread_mutex_unlock(&mLogElementsLock); |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | // get the total space allocated to "id" |
| 323 | unsigned long LogBuffer::getSize(log_id_t id) { |
| 324 | pthread_mutex_lock(&mLogElementsLock); |
| 325 | size_t retval = log_buffer_size(id); |
| 326 | pthread_mutex_unlock(&mLogElementsLock); |
| 327 | return retval; |
| 328 | } |
| 329 | |
| 330 | #else // ! USERDEBUG_BUILD |
| 331 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 332 | // get the total space allocated to "id" |
| 333 | unsigned long LogBuffer::getSize(log_id_t /*id*/) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 334 | return log_buffer_size(id); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 337 | #endif |
| 338 | |
Mark Salyzyn | 7e2f83c | 2014-03-05 07:41:49 -0800 | [diff] [blame] | 339 | log_time LogBuffer::flushTo( |
| 340 | SocketClient *reader, const log_time start, bool privileged, |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 341 | bool (*filter)(const LogBufferElement *element, void *arg), void *arg) { |
| 342 | LogBufferElementCollection::iterator it; |
| 343 | log_time max = start; |
| 344 | uid_t uid = reader->getUid(); |
| 345 | |
| 346 | pthread_mutex_lock(&mLogElementsLock); |
| 347 | for (it = mLogElements.begin(); it != mLogElements.end(); ++it) { |
| 348 | LogBufferElement *element = *it; |
| 349 | |
| 350 | if (!privileged && (element->getUid() != uid)) { |
| 351 | continue; |
| 352 | } |
| 353 | |
| 354 | if (element->getMonotonicTime() <= start) { |
| 355 | continue; |
| 356 | } |
| 357 | |
| 358 | // NB: calling out to another object with mLogElementsLock held (safe) |
| 359 | if (filter && !(*filter)(element, arg)) { |
| 360 | continue; |
| 361 | } |
| 362 | |
| 363 | pthread_mutex_unlock(&mLogElementsLock); |
| 364 | |
| 365 | // range locking in LastLogTimes looks after us |
| 366 | max = element->flushTo(reader); |
| 367 | |
| 368 | if (max == element->FLUSH_ERROR) { |
| 369 | return max; |
| 370 | } |
| 371 | |
| 372 | pthread_mutex_lock(&mLogElementsLock); |
| 373 | } |
| 374 | pthread_mutex_unlock(&mLogElementsLock); |
| 375 | |
| 376 | return max; |
| 377 | } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 378 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 379 | void LogBuffer::formatStatistics(char **strp, uid_t uid, unsigned int logMask) { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 380 | log_time oldest(CLOCK_MONOTONIC); |
| 381 | |
| 382 | pthread_mutex_lock(&mLogElementsLock); |
| 383 | |
| 384 | // Find oldest element in the log(s) |
| 385 | LogBufferElementCollection::iterator it; |
| 386 | for (it = mLogElements.begin(); it != mLogElements.end(); ++it) { |
| 387 | LogBufferElement *element = *it; |
| 388 | |
| 389 | if ((logMask & (1 << element->getLogId()))) { |
| 390 | oldest = element->getMonotonicTime(); |
| 391 | break; |
| 392 | } |
| 393 | } |
| 394 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 395 | stats.format(strp, uid, logMask, oldest); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 396 | |
| 397 | pthread_mutex_unlock(&mLogElementsLock); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 398 | } |