blob: 7fd625e472b402b4ef0acb0ecd637023eda71870 [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
19#include "logd/LogEvent.h"
20
21#include <android/util/ProtoOutputStream.h>
Yao Chena80e5c02018-09-04 13:55:29 -070022#include <condition_variable>
23#include <mutex>
Yao Chena80e5c02018-09-04 13:55:29 -070024#include <thread>
Yao Chen41e606c2018-10-05 15:54:11 -070025#include "external/StatsPullerManager.h"
Yao Chena80e5c02018-09-04 13:55:29 -070026#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
30namespace android {
31namespace os {
32namespace 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 Chen41e606c2018-10-05 15:54:11 -070053 * |size_t|shellData proto|size_t|shellData proto|....
Yao Chena80e5c02018-09-04 13:55:29 -070054 *
55 * Only one shell subscriber allowed at a time, because each shell subscriber blocks one thread
56 * until it exits.
57 */
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080058class ShellSubscriber : public virtual RefBase {
Yao Chena80e5c02018-09-04 13:55:29 -070059public:
Yao Chen41e606c2018-10-05 15:54:11 -070060 ShellSubscriber(sp<UidMap> uidMap, sp<StatsPullerManager> pullerMgr)
61 : mUidMap(uidMap), mPullerMgr(pullerMgr){};
Yao Chena80e5c02018-09-04 13:55:29 -070062
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080063 void startNewSubscription(int inFd, int outFd, int timeoutSec);
Yao Chena80e5c02018-09-04 13:55:29 -070064
65 void onLogEvent(const LogEvent& event);
66
67private:
Yao Chen41e606c2018-10-05 15:54:11 -070068 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 Chena80e5c02018-09-04 13:55:29 -070076
Ruchir Rastogia5e4bb52020-03-04 17:11:58 -080077 struct SubscriptionInfo {
78 SubscriptionInfo(const int& inputFd, const int& outputFd)
79 : mInputFd(inputFd), mOutputFd(outputFd), mClientAlive(true) {
80 }
Yao Chena80e5c02018-09-04 13:55:29 -070081
Ruchir Rastogia5e4bb52020-03-04 17:11:58 -080082 int mInputFd;
83 int mOutputFd;
84 std::vector<SimpleAtomMatcher> mPushedMatchers;
85 std::vector<PullInfo> mPulledInfo;
86 int mPullIntervalMin;
87 bool mClientAlive;
88 };
Yao Chen41e606c2018-10-05 15:54:11 -070089
Ruchir Rastogia5e4bb52020-03-04 17:11:58 -080090 int claimToken();
Yao Chena80e5c02018-09-04 13:55:29 -070091
Ruchir Rastogia5e4bb52020-03-04 17:11:58 -080092 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 Chen41e606c2018-10-05 15:54:11 -070098
Yao Chena80e5c02018-09-04 13:55:29 -070099 sp<UidMap> mUidMap;
100
Yao Chen41e606c2018-10-05 15:54:11 -0700101 sp<StatsPullerManager> mPullerMgr;
Yao Chena80e5c02018-09-04 13:55:29 -0700102
103 android::util::ProtoOutputStream mProto;
104
105 mutable std::mutex mMutex;
106
Ruchir Rastogia5e4bb52020-03-04 17:11:58 -0800107 std::condition_variable mSubscriptionShouldEnd;
Yao Chena80e5c02018-09-04 13:55:29 -0700108
Ruchir Rastogia5e4bb52020-03-04 17:11:58 -0800109 std::shared_ptr<SubscriptionInfo> mSubscriptionInfo = nullptr;
Yao Chena80e5c02018-09-04 13:55:29 -0700110
Ruchir Rastogia5e4bb52020-03-04 17:11:58 -0800111 int mToken;
Yao Chena80e5c02018-09-04 13:55:29 -0700112};
113
114} // namespace statsd
115} // namespace os
116} // namespace android