blob: 1a9a548f45a7ffd8e1a979b9aabaa16f727d9e59 [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)
36 , threadTriggered(true)
37 , mReader(reader)
38 , mLogMask(logMask)
39 , mPid(pid)
40 , skipAhead(0)
41 , mCount(0)
42 , mTail(tail)
43 , mIndex(0)
44 , mClient(client)
Mark Salyzynfa3716b2014-02-14 16:05:05 -080045 , mStart(start)
Mark Salyzyn0175b072014-02-26 09:50:16 -080046 , mNonBlock(nonBlock)
47 , mEnd(CLOCK_MONOTONIC)
48{ }
49
50void LogTimeEntry::startReader_Locked(void) {
Mark Salyzyn765f7822014-04-04 12:47:44 -070051 pthread_attr_t attr;
52
Mark Salyzyn0175b072014-02-26 09:50:16 -080053 threadRunning = true;
Mark Salyzyn765f7822014-04-04 12:47:44 -070054
55 if (!pthread_attr_init(&attr)) {
56 if (!pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) {
57 if (!pthread_create(&mThread, &attr,
58 LogTimeEntry::threadStart, this)) {
59 pthread_attr_destroy(&attr);
60 return;
61 }
Mark Salyzyn0175b072014-02-26 09:50:16 -080062 }
Mark Salyzyn765f7822014-04-04 12:47:44 -070063 pthread_attr_destroy(&attr);
Mark Salyzyn0175b072014-02-26 09:50:16 -080064 }
Mark Salyzyn765f7822014-04-04 12:47:44 -070065 threadRunning = false;
66 if (mClient) {
67 mClient->decRef();
68 }
69 decRef_Locked();
Mark Salyzyn0175b072014-02-26 09:50:16 -080070}
71
72void LogTimeEntry::threadStop(void *obj) {
73 LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj);
74
75 lock();
76
77 me->threadRunning = false;
78 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
106 me->decRef_Locked();
107
108 unlock();
109}
110
111void *LogTimeEntry::threadStart(void *obj) {
Mark Salyzyn8daa9af2014-04-28 14:07:23 -0700112 prctl(PR_SET_NAME, "logd.reader.per");
113
Mark Salyzyn0175b072014-02-26 09:50:16 -0800114 LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj);
115
116 pthread_cleanup_push(threadStop, obj);
117
118 SocketClient *client = me->mClient;
119 if (!client) {
120 me->error();
121 pthread_exit(NULL);
122 }
123
124 LogBuffer &logbuf = me->mReader.logbuf();
125
126 bool privileged = FlushCommand::hasReadLogs(client);
127
128 lock();
129
130 me->threadTriggered = true;
131
132 while(me->threadTriggered && !me->isError_Locked()) {
133
134 me->threadTriggered = false;
135
136 log_time start = me->mStart;
137
138 unlock();
139
140 if (me->mTail) {
141 logbuf.flushTo(client, start, privileged, FilterFirstPass, me);
142 }
143 start = logbuf.flushTo(client, start, privileged, FilterSecondPass, me);
144
145 if (start == LogBufferElement::FLUSH_ERROR) {
146 me->error();
147 }
148
149 if (me->mNonBlock) {
150 lock();
151 break;
152 }
153
154 sched_yield();
155
156 lock();
157 }
158
159 unlock();
160
161 pthread_exit(NULL);
162
163 pthread_cleanup_pop(true);
164
165 return NULL;
166}
167
168// A first pass to count the number of elements
169bool LogTimeEntry::FilterFirstPass(const LogBufferElement *element, void *obj) {
170 LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj);
171
172 LogTimeEntry::lock();
173
174 if (me->mCount == 0) {
175 me->mStart = element->getMonotonicTime();
176 }
177
178 if ((!me->mPid || (me->mPid == element->getPid()))
Mark Salyzync03e72c2014-02-18 11:23:53 -0800179 && (me->mLogMask & (1 << element->getLogId()))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800180 ++me->mCount;
181 }
182
183 LogTimeEntry::unlock();
184
185 return false;
186}
187
188// A second pass to send the selected elements
189bool LogTimeEntry::FilterSecondPass(const LogBufferElement *element, void *obj) {
190 LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj);
191
192 LogTimeEntry::lock();
193
194 if (me->skipAhead) {
195 me->skipAhead--;
196 }
197
198 me->mStart = element->getMonotonicTime();
199
200 // Truncate to close race between first and second pass
201 if (me->mNonBlock && me->mTail && (me->mIndex >= me->mCount)) {
202 goto skip;
203 }
204
205 if ((me->mLogMask & (1 << element->getLogId())) == 0) {
206 goto skip;
207 }
208
209 if (me->mPid && (me->mPid != element->getPid())) {
210 goto skip;
211 }
212
213 if (me->isError_Locked()) {
214 goto skip;
215 }
216
217 if (!me->mTail) {
218 goto ok;
219 }
220
221 ++me->mIndex;
222
223 if ((me->mCount > me->mTail) && (me->mIndex <= (me->mCount - me->mTail))) {
224 goto skip;
225 }
226
227 if (!me->mNonBlock) {
228 me->mTail = 0;
229 }
230
231ok:
232 if (!me->skipAhead) {
233 LogTimeEntry::unlock();
234 return true;
235 }
236 // FALLTHRU
237
238skip:
239 LogTimeEntry::unlock();
240 return false;
241}