1 // 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 
17 #include "frameworks/base/cmds/statsd/src/stats_log.pb.h"
18 #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
19 #include "src/StatsLogProcessor.h"
20 #include "src/logd/LogEvent.h"
21 #include "statslog.h"
22 
23 namespace android {
24 namespace os {
25 namespace statsd {
26 
27 // Create AtomMatcher proto to simply match a specific atom type.
28 AtomMatcher CreateSimpleAtomMatcher(const string& name, int atomId);
29 
30 // Create AtomMatcher proto for scheduled job state changed.
31 AtomMatcher CreateScheduledJobStateChangedAtomMatcher();
32 
33 // Create AtomMatcher proto for starting a scheduled job.
34 AtomMatcher CreateStartScheduledJobAtomMatcher();
35 
36 // Create AtomMatcher proto for a scheduled job is done.
37 AtomMatcher CreateFinishScheduledJobAtomMatcher();
38 
39 // Create AtomMatcher proto for screen brightness state changed.
40 AtomMatcher CreateScreenBrightnessChangedAtomMatcher();
41 
42 // Create AtomMatcher proto for acquiring wakelock.
43 AtomMatcher CreateAcquireWakelockAtomMatcher();
44 
45 // Create AtomMatcher proto for releasing wakelock.
46 AtomMatcher CreateReleaseWakelockAtomMatcher() ;
47 
48 // Create AtomMatcher proto for screen turned on.
49 AtomMatcher CreateScreenTurnedOnAtomMatcher();
50 
51 // Create AtomMatcher proto for screen turned off.
52 AtomMatcher CreateScreenTurnedOffAtomMatcher();
53 
54 // Create AtomMatcher proto for app sync turned on.
55 AtomMatcher CreateSyncStartAtomMatcher();
56 
57 // Create AtomMatcher proto for app sync turned off.
58 AtomMatcher CreateSyncEndAtomMatcher();
59 
60 // Create AtomMatcher proto for app sync moves to background.
61 AtomMatcher CreateMoveToBackgroundAtomMatcher();
62 
63 // Create AtomMatcher proto for app sync moves to foreground.
64 AtomMatcher CreateMoveToForegroundAtomMatcher();
65 
66 // Create Predicate proto for screen is off.
67 Predicate CreateScreenIsOffPredicate();
68 
69 // Create Predicate proto for a running scheduled job.
70 Predicate CreateScheduledJobPredicate();
71 
72 // Create Predicate proto for holding wakelock.
73 Predicate CreateHoldingWakelockPredicate();
74 
75 // Create a Predicate proto for app syncing.
76 Predicate CreateIsSyncingPredicate();
77 
78 // Create a Predicate proto for app is in background.
79 Predicate CreateIsInBackgroundPredicate();
80 
81 // Add a predicate to the predicate combination.
82 void addPredicateToPredicateCombination(const Predicate& predicate, Predicate* combination);
83 
84 // Create dimensions from primitive fields.
85 FieldMatcher CreateDimensions(const int atomId, const std::vector<int>& fields);
86 
87 // Create dimensions by attribution uid and tag.
88 FieldMatcher CreateAttributionUidAndTagDimensions(const int atomId,
89                                                   const std::vector<Position>& positions);
90 
91 // Create dimensions by attribution uid only.
92 FieldMatcher CreateAttributionUidDimensions(const int atomId,
93                                             const std::vector<Position>& positions);
94 
95 // Create log event for screen state changed.
96 std::unique_ptr<LogEvent> CreateScreenStateChangedEvent(
97     const android::view::DisplayStateEnum state, uint64_t timestampNs);
98 
99 // Create log event for screen brightness state changed.
100 std::unique_ptr<LogEvent> CreateScreenBrightnessChangedEvent(
101    int level, uint64_t timestampNs);
102 
103 // Create log event when scheduled job starts.
104 std::unique_ptr<LogEvent> CreateStartScheduledJobEvent(
105     const std::vector<AttributionNodeInternal>& attributions,
106     const string& name, uint64_t timestampNs);
107 
108 // Create log event when scheduled job finishes.
109 std::unique_ptr<LogEvent> CreateFinishScheduledJobEvent(
110     const std::vector<AttributionNodeInternal>& attributions,
111     const string& name, uint64_t timestampNs);
112 
113 // Create log event for app moving to background.
114 std::unique_ptr<LogEvent> CreateMoveToBackgroundEvent(const int uid, uint64_t timestampNs);
115 
116 // Create log event for app moving to foreground.
117 std::unique_ptr<LogEvent> CreateMoveToForegroundEvent(const int uid, uint64_t timestampNs);
118 
119 // Create log event when the app sync starts.
120 std::unique_ptr<LogEvent> CreateSyncStartEvent(
121         const std::vector<AttributionNodeInternal>& attributions, const string& name,
122         uint64_t timestampNs);
123 
124 // Create log event when the app sync ends.
125 std::unique_ptr<LogEvent> CreateSyncEndEvent(
126         const std::vector<AttributionNodeInternal>& attributions, const string& name,
127         uint64_t timestampNs);
128 
129 // Create log event when the app sync ends.
130 std::unique_ptr<LogEvent> CreateAppCrashEvent(
131     const int uid, uint64_t timestampNs);
132 
133 // Create log event for acquiring wakelock.
134 std::unique_ptr<LogEvent> CreateAcquireWakelockEvent(
135         const std::vector<AttributionNodeInternal>& attributions, const string& wakelockName,
136         uint64_t timestampNs);
137 
138 // Create log event for releasing wakelock.
139 std::unique_ptr<LogEvent> CreateReleaseWakelockEvent(
140         const std::vector<AttributionNodeInternal>& attributions, const string& wakelockName,
141         uint64_t timestampNs);
142 
143 // Create log event for releasing wakelock.
144 std::unique_ptr<LogEvent> CreateIsolatedUidChangedEvent(
145     int isolatedUid, int hostUid, bool is_create, uint64_t timestampNs);
146 
147 // Helper function to create an AttributionNodeInternal proto.
148 AttributionNodeInternal CreateAttribution(const int& uid, const string& tag);
149 
150 // Create a statsd log event processor upon the start time in seconds, config and key.
151 sp<StatsLogProcessor> CreateStatsLogProcessor(const long timeBaseSec, const StatsdConfig& config,
152                                               const ConfigKey& key);
153 
154 // Util function to sort the log events by timestamp.
155 void sortLogEventsByTimestamp(std::vector<std::unique_ptr<LogEvent>> *events);
156 
157 int64_t StringToId(const string& str);
158 
159 }  // namespace statsd
160 }  // namespace os
161 }  // namespace android