Mark Salyzyn | 34facab | 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 | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 17 | #include <algorithm> // std::max |
Mark Salyzyn | 9a03863 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 18 | #include <fcntl.h> |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 19 | #include <stdio.h> |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 20 | #include <string.h> |
| 21 | #include <unistd.h> |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 22 | |
| 23 | #include <log/logger.h> |
| 24 | #include <private/android_filesystem_config.h> |
| 25 | #include <utils/String8.h> |
| 26 | |
| 27 | #include "LogStatistics.h" |
| 28 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 29 | LogStatistics::LogStatistics() |
| 30 | : enable(false) { |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 31 | log_id_for_each(id) { |
| 32 | mSizes[id] = 0; |
| 33 | mElements[id] = 0; |
| 34 | mSizesTotal[id] = 0; |
| 35 | mElementsTotal[id] = 0; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 39 | namespace android { |
| 40 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 41 | // caller must own and free character string |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 42 | static char *pidToName(pid_t pid) { |
Mark Salyzyn | 9a03863 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 43 | char *retval = NULL; |
Mark Salyzyn | df5aa61 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 44 | if (pid == 0) { // special case from auditd for kernel |
| 45 | retval = strdup("logd.auditd"); |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 46 | } else { |
Mark Salyzyn | 9a03863 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 47 | char buffer[512]; |
| 48 | snprintf(buffer, sizeof(buffer), "/proc/%u/cmdline", pid); |
| 49 | int fd = open(buffer, O_RDONLY); |
| 50 | if (fd >= 0) { |
| 51 | ssize_t ret = read(fd, buffer, sizeof(buffer)); |
| 52 | if (ret > 0) { |
| 53 | buffer[sizeof(buffer)-1] = '\0'; |
| 54 | // frameworks intermediate state |
| 55 | if (strcmp(buffer, "<pre-initialized>")) { |
| 56 | retval = strdup(buffer); |
| 57 | } |
| 58 | } |
| 59 | close(fd); |
| 60 | } |
| 61 | } |
| 62 | return retval; |
| 63 | } |
| 64 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 67 | void LogStatistics::add(LogBufferElement *e) { |
| 68 | log_id_t log_id = e->getLogId(); |
| 69 | unsigned short size = e->getMsgLen(); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 70 | mSizes[log_id] += size; |
| 71 | ++mElements[log_id]; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 72 | |
| 73 | uid_t uid = e->getUid(); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 74 | unsigned short dropped = e->getDropped(); |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 75 | android::hash_t hash = android::hash_type(uid); |
| 76 | uidTable_t &table = uidTable[log_id]; |
| 77 | ssize_t index = table.find(-1, hash, uid); |
| 78 | if (index == -1) { |
| 79 | UidEntry initEntry(uid); |
| 80 | initEntry.add(size); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 81 | initEntry.add_dropped(dropped); |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 82 | table.add(hash, initEntry); |
| 83 | } else { |
| 84 | UidEntry &entry = table.editEntryAt(index); |
| 85 | entry.add(size); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 86 | entry.add_dropped(dropped); |
Mark Salyzyn | f5fc509 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 87 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 88 | |
| 89 | mSizesTotal[log_id] += size; |
| 90 | ++mElementsTotal[log_id]; |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 91 | |
| 92 | if (!enable) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | pid_t pid = e->getPid(); |
| 97 | hash = android::hash_type(pid); |
| 98 | index = pidTable.find(-1, hash, pid); |
| 99 | if (index == -1) { |
| 100 | PidEntry initEntry(pid, uid, android::pidToName(pid)); |
| 101 | initEntry.add(size); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 102 | initEntry.add_dropped(dropped); |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 103 | pidTable.add(hash, initEntry); |
| 104 | } else { |
| 105 | PidEntry &entry = pidTable.editEntryAt(index); |
| 106 | if (entry.getUid() != uid) { |
| 107 | entry.setUid(uid); |
| 108 | entry.setName(android::pidToName(pid)); |
| 109 | } else if (!entry.getName()) { |
| 110 | char *name = android::pidToName(pid); |
| 111 | if (name) { |
| 112 | entry.setName(name); |
| 113 | } |
| 114 | } |
| 115 | entry.add(size); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 116 | entry.add_dropped(dropped); |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 117 | } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 120 | void LogStatistics::subtract(LogBufferElement *e) { |
| 121 | log_id_t log_id = e->getLogId(); |
| 122 | unsigned short size = e->getMsgLen(); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 123 | mSizes[log_id] -= size; |
| 124 | --mElements[log_id]; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 125 | |
| 126 | uid_t uid = e->getUid(); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 127 | unsigned short dropped = e->getDropped(); |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 128 | android::hash_t hash = android::hash_type(uid); |
| 129 | uidTable_t &table = uidTable[log_id]; |
| 130 | ssize_t index = table.find(-1, hash, uid); |
| 131 | if (index != -1) { |
| 132 | UidEntry &entry = table.editEntryAt(index); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 133 | if (entry.subtract(size) || entry.subtract_dropped(dropped)) { |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 134 | table.removeAt(index); |
| 135 | } |
Mark Salyzyn | f5fc509 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 136 | } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 137 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 138 | if (!enable) { |
| 139 | return; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 140 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 141 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 142 | pid_t pid = e->getPid(); |
| 143 | hash = android::hash_type(pid); |
| 144 | index = pidTable.find(-1, hash, pid); |
| 145 | if (index != -1) { |
| 146 | PidEntry &entry = pidTable.editEntryAt(index); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 147 | if (entry.subtract(size) || entry.subtract_dropped(dropped)) { |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 148 | pidTable.removeAt(index); |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 149 | } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 150 | } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 153 | // Atomically set an entry to drop |
| 154 | // entry->setDropped(1) must follow this call, caller should do this explicitly. |
| 155 | void LogStatistics::drop(LogBufferElement *e) { |
| 156 | log_id_t log_id = e->getLogId(); |
| 157 | unsigned short size = e->getMsgLen(); |
| 158 | mSizes[log_id] -= size; |
| 159 | |
| 160 | uid_t uid = e->getUid(); |
| 161 | android::hash_t hash = android::hash_type(uid); |
| 162 | typeof uidTable[0] &table = uidTable[log_id]; |
| 163 | ssize_t index = table.find(-1, hash, uid); |
| 164 | if (index != -1) { |
| 165 | UidEntry &entry = table.editEntryAt(index); |
| 166 | entry.subtract(size); |
| 167 | entry.add_dropped(1); |
| 168 | } |
| 169 | |
| 170 | if (!enable) { |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | pid_t pid = e->getPid(); |
| 175 | hash = android::hash_type(pid); |
| 176 | index = pidTable.find(-1, hash, pid); |
| 177 | if (index != -1) { |
| 178 | PidEntry &entry = pidTable.editEntryAt(index); |
| 179 | entry.subtract(size); |
| 180 | entry.add_dropped(1); |
| 181 | } |
| 182 | } |
| 183 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 184 | // caller must own and free character string |
| 185 | char *LogStatistics::uidToName(uid_t uid) { |
| 186 | // Local hard coded favourites |
| 187 | if (uid == AID_LOGD) { |
| 188 | return strdup("auditd"); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 189 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 190 | |
| 191 | // Android hard coded |
| 192 | const struct android_id_info *info = android_ids; |
| 193 | |
| 194 | for (size_t i = 0; i < android_id_count; ++i) { |
| 195 | if (info->aid == uid) { |
| 196 | return strdup(info->name); |
| 197 | } |
| 198 | ++info; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 199 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 200 | |
Mark Salyzyn | 08739ba | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 201 | // Parse /data/system/packages.list |
| 202 | char *name = android::uidToName(uid); |
| 203 | if (name) { |
| 204 | return name; |
| 205 | } |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 206 | |
| 207 | // report uid -> pid(s) -> pidToName if unique |
| 208 | ssize_t index = -1; |
| 209 | while ((index = pidTable.next(index)) != -1) { |
| 210 | const PidEntry &entry = pidTable.entryAt(index); |
| 211 | |
| 212 | if (entry.getUid() == uid) { |
| 213 | const char *n = entry.getName(); |
| 214 | |
| 215 | if (n) { |
| 216 | if (!name) { |
| 217 | name = strdup(n); |
| 218 | } else if (strcmp(name, n)) { |
| 219 | free(name); |
| 220 | return NULL; |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 226 | // No one |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 227 | return name; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 230 | static void format_line(android::String8 &output, |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 231 | android::String8 &name, android::String8 &size, android::String8 &pruned) { |
| 232 | static const size_t pruned_len = 6; |
| 233 | static const size_t total_len = 70 + pruned_len; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 234 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 235 | ssize_t drop_len = std::max(pruned.length() + 1, pruned_len); |
| 236 | ssize_t size_len = std::max(size.length() + 1, |
| 237 | total_len - name.length() - drop_len - 1); |
| 238 | |
| 239 | if (pruned.length()) { |
| 240 | output.appendFormat("%s%*s%*s\n", name.string(), |
| 241 | (int)size_len, size.string(), |
| 242 | (int)drop_len, pruned.string()); |
| 243 | } else { |
| 244 | output.appendFormat("%s%*s\n", name.string(), |
| 245 | (int)size_len, size.string()); |
| 246 | } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 249 | void LogStatistics::format(char **buf, uid_t uid, unsigned int logMask) { |
Mark Salyzyn | 9a03863 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 250 | static const unsigned short spaces_total = 19; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 251 | |
| 252 | if (*buf) { |
Greg Hackmann | 239605e | 2014-04-06 21:25:58 -0700 | [diff] [blame] | 253 | free(*buf); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 254 | *buf = NULL; |
| 255 | } |
| 256 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 257 | // Report on total logging, current and for all time |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 258 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 259 | android::String8 output("size/num"); |
| 260 | size_t oldLength; |
| 261 | short spaces = 1; |
| 262 | |
| 263 | log_id_for_each(id) { |
| 264 | if (!(logMask & (1 << id))) { |
Mark Salyzyn | c8a576c | 2014-04-04 16:35:59 -0700 | [diff] [blame] | 265 | continue; |
| 266 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 267 | oldLength = output.length(); |
Mark Salyzyn | c8a576c | 2014-04-04 16:35:59 -0700 | [diff] [blame] | 268 | if (spaces < 0) { |
| 269 | spaces = 0; |
| 270 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 271 | output.appendFormat("%*s%s", spaces, "", android_log_id_to_name(id)); |
| 272 | spaces += spaces_total + oldLength - output.length(); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 273 | } |
| 274 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 275 | spaces = 4; |
| 276 | output.appendFormat("\nTotal"); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 277 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 278 | log_id_for_each(id) { |
| 279 | if (!(logMask & (1 << id))) { |
| 280 | continue; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 281 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 282 | oldLength = output.length(); |
| 283 | if (spaces < 0) { |
| 284 | spaces = 0; |
| 285 | } |
| 286 | output.appendFormat("%*s%zu/%zu", spaces, "", |
| 287 | sizesTotal(id), elementsTotal(id)); |
| 288 | spaces += spaces_total + oldLength - output.length(); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 289 | } |
| 290 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 291 | spaces = 6; |
| 292 | output.appendFormat("\nNow"); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 293 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 294 | log_id_for_each(id) { |
| 295 | if (!(logMask & (1 << id))) { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 296 | continue; |
| 297 | } |
| 298 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 299 | size_t els = elements(id); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 300 | if (els) { |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 301 | oldLength = output.length(); |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 302 | if (spaces < 0) { |
| 303 | spaces = 0; |
| 304 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 305 | output.appendFormat("%*s%zu/%zu", spaces, "", sizes(id), els); |
| 306 | spaces -= output.length() - oldLength; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 307 | } |
| 308 | spaces += spaces_total; |
| 309 | } |
| 310 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 311 | // Report on Chattiest |
Mark Salyzyn | 8e72c53 | 2014-03-26 10:46:39 -0700 | [diff] [blame] | 312 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 313 | // Chattiest by application (UID) |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 314 | static const size_t maximum_sorted_entries = 32; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 315 | log_id_for_each(id) { |
| 316 | if (!(logMask & (1 << id))) { |
Mark Salyzyn | 8e72c53 | 2014-03-26 10:46:39 -0700 | [diff] [blame] | 317 | continue; |
| 318 | } |
| 319 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 320 | bool headerPrinted = false; |
| 321 | std::unique_ptr<const UidEntry *[]> sorted = sort(maximum_sorted_entries, id); |
| 322 | ssize_t index = -1; |
| 323 | while ((index = uidTable_t::next(index, sorted, maximum_sorted_entries)) >= 0) { |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 324 | const UidEntry *entry = sorted[index]; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 325 | uid_t u = entry->getKey(); |
| 326 | if ((uid != AID_ROOT) && (u != uid)) { |
| 327 | continue; |
| 328 | } |
| 329 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 330 | if (!headerPrinted) { |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 331 | if (uid == AID_ROOT) { |
| 332 | output.appendFormat( |
| 333 | "\n\nChattiest UIDs in %s:\n", |
| 334 | android_log_id_to_name(id)); |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 335 | } else { |
| 336 | output.appendFormat( |
| 337 | "\n\nLogging for your UID in %s:\n", |
| 338 | android_log_id_to_name(id)); |
Mark Salyzyn | 8e72c53 | 2014-03-26 10:46:39 -0700 | [diff] [blame] | 339 | } |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 340 | android::String8 name("UID"); |
| 341 | android::String8 size("Size"); |
| 342 | android::String8 pruned("Pruned"); |
Mark Salyzyn | ae76923 | 2015-03-17 17:17:25 -0700 | [diff] [blame^] | 343 | if (!worstUidEnabledForLogid(id)) { |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 344 | pruned.setTo(""); |
| 345 | } |
| 346 | format_line(output, name, size, pruned); |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 347 | headerPrinted = true; |
Mark Salyzyn | 8e72c53 | 2014-03-26 10:46:39 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 350 | android::String8 name(""); |
| 351 | name.appendFormat("%u", u); |
| 352 | char *n = uidToName(u); |
| 353 | if (n) { |
| 354 | name.appendFormat("%*s%s", (int)std::max(6 - name.length(), (size_t)1), "", n); |
| 355 | free(n); |
Mark Salyzyn | 8e72c53 | 2014-03-26 10:46:39 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 358 | android::String8 size(""); |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 359 | size.appendFormat("%zu", entry->getSizes()); |
Mark Salyzyn | 8e72c53 | 2014-03-26 10:46:39 -0700 | [diff] [blame] | 360 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 361 | android::String8 pruned(""); |
| 362 | size_t dropped = entry->getDropped(); |
| 363 | if (dropped) { |
| 364 | pruned.appendFormat("%zu", dropped); |
| 365 | } |
| 366 | |
| 367 | format_line(output, name, size, pruned); |
Mark Salyzyn | 8e72c53 | 2014-03-26 10:46:39 -0700 | [diff] [blame] | 368 | } |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 369 | } |
Mark Salyzyn | 8e72c53 | 2014-03-26 10:46:39 -0700 | [diff] [blame] | 370 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 371 | if (enable) { |
| 372 | bool headerPrinted = false; |
| 373 | std::unique_ptr<const PidEntry *[]> sorted = pidTable.sort(maximum_sorted_entries); |
| 374 | ssize_t index = -1; |
| 375 | while ((index = pidTable.next(index, sorted, maximum_sorted_entries)) >= 0) { |
| 376 | const PidEntry *entry = sorted[index]; |
| 377 | uid_t u = entry->getUid(); |
| 378 | if ((uid != AID_ROOT) && (u != uid)) { |
| 379 | continue; |
| 380 | } |
| 381 | |
| 382 | if (!headerPrinted) { |
| 383 | if (uid == AID_ROOT) { |
| 384 | output.appendFormat("\n\nChattiest PIDs:\n"); |
| 385 | } else { |
| 386 | output.appendFormat("\n\nLogging for this PID:\n"); |
| 387 | } |
| 388 | android::String8 name(" PID/UID"); |
| 389 | android::String8 size("Size"); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 390 | android::String8 pruned("Pruned"); |
| 391 | format_line(output, name, size, pruned); |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 392 | headerPrinted = true; |
| 393 | } |
| 394 | |
| 395 | android::String8 name(""); |
| 396 | name.appendFormat("%5u/%u", entry->getKey(), u); |
| 397 | const char *n = entry->getName(); |
| 398 | if (n) { |
| 399 | name.appendFormat("%*s%s", (int)std::max(12 - name.length(), (size_t)1), "", n); |
| 400 | } else { |
| 401 | char *un = uidToName(u); |
| 402 | if (un) { |
| 403 | name.appendFormat("%*s%s", (int)std::max(12 - name.length(), (size_t)1), "", un); |
| 404 | free(un); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | android::String8 size(""); |
| 409 | size.appendFormat("%zu", entry->getSizes()); |
| 410 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 411 | android::String8 pruned(""); |
| 412 | size_t dropped = entry->getDropped(); |
| 413 | if (dropped) { |
| 414 | pruned.appendFormat("%zu", dropped); |
| 415 | } |
| 416 | |
| 417 | format_line(output, name, size, pruned); |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 418 | } |
Mark Salyzyn | 8e72c53 | 2014-03-26 10:46:39 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 421 | *buf = strdup(output.string()); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 422 | } |
Mark Salyzyn | 4ba0387 | 2014-04-07 07:15:33 -0700 | [diff] [blame] | 423 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 424 | namespace android { |
| 425 | |
| 426 | uid_t pidToUid(pid_t pid) { |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 427 | char buffer[512]; |
| 428 | snprintf(buffer, sizeof(buffer), "/proc/%u/status", pid); |
| 429 | FILE *fp = fopen(buffer, "r"); |
| 430 | if (fp) { |
| 431 | while (fgets(buffer, sizeof(buffer), fp)) { |
| 432 | int uid; |
| 433 | if (sscanf(buffer, "Groups: %d", &uid) == 1) { |
| 434 | fclose(fp); |
| 435 | return uid; |
Mark Salyzyn | 4ba0387 | 2014-04-07 07:15:33 -0700 | [diff] [blame] | 436 | } |
| 437 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 438 | fclose(fp); |
Mark Salyzyn | 4ba0387 | 2014-04-07 07:15:33 -0700 | [diff] [blame] | 439 | } |
| 440 | return getuid(); // associate this with the logger |
| 441 | } |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 442 | |
| 443 | } |
| 444 | |
| 445 | uid_t LogStatistics::pidToUid(pid_t pid) { |
| 446 | uid_t uid; |
| 447 | android::hash_t hash = android::hash_type(pid); |
| 448 | ssize_t index = pidTable.find(-1, hash, pid); |
| 449 | if (index == -1) { |
| 450 | uid = android::pidToUid(pid); |
| 451 | PidEntry initEntry(pid, uid, android::pidToName(pid)); |
| 452 | pidTable.add(hash, initEntry); |
| 453 | } else { |
| 454 | PidEntry &entry = pidTable.editEntryAt(index); |
| 455 | if (!entry.getName()) { |
| 456 | char *name = android::pidToName(pid); |
| 457 | if (name) { |
| 458 | entry.setName(name); |
| 459 | } |
| 460 | } |
| 461 | uid = entry.getUid(); |
| 462 | } |
| 463 | return uid; |
| 464 | } |
| 465 | |
| 466 | // caller must free character string |
| 467 | char *LogStatistics::pidToName(pid_t pid) { |
| 468 | char *name; |
| 469 | |
| 470 | android::hash_t hash = android::hash_type(pid); |
| 471 | ssize_t index = pidTable.find(-1, hash, pid); |
| 472 | if (index == -1) { |
| 473 | name = android::pidToName(pid); |
| 474 | PidEntry initEntry(pid, android::pidToUid(pid), name ? strdup(name) : NULL); |
| 475 | pidTable.add(hash, initEntry); |
| 476 | } else { |
| 477 | PidEntry &entry = pidTable.editEntryAt(index); |
| 478 | const char *n = entry.getName(); |
| 479 | if (n) { |
| 480 | name = strdup(n); |
| 481 | } else { |
| 482 | name = android::pidToName(pid); |
| 483 | if (name) { |
| 484 | entry.setName(strdup(name)); |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | return name; |
| 490 | } |