blob: 9f6f20335c32821fde00de9273836332f94d8d37 [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>
Jintao Zhud3987a92018-12-20 23:10:41 +080021#include <sys/socket.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080022#include <sys/types.h>
Mark Salyzyn501c3732017-03-10 14:31:54 -080023#include <time.h>
Mark Salyzyn98dca2d2015-08-19 13:10:14 -070024
25#include <list>
Tom Cherry4f227862018-10-08 17:33:50 -070026#include <memory>
Mark Salyzyn98dca2d2015-08-19 13:10:14 -070027
Mark Salyzynaeaaf812016-09-30 13:30:33 -070028#include <log/log.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080029#include <sysutils/SocketClient.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080030
Hao Wangf6e22962017-12-04 14:10:40 +080031typedef unsigned int log_mask_t;
32
Mark Salyzyn0175b072014-02-26 09:50:16 -080033class LogReader;
Mark Salyzyn317bfb92016-02-23 08:55:43 -080034class LogBufferElement;
Mark Salyzyn0175b072014-02-26 09:50:16 -080035
36class LogTimeEntry {
37 static pthread_mutex_t timesLock;
Tom Cherry4f227862018-10-08 17:33:50 -070038 bool mRelease = false;
Mark Salyzyn047cc072015-06-04 13:35:30 -070039 bool leadingDropped;
Mark Salyzyna16f7612014-08-07 08:16:52 -070040 pthread_cond_t threadTriggeredCondition;
Mark Salyzyn0175b072014-02-26 09:50:16 -080041 pthread_t mThread;
Mark Salyzyn501c3732017-03-10 14:31:54 -080042 LogReader& mReader;
43 static void* threadStart(void* me);
Hao Wangf6e22962017-12-04 14:10:40 +080044 const log_mask_t mLogMask;
Mark Salyzyn0175b072014-02-26 09:50:16 -080045 const pid_t mPid;
TraianX Schiauda6495d2014-12-17 10:53:41 +020046 unsigned int skipAhead[LOG_ID_MAX];
Mark Salyzynae2abf12017-03-31 10:48:39 -070047 pid_t mLastTid[LOG_ID_MAX];
Mark Salyzyn0175b072014-02-26 09:50:16 -080048 unsigned long mCount;
49 unsigned long mTail;
50 unsigned long mIndex;
51
Mark Salyzyn501c3732017-03-10 14:31:54 -080052 public:
53 LogTimeEntry(LogReader& reader, SocketClient* client, bool nonBlock,
Hao Wangf6e22962017-12-04 14:10:40 +080054 unsigned long tail, log_mask_t logMask, pid_t pid,
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -080055 log_time start, uint64_t timeout);
Mark Salyzyn0175b072014-02-26 09:50:16 -080056
Mark Salyzyn501c3732017-03-10 14:31:54 -080057 SocketClient* mClient;
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -080058 log_time mStart;
Mark Salyzynb75cce02015-11-30 11:35:56 -080059 struct timespec mTimeout;
Mark Salyzyn0175b072014-02-26 09:50:16 -080060 const bool mNonBlock;
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -080061 const log_time mEnd; // only relevant if mNonBlock
Mark Salyzyn0175b072014-02-26 09:50:16 -080062
63 // Protect List manipulations
Mark Salyzyn3c501b52017-04-18 14:09:45 -070064 static void wrlock(void) {
65 pthread_mutex_lock(&timesLock);
66 }
67 static void rdlock(void) {
Mark Salyzyn501c3732017-03-10 14:31:54 -080068 pthread_mutex_lock(&timesLock);
69 }
70 static void unlock(void) {
71 pthread_mutex_unlock(&timesLock);
72 }
Mark Salyzyn0175b072014-02-26 09:50:16 -080073
Tom Cherry4f227862018-10-08 17:33:50 -070074 bool startReader_Locked();
Mark Salyzyn0175b072014-02-26 09:50:16 -080075
Mark Salyzyna16f7612014-08-07 08:16:52 -070076 void triggerReader_Locked(void) {
77 pthread_cond_signal(&threadTriggeredCondition);
78 }
79
Mark Salyzyn501c3732017-03-10 14:31:54 -080080 void triggerSkip_Locked(log_id_t id, unsigned int skip) {
81 skipAhead[id] = skip;
82 }
TraianX Schiauda6495d2014-12-17 10:53:41 +020083 void cleanSkip_Locked(void);
Mark Salyzyn0175b072014-02-26 09:50:16 -080084
Mark Salyzync03e72c2014-02-18 11:23:53 -080085 void release_Locked(void) {
Jintao Zhud3987a92018-12-20 23:10:41 +080086 // gracefully shut down the socket.
87 shutdown(mClient->getSocket(), SHUT_RDWR);
Mark Salyzyn0175b072014-02-26 09:50:16 -080088 mRelease = true;
Mark Salyzyna16f7612014-08-07 08:16:52 -070089 pthread_cond_signal(&threadTriggeredCondition);
Mark Salyzyn0175b072014-02-26 09:50:16 -080090 }
91
Hao Wangf6e22962017-12-04 14:10:40 +080092 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 Salyzyn501c3732017-03-10 14:31:54 -080097 }
Mark Salyzyn0175b072014-02-26 09:50:16 -080098 // flushTo filter callbacks
Mark Salyzyn501c3732017-03-10 14:31:54 -080099 static int FilterFirstPass(const LogBufferElement* element, void* me);
100 static int FilterSecondPass(const LogBufferElement* element, void* me);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800101};
102
Tom Cherry4f227862018-10-08 17:33:50 -0700103typedef std::list<std::unique_ptr<LogTimeEntry>> LastLogTimes;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800104
Mark Salyzyn501c3732017-03-10 14:31:54 -0800105#endif // _LOGD_LOG_TIMES_H__