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 | |
| 63 | /** |
| 64 | * Start a new subscription. |
| 65 | */ |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 66 | void startNewSubscription(int inFd, int outFd, int timeoutSec); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 67 | |
| 68 | void onLogEvent(const LogEvent& event); |
| 69 | |
| 70 | private: |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 71 | struct PullInfo { |
| 72 | PullInfo(const SimpleAtomMatcher& matcher, int64_t interval) |
| 73 | : mPullerMatcher(matcher), mInterval(interval), mPrevPullElapsedRealtimeMs(0) { |
| 74 | } |
| 75 | SimpleAtomMatcher mPullerMatcher; |
| 76 | int64_t mInterval; |
| 77 | int64_t mPrevPullElapsedRealtimeMs; |
| 78 | }; |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 79 | bool readConfig(); |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 80 | |
| 81 | void updateConfig(const ShellSubscription& config); |
| 82 | |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 83 | void startPull(int64_t token, int64_t intervalMillis); |
| 84 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 85 | void cleanUpLocked(); |
| 86 | |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 87 | void writeToOutputLocked(const vector<std::shared_ptr<LogEvent>>& data, |
| 88 | const SimpleAtomMatcher& matcher); |
| 89 | |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 90 | sp<UidMap> mUidMap; |
| 91 | |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 92 | sp<StatsPullerManager> mPullerMgr; |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 93 | |
| 94 | android::util::ProtoOutputStream mProto; |
| 95 | |
| 96 | mutable std::mutex mMutex; |
| 97 | |
| 98 | std::condition_variable mShellDied; // semaphore for waiting until shell exits. |
| 99 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 100 | int mInput = -1; // The input file descriptor |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 101 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 102 | int mOutput = -1; // The output file descriptor |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 103 | |
| 104 | std::vector<SimpleAtomMatcher> mPushedMatchers; |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 105 | |
| 106 | std::vector<PullInfo> mPulledInfo; |
| 107 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 108 | int64_t mSubscriberId = 0; // A unique id to identify a subscriber. |
| 109 | |
Yao Chen | 41e606c | 2018-10-05 15:54:11 -0700 | [diff] [blame] | 110 | int64_t mPullToken = 0; // A unique token to identify a puller thread. |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | } // namespace statsd |
| 114 | } // namespace os |
| 115 | } // namespace android |