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 | |
| 24 | #include <log/log.h> |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 25 | #include <utils/BasicHashtable.h> |
| 26 | |
| 27 | #include "LogBufferElement.h" |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 28 | |
| 29 | #define log_id_for_each(i) \ |
| 30 | for (log_id_t i = LOG_ID_MIN; i < LOG_ID_MAX; i = (log_id_t) (i + 1)) |
| 31 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 32 | template <typename TKey, typename TEntry> |
| 33 | class LogHashtable : public android::BasicHashtable<TKey, TEntry> { |
| 34 | public: |
| 35 | std::unique_ptr<const TEntry *[]> sort(size_t n) { |
| 36 | if (!n) { |
| 37 | std::unique_ptr<const TEntry *[]> sorted(NULL); |
| 38 | return sorted; |
| 39 | } |
| 40 | |
| 41 | const TEntry **retval = new const TEntry* [n]; |
| 42 | memset(retval, 0, sizeof(*retval) * n); |
| 43 | |
| 44 | ssize_t index = -1; |
| 45 | while ((index = android::BasicHashtable<TKey, TEntry>::next(index)) >= 0) { |
| 46 | const TEntry &entry = android::BasicHashtable<TKey, TEntry>::entryAt(index); |
| 47 | size_t s = entry.getSizes(); |
| 48 | ssize_t i = n - 1; |
| 49 | while ((!retval[i] || (s > retval[i]->getSizes())) && (--i >= 0)) |
| 50 | ; |
| 51 | if (++i < (ssize_t)n) { |
| 52 | size_t b = n - i - 1; |
| 53 | if (b) { |
| 54 | memmove(&retval[i+1], &retval[i], b * sizeof(retval[0])); |
| 55 | } |
| 56 | retval[i] = &entry; |
| 57 | } |
| 58 | } |
| 59 | std::unique_ptr<const TEntry *[]> sorted(retval); |
| 60 | return sorted; |
| 61 | } |
| 62 | |
| 63 | // Iteration handler for the sort method output |
| 64 | static ssize_t next(ssize_t index, std::unique_ptr<const TEntry *[]> &sorted, size_t n) { |
| 65 | ++index; |
| 66 | if (!sorted.get() || (index < 0) || (n <= (size_t)index) || !sorted[index] |
| 67 | || (sorted[index]->getSizes() <= (sorted[0]->getSizes() / 100))) { |
| 68 | return -1; |
| 69 | } |
| 70 | return index; |
| 71 | } |
| 72 | |
| 73 | ssize_t next(ssize_t index) { |
| 74 | return android::BasicHashtable<TKey, TEntry>::next(index); |
| 75 | } |
| 76 | }; |
| 77 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 78 | struct UidEntry { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 79 | const uid_t uid; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 80 | size_t size; |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 81 | size_t dropped; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 82 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 83 | UidEntry(uid_t uid):uid(uid),size(0),dropped(0) { } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 84 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 85 | inline const uid_t&getKey() const { return uid; } |
| 86 | size_t getSizes() const { return size; } |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 87 | size_t getDropped() const { return dropped; } |
| 88 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 89 | inline void add(size_t s) { size += s; } |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 90 | inline void add_dropped(size_t d) { dropped += d; } |
| 91 | inline bool subtract(size_t s) { size -= s; return !dropped && !size; } |
| 92 | inline bool subtract_dropped(size_t d) { dropped -= d; return !dropped && !size; } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 93 | }; |
| 94 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 95 | struct PidEntry { |
| 96 | const pid_t pid; |
| 97 | uid_t uid; |
| 98 | char *name; |
| 99 | size_t size; |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 100 | size_t dropped; |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 101 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 102 | PidEntry(pid_t p, uid_t u, char *n):pid(p),uid(u),name(n),size(0),dropped(0) { } |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 103 | PidEntry(const PidEntry &c): |
| 104 | pid(c.pid), |
| 105 | uid(c.uid), |
| 106 | name(c.name ? strdup(c.name) : NULL), |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 107 | size(c.size), |
| 108 | dropped(c.dropped) { } |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 109 | ~PidEntry() { free(name); } |
| 110 | |
| 111 | const pid_t&getKey() const { return pid; } |
| 112 | const uid_t&getUid() const { return uid; } |
| 113 | uid_t&setUid(uid_t u) { return uid = u; } |
| 114 | const char*getName() const { return name; } |
| 115 | char *setName(char *n) { free(name); return name = n; } |
| 116 | size_t getSizes() const { return size; } |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 117 | size_t getDropped() const { return dropped; } |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 118 | inline void add(size_t s) { size += s; } |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 119 | inline void add_dropped(size_t d) { dropped += d; } |
| 120 | inline bool subtract(size_t s) { size -= s; return !dropped && !size; } |
| 121 | inline bool subtract_dropped(size_t d) { dropped -= d; return !dropped && !size; } |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 124 | // Log Statistics |
| 125 | class LogStatistics { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 126 | size_t mSizes[LOG_ID_MAX]; |
| 127 | size_t mElements[LOG_ID_MAX]; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 128 | size_t mSizesTotal[LOG_ID_MAX]; |
| 129 | size_t mElementsTotal[LOG_ID_MAX]; |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 130 | bool enable; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 131 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 132 | // uid to size list |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 133 | typedef LogHashtable<uid_t, UidEntry> uidTable_t; |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 134 | uidTable_t uidTable[LOG_ID_MAX]; |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 135 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 136 | // pid to uid list |
| 137 | typedef LogHashtable<pid_t, PidEntry> pidTable_t; |
| 138 | pidTable_t pidTable; |
| 139 | |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 140 | public: |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 141 | LogStatistics(); |
| 142 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 143 | void enableStatistics() { enable = true; } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 144 | |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 145 | void add(LogBufferElement *entry); |
| 146 | void subtract(LogBufferElement *entry); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 147 | // entry->setDropped(1) must follow this call |
| 148 | void drop(LogBufferElement *entry); |
| 149 | // Correct for merging two entries referencing dropped content |
| 150 | void erase(LogBufferElement *e) { --mElements[e->getLogId()]; } |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 151 | |
Mark Salyzyn | 720f6d1 | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 152 | std::unique_ptr<const UidEntry *[]> sort(size_t n, log_id i) { return uidTable[i].sort(n); } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 153 | |
| 154 | // fast track current value by id only |
| 155 | size_t sizes(log_id_t id) const { return mSizes[id]; } |
| 156 | size_t elements(log_id_t id) const { return mElements[id]; } |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 157 | size_t sizesTotal(log_id_t id) const { return mSizesTotal[id]; } |
| 158 | size_t elementsTotal(log_id_t id) const { return mElementsTotal[id]; } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 159 | |
| 160 | // *strp = malloc, balance with free |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 161 | void format(char **strp, uid_t uid, unsigned int logMask); |
Mark Salyzyn | 9a03863 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 162 | |
| 163 | // helper |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 164 | char *pidToName(pid_t pid); |
Mark Salyzyn | 4ba0387 | 2014-04-07 07:15:33 -0700 | [diff] [blame] | 165 | uid_t pidToUid(pid_t pid); |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 166 | char *uidToName(uid_t uid); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | #endif // _LOGD_LOG_STATISTICS_H__ |