blob: 0243749f937281eaf017643a8a5ea87384ed2c26 [file] [log] [blame]
Mark Salyzyn0175b072014-02-26 09:50:16 -08001/*
2 * Copyright (C) 2012-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_BUFFER_H__
18#define _LOGD_LOG_BUFFER_H__
19
20#include <sys/types.h>
21
Mark Salyzyn94a89c42015-08-19 13:41:51 -070022#include <list>
Mark Salyzyn73160ac2015-08-20 10:01:44 -070023#include <string>
Mark Salyzyn94a89c42015-08-19 13:41:51 -070024
Mark Salyzyn0175b072014-02-26 09:50:16 -080025#include <log/log.h>
26#include <sysutils/SocketClient.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080027
Mark Salyzyn1a240b42014-06-12 11:16:16 -070028#include <private/android_filesystem_config.h>
29
Mark Salyzyn0175b072014-02-26 09:50:16 -080030#include "LogBufferElement.h"
31#include "LogTimes.h"
Mark Salyzyn34facab2014-02-06 14:48:50 -080032#include "LogStatistics.h"
Mark Salyzyndfa7a072014-02-11 12:29:31 -080033#include "LogWhiteBlackList.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080034
Mark Salyzynb6bee332015-09-08 08:56:32 -070035//
36// We are either in 1970ish (MONOTONIC) or 2015+ish (REALTIME) so to
37// differentiate without prejudice, we use 1980 to delineate, earlier
38// is monotonic, later is real.
39//
40namespace android {
41
42static bool isMonotonic(const log_time &mono) {
43 static const uint32_t EPOCH_PLUS_10_YEARS = 10 * 1461 / 4 * 24 * 60 * 60;
44
45 return mono.tv_sec < EPOCH_PLUS_10_YEARS;
46}
47
48}
49
Mark Salyzyn94a89c42015-08-19 13:41:51 -070050typedef std::list<LogBufferElement *> LogBufferElementCollection;
Mark Salyzyn0175b072014-02-26 09:50:16 -080051
52class LogBuffer {
53 LogBufferElementCollection mLogElements;
54 pthread_mutex_t mLogElementsLock;
55
Mark Salyzyn34facab2014-02-06 14:48:50 -080056 LogStatistics stats;
Mark Salyzyne457b742014-02-19 17:18:31 -080057
Mark Salyzyndfa7a072014-02-11 12:29:31 -080058 PruneList mPrune;
Mark Salyzync892ea32015-08-19 17:06:11 -070059 // watermark of any worst/chatty uid processing
60 typedef std::unordered_map<uid_t,
61 LogBufferElementCollection::iterator>
62 LogBufferIteratorMap;
63 LogBufferIteratorMap mLastWorstUid[LOG_ID_MAX];
Mark Salyzynbec3c3d2015-08-28 08:02:59 -070064 // watermark of any worst/chatty pid of system processing
65 typedef std::unordered_map<pid_t,
66 LogBufferElementCollection::iterator>
67 LogBufferPidIteratorMap;
68 LogBufferPidIteratorMap mLastWorstPidOfSystem[LOG_ID_MAX];
Mark Salyzyndfa7a072014-02-11 12:29:31 -080069
70 unsigned long mMaxSize[LOG_ID_MAX];
Mark Salyzyndfa7a072014-02-11 12:29:31 -080071
Mark Salyzynb6bee332015-09-08 08:56:32 -070072 bool monotonic;
73
Mark Salyzyn0175b072014-02-26 09:50:16 -080074public:
75 LastLogTimes &mTimes;
76
77 LogBuffer(LastLogTimes *times);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070078 void init();
Mark Salyzynb6bee332015-09-08 08:56:32 -070079 bool isMonotonic() { return monotonic; }
Mark Salyzyn0175b072014-02-26 09:50:16 -080080
Mark Salyzyn202e1532015-02-09 08:21:05 -080081 int log(log_id_t log_id, log_time realtime,
82 uid_t uid, pid_t pid, pid_t tid,
83 const char *msg, unsigned short len);
Mark Salyzynf7c0f752015-03-03 13:39:37 -080084 uint64_t flushTo(SocketClient *writer, const uint64_t start,
Mark Salyzyn7e2f83c2014-03-05 07:41:49 -080085 bool privileged,
Mark Salyzynf7c0f752015-03-03 13:39:37 -080086 int (*filter)(const LogBufferElement *element, void *arg) = NULL,
Mark Salyzyn7e2f83c2014-03-05 07:41:49 -080087 void *arg = NULL);
Mark Salyzyn0175b072014-02-26 09:50:16 -080088
Mark Salyzync5dc9702015-09-16 15:34:00 -070089 bool clear(log_id_t id, uid_t uid = AID_ROOT);
Mark Salyzyn0175b072014-02-26 09:50:16 -080090 unsigned long getSize(log_id_t id);
Mark Salyzyndfa7a072014-02-11 12:29:31 -080091 int setSize(log_id_t id, unsigned long size);
Mark Salyzyn0175b072014-02-26 09:50:16 -080092 unsigned long getSizeUsed(log_id_t id);
Mark Salyzyn34facab2014-02-06 14:48:50 -080093 // *strp uses malloc, use free to release.
Mark Salyzynee3b8382015-12-17 09:58:43 -080094 std::string formatStatistics(uid_t uid, pid_t pid, unsigned int logMask);
Mark Salyzyndfa7a072014-02-11 12:29:31 -080095
Mark Salyzynf5fc5092014-09-21 14:22:18 -070096 void enableStatistics() {
97 stats.enableStatistics();
98 }
99
Mark Salyzyn73160ac2015-08-20 10:01:44 -0700100 int initPrune(const char *cp) { return mPrune.init(cp); }
101 std::string formatPrune() { return mPrune.format(); }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800102
Mark Salyzyned777e92015-06-24 16:22:54 -0700103 // helper must be protected directly or implicitly by lock()/unlock()
Mark Salyzyn758058f2015-08-21 16:44:30 -0700104 const char *pidToName(pid_t pid) { return stats.pidToName(pid); }
Mark Salyzyn4ba03872014-04-07 07:15:33 -0700105 uid_t pidToUid(pid_t pid) { return stats.pidToUid(pid); }
Mark Salyzyn758058f2015-08-21 16:44:30 -0700106 const char *uidToName(uid_t uid) { return stats.uidToName(uid); }
Mark Salyzyned777e92015-06-24 16:22:54 -0700107 void lock() { pthread_mutex_lock(&mLogElementsLock); }
108 void unlock() { pthread_mutex_unlock(&mLogElementsLock); }
Mark Salyzyn9a038632014-04-07 07:05:40 -0700109
Mark Salyzyn0175b072014-02-26 09:50:16 -0800110private:
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700111
Mark Salyzyn58b8be82015-09-30 07:40:09 -0700112 static constexpr size_t minPrune = 4;
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700113 static constexpr size_t maxPrune = 256;
114
Mark Salyzyn0175b072014-02-26 09:50:16 -0800115 void maybePrune(log_id_t id);
Mark Salyzync5dc9702015-09-16 15:34:00 -0700116 bool prune(log_id_t id, unsigned long pruneRows, uid_t uid = AID_ROOT);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700117 LogBufferElementCollection::iterator erase(
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700118 LogBufferElementCollection::iterator it, bool coalesce = false);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800119};
120
Mark Salyzyn34facab2014-02-06 14:48:50 -0800121#endif // _LOGD_LOG_BUFFER_H__