Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 | 5766f80 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 17 | #include <fcntl.h> |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 18 | #include <stdarg.h> |
| 19 | #include <time.h> |
| 20 | |
| 21 | #include <log/logger.h> |
| 22 | #include <private/android_filesystem_config.h> |
| 23 | #include <utils/String8.h> |
| 24 | |
| 25 | #include "LogStatistics.h" |
| 26 | |
Mark Salyzyn | 5766f80 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 27 | PidStatistics::PidStatistics(pid_t pid, char *name) |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 28 | : pid(pid) |
| 29 | , mSizesTotal(0) |
| 30 | , mElementsTotal(0) |
| 31 | , mSizes(0) |
Mark Salyzyn | 5766f80 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 32 | , mElements(0) |
| 33 | , name(name) |
Mark Salyzyn | 8fa720c | 2014-04-21 10:09:42 -0700 | [diff] [blame] | 34 | , mGone(false) |
Mark Salyzyn | 5766f80 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 35 | { } |
| 36 | |
| 37 | #ifdef DO_NOT_ERROR_IF_PIDSTATISTICS_USES_A_COPY_CONSTRUCTOR |
| 38 | PidStatistics::PidStatistics(const PidStatistics ©) |
| 39 | : pid(copy->pid) |
| 40 | , name(copy->name ? strdup(copy->name) : NULL) |
| 41 | , mSizesTotal(copy->mSizesTotal) |
| 42 | , mElementsTotal(copy->mElementsTotal) |
| 43 | , mSizes(copy->mSizes) |
| 44 | , mElements(copy->mElements) |
Mark Salyzyn | 8fa720c | 2014-04-21 10:09:42 -0700 | [diff] [blame] | 45 | , mGone(copy->mGone) |
Mark Salyzyn | 5766f80 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 46 | { } |
| 47 | #endif |
| 48 | |
| 49 | PidStatistics::~PidStatistics() { |
| 50 | free(name); |
| 51 | } |
| 52 | |
Mark Salyzyn | 8fa720c | 2014-04-21 10:09:42 -0700 | [diff] [blame] | 53 | bool PidStatistics::pidGone() { |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 54 | if (mGone || (pid == gone)) { |
Mark Salyzyn | 8fa720c | 2014-04-21 10:09:42 -0700 | [diff] [blame] | 55 | return true; |
| 56 | } |
Mark Salyzyn | 0f827bc | 2014-09-21 14:22:18 -0700 | [diff] [blame^] | 57 | if (pid == 0) { |
| 58 | return false; |
| 59 | } |
Mark Salyzyn | 8fa720c | 2014-04-21 10:09:42 -0700 | [diff] [blame] | 60 | if (kill(pid, 0) && (errno != EPERM)) { |
| 61 | mGone = true; |
| 62 | return true; |
| 63 | } |
| 64 | return false; |
| 65 | } |
| 66 | |
Mark Salyzyn | 5766f80 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 67 | void PidStatistics::setName(char *new_name) { |
| 68 | free(name); |
| 69 | name = new_name; |
| 70 | } |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 71 | |
| 72 | void PidStatistics::add(unsigned short size) { |
| 73 | mSizesTotal += size; |
| 74 | ++mElementsTotal; |
| 75 | mSizes += size; |
| 76 | ++mElements; |
| 77 | } |
| 78 | |
| 79 | bool PidStatistics::subtract(unsigned short size) { |
| 80 | mSizes -= size; |
| 81 | --mElements; |
Mark Salyzyn | 8fa720c | 2014-04-21 10:09:42 -0700 | [diff] [blame] | 82 | return (mElements == 0) && pidGone(); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void PidStatistics::addTotal(size_t size, size_t element) { |
| 86 | if (pid == gone) { |
| 87 | mSizesTotal += size; |
| 88 | mElementsTotal += element; |
| 89 | } |
| 90 | } |
| 91 | |
Mark Salyzyn | 5766f80 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 92 | // must call free to release return value |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 93 | // If only we could sniff our own logs for: |
| 94 | // <time> <pid> <pid> E AndroidRuntime: Process: <name>, PID: <pid> |
| 95 | // which debuggerd prints as a process is crashing. |
Mark Salyzyn | 5766f80 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 96 | char *PidStatistics::pidToName(pid_t pid) { |
| 97 | char *retval = NULL; |
Mark Salyzyn | 0f827bc | 2014-09-21 14:22:18 -0700 | [diff] [blame^] | 98 | if (pid == 0) { // special case from auditd for kernel |
| 99 | retval = strdup("logd.auditd"); |
| 100 | } else if (pid != gone) { |
Mark Salyzyn | 5766f80 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 101 | char buffer[512]; |
| 102 | snprintf(buffer, sizeof(buffer), "/proc/%u/cmdline", pid); |
| 103 | int fd = open(buffer, O_RDONLY); |
| 104 | if (fd >= 0) { |
| 105 | ssize_t ret = read(fd, buffer, sizeof(buffer)); |
| 106 | if (ret > 0) { |
| 107 | buffer[sizeof(buffer)-1] = '\0'; |
| 108 | // frameworks intermediate state |
| 109 | if (strcmp(buffer, "<pre-initialized>")) { |
| 110 | retval = strdup(buffer); |
| 111 | } |
| 112 | } |
| 113 | close(fd); |
| 114 | } |
| 115 | } |
| 116 | return retval; |
| 117 | } |
| 118 | |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 119 | UidStatistics::UidStatistics(uid_t uid) |
Mark Salyzyn | 1867ca4 | 2014-04-04 16:35:59 -0700 | [diff] [blame] | 120 | : uid(uid) |
| 121 | , mSizes(0) |
| 122 | , mElements(0) { |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 123 | Pids.clear(); |
| 124 | } |
| 125 | |
| 126 | UidStatistics::~UidStatistics() { |
| 127 | PidStatisticsCollection::iterator it; |
| 128 | for (it = begin(); it != end();) { |
| 129 | delete (*it); |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 130 | it = erase(it); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | void UidStatistics::add(unsigned short size, pid_t pid) { |
Mark Salyzyn | 1867ca4 | 2014-04-04 16:35:59 -0700 | [diff] [blame] | 135 | mSizes += size; |
| 136 | ++mElements; |
| 137 | |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 138 | PidStatistics *p = NULL; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 139 | PidStatisticsCollection::iterator last; |
| 140 | PidStatisticsCollection::iterator it; |
| 141 | for (last = it = begin(); it != end(); last = it, ++it) { |
| 142 | p = *it; |
| 143 | if (pid == p->getPid()) { |
| 144 | p->add(size); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 145 | return; |
| 146 | } |
| 147 | } |
Mark Salyzyn | 1867ca4 | 2014-04-04 16:35:59 -0700 | [diff] [blame] | 148 | // insert if the gone entry. |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 149 | bool insert_before_last = (last != it) && p && (p->getPid() == p->gone); |
Mark Salyzyn | 5766f80 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 150 | p = new PidStatistics(pid, pidToName(pid)); |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 151 | if (insert_before_last) { |
| 152 | insert(last, p); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 153 | } else { |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 154 | push_back(p); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 155 | } |
| 156 | p->add(size); |
| 157 | } |
| 158 | |
| 159 | void UidStatistics::subtract(unsigned short size, pid_t pid) { |
Mark Salyzyn | 1867ca4 | 2014-04-04 16:35:59 -0700 | [diff] [blame] | 160 | mSizes -= size; |
| 161 | --mElements; |
| 162 | |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 163 | PidStatisticsCollection::iterator it; |
| 164 | for (it = begin(); it != end(); ++it) { |
| 165 | PidStatistics *p = *it; |
| 166 | if (pid == p->getPid()) { |
| 167 | if (p->subtract(size)) { |
| 168 | size_t szsTotal = p->sizesTotal(); |
| 169 | size_t elsTotal = p->elementsTotal(); |
| 170 | delete p; |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 171 | erase(it); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 172 | it = end(); |
| 173 | --it; |
| 174 | if (it == end()) { |
| 175 | p = new PidStatistics(p->gone); |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 176 | push_back(p); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 177 | } else { |
| 178 | p = *it; |
| 179 | if (p->getPid() != p->gone) { |
| 180 | p = new PidStatistics(p->gone); |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 181 | push_back(p); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | p->addTotal(szsTotal, elsTotal); |
| 185 | } |
| 186 | return; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
Mark Salyzyn | 1867ca4 | 2014-04-04 16:35:59 -0700 | [diff] [blame] | 191 | void UidStatistics::sort() { |
| 192 | for (bool pass = true; pass;) { |
| 193 | pass = false; |
| 194 | PidStatisticsCollection::iterator it = begin(); |
| 195 | if (it != end()) { |
| 196 | PidStatisticsCollection::iterator lt = it; |
| 197 | PidStatistics *l = (*lt); |
| 198 | while (++it != end()) { |
| 199 | PidStatistics *n = (*it); |
| 200 | if ((n->getPid() != n->gone) && (n->sizes() > l->sizes())) { |
| 201 | pass = true; |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 202 | erase(it); |
| 203 | insert(lt, n); |
Mark Salyzyn | 1867ca4 | 2014-04-04 16:35:59 -0700 | [diff] [blame] | 204 | it = lt; |
| 205 | n = l; |
| 206 | } |
| 207 | lt = it; |
| 208 | l = n; |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 214 | size_t UidStatistics::sizes(pid_t pid) { |
Mark Salyzyn | 1867ca4 | 2014-04-04 16:35:59 -0700 | [diff] [blame] | 215 | if (pid == pid_all) { |
| 216 | return sizes(); |
| 217 | } |
| 218 | |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 219 | PidStatisticsCollection::iterator it; |
| 220 | for (it = begin(); it != end(); ++it) { |
| 221 | PidStatistics *p = *it; |
Mark Salyzyn | 1867ca4 | 2014-04-04 16:35:59 -0700 | [diff] [blame] | 222 | if (pid == p->getPid()) { |
| 223 | return p->sizes(); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 224 | } |
| 225 | } |
Mark Salyzyn | 1867ca4 | 2014-04-04 16:35:59 -0700 | [diff] [blame] | 226 | return 0; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | size_t UidStatistics::elements(pid_t pid) { |
Mark Salyzyn | 1867ca4 | 2014-04-04 16:35:59 -0700 | [diff] [blame] | 230 | if (pid == pid_all) { |
| 231 | return elements(); |
| 232 | } |
| 233 | |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 234 | PidStatisticsCollection::iterator it; |
| 235 | for (it = begin(); it != end(); ++it) { |
| 236 | PidStatistics *p = *it; |
Mark Salyzyn | 1867ca4 | 2014-04-04 16:35:59 -0700 | [diff] [blame] | 237 | if (pid == p->getPid()) { |
| 238 | return p->elements(); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 239 | } |
| 240 | } |
Mark Salyzyn | 1867ca4 | 2014-04-04 16:35:59 -0700 | [diff] [blame] | 241 | return 0; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | size_t UidStatistics::sizesTotal(pid_t pid) { |
| 245 | size_t sizes = 0; |
| 246 | PidStatisticsCollection::iterator it; |
| 247 | for (it = begin(); it != end(); ++it) { |
| 248 | PidStatistics *p = *it; |
| 249 | if ((pid == pid_all) || (pid == p->getPid())) { |
| 250 | sizes += p->sizesTotal(); |
| 251 | } |
| 252 | } |
| 253 | return sizes; |
| 254 | } |
| 255 | |
| 256 | size_t UidStatistics::elementsTotal(pid_t pid) { |
| 257 | size_t elements = 0; |
| 258 | PidStatisticsCollection::iterator it; |
| 259 | for (it = begin(); it != end(); ++it) { |
| 260 | PidStatistics *p = *it; |
| 261 | if ((pid == pid_all) || (pid == p->getPid())) { |
| 262 | elements += p->elementsTotal(); |
| 263 | } |
| 264 | } |
| 265 | return elements; |
| 266 | } |
| 267 | |
| 268 | LidStatistics::LidStatistics() { |
| 269 | Uids.clear(); |
| 270 | } |
| 271 | |
| 272 | LidStatistics::~LidStatistics() { |
| 273 | UidStatisticsCollection::iterator it; |
| 274 | for (it = begin(); it != end();) { |
| 275 | delete (*it); |
| 276 | it = Uids.erase(it); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | void LidStatistics::add(unsigned short size, uid_t uid, pid_t pid) { |
| 281 | UidStatistics *u; |
| 282 | UidStatisticsCollection::iterator it; |
| 283 | UidStatisticsCollection::iterator last; |
| 284 | |
| 285 | if (uid == (uid_t) -1) { // init |
| 286 | uid = (uid_t) AID_ROOT; |
| 287 | } |
| 288 | |
| 289 | for (last = it = begin(); it != end(); last = it, ++it) { |
| 290 | u = *it; |
| 291 | if (uid == u->getUid()) { |
| 292 | u->add(size, pid); |
| 293 | if ((last != it) && ((*last)->sizesTotal() < u->sizesTotal())) { |
| 294 | Uids.erase(it); |
| 295 | Uids.insert(last, u); |
| 296 | } |
| 297 | return; |
| 298 | } |
| 299 | } |
| 300 | u = new UidStatistics(uid); |
| 301 | if ((last != it) && ((*last)->sizesTotal() < (size_t) size)) { |
| 302 | Uids.insert(last, u); |
| 303 | } else { |
| 304 | Uids.push_back(u); |
| 305 | } |
| 306 | u->add(size, pid); |
| 307 | } |
| 308 | |
| 309 | void LidStatistics::subtract(unsigned short size, uid_t uid, pid_t pid) { |
| 310 | UidStatisticsCollection::iterator it; |
| 311 | for (it = begin(); it != end(); ++it) { |
| 312 | UidStatistics *u = *it; |
| 313 | if (uid == u->getUid()) { |
| 314 | u->subtract(size, pid); |
| 315 | return; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
Mark Salyzyn | 1867ca4 | 2014-04-04 16:35:59 -0700 | [diff] [blame] | 320 | void LidStatistics::sort() { |
| 321 | for (bool pass = true; pass;) { |
| 322 | pass = false; |
| 323 | UidStatisticsCollection::iterator it = begin(); |
| 324 | if (it != end()) { |
| 325 | UidStatisticsCollection::iterator lt = it; |
| 326 | UidStatistics *l = (*lt); |
| 327 | while (++it != end()) { |
| 328 | UidStatistics *n = (*it); |
| 329 | if (n->sizes() > l->sizes()) { |
| 330 | pass = true; |
| 331 | Uids.erase(it); |
| 332 | Uids.insert(lt, n); |
| 333 | it = lt; |
| 334 | n = l; |
| 335 | } |
| 336 | lt = it; |
| 337 | l = n; |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 343 | size_t LidStatistics::sizes(uid_t uid, pid_t pid) { |
| 344 | size_t sizes = 0; |
| 345 | UidStatisticsCollection::iterator it; |
| 346 | for (it = begin(); it != end(); ++it) { |
| 347 | UidStatistics *u = *it; |
| 348 | if ((uid == uid_all) || (uid == u->getUid())) { |
| 349 | sizes += u->sizes(pid); |
| 350 | } |
| 351 | } |
| 352 | return sizes; |
| 353 | } |
| 354 | |
| 355 | size_t LidStatistics::elements(uid_t uid, pid_t pid) { |
| 356 | size_t elements = 0; |
| 357 | UidStatisticsCollection::iterator it; |
| 358 | for (it = begin(); it != end(); ++it) { |
| 359 | UidStatistics *u = *it; |
| 360 | if ((uid == uid_all) || (uid == u->getUid())) { |
| 361 | elements += u->elements(pid); |
| 362 | } |
| 363 | } |
| 364 | return elements; |
| 365 | } |
| 366 | |
| 367 | size_t LidStatistics::sizesTotal(uid_t uid, pid_t pid) { |
| 368 | size_t sizes = 0; |
| 369 | UidStatisticsCollection::iterator it; |
| 370 | for (it = begin(); it != end(); ++it) { |
| 371 | UidStatistics *u = *it; |
| 372 | if ((uid == uid_all) || (uid == u->getUid())) { |
| 373 | sizes += u->sizesTotal(pid); |
| 374 | } |
| 375 | } |
| 376 | return sizes; |
| 377 | } |
| 378 | |
| 379 | size_t LidStatistics::elementsTotal(uid_t uid, pid_t pid) { |
| 380 | size_t elements = 0; |
| 381 | UidStatisticsCollection::iterator it; |
| 382 | for (it = begin(); it != end(); ++it) { |
| 383 | UidStatistics *u = *it; |
| 384 | if ((uid == uid_all) || (uid == u->getUid())) { |
| 385 | elements += u->elementsTotal(pid); |
| 386 | } |
| 387 | } |
| 388 | return elements; |
| 389 | } |
| 390 | |
| 391 | LogStatistics::LogStatistics() |
Mark Salyzyn | 6b4a4d0 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 392 | : mStatistics(false) |
| 393 | , dgramQlenStatistics(false) |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 394 | , start(CLOCK_MONOTONIC) { |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 395 | log_id_for_each(i) { |
| 396 | mSizes[i] = 0; |
| 397 | mElements[i] = 0; |
| 398 | } |
Mark Salyzyn | fe639a0 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 399 | |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 400 | for(unsigned short bucket = 0; dgramQlen(bucket); ++bucket) { |
Mark Salyzyn | 8c6af8d | 2014-04-30 09:21:54 -0700 | [diff] [blame] | 401 | mMinimum[bucket].tv_sec = mMinimum[bucket].tv_sec_max; |
| 402 | mMinimum[bucket].tv_nsec = mMinimum[bucket].tv_nsec_max; |
Mark Salyzyn | fe639a0 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 406 | // Each bucket below represents a dgramQlen of log messages. By |
Mark Salyzyn | fe639a0 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 407 | // finding the minimum period of time from start to finish |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 408 | // of each dgramQlen, we can get a performance expectation for |
Mark Salyzyn | fe639a0 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 409 | // the user space logger. The net result is that the period |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 410 | // of time divided by the dgramQlen will give us the average time |
Mark Salyzyn | fe639a0 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 411 | // between log messages; at the point where the average time |
| 412 | // is greater than the throughput capability of the logger |
| 413 | // we will not longer require the benefits of the FIFO formed |
| 414 | // by max_dgram_qlen. We will also expect to see a very visible |
| 415 | // knee in the average time between log messages at this point, |
| 416 | // so we do not necessarily have to compare the rate against the |
| 417 | // measured performance (BM_log_maximum_retry) of the logger. |
| 418 | // |
| 419 | // for example (reformatted): |
| 420 | // |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 421 | // Minimum time between log events per dgramQlen: |
Mark Salyzyn | fe639a0 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 422 | // 1 2 3 5 10 20 30 50 100 200 300 400 500 600 |
| 423 | // 5u2 12u 13u 15u 16u 27u 30u 36u 407u 3m1 3m3 3m9 3m9 5m5 |
| 424 | // |
| 425 | // demonstrates a clear knee rising at 100, so this means that for this |
| 426 | // case max_dgram_qlen = 100 would be more than sufficient to handle the |
| 427 | // worst that the system could stuff into the logger. The |
| 428 | // BM_log_maximum_retry performance (derated by the log collection) on the |
| 429 | // same system was 33.2us so we would almost be fine with max_dgram_qlen = 50. |
| 430 | // BM_log_maxumum_retry with statistics off is roughly 20us, so |
| 431 | // max_dgram_qlen = 20 would work. We will be more than willing to have |
| 432 | // a large engineering margin so the rule of thumb that lead us to 100 is |
| 433 | // fine. |
| 434 | // |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 435 | // bucket dgramQlen are tuned for /proc/sys/net/unix/max_dgram_qlen = 300 |
Mark Salyzyn | fe639a0 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 436 | const unsigned short LogStatistics::mBuckets[] = { |
| 437 | 1, 2, 3, 5, 10, 20, 30, 50, 100, 200, 300, 400, 500, 600 |
| 438 | }; |
| 439 | |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 440 | unsigned short LogStatistics::dgramQlen(unsigned short bucket) { |
Mark Salyzyn | fe639a0 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 441 | if (bucket >= sizeof(mBuckets) / sizeof(mBuckets[0])) { |
| 442 | return 0; |
| 443 | } |
| 444 | return mBuckets[bucket]; |
| 445 | } |
| 446 | |
| 447 | unsigned long long LogStatistics::minimum(unsigned short bucket) { |
Mark Salyzyn | 8c6af8d | 2014-04-30 09:21:54 -0700 | [diff] [blame] | 448 | if (mMinimum[bucket].tv_sec == mMinimum[bucket].tv_sec_max) { |
Mark Salyzyn | fe639a0 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 449 | return 0; |
| 450 | } |
| 451 | return mMinimum[bucket].nsec(); |
| 452 | } |
| 453 | |
| 454 | void LogStatistics::recordDiff(log_time diff, unsigned short bucket) { |
| 455 | if ((diff.tv_sec || diff.tv_nsec) && (mMinimum[bucket] > diff)) { |
| 456 | mMinimum[bucket] = diff; |
| 457 | } |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | void LogStatistics::add(unsigned short size, |
| 461 | log_id_t log_id, uid_t uid, pid_t pid) { |
| 462 | mSizes[log_id] += size; |
| 463 | ++mElements[log_id]; |
Mark Salyzyn | 6b4a4d0 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 464 | if (!mStatistics) { |
| 465 | return; |
| 466 | } |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 467 | id(log_id).add(size, uid, pid); |
| 468 | } |
| 469 | |
| 470 | void LogStatistics::subtract(unsigned short size, |
| 471 | log_id_t log_id, uid_t uid, pid_t pid) { |
| 472 | mSizes[log_id] -= size; |
| 473 | --mElements[log_id]; |
Mark Salyzyn | 6b4a4d0 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 474 | if (!mStatistics) { |
| 475 | return; |
| 476 | } |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 477 | id(log_id).subtract(size, uid, pid); |
| 478 | } |
| 479 | |
| 480 | size_t LogStatistics::sizes(log_id_t log_id, uid_t uid, pid_t pid) { |
| 481 | if (log_id != log_id_all) { |
| 482 | return id(log_id).sizes(uid, pid); |
| 483 | } |
| 484 | size_t sizes = 0; |
| 485 | log_id_for_each(i) { |
| 486 | sizes += id(i).sizes(uid, pid); |
| 487 | } |
| 488 | return sizes; |
| 489 | } |
| 490 | |
| 491 | size_t LogStatistics::elements(log_id_t log_id, uid_t uid, pid_t pid) { |
| 492 | if (log_id != log_id_all) { |
| 493 | return id(log_id).elements(uid, pid); |
| 494 | } |
| 495 | size_t elements = 0; |
| 496 | log_id_for_each(i) { |
| 497 | elements += id(i).elements(uid, pid); |
| 498 | } |
| 499 | return elements; |
| 500 | } |
| 501 | |
| 502 | size_t LogStatistics::sizesTotal(log_id_t log_id, uid_t uid, pid_t pid) { |
| 503 | if (log_id != log_id_all) { |
| 504 | return id(log_id).sizesTotal(uid, pid); |
| 505 | } |
| 506 | size_t sizes = 0; |
| 507 | log_id_for_each(i) { |
| 508 | sizes += id(i).sizesTotal(uid, pid); |
| 509 | } |
| 510 | return sizes; |
| 511 | } |
| 512 | |
| 513 | size_t LogStatistics::elementsTotal(log_id_t log_id, uid_t uid, pid_t pid) { |
| 514 | if (log_id != log_id_all) { |
| 515 | return id(log_id).elementsTotal(uid, pid); |
| 516 | } |
| 517 | size_t elements = 0; |
| 518 | log_id_for_each(i) { |
| 519 | elements += id(i).elementsTotal(uid, pid); |
| 520 | } |
| 521 | return elements; |
| 522 | } |
| 523 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 524 | void LogStatistics::format(char **buf, |
| 525 | uid_t uid, unsigned int logMask, log_time oldest) { |
Mark Salyzyn | 5766f80 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 526 | static const unsigned short spaces_current = 13; |
| 527 | static const unsigned short spaces_total = 19; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 528 | |
| 529 | if (*buf) { |
Greg Hackmann | 2104c41 | 2014-04-06 21:25:58 -0700 | [diff] [blame] | 530 | free(*buf); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 531 | *buf = NULL; |
| 532 | } |
| 533 | |
| 534 | android::String8 string(" span -> size/num"); |
| 535 | size_t oldLength; |
| 536 | short spaces = 2; |
| 537 | |
| 538 | log_id_for_each(i) { |
Arseniy Antonov | bbcc572 | 2014-09-03 17:29:20 +0400 | [diff] [blame] | 539 | if (!(logMask & (1 << i))) { |
Mark Salyzyn | 1867ca4 | 2014-04-04 16:35:59 -0700 | [diff] [blame] | 540 | continue; |
| 541 | } |
| 542 | oldLength = string.length(); |
| 543 | if (spaces < 0) { |
| 544 | spaces = 0; |
| 545 | } |
| 546 | string.appendFormat("%*s%s", spaces, "", android_log_id_to_name(i)); |
| 547 | spaces += spaces_total + oldLength - string.length(); |
| 548 | |
| 549 | LidStatistics &l = id(i); |
| 550 | l.sort(); |
| 551 | |
| 552 | UidStatisticsCollection::iterator iu; |
| 553 | for (iu = l.begin(); iu != l.end(); ++iu) { |
| 554 | (*iu)->sort(); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | |
| 558 | spaces = 1; |
| 559 | log_time t(CLOCK_MONOTONIC); |
Mark Salyzyn | 6b4a4d0 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 560 | unsigned long long d; |
| 561 | if (mStatistics) { |
| 562 | d = t.nsec() - start.nsec(); |
| 563 | string.appendFormat("\nTotal%4llu:%02llu:%02llu.%09llu", |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 564 | d / NS_PER_SEC / 60 / 60, (d / NS_PER_SEC / 60) % 60, |
| 565 | (d / NS_PER_SEC) % 60, d % NS_PER_SEC); |
| 566 | |
Mark Salyzyn | 6b4a4d0 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 567 | log_id_for_each(i) { |
| 568 | if (!(logMask & (1 << i))) { |
| 569 | continue; |
| 570 | } |
| 571 | oldLength = string.length(); |
| 572 | if (spaces < 0) { |
| 573 | spaces = 0; |
| 574 | } |
| 575 | string.appendFormat("%*s%zu/%zu", spaces, "", |
| 576 | sizesTotal(i), elementsTotal(i)); |
| 577 | spaces += spaces_total + oldLength - string.length(); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 578 | } |
Mark Salyzyn | 6b4a4d0 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 579 | spaces = 1; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 580 | } |
| 581 | |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 582 | d = t.nsec() - oldest.nsec(); |
| 583 | string.appendFormat("\nNow%6llu:%02llu:%02llu.%09llu", |
| 584 | d / NS_PER_SEC / 60 / 60, (d / NS_PER_SEC / 60) % 60, |
| 585 | (d / NS_PER_SEC) % 60, d % NS_PER_SEC); |
| 586 | |
| 587 | log_id_for_each(i) { |
| 588 | if (!(logMask & (1 << i))) { |
| 589 | continue; |
| 590 | } |
| 591 | |
| 592 | size_t els = elements(i); |
| 593 | if (els) { |
| 594 | oldLength = string.length(); |
Mark Salyzyn | fe639a0 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 595 | if (spaces < 0) { |
| 596 | spaces = 0; |
| 597 | } |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 598 | string.appendFormat("%*s%zu/%zu", spaces, "", sizes(i), els); |
| 599 | spaces -= string.length() - oldLength; |
| 600 | } |
| 601 | spaces += spaces_total; |
| 602 | } |
| 603 | |
Mark Salyzyn | 692e3c3 | 2014-03-26 10:46:39 -0700 | [diff] [blame] | 604 | // Construct list of worst spammers by Pid |
| 605 | static const unsigned char num_spammers = 10; |
| 606 | bool header = false; |
| 607 | |
| 608 | log_id_for_each(i) { |
| 609 | if (!(logMask & (1 << i))) { |
| 610 | continue; |
| 611 | } |
| 612 | |
| 613 | PidStatisticsCollection pids; |
| 614 | pids.clear(); |
| 615 | |
| 616 | LidStatistics &l = id(i); |
| 617 | UidStatisticsCollection::iterator iu; |
| 618 | for (iu = l.begin(); iu != l.end(); ++iu) { |
| 619 | UidStatistics &u = *(*iu); |
| 620 | PidStatisticsCollection::iterator ip; |
| 621 | for (ip = u.begin(); ip != u.end(); ++ip) { |
| 622 | PidStatistics *p = (*ip); |
| 623 | if (p->getPid() == p->gone) { |
| 624 | break; |
| 625 | } |
| 626 | |
| 627 | size_t mySizes = p->sizes(); |
| 628 | |
| 629 | PidStatisticsCollection::iterator q; |
| 630 | unsigned char num = 0; |
| 631 | for (q = pids.begin(); q != pids.end(); ++q) { |
| 632 | if (mySizes > (*q)->sizes()) { |
| 633 | pids.insert(q, p); |
| 634 | break; |
| 635 | } |
| 636 | // do we need to traverse deeper in the list? |
| 637 | if (++num > num_spammers) { |
| 638 | break; |
| 639 | } |
| 640 | } |
| 641 | if (q == pids.end()) { |
| 642 | pids.push_back(p); |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | size_t threshold = sizes(i); |
| 648 | if (threshold < 65536) { |
| 649 | threshold = 65536; |
| 650 | } |
| 651 | threshold /= 100; |
| 652 | |
| 653 | PidStatisticsCollection::iterator pt = pids.begin(); |
| 654 | |
| 655 | for(int line = 0; |
| 656 | (pt != pids.end()) && (line < num_spammers); |
| 657 | ++line, pt = pids.erase(pt)) { |
| 658 | PidStatistics *p = *pt; |
| 659 | |
| 660 | size_t sizes = p->sizes(); |
| 661 | if (sizes < threshold) { |
| 662 | break; |
| 663 | } |
| 664 | |
| 665 | char *name = p->getName(); |
| 666 | pid_t pid = p->getPid(); |
| 667 | if (!name || !*name) { |
| 668 | name = pidToName(pid); |
| 669 | if (name) { |
| 670 | if (*name) { |
| 671 | p->setName(name); |
| 672 | } else { |
| 673 | free(name); |
| 674 | name = NULL; |
| 675 | } |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | if (!header) { |
| 680 | string.appendFormat("\n\nChattiest clients:\n" |
| 681 | "log id %-*s PID[?] name", |
| 682 | spaces_total, "size/total"); |
| 683 | header = true; |
| 684 | } |
| 685 | |
| 686 | size_t sizesTotal = p->sizesTotal(); |
| 687 | |
| 688 | android::String8 sz(""); |
Chih-Hung Hsieh | 788dd4f | 2014-09-12 16:02:28 -0700 | [diff] [blame] | 689 | if (sizes == sizesTotal) { |
| 690 | sz.appendFormat("%zu", sizes); |
| 691 | } else { |
| 692 | sz.appendFormat("%zu/%zu", sizes, sizesTotal); |
| 693 | } |
Mark Salyzyn | 692e3c3 | 2014-03-26 10:46:39 -0700 | [diff] [blame] | 694 | |
| 695 | android::String8 pd(""); |
Mark Salyzyn | 8fa720c | 2014-04-21 10:09:42 -0700 | [diff] [blame] | 696 | pd.appendFormat("%u%c", pid, p->pidGone() ? '?' : ' '); |
Mark Salyzyn | 692e3c3 | 2014-03-26 10:46:39 -0700 | [diff] [blame] | 697 | |
| 698 | string.appendFormat("\n%-7s%-*s %-7s%s", |
| 699 | line ? "" : android_log_id_to_name(i), |
| 700 | spaces_total, sz.string(), pd.string(), |
| 701 | name ? name : ""); |
| 702 | } |
| 703 | |
| 704 | pids.clear(); |
| 705 | } |
| 706 | |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 707 | if (dgramQlenStatistics) { |
Mark Salyzyn | fe639a0 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 708 | const unsigned short spaces_time = 6; |
| 709 | const unsigned long long max_seconds = 100000; |
| 710 | spaces = 0; |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 711 | string.append("\n\nMinimum time between log events per max_dgram_qlen:\n"); |
| 712 | for(unsigned short i = 0; dgramQlen(i); ++i) { |
Mark Salyzyn | fe639a0 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 713 | oldLength = string.length(); |
| 714 | if (spaces < 0) { |
| 715 | spaces = 0; |
| 716 | } |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 717 | string.appendFormat("%*s%u", spaces, "", dgramQlen(i)); |
Mark Salyzyn | fe639a0 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 718 | spaces += spaces_time + oldLength - string.length(); |
| 719 | } |
| 720 | string.append("\n"); |
| 721 | spaces = 0; |
| 722 | unsigned short n; |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 723 | for(unsigned short i = 0; (n = dgramQlen(i)); ++i) { |
Mark Salyzyn | fe639a0 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 724 | unsigned long long duration = minimum(i); |
| 725 | if (duration) { |
| 726 | duration /= n; |
| 727 | if (duration >= (NS_PER_SEC * max_seconds)) { |
| 728 | duration = NS_PER_SEC * (max_seconds - 1); |
| 729 | } |
| 730 | oldLength = string.length(); |
| 731 | if (spaces < 0) { |
| 732 | spaces = 0; |
| 733 | } |
| 734 | string.appendFormat("%*s", spaces, ""); |
| 735 | if (duration >= (NS_PER_SEC * 10)) { |
| 736 | string.appendFormat("%llu", |
| 737 | (duration + (NS_PER_SEC / 2)) |
| 738 | / NS_PER_SEC); |
| 739 | } else if (duration >= (NS_PER_SEC / (1000 / 10))) { |
| 740 | string.appendFormat("%llum", |
| 741 | (duration + (NS_PER_SEC / 2 / 1000)) |
| 742 | / (NS_PER_SEC / 1000)); |
| 743 | } else if (duration >= (NS_PER_SEC / (1000000 / 10))) { |
| 744 | string.appendFormat("%lluu", |
Mark Salyzyn | 03a6a1a | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 745 | (duration + (NS_PER_SEC / 2 / 1000000)) |
| 746 | / (NS_PER_SEC / 1000000)); |
Mark Salyzyn | fe639a0 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 747 | } else { |
| 748 | string.appendFormat("%llun", duration); |
| 749 | } |
| 750 | spaces -= string.length() - oldLength; |
| 751 | } |
| 752 | spaces += spaces_time; |
| 753 | } |
| 754 | } |
| 755 | |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 756 | log_id_for_each(i) { |
| 757 | if (!(logMask & (1 << i))) { |
| 758 | continue; |
| 759 | } |
| 760 | |
Mark Salyzyn | 692e3c3 | 2014-03-26 10:46:39 -0700 | [diff] [blame] | 761 | header = false; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 762 | bool first = true; |
| 763 | |
| 764 | UidStatisticsCollection::iterator ut; |
| 765 | for(ut = id(i).begin(); ut != id(i).end(); ++ut) { |
| 766 | UidStatistics *up = *ut; |
| 767 | if ((uid != AID_ROOT) && (uid != up->getUid())) { |
| 768 | continue; |
| 769 | } |
| 770 | |
| 771 | PidStatisticsCollection::iterator pt = up->begin(); |
| 772 | if (pt == up->end()) { |
| 773 | continue; |
| 774 | } |
| 775 | |
| 776 | android::String8 intermediate; |
| 777 | |
| 778 | if (!header) { |
| 779 | // header below tuned to match spaces_total and spaces_current |
| 780 | spaces = 0; |
| 781 | intermediate = string.format("%s: UID/PID Total size/num", |
| 782 | android_log_id_to_name(i)); |
| 783 | string.appendFormat("\n\n%-31sNow " |
| 784 | "UID/PID[?] Total Now", |
| 785 | intermediate.string()); |
| 786 | intermediate.clear(); |
| 787 | header = true; |
| 788 | } |
| 789 | |
| 790 | bool oneline = ++pt == up->end(); |
| 791 | --pt; |
| 792 | |
| 793 | if (!oneline) { |
| 794 | first = true; |
Mark Salyzyn | fe639a0 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 795 | } else if (!first && (spaces > 0)) { |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 796 | string.appendFormat("%*s", spaces, ""); |
| 797 | } |
| 798 | spaces = 0; |
| 799 | |
| 800 | uid_t u = up->getUid(); |
Mark Salyzyn | 8fa720c | 2014-04-21 10:09:42 -0700 | [diff] [blame] | 801 | PidStatistics *pp = *pt; |
| 802 | pid_t p = pp->getPid(); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 803 | |
Chih-Hung Hsieh | 788dd4f | 2014-09-12 16:02:28 -0700 | [diff] [blame] | 804 | if (!oneline) { |
| 805 | intermediate = string.format("%d", u); |
| 806 | } else if (p == PidStatistics::gone) { |
| 807 | intermediate = string.format("%d/?", u); |
| 808 | } else if (pp->pidGone()) { |
| 809 | intermediate = string.format("%d/%d?", u, p); |
| 810 | } else { |
| 811 | intermediate = string.format("%d/%d", u, p); |
| 812 | } |
Mark Salyzyn | 692e3c3 | 2014-03-26 10:46:39 -0700 | [diff] [blame] | 813 | string.appendFormat(first ? "\n%-12s" : "%-12s", |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 814 | intermediate.string()); |
| 815 | intermediate.clear(); |
| 816 | |
| 817 | size_t elsTotal = up->elementsTotal(); |
| 818 | oldLength = string.length(); |
| 819 | string.appendFormat("%zu/%zu", up->sizesTotal(), elsTotal); |
| 820 | spaces += spaces_total + oldLength - string.length(); |
| 821 | |
| 822 | size_t els = up->elements(); |
| 823 | if (els == elsTotal) { |
Mark Salyzyn | fe639a0 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 824 | if (spaces < 0) { |
| 825 | spaces = 0; |
| 826 | } |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 827 | string.appendFormat("%*s=", spaces, ""); |
| 828 | spaces = -1; |
| 829 | } else if (els) { |
| 830 | oldLength = string.length(); |
Mark Salyzyn | fe639a0 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 831 | if (spaces < 0) { |
| 832 | spaces = 0; |
| 833 | } |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 834 | string.appendFormat("%*s%zu/%zu", spaces, "", up->sizes(), els); |
| 835 | spaces -= string.length() - oldLength; |
| 836 | } |
| 837 | spaces += spaces_current; |
| 838 | |
| 839 | first = !first; |
| 840 | |
| 841 | if (oneline) { |
| 842 | continue; |
| 843 | } |
| 844 | |
| 845 | size_t gone_szs = 0; |
| 846 | size_t gone_els = 0; |
| 847 | |
| 848 | for(; pt != up->end(); ++pt) { |
Mark Salyzyn | 8fa720c | 2014-04-21 10:09:42 -0700 | [diff] [blame] | 849 | pp = *pt; |
| 850 | p = pp->getPid(); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 851 | |
| 852 | // If a PID no longer has any current logs, and is not |
| 853 | // active anymore, skip & report totals for gone. |
| 854 | elsTotal = pp->elementsTotal(); |
| 855 | size_t szsTotal = pp->sizesTotal(); |
| 856 | if (p == pp->gone) { |
| 857 | gone_szs += szsTotal; |
| 858 | gone_els += elsTotal; |
| 859 | continue; |
| 860 | } |
| 861 | els = pp->elements(); |
Mark Salyzyn | 8fa720c | 2014-04-21 10:09:42 -0700 | [diff] [blame] | 862 | bool gone = pp->pidGone(); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 863 | if (gone && (els == 0)) { |
| 864 | // ToDo: garbage collection: move this statistical bucket |
| 865 | // from its current UID/PID to UID/? (races and |
| 866 | // wrap around are our achilles heel). Below is |
| 867 | // merely lipservice to catch PIDs that were still |
| 868 | // around when the stats were pruned to zero. |
| 869 | gone_szs += szsTotal; |
| 870 | gone_els += elsTotal; |
| 871 | continue; |
| 872 | } |
| 873 | |
Mark Salyzyn | fe639a0 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 874 | if (!first && (spaces > 0)) { |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 875 | string.appendFormat("%*s", spaces, ""); |
| 876 | } |
| 877 | spaces = 0; |
| 878 | |
Mark Salyzyn | 692e3c3 | 2014-03-26 10:46:39 -0700 | [diff] [blame] | 879 | intermediate = string.format(gone ? "%d/%d?" : "%d/%d", u, p); |
| 880 | string.appendFormat(first ? "\n%-12s" : "%-12s", |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 881 | intermediate.string()); |
| 882 | intermediate.clear(); |
| 883 | |
| 884 | oldLength = string.length(); |
| 885 | string.appendFormat("%zu/%zu", szsTotal, elsTotal); |
| 886 | spaces += spaces_total + oldLength - string.length(); |
| 887 | |
| 888 | if (els == elsTotal) { |
Mark Salyzyn | fe639a0 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 889 | if (spaces < 0) { |
| 890 | spaces = 0; |
| 891 | } |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 892 | string.appendFormat("%*s=", spaces, ""); |
| 893 | spaces = -1; |
| 894 | } else if (els) { |
| 895 | oldLength = string.length(); |
Mark Salyzyn | fe639a0 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 896 | if (spaces < 0) { |
| 897 | spaces = 0; |
| 898 | } |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 899 | string.appendFormat("%*s%zu/%zu", spaces, "", |
| 900 | pp->sizes(), els); |
| 901 | spaces -= string.length() - oldLength; |
| 902 | } |
| 903 | spaces += spaces_current; |
| 904 | |
| 905 | first = !first; |
| 906 | } |
| 907 | |
| 908 | if (gone_els) { |
Mark Salyzyn | fe639a0 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 909 | if (!first && (spaces > 0)) { |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 910 | string.appendFormat("%*s", spaces, ""); |
| 911 | } |
| 912 | |
| 913 | intermediate = string.format("%d/?", u); |
Mark Salyzyn | 692e3c3 | 2014-03-26 10:46:39 -0700 | [diff] [blame] | 914 | string.appendFormat(first ? "\n%-12s" : "%-12s", |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 915 | intermediate.string()); |
| 916 | intermediate.clear(); |
| 917 | |
| 918 | spaces = spaces_total + spaces_current; |
| 919 | |
| 920 | oldLength = string.length(); |
| 921 | string.appendFormat("%zu/%zu", gone_szs, gone_els); |
| 922 | spaces -= string.length() - oldLength; |
| 923 | |
| 924 | first = !first; |
| 925 | } |
| 926 | } |
| 927 | } |
| 928 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 929 | *buf = strdup(string.string()); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 930 | } |
Mark Salyzyn | f67c379 | 2014-04-07 07:15:33 -0700 | [diff] [blame] | 931 | |
| 932 | uid_t LogStatistics::pidToUid(pid_t pid) { |
| 933 | log_id_for_each(i) { |
| 934 | LidStatistics &l = id(i); |
| 935 | UidStatisticsCollection::iterator iu; |
| 936 | for (iu = l.begin(); iu != l.end(); ++iu) { |
| 937 | UidStatistics &u = *(*iu); |
| 938 | PidStatisticsCollection::iterator ip; |
| 939 | for (ip = u.begin(); ip != u.end(); ++ip) { |
| 940 | if ((*ip)->getPid() == pid) { |
| 941 | return u.getUid(); |
| 942 | } |
| 943 | } |
| 944 | } |
| 945 | } |
| 946 | return getuid(); // associate this with the logger |
| 947 | } |