blob: 76d016c13be50c7edb7e036eabbdafe723ee0c92 [file] [log] [blame]
Mark Salyzyn0175b072014-02-26 09:50:16 -08001/*
2 * Copyright (C) 2012-2013 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_TIMES_H__
18#define _LOGD_LOG_TIMES_H__
19
20#include <pthread.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080021#include <sys/types.h>
Mark Salyzyn501c3732017-03-10 14:31:54 -080022#include <time.h>
Mark Salyzyn98dca2d2015-08-19 13:10:14 -070023
24#include <list>
25
Mark Salyzynaeaaf812016-09-30 13:30:33 -070026#include <log/log.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080027#include <sysutils/SocketClient.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080028
Hao Wangf6e22962017-12-04 14:10:40 +080029typedef unsigned int log_mask_t;
30
Mark Salyzyn0175b072014-02-26 09:50:16 -080031class LogReader;
Mark Salyzyn317bfb92016-02-23 08:55:43 -080032class LogBufferElement;
Mark Salyzyn0175b072014-02-26 09:50:16 -080033
34class LogTimeEntry {
35 static pthread_mutex_t timesLock;
36 unsigned int mRefCount;
37 bool mRelease;
38 bool mError;
39 bool threadRunning;
Mark Salyzyn047cc072015-06-04 13:35:30 -070040 bool leadingDropped;
Mark Salyzyna16f7612014-08-07 08:16:52 -070041 pthread_cond_t threadTriggeredCondition;
Mark Salyzyn0175b072014-02-26 09:50:16 -080042 pthread_t mThread;
Mark Salyzyn501c3732017-03-10 14:31:54 -080043 LogReader& mReader;
44 static void* threadStart(void* me);
45 static void threadStop(void* me);
Hao Wangf6e22962017-12-04 14:10:40 +080046 const log_mask_t mLogMask;
Mark Salyzyn0175b072014-02-26 09:50:16 -080047 const pid_t mPid;
TraianX Schiauda6495d2014-12-17 10:53:41 +020048 unsigned int skipAhead[LOG_ID_MAX];
Mark Salyzynae2abf12017-03-31 10:48:39 -070049 pid_t mLastTid[LOG_ID_MAX];
Mark Salyzyn0175b072014-02-26 09:50:16 -080050 unsigned long mCount;
51 unsigned long mTail;
52 unsigned long mIndex;
53
Mark Salyzyn501c3732017-03-10 14:31:54 -080054 public:
55 LogTimeEntry(LogReader& reader, SocketClient* client, bool nonBlock,
Hao Wangf6e22962017-12-04 14:10:40 +080056 unsigned long tail, log_mask_t logMask, pid_t pid,
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -080057 log_time start, uint64_t timeout);
Mark Salyzyn0175b072014-02-26 09:50:16 -080058
Mark Salyzyn501c3732017-03-10 14:31:54 -080059 SocketClient* mClient;
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -080060 log_time mStart;
Mark Salyzynb75cce02015-11-30 11:35:56 -080061 struct timespec mTimeout;
Mark Salyzyn0175b072014-02-26 09:50:16 -080062 const bool mNonBlock;
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -080063 const log_time mEnd; // only relevant if mNonBlock
Mark Salyzyn0175b072014-02-26 09:50:16 -080064
65 // Protect List manipulations
Mark Salyzyn3c501b52017-04-18 14:09:45 -070066 static void wrlock(void) {
67 pthread_mutex_lock(&timesLock);
68 }
69 static void rdlock(void) {
Mark Salyzyn501c3732017-03-10 14:31:54 -080070 pthread_mutex_lock(&timesLock);
71 }
72 static void unlock(void) {
73 pthread_mutex_unlock(&timesLock);
74 }
Mark Salyzyn0175b072014-02-26 09:50:16 -080075
76 void startReader_Locked(void);
77
Mark Salyzync03e72c2014-02-18 11:23:53 -080078 bool runningReader_Locked(void) const {
Mark Salyzyn0175b072014-02-26 09:50:16 -080079 return threadRunning || mRelease || mError || mNonBlock;
80 }
Mark Salyzyna16f7612014-08-07 08:16:52 -070081 void triggerReader_Locked(void) {
82 pthread_cond_signal(&threadTriggeredCondition);
83 }
84
Mark Salyzyn501c3732017-03-10 14:31:54 -080085 void triggerSkip_Locked(log_id_t id, unsigned int skip) {
86 skipAhead[id] = skip;
87 }
TraianX Schiauda6495d2014-12-17 10:53:41 +020088 void cleanSkip_Locked(void);
Mark Salyzyn0175b072014-02-26 09:50:16 -080089
Mark Salyzync3484162016-03-01 14:59:32 -080090 // These called after LogTimeEntry removed from list, lock implicitly held
91 void release_nodelete_Locked(void) {
92 mRelease = true;
93 pthread_cond_signal(&threadTriggeredCondition);
94 // assumes caller code path will call decRef_Locked()
95 }
96
Mark Salyzync03e72c2014-02-18 11:23:53 -080097 void release_Locked(void) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080098 mRelease = true;
Mark Salyzyna16f7612014-08-07 08:16:52 -070099 pthread_cond_signal(&threadTriggeredCondition);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800100 if (mRefCount || threadRunning) {
101 return;
102 }
103 // No one else is holding a reference to this
104 delete this;
105 }
106
107 // Called to mark socket in jeopardy
Mark Salyzyn501c3732017-03-10 14:31:54 -0800108 void error_Locked(void) {
109 mError = true;
110 }
111 void error(void) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700112 wrlock();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800113 error_Locked();
114 unlock();
115 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800116
Mark Salyzyn501c3732017-03-10 14:31:54 -0800117 bool isError_Locked(void) const {
118 return mRelease || mError;
119 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800120
121 // Mark Used
122 // Locking implied, grabbed when protection around loop iteration
Mark Salyzyn501c3732017-03-10 14:31:54 -0800123 void incRef_Locked(void) {
124 ++mRefCount;
125 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800126
Mark Salyzyn501c3732017-03-10 14:31:54 -0800127 bool owned_Locked(void) const {
128 return mRefCount != 0;
129 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800130
Mark Salyzync03e72c2014-02-18 11:23:53 -0800131 void decRef_Locked(void) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800132 if ((mRefCount && --mRefCount) || !mRelease || threadRunning) {
133 return;
134 }
135 // No one else is holding a reference to this
136 delete this;
137 }
Hao Wangf6e22962017-12-04 14:10:40 +0800138 bool isWatching(log_id_t id) const {
139 return mLogMask & (1 << id);
140 }
141 bool isWatchingMultiple(log_mask_t logMask) const {
142 return mLogMask & logMask;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800143 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800144 // flushTo filter callbacks
Mark Salyzyn501c3732017-03-10 14:31:54 -0800145 static int FilterFirstPass(const LogBufferElement* element, void* me);
146 static int FilterSecondPass(const LogBufferElement* element, void* me);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800147};
148
Mark Salyzyn501c3732017-03-10 14:31:54 -0800149typedef std::list<LogTimeEntry*> LastLogTimes;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800150
Mark Salyzyn501c3732017-03-10 14:31:54 -0800151#endif // _LOGD_LOG_TIMES_H__