summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Salyzyn2014-02-26 11:50:16 -0600
committerMark Salyzyn2014-02-26 11:52:35 -0600
commit0175b0747a1f55329109e84c9a1322dcb95e2848 (patch)
treea48dd103279c3efc313028a09fb4570d212e624d /logd/LogTimes.cpp
parent9b986497e7f19a7fde9e35eb73d765f4a09dee07 (diff)
downloadplatform-system-core-0175b0747a1f55329109e84c9a1322dcb95e2848.tar.gz
platform-system-core-0175b0747a1f55329109e84c9a1322dcb95e2848.tar.xz
platform-system-core-0175b0747a1f55329109e84c9a1322dcb95e2848.zip
logd: initial checkin.
* Create a new userspace log daemon for handling logging messages. Original-Change-Id: I75267df16359684490121e6c31cca48614d79856 Signed-off-by: Nick Kralevich <nnk@google.com> * Merge conflicts * rename new syslog daemon to logd to prevent confusion with bionic syslog * replace racy getGroups call with KISS call to client->getGid() * Timestamps are filed at logging source * insert entries into list in timestamp order * Added LogTimeEntry tail filtration handling * Added region locking around LogWriter list * separate threads for each writer * /dev/socket/logd* permissions Signed-off-by: Mark Salyzyn <salyzyn@google.com> (cherry picked from commit 3e76e0a49760c4970b7cda6153e51026af98e4f3) Author: Nick Kralevich <nnk@google.com> Change-Id: Ice88b1412d8f9daa7f9119b2b5aaf684a5e28098
Diffstat (limited to 'logd/LogTimes.cpp')
-rw-r--r--logd/LogTimes.cpp225
1 files changed, 225 insertions, 0 deletions
diff --git a/logd/LogTimes.cpp b/logd/LogTimes.cpp
new file mode 100644
index 000000000..d6d4e9331
--- /dev/null
+++ b/logd/LogTimes.cpp
@@ -0,0 +1,225 @@
1/*
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,
28 unsigned int logMask, pid_t pid)
29 : mRefCount(1)
30 , mRelease(false)
31 , mError(false)
32 , threadRunning(false)
33 , threadTriggered(true)
34 , mReader(reader)
35 , mLogMask(logMask)
36 , mPid(pid)
37 , skipAhead(0)
38 , mCount(0)
39 , mTail(tail)
40 , mIndex(0)
41 , mClient(client)
42 , mStart(EPOCH)
43 , mNonBlock(nonBlock)
44 , mEnd(CLOCK_MONOTONIC)
45{ }
46
47void LogTimeEntry::startReader_Locked(void) {
48 threadRunning = true;
49 if (pthread_create(&mThread, NULL, LogTimeEntry::threadStart, this)) {
50 threadRunning = false;
51 if (mClient) {
52 mClient->decRef();
53 }
54 decRef_Locked();
55 }
56}
57
58void LogTimeEntry::threadStop(void *obj) {
59 LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj);
60
61 lock();
62
63 me->threadRunning = false;
64 if (me->mNonBlock) {
65 me->error_Locked();
66 }
67
68 SocketClient *client = me->mClient;
69
70 if (me->isError_Locked()) {
71 LogReader &reader = me->mReader;
72 LastLogTimes &times = reader.logbuf().mTimes;
73
74 LastLogTimes::iterator it = times.begin();
75 while(it != times.end()) {
76 if (*it == me) {
77 times.erase(it);
78 me->release_Locked();
79 break;
80 }
81 it++;
82 }
83
84 me->mClient = NULL;
85 reader.release(client);
86 }
87
88 if (client) {
89 client->decRef();
90 }
91
92 me->decRef_Locked();
93
94 unlock();
95}
96
97void *LogTimeEntry::threadStart(void *obj) {
98 LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj);
99
100 pthread_cleanup_push(threadStop, obj);
101
102 SocketClient *client = me->mClient;
103 if (!client) {
104 me->error();
105 pthread_exit(NULL);
106 }
107
108 LogBuffer &logbuf = me->mReader.logbuf();
109
110 bool privileged = FlushCommand::hasReadLogs(client);
111
112 lock();
113
114 me->threadTriggered = true;
115
116 while(me->threadTriggered && !me->isError_Locked()) {
117
118 me->threadTriggered = false;
119
120 log_time start = me->mStart;
121
122 unlock();
123
124 if (me->mTail) {
125 logbuf.flushTo(client, start, privileged, FilterFirstPass, me);
126 }
127 start = logbuf.flushTo(client, start, privileged, FilterSecondPass, me);
128
129 if (start == LogBufferElement::FLUSH_ERROR) {
130 me->error();
131 }
132
133 if (me->mNonBlock) {
134 lock();
135 break;
136 }
137
138 sched_yield();
139
140 lock();
141 }
142
143 unlock();
144
145 pthread_exit(NULL);
146
147 pthread_cleanup_pop(true);
148
149 return NULL;
150}
151
152// A first pass to count the number of elements
153bool LogTimeEntry::FilterFirstPass(const LogBufferElement *element, void *obj) {
154 LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj);
155
156 LogTimeEntry::lock();
157
158 if (me->mCount == 0) {
159 me->mStart = element->getMonotonicTime();
160 }
161
162 if ((!me->mPid || (me->mPid == element->getPid()))
163 && (me->mLogMask & (1 << element->getLogId()))) {
164 ++me->mCount;
165 }
166
167 LogTimeEntry::unlock();
168
169 return false;
170}
171
172// A second pass to send the selected elements
173bool LogTimeEntry::FilterSecondPass(const LogBufferElement *element, void *obj) {
174 LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj);
175
176 LogTimeEntry::lock();
177
178 if (me->skipAhead) {
179 me->skipAhead--;
180 }
181
182 me->mStart = element->getMonotonicTime();
183
184 // Truncate to close race between first and second pass
185 if (me->mNonBlock && me->mTail && (me->mIndex >= me->mCount)) {
186 goto skip;
187 }
188
189 if ((me->mLogMask & (1 << element->getLogId())) == 0) {
190 goto skip;
191 }
192
193 if (me->mPid && (me->mPid != element->getPid())) {
194 goto skip;
195 }
196
197 if (me->isError_Locked()) {
198 goto skip;
199 }
200
201 if (!me->mTail) {
202 goto ok;
203 }
204
205 ++me->mIndex;
206
207 if ((me->mCount > me->mTail) && (me->mIndex <= (me->mCount - me->mTail))) {
208 goto skip;
209 }
210
211 if (!me->mNonBlock) {
212 me->mTail = 0;
213 }
214
215ok:
216 if (!me->skipAhead) {
217 LogTimeEntry::unlock();
218 return true;
219 }
220 // FALLTHRU
221
222skip:
223 LogTimeEntry::unlock();
224 return false;
225}