blob: f3110d755ef1b1d007deac27d53b8f5a9f3db69d [file] [log] [blame]
Mark Salyzyn34facab2014-02-06 14:48:50 -08001/*
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 Salyzyn720f6d12015-03-16 08:26:05 -070020#include <memory>
21#include <stdlib.h>
Mark Salyzyn34facab2014-02-06 14:48:50 -080022#include <sys/types.h>
23
24#include <log/log.h>
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -070025#include <utils/BasicHashtable.h>
26
27#include "LogBufferElement.h"
Mark Salyzyn34facab2014-02-06 14:48:50 -080028
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 Salyzyn720f6d12015-03-16 08:26:05 -070032template <typename TKey, typename TEntry>
33class LogHashtable : public android::BasicHashtable<TKey, TEntry> {
34public:
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 Salyzyn97c1c2b2015-03-10 13:51:35 -070078struct UidEntry {
Mark Salyzyn34facab2014-02-06 14:48:50 -080079 const uid_t uid;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -070080 size_t size;
Mark Salyzynab0dcf62015-03-16 12:04:09 -070081 size_t dropped;
Mark Salyzyn34facab2014-02-06 14:48:50 -080082
Mark Salyzynab0dcf62015-03-16 12:04:09 -070083 UidEntry(uid_t uid):uid(uid),size(0),dropped(0) { }
Mark Salyzyn34facab2014-02-06 14:48:50 -080084
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -070085 inline const uid_t&getKey() const { return uid; }
86 size_t getSizes() const { return size; }
Mark Salyzynab0dcf62015-03-16 12:04:09 -070087 size_t getDropped() const { return dropped; }
88
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -070089 inline void add(size_t s) { size += s; }
Mark Salyzynab0dcf62015-03-16 12:04:09 -070090 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 Salyzyn34facab2014-02-06 14:48:50 -080093};
94
Mark Salyzyn720f6d12015-03-16 08:26:05 -070095struct PidEntry {
96 const pid_t pid;
97 uid_t uid;
98 char *name;
99 size_t size;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700100 size_t dropped;
Mark Salyzyn720f6d12015-03-16 08:26:05 -0700101
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700102 PidEntry(pid_t p, uid_t u, char *n):pid(p),uid(u),name(n),size(0),dropped(0) { }
Mark Salyzyn720f6d12015-03-16 08:26:05 -0700103 PidEntry(const PidEntry &c):
104 pid(c.pid),
105 uid(c.uid),
106 name(c.name ? strdup(c.name) : NULL),
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700107 size(c.size),
108 dropped(c.dropped) { }
Mark Salyzyn720f6d12015-03-16 08:26:05 -0700109 ~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 Salyzynab0dcf62015-03-16 12:04:09 -0700117 size_t getDropped() const { return dropped; }
Mark Salyzyn720f6d12015-03-16 08:26:05 -0700118 inline void add(size_t s) { size += s; }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700119 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 Salyzyn720f6d12015-03-16 08:26:05 -0700122};
123
Mark Salyzyn34facab2014-02-06 14:48:50 -0800124// Log Statistics
125class LogStatistics {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800126 size_t mSizes[LOG_ID_MAX];
127 size_t mElements[LOG_ID_MAX];
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700128 size_t mSizesTotal[LOG_ID_MAX];
129 size_t mElementsTotal[LOG_ID_MAX];
Mark Salyzyn720f6d12015-03-16 08:26:05 -0700130 bool enable;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800131
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700132 // uid to size list
Mark Salyzyn720f6d12015-03-16 08:26:05 -0700133 typedef LogHashtable<uid_t, UidEntry> uidTable_t;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700134 uidTable_t uidTable[LOG_ID_MAX];
Mark Salyzyne457b742014-02-19 17:18:31 -0800135
Mark Salyzyn720f6d12015-03-16 08:26:05 -0700136 // pid to uid list
137 typedef LogHashtable<pid_t, PidEntry> pidTable_t;
138 pidTable_t pidTable;
139
Mark Salyzyn34facab2014-02-06 14:48:50 -0800140public:
Mark Salyzyn34facab2014-02-06 14:48:50 -0800141 LogStatistics();
142
Mark Salyzyn720f6d12015-03-16 08:26:05 -0700143 void enableStatistics() { enable = true; }
Mark Salyzyn34facab2014-02-06 14:48:50 -0800144
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700145 void add(LogBufferElement *entry);
146 void subtract(LogBufferElement *entry);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700147 // 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 Salyzyne457b742014-02-19 17:18:31 -0800151
Mark Salyzyn720f6d12015-03-16 08:26:05 -0700152 std::unique_ptr<const UidEntry *[]> sort(size_t n, log_id i) { return uidTable[i].sort(n); }
Mark Salyzyn34facab2014-02-06 14:48:50 -0800153
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 Salyzyn97c1c2b2015-03-10 13:51:35 -0700157 size_t sizesTotal(log_id_t id) const { return mSizesTotal[id]; }
158 size_t elementsTotal(log_id_t id) const { return mElementsTotal[id]; }
Mark Salyzyn34facab2014-02-06 14:48:50 -0800159
160 // *strp = malloc, balance with free
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700161 void format(char **strp, uid_t uid, unsigned int logMask);
Mark Salyzyn9a038632014-04-07 07:05:40 -0700162
163 // helper
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700164 char *pidToName(pid_t pid);
Mark Salyzyn4ba03872014-04-07 07:15:33 -0700165 uid_t pidToUid(pid_t pid);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700166 char *uidToName(uid_t uid);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800167};
168
169#endif // _LOGD_LOG_STATISTICS_H__