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