Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 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 | |
Mark Salyzyn | 8daa9af | 2014-04-28 14:07:23 -0700 | [diff] [blame] | 17 | #include <sys/prctl.h> |
| 18 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 19 | #include "FlushCommand.h" |
| 20 | #include "LogBuffer.h" |
| 21 | #include "LogTimes.h" |
| 22 | #include "LogReader.h" |
| 23 | |
| 24 | pthread_mutex_t LogTimeEntry::timesLock = PTHREAD_MUTEX_INITIALIZER; |
| 25 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 26 | LogTimeEntry::LogTimeEntry(LogReader &reader, SocketClient *client, |
| 27 | bool nonBlock, unsigned long tail, |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 28 | unsigned int logMask, pid_t pid, |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 29 | uint64_t start) |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 30 | : mRefCount(1) |
| 31 | , mRelease(false) |
| 32 | , mError(false) |
| 33 | , threadRunning(false) |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 34 | , mReader(reader) |
| 35 | , mLogMask(logMask) |
| 36 | , mPid(pid) |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 37 | , mCount(0) |
| 38 | , mTail(tail) |
| 39 | , mIndex(0) |
| 40 | , mClient(client) |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 41 | , mStart(start) |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 42 | , mNonBlock(nonBlock) |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 43 | , mEnd(LogBufferElement::getCurrentSequence()) |
Mark Salyzyn | a16f761 | 2014-08-07 08:16:52 -0700 | [diff] [blame] | 44 | { |
| 45 | pthread_cond_init(&threadTriggeredCondition, NULL); |
TraianX Schiau | da6495d | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 46 | cleanSkip_Locked(); |
Mark Salyzyn | a16f761 | 2014-08-07 08:16:52 -0700 | [diff] [blame] | 47 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 48 | |
| 49 | void LogTimeEntry::startReader_Locked(void) { |
Mark Salyzyn | 765f782 | 2014-04-04 12:47:44 -0700 | [diff] [blame] | 50 | pthread_attr_t attr; |
| 51 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 52 | threadRunning = true; |
Mark Salyzyn | 765f782 | 2014-04-04 12:47:44 -0700 | [diff] [blame] | 53 | |
| 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 Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 61 | } |
Mark Salyzyn | 765f782 | 2014-04-04 12:47:44 -0700 | [diff] [blame] | 62 | pthread_attr_destroy(&attr); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 63 | } |
Mark Salyzyn | 765f782 | 2014-04-04 12:47:44 -0700 | [diff] [blame] | 64 | threadRunning = false; |
| 65 | if (mClient) { |
| 66 | mClient->decRef(); |
| 67 | } |
| 68 | decRef_Locked(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void LogTimeEntry::threadStop(void *obj) { |
| 72 | LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj); |
| 73 | |
| 74 | lock(); |
| 75 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 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 × = 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 Salyzyn | a16f761 | 2014-08-07 08:16:52 -0700 | [diff] [blame] | 104 | me->threadRunning = false; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 105 | me->decRef_Locked(); |
| 106 | |
| 107 | unlock(); |
| 108 | } |
| 109 | |
| 110 | void *LogTimeEntry::threadStart(void *obj) { |
Mark Salyzyn | 8daa9af | 2014-04-28 14:07:23 -0700 | [diff] [blame] | 111 | prctl(PR_SET_NAME, "logd.reader.per"); |
| 112 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 113 | 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 Salyzyn | a16f761 | 2014-08-07 08:16:52 -0700 | [diff] [blame] | 120 | return NULL; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | LogBuffer &logbuf = me->mReader.logbuf(); |
| 124 | |
| 125 | bool privileged = FlushCommand::hasReadLogs(client); |
| 126 | |
| 127 | lock(); |
| 128 | |
Mark Salyzyn | a16f761 | 2014-08-07 08:16:52 -0700 | [diff] [blame] | 129 | while (me->threadRunning && !me->isError_Locked()) { |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 130 | uint64_t start = me->mStart; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 131 | |
| 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 Salyzyn | a16f761 | 2014-08-07 08:16:52 -0700 | [diff] [blame] | 139 | lock(); |
| 140 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 141 | if (start == LogBufferElement::FLUSH_ERROR) { |
Mark Salyzyn | a16f761 | 2014-08-07 08:16:52 -0700 | [diff] [blame] | 142 | me->error_Locked(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Mark Salyzyn | a16f761 | 2014-08-07 08:16:52 -0700 | [diff] [blame] | 145 | if (me->mNonBlock || !me->threadRunning || me->isError_Locked()) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 146 | break; |
| 147 | } |
| 148 | |
TraianX Schiau | da6495d | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 149 | me->cleanSkip_Locked(); |
| 150 | |
Mark Salyzyn | a16f761 | 2014-08-07 08:16:52 -0700 | [diff] [blame] | 151 | pthread_cond_wait(&me->threadTriggeredCondition, ×Lock); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | unlock(); |
| 155 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 156 | pthread_cleanup_pop(true); |
| 157 | |
| 158 | return NULL; |
| 159 | } |
| 160 | |
| 161 | // A first pass to count the number of elements |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 162 | int LogTimeEntry::FilterFirstPass(const LogBufferElement *element, void *obj) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 163 | LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj); |
| 164 | |
| 165 | LogTimeEntry::lock(); |
| 166 | |
| 167 | if (me->mCount == 0) { |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 168 | me->mStart = element->getSequence(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | if ((!me->mPid || (me->mPid == element->getPid())) |
TraianX Schiau | da6495d | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 172 | && (me->isWatching(element->getLogId()))) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 173 | ++me->mCount; |
| 174 | } |
| 175 | |
| 176 | LogTimeEntry::unlock(); |
| 177 | |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | // A second pass to send the selected elements |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 182 | int LogTimeEntry::FilterSecondPass(const LogBufferElement *element, void *obj) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 183 | LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj); |
| 184 | |
| 185 | LogTimeEntry::lock(); |
| 186 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 187 | me->mStart = element->getSequence(); |
TraianX Schiau | da6495d | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 188 | |
| 189 | if (me->skipAhead[element->getLogId()]) { |
| 190 | me->skipAhead[element->getLogId()]--; |
Mark Salyzyn | 8d7656b | 2014-06-05 15:58:43 -0700 | [diff] [blame] | 191 | goto skip; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 194 | // Truncate to close race between first and second pass |
| 195 | if (me->mNonBlock && me->mTail && (me->mIndex >= me->mCount)) { |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 196 | goto stop; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 197 | } |
| 198 | |
TraianX Schiau | da6495d | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 199 | if (!me->isWatching(element->getLogId())) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 200 | goto skip; |
| 201 | } |
| 202 | |
| 203 | if (me->mPid && (me->mPid != element->getPid())) { |
| 204 | goto skip; |
| 205 | } |
| 206 | |
| 207 | if (me->isError_Locked()) { |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 208 | goto stop; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 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 | |
| 225 | ok: |
TraianX Schiau | da6495d | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 226 | if (!me->skipAhead[element->getLogId()]) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 227 | LogTimeEntry::unlock(); |
| 228 | return true; |
| 229 | } |
| 230 | // FALLTHRU |
| 231 | |
| 232 | skip: |
| 233 | LogTimeEntry::unlock(); |
| 234 | return false; |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 235 | |
| 236 | stop: |
| 237 | LogTimeEntry::unlock(); |
| 238 | return -1; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 239 | } |
TraianX Schiau | da6495d | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 240 | |
| 241 | void 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 | } |