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; |
| 40 | |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 41 | public: |
| 42 | static const pid_t gone = (pid_t) -1; |
| 43 | |
Mark Salyzyn | 9a03863 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 44 | PidStatistics(pid_t pid, char *name = NULL); |
| 45 | PidStatistics(const PidStatistics ©); |
| 46 | ~PidStatistics(); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 47 | |
| 48 | pid_t getPid() const { return pid; } |
Mark Salyzyn | 9a03863 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 49 | char *getName() const { return name; } |
| 50 | void setName(char *name); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 51 | |
| 52 | void add(unsigned short size); |
| 53 | bool subtract(unsigned short size); // returns true if stats and PID gone |
| 54 | void addTotal(size_t size, size_t element); |
| 55 | |
| 56 | size_t sizes() const { return mSizes; } |
| 57 | size_t elements() const { return mElements; } |
| 58 | |
| 59 | size_t sizesTotal() const { return mSizesTotal; } |
| 60 | size_t elementsTotal() const { return mElementsTotal; } |
Mark Salyzyn | 9a03863 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 61 | |
| 62 | // helper |
| 63 | static char *pidToName(pid_t pid); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | typedef android::List<PidStatistics *> PidStatisticsCollection; |
| 67 | |
| 68 | class UidStatistics { |
| 69 | const uid_t uid; |
| 70 | |
| 71 | PidStatisticsCollection Pids; |
| 72 | |
| 73 | public: |
| 74 | UidStatistics(uid_t uid); |
| 75 | ~UidStatistics(); |
| 76 | |
| 77 | PidStatisticsCollection::iterator begin() { return Pids.begin(); } |
| 78 | PidStatisticsCollection::iterator end() { return Pids.end(); } |
| 79 | |
| 80 | uid_t getUid() { return uid; } |
| 81 | |
| 82 | void add(unsigned short size, pid_t pid); |
| 83 | void subtract(unsigned short size, pid_t pid); |
| 84 | |
| 85 | static const pid_t pid_all = (pid_t) -1; |
| 86 | |
| 87 | size_t sizes(pid_t pid = pid_all); |
| 88 | size_t elements(pid_t pid = pid_all); |
| 89 | |
| 90 | size_t sizesTotal(pid_t pid = pid_all); |
| 91 | size_t elementsTotal(pid_t pid = pid_all); |
Mark Salyzyn | 9a03863 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 92 | |
| 93 | // helper |
| 94 | static char *pidToName(pid_t pid) { return PidStatistics::pidToName(pid); } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | typedef android::List<UidStatistics *> UidStatisticsCollection; |
| 98 | |
| 99 | class LidStatistics { |
| 100 | UidStatisticsCollection Uids; |
| 101 | |
| 102 | public: |
| 103 | LidStatistics(); |
| 104 | ~LidStatistics(); |
| 105 | |
| 106 | UidStatisticsCollection::iterator begin() { return Uids.begin(); } |
| 107 | UidStatisticsCollection::iterator end() { return Uids.end(); } |
| 108 | |
| 109 | void add(unsigned short size, uid_t uid, pid_t pid); |
| 110 | void subtract(unsigned short size, uid_t uid, pid_t pid); |
| 111 | |
| 112 | static const pid_t pid_all = (pid_t) -1; |
| 113 | static const uid_t uid_all = (uid_t) -1; |
| 114 | |
| 115 | size_t sizes(uid_t uid = uid_all, pid_t pid = pid_all); |
| 116 | size_t elements(uid_t uid = uid_all, pid_t pid = pid_all); |
| 117 | |
| 118 | size_t sizesTotal(uid_t uid = uid_all, pid_t pid = pid_all); |
| 119 | size_t elementsTotal(uid_t uid = uid_all, pid_t pid = pid_all); |
| 120 | }; |
| 121 | |
| 122 | // Log Statistics |
| 123 | class LogStatistics { |
| 124 | LidStatistics LogIds[LOG_ID_MAX]; |
| 125 | |
| 126 | size_t mSizes[LOG_ID_MAX]; |
| 127 | size_t mElements[LOG_ID_MAX]; |
| 128 | |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 129 | bool dgram_qlen_statistics; |
| 130 | |
| 131 | static const unsigned short mBuckets[14]; |
| 132 | log_time mMinimum[sizeof(mBuckets) / sizeof(mBuckets[0])]; |
| 133 | |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 134 | public: |
| 135 | const log_time start; |
| 136 | |
| 137 | LogStatistics(); |
| 138 | |
| 139 | LidStatistics &id(log_id_t log_id) { return LogIds[log_id]; } |
| 140 | |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 141 | void enableDgramQlenStatistics() { dgram_qlen_statistics = true; } |
| 142 | static unsigned short dgram_qlen(unsigned short bucket); |
| 143 | unsigned long long minimum(unsigned short bucket); |
| 144 | void recordDiff(log_time diff, unsigned short bucket); |
| 145 | |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 146 | void add(unsigned short size, log_id_t log_id, uid_t uid, pid_t pid); |
| 147 | void subtract(unsigned short size, log_id_t log_id, uid_t uid, pid_t pid); |
| 148 | |
| 149 | // fast track current value by id only |
| 150 | size_t sizes(log_id_t id) const { return mSizes[id]; } |
| 151 | size_t elements(log_id_t id) const { return mElements[id]; } |
| 152 | |
| 153 | // statistical track |
| 154 | static const log_id_t log_id_all = (log_id_t) -1; |
| 155 | static const uid_t uid_all = (uid_t) -1; |
| 156 | static const pid_t pid_all = (pid_t) -1; |
| 157 | |
| 158 | size_t sizes(log_id_t id, uid_t uid, pid_t pid = pid_all); |
| 159 | size_t elements(log_id_t id, uid_t uid, pid_t pid = pid_all); |
| 160 | size_t sizes() { return sizes(log_id_all, uid_all); } |
| 161 | size_t elements() { return elements(log_id_all, uid_all); } |
| 162 | |
| 163 | size_t sizesTotal(log_id_t id = log_id_all, |
| 164 | uid_t uid = uid_all, |
| 165 | pid_t pid = pid_all); |
| 166 | size_t elementsTotal(log_id_t id = log_id_all, |
| 167 | uid_t uid = uid_all, |
| 168 | pid_t pid = pid_all); |
| 169 | |
| 170 | // *strp = malloc, balance with free |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 171 | 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] | 172 | |
| 173 | // helper |
| 174 | static char *pidToName(pid_t pid) { return PidStatistics::pidToName(pid); } |
Mark Salyzyn | 4ba0387 | 2014-04-07 07:15:33 -0700 | [diff] [blame^] | 175 | uid_t pidToUid(pid_t pid); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 176 | }; |
| 177 | |
| 178 | #endif // _LOGD_LOG_STATISTICS_H__ |