blob: 686d8f95c7f634bd017efa792ce2e586bcf41d33 [file] [log] [blame]
Bookatz857aaa52017-12-19 15:29:06 -08001/*
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#pragma once
18
Yangster-mac932ecec2018-02-01 10:23:52 -080019#include "AlarmMonitor.h"
Bookatz857aaa52017-12-19 15:29:06 -080020#include "AnomalyTracker.h"
21
22namespace android {
23namespace os {
24namespace statsd {
25
26using std::unordered_map;
27
28class DurationAnomalyTracker : public virtual AnomalyTracker {
29public:
Yangster-mac932ecec2018-02-01 10:23:52 -080030 DurationAnomalyTracker(const Alert& alert, const ConfigKey& configKey,
31 const sp<AlarmMonitor>& alarmMonitor);
Bookatz857aaa52017-12-19 15:29:06 -080032
33 virtual ~DurationAnomalyTracker();
34
Bookatz6bf98252018-03-14 10:44:24 -070035 // Sets an alarm for the given timestamp.
36 // Replaces previous alarm if one already exists.
Yangster-macb142cc82018-03-30 15:22:08 -070037 void startAlarm(const MetricDimensionKey& dimensionKey, const int64_t& eventTime);
Bookatz857aaa52017-12-19 15:29:06 -080038
39 // Stops the alarm.
Bookatz3e8cd352018-03-16 10:09:59 -070040 // If it should have already fired, but hasn't yet (e.g. because the AlarmManager is delayed),
41 // declare the anomaly now.
Yangster-macb142cc82018-03-30 15:22:08 -070042 void stopAlarm(const MetricDimensionKey& dimensionKey, const int64_t& timestampNs);
Bookatz857aaa52017-12-19 15:29:06 -080043
Bookatz3e8cd352018-03-16 10:09:59 -070044 // Stop all the alarms owned by this tracker. Does not declare any anomalies.
45 void cancelAllAlarms();
Bookatz857aaa52017-12-19 15:29:06 -080046
47 // Declares an anomaly for each alarm in firedAlarms that belongs to this DurationAnomalyTracker
Bookatz6bf98252018-03-14 10:44:24 -070048 // and removes it from firedAlarms. The AlarmMonitor is not informed.
Bookatzc6977972018-01-16 16:55:05 -080049 // Note that this will generally be called from a different thread from the other functions;
50 // the caller is responsible for thread safety.
Yangster-macb142cc82018-03-30 15:22:08 -070051 void informAlarmsFired(const int64_t& timestampNs,
Yangster-mac932ecec2018-02-01 10:23:52 -080052 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>>& firedAlarms) override;
Bookatz857aaa52017-12-19 15:29:06 -080053
54protected:
Yangster-macbe10ddf2018-03-13 15:39:51 -070055 // Returns the alarm timestamp in seconds for the query dimension if it exists. Otherwise
56 // returns 0.
57 uint32_t getAlarmTimestampSec(const MetricDimensionKey& dimensionKey) const override {
58 auto it = mAlarms.find(dimensionKey);
59 return it == mAlarms.end() ? 0 : it->second->timestampSec;
60 }
61
Bookatz857aaa52017-12-19 15:29:06 -080062 // The alarms owned by this tracker. The alarm monitor also shares the alarm pointers when they
63 // are still active.
Yangster-mac932ecec2018-02-01 10:23:52 -080064 std::unordered_map<MetricDimensionKey, sp<const InternalAlarm>> mAlarms;
Bookatz857aaa52017-12-19 15:29:06 -080065
66 // Anomaly alarm monitor.
Yangster-mac932ecec2018-02-01 10:23:52 -080067 sp<AlarmMonitor> mAlarmMonitor;
Bookatz857aaa52017-12-19 15:29:06 -080068
Bookatz857aaa52017-12-19 15:29:06 -080069 FRIEND_TEST(OringDurationTrackerTest, TestPredictAnomalyTimestamp);
Bookatz1bf94382018-01-04 11:43:20 -080070 FRIEND_TEST(OringDurationTrackerTest, TestAnomalyDetectionExpiredAlarm);
71 FRIEND_TEST(OringDurationTrackerTest, TestAnomalyDetectionFiredAlarm);
Bookatz857aaa52017-12-19 15:29:06 -080072 FRIEND_TEST(MaxDurationTrackerTest, TestAnomalyDetection);
David Chen2e414b92018-02-12 17:24:40 -080073 FRIEND_TEST(MaxDurationTrackerTest, TestAnomalyPredictedTimestamp);
74 FRIEND_TEST(MaxDurationTrackerTest, TestAnomalyPredictedTimestamp_UpdatedOnStop);
Bookatz857aaa52017-12-19 15:29:06 -080075};
76
77} // namespace statsd
78} // namespace os
79} // namespace android