1 /*
2  * Copyright (C) 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 
17 #pragma once
18 
19 #include <android/util/ProtoOutputStream.h>
20 
21 #include "FieldValue.h"
22 #include "HashableDimensionKey.h"
23 #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
24 #include "guardrail/StatsdStats.h"
25 #include "logd/LogEvent.h"
26 
27 using android::util::ProtoOutputStream;
28 
29 namespace android {
30 namespace os {
31 namespace statsd {
32 
33 void writeFieldValueTreeToStream(int tagId, const std::vector<FieldValue>& values,
34                                  ProtoOutputStream* protoOutput);
35 void writeDimensionToProto(const HashableDimensionKey& dimension, std::set<string> *str_set,
36                            ProtoOutputStream* protoOutput);
37 
38 void writeDimensionLeafNodesToProto(const HashableDimensionKey& dimension,
39                                     const int dimensionLeafFieldId,
40                                     std::set<string> *str_set,
41                                     ProtoOutputStream* protoOutput);
42 
43 void writeDimensionPathToProto(const std::vector<Matcher>& fieldMatchers,
44                                ProtoOutputStream* protoOutput);
45 
46 void writeStateToProto(const FieldValue& state, ProtoOutputStream* protoOutput);
47 
48 // Convert the TimeUnit enum to the bucket size in millis with a guardrail on
49 // bucket size.
50 int64_t TimeUnitToBucketSizeInMillisGuardrailed(int uid, TimeUnit unit);
51 
52 // Convert the TimeUnit enum to the bucket size in millis.
53 int64_t TimeUnitToBucketSizeInMillis(TimeUnit unit);
54 
55 // Gets the elapsed timestamp in ns.
56 int64_t getElapsedRealtimeNs();
57 
58 // Gets the elapsed timestamp in millis.
59 int64_t getElapsedRealtimeMillis();
60 
61 // Gets the elapsed timestamp in seconds.
62 int64_t getElapsedRealtimeSec();
63 
64 // Gets the system uptime in millis.
65 int64_t getSystemUptimeMillis();
66 
67 // Gets the wall clock timestamp in ns.
68 int64_t getWallClockNs();
69 
70 // Gets the wall clock timestamp in millis.
71 int64_t getWallClockMillis();
72 
73 // Gets the wall clock timestamp in seconds.
74 int64_t getWallClockSec();
75 
76 int64_t NanoToMillis(const int64_t nano);
77 
78 int64_t MillisToNano(const int64_t millis);
79 
80 // Helper function to write PulledAtomStats to ProtoOutputStream
81 void writePullerStatsToStream(const std::pair<int, StatsdStats::PulledAtomStats>& pair,
82                               ProtoOutputStream* protoOutput);
83 
84 // Helper function to write AtomMetricStats to ProtoOutputStream
85 void writeAtomMetricStatsToStream(const std::pair<int64_t, StatsdStats::AtomMetricStats> &pair,
86                                   ProtoOutputStream *protoOutput);
87 
88 template<class T>
parseProtoOutputStream(ProtoOutputStream & protoOutput,T * message)89 bool parseProtoOutputStream(ProtoOutputStream& protoOutput, T* message) {
90     std::string pbBytes;
91     sp<android::util::ProtoReader> reader = protoOutput.data();
92     while (reader->readBuffer() != NULL) {
93         size_t toRead = reader->currentToRead();
94          pbBytes.append(reinterpret_cast<const char*>(reader->readBuffer()), toRead);
95         reader->move(toRead);
96     }
97     return message->ParseFromArray(pbBytes.c_str(), pbBytes.size());
98 }
99 
100 // Checks the truncate timestamp annotation as well as the blacklisted range of 300,000 - 304,999.
101 // Returns the truncated timestamp to the nearest 5 minutes if needed.
102 int64_t truncateTimestampIfNecessary(const LogEvent& event);
103 
104 // Checks permission for given pid and uid.
105 bool checkPermissionForIds(const char* permission, pid_t pid, uid_t uid);
106 
isVendorPulledAtom(int atomId)107 inline bool isVendorPulledAtom(int atomId) {
108     return atomId >= StatsdStats::kVendorPulledAtomStartTag && atomId < StatsdStats::kMaxAtomTag;
109 }
110 
isPulledAtom(int atomId)111 inline bool isPulledAtom(int atomId) {
112     return atomId >= StatsdStats::kPullAtomStartTag && atomId < StatsdStats::kVendorAtomStartTag;
113 }
114 
115 }  // namespace statsd
116 }  // namespace os
117 }  // namespace android
118