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 | |
| 20 | #include <sys/types.h> |
| 21 | |
| 22 | #include <log/log.h> |
| 23 | #include <log/log_read.h> |
| 24 | #include <utils/List.h> |
| 25 | |
| 26 | #define log_id_for_each(i) \ |
| 27 | for (log_id_t i = LOG_ID_MIN; i < LOG_ID_MAX; i = (log_id_t) (i + 1)) |
| 28 | |
| 29 | class PidStatistics { |
| 30 | const pid_t pid; |
| 31 | |
| 32 | // Total |
| 33 | size_t mSizesTotal; |
| 34 | size_t mElementsTotal; |
| 35 | // Current |
| 36 | size_t mSizes; |
| 37 | size_t mElements; |
| 38 | |
Mark Salyzyn | 9a03863 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 39 | char *name; |
Mark Salyzyn | fdabe72 | 2014-04-21 10:09:42 -0700 | [diff] [blame] | 40 | bool mGone; |
Mark Salyzyn | 9a03863 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 41 | |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 42 | public: |
| 43 | static const pid_t gone = (pid_t) -1; |
| 44 | |
Mark Salyzyn | 9a03863 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 45 | PidStatistics(pid_t pid, char *name = NULL); |
| 46 | PidStatistics(const PidStatistics ©); |
| 47 | ~PidStatistics(); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 48 | |
| 49 | pid_t getPid() const { return pid; } |
Mark Salyzyn | fdabe72 | 2014-04-21 10:09:42 -0700 | [diff] [blame] | 50 | bool pidGone(); |
Mark Salyzyn | 9a03863 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 51 | char *getName() const { return name; } |
| 52 | void setName(char *name); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 53 | |
| 54 | void add(unsigned short size); |
| 55 | bool subtract(unsigned short size); // returns true if stats and PID gone |
| 56 | void addTotal(size_t size, size_t element); |
| 57 | |
| 58 | size_t sizes() const { return mSizes; } |
| 59 | size_t elements() const { return mElements; } |
| 60 | |
| 61 | size_t sizesTotal() const { return mSizesTotal; } |
| 62 | size_t elementsTotal() const { return mElementsTotal; } |
Mark Salyzyn | 9a03863 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 63 | |
| 64 | // helper |
| 65 | static char *pidToName(pid_t pid); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | typedef android::List<PidStatistics *> PidStatisticsCollection; |
| 69 | |
| 70 | class UidStatistics { |
| 71 | const uid_t uid; |
| 72 | |
| 73 | PidStatisticsCollection Pids; |
| 74 | |
Mark Salyzyn | e72c6e4 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 75 | void insert(PidStatisticsCollection::iterator i, PidStatistics *p) |
| 76 | { Pids.insert(i, p); } |
| 77 | void push_back(PidStatistics *p) { Pids.push_back(p); } |
| 78 | |
Mark Salyzyn | c8a576c | 2014-04-04 16:35:59 -0700 | [diff] [blame] | 79 | size_t mSizes; |
| 80 | size_t mElements; |
| 81 | |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 82 | public: |
| 83 | UidStatistics(uid_t uid); |
| 84 | ~UidStatistics(); |
| 85 | |
| 86 | PidStatisticsCollection::iterator begin() { return Pids.begin(); } |
| 87 | PidStatisticsCollection::iterator end() { return Pids.end(); } |
Mark Salyzyn | e72c6e4 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 88 | PidStatisticsCollection::iterator erase(PidStatisticsCollection::iterator i) |
| 89 | { return Pids.erase(i); } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 90 | |
| 91 | uid_t getUid() { return uid; } |
| 92 | |
| 93 | void add(unsigned short size, pid_t pid); |
| 94 | void subtract(unsigned short size, pid_t pid); |
Mark Salyzyn | c8a576c | 2014-04-04 16:35:59 -0700 | [diff] [blame] | 95 | void sort(); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 96 | |
| 97 | static const pid_t pid_all = (pid_t) -1; |
| 98 | |
Mark Salyzyn | c8a576c | 2014-04-04 16:35:59 -0700 | [diff] [blame] | 99 | // fast track current value |
| 100 | size_t sizes() const { return mSizes; }; |
| 101 | size_t elements() const { return mElements; }; |
| 102 | |
| 103 | // statistical track |
| 104 | size_t sizes(pid_t pid); |
| 105 | size_t elements(pid_t pid); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 106 | |
| 107 | size_t sizesTotal(pid_t pid = pid_all); |
| 108 | size_t elementsTotal(pid_t pid = pid_all); |
Mark Salyzyn | 9a03863 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 109 | |
| 110 | // helper |
| 111 | static char *pidToName(pid_t pid) { return PidStatistics::pidToName(pid); } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | typedef android::List<UidStatistics *> UidStatisticsCollection; |
| 115 | |
| 116 | class LidStatistics { |
| 117 | UidStatisticsCollection Uids; |
| 118 | |
| 119 | public: |
| 120 | LidStatistics(); |
| 121 | ~LidStatistics(); |
| 122 | |
| 123 | UidStatisticsCollection::iterator begin() { return Uids.begin(); } |
| 124 | UidStatisticsCollection::iterator end() { return Uids.end(); } |
| 125 | |
| 126 | void add(unsigned short size, uid_t uid, pid_t pid); |
| 127 | void subtract(unsigned short size, uid_t uid, pid_t pid); |
Mark Salyzyn | c8a576c | 2014-04-04 16:35:59 -0700 | [diff] [blame] | 128 | void sort(); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 129 | |
| 130 | static const pid_t pid_all = (pid_t) -1; |
| 131 | static const uid_t uid_all = (uid_t) -1; |
| 132 | |
| 133 | size_t sizes(uid_t uid = uid_all, pid_t pid = pid_all); |
| 134 | size_t elements(uid_t uid = uid_all, pid_t pid = pid_all); |
| 135 | |
| 136 | size_t sizesTotal(uid_t uid = uid_all, pid_t pid = pid_all); |
| 137 | size_t elementsTotal(uid_t uid = uid_all, pid_t pid = pid_all); |
| 138 | }; |
| 139 | |
| 140 | // Log Statistics |
| 141 | class LogStatistics { |
| 142 | LidStatistics LogIds[LOG_ID_MAX]; |
| 143 | |
| 144 | size_t mSizes[LOG_ID_MAX]; |
| 145 | size_t mElements[LOG_ID_MAX]; |
| 146 | |
Mark Salyzyn | f5fc509 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 147 | bool mStatistics; |
Mark Salyzyn | e72c6e4 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 148 | bool dgramQlenStatistics; |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 149 | |
| 150 | static const unsigned short mBuckets[14]; |
| 151 | log_time mMinimum[sizeof(mBuckets) / sizeof(mBuckets[0])]; |
| 152 | |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 153 | public: |
| 154 | const log_time start; |
| 155 | |
| 156 | LogStatistics(); |
| 157 | |
| 158 | LidStatistics &id(log_id_t log_id) { return LogIds[log_id]; } |
| 159 | |
Mark Salyzyn | e72c6e4 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 160 | void enableDgramQlenStatistics() { dgramQlenStatistics = true; } |
Mark Salyzyn | f5fc509 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 161 | void enableStatistics() { mStatistics = true; } |
Mark Salyzyn | e72c6e4 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 162 | static unsigned short dgramQlen(unsigned short bucket); |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 163 | unsigned long long minimum(unsigned short bucket); |
| 164 | void recordDiff(log_time diff, unsigned short bucket); |
| 165 | |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 166 | void add(unsigned short size, log_id_t log_id, uid_t uid, pid_t pid); |
| 167 | void subtract(unsigned short size, log_id_t log_id, uid_t uid, pid_t pid); |
Mark Salyzyn | c8a576c | 2014-04-04 16:35:59 -0700 | [diff] [blame] | 168 | void sort(); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 169 | |
| 170 | // fast track current value by id only |
| 171 | size_t sizes(log_id_t id) const { return mSizes[id]; } |
| 172 | size_t elements(log_id_t id) const { return mElements[id]; } |
| 173 | |
| 174 | // statistical track |
| 175 | static const log_id_t log_id_all = (log_id_t) -1; |
| 176 | static const uid_t uid_all = (uid_t) -1; |
| 177 | static const pid_t pid_all = (pid_t) -1; |
| 178 | |
| 179 | size_t sizes(log_id_t id, uid_t uid, pid_t pid = pid_all); |
| 180 | size_t elements(log_id_t id, uid_t uid, pid_t pid = pid_all); |
| 181 | size_t sizes() { return sizes(log_id_all, uid_all); } |
| 182 | size_t elements() { return elements(log_id_all, uid_all); } |
| 183 | |
| 184 | size_t sizesTotal(log_id_t id = log_id_all, |
| 185 | uid_t uid = uid_all, |
| 186 | pid_t pid = pid_all); |
| 187 | size_t elementsTotal(log_id_t id = log_id_all, |
| 188 | uid_t uid = uid_all, |
| 189 | pid_t pid = pid_all); |
| 190 | |
| 191 | // *strp = malloc, balance with free |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 192 | void format(char **strp, uid_t uid, unsigned int logMask, log_time oldest); |
Mark Salyzyn | 9a03863 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 193 | |
| 194 | // helper |
| 195 | static char *pidToName(pid_t pid) { return PidStatistics::pidToName(pid); } |
Mark Salyzyn | 4ba0387 | 2014-04-07 07:15:33 -0700 | [diff] [blame] | 196 | uid_t pidToUid(pid_t pid); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | #endif // _LOGD_LOG_STATISTICS_H__ |