• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 
20 #include <aidl/android/os/BnStatsd.h>
21 #include <aidl/android/os/IPendingIntentRef.h>
22 #include <aidl/android/os/IPullAtomCallback.h>
23 #include <aidl/android/os/IStatsSubscriptionCallback.h>
24 #include <aidl/android/util/PropertyParcel.h>
25 #include <gtest/gtest_prod.h>
26 #include <utils/Looper.h>
27 
28 #include <mutex>
29 
30 #include "StatsLogProcessor.h"
31 #include "anomaly/AlarmMonitor.h"
32 #include "config/ConfigManager.h"
33 #include "external/StatsPullerManager.h"
34 #include "logd/LogEventQueue.h"
35 #include "packages/UidMap.h"
36 #include "shell/ShellSubscriber.h"
37 #include "statscompanion_util.h"
38 #include "utils/MultiConditionTrigger.h"
39 
40 using namespace android;
41 using namespace android::os;
42 using namespace std;
43 
44 using ::ndk::SpAIBinder;
45 using Status = ::ndk::ScopedAStatus;
46 using aidl::android::os::BnStatsd;
47 using aidl::android::os::IPendingIntentRef;
48 using aidl::android::os::IPullAtomCallback;
49 using aidl::android::os::IStatsQueryCallback;
50 using aidl::android::os::IStatsSubscriptionCallback;
51 using aidl::android::util::PropertyParcel;
52 using ::ndk::ScopedAIBinder_DeathRecipient;
53 using ::ndk::ScopedFileDescriptor;
54 
55 namespace android {
56 namespace os {
57 namespace statsd {
58 
59 class StatsService : public BnStatsd {
60 public:
61     StatsService(const sp<UidMap>& uidMap, shared_ptr<LogEventQueue> queue,
62                  const std::shared_ptr<LogEventFilter>& logEventFilter,
63                  int initEventDelaySecs = kStatsdInitDelaySecs);
64     virtual ~StatsService();
65 
66     /** The anomaly alarm registered with AlarmManager won't be updated by less than this. */
67     const uint32_t MIN_DIFF_TO_UPDATE_REGISTERED_ALARM_SECS = 5;
68 
69     virtual status_t dump(int fd, const char** args, uint32_t numArgs) override;
70     virtual status_t handleShellCommand(int in, int out, int err, const char** argv,
71                                         uint32_t argc) override;
72 
73     virtual Status systemRunning();
74     virtual Status statsCompanionReady();
75     virtual Status bootCompleted();
76     virtual Status informAnomalyAlarmFired();
77     virtual Status informPollAlarmFired();
78     virtual Status informAlarmForSubscriberTriggeringFired();
79 
80     virtual Status informAllUidData(const ScopedFileDescriptor& fd);
81     virtual Status informOnePackage(const string& app, int32_t uid, int64_t version,
82                                     const string& versionString, const string& installer,
83                                     const vector<uint8_t>& certificateHash);
84     virtual Status informOnePackageRemoved(const string& app, int32_t uid);
85     virtual Status informDeviceShutdown();
86 
87     /**
88      * Called right before we start processing events.
89      */
90     void Startup();
91 
92     /**
93      * Called when terminiation signal received.
94      */
95     void Terminate();
96 
97     /**
98      * Test ONLY interface. In real world, StatsService reads from LogEventQueue.
99      */
100     virtual void OnLogEvent(LogEvent* event);
101 
102     /**
103      * Binder call for clients to request data for this configuration key.
104      */
105     virtual Status getData(int64_t key,
106                            const int32_t callingUid,
107                            vector<uint8_t>* output) override;
108 
109 
110     /**
111      * Binder call for clients to get metadata across all configs in statsd.
112      */
113     virtual Status getMetadata(vector<uint8_t>* output) override;
114 
115 
116     /**
117      * Binder call to let clients send a configuration and indicate they're interested when they
118      * should requestData for this configuration.
119      */
120     virtual Status addConfiguration(int64_t key,
121                                     const vector<uint8_t>& config,
122                                     const int32_t callingUid) override;
123 
124     /**
125      * Binder call to let clients register the data fetch operation for a configuration.
126      */
127     virtual Status setDataFetchOperation(int64_t key,
128                                          const shared_ptr<IPendingIntentRef>& pir,
129                                          const int32_t callingUid) override;
130 
131     /**
132      * Binder call to remove the data fetch operation for the specified config key.
133      */
134     virtual Status removeDataFetchOperation(int64_t key,
135                                             const int32_t callingUid) override;
136 
137     /**
138      * Binder call to let clients register the active configs changed operation.
139      */
140     virtual Status setActiveConfigsChangedOperation(const shared_ptr<IPendingIntentRef>& pir,
141                                                     const int32_t callingUid,
142                                                     vector<int64_t>* output) override;
143 
144     /**
145      * Binder call to remove the active configs changed operation for the specified package..
146      */
147     virtual Status removeActiveConfigsChangedOperation(const int32_t callingUid) override;
148     /**
149      * Binder call to allow clients to remove the specified configuration.
150      */
151     virtual Status removeConfiguration(int64_t key,
152                                        const int32_t callingUid) override;
153 
154     /**
155      * Binder call to associate the given config's subscriberId with the given pendingIntentRef.
156      */
157     virtual Status setBroadcastSubscriber(int64_t configId,
158                                           int64_t subscriberId,
159                                           const shared_ptr<IPendingIntentRef>& pir,
160                                           const int32_t callingUid) override;
161 
162     /**
163      * Binder call to unassociate the given config's subscriberId with any pendingIntentRef.
164      */
165     virtual Status unsetBroadcastSubscriber(int64_t configId,
166                                             int64_t subscriberId,
167                                             const int32_t callingUid) override;
168 
169     /** Inform statsCompanion that statsd is ready. */
170     virtual void sayHiToStatsCompanion();
171 
172     /**
173      * Binder call to notify statsd that all pullers from boot have been registered.
174      */
175     virtual Status allPullersFromBootRegistered();
176 
177     /**
178      * Binder call to register a callback function for a pulled atom.
179      */
180     virtual Status registerPullAtomCallback(
181             int32_t uid, int32_t atomTag, int64_t coolDownMillis, int64_t timeoutMillis,
182             const vector<int32_t>& additiveFields,
183             const shared_ptr<IPullAtomCallback>& pullerCallback) override;
184 
185     /**
186      * Binder call to register a callback function for a pulled atom.
187      */
188     virtual Status registerNativePullAtomCallback(
189             int32_t atomTag, int64_t coolDownMillis, int64_t timeoutMillis,
190             const vector<int32_t>& additiveFields,
191             const shared_ptr<IPullAtomCallback>& pullerCallback) override;
192 
193     /**
194      * Binder call to unregister any existing callback for the given uid and atom.
195      */
196     virtual Status unregisterPullAtomCallback(int32_t uid, int32_t atomTag) override;
197 
198     /**
199      * Binder call to unregister any existing callback for the given atom and calling uid.
200      */
201     virtual Status unregisterNativePullAtomCallback(int32_t atomTag) override;
202 
203     /**
204      * Binder call to get registered experiment IDs.
205      */
206     virtual Status getRegisteredExperimentIds(vector<int64_t>* expIdsOut);
207 
208     /**
209      * Binder call to update properties in statsd_java namespace.
210      */
211     virtual Status updateProperties(const vector<PropertyParcel>& properties);
212 
213     /**
214      * Binder call to let clients register the restricted metrics changed operation for the given
215      * config and calling uid.
216      */
217     virtual Status setRestrictedMetricsChangedOperation(const int64_t configKey,
218                                                         const string& configPackage,
219                                                         const shared_ptr<IPendingIntentRef>& pir,
220                                                         const int32_t callingUid,
221                                                         vector<int64_t>* output);
222 
223     /**
224      * Binder call to remove the restricted metrics changed operation for the specified config
225      * and calling uid.
226      */
227     virtual Status removeRestrictedMetricsChangedOperation(const int64_t configKey,
228                                                            const string& configPackage,
229                                                            const int32_t callingUid);
230 
231     /**
232      * Binder call to query data in statsd sql store.
233      */
234     virtual Status querySql(const string& sqlQuery, const int32_t minSqlClientVersion,
235                             const optional<vector<uint8_t>>& policyConfig,
236                             const shared_ptr<IStatsQueryCallback>& callback,
237                             const int64_t configKey, const string& configPackage,
238                             const int32_t callingUid);
239 
240     /**
241      * Binder call to add a subscription.
242      */
243     virtual Status addSubscription(const vector<uint8_t>& subscriptionConfig,
244                                    const shared_ptr<IStatsSubscriptionCallback>& callback) override;
245 
246     /**
247      * Binder call to remove a subscription.
248      */
249     virtual Status removeSubscription(
250             const shared_ptr<IStatsSubscriptionCallback>& callback) override;
251 
252     /**
253      * Binder call to flush atom events for a subscription.
254      */
255     virtual Status flushSubscription(
256             const shared_ptr<IStatsSubscriptionCallback>& callback) override;
257 
258     const static int kStatsdInitDelaySecs = 90;
259 
260 private:
261     /**
262      * Load system properties at init.
263      */
264     void init_system_properties();
265 
266     /**
267      * Helper for loading system properties.
268      */
269     static void init_build_type_callback(void* cookie, const char* name, const char* value,
270                                          uint32_t serial);
271 
272     /**
273      * Proto output of statsd report data dumpsys, wrapped in a StatsDataDumpProto.
274      */
275     void dumpIncidentSection(int outFd);
276 
277     /**
278      * Text or proto output of statsdStats dumpsys.
279      */
280     void dumpStatsdStats(int outFd, bool verbose, bool proto);
281 
282     /**
283      * Print usage information for the commands
284      */
285     void print_cmd_help(int out);
286 
287     /* Runs on its dedicated thread to process pushed stats event from socket. */
288     void readLogs();
289 
290     /**
291      * Trigger a broadcast.
292      */
293     status_t cmd_trigger_broadcast(int outFd, Vector<String8>& args);
294 
295 
296     /**
297      * Trigger an active configs changed broadcast.
298      */
299     status_t cmd_trigger_active_config_broadcast(int outFd, Vector<String8>& args);
300 
301     /**
302      * Handle the config sub-command.
303      */
304     status_t cmd_config(int inFd, int outFd, int err, Vector<String8>& args);
305 
306     /**
307      * Prints some basic stats to std out.
308      */
309     status_t cmd_print_stats(int outFd, const Vector<String8>& args);
310 
311     /**
312      * Print the event log.
313      */
314     status_t cmd_dump_report(int outFd, const Vector<String8>& args);
315 
316     /**
317      * Print the mapping of uids to package names.
318      */
319     status_t cmd_print_uid_map(int outFd, const Vector<String8>& args);
320 
321     /**
322      * Flush the data to disk.
323      */
324     status_t cmd_write_data_to_disk(int outFd);
325 
326     /**
327      * Write an AppBreadcrumbReported event to the StatsLog buffer, as if calling
328      * StatsLog.write(APP_BREADCRUMB_REPORTED).
329      */
330     status_t cmd_log_app_breadcrumb(int outFd, const Vector<String8>& args);
331 
332     /**
333      * Write an BinaryPushStateChanged event, as if calling StatsLog.logBinaryPushStateChanged().
334      */
335     status_t cmd_log_binary_push(int outFd, const Vector<String8>& args);
336 
337     /**
338      * Print contents of a pulled metrics source.
339      */
340     status_t cmd_print_pulled_metrics(int outFd, const Vector<String8>& args);
341 
342     /**
343      * Removes all configs stored on disk and on memory.
344      */
345     status_t cmd_remove_all_configs(int outFd);
346 
347     /*
348      * Dump memory usage by statsd.
349      */
350     status_t cmd_dump_memory_info(int outFd);
351 
352     /*
353      * Clear all puller cached data
354      */
355     status_t cmd_clear_puller_cache(int outFd);
356 
357     /**
358      * Print all stats logs received to logcat.
359      */
360     status_t cmd_print_logs(int outFd, const Vector<String8>& args);
361 
362     /**
363      * Writes the value of args[uidArgIndex] into uid.
364      * Returns whether the uid is reasonable (type uid_t) and whether
365      * 1. it is equal to the calling uid, or
366      * 2. the device is mEngBuild, or
367      * 3. the caller is AID_ROOT and the uid is AID_SHELL (i.e. ROOT can impersonate SHELL).
368      */
369     bool getUidFromArgs(const Vector<String8>& args, size_t uidArgIndex, int32_t& uid);
370 
371     /**
372      * Writes the value of uidSting into uid.
373      * Returns whether the uid is reasonable (type uid_t) and whether
374      * 1. it is equal to the calling uid, or
375      * 2. the device is mEngBuild, or
376      * 3. the caller is AID_ROOT and the uid is AID_SHELL (i.e. ROOT can impersonate SHELL).
377      */
378      bool getUidFromString(const char* uidString, int32_t& uid);
379 
380     /**
381      * Adds a configuration after checking permissions and obtaining UID from binder call.
382      */
383     bool addConfigurationChecked(int uid, int64_t key, const vector<uint8_t>& config);
384 
385     /**
386      * Update a configuration.
387      */
388     void set_config(int uid, const string& name, const StatsdConfig& config);
389 
390     /**
391      * Death recipient callback that is called when StatsCompanionService dies.
392      * The cookie is a pointer to a StatsService object.
393      */
394     static void statsCompanionServiceDied(void* cookie);
395 
396     /**
397      * Implementation of statsCompanionServiceDied.
398      */
399     void statsCompanionServiceDiedImpl();
400 
401     /**
402      * Initialize ShellSubscriber
403      */
404     void initShellSubscriber();
405 
406     /*
407      * Notify StatsLogProcessor of boot completed
408      */
409     void onStatsdInitCompleted();
410 
411     /**
412      *  This method is used to stop log reader thread.
413      */
414     void stopReadingLogs();
415 
416     std::atomic<bool> mIsStopRequested = false;
417 
418     /**
419      * Tracks the uid <--> package name mapping.
420      */
421     const sp<UidMap> mUidMap;
422 
423     /**
424      * Fetches external metrics
425      */
426     sp<StatsPullerManager> mPullerManager;
427 
428     /**
429      * Tracks the configurations that have been passed to statsd.
430      */
431     sp<ConfigManager> mConfigManager;
432 
433     /**
434      * The metrics recorder.
435      */
436     sp<StatsLogProcessor> mProcessor;
437 
438     /**
439      * The alarm monitor for anomaly detection.
440      */
441     const sp<AlarmMonitor> mAnomalyAlarmMonitor;
442 
443     /**
444      * The alarm monitor for alarms to directly trigger subscriber.
445      */
446     const sp<AlarmMonitor> mPeriodicAlarmMonitor;
447 
448     /**
449      * Whether this is an eng build.
450      */
451     bool mEngBuild;
452 
453     sp<ShellSubscriber> mShellSubscriber;
454 
455     /**
456      * Mutex for setting the shell subscriber
457      */
458     mutable mutex mShellSubscriberMutex;
459     shared_ptr<LogEventQueue> mEventQueue;
460     std::shared_ptr<LogEventFilter> mLogEventFilter;
461 
462     std::unique_ptr<std::thread> mLogsReaderThread;
463 
464     MultiConditionTrigger mBootCompleteTrigger;
465     static const inline string kBootCompleteTag = "BOOT_COMPLETE";
466     static const inline string kUidMapReceivedTag = "UID_MAP";
467     static const inline string kAllPullersRegisteredTag = "PULLERS_REGISTERED";
468 
469     ScopedAIBinder_DeathRecipient mStatsCompanionServiceDeathRecipient;
470 
471     const int mInitEventDelaySecs;
472 
473     friend class StatsServiceConfigTest;
474     friend class StatsServiceStatsdInitTest;
475     friend class RestrictedConfigE2ETest;
476 
477     FRIEND_TEST(StatsLogProcessorTest, TestActivationsPersistAcrossSystemServerRestart);
478     FRIEND_TEST(StatsServiceTest, TestAddConfig_simple);
479     FRIEND_TEST(StatsServiceTest, TestAddConfig_empty);
480     FRIEND_TEST(StatsServiceTest, TestAddConfig_invalid);
481     FRIEND_TEST(StatsServiceTest, TestGetUidFromArgs);
482     FRIEND_TEST(PartialBucketE2eTest, TestCountMetricNoSplitOnNewApp);
483     FRIEND_TEST(PartialBucketE2eTest, TestCountMetricSplitOnBoot);
484     FRIEND_TEST(PartialBucketE2eTest, TestCountMetricSplitOnUpgrade);
485     FRIEND_TEST(PartialBucketE2eTest, TestCountMetricSplitOnRemoval);
486     FRIEND_TEST(PartialBucketE2eTest, TestCountMetricWithoutSplit);
487     FRIEND_TEST(PartialBucketE2eTest, TestCountMetricNoSplitOnUpgradeWhenDisabled);
488     FRIEND_TEST(PartialBucketE2eTest, TestValueMetricOnBootWithoutMinPartialBucket);
489     FRIEND_TEST(PartialBucketE2eTest, TestValueMetricWithoutMinPartialBucket);
490     FRIEND_TEST(PartialBucketE2eTest, TestValueMetricWithMinPartialBucket);
491     FRIEND_TEST(PartialBucketE2eTest, TestGaugeMetricOnBootWithoutMinPartialBucket);
492     FRIEND_TEST(PartialBucketE2eTest, TestGaugeMetricWithoutMinPartialBucket);
493     FRIEND_TEST(PartialBucketE2eTest, TestGaugeMetricWithMinPartialBucket);
494     FRIEND_TEST(PartialBucketE2eTest, TestCountMetricNoSplitByDefault);
495     FRIEND_TEST(RestrictedConfigE2ETest, NonRestrictedConfigGetReport);
496     FRIEND_TEST(RestrictedConfigE2ETest, RestrictedConfigNoReport);
497     FRIEND_TEST(RestrictedConfigE2ETest,
498                 TestSendRestrictedMetricsChangedBroadcastMultipleMatchedConfigs);
499     FRIEND_TEST(ConfigUpdateE2eTest, TestAnomalyDurationMetric);
500 
501     FRIEND_TEST(AnomalyDurationDetectionE2eTest, TestDurationMetric_SUM_single_bucket);
502     FRIEND_TEST(AnomalyDurationDetectionE2eTest, TestDurationMetric_SUM_partial_bucket);
503     FRIEND_TEST(AnomalyDurationDetectionE2eTest, TestDurationMetric_SUM_multiple_buckets);
504     FRIEND_TEST(AnomalyDurationDetectionE2eTest, TestDurationMetric_SUM_long_refractory_period);
505 
506     FRIEND_TEST(StatsServiceStatsdInitTest, StatsServiceStatsdInitTest);
507 };
508 
509 }  // namespace statsd
510 }  // namespace os
511 }  // namespace android
512 
513 #endif  // STATS_SERVICE_H
514