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 | 35f3e18 | 2017-04-14 09:46:57 -0700 | [diff] [blame] | 18 | #include <inttypes.h> |
Mark Salyzyn | b01933e | 2016-04-07 11:06:31 -0700 | [diff] [blame] | 19 | #include <pwd.h> |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 20 | #include <stdio.h> |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 21 | #include <string.h> |
Mark Salyzyn | b01933e | 2016-04-07 11:06:31 -0700 | [diff] [blame] | 22 | #include <sys/types.h> |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 23 | #include <unistd.h> |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 24 | |
Mark Salyzyn | 6ed8249 | 2016-10-05 12:34:37 -0700 | [diff] [blame] | 25 | #include <list> |
| 26 | |
Mark Salyzyn | 892fa4c | 2017-04-14 09:46:57 -0700 | [diff] [blame^] | 27 | #include <private/android_logger.h> |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 28 | |
| 29 | #include "LogStatistics.h" |
| 30 | |
Mark Salyzyn | 892fa4c | 2017-04-14 09:46:57 -0700 | [diff] [blame^] | 31 | static const uint64_t hourSec = 60 * 60; |
| 32 | static const uint64_t monthSec = 31 * 24 * hourSec; |
| 33 | |
Mark Salyzyn | 150d41e | 2016-09-12 10:29:17 -0700 | [diff] [blame] | 34 | size_t LogStatistics::SizesTotal; |
| 35 | |
Mark Salyzyn | cd766f9 | 2015-05-12 15:21:31 -0700 | [diff] [blame] | 36 | LogStatistics::LogStatistics() : enable(false) { |
Mark Salyzyn | 892fa4c | 2017-04-14 09:46:57 -0700 | [diff] [blame^] | 37 | log_time now(CLOCK_REALTIME); |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 38 | log_id_for_each(id) { |
| 39 | mSizes[id] = 0; |
| 40 | mElements[id] = 0; |
Mark Salyzyn | d745c72 | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 41 | mDroppedElements[id] = 0; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 42 | mSizesTotal[id] = 0; |
| 43 | mElementsTotal[id] = 0; |
Mark Salyzyn | 892fa4c | 2017-04-14 09:46:57 -0700 | [diff] [blame^] | 44 | mOldest[id] = now; |
| 45 | mNewest[id] = now; |
| 46 | mNewestDropped[id] = now; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | |
Mark Salyzyn | 1435b47 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 50 | namespace android { |
| 51 | |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 52 | size_t sizesTotal() { |
| 53 | return LogStatistics::sizesTotal(); |
| 54 | } |
Mark Salyzyn | 150d41e | 2016-09-12 10:29:17 -0700 | [diff] [blame] | 55 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 56 | // caller must own and free character string |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 57 | char* pidToName(pid_t pid) { |
| 58 | char* retval = NULL; |
| 59 | if (pid == 0) { // special case from auditd/klogd for kernel |
Mark Salyzyn | e73a18b | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 60 | retval = strdup("logd"); |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 61 | } else { |
Mark Salyzyn | 5766f80 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 62 | char buffer[512]; |
| 63 | snprintf(buffer, sizeof(buffer), "/proc/%u/cmdline", pid); |
| 64 | int fd = open(buffer, O_RDONLY); |
| 65 | if (fd >= 0) { |
| 66 | ssize_t ret = read(fd, buffer, sizeof(buffer)); |
| 67 | if (ret > 0) { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 68 | buffer[sizeof(buffer) - 1] = '\0'; |
Mark Salyzyn | 5766f80 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 69 | // frameworks intermediate state |
Mark Salyzyn | 292e1f9 | 2016-12-02 10:08:48 -0800 | [diff] [blame] | 70 | if (fastcmp<strcmp>(buffer, "<pre-initialized>")) { |
Mark Salyzyn | 5766f80 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 71 | retval = strdup(buffer); |
| 72 | } |
| 73 | } |
| 74 | close(fd); |
| 75 | } |
| 76 | } |
| 77 | return retval; |
| 78 | } |
Mark Salyzyn | 1435b47 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Mark Salyzyn | 35f3e18 | 2017-04-14 09:46:57 -0700 | [diff] [blame] | 81 | void LogStatistics::addTotal(LogBufferElement* element) { |
| 82 | if (element->getDropped()) return; |
| 83 | |
| 84 | log_id_t log_id = element->getLogId(); |
| 85 | unsigned short size = element->getMsgLen(); |
| 86 | mSizesTotal[log_id] += size; |
| 87 | SizesTotal += size; |
| 88 | ++mElementsTotal[log_id]; |
| 89 | } |
| 90 | |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 91 | void LogStatistics::add(LogBufferElement* element) { |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 92 | log_id_t log_id = element->getLogId(); |
| 93 | unsigned short size = element->getMsgLen(); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 94 | mSizes[log_id] += size; |
| 95 | ++mElements[log_id]; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 96 | |
Mark Salyzyn | 35f3e18 | 2017-04-14 09:46:57 -0700 | [diff] [blame] | 97 | // When caller adding a chatty entry, they will have already |
| 98 | // called add() and subtract() for each entry as they are |
| 99 | // evaluated and trimmed, thus recording size and number of |
| 100 | // elements, but we must recognize the manufactured dropped |
| 101 | // entry as not contributing to the lifetime totals. |
Mark Salyzyn | 2456d04 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 102 | if (element->getDropped()) { |
| 103 | ++mDroppedElements[log_id]; |
| 104 | } else { |
Mark Salyzyn | 2456d04 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 105 | mSizesTotal[log_id] += size; |
Mark Salyzyn | 150d41e | 2016-09-12 10:29:17 -0700 | [diff] [blame] | 106 | SizesTotal += size; |
Mark Salyzyn | 2456d04 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 107 | ++mElementsTotal[log_id]; |
| 108 | } |
Mark Salyzyn | 1435b47 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 109 | |
Mark Salyzyn | 892fa4c | 2017-04-14 09:46:57 -0700 | [diff] [blame^] | 110 | log_time stamp(element->getRealTime()); |
| 111 | if (mNewest[log_id] < stamp) { |
| 112 | // A major time update invalidates the statistics :-( |
| 113 | log_time diff = stamp - mNewest[log_id]; |
| 114 | mNewest[log_id] = stamp; |
| 115 | |
| 116 | if (diff.tv_sec > hourSec) { |
| 117 | // approximate Do-Your-Best fixup |
| 118 | diff += mOldest[log_id]; |
| 119 | if ((diff > stamp) && ((diff - stamp).tv_sec < hourSec)) { |
| 120 | diff = stamp; |
| 121 | } |
| 122 | if (diff <= stamp) { |
| 123 | mOldest[log_id] = diff; |
| 124 | if (mNewestDropped[log_id] < diff) { |
| 125 | mNewestDropped[log_id] = diff; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
Mark Salyzyn | e73a18b | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 131 | if (log_id == LOG_ID_KERNEL) { |
| 132 | return; |
| 133 | } |
| 134 | |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 135 | uidTable[log_id].add(element->getUid(), element); |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 136 | if (element->getUid() == AID_SYSTEM) { |
| 137 | pidSystemTable[log_id].add(element->getPid(), element); |
| 138 | } |
Mark Salyzyn | e73a18b | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 139 | |
Mark Salyzyn | 1435b47 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 140 | if (!enable) { |
| 141 | return; |
| 142 | } |
| 143 | |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 144 | pidTable.add(element->getPid(), element); |
| 145 | tidTable.add(element->getTid(), element); |
Mark Salyzyn | f96ac90 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 146 | |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 147 | uint32_t tag = element->getTag(); |
Mark Salyzyn | f96ac90 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 148 | if (tag) { |
Mark Salyzyn | 8885daf | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 149 | if (log_id == LOG_ID_SECURITY) { |
| 150 | securityTagTable.add(tag, element); |
| 151 | } else { |
| 152 | tagTable.add(tag, element); |
| 153 | } |
Mark Salyzyn | f96ac90 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 154 | } |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 157 | void LogStatistics::subtract(LogBufferElement* element) { |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 158 | log_id_t log_id = element->getLogId(); |
| 159 | unsigned short size = element->getMsgLen(); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 160 | mSizes[log_id] -= size; |
| 161 | --mElements[log_id]; |
Mark Salyzyn | d745c72 | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 162 | if (element->getDropped()) { |
| 163 | --mDroppedElements[log_id]; |
| 164 | } |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 165 | |
Mark Salyzyn | 892fa4c | 2017-04-14 09:46:57 -0700 | [diff] [blame^] | 166 | if (mOldest[log_id] < element->getRealTime()) { |
| 167 | mOldest[log_id] = element->getRealTime(); |
| 168 | } |
| 169 | |
Mark Salyzyn | e73a18b | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 170 | if (log_id == LOG_ID_KERNEL) { |
| 171 | return; |
| 172 | } |
| 173 | |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 174 | uidTable[log_id].subtract(element->getUid(), element); |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 175 | if (element->getUid() == AID_SYSTEM) { |
| 176 | pidSystemTable[log_id].subtract(element->getPid(), element); |
| 177 | } |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 178 | |
Mark Salyzyn | 1435b47 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 179 | if (!enable) { |
| 180 | return; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 181 | } |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 182 | |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 183 | pidTable.subtract(element->getPid(), element); |
| 184 | tidTable.subtract(element->getTid(), element); |
Mark Salyzyn | f96ac90 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 185 | |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 186 | uint32_t tag = element->getTag(); |
Mark Salyzyn | f96ac90 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 187 | if (tag) { |
Mark Salyzyn | 8885daf | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 188 | if (log_id == LOG_ID_SECURITY) { |
| 189 | securityTagTable.subtract(tag, element); |
| 190 | } else { |
| 191 | tagTable.subtract(tag, element); |
| 192 | } |
Mark Salyzyn | f96ac90 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 193 | } |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 196 | // Atomically set an entry to drop |
| 197 | // entry->setDropped(1) must follow this call, caller should do this explicitly. |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 198 | void LogStatistics::drop(LogBufferElement* element) { |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 199 | log_id_t log_id = element->getLogId(); |
| 200 | unsigned short size = element->getMsgLen(); |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 201 | mSizes[log_id] -= size; |
Mark Salyzyn | d745c72 | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 202 | ++mDroppedElements[log_id]; |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 203 | |
Mark Salyzyn | 892fa4c | 2017-04-14 09:46:57 -0700 | [diff] [blame^] | 204 | if (mNewestDropped[log_id] < element->getRealTime()) { |
| 205 | mNewestDropped[log_id] = element->getRealTime(); |
| 206 | } |
| 207 | |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 208 | uidTable[log_id].drop(element->getUid(), element); |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 209 | if (element->getUid() == AID_SYSTEM) { |
| 210 | pidSystemTable[log_id].drop(element->getPid(), element); |
| 211 | } |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 212 | |
| 213 | if (!enable) { |
| 214 | return; |
| 215 | } |
| 216 | |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 217 | pidTable.drop(element->getPid(), element); |
| 218 | tidTable.drop(element->getTid(), element); |
Mark Salyzyn | c572fc6 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 219 | |
| 220 | uint32_t tag = element->getTag(); |
| 221 | if (tag) { |
| 222 | if (log_id == LOG_ID_SECURITY) { |
| 223 | securityTagTable.drop(tag, element); |
| 224 | } else { |
| 225 | tagTable.drop(tag, element); |
| 226 | } |
| 227 | } |
Mark Salyzyn | a69d5a4 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 230 | // caller must own and free character string |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 231 | const char* LogStatistics::uidToName(uid_t uid) const { |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 232 | // Local hard coded favourites |
| 233 | if (uid == AID_LOGD) { |
| 234 | return strdup("auditd"); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 235 | } |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 236 | |
Mark Salyzyn | b01933e | 2016-04-07 11:06:31 -0700 | [diff] [blame] | 237 | // Android system |
| 238 | if (uid < AID_APP) { |
| 239 | // in bionic, thread safe as long as we copy the results |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 240 | struct passwd* pwd = getpwuid(uid); |
Mark Salyzyn | b01933e | 2016-04-07 11:06:31 -0700 | [diff] [blame] | 241 | if (pwd) { |
| 242 | return strdup(pwd->pw_name); |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 243 | } |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 244 | } |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 245 | |
Mark Salyzyn | 9018bc9 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 246 | // Parse /data/system/packages.list |
Jeff Sharkey | eff4d58 | 2016-12-13 11:55:19 -0700 | [diff] [blame] | 247 | uid_t userId = uid % AID_USER_OFFSET; |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 248 | const char* name = android::uidToName(userId); |
Mark Salyzyn | f9908b0 | 2015-04-29 12:48:45 -0700 | [diff] [blame] | 249 | if (!name && (userId > (AID_SHARED_GID_START - AID_APP))) { |
| 250 | name = android::uidToName(userId - (AID_SHARED_GID_START - AID_APP)); |
| 251 | } |
Mark Salyzyn | 9018bc9 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 252 | if (name) { |
| 253 | return name; |
| 254 | } |
Mark Salyzyn | 1435b47 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 255 | |
Mark Salyzyn | b01933e | 2016-04-07 11:06:31 -0700 | [diff] [blame] | 256 | // Android application |
| 257 | if (uid >= AID_APP) { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 258 | struct passwd* pwd = getpwuid(uid); |
Mark Salyzyn | b01933e | 2016-04-07 11:06:31 -0700 | [diff] [blame] | 259 | if (pwd) { |
| 260 | return strdup(pwd->pw_name); |
| 261 | } |
| 262 | } |
| 263 | |
Mark Salyzyn | 1435b47 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 264 | // report uid -> pid(s) -> pidToName if unique |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 265 | for (pidTable_t::const_iterator it = pidTable.begin(); it != pidTable.end(); |
| 266 | ++it) { |
| 267 | const PidEntry& entry = it->second; |
Mark Salyzyn | 1435b47 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 268 | |
| 269 | if (entry.getUid() == uid) { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 270 | const char* nameTmp = entry.getName(); |
Mark Salyzyn | 1435b47 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 271 | |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 272 | if (nameTmp) { |
Mark Salyzyn | 1435b47 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 273 | if (!name) { |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 274 | name = strdup(nameTmp); |
Mark Salyzyn | 292e1f9 | 2016-12-02 10:08:48 -0800 | [diff] [blame] | 275 | } else if (fastcmp<strcmp>(name, nameTmp)) { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 276 | free(const_cast<char*>(name)); |
Mark Salyzyn | f9908b0 | 2015-04-29 12:48:45 -0700 | [diff] [blame] | 277 | name = NULL; |
| 278 | break; |
Mark Salyzyn | 1435b47 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 284 | // No one |
Mark Salyzyn | 1435b47 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 285 | return name; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 288 | std::string UidEntry::formatHeader(const std::string& name, log_id_t id) const { |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 289 | bool isprune = worstUidEnabledForLogid(id); |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 290 | return formatLine(android::base::StringPrintf(name.c_str(), |
| 291 | android_log_id_to_name(id)), |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 292 | std::string("Size"), |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 293 | std::string(isprune ? "+/- Pruned" : "")) + |
| 294 | formatLine(std::string("UID PACKAGE"), std::string("BYTES"), |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 295 | std::string(isprune ? "NUM" : "")); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 296 | } |
| 297 | |
Mark Salyzyn | b2ffdd7 | 2017-04-13 15:35:22 -0700 | [diff] [blame] | 298 | // Helper to truncate name, if too long, and add name dressings |
| 299 | static void formatTmp(const LogStatistics& stat, const char* nameTmp, uid_t uid, |
| 300 | std::string& name, std::string& size, size_t nameLen) { |
| 301 | const char* allocNameTmp = nullptr; |
| 302 | if (!nameTmp) nameTmp = allocNameTmp = stat.uidToName(uid); |
| 303 | if (nameTmp) { |
| 304 | size_t lenSpace = std::max(nameLen - name.length(), (size_t)1); |
| 305 | size_t len = EntryBaseConstants::total_len - |
| 306 | EntryBaseConstants::pruned_len - size.length() - |
| 307 | name.length() - lenSpace - 2; |
| 308 | size_t lenNameTmp = strlen(nameTmp); |
| 309 | while ((len < lenNameTmp) && (lenSpace > 1)) { |
| 310 | ++len; |
| 311 | --lenSpace; |
| 312 | } |
| 313 | name += android::base::StringPrintf("%*s", (int)lenSpace, ""); |
| 314 | if (len < lenNameTmp) { |
| 315 | name += "..."; |
| 316 | nameTmp += lenNameTmp - std::max(len - 3, (size_t)1); |
| 317 | } |
| 318 | name += nameTmp; |
| 319 | free(const_cast<char*>(allocNameTmp)); |
| 320 | } |
| 321 | } |
| 322 | |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 323 | std::string UidEntry::format(const LogStatistics& stat, log_id_t id) const { |
Mark Salyzyn | b921ab5 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 324 | uid_t uid = getUid(); |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 325 | std::string name = android::base::StringPrintf("%u", uid); |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 326 | std::string size = android::base::StringPrintf("%zu", getSizes()); |
| 327 | |
Mark Salyzyn | b2ffdd7 | 2017-04-13 15:35:22 -0700 | [diff] [blame] | 328 | formatTmp(stat, nullptr, uid, name, size, 6); |
| 329 | |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 330 | std::string pruned = ""; |
Mark Salyzyn | 6b477ef | 2015-08-24 11:08:00 -0700 | [diff] [blame] | 331 | if (worstUidEnabledForLogid(id)) { |
| 332 | size_t totalDropped = 0; |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 333 | for (LogStatistics::uidTable_t::const_iterator it = |
| 334 | stat.uidTable[id].begin(); |
| 335 | it != stat.uidTable[id].end(); ++it) { |
Mark Salyzyn | 6b477ef | 2015-08-24 11:08:00 -0700 | [diff] [blame] | 336 | totalDropped += it->second.getDropped(); |
| 337 | } |
| 338 | size_t sizes = stat.sizes(id); |
| 339 | size_t totalSize = stat.sizesTotal(id); |
| 340 | size_t totalElements = stat.elementsTotal(id); |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 341 | float totalVirtualSize = |
| 342 | (float)sizes + (float)totalDropped * totalSize / totalElements; |
Mark Salyzyn | 6b477ef | 2015-08-24 11:08:00 -0700 | [diff] [blame] | 343 | size_t entrySize = getSizes(); |
| 344 | float virtualEntrySize = entrySize; |
| 345 | int realPermille = virtualEntrySize * 1000.0 / sizes; |
| 346 | size_t dropped = getDropped(); |
| 347 | if (dropped) { |
| 348 | pruned = android::base::StringPrintf("%zu", dropped); |
| 349 | virtualEntrySize += (float)dropped * totalSize / totalElements; |
| 350 | } |
| 351 | int virtualPermille = virtualEntrySize * 1000.0 / totalVirtualSize; |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 352 | int permille = |
| 353 | (realPermille - virtualPermille) * 1000L / (virtualPermille ?: 1); |
Mark Salyzyn | 6b477ef | 2015-08-24 11:08:00 -0700 | [diff] [blame] | 354 | if ((permille < -1) || (1 < permille)) { |
| 355 | std::string change; |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 356 | const char* units = "%"; |
| 357 | const char* prefix = (permille > 0) ? "+" : ""; |
Mark Salyzyn | 6b477ef | 2015-08-24 11:08:00 -0700 | [diff] [blame] | 358 | |
| 359 | if (permille > 999) { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 360 | permille = (permille + 1000) / 100; // Now tenths fold |
Mark Salyzyn | 6b477ef | 2015-08-24 11:08:00 -0700 | [diff] [blame] | 361 | units = "X"; |
| 362 | prefix = ""; |
| 363 | } |
| 364 | if ((-99 < permille) && (permille < 99)) { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 365 | change = android::base::StringPrintf( |
| 366 | "%s%d.%u%s", prefix, permille / 10, |
Mark Salyzyn | 6b477ef | 2015-08-24 11:08:00 -0700 | [diff] [blame] | 367 | ((permille < 0) ? (-permille % 10) : (permille % 10)), |
| 368 | units); |
| 369 | } else { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 370 | change = android::base::StringPrintf( |
| 371 | "%s%d%s", prefix, (permille + 5) / 10, units); |
Mark Salyzyn | 6b477ef | 2015-08-24 11:08:00 -0700 | [diff] [blame] | 372 | } |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 373 | ssize_t spaces = EntryBaseConstants::pruned_len - 2 - |
| 374 | pruned.length() - change.length(); |
Mark Salyzyn | 6b477ef | 2015-08-24 11:08:00 -0700 | [diff] [blame] | 375 | if ((spaces <= 0) && pruned.length()) { |
| 376 | spaces = 1; |
| 377 | } |
Mark Salyzyn | 2154ac3 | 2016-12-19 22:23:03 +0000 | [diff] [blame] | 378 | if (spaces > 0) { |
Mark Salyzyn | 6b477ef | 2015-08-24 11:08:00 -0700 | [diff] [blame] | 379 | change += android::base::StringPrintf("%*s", (int)spaces, ""); |
| 380 | } |
| 381 | pruned = change + pruned; |
| 382 | } |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 385 | std::string output = formatLine(name, size, pruned); |
| 386 | |
| 387 | if (uid != AID_SYSTEM) { |
| 388 | return output; |
| 389 | } |
| 390 | |
| 391 | static const size_t maximum_sorted_entries = 32; |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 392 | std::unique_ptr<const PidEntry* []> sorted = |
| 393 | stat.pidSystemTable[id].sort(uid, (pid_t)0, maximum_sorted_entries); |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 394 | |
| 395 | if (!sorted.get()) { |
| 396 | return output; |
| 397 | } |
| 398 | std::string byPid; |
| 399 | size_t index; |
| 400 | bool hasDropped = false; |
| 401 | for (index = 0; index < maximum_sorted_entries; ++index) { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 402 | const PidEntry* entry = sorted[index]; |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 403 | if (!entry) { |
| 404 | break; |
| 405 | } |
| 406 | if (entry->getSizes() <= (getSizes() / 100)) { |
| 407 | break; |
| 408 | } |
| 409 | if (entry->getDropped()) { |
| 410 | hasDropped = true; |
| 411 | } |
| 412 | byPid += entry->format(stat, id); |
| 413 | } |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 414 | if (index > 1) { // print this only if interesting |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 415 | std::string ditto("\" "); |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 416 | output += formatLine(std::string(" PID/UID COMMAND LINE"), ditto, |
| 417 | hasDropped ? ditto : std::string("")); |
Mark Salyzyn | 538c6a9 | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 418 | output += byPid; |
| 419 | } |
| 420 | |
| 421 | return output; |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 424 | std::string PidEntry::formatHeader(const std::string& name, |
| 425 | log_id_t /* id */) const { |
| 426 | return formatLine(name, std::string("Size"), std::string("Pruned")) + |
| 427 | formatLine(std::string(" PID/UID COMMAND LINE"), |
| 428 | std::string("BYTES"), std::string("NUM")); |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 431 | std::string PidEntry::format(const LogStatistics& stat, |
| 432 | log_id_t /* id */) const { |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 433 | uid_t uid = getUid(); |
Mark Salyzyn | b921ab5 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 434 | pid_t pid = getPid(); |
| 435 | std::string name = android::base::StringPrintf("%5u/%u", pid, uid); |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 436 | std::string size = android::base::StringPrintf("%zu", getSizes()); |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 437 | |
Mark Salyzyn | b2ffdd7 | 2017-04-13 15:35:22 -0700 | [diff] [blame] | 438 | formatTmp(stat, getName(), uid, name, size, 12); |
| 439 | |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 440 | std::string pruned = ""; |
| 441 | size_t dropped = getDropped(); |
| 442 | if (dropped) { |
| 443 | pruned = android::base::StringPrintf("%zu", dropped); |
| 444 | } |
| 445 | |
| 446 | return formatLine(name, size, pruned); |
| 447 | } |
| 448 | |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 449 | std::string TidEntry::formatHeader(const std::string& name, |
| 450 | log_id_t /* id */) const { |
| 451 | return formatLine(name, std::string("Size"), std::string("Pruned")) + |
| 452 | formatLine(std::string(" TID/UID COMM"), std::string("BYTES"), |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 453 | std::string("NUM")); |
| 454 | } |
| 455 | |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 456 | std::string TidEntry::format(const LogStatistics& stat, |
| 457 | log_id_t /* id */) const { |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 458 | uid_t uid = getUid(); |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 459 | std::string name = android::base::StringPrintf("%5u/%u", getTid(), uid); |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 460 | std::string size = android::base::StringPrintf("%zu", getSizes()); |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 461 | |
Mark Salyzyn | b2ffdd7 | 2017-04-13 15:35:22 -0700 | [diff] [blame] | 462 | formatTmp(stat, getName(), uid, name, size, 12); |
| 463 | |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 464 | std::string pruned = ""; |
| 465 | size_t dropped = getDropped(); |
| 466 | if (dropped) { |
| 467 | pruned = android::base::StringPrintf("%zu", dropped); |
| 468 | } |
| 469 | |
| 470 | return formatLine(name, size, pruned); |
| 471 | } |
| 472 | |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 473 | std::string TagEntry::formatHeader(const std::string& name, log_id_t id) const { |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 474 | bool isprune = worstUidEnabledForLogid(id); |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 475 | return formatLine(name, std::string("Size"), |
| 476 | std::string(isprune ? "Prune" : "")) + |
| 477 | formatLine(std::string(" TAG/UID TAGNAME"), |
| 478 | std::string("BYTES"), std::string(isprune ? "NUM" : "")); |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 481 | std::string TagEntry::format(const LogStatistics& /* stat */, |
| 482 | log_id_t /* id */) const { |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 483 | std::string name; |
| 484 | uid_t uid = getUid(); |
| 485 | if (uid == (uid_t)-1) { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 486 | name = android::base::StringPrintf("%7u", getKey()); |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 487 | } else { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 488 | name = android::base::StringPrintf("%7u/%u", getKey(), uid); |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 489 | } |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 490 | const char* nameTmp = getName(); |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 491 | if (nameTmp) { |
| 492 | name += android::base::StringPrintf( |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 493 | "%*s%s", (int)std::max(14 - name.length(), (size_t)1), "", nameTmp); |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 496 | std::string size = android::base::StringPrintf("%zu", getSizes()); |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 497 | |
| 498 | std::string pruned = ""; |
Mark Salyzyn | c572fc6 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 499 | size_t dropped = getDropped(); |
| 500 | if (dropped) { |
| 501 | pruned = android::base::StringPrintf("%zu", dropped); |
| 502 | } |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 503 | |
| 504 | return formatLine(name, size, pruned); |
| 505 | } |
| 506 | |
Mark Salyzyn | 892fa4c | 2017-04-14 09:46:57 -0700 | [diff] [blame^] | 507 | static std::string formatMsec(uint64_t val) { |
| 508 | static const unsigned subsecDigits = 3; |
| 509 | static const uint64_t sec = MS_PER_SEC; |
| 510 | |
| 511 | static const uint64_t minute = 60 * sec; |
| 512 | static const uint64_t hour = 60 * minute; |
| 513 | static const uint64_t day = 24 * hour; |
| 514 | |
| 515 | std::string output; |
| 516 | if (val < sec) return output; |
| 517 | |
| 518 | if (val >= day) { |
| 519 | output = android::base::StringPrintf("%" PRIu64 "d ", val / day); |
| 520 | val = (val % day) + day; |
| 521 | } |
| 522 | if (val >= minute) { |
| 523 | if (val >= hour) { |
| 524 | output += android::base::StringPrintf("%" PRIu64 ":", |
| 525 | (val / hour) % (day / hour)); |
| 526 | } |
| 527 | output += android::base::StringPrintf( |
| 528 | (val >= hour) ? "%02" PRIu64 ":" : "%" PRIu64 ":", |
| 529 | (val / minute) % (hour / minute)); |
| 530 | } |
| 531 | output += |
| 532 | android::base::StringPrintf((val >= minute) ? "%02" PRIu64 : "%" PRIu64, |
| 533 | (val / sec) % (minute / sec)); |
| 534 | val %= sec; |
| 535 | unsigned digits = subsecDigits; |
| 536 | while (digits && ((val % 10) == 0)) { |
| 537 | val /= 10; |
| 538 | --digits; |
| 539 | } |
| 540 | if (digits) { |
| 541 | output += android::base::StringPrintf(".%0*" PRIu64, digits, val); |
| 542 | } |
| 543 | return output; |
| 544 | } |
| 545 | |
Mark Salyzyn | b921ab5 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 546 | std::string LogStatistics::format(uid_t uid, pid_t pid, |
| 547 | unsigned int logMask) const { |
Mark Salyzyn | 5766f80 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 548 | static const unsigned short spaces_total = 19; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 549 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 550 | // Report on total logging, current and for all time |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 551 | |
Mark Salyzyn | 09a91e2 | 2015-08-19 15:33:01 -0700 | [diff] [blame] | 552 | std::string output = "size/num"; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 553 | size_t oldLength; |
| 554 | short spaces = 1; |
| 555 | |
| 556 | log_id_for_each(id) { |
Mark Salyzyn | 6ed8249 | 2016-10-05 12:34:37 -0700 | [diff] [blame] | 557 | if (!(logMask & (1 << id))) continue; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 558 | oldLength = output.length(); |
Mark Salyzyn | 6ed8249 | 2016-10-05 12:34:37 -0700 | [diff] [blame] | 559 | if (spaces < 0) spaces = 0; |
Mark Salyzyn | 09a91e2 | 2015-08-19 15:33:01 -0700 | [diff] [blame] | 560 | output += android::base::StringPrintf("%*s%s", spaces, "", |
| 561 | android_log_id_to_name(id)); |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 562 | spaces += spaces_total + oldLength - output.length(); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 563 | } |
Mark Salyzyn | 6ed8249 | 2016-10-05 12:34:37 -0700 | [diff] [blame] | 564 | if (spaces < 0) spaces = 0; |
| 565 | output += android::base::StringPrintf("%*sTotal", spaces, ""); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 566 | |
Mark Salyzyn | 6ed8249 | 2016-10-05 12:34:37 -0700 | [diff] [blame] | 567 | static const char TotalStr[] = "\nTotal"; |
| 568 | spaces = 10 - strlen(TotalStr); |
| 569 | output += TotalStr; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 570 | |
Mark Salyzyn | 6ed8249 | 2016-10-05 12:34:37 -0700 | [diff] [blame] | 571 | size_t totalSize = 0; |
| 572 | size_t totalEls = 0; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 573 | log_id_for_each(id) { |
Mark Salyzyn | 6ed8249 | 2016-10-05 12:34:37 -0700 | [diff] [blame] | 574 | if (!(logMask & (1 << id))) continue; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 575 | oldLength = output.length(); |
Mark Salyzyn | 6ed8249 | 2016-10-05 12:34:37 -0700 | [diff] [blame] | 576 | if (spaces < 0) spaces = 0; |
| 577 | size_t szs = sizesTotal(id); |
| 578 | totalSize += szs; |
| 579 | size_t els = elementsTotal(id); |
| 580 | totalEls += els; |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 581 | output += |
| 582 | android::base::StringPrintf("%*s%zu/%zu", spaces, "", szs, els); |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 583 | spaces += spaces_total + oldLength - output.length(); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 584 | } |
Mark Salyzyn | 6ed8249 | 2016-10-05 12:34:37 -0700 | [diff] [blame] | 585 | if (spaces < 0) spaces = 0; |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 586 | output += android::base::StringPrintf("%*s%zu/%zu", spaces, "", totalSize, |
| 587 | totalEls); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 588 | |
Mark Salyzyn | 6ed8249 | 2016-10-05 12:34:37 -0700 | [diff] [blame] | 589 | static const char NowStr[] = "\nNow"; |
| 590 | spaces = 10 - strlen(NowStr); |
| 591 | output += NowStr; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 592 | |
Mark Salyzyn | 6ed8249 | 2016-10-05 12:34:37 -0700 | [diff] [blame] | 593 | totalSize = 0; |
| 594 | totalEls = 0; |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 595 | log_id_for_each(id) { |
Mark Salyzyn | 6ed8249 | 2016-10-05 12:34:37 -0700 | [diff] [blame] | 596 | if (!(logMask & (1 << id))) continue; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 597 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 598 | size_t els = elements(id); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 599 | if (els) { |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 600 | oldLength = output.length(); |
Mark Salyzyn | 6ed8249 | 2016-10-05 12:34:37 -0700 | [diff] [blame] | 601 | if (spaces < 0) spaces = 0; |
| 602 | size_t szs = sizes(id); |
| 603 | totalSize += szs; |
| 604 | totalEls += els; |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 605 | output += |
| 606 | android::base::StringPrintf("%*s%zu/%zu", spaces, "", szs, els); |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 607 | spaces -= output.length() - oldLength; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 608 | } |
| 609 | spaces += spaces_total; |
| 610 | } |
Mark Salyzyn | 6ed8249 | 2016-10-05 12:34:37 -0700 | [diff] [blame] | 611 | if (spaces < 0) spaces = 0; |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 612 | output += android::base::StringPrintf("%*s%zu/%zu", spaces, "", totalSize, |
| 613 | totalEls); |
Mark Salyzyn | 6ed8249 | 2016-10-05 12:34:37 -0700 | [diff] [blame] | 614 | |
Mark Salyzyn | 892fa4c | 2017-04-14 09:46:57 -0700 | [diff] [blame^] | 615 | static const char SpanStr[] = "\nLogspan"; |
| 616 | spaces = 10 - strlen(SpanStr); |
| 617 | output += SpanStr; |
| 618 | |
| 619 | // Total reports the greater of the individual maximum time span, or the |
| 620 | // validated minimum start and maximum end time span if it makes sense. |
| 621 | uint64_t minTime = UINT64_MAX; |
| 622 | uint64_t maxTime = 0; |
| 623 | uint64_t maxSpan = 0; |
| 624 | totalSize = 0; |
| 625 | |
| 626 | log_id_for_each(id) { |
| 627 | if (!(logMask & (1 << id))) continue; |
| 628 | |
| 629 | // validity checking |
| 630 | uint64_t oldest = mOldest[id].msec(); |
| 631 | uint64_t newest = mNewest[id].msec(); |
| 632 | if (newest <= oldest) { |
| 633 | spaces += spaces_total; |
| 634 | continue; |
| 635 | } |
| 636 | |
| 637 | uint64_t span = newest - oldest; |
| 638 | if (span > (monthSec * MS_PER_SEC)) { |
| 639 | spaces += spaces_total; |
| 640 | continue; |
| 641 | } |
| 642 | |
| 643 | // total span |
| 644 | if (minTime > oldest) minTime = oldest; |
| 645 | if (maxTime < newest) maxTime = newest; |
| 646 | if (span > maxSpan) maxSpan = span; |
| 647 | totalSize += span; |
| 648 | |
| 649 | uint64_t dropped = mNewestDropped[id].msec(); |
| 650 | if (dropped < oldest) dropped = oldest; |
| 651 | if (dropped > newest) dropped = newest; |
| 652 | |
| 653 | oldLength = output.length(); |
| 654 | output += android::base::StringPrintf("%*s%s", spaces, "", |
| 655 | formatMsec(span).c_str()); |
| 656 | unsigned permille = ((newest - dropped) * 1000 + (span / 2)) / span; |
| 657 | if ((permille > 1) && (permille < 999)) { |
| 658 | output += android::base::StringPrintf("(%u", permille / 10); |
| 659 | permille %= 10; |
| 660 | if (permille) { |
| 661 | output += android::base::StringPrintf(".%u", permille); |
| 662 | } |
| 663 | output += android::base::StringPrintf("%%)"); |
| 664 | } |
| 665 | spaces -= output.length() - oldLength; |
| 666 | spaces += spaces_total; |
| 667 | } |
| 668 | if ((maxTime > minTime) && ((maxTime -= minTime) < totalSize) && |
| 669 | (maxTime > maxSpan)) { |
| 670 | maxSpan = maxTime; |
| 671 | } |
| 672 | if (spaces < 0) spaces = 0; |
| 673 | output += android::base::StringPrintf("%*s%s", spaces, "", |
| 674 | formatMsec(maxSpan).c_str()); |
| 675 | |
Mark Salyzyn | 6ed8249 | 2016-10-05 12:34:37 -0700 | [diff] [blame] | 676 | static const char OverheadStr[] = "\nOverhead"; |
| 677 | spaces = 10 - strlen(OverheadStr); |
| 678 | output += OverheadStr; |
| 679 | |
| 680 | totalSize = 0; |
| 681 | log_id_for_each(id) { |
| 682 | if (!(logMask & (1 << id))) continue; |
| 683 | |
| 684 | size_t els = elements(id); |
| 685 | if (els) { |
| 686 | oldLength = output.length(); |
| 687 | if (spaces < 0) spaces = 0; |
| 688 | // estimate the std::list overhead. |
| 689 | static const size_t overhead = |
| 690 | ((sizeof(LogBufferElement) + sizeof(uint64_t) - 1) & |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 691 | -sizeof(uint64_t)) + |
Mark Salyzyn | 6ed8249 | 2016-10-05 12:34:37 -0700 | [diff] [blame] | 692 | sizeof(std::list<LogBufferElement*>); |
| 693 | size_t szs = sizes(id) + els * overhead; |
| 694 | totalSize += szs; |
| 695 | output += android::base::StringPrintf("%*s%zu", spaces, "", szs); |
| 696 | spaces -= output.length() - oldLength; |
| 697 | } |
| 698 | spaces += spaces_total; |
| 699 | } |
Mark Salyzyn | e223238 | 2016-10-06 09:55:21 -0700 | [diff] [blame] | 700 | totalSize += sizeOf(); |
Mark Salyzyn | 6ed8249 | 2016-10-05 12:34:37 -0700 | [diff] [blame] | 701 | if (spaces < 0) spaces = 0; |
| 702 | output += android::base::StringPrintf("%*s%zu", spaces, "", totalSize); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 703 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 704 | // Report on Chattiest |
Mark Salyzyn | 692e3c3 | 2014-03-26 10:46:39 -0700 | [diff] [blame] | 705 | |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 706 | std::string name; |
| 707 | |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 708 | // Chattiest by application (UID) |
| 709 | log_id_for_each(id) { |
Mark Salyzyn | 6ed8249 | 2016-10-05 12:34:37 -0700 | [diff] [blame] | 710 | if (!(logMask & (1 << id))) continue; |
Mark Salyzyn | 692e3c3 | 2014-03-26 10:46:39 -0700 | [diff] [blame] | 711 | |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 712 | name = (uid == AID_ROOT) ? "Chattiest UIDs in %s log buffer:" |
| 713 | : "Logging for your UID in %s log buffer:"; |
Mark Salyzyn | b921ab5 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 714 | output += uidTable[id].format(*this, uid, pid, name, id); |
Mark Salyzyn | 1435b47 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 715 | } |
Mark Salyzyn | 692e3c3 | 2014-03-26 10:46:39 -0700 | [diff] [blame] | 716 | |
Mark Salyzyn | 1435b47 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 717 | if (enable) { |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 718 | name = ((uid == AID_ROOT) && !pid) ? "Chattiest PIDs:" |
| 719 | : "Logging for this PID:"; |
Mark Salyzyn | b921ab5 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 720 | output += pidTable.format(*this, uid, pid, name); |
| 721 | name = "Chattiest TIDs"; |
Mark Salyzyn | 6ed8249 | 2016-10-05 12:34:37 -0700 | [diff] [blame] | 722 | if (pid) name += android::base::StringPrintf(" for PID %d", pid); |
Mark Salyzyn | b921ab5 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 723 | name += ":"; |
| 724 | output += tidTable.format(*this, uid, pid, name); |
Mark Salyzyn | af5c67f | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 725 | } |
| 726 | |
Mark Salyzyn | f96ac90 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 727 | if (enable && (logMask & (1 << LOG_ID_EVENTS))) { |
Mark Salyzyn | b921ab5 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 728 | name = "Chattiest events log buffer TAGs"; |
Mark Salyzyn | 6ed8249 | 2016-10-05 12:34:37 -0700 | [diff] [blame] | 729 | if (pid) name += android::base::StringPrintf(" for PID %d", pid); |
Mark Salyzyn | b921ab5 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 730 | name += ":"; |
| 731 | output += tagTable.format(*this, uid, pid, name, LOG_ID_EVENTS); |
Mark Salyzyn | f96ac90 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Mark Salyzyn | 8885daf | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 734 | if (enable && (logMask & (1 << LOG_ID_SECURITY))) { |
Mark Salyzyn | b921ab5 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 735 | name = "Chattiest security log buffer TAGs"; |
Mark Salyzyn | 6ed8249 | 2016-10-05 12:34:37 -0700 | [diff] [blame] | 736 | if (pid) name += android::base::StringPrintf(" for PID %d", pid); |
Mark Salyzyn | b921ab5 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 737 | name += ":"; |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 738 | output += |
| 739 | securityTagTable.format(*this, uid, pid, name, LOG_ID_SECURITY); |
Mark Salyzyn | 8885daf | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 740 | } |
| 741 | |
Mark Salyzyn | 1c7b6fb | 2015-08-20 10:01:44 -0700 | [diff] [blame] | 742 | return output; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 743 | } |
Mark Salyzyn | f67c379 | 2014-04-07 07:15:33 -0700 | [diff] [blame] | 744 | |
Mark Salyzyn | 1435b47 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 745 | namespace android { |
| 746 | |
| 747 | uid_t pidToUid(pid_t pid) { |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 748 | char buffer[512]; |
| 749 | snprintf(buffer, sizeof(buffer), "/proc/%u/status", pid); |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 750 | FILE* fp = fopen(buffer, "r"); |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 751 | if (fp) { |
| 752 | while (fgets(buffer, sizeof(buffer), fp)) { |
| 753 | int uid; |
Mark Salyzyn | 8f5f896 | 2015-04-14 13:07:29 -0700 | [diff] [blame] | 754 | if (sscanf(buffer, "Uid: %d", &uid) == 1) { |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 755 | fclose(fp); |
| 756 | return uid; |
Mark Salyzyn | f67c379 | 2014-04-07 07:15:33 -0700 | [diff] [blame] | 757 | } |
| 758 | } |
Mark Salyzyn | 9cf5d40 | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 759 | fclose(fp); |
Mark Salyzyn | f67c379 | 2014-04-07 07:15:33 -0700 | [diff] [blame] | 760 | } |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 761 | return AID_LOGD; // associate this with the logger |
Mark Salyzyn | f67c379 | 2014-04-07 07:15:33 -0700 | [diff] [blame] | 762 | } |
Mark Salyzyn | 1435b47 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | uid_t LogStatistics::pidToUid(pid_t pid) { |
Mark Salyzyn | 6a40419 | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 766 | return pidTable.add(pid)->second.getUid(); |
Mark Salyzyn | 1435b47 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | // caller must free character string |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 770 | const char* LogStatistics::pidToName(pid_t pid) const { |
Mark Salyzyn | 34eb1b2 | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 771 | // An inconvenient truth ... getName() can alter the object |
Mark Salyzyn | da65bcb | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 772 | pidTable_t& writablePidTable = const_cast<pidTable_t&>(pidTable); |
| 773 | const char* name = writablePidTable.add(pid)->second.getName(); |
Mark Salyzyn | 14d39b3 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 774 | if (!name) { |
| 775 | return NULL; |
Mark Salyzyn | 1435b47 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 776 | } |
Mark Salyzyn | 14d39b3 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 777 | return strdup(name); |
Mark Salyzyn | 1435b47 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 778 | } |