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> |
Jintao Zhu | d3987a9 | 2018-12-20 23:10:41 +0800 | [diff] [blame] | 21 | #include <sys/socket.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 22 | #include <sys/types.h> |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 23 | #include <time.h> |
Mark Salyzyn | 98dca2d | 2015-08-19 13:10:14 -0700 | [diff] [blame] | 24 | |
| 25 | #include <list> |
Tom Cherry | 4f22786 | 2018-10-08 17:33:50 -0700 | [diff] [blame] | 26 | #include <memory> |
Mark Salyzyn | 98dca2d | 2015-08-19 13:10:14 -0700 | [diff] [blame] | 27 | |
Mark Salyzyn | aeaaf81 | 2016-09-30 13:30:33 -0700 | [diff] [blame] | 28 | #include <log/log.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 29 | #include <sysutils/SocketClient.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 30 | |
Hao Wang | f6e2296 | 2017-12-04 14:10:40 +0800 | [diff] [blame] | 31 | typedef unsigned int log_mask_t; |
| 32 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 33 | class LogReader; |
Mark Salyzyn | 317bfb9 | 2016-02-23 08:55:43 -0800 | [diff] [blame] | 34 | class LogBufferElement; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 35 | |
| 36 | class LogTimeEntry { |
| 37 | static pthread_mutex_t timesLock; |
Tom Cherry | 4f22786 | 2018-10-08 17:33:50 -0700 | [diff] [blame] | 38 | bool mRelease = false; |
Mark Salyzyn | 047cc07 | 2015-06-04 13:35:30 -0700 | [diff] [blame] | 39 | bool leadingDropped; |
Mark Salyzyn | a16f761 | 2014-08-07 08:16:52 -0700 | [diff] [blame] | 40 | pthread_cond_t threadTriggeredCondition; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 41 | pthread_t mThread; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 42 | LogReader& mReader; |
| 43 | static void* threadStart(void* me); |
Hao Wang | f6e2296 | 2017-12-04 14:10:40 +0800 | [diff] [blame] | 44 | const log_mask_t mLogMask; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 45 | const pid_t mPid; |
TraianX Schiau | da6495d | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 46 | unsigned int skipAhead[LOG_ID_MAX]; |
Mark Salyzyn | ae2abf1 | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 47 | pid_t mLastTid[LOG_ID_MAX]; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 48 | unsigned long mCount; |
| 49 | unsigned long mTail; |
| 50 | unsigned long mIndex; |
| 51 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 52 | public: |
| 53 | LogTimeEntry(LogReader& reader, SocketClient* client, bool nonBlock, |
Hao Wang | f6e2296 | 2017-12-04 14:10:40 +0800 | [diff] [blame] | 54 | unsigned long tail, log_mask_t logMask, pid_t pid, |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 55 | log_time start, uint64_t timeout); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 56 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 57 | SocketClient* mClient; |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 58 | log_time mStart; |
Mark Salyzyn | b75cce0 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 59 | struct timespec mTimeout; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 60 | const bool mNonBlock; |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 61 | const log_time mEnd; // only relevant if mNonBlock |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 62 | |
| 63 | // Protect List manipulations |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 64 | static void wrlock(void) { |
| 65 | pthread_mutex_lock(×Lock); |
| 66 | } |
| 67 | static void rdlock(void) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 68 | pthread_mutex_lock(×Lock); |
| 69 | } |
| 70 | static void unlock(void) { |
| 71 | pthread_mutex_unlock(×Lock); |
| 72 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 73 | |
Tom Cherry | 4f22786 | 2018-10-08 17:33:50 -0700 | [diff] [blame] | 74 | bool startReader_Locked(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 75 | |
Mark Salyzyn | a16f761 | 2014-08-07 08:16:52 -0700 | [diff] [blame] | 76 | void triggerReader_Locked(void) { |
| 77 | pthread_cond_signal(&threadTriggeredCondition); |
| 78 | } |
| 79 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 80 | void triggerSkip_Locked(log_id_t id, unsigned int skip) { |
| 81 | skipAhead[id] = skip; |
| 82 | } |
TraianX Schiau | da6495d | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 83 | void cleanSkip_Locked(void); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 84 | |
Mark Salyzyn | c03e72c | 2014-02-18 11:23:53 -0800 | [diff] [blame] | 85 | void release_Locked(void) { |
Jintao Zhu | d3987a9 | 2018-12-20 23:10:41 +0800 | [diff] [blame] | 86 | // gracefully shut down the socket. |
| 87 | shutdown(mClient->getSocket(), SHUT_RDWR); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 88 | mRelease = true; |
Mark Salyzyn | a16f761 | 2014-08-07 08:16:52 -0700 | [diff] [blame] | 89 | pthread_cond_signal(&threadTriggeredCondition); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Hao Wang | f6e2296 | 2017-12-04 14:10:40 +0800 | [diff] [blame] | 92 | bool isWatching(log_id_t id) const { |
| 93 | return mLogMask & (1 << id); |
| 94 | } |
| 95 | bool isWatchingMultiple(log_mask_t logMask) const { |
| 96 | return mLogMask & logMask; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 97 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 98 | // flushTo filter callbacks |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 99 | static int FilterFirstPass(const LogBufferElement* element, void* me); |
| 100 | static int FilterSecondPass(const LogBufferElement* element, void* me); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 101 | }; |
| 102 | |
Tom Cherry | 4f22786 | 2018-10-08 17:33:50 -0700 | [diff] [blame] | 103 | typedef std::list<std::unique_ptr<LogTimeEntry>> LastLogTimes; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 104 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 105 | #endif // _LOGD_LOG_TIMES_H__ |