blob: 6ecca46862da484f7db3c1efb30124f2f7e1f48d [file] [log] [blame]
Yangster-mac20877162017-12-22 17:19:39 -08001// Copyright (C) 2017 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
Yangster-mac93694462018-01-22 20:49:31 -080017#include <gtest/gtest.h>
Yao Chen9c1debe2018-02-19 14:39:19 -080018#include "frameworks/base/cmds/statsd/src/stats_log.pb.h"
Yangster-mac20877162017-12-22 17:19:39 -080019#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
Yangster-mac20877162017-12-22 17:19:39 -080020#include "src/StatsLogProcessor.h"
Yao Chen9c1debe2018-02-19 14:39:19 -080021#include "src/logd/LogEvent.h"
22#include "statslog.h"
Yangster-mac20877162017-12-22 17:19:39 -080023
24namespace android {
25namespace os {
26namespace statsd {
27
Yangster-mac87718e22018-01-11 16:16:26 -080028// Create AtomMatcher proto to simply match a specific atom type.
29AtomMatcher CreateSimpleAtomMatcher(const string& name, int atomId);
30
Yangster-mac15f6bbc2018-04-08 11:52:26 -070031// Create AtomMatcher proto for temperature atom.
32AtomMatcher CreateTemperatureAtomMatcher();
33
Yangster13fb7e42018-03-07 17:30:49 -080034// Create AtomMatcher proto for scheduled job state changed.
35AtomMatcher CreateScheduledJobStateChangedAtomMatcher();
36
37// Create AtomMatcher proto for starting a scheduled job.
38AtomMatcher CreateStartScheduledJobAtomMatcher();
39
40// Create AtomMatcher proto for a scheduled job is done.
41AtomMatcher CreateFinishScheduledJobAtomMatcher();
42
Yangster-mac93694462018-01-22 20:49:31 -080043// Create AtomMatcher proto for screen brightness state changed.
44AtomMatcher CreateScreenBrightnessChangedAtomMatcher();
45
46// Create AtomMatcher proto for starting battery save mode.
47AtomMatcher CreateBatterySaverModeStartAtomMatcher();
48
49// Create AtomMatcher proto for stopping battery save mode.
50AtomMatcher CreateBatterySaverModeStopAtomMatcher();
51
52// Create AtomMatcher proto for process state changed.
53AtomMatcher CreateUidProcessStateChangedAtomMatcher();
54
Yangster-mac20877162017-12-22 17:19:39 -080055// Create AtomMatcher proto for acquiring wakelock.
56AtomMatcher CreateAcquireWakelockAtomMatcher();
57
58// Create AtomMatcher proto for releasing wakelock.
59AtomMatcher CreateReleaseWakelockAtomMatcher() ;
60
61// Create AtomMatcher proto for screen turned on.
62AtomMatcher CreateScreenTurnedOnAtomMatcher();
63
64// Create AtomMatcher proto for screen turned off.
65AtomMatcher CreateScreenTurnedOffAtomMatcher();
66
67// Create AtomMatcher proto for app sync turned on.
68AtomMatcher CreateSyncStartAtomMatcher();
69
70// Create AtomMatcher proto for app sync turned off.
71AtomMatcher CreateSyncEndAtomMatcher();
72
73// Create AtomMatcher proto for app sync moves to background.
74AtomMatcher CreateMoveToBackgroundAtomMatcher();
75
76// Create AtomMatcher proto for app sync moves to foreground.
77AtomMatcher CreateMoveToForegroundAtomMatcher();
78
79// Create AtomMatcher proto for process crashes
80AtomMatcher CreateProcessCrashAtomMatcher() ;
81
82// Create Predicate proto for screen is on.
83Predicate CreateScreenIsOnPredicate();
84
85// Create Predicate proto for screen is off.
86Predicate CreateScreenIsOffPredicate();
87
Yangster13fb7e42018-03-07 17:30:49 -080088// Create Predicate proto for a running scheduled job.
89Predicate CreateScheduledJobPredicate();
90
Yangster-mac93694462018-01-22 20:49:31 -080091// Create Predicate proto for battery saver mode.
92Predicate CreateBatterySaverModePredicate();
93
Yangster-mac20877162017-12-22 17:19:39 -080094// Create Predicate proto for holding wakelock.
95Predicate CreateHoldingWakelockPredicate();
96
97// Create a Predicate proto for app syncing.
98Predicate CreateIsSyncingPredicate();
99
100// Create a Predicate proto for app is in background.
101Predicate CreateIsInBackgroundPredicate();
102
103// Add a predicate to the predicate combination.
104void addPredicateToPredicateCombination(const Predicate& predicate, Predicate* combination);
105
106// Create dimensions from primitive fields.
107FieldMatcher CreateDimensions(const int atomId, const std::vector<int>& fields);
108
109// Create dimensions by attribution uid and tag.
110FieldMatcher CreateAttributionUidAndTagDimensions(const int atomId,
111 const std::vector<Position>& positions);
112
113// Create dimensions by attribution uid only.
114FieldMatcher CreateAttributionUidDimensions(const int atomId,
115 const std::vector<Position>& positions);
116
117// Create log event for screen state changed.
118std::unique_ptr<LogEvent> CreateScreenStateChangedEvent(
Bookatz1a1b0462018-01-12 11:47:03 -0800119 const android::view::DisplayStateEnum state, uint64_t timestampNs);
Yangster-mac20877162017-12-22 17:19:39 -0800120
Yangster-mac93694462018-01-22 20:49:31 -0800121// Create log event for screen brightness state changed.
122std::unique_ptr<LogEvent> CreateScreenBrightnessChangedEvent(
123 int level, uint64_t timestampNs);
124
Yangster13fb7e42018-03-07 17:30:49 -0800125// Create log event when scheduled job starts.
126std::unique_ptr<LogEvent> CreateStartScheduledJobEvent(
127 const std::vector<AttributionNodeInternal>& attributions,
128 const string& name, uint64_t timestampNs);
129
130// Create log event when scheduled job finishes.
131std::unique_ptr<LogEvent> CreateFinishScheduledJobEvent(
132 const std::vector<AttributionNodeInternal>& attributions,
133 const string& name, uint64_t timestampNs);
134
Yangster-mac93694462018-01-22 20:49:31 -0800135// Create log event when battery saver starts.
136std::unique_ptr<LogEvent> CreateBatterySaverOnEvent(uint64_t timestampNs);
137// Create log event when battery saver stops.
138std::unique_ptr<LogEvent> CreateBatterySaverOffEvent(uint64_t timestampNs);
139
Yangster-mac20877162017-12-22 17:19:39 -0800140// Create log event for app moving to background.
141std::unique_ptr<LogEvent> CreateMoveToBackgroundEvent(const int uid, uint64_t timestampNs);
142
143// Create log event for app moving to foreground.
144std::unique_ptr<LogEvent> CreateMoveToForegroundEvent(const int uid, uint64_t timestampNs);
145
146// Create log event when the app sync starts.
147std::unique_ptr<LogEvent> CreateSyncStartEvent(
Yao Chen9c1debe2018-02-19 14:39:19 -0800148 const std::vector<AttributionNodeInternal>& attributions, const string& name,
149 uint64_t timestampNs);
Yangster-mac20877162017-12-22 17:19:39 -0800150
151// Create log event when the app sync ends.
152std::unique_ptr<LogEvent> CreateSyncEndEvent(
Yao Chen9c1debe2018-02-19 14:39:19 -0800153 const std::vector<AttributionNodeInternal>& attributions, const string& name,
154 uint64_t timestampNs);
Yangster-mac20877162017-12-22 17:19:39 -0800155
156// Create log event when the app sync ends.
157std::unique_ptr<LogEvent> CreateAppCrashEvent(
158 const int uid, uint64_t timestampNs);
159
160// Create log event for acquiring wakelock.
161std::unique_ptr<LogEvent> CreateAcquireWakelockEvent(
Yao Chen9c1debe2018-02-19 14:39:19 -0800162 const std::vector<AttributionNodeInternal>& attributions, const string& wakelockName,
163 uint64_t timestampNs);
Yangster-mac20877162017-12-22 17:19:39 -0800164
165// Create log event for releasing wakelock.
166std::unique_ptr<LogEvent> CreateReleaseWakelockEvent(
Yao Chen9c1debe2018-02-19 14:39:19 -0800167 const std::vector<AttributionNodeInternal>& attributions, const string& wakelockName,
168 uint64_t timestampNs);
Yangster-mac20877162017-12-22 17:19:39 -0800169
Yangster-macd40053e2018-01-09 16:29:22 -0800170// Create log event for releasing wakelock.
171std::unique_ptr<LogEvent> CreateIsolatedUidChangedEvent(
172 int isolatedUid, int hostUid, bool is_create, uint64_t timestampNs);
173
Yao Chen9c1debe2018-02-19 14:39:19 -0800174// Helper function to create an AttributionNodeInternal proto.
175AttributionNodeInternal CreateAttribution(const int& uid, const string& tag);
Yangster-mac20877162017-12-22 17:19:39 -0800176
177// Create a statsd log event processor upon the start time in seconds, config and key.
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700178sp<StatsLogProcessor> CreateStatsLogProcessor(const int64_t timeBaseNs,
179 const int64_t currentTimeNs,
180 const StatsdConfig& config, const ConfigKey& key);
Yangster-mac20877162017-12-22 17:19:39 -0800181
182// Util function to sort the log events by timestamp.
183void sortLogEventsByTimestamp(std::vector<std::unique_ptr<LogEvent>> *events);
184
Yangster-mac94e197c2018-01-02 16:03:03 -0800185int64_t StringToId(const string& str);
186
Yangster-mace06cfd72018-03-10 23:22:59 -0800187void ValidateUidDimension(const DimensionsValue& value, int node_idx, int atomId, int uid);
Yangster-macb5bc7412018-01-06 23:17:45 -0800188void ValidateAttributionUidDimension(const DimensionsValue& value, int atomId, int uid);
189void ValidateAttributionUidAndTagDimension(
190 const DimensionsValue& value, int atomId, int uid, const std::string& tag);
Yangster-mace06cfd72018-03-10 23:22:59 -0800191void ValidateAttributionUidAndTagDimension(
192 const DimensionsValue& value, int node_idx, int atomId, int uid, const std::string& tag);
Yangster-macb5bc7412018-01-06 23:17:45 -0800193
Yao Chen8a8d16c2018-02-08 14:50:40 -0800194struct DimensionsPair {
195 DimensionsPair(DimensionsValue m1, DimensionsValue m2) : dimInWhat(m1), dimInCondition(m2){};
196
197 DimensionsValue dimInWhat;
198 DimensionsValue dimInCondition;
199};
200
201bool LessThan(const DimensionsValue& s1, const DimensionsValue& s2);
202bool LessThan(const DimensionsPair& s1, const DimensionsPair& s2);
203
204struct DimensionCompare {
205 bool operator()(const DimensionsPair& s1, const DimensionsPair& s2) const {
206 return LessThan(s1, s2);
207 }
208};
209
Yangster-macb5bc7412018-01-06 23:17:45 -0800210template <typename T>
211void sortMetricDataByDimensionsValue(const T& metricData, T* sortedMetricData) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800212 std::map<DimensionsPair, int, DimensionCompare> dimensionIndexMap;
Yangster-macb5bc7412018-01-06 23:17:45 -0800213 for (int i = 0; i < metricData.data_size(); ++i) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800214 dimensionIndexMap.insert(
215 std::make_pair(DimensionsPair(metricData.data(i).dimensions_in_what(),
216 metricData.data(i).dimensions_in_condition()),
217 i));
Yangster-macb5bc7412018-01-06 23:17:45 -0800218 }
219 for (const auto& itr : dimensionIndexMap) {
220 *sortedMetricData->add_data() = metricData.data(itr.second);
221 }
222}
223
Yangster-mac20877162017-12-22 17:19:39 -0800224} // namespace statsd
225} // namespace os
226} // namespace android