blob: 0ace35fab8509bc5d806e8bbc1233a2f85779b3d [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>
27#include "frameworks/base/cmds/statsd/src/shell/shell_config.pb.h"
28#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
29#include "packages/UidMap.h"
30
31namespace android {
32namespace os {
33namespace statsd {
34
35/**
36 * Handles atoms subscription via shell cmd.
37 *
38 * A shell subscription lasts *until shell exits*. Unlike config based clients, a shell client
39 * communicates with statsd via file descriptors. They can subscribe pushed and pulled atoms.
40 * The atoms are sent back to the client in real time, as opposed to
41 * keeping the data in memory. Shell clients do not subscribe aggregated metrics, as they are
42 * responsible for doing the aggregation after receiving the atom events.
43 *
44 * Shell client pass ShellSubscription in the proto binary format. Client can update the
45 * subscription by sending a new subscription. The new subscription would replace the old one.
46 * Input data stream format is:
47 *
48 * |size_t|subscription proto|size_t|subscription proto|....
49 *
50 * statsd sends the events back in Atom proto binary format. Each Atom message is preceded
51 * with sizeof(size_t) bytes indicating the size of the proto message payload.
52 *
53 * The stream would be in the following format:
54 * |size_t|atom1 proto|size_t|atom2 proto|....
55 *
56 * Only one shell subscriber allowed at a time, because each shell subscriber blocks one thread
57 * until it exits.
58 */
59class ShellSubscriber : public virtual IBinder::DeathRecipient {
60public:
61 ShellSubscriber(sp<UidMap> uidMap) : mUidMap(uidMap){};
62
63 /**
64 * Start a new subscription.
65 */
66 void startNewSubscription(int inFd, int outFd, sp<IResultReceiver> resultReceiver);
67
68 void binderDied(const wp<IBinder>& who);
69
70 void onLogEvent(const LogEvent& event);
71
72private:
73 void readConfig(int in);
74
75 void updateConfig(const ShellSubscription& config);
76
77 void cleanUpLocked();
78
79 sp<UidMap> mUidMap;
80
81 // bool mWritten = false;
82
83 android::util::ProtoOutputStream mProto;
84
85 mutable std::mutex mMutex;
86
87 std::condition_variable mShellDied; // semaphore for waiting until shell exits.
88
89 int mInput; // The input file descriptor
90
91 int mOutput; // The output file descriptor
92
93 sp<IResultReceiver> mResultReceiver;
94
95 std::vector<SimpleAtomMatcher> mPushedMatchers;
96};
97
98} // namespace statsd
99} // namespace os
100} // namespace android