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 | |
| 17 | #ifndef _LOGD_LOG_STATISTICS_H__ |
| 18 | #define _LOGD_LOG_STATISTICS_H__ |
| 19 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 20 | #include <memory> |
| 21 | #include <stdlib.h> |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 22 | #include <sys/types.h> |
| 23 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 24 | #include <algorithm> // std::max |
| 25 | #include <string> // std::string |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 26 | #include <unordered_map> |
| 27 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 28 | #include <android-base/stringprintf.h> |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 29 | #include <log/log.h> |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 30 | #include <private/android_filesystem_config.h> |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 31 | |
| 32 | #include "LogBufferElement.h" |
Mark Salyzyn | 5ac5c6b | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 33 | #include "LogUtils.h" |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 34 | |
| 35 | #define log_id_for_each(i) \ |
| 36 | for (log_id_t i = LOG_ID_MIN; i < LOG_ID_MAX; i = (log_id_t) (i + 1)) |
| 37 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 38 | class LogStatistics; |
| 39 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 40 | template <typename TKey, typename TEntry> |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 41 | class LogHashtable { |
| 42 | |
| 43 | std::unordered_map<TKey, TEntry> map; |
| 44 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 45 | public: |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 46 | |
| 47 | typedef typename std::unordered_map<TKey, TEntry>::iterator iterator; |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 48 | typedef typename std::unordered_map<TKey, TEntry>::const_iterator const_iterator; |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 49 | |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 50 | std::unique_ptr<const TEntry *[]> sort(uid_t uid, pid_t pid, |
| 51 | size_t len) const { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 52 | if (!len) { |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 53 | std::unique_ptr<const TEntry *[]> sorted(NULL); |
| 54 | return sorted; |
| 55 | } |
| 56 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 57 | const TEntry **retval = new const TEntry* [len]; |
| 58 | memset(retval, 0, sizeof(*retval) * len); |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 59 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 60 | for(const_iterator it = map.begin(); it != map.end(); ++it) { |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 61 | const TEntry &entry = it->second; |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 62 | |
| 63 | if ((uid != AID_ROOT) && (uid != entry.getUid())) { |
| 64 | continue; |
| 65 | } |
| 66 | if (pid && entry.getPid() && (pid != entry.getPid())) { |
| 67 | continue; |
| 68 | } |
| 69 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 70 | size_t sizes = entry.getSizes(); |
| 71 | ssize_t index = len - 1; |
| 72 | while ((!retval[index] || (sizes > retval[index]->getSizes())) |
| 73 | && (--index >= 0)) |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 74 | ; |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 75 | if (++index < (ssize_t)len) { |
| 76 | size_t num = len - index - 1; |
| 77 | if (num) { |
| 78 | memmove(&retval[index + 1], &retval[index], |
| 79 | num * sizeof(retval[0])); |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 80 | } |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 81 | retval[index] = &entry; |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | std::unique_ptr<const TEntry *[]> sorted(retval); |
| 85 | return sorted; |
| 86 | } |
| 87 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 88 | inline iterator add(TKey key, LogBufferElement *element) { |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 89 | iterator it = map.find(key); |
| 90 | if (it == map.end()) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 91 | it = map.insert(std::make_pair(key, TEntry(element))).first; |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 92 | } else { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 93 | it->second.add(element); |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 94 | } |
| 95 | return it; |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 96 | } |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 97 | |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 98 | inline iterator add(TKey key) { |
| 99 | iterator it = map.find(key); |
| 100 | if (it == map.end()) { |
| 101 | it = map.insert(std::make_pair(key, TEntry(key))).first; |
| 102 | } else { |
| 103 | it->second.add(key); |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 104 | } |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 105 | return it; |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 108 | void subtract(TKey key, LogBufferElement *element) { |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 109 | iterator it = map.find(key); |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 110 | if ((it != map.end()) && it->second.subtract(element)) { |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 111 | map.erase(it); |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 115 | inline void drop(TKey key, LogBufferElement *element) { |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 116 | iterator it = map.find(key); |
| 117 | if (it != map.end()) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 118 | it->second.drop(element); |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 122 | inline iterator begin() { return map.begin(); } |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 123 | inline const_iterator begin() const { return map.begin(); } |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 124 | inline iterator end() { return map.end(); } |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 125 | inline const_iterator end() const { return map.end(); } |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 126 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 127 | std::string format( |
| 128 | const LogStatistics &stat, |
| 129 | uid_t uid, |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 130 | pid_t pid, |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 131 | const std::string &name = std::string(""), |
| 132 | log_id_t id = LOG_ID_MAX) const { |
| 133 | static const size_t maximum_sorted_entries = 32; |
| 134 | std::string output; |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 135 | std::unique_ptr<const TEntry *[]> sorted = sort(uid, pid, |
| 136 | maximum_sorted_entries); |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 137 | if (!sorted.get()) { |
| 138 | return output; |
| 139 | } |
| 140 | bool headerPrinted = false; |
| 141 | for (size_t index = 0; index < maximum_sorted_entries; ++index) { |
| 142 | const TEntry *entry = sorted[index]; |
| 143 | if (!entry) { |
| 144 | break; |
| 145 | } |
| 146 | if (entry->getSizes() <= (sorted[0]->getSizes() / 100)) { |
| 147 | break; |
| 148 | } |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 149 | if (!headerPrinted) { |
| 150 | output += "\n\n"; |
| 151 | output += entry->formatHeader(name, id); |
| 152 | headerPrinted = true; |
| 153 | } |
| 154 | output += entry->format(stat, id); |
| 155 | } |
| 156 | return output; |
| 157 | } |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 158 | }; |
| 159 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 160 | namespace EntryBaseConstants { |
| 161 | static constexpr size_t pruned_len = 14; |
| 162 | static constexpr size_t total_len = 80; |
| 163 | } |
| 164 | |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 165 | struct EntryBase { |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 166 | size_t size; |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 167 | |
| 168 | EntryBase():size(0) { } |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 169 | EntryBase(LogBufferElement *element):size(element->getMsgLen()) { } |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 170 | |
| 171 | size_t getSizes() const { return size; } |
| 172 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 173 | inline void add(LogBufferElement *element) { size += element->getMsgLen(); } |
| 174 | inline bool subtract(LogBufferElement *element) { |
| 175 | size -= element->getMsgLen(); |
| 176 | return !size; |
| 177 | } |
| 178 | |
| 179 | static std::string formatLine( |
| 180 | const std::string &name, |
| 181 | const std::string &size, |
| 182 | const std::string &pruned) { |
| 183 | ssize_t drop_len = std::max(pruned.length() + 1, |
| 184 | EntryBaseConstants::pruned_len); |
| 185 | ssize_t size_len = std::max(size.length() + 1, |
| 186 | EntryBaseConstants::total_len |
| 187 | - name.length() - drop_len - 1); |
| 188 | |
| 189 | if (pruned.length()) { |
| 190 | return android::base::StringPrintf("%s%*s%*s\n", name.c_str(), |
| 191 | (int)size_len, size.c_str(), |
| 192 | (int)drop_len, pruned.c_str()); |
| 193 | } else { |
| 194 | return android::base::StringPrintf("%s%*s\n", name.c_str(), |
| 195 | (int)size_len, size.c_str()); |
| 196 | } |
| 197 | } |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | struct EntryBaseDropped : public EntryBase { |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 201 | size_t dropped; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 202 | |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 203 | EntryBaseDropped():dropped(0) { } |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 204 | EntryBaseDropped(LogBufferElement *element): |
| 205 | EntryBase(element), |
| 206 | dropped(element->getDropped()){ |
| 207 | } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 208 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 209 | size_t getDropped() const { return dropped; } |
| 210 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 211 | inline void add(LogBufferElement *element) { |
| 212 | dropped += element->getDropped(); |
| 213 | EntryBase::add(element); |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 214 | } |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 215 | inline bool subtract(LogBufferElement *element) { |
| 216 | dropped -= element->getDropped(); |
| 217 | return EntryBase::subtract(element) && !dropped; |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 218 | } |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 219 | inline void drop(LogBufferElement *element) { |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 220 | dropped += 1; |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 221 | EntryBase::subtract(element); |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 222 | } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 223 | }; |
| 224 | |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 225 | struct UidEntry : public EntryBaseDropped { |
| 226 | const uid_t uid; |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 227 | pid_t pid; |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 228 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 229 | UidEntry(LogBufferElement *element): |
| 230 | EntryBaseDropped(element), |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 231 | uid(element->getUid()), |
| 232 | pid(element->getPid()) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 233 | } |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 234 | |
| 235 | inline const uid_t&getKey() const { return uid; } |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 236 | inline const uid_t&getUid() const { return getKey(); } |
| 237 | inline const pid_t&getPid() const { return pid; } |
| 238 | |
| 239 | inline void add(LogBufferElement *element) { |
| 240 | if (pid != element->getPid()) { |
| 241 | pid = -1; |
| 242 | } |
| 243 | EntryBase::add(element); |
| 244 | } |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 245 | |
| 246 | std::string formatHeader(const std::string &name, log_id_t id) const; |
| 247 | std::string format(const LogStatistics &stat, log_id_t id) const; |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | namespace android { |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 251 | uid_t pidToUid(pid_t pid); |
| 252 | } |
| 253 | |
| 254 | struct PidEntry : public EntryBaseDropped { |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 255 | const pid_t pid; |
| 256 | uid_t uid; |
| 257 | char *name; |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 258 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 259 | PidEntry(pid_t pid): |
| 260 | EntryBaseDropped(), |
| 261 | pid(pid), |
| 262 | uid(android::pidToUid(pid)), |
| 263 | name(android::pidToName(pid)) { |
| 264 | } |
| 265 | PidEntry(LogBufferElement *element): |
| 266 | EntryBaseDropped(element), |
| 267 | pid(element->getPid()), |
| 268 | uid(element->getUid()), |
| 269 | name(android::pidToName(pid)) { |
| 270 | } |
| 271 | PidEntry(const PidEntry &element): |
| 272 | EntryBaseDropped(element), |
| 273 | pid(element.pid), |
| 274 | uid(element.uid), |
| 275 | name(element.name ? strdup(element.name) : NULL) { |
| 276 | } |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 277 | ~PidEntry() { free(name); } |
| 278 | |
| 279 | const pid_t&getKey() const { return pid; } |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 280 | const pid_t&getPid() const { return getKey(); } |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 281 | const uid_t&getUid() const { return uid; } |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 282 | const char*getName() const { return name; } |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 283 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 284 | inline void add(pid_t newPid) { |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 285 | if (name && !fast<strncmp>(name, "zygote", 6)) { |
Mark Salyzyn | aa43ae2 | 2015-04-20 10:27:38 -0700 | [diff] [blame] | 286 | free(name); |
| 287 | name = NULL; |
| 288 | } |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 289 | if (!name) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 290 | name = android::pidToName(newPid); |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 294 | inline void add(LogBufferElement *element) { |
| 295 | uid_t incomingUid = element->getUid(); |
| 296 | if (getUid() != incomingUid) { |
| 297 | uid = incomingUid; |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 298 | free(name); |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 299 | name = android::pidToName(element->getPid()); |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 300 | } else { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 301 | add(element->getPid()); |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 302 | } |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 303 | EntryBaseDropped::add(element); |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 304 | } |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 305 | |
| 306 | std::string formatHeader(const std::string &name, log_id_t id) const; |
| 307 | std::string format(const LogStatistics &stat, log_id_t id) const; |
Mark Salyzyn | 344bff4 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 308 | }; |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 309 | |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 310 | struct TidEntry : public EntryBaseDropped { |
| 311 | const pid_t tid; |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 312 | pid_t pid; |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 313 | uid_t uid; |
| 314 | char *name; |
| 315 | |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 316 | TidEntry(pid_t tid, pid_t pid): |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 317 | EntryBaseDropped(), |
| 318 | tid(tid), |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 319 | pid(pid), |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 320 | uid(android::pidToUid(tid)), |
| 321 | name(android::tidToName(tid)) { |
| 322 | } |
| 323 | TidEntry(LogBufferElement *element): |
| 324 | EntryBaseDropped(element), |
| 325 | tid(element->getTid()), |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 326 | pid(element->getPid()), |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 327 | uid(element->getUid()), |
| 328 | name(android::tidToName(tid)) { |
| 329 | } |
| 330 | TidEntry(const TidEntry &element): |
| 331 | EntryBaseDropped(element), |
| 332 | tid(element.tid), |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 333 | pid(element.pid), |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 334 | uid(element.uid), |
| 335 | name(element.name ? strdup(element.name) : NULL) { |
| 336 | } |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 337 | ~TidEntry() { free(name); } |
| 338 | |
| 339 | const pid_t&getKey() const { return tid; } |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 340 | const pid_t&getTid() const { return getKey(); } |
| 341 | const pid_t&getPid() const { return pid; } |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 342 | const uid_t&getUid() const { return uid; } |
| 343 | const char*getName() const { return name; } |
| 344 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 345 | inline void add(pid_t incomingTid) { |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 346 | if (name && !fast<strncmp>(name, "zygote", 6)) { |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 347 | free(name); |
| 348 | name = NULL; |
| 349 | } |
| 350 | if (!name) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 351 | name = android::tidToName(incomingTid); |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 355 | inline void add(LogBufferElement *element) { |
| 356 | uid_t incomingUid = element->getUid(); |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 357 | pid_t incomingPid = element->getPid(); |
| 358 | if ((getUid() != incomingUid) || (getPid() != incomingPid)) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 359 | uid = incomingUid; |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 360 | pid = incomingPid; |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 361 | free(name); |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 362 | name = android::tidToName(element->getTid()); |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 363 | } else { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 364 | add(element->getTid()); |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 365 | } |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 366 | EntryBaseDropped::add(element); |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 367 | } |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 368 | |
| 369 | std::string formatHeader(const std::string &name, log_id_t id) const; |
| 370 | std::string format(const LogStatistics &stat, log_id_t id) const; |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 371 | }; |
| 372 | |
Mark Salyzyn | 344bff4 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 373 | struct TagEntry : public EntryBase { |
| 374 | const uint32_t tag; |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 375 | pid_t pid; |
Mark Salyzyn | 344bff4 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 376 | uid_t uid; |
| 377 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 378 | TagEntry(LogBufferElement *element): |
| 379 | EntryBase(element), |
| 380 | tag(element->getTag()), |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 381 | pid(element->getPid()), |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 382 | uid(element->getUid()) { |
| 383 | } |
Mark Salyzyn | 344bff4 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 384 | |
| 385 | const uint32_t&getKey() const { return tag; } |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 386 | const pid_t&getPid() const { return pid; } |
Mark Salyzyn | 344bff4 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 387 | const uid_t&getUid() const { return uid; } |
| 388 | const char*getName() const { return android::tagToName(tag); } |
| 389 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 390 | inline void add(LogBufferElement *element) { |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 391 | if (uid != element->getUid()) { |
Mark Salyzyn | 344bff4 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 392 | uid = -1; |
| 393 | } |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 394 | if (pid != element->getPid()) { |
| 395 | pid = -1; |
| 396 | } |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 397 | EntryBase::add(element); |
Mark Salyzyn | 344bff4 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 398 | } |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 399 | |
| 400 | std::string formatHeader(const std::string &name, log_id_t id) const; |
| 401 | std::string format(const LogStatistics &stat, log_id_t id) const; |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 402 | }; |
| 403 | |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 404 | // Log Statistics |
| 405 | class LogStatistics { |
Mark Salyzyn | c723df8 | 2015-08-24 11:08:00 -0700 | [diff] [blame] | 406 | friend UidEntry; |
| 407 | |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 408 | size_t mSizes[LOG_ID_MAX]; |
| 409 | size_t mElements[LOG_ID_MAX]; |
Mark Salyzyn | 58b8be8 | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 410 | size_t mDroppedElements[LOG_ID_MAX]; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 411 | size_t mSizesTotal[LOG_ID_MAX]; |
| 412 | size_t mElementsTotal[LOG_ID_MAX]; |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 413 | bool enable; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 414 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 415 | // uid to size list |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 416 | typedef LogHashtable<uid_t, UidEntry> uidTable_t; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 417 | uidTable_t uidTable[LOG_ID_MAX]; |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 418 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 419 | // pid of system to size list |
| 420 | typedef LogHashtable<pid_t, PidEntry> pidSystemTable_t; |
| 421 | pidSystemTable_t pidSystemTable[LOG_ID_MAX]; |
| 422 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 423 | // pid to uid list |
| 424 | typedef LogHashtable<pid_t, PidEntry> pidTable_t; |
| 425 | pidTable_t pidTable; |
| 426 | |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 427 | // tid to uid list |
| 428 | typedef LogHashtable<pid_t, TidEntry> tidTable_t; |
| 429 | tidTable_t tidTable; |
| 430 | |
Mark Salyzyn | 344bff4 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 431 | // tag list |
| 432 | typedef LogHashtable<uint32_t, TagEntry> tagTable_t; |
| 433 | tagTable_t tagTable; |
| 434 | |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 435 | // security tag list |
| 436 | tagTable_t securityTagTable; |
| 437 | |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 438 | public: |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 439 | LogStatistics(); |
| 440 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 441 | void enableStatistics() { enable = true; } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 442 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 443 | void add(LogBufferElement *entry); |
| 444 | void subtract(LogBufferElement *entry); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 445 | // entry->setDropped(1) must follow this call |
| 446 | void drop(LogBufferElement *entry); |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 447 | // Correct for coalescing two entries referencing dropped content |
Mark Salyzyn | 58b8be8 | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 448 | void erase(LogBufferElement *element) { |
| 449 | log_id_t log_id = element->getLogId(); |
| 450 | --mElements[log_id]; |
| 451 | --mDroppedElements[log_id]; |
| 452 | } |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 453 | |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 454 | std::unique_ptr<const UidEntry *[]> sort(uid_t uid, pid_t pid, |
| 455 | size_t len, log_id id) { |
| 456 | return uidTable[id].sort(uid, pid, len); |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 457 | } |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 458 | std::unique_ptr<const PidEntry *[]> sort(uid_t uid, pid_t pid, |
| 459 | size_t len, log_id id, uid_t) { |
| 460 | return pidSystemTable[id].sort(uid, pid, len); |
| 461 | } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 462 | |
| 463 | // fast track current value by id only |
| 464 | size_t sizes(log_id_t id) const { return mSizes[id]; } |
| 465 | size_t elements(log_id_t id) const { return mElements[id]; } |
Mark Salyzyn | 58b8be8 | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 466 | size_t realElements(log_id_t id) const { |
| 467 | return mElements[id] - mDroppedElements[id]; |
| 468 | } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 469 | size_t sizesTotal(log_id_t id) const { return mSizesTotal[id]; } |
| 470 | size_t elementsTotal(log_id_t id) const { return mElementsTotal[id]; } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 471 | |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 472 | std::string format(uid_t uid, pid_t pid, unsigned int logMask) const; |
Mark Salyzyn | 9a03863 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 473 | |
Mark Salyzyn | ed777e9 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 474 | // helper (must be locked directly or implicitly by mLogElementsLock) |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 475 | const char *pidToName(pid_t pid) const; |
Mark Salyzyn | 4ba0387 | 2014-04-07 07:15:33 -0700 | [diff] [blame] | 476 | uid_t pidToUid(pid_t pid); |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 477 | const char *uidToName(uid_t uid) const; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 478 | }; |
| 479 | |
| 480 | #endif // _LOGD_LOG_STATISTICS_H__ |