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