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 | |
| 19 | #include "logd/LogEvent.h" |
| 20 | |
| 21 | #include <android/util/ProtoOutputStream.h> |
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> |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 25 | #include "external/StatsPullerManager.h" |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 26 | #include "frameworks/base/cmds/statsd/src/shell/shell_config.pb.h" |
| 27 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
| 28 | #include "packages/UidMap.h" |
| 29 | |
| 30 | namespace android { |
| 31 | namespace os { |
| 32 | namespace statsd { |
| 33 | |
| 34 | /** |
| 35 | * Handles atoms subscription via shell cmd. |
| 36 | * |
| 37 | * A shell subscription lasts *until shell exits*. Unlike config based clients, a shell client |
| 38 | * communicates with statsd via file descriptors. They can subscribe pushed and pulled atoms. |
| 39 | * The atoms are sent back to the client in real time, as opposed to |
| 40 | * keeping the data in memory. Shell clients do not subscribe aggregated metrics, as they are |
| 41 | * responsible for doing the aggregation after receiving the atom events. |
| 42 | * |
| 43 | * Shell client pass ShellSubscription in the proto binary format. Client can update the |
| 44 | * subscription by sending a new subscription. The new subscription would replace the old one. |
| 45 | * Input data stream format is: |
| 46 | * |
| 47 | * |size_t|subscription proto|size_t|subscription proto|.... |
| 48 | * |
| 49 | * statsd sends the events back in Atom proto binary format. Each Atom message is preceded |
| 50 | * with sizeof(size_t) bytes indicating the size of the proto message payload. |
| 51 | * |
| 52 | * The stream would be in the following format: |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 53 | * |size_t|shellData proto|size_t|shellData proto|.... |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 54 | * |
| 55 | * Only one shell subscriber allowed at a time, because each shell subscriber blocks one thread |
| 56 | * until it exits. |
| 57 | */ |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 58 | class ShellSubscriber : public virtual RefBase { |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 59 | public: |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 60 | ShellSubscriber(sp<UidMap> uidMap, sp<StatsPullerManager> pullerMgr) |
| 61 | : mUidMap(uidMap), mPullerMgr(pullerMgr){}; |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 62 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 63 | void startNewSubscription(int inFd, int outFd, int timeoutSec); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 64 | |
| 65 | void onLogEvent(const LogEvent& event); |
| 66 | |
| 67 | private: |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 68 | struct PullInfo { |
| 69 | PullInfo(const SimpleAtomMatcher& matcher, int64_t interval) |
| 70 | : mPullerMatcher(matcher), mInterval(interval), mPrevPullElapsedRealtimeMs(0) { |
| 71 | } |
| 72 | SimpleAtomMatcher mPullerMatcher; |
| 73 | int64_t mInterval; |
| 74 | int64_t mPrevPullElapsedRealtimeMs; |
| 75 | }; |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 76 | |
Ruchir Rastogi | a5e4bb5 | 2020-03-04 17:11:58 -0800 | [diff] [blame] | 77 | struct SubscriptionInfo { |
| 78 | SubscriptionInfo(const int& inputFd, const int& outputFd) |
| 79 | : mInputFd(inputFd), mOutputFd(outputFd), mClientAlive(true) { |
| 80 | } |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 81 | |
Ruchir Rastogi | a5e4bb5 | 2020-03-04 17:11:58 -0800 | [diff] [blame] | 82 | int mInputFd; |
| 83 | int mOutputFd; |
| 84 | std::vector<SimpleAtomMatcher> mPushedMatchers; |
| 85 | std::vector<PullInfo> mPulledInfo; |
| 86 | int mPullIntervalMin; |
| 87 | bool mClientAlive; |
| 88 | }; |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 89 | |
Ruchir Rastogi | a5e4bb5 | 2020-03-04 17:11:58 -0800 | [diff] [blame] | 90 | int claimToken(); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 91 | |
Ruchir Rastogi | a5e4bb5 | 2020-03-04 17:11:58 -0800 | [diff] [blame] | 92 | bool readConfig(std::shared_ptr<SubscriptionInfo> subscriptionInfo); |
| 93 | |
| 94 | void startPull(int64_t myToken); |
| 95 | |
| 96 | bool writePulledAtomsLocked(const vector<std::shared_ptr<LogEvent>>& data, |
| 97 | const SimpleAtomMatcher& matcher); |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 98 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 99 | sp<UidMap> mUidMap; |
| 100 | |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 101 | sp<StatsPullerManager> mPullerMgr; |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 102 | |
| 103 | android::util::ProtoOutputStream mProto; |
| 104 | |
| 105 | mutable std::mutex mMutex; |
| 106 | |
Ruchir Rastogi | a5e4bb5 | 2020-03-04 17:11:58 -0800 | [diff] [blame] | 107 | std::condition_variable mSubscriptionShouldEnd; |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 108 | |
Ruchir Rastogi | a5e4bb5 | 2020-03-04 17:11:58 -0800 | [diff] [blame] | 109 | std::shared_ptr<SubscriptionInfo> mSubscriptionInfo = nullptr; |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 110 | |
Ruchir Rastogi | a5e4bb5 | 2020-03-04 17:11:58 -0800 | [diff] [blame] | 111 | int mToken; |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | } // namespace statsd |
| 115 | } // namespace os |
| 116 | } // namespace android |