blob: 541f7e8be7fac89d1eb2afbe69411a3d48305c42 [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
Bookatzb487b552017-09-18 11:26:01 -070020#include "AnomalyMonitor.h"
David Chen0656b7a2017-09-13 15:53:39 -070021#include "StatsLogProcessor.h"
Chenjie Yu1a317ba2017-10-05 16:05:32 -070022#include "StatsPullerManager.h"
David Chende701692017-10-05 13:16:02 -070023#include "StatsPuller.h"
24#include "UidMap.h"
David Chen0656b7a2017-09-13 15:53:39 -070025
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070026#include <android/os/BnStatsManager.h>
Bookatzb487b552017-09-18 11:26:01 -070027#include <android/os/IStatsCompanionService.h>
Joe Onorato2cbc2cc2017-08-30 17:03:23 -070028#include <binder/IResultReceiver.h>
29#include <binder/IShellCallback.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070030#include <utils/Looper.h>
31
32#include <deque>
33#include <mutex>
34
35using namespace android;
36using namespace android::base;
37using namespace android::binder;
38using namespace android::os;
39using namespace std;
Bookatzb487b552017-09-18 11:26:01 -070040
Bookatz906a35c2017-09-20 15:26:44 -070041namespace android {
42namespace os {
43namespace statsd {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070044
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070045class StatsService : public BnStatsManager {
46public:
47 StatsService(const sp<Looper>& handlerLooper);
48 virtual ~StatsService();
49
Joe Onorato2cbc2cc2017-08-30 17:03:23 -070050 virtual status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070051
Joe Onorato2cbc2cc2017-08-30 17:03:23 -070052 virtual status_t dump(int fd, const Vector<String16>& args);
53
54 virtual status_t command(FILE* in, FILE* out, FILE* err, Vector<String8>& args);
55
56 virtual Status systemRunning();
Yao Chen482d2722017-09-12 13:25:43 -070057
Bookatzb487b552017-09-18 11:26:01 -070058 // Inform statsd that statsCompanion is ready.
59 virtual Status statsCompanionReady();
60
Bookatz1b0b1142017-09-08 11:58:42 -070061 virtual Status informAnomalyAlarmFired();
62
63 virtual Status informPollAlarmFired();
64
David Chende701692017-10-05 13:16:02 -070065 virtual Status informAllUidData(const vector<int32_t>& uid, const vector<int32_t>& version,
66 const vector<String16>& app);
67 virtual Status informOnePackage(const String16& app, int32_t uid, int32_t version);
68 virtual Status informOnePackageRemoved(const String16& app, int32_t uid);
69
David Chen0656b7a2017-09-13 15:53:39 -070070 virtual status_t setProcessor(const sp<StatsLogProcessor>& main_processor);
71
Yao Chenef99c4f2017-09-22 16:26:54 -070072 // TODO: public for testing since statsd doesn't run when system starts. Change to private
73 // later.
Bookatzb487b552017-09-18 11:26:01 -070074 /** Inform statsCompanion that statsd is ready. */
75 virtual void sayHiToStatsCompanion();
76
Bookatzc68a9d22017-09-27 14:09:55 -070077 // TODO: Move this to a more logical file/class
78 // TODO: Should be private. Temporarily public for testing purposes only.
79 const sp<AnomalyMonitor> mAnomalyMonitor;
Bookatzb487b552017-09-18 11:26:01 -070080
David Chende701692017-10-05 13:16:02 -070081 sp<UidMap> getUidMap() {
82 return m_UidMap;
83 }
84
Bookatzc68a9d22017-09-27 14:09:55 -070085 /** Fetches and returns the StatsCompanionService. */
86 static sp<IStatsCompanionService> getStatsCompanionService();
87
Chenjie Yu1a317ba2017-10-05 16:05:32 -070088private:
David Chende701692017-10-05 13:16:02 -070089 sp<UidMap> m_UidMap; // Reference to the UID map needed for translating UID to app name/version.
90
Bookatzc68a9d22017-09-27 14:09:55 -070091 sp<StatsLogProcessor> m_processor; // Reference to the processor for updating configs.
Bookatz906a35c2017-09-20 15:26:44 -070092
Yao Chen482d2722017-09-12 13:25:43 -070093 status_t doPrintStatsLog(FILE* out, const Vector<String8>& args);
Bookatzb487b552017-09-18 11:26:01 -070094
Yao Chen482d2722017-09-12 13:25:43 -070095 void printCmdHelp(FILE* out);
Bookatzb487b552017-09-18 11:26:01 -070096
David Chen0656b7a2017-09-13 15:53:39 -070097 status_t doLoadConfig(FILE* in);
Chenjie Yu1a317ba2017-10-05 16:05:32 -070098
99 StatsPullerManager mStatsPullerManager;
David Chende701692017-10-05 13:16:02 -0700100
101 status_t doPrintUidMap(FILE* out);
Bookatzb487b552017-09-18 11:26:01 -0700102};
103
104// --- StatsdDeathRecipient ---
105class StatsdDeathRecipient : public IBinder::DeathRecipient {
106public:
Yao Chenef99c4f2017-09-22 16:26:54 -0700107 StatsdDeathRecipient(sp<AnomalyMonitor> anomalyMonitor) : mAnmlyMntr(anomalyMonitor) {
Bookatzb487b552017-09-18 11:26:01 -0700108 }
109
110 virtual void binderDied(const wp<IBinder>& who);
111
112private:
113 const sp<AnomalyMonitor> mAnmlyMntr;
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700114};
115
Yao Chenef99c4f2017-09-22 16:26:54 -0700116} // namespace statsd
117} // namespace os
118} // namespace android
Bookatz906a35c2017-09-20 15:26:44 -0700119
Yao Chenef99c4f2017-09-22 16:26:54 -0700120#endif // STATS_SERVICE_H