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> |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 19 | #include <stdlib.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 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 | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 30 | #include "LogStatistics.h" |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 31 | #include "LogWhiteBlackList.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 32 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 33 | // Default |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 34 | #define LOG_BUFFER_SIZE (256 * 1024) // Tuned on a per-platform basis here? |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 35 | #define log_buffer_size(id) mMaxSize[id] |
Mark Salyzyn | 57a0af9 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 36 | #define LOG_BUFFER_MIN_SIZE (64 * 1024UL) |
| 37 | #define LOG_BUFFER_MAX_SIZE (256 * 1024 * 1024UL) |
| 38 | |
| 39 | static bool valid_size(unsigned long value) { |
| 40 | if ((value < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < value)) { |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | long pages = sysconf(_SC_PHYS_PAGES); |
| 45 | if (pages < 1) { |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | long pagesize = sysconf(_SC_PAGESIZE); |
| 50 | if (pagesize <= 1) { |
| 51 | pagesize = PAGE_SIZE; |
| 52 | } |
| 53 | |
| 54 | // maximum memory impact a somewhat arbitrary ~3% |
| 55 | pages = (pages + 31) / 32; |
| 56 | unsigned long maximum = pages * pagesize; |
| 57 | |
| 58 | if ((maximum < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < maximum)) { |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | return value <= maximum; |
| 63 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 64 | |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 65 | static unsigned long property_get_size(const char *key) { |
| 66 | char property[PROPERTY_VALUE_MAX]; |
| 67 | property_get(key, property, ""); |
| 68 | |
| 69 | char *cp; |
| 70 | unsigned long value = strtoul(property, &cp, 10); |
| 71 | |
| 72 | switch(*cp) { |
| 73 | case 'm': |
| 74 | case 'M': |
| 75 | value *= 1024; |
| 76 | /* FALLTHRU */ |
| 77 | case 'k': |
| 78 | case 'K': |
| 79 | value *= 1024; |
| 80 | /* FALLTHRU */ |
| 81 | case '\0': |
| 82 | break; |
| 83 | |
| 84 | default: |
| 85 | value = 0; |
| 86 | } |
| 87 | |
Mark Salyzyn | 57a0af9 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 88 | if (!valid_size(value)) { |
| 89 | value = 0; |
| 90 | } |
| 91 | |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 92 | return value; |
| 93 | } |
| 94 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 95 | LogBuffer::LogBuffer(LastLogTimes *times) |
Mark Salyzyn | e72c6e4 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 96 | : dgramQlenStatistics(false) |
| 97 | , mTimes(*times) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 98 | pthread_mutex_init(&mLogElementsLock, NULL); |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 99 | |
Mark Salyzyn | 57a0af9 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 100 | static const char global_tuneable[] = "persist.logd.size"; // Settings App |
| 101 | static const char global_default[] = "ro.logd.size"; // BoardConfig.mk |
| 102 | |
| 103 | unsigned long default_size = property_get_size(global_tuneable); |
| 104 | if (!default_size) { |
| 105 | default_size = property_get_size(global_default); |
| 106 | } |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 107 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 108 | log_id_for_each(i) { |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 109 | char key[PROP_NAME_MAX]; |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 110 | |
Mark Salyzyn | 57a0af9 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 111 | snprintf(key, sizeof(key), "%s.%s", |
| 112 | global_tuneable, android_log_id_to_name(i)); |
| 113 | unsigned long property_size = property_get_size(key); |
| 114 | |
| 115 | if (!property_size) { |
| 116 | snprintf(key, sizeof(key), "%s.%s", |
| 117 | global_default, android_log_id_to_name(i)); |
| 118 | property_size = property_get_size(key); |
| 119 | } |
| 120 | |
| 121 | if (!property_size) { |
| 122 | property_size = default_size; |
| 123 | } |
| 124 | |
| 125 | if (!property_size) { |
| 126 | property_size = LOG_BUFFER_SIZE; |
| 127 | } |
| 128 | |
| 129 | if (setSize(i, property_size)) { |
| 130 | setSize(i, LOG_BUFFER_MIN_SIZE); |
| 131 | } |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 132 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 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 | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 153 | // halves the peak performance, use with caution |
Mark Salyzyn | e72c6e4 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 154 | if (dgramQlenStatistics) { |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 155 | LogBufferElementCollection::iterator ib = it; |
| 156 | unsigned short buckets, num = 1; |
Mark Salyzyn | e72c6e4 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 157 | for (unsigned short i = 0; (buckets = stats.dgramQlen(i)); ++i) { |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 158 | buckets -= num; |
| 159 | num += buckets; |
| 160 | while (buckets && (--ib != mLogElements.begin())) { |
| 161 | --buckets; |
| 162 | } |
| 163 | if (buckets) { |
| 164 | break; |
| 165 | } |
| 166 | stats.recordDiff( |
| 167 | elem->getRealTime() - (*ib)->getRealTime(), i); |
| 168 | } |
| 169 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 170 | break; |
| 171 | } |
| 172 | last = it; |
| 173 | } |
Mark Salyzyn | c03e72c | 2014-02-18 11:23:53 -0800 | [diff] [blame] | 174 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 175 | if (last == mLogElements.end()) { |
| 176 | mLogElements.push_back(elem); |
| 177 | } else { |
Mark Salyzyn | ab4b730 | 2014-05-22 16:07:31 -0700 | [diff] [blame] | 178 | log_time end = log_time::EPOCH; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 179 | bool end_set = false; |
| 180 | bool end_always = false; |
| 181 | |
| 182 | LogTimeEntry::lock(); |
| 183 | |
| 184 | LastLogTimes::iterator t = mTimes.begin(); |
| 185 | while(t != mTimes.end()) { |
| 186 | LogTimeEntry *entry = (*t); |
| 187 | if (entry->owned_Locked()) { |
| 188 | if (!entry->mNonBlock) { |
| 189 | end_always = true; |
| 190 | break; |
| 191 | } |
| 192 | if (!end_set || (end <= entry->mEnd)) { |
| 193 | end = entry->mEnd; |
| 194 | end_set = true; |
| 195 | } |
| 196 | } |
| 197 | t++; |
| 198 | } |
| 199 | |
| 200 | if (end_always |
Mark Salyzyn | c03e72c | 2014-02-18 11:23:53 -0800 | [diff] [blame] | 201 | || (end_set && (end >= (*last)->getMonotonicTime()))) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 202 | mLogElements.push_back(elem); |
| 203 | } else { |
| 204 | mLogElements.insert(last,elem); |
| 205 | } |
| 206 | |
| 207 | LogTimeEntry::unlock(); |
| 208 | } |
| 209 | |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 210 | stats.add(len, log_id, uid, pid); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 211 | maybePrune(log_id); |
| 212 | pthread_mutex_unlock(&mLogElementsLock); |
| 213 | } |
| 214 | |
| 215 | // 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] | 216 | // at least 10% of the log entries. |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 217 | // |
| 218 | // mLogElementsLock must be held when this function is called. |
| 219 | void LogBuffer::maybePrune(log_id_t id) { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 220 | size_t sizes = stats.sizes(id); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 221 | if (sizes > log_buffer_size(id)) { |
| 222 | size_t sizeOver90Percent = sizes - ((log_buffer_size(id) * 9) / 10); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 223 | size_t elements = stats.elements(id); |
Mark Salyzyn | 740f9b4 | 2014-01-13 16:37:51 -0800 | [diff] [blame] | 224 | unsigned long pruneRows = elements * sizeOver90Percent / sizes; |
| 225 | elements /= 10; |
| 226 | if (pruneRows <= elements) { |
| 227 | pruneRows = elements; |
| 228 | } |
| 229 | prune(id, pruneRows); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
| 233 | // prune "pruneRows" of type "id" from the buffer. |
| 234 | // |
| 235 | // mLogElementsLock must be held when this function is called. |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 236 | 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] | 237 | LogTimeEntry *oldest = NULL; |
| 238 | |
| 239 | LogTimeEntry::lock(); |
| 240 | |
| 241 | // Region locked? |
| 242 | LastLogTimes::iterator t = mTimes.begin(); |
| 243 | while(t != mTimes.end()) { |
| 244 | LogTimeEntry *entry = (*t); |
TraianX Schiau | da6495d | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 245 | if (entry->owned_Locked() && entry->isWatching(id) |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 246 | && (!oldest || (oldest->mStart > entry->mStart))) { |
| 247 | oldest = entry; |
| 248 | } |
| 249 | t++; |
| 250 | } |
| 251 | |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 252 | LogBufferElementCollection::iterator it; |
| 253 | |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 254 | if (caller_uid != AID_ROOT) { |
| 255 | for(it = mLogElements.begin(); it != mLogElements.end();) { |
| 256 | LogBufferElement *e = *it; |
| 257 | |
| 258 | if (oldest && (oldest->mStart <= e->getMonotonicTime())) { |
| 259 | break; |
| 260 | } |
| 261 | |
| 262 | if (e->getLogId() != id) { |
| 263 | ++it; |
| 264 | continue; |
| 265 | } |
| 266 | |
| 267 | uid_t uid = e->getUid(); |
| 268 | |
| 269 | if (uid == caller_uid) { |
| 270 | it = mLogElements.erase(it); |
Mark Salyzyn | e72c6e4 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 271 | stats.subtract(e->getMsgLen(), id, uid, e->getPid()); |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 272 | delete e; |
| 273 | pruneRows--; |
| 274 | if (pruneRows == 0) { |
| 275 | break; |
| 276 | } |
| 277 | } else { |
| 278 | ++it; |
| 279 | } |
| 280 | } |
| 281 | LogTimeEntry::unlock(); |
| 282 | return; |
| 283 | } |
| 284 | |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 285 | // prune by worst offender by uid |
| 286 | while (pruneRows > 0) { |
| 287 | // recalculate the worst offender on every batched pass |
| 288 | uid_t worst = (uid_t) -1; |
| 289 | size_t worst_sizes = 0; |
| 290 | size_t second_worst_sizes = 0; |
| 291 | |
Mark Salyzyn | 99f47a9 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 292 | if ((id != LOG_ID_CRASH) && mPrune.worstUidEnabled()) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 293 | LidStatistics &l = stats.id(id); |
Mark Salyzyn | c8a576c | 2014-04-04 16:35:59 -0700 | [diff] [blame] | 294 | l.sort(); |
| 295 | UidStatisticsCollection::iterator iu = l.begin(); |
| 296 | if (iu != l.end()) { |
| 297 | UidStatistics *u = *iu; |
| 298 | worst = u->getUid(); |
| 299 | worst_sizes = u->sizes(); |
| 300 | if (++iu != l.end()) { |
| 301 | second_worst_sizes = (*iu)->sizes(); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 302 | } |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
| 306 | bool kick = false; |
| 307 | for(it = mLogElements.begin(); it != mLogElements.end();) { |
| 308 | LogBufferElement *e = *it; |
| 309 | |
| 310 | if (oldest && (oldest->mStart <= e->getMonotonicTime())) { |
| 311 | break; |
| 312 | } |
| 313 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 314 | if (e->getLogId() != id) { |
| 315 | ++it; |
| 316 | continue; |
| 317 | } |
| 318 | |
| 319 | uid_t uid = e->getUid(); |
| 320 | |
Mark Salyzyn | e72c6e4 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 321 | if ((uid == worst) || mPrune.naughty(e)) { // Worst or BlackListed |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 322 | it = mLogElements.erase(it); |
| 323 | unsigned short len = e->getMsgLen(); |
Mark Salyzyn | e72c6e4 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 324 | stats.subtract(len, id, uid, e->getPid()); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 325 | delete e; |
| 326 | pruneRows--; |
Mark Salyzyn | e72c6e4 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 327 | if (uid == worst) { |
| 328 | kick = true; |
| 329 | if ((pruneRows == 0) || (worst_sizes < second_worst_sizes)) { |
| 330 | break; |
| 331 | } |
| 332 | worst_sizes -= len; |
| 333 | } else if (pruneRows == 0) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 334 | break; |
| 335 | } |
Mark Salyzyn | 1c95047 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 336 | } else { |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 337 | ++it; |
| 338 | } |
| 339 | } |
| 340 | |
Mark Salyzyn | 1c95047 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 341 | if (!kick || !mPrune.worstUidEnabled()) { |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 342 | break; // the following loop will ask bad clients to skip/drop |
| 343 | } |
| 344 | } |
| 345 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 346 | bool whitelist = false; |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 347 | it = mLogElements.begin(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 348 | while((pruneRows > 0) && (it != mLogElements.end())) { |
| 349 | LogBufferElement *e = *it; |
| 350 | if (e->getLogId() == id) { |
| 351 | if (oldest && (oldest->mStart <= e->getMonotonicTime())) { |
Mark Salyzyn | 1c95047 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 352 | if (!whitelist) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 353 | if (stats.sizes(id) > (2 * log_buffer_size(id))) { |
| 354 | // kick a misbehaving log reader client off the island |
| 355 | oldest->release_Locked(); |
| 356 | } else { |
TraianX Schiau | da6495d | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 357 | oldest->triggerSkip_Locked(id, pruneRows); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 358 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 359 | } |
| 360 | break; |
| 361 | } |
Mark Salyzyn | 1c95047 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 362 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 363 | if (mPrune.nice(e)) { // WhiteListed |
| 364 | whitelist = true; |
| 365 | it++; |
| 366 | continue; |
| 367 | } |
Mark Salyzyn | 1c95047 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 368 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 369 | it = mLogElements.erase(it); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 370 | stats.subtract(e->getMsgLen(), id, e->getUid(), e->getPid()); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 371 | delete e; |
| 372 | pruneRows--; |
| 373 | } else { |
| 374 | it++; |
| 375 | } |
| 376 | } |
| 377 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 378 | if (whitelist && (pruneRows > 0)) { |
| 379 | it = mLogElements.begin(); |
| 380 | while((it != mLogElements.end()) && (pruneRows > 0)) { |
| 381 | LogBufferElement *e = *it; |
| 382 | if (e->getLogId() == id) { |
| 383 | if (oldest && (oldest->mStart <= e->getMonotonicTime())) { |
| 384 | if (stats.sizes(id) > (2 * log_buffer_size(id))) { |
| 385 | // kick a misbehaving log reader client off the island |
| 386 | oldest->release_Locked(); |
| 387 | } else { |
TraianX Schiau | da6495d | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 388 | oldest->triggerSkip_Locked(id, pruneRows); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 389 | } |
| 390 | break; |
| 391 | } |
| 392 | it = mLogElements.erase(it); |
| 393 | stats.subtract(e->getMsgLen(), id, e->getUid(), e->getPid()); |
| 394 | delete e; |
| 395 | pruneRows--; |
| 396 | } else { |
| 397 | it++; |
| 398 | } |
| 399 | } |
| 400 | } |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 401 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 402 | LogTimeEntry::unlock(); |
| 403 | } |
| 404 | |
| 405 | // clear all rows of type "id" from the buffer. |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 406 | void LogBuffer::clear(log_id_t id, uid_t uid) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 407 | pthread_mutex_lock(&mLogElementsLock); |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 408 | prune(id, ULONG_MAX, uid); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 409 | pthread_mutex_unlock(&mLogElementsLock); |
| 410 | } |
| 411 | |
| 412 | // get the used space associated with "id". |
| 413 | unsigned long LogBuffer::getSizeUsed(log_id_t id) { |
| 414 | pthread_mutex_lock(&mLogElementsLock); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 415 | size_t retval = stats.sizes(id); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 416 | pthread_mutex_unlock(&mLogElementsLock); |
| 417 | return retval; |
| 418 | } |
| 419 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 420 | // set the total space allocated to "id" |
| 421 | int LogBuffer::setSize(log_id_t id, unsigned long size) { |
| 422 | // Reasonable limits ... |
Mark Salyzyn | 57a0af9 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 423 | if (!valid_size(size)) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 424 | return -1; |
| 425 | } |
| 426 | pthread_mutex_lock(&mLogElementsLock); |
| 427 | log_buffer_size(id) = size; |
| 428 | pthread_mutex_unlock(&mLogElementsLock); |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | // get the total space allocated to "id" |
| 433 | unsigned long LogBuffer::getSize(log_id_t id) { |
| 434 | pthread_mutex_lock(&mLogElementsLock); |
| 435 | size_t retval = log_buffer_size(id); |
| 436 | pthread_mutex_unlock(&mLogElementsLock); |
| 437 | return retval; |
| 438 | } |
| 439 | |
Mark Salyzyn | 7e2f83c | 2014-03-05 07:41:49 -0800 | [diff] [blame] | 440 | log_time LogBuffer::flushTo( |
| 441 | SocketClient *reader, const log_time start, bool privileged, |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 442 | bool (*filter)(const LogBufferElement *element, void *arg), void *arg) { |
| 443 | LogBufferElementCollection::iterator it; |
| 444 | log_time max = start; |
| 445 | uid_t uid = reader->getUid(); |
| 446 | |
| 447 | pthread_mutex_lock(&mLogElementsLock); |
Dragoslav Mitrinovic | 8e8e8db | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 448 | |
| 449 | if (start == LogTimeEntry::EPOCH) { |
| 450 | // client wants to start from the beginning |
| 451 | it = mLogElements.begin(); |
| 452 | } else { |
| 453 | // Client wants to start from some specified time. Chances are |
| 454 | // we are better off starting from the end of the time sorted list. |
| 455 | for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) { |
| 456 | --it; |
| 457 | LogBufferElement *element = *it; |
| 458 | if (element->getMonotonicTime() <= start) { |
| 459 | it++; |
| 460 | break; |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | for (; it != mLogElements.end(); ++it) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 466 | LogBufferElement *element = *it; |
| 467 | |
| 468 | if (!privileged && (element->getUid() != uid)) { |
| 469 | continue; |
| 470 | } |
| 471 | |
| 472 | if (element->getMonotonicTime() <= start) { |
| 473 | continue; |
| 474 | } |
| 475 | |
| 476 | // NB: calling out to another object with mLogElementsLock held (safe) |
| 477 | if (filter && !(*filter)(element, arg)) { |
| 478 | continue; |
| 479 | } |
| 480 | |
| 481 | pthread_mutex_unlock(&mLogElementsLock); |
| 482 | |
| 483 | // range locking in LastLogTimes looks after us |
| 484 | max = element->flushTo(reader); |
| 485 | |
| 486 | if (max == element->FLUSH_ERROR) { |
| 487 | return max; |
| 488 | } |
| 489 | |
| 490 | pthread_mutex_lock(&mLogElementsLock); |
| 491 | } |
| 492 | pthread_mutex_unlock(&mLogElementsLock); |
| 493 | |
| 494 | return max; |
| 495 | } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 496 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 497 | void LogBuffer::formatStatistics(char **strp, uid_t uid, unsigned int logMask) { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 498 | log_time oldest(CLOCK_MONOTONIC); |
| 499 | |
| 500 | pthread_mutex_lock(&mLogElementsLock); |
| 501 | |
| 502 | // Find oldest element in the log(s) |
| 503 | LogBufferElementCollection::iterator it; |
| 504 | for (it = mLogElements.begin(); it != mLogElements.end(); ++it) { |
| 505 | LogBufferElement *element = *it; |
| 506 | |
| 507 | if ((logMask & (1 << element->getLogId()))) { |
| 508 | oldest = element->getMonotonicTime(); |
| 509 | break; |
| 510 | } |
| 511 | } |
| 512 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 513 | stats.format(strp, uid, logMask, oldest); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 514 | |
| 515 | pthread_mutex_unlock(&mLogElementsLock); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 516 | } |