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 | 3fe2593 | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 95 | void LogBuffer::init() { |
Mark Salyzyn | 7b85c59 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 96 | static const char global_tuneable[] = "persist.logd.size"; // Settings App |
| 97 | static const char global_default[] = "ro.logd.size"; // BoardConfig.mk |
| 98 | |
| 99 | unsigned long default_size = property_get_size(global_tuneable); |
| 100 | if (!default_size) { |
| 101 | default_size = property_get_size(global_default); |
| 102 | } |
Mark Salyzyn | 4e756fb | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 103 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 104 | log_id_for_each(i) { |
Mark Salyzyn | 4e756fb | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 105 | char key[PROP_NAME_MAX]; |
Mark Salyzyn | 4e756fb | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 106 | |
Mark Salyzyn | 7b85c59 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 107 | snprintf(key, sizeof(key), "%s.%s", |
| 108 | global_tuneable, android_log_id_to_name(i)); |
| 109 | unsigned long property_size = property_get_size(key); |
| 110 | |
| 111 | if (!property_size) { |
| 112 | snprintf(key, sizeof(key), "%s.%s", |
| 113 | global_default, android_log_id_to_name(i)); |
| 114 | property_size = property_get_size(key); |
| 115 | } |
| 116 | |
| 117 | if (!property_size) { |
| 118 | property_size = default_size; |
| 119 | } |
| 120 | |
| 121 | if (!property_size) { |
| 122 | property_size = LOG_BUFFER_SIZE; |
| 123 | } |
| 124 | |
| 125 | if (setSize(i, property_size)) { |
| 126 | setSize(i, LOG_BUFFER_MIN_SIZE); |
| 127 | } |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 128 | } |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Mark Salyzyn | 3fe2593 | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 131 | LogBuffer::LogBuffer(LastLogTimes *times) |
| 132 | : mTimes(*times) { |
| 133 | pthread_mutex_init(&mLogElementsLock, NULL); |
| 134 | |
| 135 | init(); |
| 136 | } |
| 137 | |
Mark Salyzyn | 7139117 | 2014-03-05 07:41:49 -0800 | [diff] [blame] | 138 | void LogBuffer::log(log_id_t log_id, log_time realtime, |
Mark Salyzyn | 3661dd0 | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 139 | uid_t uid, pid_t pid, pid_t tid, |
| 140 | const char *msg, unsigned short len) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 141 | if ((log_id >= LOG_ID_MAX) || (log_id < 0)) { |
| 142 | return; |
| 143 | } |
| 144 | LogBufferElement *elem = new LogBufferElement(log_id, realtime, |
Mark Salyzyn | 3661dd0 | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 145 | uid, pid, tid, msg, len); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 146 | |
| 147 | pthread_mutex_lock(&mLogElementsLock); |
| 148 | |
| 149 | // Insert elements in time sorted order if possible |
| 150 | // NB: if end is region locked, place element at end of list |
| 151 | LogBufferElementCollection::iterator it = mLogElements.end(); |
| 152 | LogBufferElementCollection::iterator last = it; |
Mark Salyzyn | a222a77 | 2014-10-13 16:49:47 -0700 | [diff] [blame] | 153 | while (last != mLogElements.begin()) { |
| 154 | --it; |
Mark Salyzyn | 5b7a8d8 | 2014-02-18 11:23:53 -0800 | [diff] [blame] | 155 | if ((*it)->getRealTime() <= realtime) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 156 | break; |
| 157 | } |
| 158 | last = it; |
| 159 | } |
Mark Salyzyn | 5b7a8d8 | 2014-02-18 11:23:53 -0800 | [diff] [blame] | 160 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 161 | if (last == mLogElements.end()) { |
| 162 | mLogElements.push_back(elem); |
| 163 | } else { |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame^] | 164 | uint64_t end = 1; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 165 | bool end_set = false; |
| 166 | bool end_always = false; |
| 167 | |
| 168 | LogTimeEntry::lock(); |
| 169 | |
| 170 | LastLogTimes::iterator t = mTimes.begin(); |
| 171 | while(t != mTimes.end()) { |
| 172 | LogTimeEntry *entry = (*t); |
| 173 | if (entry->owned_Locked()) { |
| 174 | if (!entry->mNonBlock) { |
| 175 | end_always = true; |
| 176 | break; |
| 177 | } |
| 178 | if (!end_set || (end <= entry->mEnd)) { |
| 179 | end = entry->mEnd; |
| 180 | end_set = true; |
| 181 | } |
| 182 | } |
| 183 | t++; |
| 184 | } |
| 185 | |
| 186 | if (end_always |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame^] | 187 | || (end_set && (end >= (*last)->getSequence()))) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 188 | mLogElements.push_back(elem); |
| 189 | } else { |
| 190 | mLogElements.insert(last,elem); |
| 191 | } |
| 192 | |
| 193 | LogTimeEntry::unlock(); |
| 194 | } |
| 195 | |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 196 | stats.add(len, log_id, uid, pid); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 197 | maybePrune(log_id); |
| 198 | pthread_mutex_unlock(&mLogElementsLock); |
| 199 | } |
| 200 | |
| 201 | // 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] | 202 | // at least 10% of the log entries. |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 203 | // |
| 204 | // mLogElementsLock must be held when this function is called. |
| 205 | void LogBuffer::maybePrune(log_id_t id) { |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 206 | size_t sizes = stats.sizes(id); |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 207 | if (sizes > log_buffer_size(id)) { |
| 208 | size_t sizeOver90Percent = sizes - ((log_buffer_size(id) * 9) / 10); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 209 | size_t elements = stats.elements(id); |
Mark Salyzyn | 9d4e34e | 2014-01-13 16:37:51 -0800 | [diff] [blame] | 210 | unsigned long pruneRows = elements * sizeOver90Percent / sizes; |
| 211 | elements /= 10; |
| 212 | if (pruneRows <= elements) { |
| 213 | pruneRows = elements; |
| 214 | } |
| 215 | prune(id, pruneRows); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
| 219 | // prune "pruneRows" of type "id" from the buffer. |
| 220 | // |
| 221 | // mLogElementsLock must be held when this function is called. |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 222 | 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] | 223 | LogTimeEntry *oldest = NULL; |
| 224 | |
| 225 | LogTimeEntry::lock(); |
| 226 | |
| 227 | // Region locked? |
| 228 | LastLogTimes::iterator t = mTimes.begin(); |
| 229 | while(t != mTimes.end()) { |
| 230 | LogTimeEntry *entry = (*t); |
TraianX Schiau | 6ba427e | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 231 | if (entry->owned_Locked() && entry->isWatching(id) |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 232 | && (!oldest || (oldest->mStart > entry->mStart))) { |
| 233 | oldest = entry; |
| 234 | } |
| 235 | t++; |
| 236 | } |
| 237 | |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 238 | LogBufferElementCollection::iterator it; |
| 239 | |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 240 | if (caller_uid != AID_ROOT) { |
| 241 | for(it = mLogElements.begin(); it != mLogElements.end();) { |
| 242 | LogBufferElement *e = *it; |
| 243 | |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame^] | 244 | if (oldest && (oldest->mStart <= e->getSequence())) { |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 245 | break; |
| 246 | } |
| 247 | |
| 248 | if (e->getLogId() != id) { |
| 249 | ++it; |
| 250 | continue; |
| 251 | } |
| 252 | |
| 253 | uid_t uid = e->getUid(); |
| 254 | |
| 255 | if (uid == caller_uid) { |
| 256 | it = mLogElements.erase(it); |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 257 | stats.subtract(e->getMsgLen(), id, uid, e->getPid()); |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 258 | delete e; |
| 259 | pruneRows--; |
| 260 | if (pruneRows == 0) { |
| 261 | break; |
| 262 | } |
| 263 | } else { |
| 264 | ++it; |
| 265 | } |
| 266 | } |
| 267 | LogTimeEntry::unlock(); |
| 268 | return; |
| 269 | } |
| 270 | |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 271 | // prune by worst offender by uid |
| 272 | while (pruneRows > 0) { |
| 273 | // recalculate the worst offender on every batched pass |
| 274 | uid_t worst = (uid_t) -1; |
| 275 | size_t worst_sizes = 0; |
| 276 | size_t second_worst_sizes = 0; |
| 277 | |
Mark Salyzyn | 855c730 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 278 | if ((id != LOG_ID_CRASH) && mPrune.worstUidEnabled()) { |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 279 | LidStatistics &l = stats.id(id); |
Mark Salyzyn | 1867ca4 | 2014-04-04 16:35:59 -0700 | [diff] [blame] | 280 | l.sort(); |
| 281 | UidStatisticsCollection::iterator iu = l.begin(); |
| 282 | if (iu != l.end()) { |
| 283 | UidStatistics *u = *iu; |
| 284 | worst = u->getUid(); |
| 285 | worst_sizes = u->sizes(); |
| 286 | if (++iu != l.end()) { |
| 287 | second_worst_sizes = (*iu)->sizes(); |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 288 | } |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
| 292 | bool kick = false; |
| 293 | for(it = mLogElements.begin(); it != mLogElements.end();) { |
| 294 | LogBufferElement *e = *it; |
| 295 | |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame^] | 296 | if (oldest && (oldest->mStart <= e->getSequence())) { |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 297 | break; |
| 298 | } |
| 299 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 300 | if (e->getLogId() != id) { |
| 301 | ++it; |
| 302 | continue; |
| 303 | } |
| 304 | |
| 305 | uid_t uid = e->getUid(); |
| 306 | |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 307 | if ((uid == worst) || mPrune.naughty(e)) { // Worst or BlackListed |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 308 | it = mLogElements.erase(it); |
| 309 | unsigned short len = e->getMsgLen(); |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 310 | stats.subtract(len, id, uid, e->getPid()); |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 311 | delete e; |
| 312 | pruneRows--; |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 313 | if (uid == worst) { |
| 314 | kick = true; |
| 315 | if ((pruneRows == 0) || (worst_sizes < second_worst_sizes)) { |
| 316 | break; |
| 317 | } |
| 318 | worst_sizes -= len; |
| 319 | } else if (pruneRows == 0) { |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 320 | break; |
| 321 | } |
Mark Salyzyn | c402bc7 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 322 | } else { |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 323 | ++it; |
| 324 | } |
| 325 | } |
| 326 | |
Mark Salyzyn | c402bc7 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 327 | if (!kick || !mPrune.worstUidEnabled()) { |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 328 | break; // the following loop will ask bad clients to skip/drop |
| 329 | } |
| 330 | } |
| 331 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 332 | bool whitelist = false; |
Mark Salyzyn | 5c06ca4 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 333 | it = mLogElements.begin(); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 334 | while((pruneRows > 0) && (it != mLogElements.end())) { |
| 335 | LogBufferElement *e = *it; |
| 336 | if (e->getLogId() == id) { |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame^] | 337 | if (oldest && (oldest->mStart <= e->getSequence())) { |
Mark Salyzyn | c402bc7 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 338 | if (!whitelist) { |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 339 | if (stats.sizes(id) > (2 * log_buffer_size(id))) { |
| 340 | // kick a misbehaving log reader client off the island |
| 341 | oldest->release_Locked(); |
| 342 | } else { |
TraianX Schiau | 6ba427e | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 343 | oldest->triggerSkip_Locked(id, pruneRows); |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 344 | } |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 345 | } |
| 346 | break; |
| 347 | } |
Mark Salyzyn | c402bc7 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 348 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 349 | if (mPrune.nice(e)) { // WhiteListed |
| 350 | whitelist = true; |
| 351 | it++; |
| 352 | continue; |
| 353 | } |
Mark Salyzyn | c402bc7 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 354 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 355 | it = mLogElements.erase(it); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 356 | stats.subtract(e->getMsgLen(), id, e->getUid(), e->getPid()); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 357 | delete e; |
| 358 | pruneRows--; |
| 359 | } else { |
| 360 | it++; |
| 361 | } |
| 362 | } |
| 363 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 364 | if (whitelist && (pruneRows > 0)) { |
| 365 | it = mLogElements.begin(); |
| 366 | while((it != mLogElements.end()) && (pruneRows > 0)) { |
| 367 | LogBufferElement *e = *it; |
| 368 | if (e->getLogId() == id) { |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame^] | 369 | if (oldest && (oldest->mStart <= e->getSequence())) { |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 370 | if (stats.sizes(id) > (2 * log_buffer_size(id))) { |
| 371 | // kick a misbehaving log reader client off the island |
| 372 | oldest->release_Locked(); |
| 373 | } else { |
TraianX Schiau | 6ba427e | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 374 | oldest->triggerSkip_Locked(id, pruneRows); |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 375 | } |
| 376 | break; |
| 377 | } |
| 378 | it = mLogElements.erase(it); |
| 379 | stats.subtract(e->getMsgLen(), id, e->getUid(), e->getPid()); |
| 380 | delete e; |
| 381 | pruneRows--; |
| 382 | } else { |
| 383 | it++; |
| 384 | } |
| 385 | } |
| 386 | } |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 387 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 388 | LogTimeEntry::unlock(); |
| 389 | } |
| 390 | |
| 391 | // clear all rows of type "id" from the buffer. |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 392 | void LogBuffer::clear(log_id_t id, uid_t uid) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 393 | pthread_mutex_lock(&mLogElementsLock); |
Mark Salyzyn | 276d535 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 394 | prune(id, ULONG_MAX, uid); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 395 | pthread_mutex_unlock(&mLogElementsLock); |
| 396 | } |
| 397 | |
| 398 | // get the used space associated with "id". |
| 399 | unsigned long LogBuffer::getSizeUsed(log_id_t id) { |
| 400 | pthread_mutex_lock(&mLogElementsLock); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 401 | size_t retval = stats.sizes(id); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 402 | pthread_mutex_unlock(&mLogElementsLock); |
| 403 | return retval; |
| 404 | } |
| 405 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 406 | // set the total space allocated to "id" |
| 407 | int LogBuffer::setSize(log_id_t id, unsigned long size) { |
| 408 | // Reasonable limits ... |
Mark Salyzyn | 7b85c59 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 409 | if (!valid_size(size)) { |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 410 | return -1; |
| 411 | } |
| 412 | pthread_mutex_lock(&mLogElementsLock); |
| 413 | log_buffer_size(id) = size; |
| 414 | pthread_mutex_unlock(&mLogElementsLock); |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | // get the total space allocated to "id" |
| 419 | unsigned long LogBuffer::getSize(log_id_t id) { |
| 420 | pthread_mutex_lock(&mLogElementsLock); |
| 421 | size_t retval = log_buffer_size(id); |
| 422 | pthread_mutex_unlock(&mLogElementsLock); |
| 423 | return retval; |
| 424 | } |
| 425 | |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame^] | 426 | uint64_t LogBuffer::flushTo( |
| 427 | SocketClient *reader, const uint64_t start, bool privileged, |
| 428 | int (*filter)(const LogBufferElement *element, void *arg), void *arg) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 429 | LogBufferElementCollection::iterator it; |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame^] | 430 | uint64_t max = start; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 431 | uid_t uid = reader->getUid(); |
| 432 | |
| 433 | pthread_mutex_lock(&mLogElementsLock); |
Dragoslav Mitrinovic | 81624f2 | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 434 | |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame^] | 435 | if (start <= 1) { |
Dragoslav Mitrinovic | 81624f2 | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 436 | // client wants to start from the beginning |
| 437 | it = mLogElements.begin(); |
| 438 | } else { |
| 439 | // Client wants to start from some specified time. Chances are |
| 440 | // we are better off starting from the end of the time sorted list. |
| 441 | for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) { |
| 442 | --it; |
| 443 | LogBufferElement *element = *it; |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame^] | 444 | if (element->getSequence() <= start) { |
Dragoslav Mitrinovic | 81624f2 | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 445 | it++; |
| 446 | break; |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | for (; it != mLogElements.end(); ++it) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 452 | LogBufferElement *element = *it; |
| 453 | |
| 454 | if (!privileged && (element->getUid() != uid)) { |
| 455 | continue; |
| 456 | } |
| 457 | |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame^] | 458 | if (element->getSequence() <= start) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 459 | continue; |
| 460 | } |
| 461 | |
| 462 | // NB: calling out to another object with mLogElementsLock held (safe) |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame^] | 463 | if (filter) { |
| 464 | int ret = (*filter)(element, arg); |
| 465 | if (ret == false) { |
| 466 | continue; |
| 467 | } |
| 468 | if (ret != true) { |
| 469 | break; |
| 470 | } |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | pthread_mutex_unlock(&mLogElementsLock); |
| 474 | |
| 475 | // range locking in LastLogTimes looks after us |
| 476 | max = element->flushTo(reader); |
| 477 | |
| 478 | if (max == element->FLUSH_ERROR) { |
| 479 | return max; |
| 480 | } |
| 481 | |
| 482 | pthread_mutex_lock(&mLogElementsLock); |
| 483 | } |
| 484 | pthread_mutex_unlock(&mLogElementsLock); |
| 485 | |
| 486 | return max; |
| 487 | } |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 488 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 489 | void LogBuffer::formatStatistics(char **strp, uid_t uid, unsigned int logMask) { |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame^] | 490 | uint64_t oldest = UINT64_MAX; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 491 | |
| 492 | pthread_mutex_lock(&mLogElementsLock); |
| 493 | |
| 494 | // Find oldest element in the log(s) |
| 495 | LogBufferElementCollection::iterator it; |
| 496 | for (it = mLogElements.begin(); it != mLogElements.end(); ++it) { |
| 497 | LogBufferElement *element = *it; |
| 498 | |
| 499 | if ((logMask & (1 << element->getLogId()))) { |
Mark Salyzyn | 0aaf6cd | 2015-03-03 13:39:37 -0800 | [diff] [blame^] | 500 | oldest = element->getSequence(); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 501 | break; |
| 502 | } |
| 503 | } |
| 504 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 505 | stats.format(strp, uid, logMask, oldest); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 506 | |
| 507 | pthread_mutex_unlock(&mLogElementsLock); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 508 | } |