Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | */ |
Yi Yang | bdee486 | 2019-03-18 14:53:32 -0700 | [diff] [blame] | 16 | #define DEBUG false // STOPSHIP if true |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 17 | #include "Log.h" |
| 18 | |
| 19 | #include "ShellSubscriber.h" |
| 20 | |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 21 | #include "matchers/matcher_util.h" |
| 22 | #include "stats_log_util.h" |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 23 | |
| 24 | using android::util::ProtoOutputStream; |
| 25 | |
| 26 | namespace android { |
| 27 | namespace os { |
| 28 | namespace statsd { |
| 29 | |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 30 | const static int FIELD_ID_ATOM = 1; |
| 31 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 32 | void ShellSubscriber::startNewSubscription(int in, int out, int timeoutSec) { |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 33 | VLOG("start new shell subscription"); |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 34 | int64_t subscriberId = getElapsedRealtimeNs(); |
| 35 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 36 | { |
| 37 | std::lock_guard<std::mutex> lock(mMutex); |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 38 | if (mSubscriberId> 0) { |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 39 | VLOG("Only one shell subscriber is allowed."); |
| 40 | return; |
| 41 | } |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 42 | mSubscriberId = subscriberId; |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 43 | mInput = in; |
| 44 | mOutput = out; |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 47 | bool success = readConfig(); |
| 48 | if (!success) { |
| 49 | std::lock_guard<std::mutex> lock(mMutex); |
| 50 | cleanUpLocked(); |
| 51 | } |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 52 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 53 | VLOG("Wait for client to exit or timeout (%d sec)", timeoutSec); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 54 | std::unique_lock<std::mutex> lk(mMutex); |
Yao Chen | 35cb8d6 | 2019-01-03 16:49:14 -0800 | [diff] [blame] | 55 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 56 | // Note that the following is blocking, and it's intended as we cannot return until the shell |
| 57 | // cmd exits or we time out. |
Yao Chen | 35cb8d6 | 2019-01-03 16:49:14 -0800 | [diff] [blame] | 58 | if (timeoutSec > 0) { |
| 59 | mShellDied.wait_for(lk, timeoutSec * 1s, |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 60 | [this, subscriberId] { return mSubscriberId != subscriberId; }); |
Yao Chen | 35cb8d6 | 2019-01-03 16:49:14 -0800 | [diff] [blame] | 61 | } else { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 62 | mShellDied.wait(lk, [this, subscriberId] { return mSubscriberId != subscriberId; }); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | |
| 67 | // Read configs until EOF is reached. There may be multiple configs in the input |
| 68 | // -- each new config should replace the previous one. |
| 69 | // |
| 70 | // Returns a boolean indicating whether the input was read successfully. |
| 71 | bool ShellSubscriber::readConfig() { |
| 72 | if (mInput < 0) { |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | while (true) { |
| 77 | // Read the size of the config. |
| 78 | size_t bufferSize = 0; |
| 79 | ssize_t bytesRead = read(mInput, &bufferSize, sizeof(bufferSize)); |
| 80 | if (bytesRead == 0) { |
| 81 | VLOG("We have reached the end of the input."); |
| 82 | return true; |
| 83 | } else if (bytesRead < 0 || (size_t)bytesRead != sizeof(bufferSize)) { |
| 84 | ALOGE("Error reading config size"); |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | // Read and parse the config. |
| 89 | vector<uint8_t> buffer(bufferSize); |
| 90 | bytesRead = read(mInput, buffer.data(), bufferSize); |
| 91 | if (bytesRead > 0 && (size_t)bytesRead == bufferSize) { |
| 92 | ShellSubscription config; |
| 93 | if (config.ParseFromArray(buffer.data(), bufferSize)) { |
| 94 | updateConfig(config); |
| 95 | } else { |
| 96 | ALOGE("Error parsing the config"); |
| 97 | return false; |
| 98 | } |
| 99 | } else { |
| 100 | VLOG("Error reading the config, expected bytes: %zu, actual bytes: %zu", bufferSize, |
| 101 | bytesRead); |
| 102 | return false; |
| 103 | } |
Yao Chen | 35cb8d6 | 2019-01-03 16:49:14 -0800 | [diff] [blame] | 104 | } |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void ShellSubscriber::updateConfig(const ShellSubscription& config) { |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 108 | mPushedMatchers.clear(); |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 109 | mPulledInfo.clear(); |
| 110 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 111 | for (const auto& pushed : config.pushed()) { |
| 112 | mPushedMatchers.push_back(pushed); |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 113 | VLOG("adding matcher for pushed atom %d", pushed.atom_id()); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 114 | } |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 115 | |
| 116 | int64_t token = getElapsedRealtimeNs(); |
| 117 | mPullToken = token; |
| 118 | |
| 119 | int64_t minInterval = -1; |
| 120 | for (const auto& pulled : config.pulled()) { |
| 121 | // All intervals need to be multiples of the min interval. |
| 122 | if (minInterval < 0 || pulled.freq_millis() < minInterval) { |
| 123 | minInterval = pulled.freq_millis(); |
| 124 | } |
| 125 | |
| 126 | mPulledInfo.emplace_back(pulled.matcher(), pulled.freq_millis()); |
| 127 | VLOG("adding matcher for pulled atom %d", pulled.matcher().atom_id()); |
| 128 | } |
| 129 | |
| 130 | if (mPulledInfo.size() > 0 && minInterval > 0) { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 131 | // This thread is guaranteed to terminate after it detects the token is |
| 132 | // different. |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 133 | std::thread puller([token, minInterval, this] { startPull(token, minInterval); }); |
| 134 | puller.detach(); |
| 135 | } |
| 136 | } |
| 137 | |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 138 | void ShellSubscriber::startPull(int64_t token, int64_t intervalMillis) { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 139 | while (true) { |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 140 | int64_t nowMillis = getElapsedRealtimeMillis(); |
| 141 | { |
| 142 | std::lock_guard<std::mutex> lock(mMutex); |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 143 | // If the token has changed, the config has changed, so this |
| 144 | // puller can now stop. |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 145 | if (mPulledInfo.size() == 0 || mPullToken != token) { |
| 146 | VLOG("Pulling thread %lld done!", (long long)token); |
| 147 | return; |
| 148 | } |
| 149 | for (auto& pullInfo : mPulledInfo) { |
| 150 | if (pullInfo.mPrevPullElapsedRealtimeMs + pullInfo.mInterval < nowMillis) { |
| 151 | VLOG("pull atom %d now", pullInfo.mPullerMatcher.atom_id()); |
| 152 | |
| 153 | vector<std::shared_ptr<LogEvent>> data; |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 154 | mPullerMgr->Pull(pullInfo.mPullerMatcher.atom_id(), &data); |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 155 | VLOG("pulled %zu atoms", data.size()); |
| 156 | if (data.size() > 0) { |
| 157 | writeToOutputLocked(data, pullInfo.mPullerMatcher); |
| 158 | } |
| 159 | pullInfo.mPrevPullElapsedRealtimeMs = nowMillis; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | VLOG("Pulling thread %lld sleep....", (long long)token); |
| 164 | std::this_thread::sleep_for(std::chrono::milliseconds(intervalMillis)); |
| 165 | } |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 168 | // Must be called with the lock acquired, so that mProto isn't being written to |
| 169 | // at the same time by multiple threads. |
| 170 | void ShellSubscriber::writeToOutputLocked(const vector<std::shared_ptr<LogEvent>>& data, |
| 171 | const SimpleAtomMatcher& matcher) { |
| 172 | if (mOutput < 0) { |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 173 | return; |
| 174 | } |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 175 | int count = 0; |
| 176 | mProto.clear(); |
| 177 | for (const auto& event : data) { |
| 178 | VLOG("%s", event->ToString().c_str()); |
| 179 | if (matchesSimple(*mUidMap, matcher, *event)) { |
| 180 | VLOG("matched"); |
| 181 | count++; |
| 182 | uint64_t atomToken = mProto.start(util::FIELD_TYPE_MESSAGE | |
| 183 | util::FIELD_COUNT_REPEATED | FIELD_ID_ATOM); |
| 184 | event->ToProto(mProto); |
| 185 | mProto.end(atomToken); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 188 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 189 | if (count > 0) { |
| 190 | // First write the payload size. |
| 191 | size_t bufferSize = mProto.size(); |
| 192 | write(mOutput, &bufferSize, sizeof(bufferSize)); |
| 193 | |
| 194 | VLOG("%d atoms, proto size: %zu", count, bufferSize); |
| 195 | // Then write the payload. |
| 196 | mProto.flush(mOutput); |
| 197 | } |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | void ShellSubscriber::onLogEvent(const LogEvent& event) { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 201 | // Acquire a lock to prevent corruption from multiple threads writing to |
| 202 | // mProto. |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 203 | std::lock_guard<std::mutex> lock(mMutex); |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 204 | if (mOutput < 0) { |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 205 | return; |
| 206 | } |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 207 | |
| 208 | mProto.clear(); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 209 | for (const auto& matcher : mPushedMatchers) { |
| 210 | if (matchesSimple(*mUidMap, matcher, event)) { |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 211 | VLOG("%s", event.ToString().c_str()); |
| 212 | uint64_t atomToken = mProto.start(util::FIELD_TYPE_MESSAGE | |
| 213 | util::FIELD_COUNT_REPEATED | FIELD_ID_ATOM); |
Yao Chen | 398dd19 | 2018-10-01 14:49:03 -0700 | [diff] [blame] | 214 | event.ToProto(mProto); |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 215 | mProto.end(atomToken); |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 216 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 217 | // First write the payload size. |
| 218 | size_t bufferSize = mProto.size(); |
| 219 | write(mOutput, &bufferSize, sizeof(bufferSize)); |
| 220 | |
| 221 | // Then write the payload. |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 222 | mProto.flush(mOutput); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 227 | void ShellSubscriber::cleanUpLocked() { |
| 228 | // The file descriptors will be closed by binder. |
| 229 | mInput = -1; |
| 230 | mOutput = -1; |
| 231 | mSubscriberId = 0; |
| 232 | mPushedMatchers.clear(); |
| 233 | mPulledInfo.clear(); |
| 234 | // Setting mPullToken == 0 tells pull thread that its work is done. |
| 235 | mPullToken = 0; |
| 236 | VLOG("done clean up"); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | } // namespace statsd |
| 240 | } // namespace os |
Yao Chen | 398dd19 | 2018-10-01 14:49:03 -0700 | [diff] [blame] | 241 | } // namespace android |