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