blob: ea4e8c83d5bd29f2c113b296807aa66c3bc1b836 [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
26const struct timespec LogTimeEntry::EPOCH = { 0, 1 };
27
28LogTimeEntry::LogTimeEntry(LogReader &reader, SocketClient *client,
29 bool nonBlock, unsigned long tail,
Mark Salyzynfa3716b2014-02-14 16:05:05 -080030 unsigned int logMask, pid_t pid,
31 log_time start)
Mark Salyzyn0175b072014-02-26 09:50:16 -080032 : mRefCount(1)
33 , mRelease(false)
34 , mError(false)
35 , threadRunning(false)
Mark Salyzyn0175b072014-02-26 09:50:16 -080036 , mReader(reader)
37 , mLogMask(logMask)
38 , mPid(pid)
39 , skipAhead(0)
40 , mCount(0)
41 , mTail(tail)
42 , mIndex(0)
43 , mClient(client)
Mark Salyzynfa3716b2014-02-14 16:05:05 -080044 , mStart(start)
Mark Salyzyn0175b072014-02-26 09:50:16 -080045 , mNonBlock(nonBlock)
46 , mEnd(CLOCK_MONOTONIC)
Mark Salyzyna16f7612014-08-07 08:16:52 -070047{
48 pthread_cond_init(&threadTriggeredCondition, NULL);
49}
Mark Salyzyn0175b072014-02-26 09:50:16 -080050
51void LogTimeEntry::startReader_Locked(void) {
Mark Salyzyn765f7822014-04-04 12:47:44 -070052 pthread_attr_t attr;
53
Mark Salyzyn0175b072014-02-26 09:50:16 -080054 threadRunning = true;
Mark Salyzyn765f7822014-04-04 12:47:44 -070055
56 if (!pthread_attr_init(&attr)) {
57 if (!pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) {
58 if (!pthread_create(&mThread, &attr,
59 LogTimeEntry::threadStart, this)) {
60 pthread_attr_destroy(&attr);
61 return;
62 }
Mark Salyzyn0175b072014-02-26 09:50:16 -080063 }
Mark Salyzyn765f7822014-04-04 12:47:44 -070064 pthread_attr_destroy(&attr);
Mark Salyzyn0175b072014-02-26 09:50:16 -080065 }
Mark Salyzyn765f7822014-04-04 12:47:44 -070066 threadRunning = false;
67 if (mClient) {
68 mClient->decRef();
69 }
70 decRef_Locked();
Mark Salyzyn0175b072014-02-26 09:50:16 -080071}
72
73void LogTimeEntry::threadStop(void *obj) {
74 LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj);
75
76 lock();
77
Mark Salyzyn0175b072014-02-26 09:50:16 -080078 if (me->mNonBlock) {
79 me->error_Locked();
80 }
81
82 SocketClient *client = me->mClient;
83
84 if (me->isError_Locked()) {
85 LogReader &reader = me->mReader;
86 LastLogTimes &times = reader.logbuf().mTimes;
87
88 LastLogTimes::iterator it = times.begin();
89 while(it != times.end()) {
90 if (*it == me) {
91 times.erase(it);
92 me->release_Locked();
93 break;
94 }
95 it++;
96 }
97
98 me->mClient = NULL;
99 reader.release(client);
100 }
101
102 if (client) {
103 client->decRef();
104 }
105
Mark Salyzyna16f7612014-08-07 08:16:52 -0700106 me->threadRunning = false;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800107 me->decRef_Locked();
108
109 unlock();
110}
111
112void *LogTimeEntry::threadStart(void *obj) {
Mark Salyzyn8daa9af2014-04-28 14:07:23 -0700113 prctl(PR_SET_NAME, "logd.reader.per");
114
Mark Salyzyn0175b072014-02-26 09:50:16 -0800115 LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj);
116
117 pthread_cleanup_push(threadStop, obj);
118
119 SocketClient *client = me->mClient;
120 if (!client) {
121 me->error();
Mark Salyzyna16f7612014-08-07 08:16:52 -0700122 return NULL;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800123 }
124
125 LogBuffer &logbuf = me->mReader.logbuf();
126
127 bool privileged = FlushCommand::hasReadLogs(client);
128
129 lock();
130
Mark Salyzyna16f7612014-08-07 08:16:52 -0700131 while (me->threadRunning && !me->isError_Locked()) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800132 log_time start = me->mStart;
133
134 unlock();
135
136 if (me->mTail) {
137 logbuf.flushTo(client, start, privileged, FilterFirstPass, me);
138 }
139 start = logbuf.flushTo(client, start, privileged, FilterSecondPass, me);
140
Mark Salyzyna16f7612014-08-07 08:16:52 -0700141 lock();
142
Mark Salyzyn0175b072014-02-26 09:50:16 -0800143 if (start == LogBufferElement::FLUSH_ERROR) {
Mark Salyzyna16f7612014-08-07 08:16:52 -0700144 me->error_Locked();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800145 }
146
Mark Salyzyna16f7612014-08-07 08:16:52 -0700147 if (me->mNonBlock || !me->threadRunning || me->isError_Locked()) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800148 break;
149 }
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
162bool LogTimeEntry::FilterFirstPass(const LogBufferElement *element, void *obj) {
163 LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj);
164
165 LogTimeEntry::lock();
166
167 if (me->mCount == 0) {
168 me->mStart = element->getMonotonicTime();
169 }
170
171 if ((!me->mPid || (me->mPid == element->getPid()))
Mark Salyzync03e72c2014-02-18 11:23:53 -0800172 && (me->mLogMask & (1 << 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
182bool LogTimeEntry::FilterSecondPass(const LogBufferElement *element, void *obj) {
183 LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj);
184
185 LogTimeEntry::lock();
186
187 if (me->skipAhead) {
188 me->skipAhead--;
Mark Salyzyn8d7656b2014-06-05 15:58:43 -0700189 goto skip;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800190 }
191
192 me->mStart = element->getMonotonicTime();
193
194 // Truncate to close race between first and second pass
195 if (me->mNonBlock && me->mTail && (me->mIndex >= me->mCount)) {
196 goto skip;
197 }
198
199 if ((me->mLogMask & (1 << element->getLogId())) == 0) {
200 goto skip;
201 }
202
203 if (me->mPid && (me->mPid != element->getPid())) {
204 goto skip;
205 }
206
207 if (me->isError_Locked()) {
208 goto skip;
209 }
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:
226 if (!me->skipAhead) {
227 LogTimeEntry::unlock();
228 return true;
229 }
230 // FALLTHRU
231
232skip:
233 LogTimeEntry::unlock();
234 return false;
235}