blob: 26c8a2a0b683f2a8f0b1dcce0ce92b242bc650a7 [file] [log] [blame]
Yao Chena80e5c02018-09-04 13:55:29 -07001/*
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 Chena80e5c02018-09-04 13:55:29 -070019#include <android/util/ProtoOutputStream.h>
Tej Singh3be093b2020-03-04 20:08:38 -080020#include <private/android_filesystem_config.h>
21
Yao Chena80e5c02018-09-04 13:55:29 -070022#include <condition_variable>
23#include <mutex>
Yao Chena80e5c02018-09-04 13:55:29 -070024#include <thread>
Tej Singh3be093b2020-03-04 20:08:38 -080025
Yao Chen41e606c2018-10-05 15:54:11 -070026#include "external/StatsPullerManager.h"
Yao Chena80e5c02018-09-04 13:55:29 -070027#include "frameworks/base/cmds/statsd/src/shell/shell_config.pb.h"
28#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
Tej Singh3be093b2020-03-04 20:08:38 -080029#include "logd/LogEvent.h"
Yao Chena80e5c02018-09-04 13:55:29 -070030#include "packages/UidMap.h"
31
32namespace android {
33namespace os {
34namespace 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 Rastogi1e240512020-04-22 09:03:22 -070041 * 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 Chena80e5c02018-09-04 13:55:29 -070044 *
Ruchir Rastogi1e240512020-04-22 09:03:22 -070045 * Shell clients pass ShellSubscription in the proto binary format. Clients can update the
Yao Chena80e5c02018-09-04 13:55:29 -070046 * 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 Chen41e606c2018-10-05 15:54:11 -070055 * |size_t|shellData proto|size_t|shellData proto|....
Yao Chena80e5c02018-09-04 13:55:29 -070056 *
Ruchir Rastogi1e240512020-04-22 09:03:22 -070057 * Only one shell subscriber is allowed at a time because each shell subscriber blocks one thread
Yao Chena80e5c02018-09-04 13:55:29 -070058 * until it exits.
59 */
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080060class ShellSubscriber : public virtual RefBase {
Yao Chena80e5c02018-09-04 13:55:29 -070061public:
Yao Chen41e606c2018-10-05 15:54:11 -070062 ShellSubscriber(sp<UidMap> uidMap, sp<StatsPullerManager> pullerMgr)
63 : mUidMap(uidMap), mPullerMgr(pullerMgr){};
Yao Chena80e5c02018-09-04 13:55:29 -070064
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080065 void startNewSubscription(int inFd, int outFd, int timeoutSec);
Yao Chena80e5c02018-09-04 13:55:29 -070066
67 void onLogEvent(const LogEvent& event);
68
69private:
Yao Chen41e606c2018-10-05 15:54:11 -070070 struct PullInfo {
Tej Singh3be093b2020-03-04 20:08:38 -080071 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 Chen41e606c2018-10-05 15:54:11 -070078 }
79 SimpleAtomMatcher mPullerMatcher;
80 int64_t mInterval;
81 int64_t mPrevPullElapsedRealtimeMs;
Tej Singh3be093b2020-03-04 20:08:38 -080082 std::vector<std::string> mPullPackages;
83 std::vector<int32_t> mPullUids;
Yao Chen41e606c2018-10-05 15:54:11 -070084 };
Yao Chena80e5c02018-09-04 13:55:29 -070085
Ruchir Rastogia5e4bb52020-03-04 17:11:58 -080086 struct SubscriptionInfo {
87 SubscriptionInfo(const int& inputFd, const int& outputFd)
88 : mInputFd(inputFd), mOutputFd(outputFd), mClientAlive(true) {
89 }
Yao Chena80e5c02018-09-04 13:55:29 -070090
Ruchir Rastogia5e4bb52020-03-04 17:11:58 -080091 int mInputFd;
92 int mOutputFd;
93 std::vector<SimpleAtomMatcher> mPushedMatchers;
94 std::vector<PullInfo> mPulledInfo;
95 int mPullIntervalMin;
96 bool mClientAlive;
97 };
Yao Chen41e606c2018-10-05 15:54:11 -070098
Ruchir Rastogia5e4bb52020-03-04 17:11:58 -080099 int claimToken();
Yao Chena80e5c02018-09-04 13:55:29 -0700100
Ruchir Rastogia5e4bb52020-03-04 17:11:58 -0800101 bool readConfig(std::shared_ptr<SubscriptionInfo> subscriptionInfo);
102
Ruchir Rastogi1e240512020-04-22 09:03:22 -0700103 void spawnHelperThreadsLocked(std::shared_ptr<SubscriptionInfo> myInfo, int myToken);
Ruchir Rastogia5e4bb52020-03-04 17:11:58 -0800104
Ruchir Rastogi1e240512020-04-22 09:03:22 -0700105 void waitForSubscriptionToEndLocked(std::shared_ptr<SubscriptionInfo> myInfo,
106 int myToken,
107 std::unique_lock<std::mutex>& lock,
108 int timeoutSec);
109
110 void startPull(int myToken);
111
112 void writePulledAtomsLocked(const vector<std::shared_ptr<LogEvent>>& data,
Ruchir Rastogia5e4bb52020-03-04 17:11:58 -0800113 const SimpleAtomMatcher& matcher);
Yao Chen41e606c2018-10-05 15:54:11 -0700114
Ruchir Rastogi1e240512020-04-22 09:03:22 -0700115 void getUidsForPullAtom(vector<int32_t>* uids, const PullInfo& pullInfo);
116
117 void attemptWriteToSocketLocked(size_t dataSize);
118
119 // Send ocassional heartbeats for two reasons: (a) for statsd to detect when
120 // the read end of the pipe has closed and (b) for perfd to escape a
121 // blocking read call and recheck if the user has terminated the
122 // subscription.
123 void sendHeartbeats(int myToken);
124
Yao Chena80e5c02018-09-04 13:55:29 -0700125 sp<UidMap> mUidMap;
126
Yao Chen41e606c2018-10-05 15:54:11 -0700127 sp<StatsPullerManager> mPullerMgr;
Yao Chena80e5c02018-09-04 13:55:29 -0700128
129 android::util::ProtoOutputStream mProto;
130
131 mutable std::mutex mMutex;
132
Ruchir Rastogia5e4bb52020-03-04 17:11:58 -0800133 std::condition_variable mSubscriptionShouldEnd;
Yao Chena80e5c02018-09-04 13:55:29 -0700134
Ruchir Rastogia5e4bb52020-03-04 17:11:58 -0800135 std::shared_ptr<SubscriptionInfo> mSubscriptionInfo = nullptr;
Yao Chena80e5c02018-09-04 13:55:29 -0700136
Greg Kaiserf0f1d832020-03-23 09:03:39 -0700137 int mToken = 0;
Tej Singh3be093b2020-03-04 20:08:38 -0800138
139 const int32_t DEFAULT_PULL_UID = AID_SYSTEM;
Ruchir Rastogi1e240512020-04-22 09:03:22 -0700140
141 // Tracks when we last send data to perfd. We need that time to determine
142 // when next to send a heartbeat.
143 int64_t mLastWriteMs = 0;
144 const int64_t kMsBetweenHeartbeats = 1000;
Yao Chena80e5c02018-09-04 13:55:29 -0700145};
146
147} // namespace statsd
148} // namespace os
149} // namespace android