1 /*
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>
21 #include <time.h>
22 #include <sys/types.h>
23 
24 #include <list>
25 
26 #include <sysutils/SocketClient.h>
27 #include <log/log.h>
28 
29 class LogReader;
30 class LogBufferElement;
31 
32 class LogTimeEntry {
33     static pthread_mutex_t timesLock;
34     unsigned int mRefCount;
35     bool mRelease;
36     bool mError;
37     bool threadRunning;
38     bool leadingDropped;
39     pthread_cond_t threadTriggeredCondition;
40     pthread_t mThread;
41     LogReader &mReader;
42     static void *threadStart(void *me);
43     static void threadStop(void *me);
44     const unsigned int mLogMask;
45     const pid_t mPid;
46     unsigned int skipAhead[LOG_ID_MAX];
47     unsigned long mCount;
48     unsigned long mTail;
49     unsigned long mIndex;
50 
51 public:
52     LogTimeEntry(LogReader &reader, SocketClient *client, bool nonBlock,
53                  unsigned long tail, unsigned int logMask, pid_t pid,
54                  uint64_t start, uint64_t timeout);
55 
56     SocketClient *mClient;
57     uint64_t mStart;
58     struct timespec mTimeout;
59     const bool mNonBlock;
60     const uint64_t mEnd; // only relevant if mNonBlock
61 
62     // Protect List manipulations
lock(void)63     static void lock(void) { pthread_mutex_lock(&timesLock); }
unlock(void)64     static void unlock(void) { pthread_mutex_unlock(&timesLock); }
65 
66     void startReader_Locked(void);
67 
runningReader_Locked(void)68     bool runningReader_Locked(void) const {
69         return threadRunning || mRelease || mError || mNonBlock;
70     }
triggerReader_Locked(void)71     void triggerReader_Locked(void) {
72         pthread_cond_signal(&threadTriggeredCondition);
73     }
74 
triggerSkip_Locked(log_id_t id,unsigned int skip)75     void triggerSkip_Locked(log_id_t id, unsigned int skip) { skipAhead[id] = skip; }
76     void cleanSkip_Locked(void);
77 
78     // These called after LogTimeEntry removed from list, lock implicitly held
release_nodelete_Locked(void)79     void release_nodelete_Locked(void) {
80         mRelease = true;
81         pthread_cond_signal(&threadTriggeredCondition);
82         // assumes caller code path will call decRef_Locked()
83     }
84 
release_Locked(void)85     void release_Locked(void) {
86         mRelease = true;
87         pthread_cond_signal(&threadTriggeredCondition);
88         if (mRefCount || threadRunning) {
89             return;
90         }
91         // No one else is holding a reference to this
92         delete this;
93     }
94 
95     // Called to mark socket in jeopardy
error_Locked(void)96     void error_Locked(void) { mError = true; }
error(void)97     void error(void) { lock(); error_Locked(); unlock(); }
98 
isError_Locked(void)99     bool isError_Locked(void) const { return mRelease || mError; }
100 
101     // Mark Used
102     //  Locking implied, grabbed when protection around loop iteration
incRef_Locked(void)103     void incRef_Locked(void) { ++mRefCount; }
104 
owned_Locked(void)105     bool owned_Locked(void) const { return mRefCount != 0; }
106 
decRef_Locked(void)107     void decRef_Locked(void) {
108         if ((mRefCount && --mRefCount) || !mRelease || threadRunning) {
109             return;
110         }
111         // No one else is holding a reference to this
112         delete this;
113     }
isWatching(log_id_t id)114     bool isWatching(log_id_t id) { return (mLogMask & (1<<id)) != 0; }
115     // flushTo filter callbacks
116     static int FilterFirstPass(const LogBufferElement *element, void *me);
117     static int FilterSecondPass(const LogBufferElement *element, void *me);
118 };
119 
120 typedef std::list<LogTimeEntry *> LastLogTimes;
121 
122 #endif // _LOGD_LOG_TIMES_H__
123