blob: 7f046584b2d0bf38e98cd65998d75956b797841c [file] [log] [blame]
Joe Onorato5dcbc6c2017-08-29 15:13:58 -07001/*
2 * Copyright (C) 2016 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#ifndef STATS_SERVICE_H
18#define STATS_SERVICE_H
19
David Chen0656b7a2017-09-13 15:53:39 -070020#include "StatsLogProcessor.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070021#include "anomaly/AnomalyMonitor.h"
22#include "config/ConfigManager.h"
23#include "external/StatsPullerManager.h"
24#include "packages/UidMap.h"
David Chen0656b7a2017-09-13 15:53:39 -070025
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070026#include <android/os/BnStatsManager.h>
yro31eb67b2017-10-24 13:33:21 -070027#include <android/os/IStatsCallbacks.h>
Bookatzb487b552017-09-18 11:26:01 -070028#include <android/os/IStatsCompanionService.h>
Joe Onorato2cbc2cc2017-08-30 17:03:23 -070029#include <binder/IResultReceiver.h>
30#include <binder/IShellCallback.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070031#include <utils/Looper.h>
32
33#include <deque>
34#include <mutex>
35
36using namespace android;
37using namespace android::base;
38using namespace android::binder;
39using namespace android::os;
40using namespace std;
Bookatzb487b552017-09-18 11:26:01 -070041
Bookatz906a35c2017-09-20 15:26:44 -070042namespace android {
43namespace os {
44namespace statsd {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070045
yro31eb67b2017-10-24 13:33:21 -070046class StatsService : public BnStatsManager, public LogListener, public IBinder::DeathRecipient {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070047public:
48 StatsService(const sp<Looper>& handlerLooper);
49 virtual ~StatsService();
50
Joe Onorato2cbc2cc2017-08-30 17:03:23 -070051 virtual status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
Joe Onorato2cbc2cc2017-08-30 17:03:23 -070052 virtual status_t dump(int fd, const Vector<String16>& args);
Joe Onorato2cbc2cc2017-08-30 17:03:23 -070053 virtual status_t command(FILE* in, FILE* out, FILE* err, Vector<String8>& args);
54
55 virtual Status systemRunning();
Bookatzb487b552017-09-18 11:26:01 -070056 virtual Status statsCompanionReady();
Bookatz1b0b1142017-09-08 11:58:42 -070057 virtual Status informAnomalyAlarmFired();
Bookatz1b0b1142017-09-08 11:58:42 -070058 virtual Status informPollAlarmFired();
David Chende701692017-10-05 13:16:02 -070059 virtual Status informAllUidData(const vector<int32_t>& uid, const vector<int32_t>& version,
60 const vector<String16>& app);
61 virtual Status informOnePackage(const String16& app, int32_t uid, int32_t version);
62 virtual Status informOnePackageRemoved(const String16& app, int32_t uid);
63
Joe Onorato9fc9edf2017-10-15 20:08:52 -070064 /**
65 * Called right before we start processing events.
66 */
67 void Startup();
68
69 /**
70 * Called by LogReader when there's a log event to process.
71 */
Joe Onoratoc4dfae52017-10-17 23:38:21 -070072 virtual void OnLogEvent(const LogEvent& event);
David Chen0656b7a2017-09-13 15:53:39 -070073
yro31eb67b2017-10-24 13:33:21 -070074 /**
75 * Binder call to force trigger pushLog. This would be called by callback
76 * clients.
77 */
78 virtual Status requestPush() override;
79
80 /**
81 * Pushes stats log entries from statsd to callback clients.
82 */
83 Status pushLog(const vector<uint8_t>& log);
84
85 /**
86 * Binder call to listen to statsd to send stats log entries.
87 */
88 virtual Status subscribeStatsLog(const sp<IStatsCallbacks>& callbacks) override;
89
Yao Chenef99c4f2017-09-22 16:26:54 -070090 // TODO: public for testing since statsd doesn't run when system starts. Change to private
91 // later.
Bookatzb487b552017-09-18 11:26:01 -070092 /** Inform statsCompanion that statsd is ready. */
93 virtual void sayHiToStatsCompanion();
94
Bookatzc68a9d22017-09-27 14:09:55 -070095 /** Fetches and returns the StatsCompanionService. */
96 static sp<IStatsCompanionService> getStatsCompanionService();
97
yro31eb67b2017-10-24 13:33:21 -070098 /** IBinder::DeathRecipient */
99 virtual void binderDied(const wp<IBinder>& who) override;
100
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700101private:
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700102 /**
103 * Load system properties at init.
104 */
105 void init_system_properties();
David Chende701692017-10-05 13:16:02 -0700106
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700107 /**
108 * Helper for loading system properties.
109 */
110 static void init_build_type_callback(void* cookie, const char* name, const char* value,
111 uint32_t serial);
Bookatz906a35c2017-09-20 15:26:44 -0700112
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700113 /**
114 * Text output of dumpsys.
115 */
116 void dump_impl(FILE* out);
Bookatzb487b552017-09-18 11:26:01 -0700117
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700118 /**
119 * Print usage information for the commands
120 */
121 void print_cmd_help(FILE* out);
Bookatzb487b552017-09-18 11:26:01 -0700122
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700123 /**
124 * Handle the config sub-command.
125 */
126 status_t cmd_config(FILE* in, FILE* out, FILE* err, Vector<String8>& args);
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700127
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700128 /**
129 * Print the event log.
130 */
131 status_t cmd_print_stats_log(FILE* out, const Vector<String8>& args);
132
133 /**
Yao Chen729093d2017-10-16 10:33:26 -0700134 * Print the event log.
135 */
136 status_t cmd_dump_report(FILE* out, FILE* err, const Vector<String8>& args);
137
138 /**
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700139 * Print the mapping of uids to package names.
140 */
141 status_t cmd_print_uid_map(FILE* out);
142
143 /**
David Chen1481fe12017-10-16 13:16:34 -0700144 * Print contents of a pulled metrics source.
145 */
146 status_t cmd_print_pulled_metrics(FILE* out, const Vector<String8>& args);
147
148 /**
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700149 * Update a configuration.
150 */
151 void set_config(int uid, const string& name, const StatsdConfig& config);
152
153 /**
154 * Tracks the uid <--> package name mapping.
155 */
156 sp<UidMap> mUidMap;
157
158 /**
159 * Fetches external metrics.
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700160 */
David Chen1481fe12017-10-16 13:16:34 -0700161 sp<StatsPullerManager> mStatsPullerManager;
David Chende701692017-10-05 13:16:02 -0700162
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700163 /**
164 * Tracks the configurations that have been passed to statsd.
165 */
166 sp<ConfigManager> mConfigManager;
Bookatzb487b552017-09-18 11:26:01 -0700167
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700168 /**
169 * The metrics recorder.
170 */
171 sp<StatsLogProcessor> mProcessor;
Bookatzb487b552017-09-18 11:26:01 -0700172
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700173 /**
174 * The anomaly detector.
175 */
176 const sp<AnomalyMonitor> mAnomalyMonitor;
Bookatzb487b552017-09-18 11:26:01 -0700177
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700178 /**
179 * Whether this is an eng build.
180 */
181 bool mEngBuild;
yro31eb67b2017-10-24 13:33:21 -0700182
183 /**
184 * Lock for callback handling.
185 */
186 std::mutex mLock;
187
188 /**
189 * Vector maintaining the list of callbacks for clients.
190 */
191 Vector< sp<IStatsCallbacks> > mCallbacks;
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700192};
193
Yao Chenef99c4f2017-09-22 16:26:54 -0700194} // namespace statsd
195} // namespace os
196} // namespace android
Bookatz906a35c2017-09-20 15:26:44 -0700197
Yao Chenef99c4f2017-09-22 16:26:54 -0700198#endif // STATS_SERVICE_H