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 39 char *name; 40 bool mGone; 41 42 public: 43 static const pid_t gone = (pid_t) -1; 44 45 PidStatistics(pid_t pid, char *name = NULL); 46 PidStatistics(const PidStatistics ©); 47 ~PidStatistics(); 48 getPid()49 pid_t getPid() const { return pid; } 50 bool pidGone(); getName()51 char *getName() const { return name; } 52 void setName(char *name); 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 sizes()58 size_t sizes() const { return mSizes; } elements()59 size_t elements() const { return mElements; } 60 sizesTotal()61 size_t sizesTotal() const { return mSizesTotal; } elementsTotal()62 size_t elementsTotal() const { return mElementsTotal; } 63 64 // helper 65 static char *pidToName(pid_t pid); 66 }; 67 68 typedef android::List<PidStatistics *> PidStatisticsCollection; 69 70 class UidStatistics { 71 const uid_t uid; 72 73 PidStatisticsCollection Pids; 74 75 size_t mSizes; 76 size_t mElements; 77 78 public: 79 UidStatistics(uid_t uid); 80 ~UidStatistics(); 81 begin()82 PidStatisticsCollection::iterator begin() { return Pids.begin(); } end()83 PidStatisticsCollection::iterator end() { return Pids.end(); } 84 getUid()85 uid_t getUid() { return uid; } 86 87 void add(unsigned short size, pid_t pid); 88 void subtract(unsigned short size, pid_t pid); 89 void sort(); 90 91 static const pid_t pid_all = (pid_t) -1; 92 93 // fast track current value sizes()94 size_t sizes() const { return mSizes; }; elements()95 size_t elements() const { return mElements; }; 96 97 // statistical track 98 size_t sizes(pid_t pid); 99 size_t elements(pid_t pid); 100 101 size_t sizesTotal(pid_t pid = pid_all); 102 size_t elementsTotal(pid_t pid = pid_all); 103 104 // helper pidToName(pid_t pid)105 static char *pidToName(pid_t pid) { return PidStatistics::pidToName(pid); } 106 }; 107 108 typedef android::List<UidStatistics *> UidStatisticsCollection; 109 110 class LidStatistics { 111 UidStatisticsCollection Uids; 112 113 public: 114 LidStatistics(); 115 ~LidStatistics(); 116 begin()117 UidStatisticsCollection::iterator begin() { return Uids.begin(); } end()118 UidStatisticsCollection::iterator end() { return Uids.end(); } 119 120 void add(unsigned short size, uid_t uid, pid_t pid); 121 void subtract(unsigned short size, uid_t uid, pid_t pid); 122 void sort(); 123 124 static const pid_t pid_all = (pid_t) -1; 125 static const uid_t uid_all = (uid_t) -1; 126 127 size_t sizes(uid_t uid = uid_all, pid_t pid = pid_all); 128 size_t elements(uid_t uid = uid_all, pid_t pid = pid_all); 129 130 size_t sizesTotal(uid_t uid = uid_all, pid_t pid = pid_all); 131 size_t elementsTotal(uid_t uid = uid_all, pid_t pid = pid_all); 132 }; 133 134 // Log Statistics 135 class LogStatistics { 136 LidStatistics LogIds[LOG_ID_MAX]; 137 138 size_t mSizes[LOG_ID_MAX]; 139 size_t mElements[LOG_ID_MAX]; 140 141 bool dgram_qlen_statistics; 142 143 static const unsigned short mBuckets[14]; 144 log_time mMinimum[sizeof(mBuckets) / sizeof(mBuckets[0])]; 145 146 public: 147 const log_time start; 148 149 LogStatistics(); 150 id(log_id_t log_id)151 LidStatistics &id(log_id_t log_id) { return LogIds[log_id]; } 152 enableDgramQlenStatistics()153 void enableDgramQlenStatistics() { dgram_qlen_statistics = true; } 154 static unsigned short dgram_qlen(unsigned short bucket); 155 unsigned long long minimum(unsigned short bucket); 156 void recordDiff(log_time diff, unsigned short bucket); 157 158 void add(unsigned short size, log_id_t log_id, uid_t uid, pid_t pid); 159 void subtract(unsigned short size, log_id_t log_id, uid_t uid, pid_t pid); 160 void sort(); 161 162 // fast track current value by id only sizes(log_id_t id)163 size_t sizes(log_id_t id) const { return mSizes[id]; } elements(log_id_t id)164 size_t elements(log_id_t id) const { return mElements[id]; } 165 166 // statistical track 167 static const log_id_t log_id_all = (log_id_t) -1; 168 static const uid_t uid_all = (uid_t) -1; 169 static const pid_t pid_all = (pid_t) -1; 170 171 size_t sizes(log_id_t id, uid_t uid, pid_t pid = pid_all); 172 size_t elements(log_id_t id, uid_t uid, pid_t pid = pid_all); sizes()173 size_t sizes() { return sizes(log_id_all, uid_all); } elements()174 size_t elements() { return elements(log_id_all, uid_all); } 175 176 size_t sizesTotal(log_id_t id = log_id_all, 177 uid_t uid = uid_all, 178 pid_t pid = pid_all); 179 size_t elementsTotal(log_id_t id = log_id_all, 180 uid_t uid = uid_all, 181 pid_t pid = pid_all); 182 183 // *strp = malloc, balance with free 184 void format(char **strp, uid_t uid, unsigned int logMask, log_time oldest); 185 186 // helper pidToName(pid_t pid)187 static char *pidToName(pid_t pid) { return PidStatistics::pidToName(pid); } 188 uid_t pidToUid(pid_t pid); 189 }; 190 191 #endif // _LOGD_LOG_STATISTICS_H__ 192