blob: c32ac2dce418383bcc35408fd4ff7fd57cd6bacf [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
17#include "FlushCommand.h"
18#include "LogBuffer.h"
19#include "LogTimes.h"
20#include "LogReader.h"
21
22pthread_mutex_t LogTimeEntry::timesLock = PTHREAD_MUTEX_INITIALIZER;
23
24const struct timespec LogTimeEntry::EPOCH = { 0, 1 };
25
26LogTimeEntry::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,
29 log_time start)
Mark Salyzyn0175b072014-02-26 09:50:16 -080030 : mRefCount(1)
31 , mRelease(false)
32 , mError(false)
33 , threadRunning(false)
34 , threadTriggered(true)
35 , mReader(reader)
36 , mLogMask(logMask)
37 , mPid(pid)
38 , skipAhead(0)
39 , mCount(0)
40 , mTail(tail)
41 , mIndex(0)
42 , mClient(client)
Mark Salyzynfa3716b2014-02-14 16:05:05 -080043 , mStart(start)
Mark Salyzyn0175b072014-02-26 09:50:16 -080044 , mNonBlock(nonBlock)
45 , mEnd(CLOCK_MONOTONIC)
46{ }
47
48void LogTimeEntry::startReader_Locked(void) {
Mark Salyzyn765f7822014-04-04 12:47:44 -070049 pthread_attr_t attr;
50
Mark Salyzyn0175b072014-02-26 09:50:16 -080051 threadRunning = true;
Mark Salyzyn765f7822014-04-04 12:47:44 -070052
53 if (!pthread_attr_init(&attr)) {
54 if (!pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) {
55 if (!pthread_create(&mThread, &attr,
56 LogTimeEntry::threadStart, this)) {
57 pthread_attr_destroy(&attr);
58 return;
59 }
Mark Salyzyn0175b072014-02-26 09:50:16 -080060 }
Mark Salyzyn765f7822014-04-04 12:47:44 -070061 pthread_attr_destroy(&attr);
Mark Salyzyn0175b072014-02-26 09:50:16 -080062 }
Mark Salyzyn765f7822014-04-04 12:47:44 -070063 threadRunning = false;
64 if (mClient) {
65 mClient->decRef();
66 }
67 decRef_Locked();
Mark Salyzyn0175b072014-02-26 09:50:16 -080068}
69
70void LogTimeEntry::threadStop(void *obj) {
71 LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj);
72
73 lock();
74
75 me->threadRunning = false;
76 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
104 me->decRef_Locked();
105
106 unlock();
107}
108
109void *LogTimeEntry::threadStart(void *obj) {
110 LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj);
111
112 pthread_cleanup_push(threadStop, obj);
113
114 SocketClient *client = me->mClient;
115 if (!client) {
116 me->error();
117 pthread_exit(NULL);
118 }
119
120 LogBuffer &logbuf = me->mReader.logbuf();
121
122 bool privileged = FlushCommand::hasReadLogs(client);
123
124 lock();
125
126 me->threadTriggered = true;
127
128 while(me->threadTriggered && !me->isError_Locked()) {
129
130 me->threadTriggered = false;
131
132 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
141 if (start == LogBufferElement::FLUSH_ERROR) {
142 me->error();
143 }
144
145 if (me->mNonBlock) {
146 lock();
147 break;
148 }
149
150 sched_yield();
151
152 lock();
153 }
154
155 unlock();
156
157 pthread_exit(NULL);
158
159 pthread_cleanup_pop(true);
160
161 return NULL;
162}
163
164// A first pass to count the number of elements
165bool LogTimeEntry::FilterFirstPass(const LogBufferElement *element, void *obj) {
166 LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj);
167
168 LogTimeEntry::lock();
169
170 if (me->mCount == 0) {
171 me->mStart = element->getMonotonicTime();
172 }
173
174 if ((!me->mPid || (me->mPid == element->getPid()))
Mark Salyzync03e72c2014-02-18 11:23:53 -0800175 && (me->mLogMask & (1 << element->getLogId()))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800176 ++me->mCount;
177 }
178
179 LogTimeEntry::unlock();
180
181 return false;
182}
183
184// A second pass to send the selected elements
185bool LogTimeEntry::FilterSecondPass(const LogBufferElement *element, void *obj) {
186 LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj);
187
188 LogTimeEntry::lock();
189
190 if (me->skipAhead) {
191 me->skipAhead--;
192 }
193
194 me->mStart = element->getMonotonicTime();
195
196 // Truncate to close race between first and second pass
197 if (me->mNonBlock && me->mTail && (me->mIndex >= me->mCount)) {
198 goto skip;
199 }
200
201 if ((me->mLogMask & (1 << element->getLogId())) == 0) {
202 goto skip;
203 }
204
205 if (me->mPid && (me->mPid != element->getPid())) {
206 goto skip;
207 }
208
209 if (me->isError_Locked()) {
210 goto skip;
211 }
212
213 if (!me->mTail) {
214 goto ok;
215 }
216
217 ++me->mIndex;
218
219 if ((me->mCount > me->mTail) && (me->mIndex <= (me->mCount - me->mTail))) {
220 goto skip;
221 }
222
223 if (!me->mNonBlock) {
224 me->mTail = 0;
225 }
226
227ok:
228 if (!me->skipAhead) {
229 LogTimeEntry::unlock();
230 return true;
231 }
232 // FALLTHRU
233
234skip:
235 LogTimeEntry::unlock();
236 return false;
237}