blob: 5401f31ce68c0a19e05a9c2b56bff381a60c3933 [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>
22#include <binder/IResultReceiver.h>
23#include <condition_variable>
24#include <mutex>
25#include <string>
26#include <thread>
Yao Chen41e606c2018-10-05 15:54:11 -070027#include "external/StatsPullerManager.h"
Yao Chena80e5c02018-09-04 13:55:29 -070028#include "frameworks/base/cmds/statsd/src/shell/shell_config.pb.h"
29#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
30#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 */
60class ShellSubscriber : public virtual IBinder::DeathRecipient {
61public:
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
65 /**
66 * Start a new subscription.
67 */
68 void startNewSubscription(int inFd, int outFd, sp<IResultReceiver> resultReceiver);
69
70 void binderDied(const wp<IBinder>& who);
71
72 void onLogEvent(const LogEvent& event);
73
74private:
Yao Chen41e606c2018-10-05 15:54:11 -070075 struct PullInfo {
76 PullInfo(const SimpleAtomMatcher& matcher, int64_t interval)
77 : mPullerMatcher(matcher), mInterval(interval), mPrevPullElapsedRealtimeMs(0) {
78 }
79 SimpleAtomMatcher mPullerMatcher;
80 int64_t mInterval;
81 int64_t mPrevPullElapsedRealtimeMs;
82 };
Yao Chena80e5c02018-09-04 13:55:29 -070083 void readConfig(int in);
84
85 void updateConfig(const ShellSubscription& config);
86
Yao Chen41e606c2018-10-05 15:54:11 -070087 void startPull(int64_t token, int64_t intervalMillis);
88
Yao Chena80e5c02018-09-04 13:55:29 -070089 void cleanUpLocked();
90
Yao Chen41e606c2018-10-05 15:54:11 -070091 void writeToOutputLocked(const vector<std::shared_ptr<LogEvent>>& data,
92 const SimpleAtomMatcher& matcher);
93
Yao Chena80e5c02018-09-04 13:55:29 -070094 sp<UidMap> mUidMap;
95
Yao Chen41e606c2018-10-05 15:54:11 -070096 sp<StatsPullerManager> mPullerMgr;
Yao Chena80e5c02018-09-04 13:55:29 -070097
98 android::util::ProtoOutputStream mProto;
99
100 mutable std::mutex mMutex;
101
102 std::condition_variable mShellDied; // semaphore for waiting until shell exits.
103
104 int mInput; // The input file descriptor
105
106 int mOutput; // The output file descriptor
107
108 sp<IResultReceiver> mResultReceiver;
109
110 std::vector<SimpleAtomMatcher> mPushedMatchers;
Yao Chen41e606c2018-10-05 15:54:11 -0700111
112 std::vector<PullInfo> mPulledInfo;
113
114 int64_t mPullToken = 0; // A unique token to identify a puller thread.
Yao Chena80e5c02018-09-04 13:55:29 -0700115};
116
117} // namespace statsd
118} // namespace os
119} // namespace android