LogStatistics.h revision 97c1c2beee2cbd8c67c1cd507367e5b084d853c8
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 <utils/BasicHashtable.h>
24
25#include "LogBufferElement.h"
26
27#define log_id_for_each(i) \
28    for (log_id_t i = LOG_ID_MIN; i < LOG_ID_MAX; i = (log_id_t) (i + 1))
29
30struct UidEntry {
31    const uid_t uid;
32    size_t size;
33
34    UidEntry(uid_t uid):uid(uid),size(0) { }
35
36    inline const uid_t&getKey() const { return uid; }
37    size_t getSizes() const { return size; }
38    inline void add(size_t s) { size += s; }
39    inline bool subtract(size_t s) { size -= s; return !size; }
40};
41
42// Log Statistics
43class LogStatistics {
44    size_t mSizes[LOG_ID_MAX];
45    size_t mElements[LOG_ID_MAX];
46    size_t mSizesTotal[LOG_ID_MAX];
47    size_t mElementsTotal[LOG_ID_MAX];
48
49    // uid to size list
50    typedef android::BasicHashtable<uid_t, UidEntry> uidTable_t;
51    uidTable_t uidTable[LOG_ID_MAX];
52
53public:
54    LogStatistics();
55
56    void enableStatistics() { }
57
58    void add(LogBufferElement *entry);
59    void subtract(LogBufferElement *entry);
60
61    // Caller must delete array
62    const UidEntry **sort(size_t n, log_id i);
63
64    // fast track current value by id only
65    size_t sizes(log_id_t id) const { return mSizes[id]; }
66    size_t elements(log_id_t id) const { return mElements[id]; }
67    size_t sizesTotal(log_id_t id) const { return mSizesTotal[id]; }
68    size_t elementsTotal(log_id_t id) const { return mElementsTotal[id]; }
69
70    // *strp = malloc, balance with free
71    void format(char **strp, uid_t uid, unsigned int logMask);
72
73    // helper
74    char *pidToName(pid_t pid);
75    uid_t pidToUid(pid_t pid);
76    char *uidToName(uid_t uid);
77};
78
79#endif // _LOGD_LOG_STATISTICS_H__
80