blob: 1708cc31bf14f8ce7447787ee2eeaf31897895c7 [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-mac93694462018-01-22 20:49:31 -080031// Create AtomMatcher proto for screen brightness state changed.
32AtomMatcher CreateScreenBrightnessChangedAtomMatcher();
33
34// Create AtomMatcher proto for starting battery save mode.
35AtomMatcher CreateBatterySaverModeStartAtomMatcher();
36
37// Create AtomMatcher proto for stopping battery save mode.
38AtomMatcher CreateBatterySaverModeStopAtomMatcher();
39
40// Create AtomMatcher proto for process state changed.
41AtomMatcher CreateUidProcessStateChangedAtomMatcher();
42
Yangster-mac20877162017-12-22 17:19:39 -080043// Create AtomMatcher proto for acquiring wakelock.
44AtomMatcher CreateAcquireWakelockAtomMatcher();
45
46// Create AtomMatcher proto for releasing wakelock.
47AtomMatcher CreateReleaseWakelockAtomMatcher() ;
48
49// Create AtomMatcher proto for screen turned on.
50AtomMatcher CreateScreenTurnedOnAtomMatcher();
51
52// Create AtomMatcher proto for screen turned off.
53AtomMatcher CreateScreenTurnedOffAtomMatcher();
54
55// Create AtomMatcher proto for app sync turned on.
56AtomMatcher CreateSyncStartAtomMatcher();
57
58// Create AtomMatcher proto for app sync turned off.
59AtomMatcher CreateSyncEndAtomMatcher();
60
61// Create AtomMatcher proto for app sync moves to background.
62AtomMatcher CreateMoveToBackgroundAtomMatcher();
63
64// Create AtomMatcher proto for app sync moves to foreground.
65AtomMatcher CreateMoveToForegroundAtomMatcher();
66
67// Create AtomMatcher proto for process crashes
68AtomMatcher CreateProcessCrashAtomMatcher() ;
69
70// Create Predicate proto for screen is on.
71Predicate CreateScreenIsOnPredicate();
72
73// Create Predicate proto for screen is off.
74Predicate CreateScreenIsOffPredicate();
75
Yangster-mac93694462018-01-22 20:49:31 -080076// Create Predicate proto for battery saver mode.
77Predicate CreateBatterySaverModePredicate();
78
Yangster-mac20877162017-12-22 17:19:39 -080079// Create Predicate proto for holding wakelock.
80Predicate CreateHoldingWakelockPredicate();
81
82// Create a Predicate proto for app syncing.
83Predicate CreateIsSyncingPredicate();
84
85// Create a Predicate proto for app is in background.
86Predicate CreateIsInBackgroundPredicate();
87
88// Add a predicate to the predicate combination.
89void addPredicateToPredicateCombination(const Predicate& predicate, Predicate* combination);
90
91// Create dimensions from primitive fields.
92FieldMatcher CreateDimensions(const int atomId, const std::vector<int>& fields);
93
94// Create dimensions by attribution uid and tag.
95FieldMatcher CreateAttributionUidAndTagDimensions(const int atomId,
96 const std::vector<Position>& positions);
97
98// Create dimensions by attribution uid only.
99FieldMatcher CreateAttributionUidDimensions(const int atomId,
100 const std::vector<Position>& positions);
101
102// Create log event for screen state changed.
103std::unique_ptr<LogEvent> CreateScreenStateChangedEvent(
Bookatz1a1b0462018-01-12 11:47:03 -0800104 const android::view::DisplayStateEnum state, uint64_t timestampNs);
Yangster-mac20877162017-12-22 17:19:39 -0800105
Yangster-mac93694462018-01-22 20:49:31 -0800106// Create log event for screen brightness state changed.
107std::unique_ptr<LogEvent> CreateScreenBrightnessChangedEvent(
108 int level, uint64_t timestampNs);
109
110// Create log event when battery saver starts.
111std::unique_ptr<LogEvent> CreateBatterySaverOnEvent(uint64_t timestampNs);
112// Create log event when battery saver stops.
113std::unique_ptr<LogEvent> CreateBatterySaverOffEvent(uint64_t timestampNs);
114
Yangster-mac20877162017-12-22 17:19:39 -0800115// Create log event for app moving to background.
116std::unique_ptr<LogEvent> CreateMoveToBackgroundEvent(const int uid, uint64_t timestampNs);
117
118// Create log event for app moving to foreground.
119std::unique_ptr<LogEvent> CreateMoveToForegroundEvent(const int uid, uint64_t timestampNs);
120
121// Create log event when the app sync starts.
122std::unique_ptr<LogEvent> CreateSyncStartEvent(
Yao Chen9c1debe2018-02-19 14:39:19 -0800123 const std::vector<AttributionNodeInternal>& attributions, const string& name,
124 uint64_t timestampNs);
Yangster-mac20877162017-12-22 17:19:39 -0800125
126// Create log event when the app sync ends.
127std::unique_ptr<LogEvent> CreateSyncEndEvent(
Yao Chen9c1debe2018-02-19 14:39:19 -0800128 const std::vector<AttributionNodeInternal>& attributions, const string& name,
129 uint64_t timestampNs);
Yangster-mac20877162017-12-22 17:19:39 -0800130
131// Create log event when the app sync ends.
132std::unique_ptr<LogEvent> CreateAppCrashEvent(
133 const int uid, uint64_t timestampNs);
134
135// Create log event for acquiring wakelock.
136std::unique_ptr<LogEvent> CreateAcquireWakelockEvent(
Yao Chen9c1debe2018-02-19 14:39:19 -0800137 const std::vector<AttributionNodeInternal>& attributions, const string& wakelockName,
138 uint64_t timestampNs);
Yangster-mac20877162017-12-22 17:19:39 -0800139
140// Create log event for releasing wakelock.
141std::unique_ptr<LogEvent> CreateReleaseWakelockEvent(
Yao Chen9c1debe2018-02-19 14:39:19 -0800142 const std::vector<AttributionNodeInternal>& attributions, const string& wakelockName,
143 uint64_t timestampNs);
Yangster-mac20877162017-12-22 17:19:39 -0800144
Yangster-macd40053e2018-01-09 16:29:22 -0800145// Create log event for releasing wakelock.
146std::unique_ptr<LogEvent> CreateIsolatedUidChangedEvent(
147 int isolatedUid, int hostUid, bool is_create, uint64_t timestampNs);
148
Yao Chen9c1debe2018-02-19 14:39:19 -0800149// Helper function to create an AttributionNodeInternal proto.
150AttributionNodeInternal CreateAttribution(const int& uid, const string& tag);
Yangster-mac20877162017-12-22 17:19:39 -0800151
152// Create a statsd log event processor upon the start time in seconds, config and key.
153sp<StatsLogProcessor> CreateStatsLogProcessor(const long timeBaseSec, const StatsdConfig& config,
154 const ConfigKey& key);
155
156// Util function to sort the log events by timestamp.
157void sortLogEventsByTimestamp(std::vector<std::unique_ptr<LogEvent>> *events);
158
Yangster-mac94e197c2018-01-02 16:03:03 -0800159int64_t StringToId(const string& str);
160
Yangster-macb5bc7412018-01-06 23:17:45 -0800161void ValidateAttributionUidDimension(const DimensionsValue& value, int atomId, int uid);
162void ValidateAttributionUidAndTagDimension(
163 const DimensionsValue& value, int atomId, int uid, const std::string& tag);
164
Yao Chen8a8d16c2018-02-08 14:50:40 -0800165struct DimensionsPair {
166 DimensionsPair(DimensionsValue m1, DimensionsValue m2) : dimInWhat(m1), dimInCondition(m2){};
167
168 DimensionsValue dimInWhat;
169 DimensionsValue dimInCondition;
170};
171
172bool LessThan(const DimensionsValue& s1, const DimensionsValue& s2);
173bool LessThan(const DimensionsPair& s1, const DimensionsPair& s2);
174
175struct DimensionCompare {
176 bool operator()(const DimensionsPair& s1, const DimensionsPair& s2) const {
177 return LessThan(s1, s2);
178 }
179};
180
Yangster-macb5bc7412018-01-06 23:17:45 -0800181template <typename T>
182void sortMetricDataByDimensionsValue(const T& metricData, T* sortedMetricData) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800183 std::map<DimensionsPair, int, DimensionCompare> dimensionIndexMap;
Yangster-macb5bc7412018-01-06 23:17:45 -0800184 for (int i = 0; i < metricData.data_size(); ++i) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800185 dimensionIndexMap.insert(
186 std::make_pair(DimensionsPair(metricData.data(i).dimensions_in_what(),
187 metricData.data(i).dimensions_in_condition()),
188 i));
Yangster-macb5bc7412018-01-06 23:17:45 -0800189 }
190 for (const auto& itr : dimensionIndexMap) {
191 *sortedMetricData->add_data() = metricData.data(itr.second);
192 }
193}
194
Yangster-mac20877162017-12-22 17:19:39 -0800195} // namespace statsd
196} // namespace os
197} // namespace android