blob: 61457d89f224231422a7e9a49eaa249078b14021 [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.
41 * The atoms are sent back to the client in real time, as opposed to
42 * keeping the data in memory. Shell clients do not subscribe aggregated metrics, as they are
43 * responsible for doing the aggregation after receiving the atom events.
44 *
45 * Shell client pass ShellSubscription in the proto binary format. Client can update the
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 Chen41e606c2018-10-05 15:54:11 -070055 * |size_t|shellData proto|size_t|shellData proto|....
Yao Chena80e5c02018-09-04 13:55:29 -070056 *
57 * Only one shell subscriber allowed at a time, because each shell subscriber blocks one thread
58 * 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
103 void startPull(int64_t myToken);
104
105 bool writePulledAtomsLocked(const vector<std::shared_ptr<LogEvent>>& data,
106 const SimpleAtomMatcher& matcher);
Yao Chen41e606c2018-10-05 15:54:11 -0700107
Yao Chena80e5c02018-09-04 13:55:29 -0700108 sp<UidMap> mUidMap;
109
Yao Chen41e606c2018-10-05 15:54:11 -0700110 sp<StatsPullerManager> mPullerMgr;
Yao Chena80e5c02018-09-04 13:55:29 -0700111
112 android::util::ProtoOutputStream mProto;
113
114 mutable std::mutex mMutex;
115
Ruchir Rastogia5e4bb52020-03-04 17:11:58 -0800116 std::condition_variable mSubscriptionShouldEnd;
Yao Chena80e5c02018-09-04 13:55:29 -0700117
Ruchir Rastogia5e4bb52020-03-04 17:11:58 -0800118 std::shared_ptr<SubscriptionInfo> mSubscriptionInfo = nullptr;
Yao Chena80e5c02018-09-04 13:55:29 -0700119
Greg Kaiserf0f1d832020-03-23 09:03:39 -0700120 int mToken = 0;
Tej Singh3be093b2020-03-04 20:08:38 -0800121
122 const int32_t DEFAULT_PULL_UID = AID_SYSTEM;
Yao Chena80e5c02018-09-04 13:55:29 -0700123};
124
125} // namespace statsd
126} // namespace os
127} // namespace android