blob: eaf2ad141c8e72497b231a7094b7dc5d3fea0a16 [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
63 /**
64 * Start a new subscription.
65 */
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080066 void startNewSubscription(int inFd, int outFd, int timeoutSec);
Yao Chena80e5c02018-09-04 13:55:29 -070067
68 void onLogEvent(const LogEvent& event);
69
70private:
Yao Chen41e606c2018-10-05 15:54:11 -070071 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 Rastogie449b0c2020-02-10 17:40:09 -080079 bool readConfig();
Yao Chena80e5c02018-09-04 13:55:29 -070080
81 void updateConfig(const ShellSubscription& config);
82
Yao Chen41e606c2018-10-05 15:54:11 -070083 void startPull(int64_t token, int64_t intervalMillis);
84
Yao Chena80e5c02018-09-04 13:55:29 -070085 void cleanUpLocked();
86
Yao Chen41e606c2018-10-05 15:54:11 -070087 void writeToOutputLocked(const vector<std::shared_ptr<LogEvent>>& data,
88 const SimpleAtomMatcher& matcher);
89
Yao Chena80e5c02018-09-04 13:55:29 -070090 sp<UidMap> mUidMap;
91
Yao Chen41e606c2018-10-05 15:54:11 -070092 sp<StatsPullerManager> mPullerMgr;
Yao Chena80e5c02018-09-04 13:55:29 -070093
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 Rastogie449b0c2020-02-10 17:40:09 -0800100 int mInput = -1; // The input file descriptor
Yao Chena80e5c02018-09-04 13:55:29 -0700101
Ruchir Rastogie449b0c2020-02-10 17:40:09 -0800102 int mOutput = -1; // The output file descriptor
Yao Chena80e5c02018-09-04 13:55:29 -0700103
104 std::vector<SimpleAtomMatcher> mPushedMatchers;
Yao Chen41e606c2018-10-05 15:54:11 -0700105
106 std::vector<PullInfo> mPulledInfo;
107
Ruchir Rastogie449b0c2020-02-10 17:40:09 -0800108 int64_t mSubscriberId = 0; // A unique id to identify a subscriber.
109
Yao Chen41e606c2018-10-05 15:54:11 -0700110 int64_t mPullToken = 0; // A unique token to identify a puller thread.
Yao Chena80e5c02018-09-04 13:55:29 -0700111};
112
113} // namespace statsd
114} // namespace os
115} // namespace android