blob: 1b60b7ead469e5885cc892e9b964fece43a686ab [file] [log] [blame]
Mark Salyzyn0175b072014-02-26 09:50:16 -08001/*
2 * Copyright (C) 2014 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
Mark Salyzyn8daa9af2014-04-28 14:07:23 -070017#include <sys/prctl.h>
18
Mark Salyzyn0175b072014-02-26 09:50:16 -080019#include "FlushCommand.h"
20#include "LogBuffer.h"
21#include "LogTimes.h"
22#include "LogReader.h"
23
24pthread_mutex_t LogTimeEntry::timesLock = PTHREAD_MUTEX_INITIALIZER;
25
Mark Salyzyn0175b072014-02-26 09:50:16 -080026LogTimeEntry::LogTimeEntry(LogReader &reader, SocketClient *client,
27 bool nonBlock, unsigned long tail,
Mark Salyzynfa3716b2014-02-14 16:05:05 -080028 unsigned int logMask, pid_t pid,
Mark Salyzynf7c0f752015-03-03 13:39:37 -080029 uint64_t start)
Mark Salyzyn0175b072014-02-26 09:50:16 -080030 : mRefCount(1)
31 , mRelease(false)
32 , mError(false)
33 , threadRunning(false)
Mark Salyzyn0175b072014-02-26 09:50:16 -080034 , mReader(reader)
35 , mLogMask(logMask)
36 , mPid(pid)
Mark Salyzyn0175b072014-02-26 09:50:16 -080037 , mCount(0)
38 , mTail(tail)
39 , mIndex(0)
40 , mClient(client)
Mark Salyzynfa3716b2014-02-14 16:05:05 -080041 , mStart(start)
Mark Salyzyn0175b072014-02-26 09:50:16 -080042 , mNonBlock(nonBlock)
Mark Salyzynf7c0f752015-03-03 13:39:37 -080043 , mEnd(LogBufferElement::getCurrentSequence())
Mark Salyzyna16f7612014-08-07 08:16:52 -070044{
45 pthread_cond_init(&threadTriggeredCondition, NULL);
TraianX Schiauda6495d2014-12-17 10:53:41 +020046 cleanSkip_Locked();
Mark Salyzyna16f7612014-08-07 08:16:52 -070047}
Mark Salyzyn0175b072014-02-26 09:50:16 -080048
49void LogTimeEntry::startReader_Locked(void) {
Mark Salyzyn765f7822014-04-04 12:47:44 -070050 pthread_attr_t attr;
51
Mark Salyzyn0175b072014-02-26 09:50:16 -080052 threadRunning = true;
Mark Salyzyn765f7822014-04-04 12:47:44 -070053
54 if (!pthread_attr_init(&attr)) {
55 if (!pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) {
56 if (!pthread_create(&mThread, &attr,
57 LogTimeEntry::threadStart, this)) {
58 pthread_attr_destroy(&attr);
59 return;
60 }
Mark Salyzyn0175b072014-02-26 09:50:16 -080061 }
Mark Salyzyn765f7822014-04-04 12:47:44 -070062 pthread_attr_destroy(&attr);
Mark Salyzyn0175b072014-02-26 09:50:16 -080063 }
Mark Salyzyn765f7822014-04-04 12:47:44 -070064 threadRunning = false;
65 if (mClient) {
66 mClient->decRef();
67 }
68 decRef_Locked();
Mark Salyzyn0175b072014-02-26 09:50:16 -080069}
70
71void LogTimeEntry::threadStop(void *obj) {
72 LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj);
73
74 lock();
75
Mark Salyzyn0175b072014-02-26 09:50:16 -080076 if (me->mNonBlock) {
77 me->error_Locked();
78 }
79
80 SocketClient *client = me->mClient;
81
82 if (me->isError_Locked()) {
83 LogReader &reader = me->mReader;
84 LastLogTimes &times = reader.logbuf().mTimes;
85
86 LastLogTimes::iterator it = times.begin();
87 while(it != times.end()) {
88 if (*it == me) {
89 times.erase(it);
90 me->release_Locked();
91 break;
92 }
93 it++;
94 }
95
96 me->mClient = NULL;
97 reader.release(client);
98 }
99
100 if (client) {
101 client->decRef();
102 }
103
Mark Salyzyna16f7612014-08-07 08:16:52 -0700104 me->threadRunning = false;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800105 me->decRef_Locked();
106
107 unlock();
108}
109
110void *LogTimeEntry::threadStart(void *obj) {
Mark Salyzyn8daa9af2014-04-28 14:07:23 -0700111 prctl(PR_SET_NAME, "logd.reader.per");
112
Mark Salyzyn0175b072014-02-26 09:50:16 -0800113 LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj);
114
115 pthread_cleanup_push(threadStop, obj);
116
117 SocketClient *client = me->mClient;
118 if (!client) {
119 me->error();
Mark Salyzyna16f7612014-08-07 08:16:52 -0700120 return NULL;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800121 }
122
123 LogBuffer &logbuf = me->mReader.logbuf();
124
125 bool privileged = FlushCommand::hasReadLogs(client);
126
127 lock();
128
Mark Salyzyna16f7612014-08-07 08:16:52 -0700129 while (me->threadRunning && !me->isError_Locked()) {
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800130 uint64_t start = me->mStart;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800131
132 unlock();
133
134 if (me->mTail) {
135 logbuf.flushTo(client, start, privileged, FilterFirstPass, me);
136 }
137 start = logbuf.flushTo(client, start, privileged, FilterSecondPass, me);
138
Mark Salyzyna16f7612014-08-07 08:16:52 -0700139 lock();
140
Mark Salyzyn0175b072014-02-26 09:50:16 -0800141 if (start == LogBufferElement::FLUSH_ERROR) {
Mark Salyzyna16f7612014-08-07 08:16:52 -0700142 me->error_Locked();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800143 }
144
Mark Salyzyna16f7612014-08-07 08:16:52 -0700145 if (me->mNonBlock || !me->threadRunning || me->isError_Locked()) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800146 break;
147 }
148
TraianX Schiauda6495d2014-12-17 10:53:41 +0200149 me->cleanSkip_Locked();
150
Mark Salyzyna16f7612014-08-07 08:16:52 -0700151 pthread_cond_wait(&me->threadTriggeredCondition, &timesLock);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800152 }
153
154 unlock();
155
Mark Salyzyn0175b072014-02-26 09:50:16 -0800156 pthread_cleanup_pop(true);
157
158 return NULL;
159}
160
161// A first pass to count the number of elements
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800162int LogTimeEntry::FilterFirstPass(const LogBufferElement *element, void *obj) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800163 LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj);
164
165 LogTimeEntry::lock();
166
167 if (me->mCount == 0) {
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800168 me->mStart = element->getSequence();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800169 }
170
171 if ((!me->mPid || (me->mPid == element->getPid()))
TraianX Schiauda6495d2014-12-17 10:53:41 +0200172 && (me->isWatching(element->getLogId()))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800173 ++me->mCount;
174 }
175
176 LogTimeEntry::unlock();
177
178 return false;
179}
180
181// A second pass to send the selected elements
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800182int LogTimeEntry::FilterSecondPass(const LogBufferElement *element, void *obj) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800183 LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj);
184
185 LogTimeEntry::lock();
186
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800187 me->mStart = element->getSequence();
TraianX Schiauda6495d2014-12-17 10:53:41 +0200188
189 if (me->skipAhead[element->getLogId()]) {
190 me->skipAhead[element->getLogId()]--;
Mark Salyzyn8d7656b2014-06-05 15:58:43 -0700191 goto skip;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800192 }
193
Mark Salyzyn0175b072014-02-26 09:50:16 -0800194 // Truncate to close race between first and second pass
195 if (me->mNonBlock && me->mTail && (me->mIndex >= me->mCount)) {
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800196 goto stop;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800197 }
198
TraianX Schiauda6495d2014-12-17 10:53:41 +0200199 if (!me->isWatching(element->getLogId())) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800200 goto skip;
201 }
202
203 if (me->mPid && (me->mPid != element->getPid())) {
204 goto skip;
205 }
206
207 if (me->isError_Locked()) {
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800208 goto stop;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800209 }
210
211 if (!me->mTail) {
212 goto ok;
213 }
214
215 ++me->mIndex;
216
217 if ((me->mCount > me->mTail) && (me->mIndex <= (me->mCount - me->mTail))) {
218 goto skip;
219 }
220
221 if (!me->mNonBlock) {
222 me->mTail = 0;
223 }
224
225ok:
TraianX Schiauda6495d2014-12-17 10:53:41 +0200226 if (!me->skipAhead[element->getLogId()]) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800227 LogTimeEntry::unlock();
228 return true;
229 }
230 // FALLTHRU
231
232skip:
233 LogTimeEntry::unlock();
234 return false;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800235
236stop:
237 LogTimeEntry::unlock();
238 return -1;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800239}
TraianX Schiauda6495d2014-12-17 10:53:41 +0200240
241void LogTimeEntry::cleanSkip_Locked(void) {
242 for (log_id_t i = LOG_ID_MIN; i < LOG_ID_MAX; i = (log_id_t) (i + 1)) {
243 skipAhead[i] = 0;
244 }
245}