Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d4baac77 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge changes Ife0b17c2,If37e5c6c

* changes:
  Remove dead code for AnomalyAlarm
  Remove alarm manager dependency from anomalyalarms
parents 114124c3 39074df9
Loading
Loading
Loading
Loading
+0 −11
Original line number Diff line number Diff line
@@ -26,17 +26,6 @@ interface IStatsCompanionService {
     */
    oneway void statsdReady();

    /**
    * Register an alarm for anomaly detection to fire at the given timestamp (ms since epoch).
    * If anomaly alarm had already been registered, it will be replaced with the new timestamp.
    * Uses AlarmManager.set API, so  if the timestamp is in the past, alarm fires immediately, and
    * alarm is inexact.
    */
    oneway void setAnomalyAlarm(long timestampMs);

    /** Cancel any anomaly detection alarm. */
    oneway void cancelAnomalyAlarm();

    /**
      * Register a repeating alarm for pulling to fire at the given timestamp and every
      * intervalMs thereafter (in ms since epoch).
+0 −7
Original line number Diff line number Diff line
@@ -41,13 +41,6 @@ interface IStatsd {
     */
    void statsCompanionReady();

    /**
     * Tells statsd that an anomaly may have occurred, so statsd can check whether this is so and
     * act accordingly.
     * Two-way binder call so that caller's method (and corresponding wakelocks) will linger.
     */
    void informAnomalyAlarmFired();

    /**
     * Tells statsd that it is time to poll some stats. Statsd will be responsible for determing
     * what stats to poll and initiating the polling.
+0 −66
Original line number Diff line number Diff line
@@ -100,7 +100,6 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
    private static IStatsd sStatsd;
    private static final Object sStatsdLock = new Object();

    private final OnAlarmListener mAnomalyAlarmListener;
    private final OnAlarmListener mPullingAlarmListener;
    private final OnAlarmListener mPeriodicAlarmListener;

@@ -124,7 +123,6 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
        handlerThread.start();
        mHandler = new CompanionHandler(handlerThread.getLooper());

        mAnomalyAlarmListener = new AnomalyAlarmListener(context);
        mPullingAlarmListener = new PullingAlarmListener(context);
        mPeriodicAlarmListener = new PeriodicAlarmListener(context);
    }
@@ -336,41 +334,6 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
        }
    }

    public static final class AnomalyAlarmListener implements OnAlarmListener {
        private final Context mContext;

        AnomalyAlarmListener(Context context) {
            mContext = context;
        }

        @Override
        public void onAlarm() {
            if (DEBUG) {
                Log.i(TAG, "StatsCompanionService believes an anomaly has occurred at time "
                        + System.currentTimeMillis() + "ms.");
            }
            IStatsd statsd = getStatsdNonblocking();
            if (statsd == null) {
                Log.w(TAG, "Could not access statsd to inform it of anomaly alarm firing");
                return;
            }

            // Wakelock needs to be retained while calling statsd.
            Thread thread = new WakelockThread(mContext,
                    AnomalyAlarmListener.class.getCanonicalName(), new Runnable() {
                        @Override
                        public void run() {
                            try {
                                statsd.informAnomalyAlarmFired();
                            } catch (RemoteException e) {
                                Log.w(TAG, "Failed to inform statsd of anomaly alarm firing", e);
                            }
                        }
                    });
            thread.start();
        }
    }

    public final static class PullingAlarmListener implements OnAlarmListener {
        private final Context mContext;

@@ -468,34 +431,6 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
        }
    }

    @Override // Binder call
    public void setAnomalyAlarm(long timestampMs) {
        StatsCompanion.enforceStatsdCallingUid();
        if (DEBUG) Log.d(TAG, "Setting anomaly alarm for " + timestampMs);
        final long callingToken = Binder.clearCallingIdentity();
        try {
            // using ELAPSED_REALTIME, not ELAPSED_REALTIME_WAKEUP, so if device is asleep, will
            // only fire when it awakens.
            // AlarmManager will automatically cancel any previous mAnomalyAlarmListener alarm.
            mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME, timestampMs, TAG + ".anomaly",
                    mAnomalyAlarmListener, mHandler);
        } finally {
            Binder.restoreCallingIdentity(callingToken);
        }
    }

    @Override // Binder call
    public void cancelAnomalyAlarm() {
        StatsCompanion.enforceStatsdCallingUid();
        if (DEBUG) Log.d(TAG, "Cancelling anomaly alarm");
        final long callingToken = Binder.clearCallingIdentity();
        try {
            mAlarmManager.cancel(mAnomalyAlarmListener);
        } finally {
            Binder.restoreCallingIdentity(callingToken);
        }
    }

    @Override // Binder call
    public void setAlarmForSubscriberTriggering(long timestampMs) {
        StatsCompanion.enforceStatsdCallingUid();
@@ -666,7 +601,6 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
        // instead of in binder death because statsd can come back and set different alarms, or not
        // want to set an alarm when it had been set. This guarantees that when we get a new statsd,
        // we cancel any alarms before it is able to set them.
        cancelAnomalyAlarm();
        cancelPullingAlarm();
        cancelAlarmForSubscriberTriggering();

+37 −2
Original line number Diff line number Diff line
@@ -120,10 +120,9 @@ static void flushProtoToBuffer(ProtoOutputStream& proto, vector<uint8_t>* outDat
    }
}

void StatsLogProcessor::onAnomalyAlarmFired(
void StatsLogProcessor::processFiredAnomalyAlarmsLocked(
        const int64_t& timestampNs,
        unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet) {
    std::lock_guard<std::mutex> lock(mMetricsMutex);
    for (const auto& itr : mMetricsManagers) {
        itr.second->onAnomalyAlarmFired(timestampNs, alarmSet);
    }
@@ -431,6 +430,20 @@ void StatsLogProcessor::OnLogEvent(LogEvent* event, int64_t elapsedRealtimeNs) {
        return;
    }

    bool fireAlarm = false;
    {
        std::lock_guard<std::mutex> anomalyLock(mAnomalyAlarmMutex);
        if (mNextAnomalyAlarmTime != 0 &&
            MillisToNano(mNextAnomalyAlarmTime) <= elapsedRealtimeNs) {
            mNextAnomalyAlarmTime = 0;
            VLOG("informing anomaly alarm at time %lld", (long long)elapsedRealtimeNs);
            fireAlarm = true;
        }
    }
    if (fireAlarm) {
        informAnomalyAlarmFiredLocked(NanoToMillis(elapsedRealtimeNs));
    }

    int64_t curTimeSec = getElapsedRealtimeSec();
    if (curTimeSec - mLastPullerCacheClearTimeSec > StatsdStats::kPullerCacheClearIntervalSec) {
        mPullerManager->ClearPullerCacheIfNecessary(curTimeSec * NS_PER_SEC);
@@ -1092,6 +1105,28 @@ void StatsLogProcessor::noteOnDiskData(const ConfigKey& key) {
    mOnDiskDataConfigs.insert(key);
}

void StatsLogProcessor::setAnomalyAlarm(const int64_t elapsedTimeMillis) {
    std::lock_guard<std::mutex> lock(mAnomalyAlarmMutex);
    mNextAnomalyAlarmTime = elapsedTimeMillis;
}

void StatsLogProcessor::cancelAnomalyAlarm() {
    std::lock_guard<std::mutex> lock(mAnomalyAlarmMutex);
    mNextAnomalyAlarmTime = 0;
}

void StatsLogProcessor::informAnomalyAlarmFiredLocked(const int64_t elapsedTimeMillis) {
    VLOG("StatsService::informAlarmForSubscriberTriggeringFired was called");
    std::unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet =
            mAnomalyAlarmMonitor->popSoonerThan(static_cast<uint32_t>(elapsedTimeMillis / 1000));
    if (alarmSet.size() > 0) {
        VLOG("Found periodic alarm fired.");
        processFiredAnomalyAlarmsLocked(MillisToNano(elapsedTimeMillis), alarmSet);
    } else {
        ALOGW("Cannot find an periodic alarm that fired. Perhaps it was recently cancelled.");
    }
}

}  // namespace statsd
}  // namespace os
}  // namespace android
+21 −5
Original line number Diff line number Diff line
@@ -66,11 +66,6 @@ public:
                      const DumpLatency dumpLatency,
                      ProtoOutputStream* proto);

    /* Tells MetricsManager that the alarms in alarmSet have fired. Modifies anomaly alarmSet. */
    void onAnomalyAlarmFired(
            const int64_t& timestampNs,
            unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet);

    /* Tells MetricsManager that the alarms in alarmSet have fired. Modifies periodic alarmSet. */
    void onPeriodicAlarmFired(
            const int64_t& timestampNs,
@@ -148,6 +143,10 @@ public:
    // Add a specific config key to the possible configs to dump ASAP.
    void noteOnDiskData(const ConfigKey& key);

    void setAnomalyAlarm(const int64_t timeMillis);

    void cancelAnomalyAlarm();

private:
    // For testing only.
    inline sp<AlarmMonitor> getAnomalyAlarmMonitor() const {
@@ -160,6 +159,11 @@ private:

    mutable mutex mMetricsMutex;

    // Guards mNextAnomalyAlarmTime. A separate mutex is needed because alarms are set/cancelled
    // in the onLogEvent code path, which is locked by mMetricsMutex.
    // DO NOT acquire mMetricsMutex while holding mAnomalyAlarmMutex. This can lead to a deadlock.
    mutable mutex mAnomalyAlarmMutex;

    std::unordered_map<ConfigKey, sp<MetricsManager>> mMetricsManagers;

    std::unordered_map<ConfigKey, int64_t> mLastBroadcastTimes;
@@ -250,6 +254,15 @@ private:
    // Reset the specified configs.
    void resetConfigsLocked(const int64_t timestampNs, const std::vector<ConfigKey>& configs);

    // An anomaly alarm should have fired.
    // Check with anomaly alarm manager to find the alarms and process the result.
    void informAnomalyAlarmFiredLocked(const int64_t elapsedTimeMillis);

    /* Tells MetricsManager that the alarms in alarmSet have fired. Modifies anomaly alarmSet. */
    void processFiredAnomalyAlarmsLocked(
            const int64_t& timestampNs,
            unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet);

    // Function used to send a broadcast so that receiver for the config key can call getData
    // to retrieve the stored data.
    std::function<bool(const ConfigKey& key)> mSendBroadcast;
@@ -276,6 +289,9 @@ private:
    //Last time we wrote metadata to disk.
    int64_t mLastMetadataWriteNs = 0;

    // The time for the next anomaly alarm for alerts.
    int64_t mNextAnomalyAlarmTime = 0;

#ifdef VERY_VERBOSE_PRINTING
    bool mPrintAllLogs = false;
#endif
Loading