blob: 783bce6758b34140cd4efd615ca6e474ad2ce5ef [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>
21#include <time.h>
22#include <sys/types.h>
23#include <sysutils/SocketClient.h>
24#include <utils/List.h>
TraianX Schiauda6495d2014-12-17 10:53:41 +020025#include <log/log.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080026
27class LogReader;
28
29class LogTimeEntry {
30 static pthread_mutex_t timesLock;
31 unsigned int mRefCount;
32 bool mRelease;
33 bool mError;
34 bool threadRunning;
Mark Salyzyn047cc072015-06-04 13:35:30 -070035 bool leadingDropped;
Mark Salyzyna16f7612014-08-07 08:16:52 -070036 pthread_cond_t threadTriggeredCondition;
Mark Salyzyn0175b072014-02-26 09:50:16 -080037 pthread_t mThread;
38 LogReader &mReader;
39 static void *threadStart(void *me);
40 static void threadStop(void *me);
41 const unsigned int mLogMask;
42 const pid_t mPid;
TraianX Schiauda6495d2014-12-17 10:53:41 +020043 unsigned int skipAhead[LOG_ID_MAX];
Mark Salyzyn0175b072014-02-26 09:50:16 -080044 unsigned long mCount;
45 unsigned long mTail;
46 unsigned long mIndex;
47
48public:
49 LogTimeEntry(LogReader &reader, SocketClient *client, bool nonBlock,
Mark Salyzynfa3716b2014-02-14 16:05:05 -080050 unsigned long tail, unsigned int logMask, pid_t pid,
Mark Salyzynf7c0f752015-03-03 13:39:37 -080051 uint64_t start);
Mark Salyzyn0175b072014-02-26 09:50:16 -080052
53 SocketClient *mClient;
Mark Salyzynf7c0f752015-03-03 13:39:37 -080054 uint64_t mStart;
Mark Salyzyn0175b072014-02-26 09:50:16 -080055 const bool mNonBlock;
Mark Salyzynf7c0f752015-03-03 13:39:37 -080056 const uint64_t mEnd; // only relevant if mNonBlock
Mark Salyzyn0175b072014-02-26 09:50:16 -080057
58 // Protect List manipulations
59 static void lock(void) { pthread_mutex_lock(&timesLock); }
60 static void unlock(void) { pthread_mutex_unlock(&timesLock); }
61
62 void startReader_Locked(void);
63
Mark Salyzync03e72c2014-02-18 11:23:53 -080064 bool runningReader_Locked(void) const {
Mark Salyzyn0175b072014-02-26 09:50:16 -080065 return threadRunning || mRelease || mError || mNonBlock;
66 }
Mark Salyzyna16f7612014-08-07 08:16:52 -070067 void triggerReader_Locked(void) {
68 pthread_cond_signal(&threadTriggeredCondition);
69 }
70
TraianX Schiauda6495d2014-12-17 10:53:41 +020071 void triggerSkip_Locked(log_id_t id, unsigned int skip) { skipAhead[id] = skip; }
72 void cleanSkip_Locked(void);
Mark Salyzyn0175b072014-02-26 09:50:16 -080073
74 // Called after LogTimeEntry removed from list, lock implicitly held
Mark Salyzync03e72c2014-02-18 11:23:53 -080075 void release_Locked(void) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080076 mRelease = true;
Mark Salyzyna16f7612014-08-07 08:16:52 -070077 pthread_cond_signal(&threadTriggeredCondition);
Mark Salyzyn0175b072014-02-26 09:50:16 -080078 if (mRefCount || threadRunning) {
79 return;
80 }
81 // No one else is holding a reference to this
82 delete this;
83 }
84
85 // Called to mark socket in jeopardy
86 void error_Locked(void) { mError = true; }
Mark Salyzyna16f7612014-08-07 08:16:52 -070087 void error(void) { lock(); error_Locked(); unlock(); }
Mark Salyzyn0175b072014-02-26 09:50:16 -080088
89 bool isError_Locked(void) const { return mRelease || mError; }
90
91 // Mark Used
92 // Locking implied, grabbed when protection around loop iteration
93 void incRef_Locked(void) { ++mRefCount; }
94
95 bool owned_Locked(void) const { return mRefCount != 0; }
96
Mark Salyzync03e72c2014-02-18 11:23:53 -080097 void decRef_Locked(void) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080098 if ((mRefCount && --mRefCount) || !mRelease || threadRunning) {
99 return;
100 }
101 // No one else is holding a reference to this
102 delete this;
103 }
TraianX Schiauda6495d2014-12-17 10:53:41 +0200104 bool isWatching(log_id_t id) { return (mLogMask & (1<<id)) != 0; }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800105 // flushTo filter callbacks
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800106 static int FilterFirstPass(const LogBufferElement *element, void *me);
107 static int FilterSecondPass(const LogBufferElement *element, void *me);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800108};
109
110typedef android::List<LogTimeEntry *> LastLogTimes;
111
112#endif