blob: 39405630372245864e23bb652b6e70936ce2c052 [file] [log] [blame]
Yao Chenb3561512017-11-21 18:07:17 -08001/*
2 * Copyright 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#pragma once
17
18#include "config/ConfigKey.h"
19#include "frameworks/base/cmds/statsd/src/stats_log.pb.h"
20
Yao Chen69f1baf2017-11-27 17:25:36 -080021#include <gtest/gtest_prod.h>
Yao Chenb3561512017-11-21 18:07:17 -080022#include <mutex>
23#include <string>
24#include <vector>
25
26namespace android {
27namespace os {
28namespace statsd {
29
30// Keeps track of stats of statsd.
31// Single instance shared across the process. All methods are thread safe.
32class StatsdStats {
33public:
34 static StatsdStats& getInstance();
35 ~StatsdStats(){};
36
37 // TODO: set different limit if the device is low ram.
38 const static int kDimensionKeySizeSoftLimit = 300;
39 const static int kDimensionKeySizeHardLimit = 500;
40
41 const static int kMaxConfigCount = 10;
42 const static int kMaxConditionCountPerConfig = 200;
43 const static int kMaxMetricCountPerConfig = 300;
44 const static int kMaxMatcherCountPerConfig = 500;
45
Yao Chen0fac5b12017-11-28 16:07:02 -080046 const static int kMaxTimestampCount = 20;
47
David Chenc136f452017-11-27 11:52:26 -080048 // Cap the UID map's memory usage to this. This should be fairly high since the UID information
49 // is critical for understanding the metrics.
50 const static size_t kMaxBytesUsedUidMap = 50 * 1024;
51
Yao Chenb3561512017-11-21 18:07:17 -080052 /**
53 * Report a new config has been received and report the static stats about the config.
54 *
55 * The static stats include: the count of metrics, conditions, matchers, and alerts.
56 * If the config is not valid, this config stats will be put into icebox immediately.
57 */
58 void noteConfigReceived(const ConfigKey& key, int metricsCount, int conditionsCount,
59 int matchersCount, int alertCount, bool isValid);
60 /**
61 * Report a config has been removed.
62 */
63 void noteConfigRemoved(const ConfigKey& key);
64
65 /**
66 * Report a broadcast has been sent to a config owner to collect the data.
67 */
68 void noteBroadcastSent(const ConfigKey& key);
69
70 /**
71 * Report a config's metrics data has been dropped.
72 */
Yao Chen69f1baf2017-11-27 17:25:36 -080073 void noteDataDropped(const ConfigKey& key);
74
75 /**
76 * Report metrics data report has been sent.
77 *
78 * The report may be requested via StatsManager API, or through adb cmd.
79 */
80 void noteMetricsReportSent(const ConfigKey& key);
Yao Chenb3561512017-11-21 18:07:17 -080081
82 /**
83 * Report the size of output tuple of a condition.
84 *
85 * Note: only report when the condition has an output dimension, and the tuple
86 * count > kDimensionKeySizeSoftLimit.
87 *
88 * [key]: The config key that this condition belongs to.
89 * [name]: The name of the condition.
90 * [size]: The output tuple size.
91 */
92 void noteConditionDimensionSize(const ConfigKey& key, const std::string& name, int size);
93
94 /**
95 * Report the size of output tuple of a metric.
96 *
97 * Note: only report when the metric has an output dimension, and the tuple
98 * count > kDimensionKeySizeSoftLimit.
99 *
100 * [key]: The config key that this metric belongs to.
101 * [name]: The name of the metric.
102 * [size]: The output tuple size.
103 */
104 void noteMetricDimensionSize(const ConfigKey& key, const std::string& name, int size);
105
106 /**
107 * Report a matcher has been matched.
108 *
109 * [key]: The config key that this matcher belongs to.
110 * [name]: The name of the matcher.
111 */
112 void noteMatcherMatched(const ConfigKey& key, const std::string& name);
113
114 /**
Bookatz8f2f3d82017-12-07 13:53:21 -0800115 * Report that an anomaly detection alert has been declared.
116 *
117 * [key]: The config key that this alert belongs to.
118 * [name]: The name of the alert.
119 */
120 void noteAnomalyDeclared(const ConfigKey& key, const std::string& name);
121
122 /**
Yao Chenb3561512017-11-21 18:07:17 -0800123 * Report an atom event has been logged.
124 */
125 void noteAtomLogged(int atomId, int32_t timeSec);
126
127 /**
Bookatz1d0136d2017-12-01 11:13:32 -0800128 * Report that statsd modified the anomaly alarm registered with StatsCompanionService.
129 */
130 void noteRegisteredAnomalyAlarmChanged();
131
132 /**
David Chenc136f452017-11-27 11:52:26 -0800133 * Records the number of snapshot and delta entries that are being dropped from the uid map.
134 */
135 void noteUidMapDropped(int snapshots, int deltas);
136
137 /**
138 * Updates the number of snapshots currently stored in the uid map.
139 */
140 void setUidMapSnapshots(int snapshots);
141 void setUidMapChanges(int changes);
142 void setCurrentUidMapMemory(int bytes);
143
144 /**
Yao Chenb3561512017-11-21 18:07:17 -0800145 * Reset the historical stats. Including all stats in icebox, and the tracked stats about
146 * metrics, matchers, and atoms. The active configs will be kept and StatsdStats will continue
147 * to collect stats after reset() has been called.
148 */
149 void reset();
150
151 /**
152 * Output the stats in protobuf binary format to [buffer].
153 *
154 * [reset]: whether to clear the historical stats after the call.
155 */
Yao Chen69f1baf2017-11-27 17:25:36 -0800156 void dumpStats(std::vector<uint8_t>* buffer, bool reset);
Yao Chenb3561512017-11-21 18:07:17 -0800157
158private:
159 StatsdStats();
160
161 mutable std::mutex mLock;
162
Yao Chen69f1baf2017-11-27 17:25:36 -0800163 int32_t mStartTimeSec;
Yao Chenb3561512017-11-21 18:07:17 -0800164
David Chenc136f452017-11-27 11:52:26 -0800165 // Track the number of dropped entries used by the uid map.
166 StatsdStatsReport_UidMapStats mUidMapStats;
167
Yao Chenb3561512017-11-21 18:07:17 -0800168 // The stats about the configs that are still in use.
169 std::map<const ConfigKey, StatsdStatsReport_ConfigStats> mConfigStats;
170
171 // Stores the stats for the configs that are no longer in use.
172 std::vector<const StatsdStatsReport_ConfigStats> mIceBox;
173
174 // Stores the number of output tuple of condition trackers when it's bigger than
175 // kDimensionKeySizeSoftLimit. When you see the number is kDimensionKeySizeHardLimit +1,
176 // it means some data has been dropped.
177 std::map<const ConfigKey, std::map<const std::string, int>> mConditionStats;
178
179 // Stores the number of output tuple of metric producers when it's bigger than
180 // kDimensionKeySizeSoftLimit. When you see the number is kDimensionKeySizeHardLimit +1,
181 // it means some data has been dropped.
182 std::map<const ConfigKey, std::map<const std::string, int>> mMetricsStats;
183
184 // Stores the number of times a pushed atom is logged.
185 // The size of the vector is the largest pushed atom id in atoms.proto + 1. Atoms
186 // out of that range will be dropped (it's either pulled atoms or test atoms).
187 // This is a vector, not a map because it will be accessed A LOT -- for each stats log.
188 std::vector<int> mPushedAtomStats;
189
Bookatz1d0136d2017-12-01 11:13:32 -0800190 // Stores the number of times statsd modified the anomaly alarm registered with
191 // StatsCompanionService.
192 int mAnomalyAlarmRegisteredStats = 0;
193
Bookatz8f2f3d82017-12-07 13:53:21 -0800194 // Stores the number of times an anomaly detection alert has been declared
195 // (per config, per alert name).
196 std::map<const ConfigKey, std::map<const std::string, int>> mAlertStats;
197
Yao Chenb3561512017-11-21 18:07:17 -0800198 // Stores how many times a matcher have been matched.
199 std::map<const ConfigKey, std::map<const std::string, int>> mMatcherStats;
200
201 void noteConfigRemovedInternalLocked(const ConfigKey& key);
202
203 void resetInternalLocked();
204
Bookatz8f2f3d82017-12-07 13:53:21 -0800205 void addSubStatsToConfigLocked(const ConfigKey& key,
206 StatsdStatsReport_ConfigStats& configStats);
Yao Chen69f1baf2017-11-27 17:25:36 -0800207
Yao Chen0fac5b12017-11-28 16:07:02 -0800208 void noteDataDropped(const ConfigKey& key, int32_t timeSec);
209
210 void noteMetricsReportSent(const ConfigKey& key, int32_t timeSec);
211
212 void noteBroadcastSent(const ConfigKey& key, int32_t timeSec);
213
Yao Chen69f1baf2017-11-27 17:25:36 -0800214 FRIEND_TEST(StatsdStatsTest, TestValidConfigAdd);
215 FRIEND_TEST(StatsdStatsTest, TestInvalidConfigAdd);
216 FRIEND_TEST(StatsdStatsTest, TestConfigRemove);
217 FRIEND_TEST(StatsdStatsTest, TestSubStats);
218 FRIEND_TEST(StatsdStatsTest, TestAtomLog);
Yao Chen0fac5b12017-11-28 16:07:02 -0800219 FRIEND_TEST(StatsdStatsTest, TestTimestampThreshold);
Bookatz1d0136d2017-12-01 11:13:32 -0800220 FRIEND_TEST(StatsdStatsTest, TestAnomalyMonitor);
Yao Chenb3561512017-11-21 18:07:17 -0800221};
222
223} // namespace statsd
224} // namespace os
225} // namespace android