Remove alarm manager dependency from anomalyalarms

Anomaly alarms, which are used for prediction-base duration metric
anomaly detection, currently use alarm manager for setting alarms. This
can result in excessive binder calls to SCS to set/cancel the alarms if
the duration metric that the alarm is based on gets stopped/started in
quick succession.

To resolve this without losing precision, we can instead rely on the
flow of events into the statsd socket for this prediction. The socket
receives events very frequently, so there should be minimal (if any)
loss of precision, and we can remove the binder call entirely.

One concern is that we used to hold a wakelock when the alarm fired
(since alarm manager would hold a wakelock), and we now lose this
ability.

Test: atest statsd_test
Test: atest AnomalyDetectionTests
Bug: 161326200
Change-Id: If37e5c6ccd8efa4e822c70a0444fa6b5a31bebca
diff --git a/cmds/statsd/src/StatsService.cpp b/cmds/statsd/src/StatsService.cpp
index d5e3314..99dadb5 100644
--- a/cmds/statsd/src/StatsService.cpp
+++ b/cmds/statsd/src/StatsService.cpp
@@ -91,17 +91,13 @@
 StatsService::StatsService(const sp<Looper>& handlerLooper, shared_ptr<LogEventQueue> queue)
     : mAnomalyAlarmMonitor(new AlarmMonitor(
               MIN_DIFF_TO_UPDATE_REGISTERED_ALARM_SECS,
-              [](const shared_ptr<IStatsCompanionService>& sc, int64_t timeMillis) {
-                  if (sc != nullptr) {
-                      sc->setAnomalyAlarm(timeMillis);
-                      StatsdStats::getInstance().noteRegisteredAnomalyAlarmChanged();
-                  }
+              [this](const shared_ptr<IStatsCompanionService>& /*sc*/, int64_t timeMillis) {
+                  mProcessor->setAnomalyAlarm(timeMillis);
+                  StatsdStats::getInstance().noteRegisteredAnomalyAlarmChanged();
               },
-              [](const shared_ptr<IStatsCompanionService>& sc) {
-                  if (sc != nullptr) {
-                      sc->cancelAnomalyAlarm();
-                      StatsdStats::getInstance().noteRegisteredAnomalyAlarmChanged();
-                  }
+              [this](const shared_ptr<IStatsCompanionService>& /*sc*/) {
+                  mProcessor->cancelAnomalyAlarm();
+                  StatsdStats::getInstance().noteRegisteredAnomalyAlarmChanged();
               })),
       mPeriodicAlarmMonitor(new AlarmMonitor(
               MIN_DIFF_TO_UPDATE_REGISTERED_ALARM_SECS,
@@ -979,17 +975,7 @@
 
 Status StatsService::informAnomalyAlarmFired() {
     ENFORCE_UID(AID_SYSTEM);
-
-    VLOG("StatsService::informAnomalyAlarmFired was called");
-    int64_t currentTimeSec = getElapsedRealtimeSec();
-    std::unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet =
-            mAnomalyAlarmMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec));
-    if (alarmSet.size() > 0) {
-        VLOG("Found an anomaly alarm that fired.");
-        mProcessor->onAnomalyAlarmFired(currentTimeSec * NS_PER_SEC, alarmSet);
-    } else {
-        VLOG("Cannot find an anomaly alarm that fired. Perhaps it was recently cancelled.");
-    }
+    // Anomaly alarms are handled internally now. This code should be fully deleted.
     return Status::ok();
 }