blob: 39bcdd4e6322e9e8397b9b154cd7f6f07564eb89 [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>
Mark Salyzyn98dca2d2015-08-19 13:10:14 -070023
24#include <list>
25
Mark Salyzyn0175b072014-02-26 09:50:16 -080026#include <sysutils/SocketClient.h>
TraianX Schiauda6495d2014-12-17 10:53:41 +020027#include <log/log.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080028
29class LogReader;
30
31class LogTimeEntry {
32 static pthread_mutex_t timesLock;
33 unsigned int mRefCount;
34 bool mRelease;
35 bool mError;
36 bool threadRunning;
Mark Salyzyn047cc072015-06-04 13:35:30 -070037 bool leadingDropped;
Mark Salyzyna16f7612014-08-07 08:16:52 -070038 pthread_cond_t threadTriggeredCondition;
Mark Salyzyn0175b072014-02-26 09:50:16 -080039 pthread_t mThread;
40 LogReader &mReader;
41 static void *threadStart(void *me);
42 static void threadStop(void *me);
43 const unsigned int mLogMask;
44 const pid_t mPid;
TraianX Schiauda6495d2014-12-17 10:53:41 +020045 unsigned int skipAhead[LOG_ID_MAX];
Mark Salyzyn0175b072014-02-26 09:50:16 -080046 unsigned long mCount;
47 unsigned long mTail;
48 unsigned long mIndex;
49
50public:
51 LogTimeEntry(LogReader &reader, SocketClient *client, bool nonBlock,
Mark Salyzynfa3716b2014-02-14 16:05:05 -080052 unsigned long tail, unsigned int logMask, pid_t pid,
Mark Salyzynf7c0f752015-03-03 13:39:37 -080053 uint64_t start);
Mark Salyzyn0175b072014-02-26 09:50:16 -080054
55 SocketClient *mClient;
Mark Salyzynf7c0f752015-03-03 13:39:37 -080056 uint64_t mStart;
Mark Salyzyn0175b072014-02-26 09:50:16 -080057 const bool mNonBlock;
Mark Salyzynf7c0f752015-03-03 13:39:37 -080058 const uint64_t mEnd; // only relevant if mNonBlock
Mark Salyzyn0175b072014-02-26 09:50:16 -080059
60 // Protect List manipulations
61 static void lock(void) { pthread_mutex_lock(&timesLock); }
62 static void unlock(void) { pthread_mutex_unlock(&timesLock); }
63
64 void startReader_Locked(void);
65
Mark Salyzync03e72c2014-02-18 11:23:53 -080066 bool runningReader_Locked(void) const {
Mark Salyzyn0175b072014-02-26 09:50:16 -080067 return threadRunning || mRelease || mError || mNonBlock;
68 }
Mark Salyzyna16f7612014-08-07 08:16:52 -070069 void triggerReader_Locked(void) {
70 pthread_cond_signal(&threadTriggeredCondition);
71 }
72
TraianX Schiauda6495d2014-12-17 10:53:41 +020073 void triggerSkip_Locked(log_id_t id, unsigned int skip) { skipAhead[id] = skip; }
74 void cleanSkip_Locked(void);
Mark Salyzyn0175b072014-02-26 09:50:16 -080075
76 // Called after LogTimeEntry removed from list, lock implicitly held
Mark Salyzync03e72c2014-02-18 11:23:53 -080077 void release_Locked(void) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080078 mRelease = true;
Mark Salyzyna16f7612014-08-07 08:16:52 -070079 pthread_cond_signal(&threadTriggeredCondition);
Mark Salyzyn0175b072014-02-26 09:50:16 -080080 if (mRefCount || threadRunning) {
81 return;
82 }
83 // No one else is holding a reference to this
84 delete this;
85 }
86
87 // Called to mark socket in jeopardy
88 void error_Locked(void) { mError = true; }
Mark Salyzyna16f7612014-08-07 08:16:52 -070089 void error(void) { lock(); error_Locked(); unlock(); }
Mark Salyzyn0175b072014-02-26 09:50:16 -080090
91 bool isError_Locked(void) const { return mRelease || mError; }
92
93 // Mark Used
94 // Locking implied, grabbed when protection around loop iteration
95 void incRef_Locked(void) { ++mRefCount; }
96
97 bool owned_Locked(void) const { return mRefCount != 0; }
98
Mark Salyzync03e72c2014-02-18 11:23:53 -080099 void decRef_Locked(void) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800100 if ((mRefCount && --mRefCount) || !mRelease || threadRunning) {
101 return;
102 }
103 // No one else is holding a reference to this
104 delete this;
105 }
TraianX Schiauda6495d2014-12-17 10:53:41 +0200106 bool isWatching(log_id_t id) { return (mLogMask & (1<<id)) != 0; }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800107 // flushTo filter callbacks
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800108 static int FilterFirstPass(const LogBufferElement *element, void *me);
109 static int FilterSecondPass(const LogBufferElement *element, void *me);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800110};
111
Mark Salyzyn98dca2d2015-08-19 13:10:14 -0700112typedef std::list<LogTimeEntry *> LastLogTimes;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800113
Mark Salyzyn98dca2d2015-08-19 13:10:14 -0700114#endif // _LOGD_LOG_TIMES_H__