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