Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 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> |
Mark Salyzyn | 98dca2d | 2015-08-19 13:10:14 -0700 | [diff] [blame] | 23 | |
| 24 | #include <list> |
| 25 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 26 | #include <sysutils/SocketClient.h> |
TraianX Schiau | da6495d | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 27 | #include <log/log.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 28 | |
| 29 | class LogReader; |
| 30 | |
| 31 | class LogTimeEntry { |
| 32 | static pthread_mutex_t timesLock; |
| 33 | unsigned int mRefCount; |
| 34 | bool mRelease; |
| 35 | bool mError; |
| 36 | bool threadRunning; |
Mark Salyzyn | 047cc07 | 2015-06-04 13:35:30 -0700 | [diff] [blame] | 37 | bool leadingDropped; |
Mark Salyzyn | a16f761 | 2014-08-07 08:16:52 -0700 | [diff] [blame] | 38 | pthread_cond_t threadTriggeredCondition; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 39 | 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 Schiau | da6495d | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 45 | unsigned int skipAhead[LOG_ID_MAX]; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 46 | unsigned long mCount; |
| 47 | unsigned long mTail; |
| 48 | unsigned long mIndex; |
| 49 | |
| 50 | public: |
| 51 | LogTimeEntry(LogReader &reader, SocketClient *client, bool nonBlock, |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 52 | unsigned long tail, unsigned int logMask, pid_t pid, |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 53 | uint64_t start, uint64_t timeout); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 54 | |
| 55 | SocketClient *mClient; |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 56 | uint64_t mStart; |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 57 | struct timespec mTimeout; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 58 | const bool mNonBlock; |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 59 | const uint64_t mEnd; // only relevant if mNonBlock |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 60 | |
| 61 | // Protect List manipulations |
| 62 | static void lock(void) { pthread_mutex_lock(×Lock); } |
| 63 | static void unlock(void) { pthread_mutex_unlock(×Lock); } |
| 64 | |
| 65 | void startReader_Locked(void); |
| 66 | |
Mark Salyzyn | c03e72c | 2014-02-18 11:23:53 -0800 | [diff] [blame] | 67 | bool runningReader_Locked(void) const { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 68 | return threadRunning || mRelease || mError || mNonBlock; |
| 69 | } |
Mark Salyzyn | a16f761 | 2014-08-07 08:16:52 -0700 | [diff] [blame] | 70 | void triggerReader_Locked(void) { |
| 71 | pthread_cond_signal(&threadTriggeredCondition); |
| 72 | } |
| 73 | |
TraianX Schiau | da6495d | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 74 | void triggerSkip_Locked(log_id_t id, unsigned int skip) { skipAhead[id] = skip; } |
| 75 | void cleanSkip_Locked(void); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 76 | |
| 77 | // Called after LogTimeEntry removed from list, lock implicitly held |
Mark Salyzyn | c03e72c | 2014-02-18 11:23:53 -0800 | [diff] [blame] | 78 | void release_Locked(void) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 79 | mRelease = true; |
Mark Salyzyn | a16f761 | 2014-08-07 08:16:52 -0700 | [diff] [blame] | 80 | pthread_cond_signal(&threadTriggeredCondition); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 81 | if (mRefCount || threadRunning) { |
| 82 | return; |
| 83 | } |
| 84 | // No one else is holding a reference to this |
| 85 | delete this; |
| 86 | } |
| 87 | |
| 88 | // Called to mark socket in jeopardy |
| 89 | void error_Locked(void) { mError = true; } |
Mark Salyzyn | a16f761 | 2014-08-07 08:16:52 -0700 | [diff] [blame] | 90 | void error(void) { lock(); error_Locked(); unlock(); } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 91 | |
| 92 | bool isError_Locked(void) const { return mRelease || mError; } |
| 93 | |
| 94 | // Mark Used |
| 95 | // Locking implied, grabbed when protection around loop iteration |
| 96 | void incRef_Locked(void) { ++mRefCount; } |
| 97 | |
| 98 | bool owned_Locked(void) const { return mRefCount != 0; } |
| 99 | |
Mark Salyzyn | c03e72c | 2014-02-18 11:23:53 -0800 | [diff] [blame] | 100 | void decRef_Locked(void) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 101 | if ((mRefCount && --mRefCount) || !mRelease || threadRunning) { |
| 102 | return; |
| 103 | } |
| 104 | // No one else is holding a reference to this |
| 105 | delete this; |
| 106 | } |
TraianX Schiau | da6495d | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 107 | bool isWatching(log_id_t id) { return (mLogMask & (1<<id)) != 0; } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 108 | // flushTo filter callbacks |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 109 | static int FilterFirstPass(const LogBufferElement *element, void *me); |
| 110 | static int FilterSecondPass(const LogBufferElement *element, void *me); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 111 | }; |
| 112 | |
Mark Salyzyn | 98dca2d | 2015-08-19 13:10:14 -0700 | [diff] [blame] | 113 | typedef std::list<LogTimeEntry *> LastLogTimes; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 114 | |
Mark Salyzyn | 98dca2d | 2015-08-19 13:10:14 -0700 | [diff] [blame] | 115 | #endif // _LOGD_LOG_TIMES_H__ |