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 | b545e1c | 2016-12-19 14:51:15 -0800 | [diff] [blame] | 20 | #include <ctype.h> |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 21 | #include <stdlib.h> |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 22 | #include <sys/types.h> |
| 23 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 24 | #include <algorithm> // std::max |
Mark Salyzyn | b545e1c | 2016-12-19 14:51:15 -0800 | [diff] [blame] | 25 | #include <memory> |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 26 | #include <string> // std::string |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 27 | #include <unordered_map> |
| 28 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 29 | #include <android-base/stringprintf.h> |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 30 | #include <android/log.h> |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 31 | #include <private/android_filesystem_config.h> |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 32 | |
| 33 | #include "LogBufferElement.h" |
Mark Salyzyn | 5ac5c6b | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 34 | #include "LogUtils.h" |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 35 | |
| 36 | #define log_id_for_each(i) \ |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 37 | for (log_id_t i = LOG_ID_MIN; (i) < LOG_ID_MAX; (i) = (log_id_t)((i) + 1)) |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 38 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 39 | class LogStatistics; |
| 40 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 41 | template <typename TKey, typename TEntry> |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 42 | class LogHashtable { |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 43 | std::unordered_map<TKey, TEntry> map; |
| 44 | |
Mark Salyzyn | 6d981af | 2016-10-06 09:55:21 -0700 | [diff] [blame] | 45 | size_t bucket_size() const { |
| 46 | size_t count = 0; |
| 47 | for (size_t idx = 0; idx < map.bucket_count(); ++idx) { |
| 48 | size_t bucket_size = map.bucket_size(idx); |
| 49 | if (bucket_size == 0) bucket_size = 1; |
| 50 | count += bucket_size; |
| 51 | } |
| 52 | float load_factor = map.max_load_factor(); |
| 53 | if (load_factor < 1.0) return count; |
| 54 | return count * load_factor; |
| 55 | } |
| 56 | |
| 57 | static const size_t unordered_map_per_entry_overhead = sizeof(void*); |
| 58 | static const size_t unordered_map_bucket_overhead = sizeof(void*); |
| 59 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 60 | public: |
| 61 | size_t size() const { |
| 62 | return map.size(); |
| 63 | } |
Mark Salyzyn | b067229 | 2016-10-06 10:09:24 -0700 | [diff] [blame] | 64 | |
Mark Salyzyn | 6d981af | 2016-10-06 09:55:21 -0700 | [diff] [blame] | 65 | // Estimate unordered_map memory usage. |
| 66 | size_t sizeOf() const { |
| 67 | return sizeof(*this) + |
Mark Salyzyn | b067229 | 2016-10-06 10:09:24 -0700 | [diff] [blame] | 68 | (size() * (sizeof(TEntry) + unordered_map_per_entry_overhead)) + |
Mark Salyzyn | 6d981af | 2016-10-06 09:55:21 -0700 | [diff] [blame] | 69 | (bucket_size() * sizeof(size_t) + unordered_map_bucket_overhead); |
| 70 | } |
| 71 | |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 72 | typedef typename std::unordered_map<TKey, TEntry>::iterator iterator; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 73 | typedef |
| 74 | typename std::unordered_map<TKey, TEntry>::const_iterator const_iterator; |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 75 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 76 | std::unique_ptr<const TEntry* []> sort(uid_t uid, pid_t pid, |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 77 | size_t len) const { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 78 | if (!len) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 79 | std::unique_ptr<const TEntry* []> sorted(NULL); |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 80 | return sorted; |
| 81 | } |
| 82 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 83 | const TEntry** retval = new const TEntry*[len]; |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 84 | memset(retval, 0, sizeof(*retval) * len); |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 85 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 86 | for (const_iterator it = map.begin(); it != map.end(); ++it) { |
| 87 | const TEntry& entry = it->second; |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 88 | |
| 89 | if ((uid != AID_ROOT) && (uid != entry.getUid())) { |
| 90 | continue; |
| 91 | } |
| 92 | if (pid && entry.getPid() && (pid != entry.getPid())) { |
| 93 | continue; |
| 94 | } |
| 95 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 96 | size_t sizes = entry.getSizes(); |
| 97 | ssize_t index = len - 1; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 98 | while ((!retval[index] || (sizes > retval[index]->getSizes())) && |
| 99 | (--index >= 0)) |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 100 | ; |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 101 | if (++index < (ssize_t)len) { |
| 102 | size_t num = len - index - 1; |
| 103 | if (num) { |
| 104 | memmove(&retval[index + 1], &retval[index], |
| 105 | num * sizeof(retval[0])); |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 106 | } |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 107 | retval[index] = &entry; |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 110 | std::unique_ptr<const TEntry* []> sorted(retval); |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 111 | return sorted; |
| 112 | } |
| 113 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 114 | inline iterator add(TKey key, LogBufferElement* element) { |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 115 | iterator it = map.find(key); |
| 116 | if (it == map.end()) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 117 | it = map.insert(std::make_pair(key, TEntry(element))).first; |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 118 | } else { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 119 | it->second.add(element); |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 120 | } |
| 121 | return it; |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 122 | } |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 123 | |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 124 | inline iterator add(TKey key) { |
| 125 | iterator it = map.find(key); |
| 126 | if (it == map.end()) { |
| 127 | it = map.insert(std::make_pair(key, TEntry(key))).first; |
| 128 | } else { |
| 129 | it->second.add(key); |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 130 | } |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 131 | return it; |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 134 | void subtract(TKey key, LogBufferElement* element) { |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 135 | iterator it = map.find(key); |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 136 | if ((it != map.end()) && it->second.subtract(element)) { |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 137 | map.erase(it); |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 141 | inline void drop(TKey key, LogBufferElement* element) { |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 142 | iterator it = map.find(key); |
| 143 | if (it != map.end()) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 144 | it->second.drop(element); |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 148 | inline iterator begin() { |
| 149 | return map.begin(); |
| 150 | } |
| 151 | inline const_iterator begin() const { |
| 152 | return map.begin(); |
| 153 | } |
| 154 | inline iterator end() { |
| 155 | return map.end(); |
| 156 | } |
| 157 | inline const_iterator end() const { |
| 158 | return map.end(); |
| 159 | } |
Mark Salyzyn | 511338d | 2015-05-19 09:12:30 -0700 | [diff] [blame] | 160 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 161 | std::string format(const LogStatistics& stat, uid_t uid, pid_t pid, |
| 162 | const std::string& name = std::string(""), |
| 163 | log_id_t id = LOG_ID_MAX) const { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 164 | static const size_t maximum_sorted_entries = 32; |
| 165 | std::string output; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 166 | std::unique_ptr<const TEntry* []> sorted = |
| 167 | sort(uid, pid, maximum_sorted_entries); |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 168 | if (!sorted.get()) { |
| 169 | return output; |
| 170 | } |
| 171 | bool headerPrinted = false; |
| 172 | for (size_t index = 0; index < maximum_sorted_entries; ++index) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 173 | const TEntry* entry = sorted[index]; |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 174 | if (!entry) { |
| 175 | break; |
| 176 | } |
| 177 | if (entry->getSizes() <= (sorted[0]->getSizes() / 100)) { |
| 178 | break; |
| 179 | } |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 180 | if (!headerPrinted) { |
| 181 | output += "\n\n"; |
| 182 | output += entry->formatHeader(name, id); |
| 183 | headerPrinted = true; |
| 184 | } |
| 185 | output += entry->format(stat, id); |
| 186 | } |
| 187 | return output; |
| 188 | } |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 189 | }; |
| 190 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 191 | namespace EntryBaseConstants { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 192 | static constexpr size_t pruned_len = 14; |
| 193 | static constexpr size_t total_len = 80; |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 196 | struct EntryBase { |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 197 | size_t size; |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 198 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 199 | EntryBase() : size(0) { |
| 200 | } |
| 201 | explicit EntryBase(LogBufferElement* element) : size(element->getMsgLen()) { |
| 202 | } |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 203 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 204 | size_t getSizes() const { |
| 205 | return size; |
| 206 | } |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 207 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 208 | inline void add(LogBufferElement* element) { |
| 209 | size += element->getMsgLen(); |
| 210 | } |
| 211 | inline bool subtract(LogBufferElement* element) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 212 | size -= element->getMsgLen(); |
| 213 | return !size; |
| 214 | } |
| 215 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 216 | static std::string formatLine(const std::string& name, |
| 217 | const std::string& size, |
| 218 | const std::string& pruned) { |
| 219 | ssize_t drop_len = |
| 220 | std::max(pruned.length() + 1, EntryBaseConstants::pruned_len); |
| 221 | ssize_t size_len = |
| 222 | std::max(size.length() + 1, EntryBaseConstants::total_len - |
| 223 | name.length() - drop_len - 1); |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 224 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 225 | std::string ret = android::base::StringPrintf( |
| 226 | "%s%*s%*s", name.c_str(), (int)size_len, size.c_str(), |
| 227 | (int)drop_len, pruned.c_str()); |
Mark Salyzyn | b545e1c | 2016-12-19 14:51:15 -0800 | [diff] [blame] | 228 | // remove any trailing spaces |
| 229 | size_t pos = ret.size(); |
| 230 | size_t len = 0; |
| 231 | while (pos && isspace(ret[--pos])) ++len; |
| 232 | if (len) ret.erase(pos + 1, len); |
| 233 | return ret + "\n"; |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 234 | } |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | struct EntryBaseDropped : public EntryBase { |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 238 | size_t dropped; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 239 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 240 | EntryBaseDropped() : dropped(0) { |
| 241 | } |
| 242 | explicit EntryBaseDropped(LogBufferElement* element) |
| 243 | : EntryBase(element), dropped(element->getDropped()) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 244 | } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 245 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 246 | size_t getDropped() const { |
| 247 | return dropped; |
| 248 | } |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 249 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 250 | inline void add(LogBufferElement* element) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 251 | dropped += element->getDropped(); |
| 252 | EntryBase::add(element); |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 253 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 254 | inline bool subtract(LogBufferElement* element) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 255 | dropped -= element->getDropped(); |
| 256 | return EntryBase::subtract(element) && !dropped; |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 257 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 258 | inline void drop(LogBufferElement* element) { |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 259 | dropped += 1; |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 260 | EntryBase::subtract(element); |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 261 | } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 262 | }; |
| 263 | |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 264 | struct UidEntry : public EntryBaseDropped { |
| 265 | const uid_t uid; |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 266 | pid_t pid; |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 267 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 268 | explicit UidEntry(LogBufferElement* element) |
| 269 | : EntryBaseDropped(element), |
| 270 | uid(element->getUid()), |
| 271 | pid(element->getPid()) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 272 | } |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 273 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 274 | inline const uid_t& getKey() const { |
| 275 | return uid; |
| 276 | } |
| 277 | inline const uid_t& getUid() const { |
| 278 | return getKey(); |
| 279 | } |
| 280 | inline const pid_t& getPid() const { |
| 281 | return pid; |
| 282 | } |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 283 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 284 | inline void add(LogBufferElement* element) { |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 285 | if (pid != element->getPid()) { |
| 286 | pid = -1; |
| 287 | } |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 288 | EntryBaseDropped::add(element); |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 289 | } |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 290 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 291 | std::string formatHeader(const std::string& name, log_id_t id) const; |
| 292 | std::string format(const LogStatistics& stat, log_id_t id) const; |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 293 | }; |
| 294 | |
| 295 | namespace android { |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 296 | uid_t pidToUid(pid_t pid); |
| 297 | } |
| 298 | |
| 299 | struct PidEntry : public EntryBaseDropped { |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 300 | const pid_t pid; |
| 301 | uid_t uid; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 302 | char* name; |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 303 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 304 | explicit PidEntry(pid_t pid) |
| 305 | : EntryBaseDropped(), |
| 306 | pid(pid), |
| 307 | uid(android::pidToUid(pid)), |
| 308 | name(android::pidToName(pid)) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 309 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 310 | explicit PidEntry(LogBufferElement* element) |
| 311 | : EntryBaseDropped(element), |
| 312 | pid(element->getPid()), |
| 313 | uid(element->getUid()), |
| 314 | name(android::pidToName(pid)) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 315 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 316 | PidEntry(const PidEntry& element) |
| 317 | : EntryBaseDropped(element), |
| 318 | pid(element.pid), |
| 319 | uid(element.uid), |
| 320 | name(element.name ? strdup(element.name) : NULL) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 321 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 322 | ~PidEntry() { |
| 323 | free(name); |
| 324 | } |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 325 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 326 | const pid_t& getKey() const { |
| 327 | return pid; |
| 328 | } |
| 329 | const pid_t& getPid() const { |
| 330 | return getKey(); |
| 331 | } |
| 332 | const uid_t& getUid() const { |
| 333 | return uid; |
| 334 | } |
| 335 | const char* getName() const { |
| 336 | return name; |
| 337 | } |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 338 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 339 | inline void add(pid_t newPid) { |
Mark Salyzyn | 0eeb06b | 2016-12-02 10:08:48 -0800 | [diff] [blame] | 340 | if (name && !fastcmp<strncmp>(name, "zygote", 6)) { |
Mark Salyzyn | aa43ae2 | 2015-04-20 10:27:38 -0700 | [diff] [blame] | 341 | free(name); |
| 342 | name = NULL; |
| 343 | } |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 344 | if (!name) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 345 | name = android::pidToName(newPid); |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 349 | inline void add(LogBufferElement* element) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 350 | uid_t incomingUid = element->getUid(); |
| 351 | if (getUid() != incomingUid) { |
| 352 | uid = incomingUid; |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 353 | free(name); |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 354 | name = android::pidToName(element->getPid()); |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 355 | } else { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 356 | add(element->getPid()); |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 357 | } |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 358 | EntryBaseDropped::add(element); |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 359 | } |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 360 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 361 | std::string formatHeader(const std::string& name, log_id_t id) const; |
| 362 | std::string format(const LogStatistics& stat, log_id_t id) const; |
Mark Salyzyn | 344bff4 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 363 | }; |
Mark Salyzyn | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 364 | |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 365 | struct TidEntry : public EntryBaseDropped { |
| 366 | const pid_t tid; |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 367 | pid_t pid; |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 368 | uid_t uid; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 369 | char* name; |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 370 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 371 | TidEntry(pid_t tid, pid_t pid) |
| 372 | : EntryBaseDropped(), |
| 373 | tid(tid), |
| 374 | pid(pid), |
| 375 | uid(android::pidToUid(tid)), |
| 376 | name(android::tidToName(tid)) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 377 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 378 | explicit TidEntry(LogBufferElement* element) |
| 379 | : EntryBaseDropped(element), |
| 380 | tid(element->getTid()), |
| 381 | pid(element->getPid()), |
| 382 | uid(element->getUid()), |
| 383 | name(android::tidToName(tid)) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 384 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 385 | TidEntry(const TidEntry& element) |
| 386 | : EntryBaseDropped(element), |
| 387 | tid(element.tid), |
| 388 | pid(element.pid), |
| 389 | uid(element.uid), |
| 390 | name(element.name ? strdup(element.name) : NULL) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 391 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 392 | ~TidEntry() { |
| 393 | free(name); |
| 394 | } |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 395 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 396 | const pid_t& getKey() const { |
| 397 | return tid; |
| 398 | } |
| 399 | const pid_t& getTid() const { |
| 400 | return getKey(); |
| 401 | } |
| 402 | const pid_t& getPid() const { |
| 403 | return pid; |
| 404 | } |
| 405 | const uid_t& getUid() const { |
| 406 | return uid; |
| 407 | } |
| 408 | const char* getName() const { |
| 409 | return name; |
| 410 | } |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 411 | |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 412 | inline void add(pid_t incomingTid) { |
Mark Salyzyn | 0eeb06b | 2016-12-02 10:08:48 -0800 | [diff] [blame] | 413 | if (name && !fastcmp<strncmp>(name, "zygote", 6)) { |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 414 | free(name); |
| 415 | name = NULL; |
| 416 | } |
| 417 | if (!name) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 418 | name = android::tidToName(incomingTid); |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 422 | inline void add(LogBufferElement* element) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 423 | uid_t incomingUid = element->getUid(); |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 424 | pid_t incomingPid = element->getPid(); |
| 425 | if ((getUid() != incomingUid) || (getPid() != incomingPid)) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 426 | uid = incomingUid; |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 427 | pid = incomingPid; |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 428 | free(name); |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 429 | name = android::tidToName(element->getTid()); |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 430 | } else { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 431 | add(element->getTid()); |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 432 | } |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 433 | EntryBaseDropped::add(element); |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 434 | } |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 435 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 436 | std::string formatHeader(const std::string& name, log_id_t id) const; |
| 437 | std::string format(const LogStatistics& stat, log_id_t id) const; |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 438 | }; |
| 439 | |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 440 | struct TagEntry : public EntryBaseDropped { |
Mark Salyzyn | 344bff4 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 441 | const uint32_t tag; |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 442 | pid_t pid; |
Mark Salyzyn | 344bff4 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 443 | uid_t uid; |
| 444 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 445 | explicit TagEntry(LogBufferElement* element) |
| 446 | : EntryBaseDropped(element), |
| 447 | tag(element->getTag()), |
| 448 | pid(element->getPid()), |
| 449 | uid(element->getUid()) { |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 450 | } |
Mark Salyzyn | 344bff4 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 451 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 452 | const uint32_t& getKey() const { |
| 453 | return tag; |
| 454 | } |
| 455 | const pid_t& getPid() const { |
| 456 | return pid; |
| 457 | } |
| 458 | const uid_t& getUid() const { |
| 459 | return uid; |
| 460 | } |
| 461 | const char* getName() const { |
| 462 | return android::tagToName(tag); |
| 463 | } |
Mark Salyzyn | 344bff4 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 464 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 465 | inline void add(LogBufferElement* element) { |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 466 | if (uid != element->getUid()) { |
Mark Salyzyn | 344bff4 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 467 | uid = -1; |
| 468 | } |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 469 | if (pid != element->getPid()) { |
| 470 | pid = -1; |
| 471 | } |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 472 | EntryBaseDropped::add(element); |
Mark Salyzyn | 344bff4 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 473 | } |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 474 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 475 | std::string formatHeader(const std::string& name, log_id_t id) const; |
| 476 | std::string format(const LogStatistics& stat, log_id_t id) const; |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 477 | }; |
| 478 | |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 479 | template <typename TEntry> |
| 480 | class LogFindWorst { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 481 | std::unique_ptr<const TEntry* []> sorted; |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 482 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 483 | public: |
| 484 | explicit LogFindWorst(std::unique_ptr<const TEntry* []>&& sorted) |
| 485 | : sorted(std::move(sorted)) { |
| 486 | } |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 487 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 488 | void findWorst(int& worst, size_t& worst_sizes, size_t& second_worst_sizes, |
| 489 | size_t threshold) { |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 490 | if (sorted.get() && sorted[0] && sorted[1]) { |
| 491 | worst_sizes = sorted[0]->getSizes(); |
| 492 | if ((worst_sizes > threshold) |
| 493 | // Allow time horizon to extend roughly tenfold, assume |
| 494 | // average entry length is 100 characters. |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 495 | && (worst_sizes > (10 * sorted[0]->getDropped()))) { |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 496 | worst = sorted[0]->getKey(); |
| 497 | second_worst_sizes = sorted[1]->getSizes(); |
| 498 | if (second_worst_sizes < threshold) { |
| 499 | second_worst_sizes = threshold; |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 505 | void findWorst(int& worst, size_t worst_sizes, size_t& second_worst_sizes) { |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 506 | if (sorted.get() && sorted[0] && sorted[1]) { |
| 507 | worst = sorted[0]->getKey(); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 508 | second_worst_sizes = |
| 509 | worst_sizes - sorted[0]->getSizes() + sorted[1]->getSizes(); |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | }; |
| 513 | |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 514 | // Log Statistics |
| 515 | class LogStatistics { |
Mark Salyzyn | c723df8 | 2015-08-24 11:08:00 -0700 | [diff] [blame] | 516 | friend UidEntry; |
| 517 | |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 518 | size_t mSizes[LOG_ID_MAX]; |
| 519 | size_t mElements[LOG_ID_MAX]; |
Mark Salyzyn | 58b8be8 | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 520 | size_t mDroppedElements[LOG_ID_MAX]; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 521 | size_t mSizesTotal[LOG_ID_MAX]; |
| 522 | size_t mElementsTotal[LOG_ID_MAX]; |
Mark Salyzyn | 3296291 | 2016-09-12 10:29:17 -0700 | [diff] [blame] | 523 | static size_t SizesTotal; |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 524 | bool enable; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 525 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 526 | // uid to size list |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 527 | typedef LogHashtable<uid_t, UidEntry> uidTable_t; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 528 | uidTable_t uidTable[LOG_ID_MAX]; |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 529 | |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 530 | // pid of system to size list |
| 531 | typedef LogHashtable<pid_t, PidEntry> pidSystemTable_t; |
| 532 | pidSystemTable_t pidSystemTable[LOG_ID_MAX]; |
| 533 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 534 | // pid to uid list |
| 535 | typedef LogHashtable<pid_t, PidEntry> pidTable_t; |
| 536 | pidTable_t pidTable; |
| 537 | |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 538 | // tid to uid list |
| 539 | typedef LogHashtable<pid_t, TidEntry> tidTable_t; |
| 540 | tidTable_t tidTable; |
| 541 | |
Mark Salyzyn | 344bff4 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 542 | // tag list |
| 543 | typedef LogHashtable<uint32_t, TagEntry> tagTable_t; |
| 544 | tagTable_t tagTable; |
| 545 | |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 546 | // security tag list |
| 547 | tagTable_t securityTagTable; |
| 548 | |
Mark Salyzyn | 6d981af | 2016-10-06 09:55:21 -0700 | [diff] [blame] | 549 | size_t sizeOf() const { |
| 550 | size_t size = sizeof(*this) + pidTable.sizeOf() + tidTable.sizeOf() + |
Mark Salyzyn | b067229 | 2016-10-06 10:09:24 -0700 | [diff] [blame] | 551 | tagTable.sizeOf() + securityTagTable.sizeOf() + |
| 552 | (pidTable.size() * sizeof(pidTable_t::iterator)) + |
| 553 | (tagTable.size() * sizeof(tagTable_t::iterator)); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 554 | for (auto it : pidTable) { |
Mark Salyzyn | 6d981af | 2016-10-06 09:55:21 -0700 | [diff] [blame] | 555 | const char* name = it.second.getName(); |
| 556 | if (name) size += strlen(name) + 1; |
| 557 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 558 | for (auto it : tidTable) { |
Mark Salyzyn | 6d981af | 2016-10-06 09:55:21 -0700 | [diff] [blame] | 559 | const char* name = it.second.getName(); |
| 560 | if (name) size += strlen(name) + 1; |
| 561 | } |
| 562 | log_id_for_each(id) { |
| 563 | size += uidTable[id].sizeOf(); |
Mark Salyzyn | b067229 | 2016-10-06 10:09:24 -0700 | [diff] [blame] | 564 | size += uidTable[id].size() * sizeof(uidTable_t::iterator); |
Mark Salyzyn | 6d981af | 2016-10-06 09:55:21 -0700 | [diff] [blame] | 565 | size += pidSystemTable[id].sizeOf(); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 566 | size += |
| 567 | pidSystemTable[id].size() * sizeof(pidSystemTable_t::iterator); |
Mark Salyzyn | 6d981af | 2016-10-06 09:55:21 -0700 | [diff] [blame] | 568 | } |
| 569 | return size; |
| 570 | } |
| 571 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 572 | public: |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 573 | LogStatistics(); |
| 574 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 575 | void enableStatistics() { |
| 576 | enable = true; |
| 577 | } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 578 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 579 | void add(LogBufferElement* entry); |
| 580 | void subtract(LogBufferElement* entry); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 581 | // entry->setDropped(1) must follow this call |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 582 | void drop(LogBufferElement* entry); |
Mark Salyzyn | aaad42f | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 583 | // Correct for coalescing two entries referencing dropped content |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 584 | void erase(LogBufferElement* element) { |
Mark Salyzyn | 58b8be8 | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 585 | log_id_t log_id = element->getLogId(); |
| 586 | --mElements[log_id]; |
| 587 | --mDroppedElements[log_id]; |
| 588 | } |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 589 | |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 590 | LogFindWorst<UidEntry> sort(uid_t uid, pid_t pid, size_t len, log_id id) { |
| 591 | return LogFindWorst<UidEntry>(uidTable[id].sort(uid, pid, len)); |
Mark Salyzyn | 758058f | 2015-08-21 16:44:30 -0700 | [diff] [blame] | 592 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 593 | LogFindWorst<PidEntry> sortPids(uid_t uid, pid_t pid, size_t len, |
| 594 | log_id id) { |
Mark Salyzyn | 6a06694 | 2016-07-14 15:34:30 -0700 | [diff] [blame] | 595 | return LogFindWorst<PidEntry>(pidSystemTable[id].sort(uid, pid, len)); |
| 596 | } |
| 597 | LogFindWorst<TagEntry> sortTags(uid_t uid, pid_t pid, size_t len, log_id) { |
| 598 | return LogFindWorst<TagEntry>(tagTable.sort(uid, pid, len)); |
Mark Salyzyn | bec3c3d | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 599 | } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 600 | |
| 601 | // fast track current value by id only |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 602 | size_t sizes(log_id_t id) const { |
| 603 | return mSizes[id]; |
| 604 | } |
| 605 | size_t elements(log_id_t id) const { |
| 606 | return mElements[id]; |
| 607 | } |
Mark Salyzyn | 58b8be8 | 2015-09-30 07:40:09 -0700 | [diff] [blame] | 608 | size_t realElements(log_id_t id) const { |
| 609 | return mElements[id] - mDroppedElements[id]; |
| 610 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 611 | size_t sizesTotal(log_id_t id) const { |
| 612 | return mSizesTotal[id]; |
| 613 | } |
| 614 | size_t elementsTotal(log_id_t id) const { |
| 615 | return mElementsTotal[id]; |
| 616 | } |
| 617 | static size_t sizesTotal() { |
| 618 | return SizesTotal; |
| 619 | } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 620 | |
Mark Salyzyn | ee3b838 | 2015-12-17 09:58:43 -0800 | [diff] [blame] | 621 | 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] | 622 | |
Mark Salyzyn | ed777e9 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 623 | // helper (must be locked directly or implicitly by mLogElementsLock) |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 624 | const char* pidToName(pid_t pid) const; |
Mark Salyzyn | 4ba0387 | 2014-04-07 07:15:33 -0700 | [diff] [blame] | 625 | uid_t pidToUid(pid_t pid); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 626 | const char* uidToName(uid_t uid) const; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 627 | }; |
| 628 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 629 | #endif // _LOGD_LOG_STATISTICS_H__ |