blob: 9a3ddab3995153ff3c87678775c74a5fdeb0e8be [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
29class LogReader;
Mark Salyzyn317bfb92016-02-23 08:55:43 -080030class LogBufferElement;
Mark Salyzyn0175b072014-02-26 09:50:16 -080031
32class LogTimeEntry {
33 static pthread_mutex_t timesLock;
34 unsigned int mRefCount;
35 bool mRelease;
36 bool mError;
37 bool threadRunning;
Mark Salyzyn047cc072015-06-04 13:35:30 -070038 bool leadingDropped;
Mark Salyzyna16f7612014-08-07 08:16:52 -070039 pthread_cond_t threadTriggeredCondition;
Mark Salyzyn0175b072014-02-26 09:50:16 -080040 pthread_t mThread;
Mark Salyzyn501c3732017-03-10 14:31:54 -080041 LogReader& mReader;
42 static void* threadStart(void* me);
43 static void threadStop(void* me);
Mark Salyzyn0175b072014-02-26 09:50:16 -080044 const unsigned int mLogMask;
45 const pid_t mPid;
TraianX Schiauda6495d2014-12-17 10:53:41 +020046 unsigned int skipAhead[LOG_ID_MAX];
Mark Salyzyn0175b072014-02-26 09:50:16 -080047 unsigned long mCount;
48 unsigned long mTail;
49 unsigned long mIndex;
50
Mark Salyzyn501c3732017-03-10 14:31:54 -080051 public:
52 LogTimeEntry(LogReader& reader, SocketClient* client, bool nonBlock,
Mark Salyzynfa3716b2014-02-14 16:05:05 -080053 unsigned long tail, unsigned int logMask, pid_t pid,
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -080054 log_time start, uint64_t timeout);
Mark Salyzyn0175b072014-02-26 09:50:16 -080055
Mark Salyzyn501c3732017-03-10 14:31:54 -080056 SocketClient* mClient;
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -080057 log_time mStart;
Mark Salyzynb75cce02015-11-30 11:35:56 -080058 struct timespec mTimeout;
Mark Salyzyn0175b072014-02-26 09:50:16 -080059 const bool mNonBlock;
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -080060 const log_time mEnd; // only relevant if mNonBlock
Mark Salyzyn0175b072014-02-26 09:50:16 -080061
62 // Protect List manipulations
Mark Salyzyn501c3732017-03-10 14:31:54 -080063 static void lock(void) {
64 pthread_mutex_lock(&timesLock);
65 }
66 static void unlock(void) {
67 pthread_mutex_unlock(&timesLock);
68 }
Mark Salyzyn0175b072014-02-26 09:50:16 -080069
70 void startReader_Locked(void);
71
Mark Salyzync03e72c2014-02-18 11:23:53 -080072 bool runningReader_Locked(void) const {
Mark Salyzyn0175b072014-02-26 09:50:16 -080073 return threadRunning || mRelease || mError || mNonBlock;
74 }
Mark Salyzyna16f7612014-08-07 08:16:52 -070075 void triggerReader_Locked(void) {
76 pthread_cond_signal(&threadTriggeredCondition);
77 }
78
Mark Salyzyn501c3732017-03-10 14:31:54 -080079 void triggerSkip_Locked(log_id_t id, unsigned int skip) {
80 skipAhead[id] = skip;
81 }
TraianX Schiauda6495d2014-12-17 10:53:41 +020082 void cleanSkip_Locked(void);
Mark Salyzyn0175b072014-02-26 09:50:16 -080083
Mark Salyzync3484162016-03-01 14:59:32 -080084 // These called after LogTimeEntry removed from list, lock implicitly held
85 void release_nodelete_Locked(void) {
86 mRelease = true;
87 pthread_cond_signal(&threadTriggeredCondition);
88 // assumes caller code path will call decRef_Locked()
89 }
90
Mark Salyzync03e72c2014-02-18 11:23:53 -080091 void release_Locked(void) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080092 mRelease = true;
Mark Salyzyna16f7612014-08-07 08:16:52 -070093 pthread_cond_signal(&threadTriggeredCondition);
Mark Salyzyn0175b072014-02-26 09:50:16 -080094 if (mRefCount || threadRunning) {
95 return;
96 }
97 // No one else is holding a reference to this
98 delete this;
99 }
100
101 // Called to mark socket in jeopardy
Mark Salyzyn501c3732017-03-10 14:31:54 -0800102 void error_Locked(void) {
103 mError = true;
104 }
105 void error(void) {
106 lock();
107 error_Locked();
108 unlock();
109 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800110
Mark Salyzyn501c3732017-03-10 14:31:54 -0800111 bool isError_Locked(void) const {
112 return mRelease || mError;
113 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800114
115 // Mark Used
116 // Locking implied, grabbed when protection around loop iteration
Mark Salyzyn501c3732017-03-10 14:31:54 -0800117 void incRef_Locked(void) {
118 ++mRefCount;
119 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800120
Mark Salyzyn501c3732017-03-10 14:31:54 -0800121 bool owned_Locked(void) const {
122 return mRefCount != 0;
123 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800124
Mark Salyzync03e72c2014-02-18 11:23:53 -0800125 void decRef_Locked(void) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800126 if ((mRefCount && --mRefCount) || !mRelease || threadRunning) {
127 return;
128 }
129 // No one else is holding a reference to this
130 delete this;
131 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800132 bool isWatching(log_id_t id) {
133 return (mLogMask & (1 << id)) != 0;
134 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800135 // flushTo filter callbacks
Mark Salyzyn501c3732017-03-10 14:31:54 -0800136 static int FilterFirstPass(const LogBufferElement* element, void* me);
137 static int FilterSecondPass(const LogBufferElement* element, void* me);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800138};
139
Mark Salyzyn501c3732017-03-10 14:31:54 -0800140typedef std::list<LogTimeEntry*> LastLogTimes;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800141
Mark Salyzyn501c3732017-03-10 14:31:54 -0800142#endif // _LOGD_LOG_TIMES_H__