Mark Salyzyn | 12bac90 | 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 | 4e756fb | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 17 | #include <ctype.h> |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 18 | #include <stdio.h> |
Mark Salyzyn | 4e756fb | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 19 | #include <stdlib.h> |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 20 | #include <string.h> |
Mark Salyzyn | 7b85c59 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 21 | #include <sys/user.h> |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 22 | #include <time.h> |
| 23 | #include <unistd.h> |
| 24 | |
Mark Salyzyn | 4e756fb | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 25 | #include <cutils/properties.h> |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 26 | #include <log/logger.h> |
| 27 | |
| 28 | #include "LogBuffer.h" |
Mark Salyzyn | 4e756fb | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 29 | #include "LogReader.h" |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 30 | #include "LogStatistics.h" |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 31 | #include "LogWhiteBlackList.h" |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 32 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 33 | // Default |
Mark Salyzyn | 12bac90 | 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 | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 35 | #define log_buffer_size(id) mMaxSize[id] |
Mark Salyzyn | 7b85c59 | 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 | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 64 | |
Mark Salyzyn | 4e756fb | 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 | 7b85c59 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 88 | if (!valid_size(value)) { |
| 89 | value = 0; |
| 90 | } |
| 91 | |
Mark Salyzyn | 4e756fb | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 92 | return value; |
| 93 | } |
| 94 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 95 | LogBuffer::LogBuffer(LastLogTimes *times) |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 96 | : dgramQlenStatistics(false) |
| 97 | , mTimes(*times) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 98 | pthread_mutex_init(&mLogElementsLock, NULL); |
Mark Salyzyn | fe639a0 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 99 | |
Mark Salyzyn | 7b85c59 | 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 | 4e756fb | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 107 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 108 | log_id_for_each(i) { |
Mark Salyzyn | 4e756fb | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 109 | char key[PROP_NAME_MAX]; |
Mark Salyzyn | 4e756fb | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 110 | |
Mark Salyzyn | 7b85c59 | 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 | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 132 | } |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Mark Salyzyn | 7139117 | 2014-03-05 07:41:49 -0800 | [diff] [blame] | 135 | void LogBuffer::log(log_id_t log_id, log_time realtime, |
Mark Salyzyn | 3661dd0 | 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 | 12bac90 | 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 | 3661dd0 | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 142 | uid, pid, tid, msg, len); |
Mark Salyzyn | 12bac90 | 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 | a222a77 | 2014-10-13 16:49:47 -0700 | [diff] [blame] | 150 | while (last != mLogElements.begin()) { |
| 151 | --it; |
Mark Salyzyn | 5b7a8d8 | 2014-02-18 11:23:53 -0800 | [diff] [blame] | 152 | if ((*it)->getRealTime() <= realtime) { |
Mark Salyzyn | fe639a0 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 153 | // halves the peak performance, use with caution |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 154 | if (dgramQlenStatistics) { |
Mark Salyzyn | fe639a0 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 155 | LogBufferElementCollection::iterator ib = it; |
| 156 | unsigned short buckets, num = 1; |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 157 | for (unsigned short i = 0; (buckets = stats.dgramQlen(i)); ++i) { |
Mark Salyzyn | fe639a0 | 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 | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 170 | break; |
| 171 | } |
| 172 | last = it; |
| 173 | } |
Mark Salyzyn | 5b7a8d8 | 2014-02-18 11:23:53 -0800 | [diff] [blame] | 174 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 175 | if (last == mLogElements.end()) { |
| 176 | mLogElements.push_back(elem); |
| 177 | } else { |
Mark Salyzyn | 28ac101 | 2014-05-22 16:07:31 -0700 | [diff] [blame] | 178 | log_time end = log_time::EPOCH; |
Mark Salyzyn | 12bac90 | 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 | 5b7a8d8 | 2014-02-18 11:23:53 -0800 | [diff] [blame] | 201 | || (end_set && (end >= (*last)->getMonotonicTime()))) { |
Mark Salyzyn | 12bac90 | 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 | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 210 | stats.add(len, log_id, uid, pid); |
Mark Salyzyn | 12bac90 | 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 | 9d4e34e | 2014-01-13 16:37:51 -0800 | [diff] [blame] | 216 | // at least 10% of the log entries. |
Mark Salyzyn | 12bac90 | 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 | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 220 | size_t sizes = stats.sizes(id); |
Mark Salyzyn | c89839a | 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 | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 223 | size_t elements = stats.elements(id); |
Mark Salyzyn | 9d4e34e | 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 | 12bac90 | 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 | 276d535 | 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 | 12bac90 | 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 | 6ba427e | 2014-12-17 10:53:41 +0200 | [diff] [blame^] | 245 | if (entry->owned_Locked() && entry->isWatching(id) |
Mark Salyzyn | 12bac90 | 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 | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 252 | LogBufferElementCollection::iterator it; |
| 253 | |
Mark Salyzyn | 276d535 | 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 | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 271 | stats.subtract(e->getMsgLen(), id, uid, e->getPid()); |
Mark Salyzyn | 276d535 | 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 | 5c06ca4 | 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 | 855c730 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 292 | if ((id != LOG_ID_CRASH) && mPrune.worstUidEnabled()) { |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 293 | LidStatistics &l = stats.id(id); |
Mark Salyzyn | 1867ca4 | 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 | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 302 | } |
Mark Salyzyn | 5c06ca4 | 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 | c89839a | 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 | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 321 | if ((uid == worst) || mPrune.naughty(e)) { // Worst or BlackListed |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 322 | it = mLogElements.erase(it); |
| 323 | unsigned short len = e->getMsgLen(); |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 324 | stats.subtract(len, id, uid, e->getPid()); |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 325 | delete e; |
| 326 | pruneRows--; |
Mark Salyzyn | 03a6a1a | 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 | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 334 | break; |
| 335 | } |
Mark Salyzyn | c402bc7 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 336 | } else { |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 337 | ++it; |
| 338 | } |
| 339 | } |
| 340 | |
Mark Salyzyn | c402bc7 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 341 | if (!kick || !mPrune.worstUidEnabled()) { |
Mark Salyzyn | 5c06ca4 | 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 | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 346 | bool whitelist = false; |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 347 | it = mLogElements.begin(); |
Mark Salyzyn | 12bac90 | 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 | c402bc7 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 352 | if (!whitelist) { |
Mark Salyzyn | c89839a | 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 | 6ba427e | 2014-12-17 10:53:41 +0200 | [diff] [blame^] | 357 | oldest->triggerSkip_Locked(id, pruneRows); |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 358 | } |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 359 | } |
| 360 | break; |
| 361 | } |
Mark Salyzyn | c402bc7 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 362 | |
Mark Salyzyn | c89839a | 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 | c402bc7 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 368 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 369 | it = mLogElements.erase(it); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 370 | stats.subtract(e->getMsgLen(), id, e->getUid(), e->getPid()); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 371 | delete e; |
| 372 | pruneRows--; |
| 373 | } else { |
| 374 | it++; |
| 375 | } |
| 376 | } |
| 377 | |
Mark Salyzyn | c89839a | 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 | 6ba427e | 2014-12-17 10:53:41 +0200 | [diff] [blame^] | 388 | oldest->triggerSkip_Locked(id, pruneRows); |
Mark Salyzyn | c89839a | 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 | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 401 | |
Mark Salyzyn | 12bac90 | 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 | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 406 | void LogBuffer::clear(log_id_t id, uid_t uid) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 407 | pthread_mutex_lock(&mLogElementsLock); |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 408 | prune(id, ULONG_MAX, uid); |
Mark Salyzyn | 12bac90 | 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 | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 415 | size_t retval = stats.sizes(id); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 416 | pthread_mutex_unlock(&mLogElementsLock); |
| 417 | return retval; |
| 418 | } |
| 419 | |
Mark Salyzyn | c89839a | 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 | 7b85c59 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 423 | if (!valid_size(size)) { |
Mark Salyzyn | c89839a | 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 | 7139117 | 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 | 12bac90 | 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); |
| 448 | for (it = mLogElements.begin(); it != mLogElements.end(); ++it) { |
| 449 | LogBufferElement *element = *it; |
| 450 | |
| 451 | if (!privileged && (element->getUid() != uid)) { |
| 452 | continue; |
| 453 | } |
| 454 | |
| 455 | if (element->getMonotonicTime() <= start) { |
| 456 | continue; |
| 457 | } |
| 458 | |
| 459 | // NB: calling out to another object with mLogElementsLock held (safe) |
| 460 | if (filter && !(*filter)(element, arg)) { |
| 461 | continue; |
| 462 | } |
| 463 | |
| 464 | pthread_mutex_unlock(&mLogElementsLock); |
| 465 | |
| 466 | // range locking in LastLogTimes looks after us |
| 467 | max = element->flushTo(reader); |
| 468 | |
| 469 | if (max == element->FLUSH_ERROR) { |
| 470 | return max; |
| 471 | } |
| 472 | |
| 473 | pthread_mutex_lock(&mLogElementsLock); |
| 474 | } |
| 475 | pthread_mutex_unlock(&mLogElementsLock); |
| 476 | |
| 477 | return max; |
| 478 | } |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 479 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 480 | void LogBuffer::formatStatistics(char **strp, uid_t uid, unsigned int logMask) { |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 481 | log_time oldest(CLOCK_MONOTONIC); |
| 482 | |
| 483 | pthread_mutex_lock(&mLogElementsLock); |
| 484 | |
| 485 | // Find oldest element in the log(s) |
| 486 | LogBufferElementCollection::iterator it; |
| 487 | for (it = mLogElements.begin(); it != mLogElements.end(); ++it) { |
| 488 | LogBufferElement *element = *it; |
| 489 | |
| 490 | if ((logMask & (1 << element->getLogId()))) { |
| 491 | oldest = element->getMonotonicTime(); |
| 492 | break; |
| 493 | } |
| 494 | } |
| 495 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 496 | stats.format(strp, uid, logMask, oldest); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 497 | |
| 498 | pthread_mutex_unlock(&mLogElementsLock); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 499 | } |