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 <gtest/gtest_prod.h> 21 #include "StatsLogProcessor.h" 22 #include "anomaly/AlarmMonitor.h" 23 #include "config/ConfigManager.h" 24 #include "external/StatsPullerManager.h" 25 #include "packages/UidMap.h" 26 #include "statscompanion_util.h" 27 28 #include <android/os/BnStatsManager.h> 29 #include <android/os/IStatsCompanionService.h> 30 #include <binder/IResultReceiver.h> 31 #include <utils/Looper.h> 32 33 #include <deque> 34 #include <mutex> 35 36 using namespace android; 37 using namespace android::base; 38 using namespace android::binder; 39 using namespace android::os; 40 using namespace std; 41 42 namespace android { 43 namespace os { 44 namespace statsd { 45 46 class StatsService : public BnStatsManager, public LogListener, public IBinder::DeathRecipient { 47 public: 48 StatsService(const sp<Looper>& handlerLooper); 49 virtual ~StatsService(); 50 51 /** The anomaly alarm registered with AlarmManager won't be updated by less than this. */ 52 // TODO: Consider making this configurable. And choose a good number. 53 const uint32_t MIN_DIFF_TO_UPDATE_REGISTERED_ALARM_SECS = 5; 54 55 virtual status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags); 56 virtual status_t dump(int fd, const Vector<String16>& args); 57 virtual status_t command(FILE* in, FILE* out, FILE* err, Vector<String8>& args); 58 59 virtual Status systemRunning(); 60 virtual Status statsCompanionReady(); 61 virtual Status informAnomalyAlarmFired(); 62 virtual Status informPollAlarmFired(); 63 virtual Status informAlarmForSubscriberTriggeringFired(); 64 65 virtual Status informAllUidData(const vector<int32_t>& uid, const vector<int64_t>& version, 66 const vector<String16>& app); 67 virtual Status informOnePackage(const String16& app, int32_t uid, int64_t version); 68 virtual Status informOnePackageRemoved(const String16& app, int32_t uid); 69 virtual Status informDeviceShutdown(); 70 71 /** 72 * Called right before we start processing events. 73 */ 74 void Startup(); 75 76 /** 77 * Called by LogReader when there's a log event to process. 78 */ 79 virtual void OnLogEvent(LogEvent* event, bool reconnectionStarts); 80 81 /** 82 * Binder call for clients to request data for this configuration key. 83 */ 84 virtual Status getData(int64_t key, 85 const String16& packageName, 86 vector<uint8_t>* output) override; 87 88 89 /** 90 * Binder call for clients to get metadata across all configs in statsd. 91 */ 92 virtual Status getMetadata(const String16& packageName, 93 vector<uint8_t>* output) override; 94 95 96 /** 97 * Binder call to let clients send a configuration and indicate they're interested when they 98 * should requestData for this configuration. 99 */ 100 virtual Status addConfiguration(int64_t key, 101 const vector<uint8_t>& config, 102 const String16& packageName) override; 103 104 /** 105 * Binder call to let clients register the data fetch operation for a configuration. 106 */ 107 virtual Status setDataFetchOperation(int64_t key, 108 const sp<android::IBinder>& intentSender, 109 const String16& packageName) override; 110 111 /** 112 * Binder call to remove the data fetch operation for the specified config key. 113 */ 114 virtual Status removeDataFetchOperation(int64_t key, 115 const String16& packageName) override; 116 117 /** 118 * Binder call to allow clients to remove the specified configuration. 119 */ 120 virtual Status removeConfiguration(int64_t key, 121 const String16& packageName) override; 122 123 /** 124 * Binder call to associate the given config's subscriberId with the given intentSender. 125 * intentSender must be convertible into an IntentSender (in Java) using IntentSender(IBinder). 126 */ 127 virtual Status setBroadcastSubscriber(int64_t configId, 128 int64_t subscriberId, 129 const sp<android::IBinder>& intentSender, 130 const String16& packageName) override; 131 132 /** 133 * Binder call to unassociate the given config's subscriberId with any intentSender. 134 */ 135 virtual Status unsetBroadcastSubscriber(int64_t configId, 136 int64_t subscriberId, 137 const String16& packageName) override; 138 139 /** Inform statsCompanion that statsd is ready. */ 140 virtual void sayHiToStatsCompanion(); 141 142 /** 143 * Binder call to get AppBreadcrumbReported atom. 144 */ 145 virtual Status sendAppBreadcrumbAtom(int32_t label, int32_t state) override; 146 147 /** IBinder::DeathRecipient */ 148 virtual void binderDied(const wp<IBinder>& who) override; 149 150 private: 151 /** 152 * Load system properties at init. 153 */ 154 void init_system_properties(); 155 156 /** 157 * Helper for loading system properties. 158 */ 159 static void init_build_type_callback(void* cookie, const char* name, const char* value, 160 uint32_t serial); 161 162 /** 163 * Text or proto output of dumpsys. 164 */ 165 void dump_impl(FILE* out, bool verbose, bool proto); 166 167 /** 168 * Print usage information for the commands 169 */ 170 void print_cmd_help(FILE* out); 171 172 /** 173 * Trigger a broadcast. 174 */ 175 status_t cmd_trigger_broadcast(FILE* out, Vector<String8>& args); 176 177 /** 178 * Handle the config sub-command. 179 */ 180 status_t cmd_config(FILE* in, FILE* out, FILE* err, Vector<String8>& args); 181 182 /** 183 * Prints some basic stats to std out. 184 */ 185 status_t cmd_print_stats(FILE* out, const Vector<String8>& args); 186 187 /** 188 * Print the event log. 189 */ 190 status_t cmd_dump_report(FILE* out, FILE* err, const Vector<String8>& args); 191 192 /** 193 * Print the mapping of uids to package names. 194 */ 195 status_t cmd_print_uid_map(FILE* out, const Vector<String8>& args); 196 197 /** 198 * Flush the data to disk. 199 */ 200 status_t cmd_write_data_to_disk(FILE* out); 201 202 /** 203 * Write an AppBreadcrumbReported event to the StatsLog buffer, as if calling 204 * StatsLog.write(APP_BREADCRUMB_REPORTED). 205 */ 206 status_t cmd_log_app_breadcrumb(FILE* out, const Vector<String8>& args); 207 208 /** 209 * Print contents of a pulled metrics source. 210 */ 211 status_t cmd_print_pulled_metrics(FILE* out, const Vector<String8>& args); 212 213 /** 214 * Removes all configs stored on disk and on memory. 215 */ 216 status_t cmd_remove_all_configs(FILE* out); 217 218 /* 219 * Dump memory usage by statsd. 220 */ 221 status_t cmd_dump_memory_info(FILE* out); 222 223 /* 224 * Clear all puller cached data 225 */ 226 status_t cmd_clear_puller_cache(FILE* out); 227 228 /** 229 * Print all stats logs received to logcat. 230 */ 231 status_t cmd_print_logs(FILE* out, const Vector<String8>& args); 232 233 /** 234 * Adds a configuration after checking permissions and obtaining UID from binder call. 235 */ 236 bool addConfigurationChecked(int uid, int64_t key, const vector<uint8_t>& config); 237 238 /** 239 * Update a configuration. 240 */ 241 void set_config(int uid, const string& name, const StatsdConfig& config); 242 243 /** 244 * Tracks the uid <--> package name mapping. 245 */ 246 sp<UidMap> mUidMap; 247 248 /** 249 * Fetches external metrics. 250 */ 251 StatsPullerManager mStatsPullerManager; 252 253 /** 254 * Tracks the configurations that have been passed to statsd. 255 */ 256 sp<ConfigManager> mConfigManager; 257 258 /** 259 * The metrics recorder. 260 */ 261 sp<StatsLogProcessor> mProcessor; 262 263 /** 264 * The alarm monitor for anomaly detection. 265 */ 266 const sp<AlarmMonitor> mAnomalyAlarmMonitor; 267 268 /** 269 * The alarm monitor for alarms to directly trigger subscriber. 270 */ 271 const sp<AlarmMonitor> mPeriodicAlarmMonitor; 272 273 /** 274 * Whether this is an eng build. 275 */ 276 bool mEngBuild; 277 278 FRIEND_TEST(StatsServiceTest, TestAddConfig_simple); 279 FRIEND_TEST(StatsServiceTest, TestAddConfig_empty); 280 FRIEND_TEST(StatsServiceTest, TestAddConfig_invalid); 281 FRIEND_TEST(PartialBucketE2eTest, TestCountMetricNoSplitOnNewApp); 282 FRIEND_TEST(PartialBucketE2eTest, TestCountMetricSplitOnUpgrade); 283 FRIEND_TEST(PartialBucketE2eTest, TestCountMetricSplitOnRemoval); 284 FRIEND_TEST(PartialBucketE2eTest, TestCountMetricWithoutSplit); 285 FRIEND_TEST(PartialBucketE2eTest, TestValueMetricWithoutMinPartialBucket); 286 FRIEND_TEST(PartialBucketE2eTest, TestValueMetricWithMinPartialBucket); 287 FRIEND_TEST(PartialBucketE2eTest, TestGaugeMetricWithoutMinPartialBucket); 288 FRIEND_TEST(PartialBucketE2eTest, TestGaugeMetricWithMinPartialBucket); 289 }; 290 291 } // namespace statsd 292 } // namespace os 293 } // namespace android 294 295 #endif // STATS_SERVICE_H 296