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