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 | |
| 17 | #include "FlushCommand.h" |
| 18 | #include "LogBuffer.h" |
| 19 | #include "LogTimes.h" |
| 20 | #include "LogReader.h" |
| 21 | |
| 22 | pthread_mutex_t LogTimeEntry::timesLock = PTHREAD_MUTEX_INITIALIZER; |
| 23 | |
| 24 | const struct timespec LogTimeEntry::EPOCH = { 0, 1 }; |
| 25 | |
| 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, |
| 29 | log_time 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) |
| 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 Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame^] | 43 | , mStart(start) |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 44 | , mNonBlock(nonBlock) |
| 45 | , mEnd(CLOCK_MONOTONIC) |
| 46 | { } |
| 47 | |
| 48 | void LogTimeEntry::startReader_Locked(void) { |
| 49 | threadRunning = true; |
| 50 | if (pthread_create(&mThread, NULL, LogTimeEntry::threadStart, this)) { |
| 51 | threadRunning = false; |
| 52 | if (mClient) { |
| 53 | mClient->decRef(); |
| 54 | } |
| 55 | decRef_Locked(); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | void LogTimeEntry::threadStop(void *obj) { |
| 60 | LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj); |
| 61 | |
| 62 | lock(); |
| 63 | |
| 64 | me->threadRunning = false; |
| 65 | if (me->mNonBlock) { |
| 66 | me->error_Locked(); |
| 67 | } |
| 68 | |
| 69 | SocketClient *client = me->mClient; |
| 70 | |
| 71 | if (me->isError_Locked()) { |
| 72 | LogReader &reader = me->mReader; |
| 73 | LastLogTimes × = reader.logbuf().mTimes; |
| 74 | |
| 75 | LastLogTimes::iterator it = times.begin(); |
| 76 | while(it != times.end()) { |
| 77 | if (*it == me) { |
| 78 | times.erase(it); |
| 79 | me->release_Locked(); |
| 80 | break; |
| 81 | } |
| 82 | it++; |
| 83 | } |
| 84 | |
| 85 | me->mClient = NULL; |
| 86 | reader.release(client); |
| 87 | } |
| 88 | |
| 89 | if (client) { |
| 90 | client->decRef(); |
| 91 | } |
| 92 | |
| 93 | me->decRef_Locked(); |
| 94 | |
| 95 | unlock(); |
| 96 | } |
| 97 | |
| 98 | void *LogTimeEntry::threadStart(void *obj) { |
| 99 | LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj); |
| 100 | |
| 101 | pthread_cleanup_push(threadStop, obj); |
| 102 | |
| 103 | SocketClient *client = me->mClient; |
| 104 | if (!client) { |
| 105 | me->error(); |
| 106 | pthread_exit(NULL); |
| 107 | } |
| 108 | |
| 109 | LogBuffer &logbuf = me->mReader.logbuf(); |
| 110 | |
| 111 | bool privileged = FlushCommand::hasReadLogs(client); |
| 112 | |
| 113 | lock(); |
| 114 | |
| 115 | me->threadTriggered = true; |
| 116 | |
| 117 | while(me->threadTriggered && !me->isError_Locked()) { |
| 118 | |
| 119 | me->threadTriggered = false; |
| 120 | |
| 121 | log_time start = me->mStart; |
| 122 | |
| 123 | unlock(); |
| 124 | |
| 125 | if (me->mTail) { |
| 126 | logbuf.flushTo(client, start, privileged, FilterFirstPass, me); |
| 127 | } |
| 128 | start = logbuf.flushTo(client, start, privileged, FilterSecondPass, me); |
| 129 | |
| 130 | if (start == LogBufferElement::FLUSH_ERROR) { |
| 131 | me->error(); |
| 132 | } |
| 133 | |
| 134 | if (me->mNonBlock) { |
| 135 | lock(); |
| 136 | break; |
| 137 | } |
| 138 | |
| 139 | sched_yield(); |
| 140 | |
| 141 | lock(); |
| 142 | } |
| 143 | |
| 144 | unlock(); |
| 145 | |
| 146 | pthread_exit(NULL); |
| 147 | |
| 148 | pthread_cleanup_pop(true); |
| 149 | |
| 150 | return NULL; |
| 151 | } |
| 152 | |
| 153 | // A first pass to count the number of elements |
| 154 | bool LogTimeEntry::FilterFirstPass(const LogBufferElement *element, void *obj) { |
| 155 | LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj); |
| 156 | |
| 157 | LogTimeEntry::lock(); |
| 158 | |
| 159 | if (me->mCount == 0) { |
| 160 | me->mStart = element->getMonotonicTime(); |
| 161 | } |
| 162 | |
| 163 | if ((!me->mPid || (me->mPid == element->getPid())) |
Mark Salyzyn | c03e72c | 2014-02-18 11:23:53 -0800 | [diff] [blame] | 164 | && (me->mLogMask & (1 << element->getLogId()))) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 165 | ++me->mCount; |
| 166 | } |
| 167 | |
| 168 | LogTimeEntry::unlock(); |
| 169 | |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | // A second pass to send the selected elements |
| 174 | bool LogTimeEntry::FilterSecondPass(const LogBufferElement *element, void *obj) { |
| 175 | LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj); |
| 176 | |
| 177 | LogTimeEntry::lock(); |
| 178 | |
| 179 | if (me->skipAhead) { |
| 180 | me->skipAhead--; |
| 181 | } |
| 182 | |
| 183 | me->mStart = element->getMonotonicTime(); |
| 184 | |
| 185 | // Truncate to close race between first and second pass |
| 186 | if (me->mNonBlock && me->mTail && (me->mIndex >= me->mCount)) { |
| 187 | goto skip; |
| 188 | } |
| 189 | |
| 190 | if ((me->mLogMask & (1 << element->getLogId())) == 0) { |
| 191 | goto skip; |
| 192 | } |
| 193 | |
| 194 | if (me->mPid && (me->mPid != element->getPid())) { |
| 195 | goto skip; |
| 196 | } |
| 197 | |
| 198 | if (me->isError_Locked()) { |
| 199 | goto skip; |
| 200 | } |
| 201 | |
| 202 | if (!me->mTail) { |
| 203 | goto ok; |
| 204 | } |
| 205 | |
| 206 | ++me->mIndex; |
| 207 | |
| 208 | if ((me->mCount > me->mTail) && (me->mIndex <= (me->mCount - me->mTail))) { |
| 209 | goto skip; |
| 210 | } |
| 211 | |
| 212 | if (!me->mNonBlock) { |
| 213 | me->mTail = 0; |
| 214 | } |
| 215 | |
| 216 | ok: |
| 217 | if (!me->skipAhead) { |
| 218 | LogTimeEntry::unlock(); |
| 219 | return true; |
| 220 | } |
| 221 | // FALLTHRU |
| 222 | |
| 223 | skip: |
| 224 | LogTimeEntry::unlock(); |
| 225 | return false; |
| 226 | } |