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 | */ |
| 16 | |
| 17 | #pragma once |
| 18 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 19 | #include <android/util/ProtoOutputStream.h> |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 20 | #include <private/android_filesystem_config.h> |
| 21 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 22 | #include <condition_variable> |
| 23 | #include <mutex> |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 24 | #include <thread> |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 25 | |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 26 | #include "external/StatsPullerManager.h" |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 27 | #include "frameworks/base/cmds/statsd/src/shell/shell_config.pb.h" |
| 28 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 29 | #include "logd/LogEvent.h" |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 30 | #include "packages/UidMap.h" |
| 31 | |
| 32 | namespace android { |
| 33 | namespace os { |
| 34 | namespace statsd { |
| 35 | |
| 36 | /** |
| 37 | * Handles atoms subscription via shell cmd. |
| 38 | * |
| 39 | * A shell subscription lasts *until shell exits*. Unlike config based clients, a shell client |
| 40 | * communicates with statsd via file descriptors. They can subscribe pushed and pulled atoms. |
Ruchir Rastogi | 1e24051 | 2020-04-22 09:03:22 -0700 | [diff] [blame] | 41 | * The atoms are sent back to the client in real time, as opposed to keeping the data in memory. |
| 42 | * Shell clients do not subscribe aggregated metrics, as they are responsible for doing the |
| 43 | * aggregation after receiving the atom events. |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 44 | * |
Ruchir Rastogi | 1e24051 | 2020-04-22 09:03:22 -0700 | [diff] [blame] | 45 | * Shell clients pass ShellSubscription in the proto binary format. Clients can update the |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 46 | * subscription by sending a new subscription. The new subscription would replace the old one. |
| 47 | * Input data stream format is: |
| 48 | * |
| 49 | * |size_t|subscription proto|size_t|subscription proto|.... |
| 50 | * |
| 51 | * statsd sends the events back in Atom proto binary format. Each Atom message is preceded |
| 52 | * with sizeof(size_t) bytes indicating the size of the proto message payload. |
| 53 | * |
| 54 | * The stream would be in the following format: |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 55 | * |size_t|shellData proto|size_t|shellData proto|.... |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 56 | * |
Ruchir Rastogi | 1e24051 | 2020-04-22 09:03:22 -0700 | [diff] [blame] | 57 | * Only one shell subscriber is allowed at a time because each shell subscriber blocks one thread |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 58 | * until it exits. |
| 59 | */ |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 60 | class ShellSubscriber : public virtual RefBase { |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 61 | public: |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 62 | ShellSubscriber(sp<UidMap> uidMap, sp<StatsPullerManager> pullerMgr) |
| 63 | : mUidMap(uidMap), mPullerMgr(pullerMgr){}; |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 64 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 65 | void startNewSubscription(int inFd, int outFd, int timeoutSec); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 66 | |
| 67 | void onLogEvent(const LogEvent& event); |
| 68 | |
| 69 | private: |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 70 | struct PullInfo { |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 71 | PullInfo(const SimpleAtomMatcher& matcher, int64_t interval, |
| 72 | const std::vector<std::string>& packages, const std::vector<int32_t>& uids) |
| 73 | : mPullerMatcher(matcher), |
| 74 | mInterval(interval), |
| 75 | mPrevPullElapsedRealtimeMs(0), |
| 76 | mPullPackages(packages), |
| 77 | mPullUids(uids) { |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 78 | } |
| 79 | SimpleAtomMatcher mPullerMatcher; |
| 80 | int64_t mInterval; |
| 81 | int64_t mPrevPullElapsedRealtimeMs; |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 82 | std::vector<std::string> mPullPackages; |
| 83 | std::vector<int32_t> mPullUids; |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 84 | }; |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 85 | |
Ruchir Rastogi | a5e4bb5 | 2020-03-04 17:11:58 -0800 | [diff] [blame] | 86 | struct SubscriptionInfo { |
| 87 | SubscriptionInfo(const int& inputFd, const int& outputFd) |
| 88 | : mInputFd(inputFd), mOutputFd(outputFd), mClientAlive(true) { |
| 89 | } |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 90 | |
Ruchir Rastogi | a5e4bb5 | 2020-03-04 17:11:58 -0800 | [diff] [blame] | 91 | int mInputFd; |
| 92 | int mOutputFd; |
| 93 | std::vector<SimpleAtomMatcher> mPushedMatchers; |
| 94 | std::vector<PullInfo> mPulledInfo; |
Ruchir Rastogi | a5e4bb5 | 2020-03-04 17:11:58 -0800 | [diff] [blame] | 95 | bool mClientAlive; |
| 96 | }; |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 97 | |
Ruchir Rastogi | a5e4bb5 | 2020-03-04 17:11:58 -0800 | [diff] [blame] | 98 | int claimToken(); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 99 | |
Ruchir Rastogi | a5e4bb5 | 2020-03-04 17:11:58 -0800 | [diff] [blame] | 100 | bool readConfig(std::shared_ptr<SubscriptionInfo> subscriptionInfo); |
| 101 | |
Ruchir Rastogi | 835e75c | 2020-05-15 12:57:15 -0700 | [diff] [blame^] | 102 | void spawnHelperThread(int myToken); |
Ruchir Rastogi | a5e4bb5 | 2020-03-04 17:11:58 -0800 | [diff] [blame] | 103 | |
Ruchir Rastogi | 1e24051 | 2020-04-22 09:03:22 -0700 | [diff] [blame] | 104 | void waitForSubscriptionToEndLocked(std::shared_ptr<SubscriptionInfo> myInfo, |
| 105 | int myToken, |
| 106 | std::unique_lock<std::mutex>& lock, |
| 107 | int timeoutSec); |
| 108 | |
Ruchir Rastogi | 835e75c | 2020-05-15 12:57:15 -0700 | [diff] [blame^] | 109 | // Helper thread that pulls atoms at a regular frequency and sends |
| 110 | // heartbeats to perfd if statsd hasn't recently sent any data. Statsd must |
| 111 | // send heartbeats for perfd to escape a blocking read call and recheck if |
| 112 | // the user has terminated the subscription. |
| 113 | void pullAndSendHeartbeats(int myToken); |
Ruchir Rastogi | 1e24051 | 2020-04-22 09:03:22 -0700 | [diff] [blame] | 114 | |
| 115 | void writePulledAtomsLocked(const vector<std::shared_ptr<LogEvent>>& data, |
Ruchir Rastogi | a5e4bb5 | 2020-03-04 17:11:58 -0800 | [diff] [blame] | 116 | const SimpleAtomMatcher& matcher); |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 117 | |
Ruchir Rastogi | 1e24051 | 2020-04-22 09:03:22 -0700 | [diff] [blame] | 118 | void getUidsForPullAtom(vector<int32_t>* uids, const PullInfo& pullInfo); |
| 119 | |
Ruchir Rastogi | 835e75c | 2020-05-15 12:57:15 -0700 | [diff] [blame^] | 120 | void attemptWriteToPipeLocked(size_t dataSize); |
Ruchir Rastogi | 1e24051 | 2020-04-22 09:03:22 -0700 | [diff] [blame] | 121 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 122 | sp<UidMap> mUidMap; |
| 123 | |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 124 | sp<StatsPullerManager> mPullerMgr; |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 125 | |
| 126 | android::util::ProtoOutputStream mProto; |
| 127 | |
| 128 | mutable std::mutex mMutex; |
| 129 | |
Ruchir Rastogi | a5e4bb5 | 2020-03-04 17:11:58 -0800 | [diff] [blame] | 130 | std::condition_variable mSubscriptionShouldEnd; |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 131 | |
Ruchir Rastogi | a5e4bb5 | 2020-03-04 17:11:58 -0800 | [diff] [blame] | 132 | std::shared_ptr<SubscriptionInfo> mSubscriptionInfo = nullptr; |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 133 | |
Greg Kaiser | f0f1d83 | 2020-03-23 09:03:39 -0700 | [diff] [blame] | 134 | int mToken = 0; |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 135 | |
| 136 | const int32_t DEFAULT_PULL_UID = AID_SYSTEM; |
Ruchir Rastogi | 1e24051 | 2020-04-22 09:03:22 -0700 | [diff] [blame] | 137 | |
| 138 | // Tracks when we last send data to perfd. We need that time to determine |
| 139 | // when next to send a heartbeat. |
| 140 | int64_t mLastWriteMs = 0; |
| 141 | const int64_t kMsBetweenHeartbeats = 1000; |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | } // namespace statsd |
| 145 | } // namespace os |
| 146 | } // namespace android |