blob: 08a1a6623e83b6b41ce1f871e41c39c9ced194dd [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>
David Chend9269e22017-12-05 13:43:51 -080022#include <log/log_time.h>
Yao Chenb3561512017-11-21 18:07:17 -080023#include <mutex>
24#include <string>
25#include <vector>
26
27namespace android {
28namespace os {
29namespace statsd {
30
31// Keeps track of stats of statsd.
32// Single instance shared across the process. All methods are thread safe.
33class StatsdStats {
34public:
35 static StatsdStats& getInstance();
36 ~StatsdStats(){};
37
38 // TODO: set different limit if the device is low ram.
39 const static int kDimensionKeySizeSoftLimit = 300;
40 const static int kDimensionKeySizeHardLimit = 500;
41
42 const static int kMaxConfigCount = 10;
43 const static int kMaxConditionCountPerConfig = 200;
44 const static int kMaxMetricCountPerConfig = 300;
45 const static int kMaxMatcherCountPerConfig = 500;
46
Yao Chen0fac5b12017-11-28 16:07:02 -080047 const static int kMaxTimestampCount = 20;
48
David Chend9269e22017-12-05 13:43:51 -080049 // Max memory allowed for storing metrics per configuration. When this limit is approached,
50 // statsd will send a broadcast so that the client can fetch the data and clear this memory.
51 static const size_t kMaxMetricsBytesPerConfig = 128 * 1024;
52
David Chenc136f452017-11-27 11:52:26 -080053 // Cap the UID map's memory usage to this. This should be fairly high since the UID information
54 // is critical for understanding the metrics.
55 const static size_t kMaxBytesUsedUidMap = 50 * 1024;
56
David Chend9269e22017-12-05 13:43:51 -080057 /* Minimum period between two broadcasts in nanoseconds. */
58 static const unsigned long long kMinBroadcastPeriodNs = 60 * NS_PER_SEC;
59
60 /* Min period between two checks of byte size per config key in nanoseconds. */
61 static const unsigned long long kMinByteSizeCheckPeriodNs = 10 * NS_PER_SEC;
62
Yao Chenb3561512017-11-21 18:07:17 -080063 /**
64 * Report a new config has been received and report the static stats about the config.
65 *
66 * The static stats include: the count of metrics, conditions, matchers, and alerts.
67 * If the config is not valid, this config stats will be put into icebox immediately.
68 */
69 void noteConfigReceived(const ConfigKey& key, int metricsCount, int conditionsCount,
70 int matchersCount, int alertCount, bool isValid);
71 /**
72 * Report a config has been removed.
73 */
74 void noteConfigRemoved(const ConfigKey& key);
75
76 /**
77 * Report a broadcast has been sent to a config owner to collect the data.
78 */
79 void noteBroadcastSent(const ConfigKey& key);
80
81 /**
82 * Report a config's metrics data has been dropped.
83 */
Yao Chen69f1baf2017-11-27 17:25:36 -080084 void noteDataDropped(const ConfigKey& key);
85
86 /**
87 * Report metrics data report has been sent.
88 *
89 * The report may be requested via StatsManager API, or through adb cmd.
90 */
91 void noteMetricsReportSent(const ConfigKey& key);
Yao Chenb3561512017-11-21 18:07:17 -080092
93 /**
94 * Report the size of output tuple of a condition.
95 *
96 * Note: only report when the condition has an output dimension, and the tuple
97 * count > kDimensionKeySizeSoftLimit.
98 *
99 * [key]: The config key that this condition belongs to.
100 * [name]: The name of the condition.
101 * [size]: The output tuple size.
102 */
103 void noteConditionDimensionSize(const ConfigKey& key, const std::string& name, int size);
104
105 /**
106 * Report the size of output tuple of a metric.
107 *
108 * Note: only report when the metric has an output dimension, and the tuple
109 * count > kDimensionKeySizeSoftLimit.
110 *
111 * [key]: The config key that this metric belongs to.
112 * [name]: The name of the metric.
113 * [size]: The output tuple size.
114 */
115 void noteMetricDimensionSize(const ConfigKey& key, const std::string& name, int size);
116
117 /**
118 * Report a matcher has been matched.
119 *
120 * [key]: The config key that this matcher belongs to.
121 * [name]: The name of the matcher.
122 */
123 void noteMatcherMatched(const ConfigKey& key, const std::string& name);
124
125 /**
126 * Report an atom event has been logged.
127 */
128 void noteAtomLogged(int atomId, int32_t timeSec);
129
130 /**
David Chenc136f452017-11-27 11:52:26 -0800131 * Records the number of snapshot and delta entries that are being dropped from the uid map.
132 */
133 void noteUidMapDropped(int snapshots, int deltas);
134
135 /**
136 * Updates the number of snapshots currently stored in the uid map.
137 */
138 void setUidMapSnapshots(int snapshots);
139 void setUidMapChanges(int changes);
140 void setCurrentUidMapMemory(int bytes);
141
142 /**
Yao Chenb3561512017-11-21 18:07:17 -0800143 * Reset the historical stats. Including all stats in icebox, and the tracked stats about
144 * metrics, matchers, and atoms. The active configs will be kept and StatsdStats will continue
145 * to collect stats after reset() has been called.
146 */
147 void reset();
148
149 /**
150 * Output the stats in protobuf binary format to [buffer].
151 *
152 * [reset]: whether to clear the historical stats after the call.
153 */
Yao Chen69f1baf2017-11-27 17:25:36 -0800154 void dumpStats(std::vector<uint8_t>* buffer, bool reset);
Yao Chenb3561512017-11-21 18:07:17 -0800155
156private:
157 StatsdStats();
158
159 mutable std::mutex mLock;
160
Yao Chen69f1baf2017-11-27 17:25:36 -0800161 int32_t mStartTimeSec;
Yao Chenb3561512017-11-21 18:07:17 -0800162
David Chenc136f452017-11-27 11:52:26 -0800163 // Track the number of dropped entries used by the uid map.
164 StatsdStatsReport_UidMapStats mUidMapStats;
165
Yao Chenb3561512017-11-21 18:07:17 -0800166 // The stats about the configs that are still in use.
167 std::map<const ConfigKey, StatsdStatsReport_ConfigStats> mConfigStats;
168
169 // Stores the stats for the configs that are no longer in use.
170 std::vector<const StatsdStatsReport_ConfigStats> mIceBox;
171
172 // Stores the number of output tuple of condition trackers when it's bigger than
173 // kDimensionKeySizeSoftLimit. When you see the number is kDimensionKeySizeHardLimit +1,
174 // it means some data has been dropped.
175 std::map<const ConfigKey, std::map<const std::string, int>> mConditionStats;
176
177 // Stores the number of output tuple of metric producers when it's bigger than
178 // kDimensionKeySizeSoftLimit. When you see the number is kDimensionKeySizeHardLimit +1,
179 // it means some data has been dropped.
180 std::map<const ConfigKey, std::map<const std::string, int>> mMetricsStats;
181
182 // Stores the number of times a pushed atom is logged.
183 // The size of the vector is the largest pushed atom id in atoms.proto + 1. Atoms
184 // out of that range will be dropped (it's either pulled atoms or test atoms).
185 // This is a vector, not a map because it will be accessed A LOT -- for each stats log.
186 std::vector<int> mPushedAtomStats;
187
188 // Stores how many times a matcher have been matched.
189 std::map<const ConfigKey, std::map<const std::string, int>> mMatcherStats;
190
191 void noteConfigRemovedInternalLocked(const ConfigKey& key);
192
193 void resetInternalLocked();
194
195 void addSubStatsToConfig(const ConfigKey& key, StatsdStatsReport_ConfigStats& configStats);
Yao Chen69f1baf2017-11-27 17:25:36 -0800196
Yao Chen0fac5b12017-11-28 16:07:02 -0800197 void noteDataDropped(const ConfigKey& key, int32_t timeSec);
198
199 void noteMetricsReportSent(const ConfigKey& key, int32_t timeSec);
200
201 void noteBroadcastSent(const ConfigKey& key, int32_t timeSec);
202
Yao Chen69f1baf2017-11-27 17:25:36 -0800203 FRIEND_TEST(StatsdStatsTest, TestValidConfigAdd);
204 FRIEND_TEST(StatsdStatsTest, TestInvalidConfigAdd);
205 FRIEND_TEST(StatsdStatsTest, TestConfigRemove);
206 FRIEND_TEST(StatsdStatsTest, TestSubStats);
207 FRIEND_TEST(StatsdStatsTest, TestAtomLog);
Yao Chen0fac5b12017-11-28 16:07:02 -0800208 FRIEND_TEST(StatsdStatsTest, TestTimestampThreshold);
Yao Chenb3561512017-11-21 18:07:17 -0800209};
210
211} // namespace statsd
212} // namespace os
213} // namespace android