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 | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 18 | #include <errno.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 19 | #include <stdio.h> |
| 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 | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame^] | 25 | #include <unordered_map> |
| 26 | |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 27 | #include <cutils/properties.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 28 | #include <log/logger.h> |
| 29 | |
| 30 | #include "LogBuffer.h" |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 31 | #include "LogReader.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 | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 95 | void LogBuffer::init() { |
Mark Salyzyn | 57a0af9 | 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 | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 103 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 104 | log_id_for_each(i) { |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 105 | char key[PROP_NAME_MAX]; |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 106 | |
Mark Salyzyn | 57a0af9 | 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 | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 128 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Mark Salyzyn | 7718778 | 2015-05-12 15:21:31 -0700 | [diff] [blame] | 131 | LogBuffer::LogBuffer(LastLogTimes *times) : mTimes(*times) { |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 132 | pthread_mutex_init(&mLogElementsLock, NULL); |
| 133 | |
| 134 | init(); |
| 135 | } |
| 136 | |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 137 | int LogBuffer::log(log_id_t log_id, log_time realtime, |
| 138 | uid_t uid, pid_t pid, pid_t tid, |
| 139 | const char *msg, unsigned short len) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 140 | if ((log_id >= LOG_ID_MAX) || (log_id < 0)) { |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 141 | return -EINVAL; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 142 | } |
| 143 | LogBufferElement *elem = new LogBufferElement(log_id, realtime, |
Mark Salyzyn | b992d0d | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 144 | uid, pid, tid, msg, len); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 145 | |
| 146 | pthread_mutex_lock(&mLogElementsLock); |
| 147 | |
| 148 | // Insert elements in time sorted order if possible |
| 149 | // NB: if end is region locked, place element at end of list |
| 150 | LogBufferElementCollection::iterator it = mLogElements.end(); |
| 151 | LogBufferElementCollection::iterator last = it; |
Mark Salyzyn | eae155e | 2014-10-13 16:49:47 -0700 | [diff] [blame] | 152 | while (last != mLogElements.begin()) { |
| 153 | --it; |
Mark Salyzyn | c03e72c | 2014-02-18 11:23:53 -0800 | [diff] [blame] | 154 | if ((*it)->getRealTime() <= realtime) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 155 | break; |
| 156 | } |
| 157 | last = it; |
| 158 | } |
Mark Salyzyn | c03e72c | 2014-02-18 11:23:53 -0800 | [diff] [blame] | 159 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 160 | if (last == mLogElements.end()) { |
| 161 | mLogElements.push_back(elem); |
| 162 | } else { |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 163 | uint64_t end = 1; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 164 | bool end_set = false; |
| 165 | bool end_always = false; |
| 166 | |
| 167 | LogTimeEntry::lock(); |
| 168 | |
| 169 | LastLogTimes::iterator t = mTimes.begin(); |
| 170 | while(t != mTimes.end()) { |
| 171 | LogTimeEntry *entry = (*t); |
| 172 | if (entry->owned_Locked()) { |
| 173 | if (!entry->mNonBlock) { |
| 174 | end_always = true; |
| 175 | break; |
| 176 | } |
| 177 | if (!end_set || (end <= entry->mEnd)) { |
| 178 | end = entry->mEnd; |
| 179 | end_set = true; |
| 180 | } |
| 181 | } |
| 182 | t++; |
| 183 | } |
| 184 | |
| 185 | if (end_always |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 186 | || (end_set && (end >= (*last)->getSequence()))) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 187 | mLogElements.push_back(elem); |
| 188 | } else { |
| 189 | mLogElements.insert(last,elem); |
| 190 | } |
| 191 | |
| 192 | LogTimeEntry::unlock(); |
| 193 | } |
| 194 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 195 | stats.add(elem); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 196 | maybePrune(log_id); |
| 197 | pthread_mutex_unlock(&mLogElementsLock); |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 198 | |
| 199 | return len; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | // 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] | 203 | // at least 10% of the log entries. |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 204 | // |
| 205 | // mLogElementsLock must be held when this function is called. |
| 206 | void LogBuffer::maybePrune(log_id_t id) { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 207 | size_t sizes = stats.sizes(id); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 208 | if (sizes > log_buffer_size(id)) { |
| 209 | size_t sizeOver90Percent = sizes - ((log_buffer_size(id) * 9) / 10); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 210 | size_t elements = stats.elements(id); |
Mark Salyzyn | 740f9b4 | 2014-01-13 16:37:51 -0800 | [diff] [blame] | 211 | unsigned long pruneRows = elements * sizeOver90Percent / sizes; |
| 212 | elements /= 10; |
| 213 | if (pruneRows <= elements) { |
| 214 | pruneRows = elements; |
| 215 | } |
| 216 | prune(id, pruneRows); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 220 | LogBufferElementCollection::iterator LogBuffer::erase(LogBufferElementCollection::iterator it) { |
| 221 | LogBufferElement *e = *it; |
| 222 | |
| 223 | it = mLogElements.erase(it); |
| 224 | stats.subtract(e); |
| 225 | delete e; |
| 226 | |
| 227 | return it; |
| 228 | } |
| 229 | |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 230 | // Define a temporary mechanism to report the last LogBufferElement pointer |
| 231 | // for the specified uid, pid and tid. Used below to help merge-sort when |
| 232 | // pruning for worst UID. |
| 233 | class LogBufferElementKey { |
| 234 | const union { |
| 235 | struct { |
| 236 | uint16_t uid; |
| 237 | uint16_t pid; |
| 238 | uint16_t tid; |
| 239 | uint16_t padding; |
| 240 | } __packed; |
| 241 | uint64_t value; |
| 242 | } __packed; |
| 243 | |
| 244 | public: |
| 245 | LogBufferElementKey(uid_t u, pid_t p, pid_t t):uid(u),pid(p),tid(t),padding(0) { } |
| 246 | LogBufferElementKey(uint64_t k):value(k) { } |
| 247 | |
| 248 | uint64_t getKey() { return value; } |
| 249 | }; |
| 250 | |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame^] | 251 | class LogBufferElementLast { |
| 252 | |
| 253 | typedef std::unordered_map<uint64_t, LogBufferElement *> LogBufferElementMap; |
| 254 | LogBufferElementMap map; |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 255 | |
| 256 | public: |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 257 | |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 258 | bool merge(LogBufferElement *e, unsigned short dropped) { |
| 259 | LogBufferElementKey key(e->getUid(), e->getPid(), e->getTid()); |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame^] | 260 | LogBufferElementMap::iterator it = map.find(key.getKey()); |
| 261 | if (it != map.end()) { |
| 262 | LogBufferElement *l = it->second; |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 263 | unsigned short d = l->getDropped(); |
| 264 | if ((dropped + d) > USHRT_MAX) { |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame^] | 265 | map.erase(it); |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 266 | } else { |
| 267 | l->setDropped(dropped + d); |
| 268 | return true; |
| 269 | } |
| 270 | } |
| 271 | return false; |
| 272 | } |
| 273 | |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame^] | 274 | void add(LogBufferElement *e) { |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 275 | LogBufferElementKey key(e->getUid(), e->getPid(), e->getTid()); |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame^] | 276 | map[key.getKey()] = e; |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Mark Salyzyn | e06a6e0 | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 279 | inline void clear() { |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame^] | 280 | map.clear(); |
Mark Salyzyn | e06a6e0 | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | void clear(LogBufferElement *e) { |
| 284 | uint64_t current = e->getRealTime().nsec() - NS_PER_SEC; |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame^] | 285 | for(LogBufferElementMap::iterator it = map.begin(); it != map.end();) { |
| 286 | LogBufferElement *l = it->second; |
Mark Salyzyn | 833a9b1 | 2015-05-15 15:58:17 -0700 | [diff] [blame] | 287 | if ((l->getDropped() >= EXPIRE_THRESHOLD) |
| 288 | && (current > l->getRealTime().nsec())) { |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame^] | 289 | it = map.erase(it); |
| 290 | } else { |
| 291 | ++it; |
Mark Salyzyn | e06a6e0 | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 296 | }; |
| 297 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 298 | // prune "pruneRows" of type "id" from the buffer. |
| 299 | // |
| 300 | // mLogElementsLock must be held when this function is called. |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 301 | 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] | 302 | LogTimeEntry *oldest = NULL; |
| 303 | |
| 304 | LogTimeEntry::lock(); |
| 305 | |
| 306 | // Region locked? |
| 307 | LastLogTimes::iterator t = mTimes.begin(); |
| 308 | while(t != mTimes.end()) { |
| 309 | LogTimeEntry *entry = (*t); |
TraianX Schiau | da6495d | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 310 | if (entry->owned_Locked() && entry->isWatching(id) |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 311 | && (!oldest || (oldest->mStart > entry->mStart))) { |
| 312 | oldest = entry; |
| 313 | } |
| 314 | t++; |
| 315 | } |
| 316 | |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 317 | LogBufferElementCollection::iterator it; |
| 318 | |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 319 | if (caller_uid != AID_ROOT) { |
| 320 | for(it = mLogElements.begin(); it != mLogElements.end();) { |
| 321 | LogBufferElement *e = *it; |
| 322 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 323 | if (oldest && (oldest->mStart <= e->getSequence())) { |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 324 | break; |
| 325 | } |
| 326 | |
| 327 | if (e->getLogId() != id) { |
| 328 | ++it; |
| 329 | continue; |
| 330 | } |
| 331 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 332 | if (e->getUid() == caller_uid) { |
| 333 | it = erase(it); |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 334 | pruneRows--; |
| 335 | if (pruneRows == 0) { |
| 336 | break; |
| 337 | } |
| 338 | } else { |
| 339 | ++it; |
| 340 | } |
| 341 | } |
| 342 | LogTimeEntry::unlock(); |
| 343 | return; |
| 344 | } |
| 345 | |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 346 | // prune by worst offender by uid |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 347 | bool hasBlacklist = mPrune.naughty(); |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 348 | while (pruneRows > 0) { |
| 349 | // recalculate the worst offender on every batched pass |
| 350 | uid_t worst = (uid_t) -1; |
| 351 | size_t worst_sizes = 0; |
| 352 | size_t second_worst_sizes = 0; |
| 353 | |
Mark Salyzyn | ae76923 | 2015-03-17 17:17:25 -0700 | [diff] [blame] | 354 | if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) { |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 355 | std::unique_ptr<const UidEntry *[]> sorted = stats.sort(2, id); |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 356 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 357 | if (sorted.get()) { |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 358 | if (sorted[0] && sorted[1]) { |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 359 | worst_sizes = sorted[0]->getSizes(); |
Mark Salyzyn | d717d80 | 2015-04-20 15:36:12 -0700 | [diff] [blame] | 360 | // Calculate threshold as 12.5% of available storage |
| 361 | size_t threshold = log_buffer_size(id) / 8; |
| 362 | if (worst_sizes > threshold) { |
| 363 | worst = sorted[0]->getKey(); |
| 364 | second_worst_sizes = sorted[1]->getSizes(); |
| 365 | if (second_worst_sizes < threshold) { |
| 366 | second_worst_sizes = threshold; |
| 367 | } |
| 368 | } |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 369 | } |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 373 | // skip if we have neither worst nor naughty filters |
| 374 | if ((worst == (uid_t) -1) && !hasBlacklist) { |
| 375 | break; |
| 376 | } |
| 377 | |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 378 | bool kick = false; |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 379 | bool leading = true; |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 380 | LogBufferElementLast last; |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 381 | for(it = mLogElements.begin(); it != mLogElements.end();) { |
| 382 | LogBufferElement *e = *it; |
| 383 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 384 | if (oldest && (oldest->mStart <= e->getSequence())) { |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 385 | break; |
| 386 | } |
| 387 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 388 | if (e->getLogId() != id) { |
| 389 | ++it; |
| 390 | continue; |
| 391 | } |
| 392 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 393 | unsigned short dropped = e->getDropped(); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 394 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 395 | // remove any leading drops |
| 396 | if (leading && dropped) { |
| 397 | it = erase(it); |
| 398 | continue; |
| 399 | } |
| 400 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 401 | // merge any drops |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 402 | if (dropped && last.merge(e, dropped)) { |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 403 | it = mLogElements.erase(it); |
| 404 | stats.erase(e); |
| 405 | delete e; |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 406 | continue; |
| 407 | } |
| 408 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 409 | if (hasBlacklist && mPrune.naughty(e)) { |
Mark Salyzyn | e06a6e0 | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 410 | last.clear(e); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 411 | it = erase(it); |
| 412 | if (dropped) { |
| 413 | continue; |
| 414 | } |
| 415 | |
| 416 | pruneRows--; |
| 417 | if (pruneRows == 0) { |
| 418 | break; |
| 419 | } |
| 420 | |
| 421 | if (e->getUid() == worst) { |
| 422 | kick = true; |
| 423 | if (worst_sizes < second_worst_sizes) { |
| 424 | break; |
| 425 | } |
| 426 | worst_sizes -= e->getMsgLen(); |
| 427 | } |
| 428 | continue; |
| 429 | } |
| 430 | |
| 431 | if (dropped) { |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 432 | last.add(e); |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 433 | ++it; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 434 | continue; |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 435 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 436 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 437 | if (e->getUid() != worst) { |
Mark Salyzyn | 5921276 | 2015-06-01 09:41:19 -0700 | [diff] [blame] | 438 | if (leading) { |
Mark Salyzyn | 833a9b1 | 2015-05-15 15:58:17 -0700 | [diff] [blame] | 439 | static const timespec too_old = { |
| 440 | EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 |
| 441 | }; |
Mark Salyzyn | 5921276 | 2015-06-01 09:41:19 -0700 | [diff] [blame] | 442 | LogBufferElementCollection::iterator last; |
| 443 | last = mLogElements.end(); |
| 444 | --last; |
| 445 | if ((e->getRealTime() < ((*last)->getRealTime() - too_old)) |
| 446 | || (e->getRealTime() > (*last)->getRealTime())) { |
| 447 | break; |
| 448 | } |
Mark Salyzyn | 833a9b1 | 2015-05-15 15:58:17 -0700 | [diff] [blame] | 449 | } |
Mark Salyzyn | 5921276 | 2015-06-01 09:41:19 -0700 | [diff] [blame] | 450 | leading = false; |
Mark Salyzyn | e06a6e0 | 2015-04-20 14:08:56 -0700 | [diff] [blame] | 451 | last.clear(e); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 452 | ++it; |
| 453 | continue; |
| 454 | } |
| 455 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 456 | pruneRows--; |
| 457 | if (pruneRows == 0) { |
| 458 | break; |
| 459 | } |
| 460 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 461 | kick = true; |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 462 | |
| 463 | unsigned short len = e->getMsgLen(); |
Mark Salyzyn | 5392aac | 2015-05-22 10:03:31 -0700 | [diff] [blame] | 464 | |
| 465 | // do not create any leading drops |
| 466 | if (leading) { |
| 467 | it = erase(it); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 468 | } else { |
Mark Salyzyn | 5392aac | 2015-05-22 10:03:31 -0700 | [diff] [blame] | 469 | stats.drop(e); |
| 470 | e->setDropped(1); |
| 471 | if (last.merge(e, 1)) { |
| 472 | it = mLogElements.erase(it); |
| 473 | stats.erase(e); |
| 474 | delete e; |
| 475 | } else { |
| 476 | last.add(e); |
| 477 | ++it; |
| 478 | } |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 479 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 480 | if (worst_sizes < second_worst_sizes) { |
| 481 | break; |
| 482 | } |
| 483 | worst_sizes -= len; |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 484 | } |
Mark Salyzyn | 2c9d909 | 2015-04-17 15:38:04 -0700 | [diff] [blame] | 485 | last.clear(); |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 486 | |
Mark Salyzyn | 1c95047 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 487 | if (!kick || !mPrune.worstUidEnabled()) { |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 488 | break; // the following loop will ask bad clients to skip/drop |
| 489 | } |
| 490 | } |
| 491 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 492 | bool whitelist = false; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 493 | bool hasWhitelist = mPrune.nice(); |
Mark Salyzyn | 64d6fe9 | 2014-02-06 18:11:13 -0800 | [diff] [blame] | 494 | it = mLogElements.begin(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 495 | while((pruneRows > 0) && (it != mLogElements.end())) { |
| 496 | LogBufferElement *e = *it; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 497 | |
| 498 | if (e->getLogId() != id) { |
| 499 | it++; |
| 500 | continue; |
| 501 | } |
| 502 | |
| 503 | if (oldest && (oldest->mStart <= e->getSequence())) { |
| 504 | if (whitelist) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 505 | break; |
| 506 | } |
Mark Salyzyn | 1c95047 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 507 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 508 | if (stats.sizes(id) > (2 * log_buffer_size(id))) { |
| 509 | // kick a misbehaving log reader client off the island |
| 510 | oldest->release_Locked(); |
| 511 | } else { |
| 512 | oldest->triggerSkip_Locked(id, pruneRows); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 513 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 514 | break; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 515 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 516 | |
Mark Salyzyn | c5bf3b8 | 2015-05-21 12:50:31 -0700 | [diff] [blame] | 517 | if (hasWhitelist && !e->getDropped() && mPrune.nice(e)) { // WhiteListed |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 518 | whitelist = true; |
| 519 | it++; |
| 520 | continue; |
| 521 | } |
| 522 | |
| 523 | it = erase(it); |
| 524 | pruneRows--; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 525 | } |
| 526 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 527 | // Do not save the whitelist if we are reader range limited |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 528 | if (whitelist && (pruneRows > 0)) { |
| 529 | it = mLogElements.begin(); |
| 530 | while((it != mLogElements.end()) && (pruneRows > 0)) { |
| 531 | LogBufferElement *e = *it; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 532 | |
| 533 | if (e->getLogId() != id) { |
| 534 | ++it; |
| 535 | continue; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 536 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 537 | |
| 538 | if (oldest && (oldest->mStart <= e->getSequence())) { |
| 539 | if (stats.sizes(id) > (2 * log_buffer_size(id))) { |
| 540 | // kick a misbehaving log reader client off the island |
| 541 | oldest->release_Locked(); |
| 542 | } else { |
| 543 | oldest->triggerSkip_Locked(id, pruneRows); |
| 544 | } |
| 545 | break; |
| 546 | } |
| 547 | |
| 548 | it = erase(it); |
| 549 | pruneRows--; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 550 | } |
| 551 | } |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 552 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 553 | LogTimeEntry::unlock(); |
| 554 | } |
| 555 | |
| 556 | // clear all rows of type "id" from the buffer. |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 557 | void LogBuffer::clear(log_id_t id, uid_t uid) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 558 | pthread_mutex_lock(&mLogElementsLock); |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 559 | prune(id, ULONG_MAX, uid); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 560 | pthread_mutex_unlock(&mLogElementsLock); |
| 561 | } |
| 562 | |
| 563 | // get the used space associated with "id". |
| 564 | unsigned long LogBuffer::getSizeUsed(log_id_t id) { |
| 565 | pthread_mutex_lock(&mLogElementsLock); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 566 | size_t retval = stats.sizes(id); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 567 | pthread_mutex_unlock(&mLogElementsLock); |
| 568 | return retval; |
| 569 | } |
| 570 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 571 | // set the total space allocated to "id" |
| 572 | int LogBuffer::setSize(log_id_t id, unsigned long size) { |
| 573 | // Reasonable limits ... |
Mark Salyzyn | 57a0af9 | 2014-05-09 17:44:18 -0700 | [diff] [blame] | 574 | if (!valid_size(size)) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 575 | return -1; |
| 576 | } |
| 577 | pthread_mutex_lock(&mLogElementsLock); |
| 578 | log_buffer_size(id) = size; |
| 579 | pthread_mutex_unlock(&mLogElementsLock); |
| 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | // get the total space allocated to "id" |
| 584 | unsigned long LogBuffer::getSize(log_id_t id) { |
| 585 | pthread_mutex_lock(&mLogElementsLock); |
| 586 | size_t retval = log_buffer_size(id); |
| 587 | pthread_mutex_unlock(&mLogElementsLock); |
| 588 | return retval; |
| 589 | } |
| 590 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 591 | uint64_t LogBuffer::flushTo( |
| 592 | SocketClient *reader, const uint64_t start, bool privileged, |
| 593 | int (*filter)(const LogBufferElement *element, void *arg), void *arg) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 594 | LogBufferElementCollection::iterator it; |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 595 | uint64_t max = start; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 596 | uid_t uid = reader->getUid(); |
| 597 | |
| 598 | pthread_mutex_lock(&mLogElementsLock); |
Dragoslav Mitrinovic | 8e8e8db | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 599 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 600 | if (start <= 1) { |
Dragoslav Mitrinovic | 8e8e8db | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 601 | // client wants to start from the beginning |
| 602 | it = mLogElements.begin(); |
| 603 | } else { |
| 604 | // Client wants to start from some specified time. Chances are |
| 605 | // we are better off starting from the end of the time sorted list. |
| 606 | for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) { |
| 607 | --it; |
| 608 | LogBufferElement *element = *it; |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 609 | if (element->getSequence() <= start) { |
Dragoslav Mitrinovic | 8e8e8db | 2015-01-15 09:29:43 -0600 | [diff] [blame] | 610 | it++; |
| 611 | break; |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | for (; it != mLogElements.end(); ++it) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 617 | LogBufferElement *element = *it; |
| 618 | |
| 619 | if (!privileged && (element->getUid() != uid)) { |
| 620 | continue; |
| 621 | } |
| 622 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 623 | if (element->getSequence() <= start) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 624 | continue; |
| 625 | } |
| 626 | |
| 627 | // NB: calling out to another object with mLogElementsLock held (safe) |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 628 | if (filter) { |
| 629 | int ret = (*filter)(element, arg); |
| 630 | if (ret == false) { |
| 631 | continue; |
| 632 | } |
| 633 | if (ret != true) { |
| 634 | break; |
| 635 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | pthread_mutex_unlock(&mLogElementsLock); |
| 639 | |
| 640 | // range locking in LastLogTimes looks after us |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 641 | max = element->flushTo(reader, this); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 642 | |
| 643 | if (max == element->FLUSH_ERROR) { |
| 644 | return max; |
| 645 | } |
| 646 | |
| 647 | pthread_mutex_lock(&mLogElementsLock); |
| 648 | } |
| 649 | pthread_mutex_unlock(&mLogElementsLock); |
| 650 | |
| 651 | return max; |
| 652 | } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 653 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 654 | void LogBuffer::formatStatistics(char **strp, uid_t uid, unsigned int logMask) { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 655 | pthread_mutex_lock(&mLogElementsLock); |
| 656 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 657 | stats.format(strp, uid, logMask); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 658 | |
| 659 | pthread_mutex_unlock(&mLogElementsLock); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 660 | } |