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 MillisToNano(const int64_t millis);
80
81 // Helper function to write a stats field to ProtoOutputStream if it's a non-zero value.
82 void writeNonZeroStatToStream(const uint64_t fieldId, const int64_t value,
83 ProtoOutputStream* protoOutput);
84
85 // Helper function to write PulledAtomStats to ProtoOutputStream
86 void writePullerStatsToStream(const std::pair<int, StatsdStats::PulledAtomStats>& pair,
87 ProtoOutputStream* protoOutput);
88
89 // Helper function to write AtomMetricStats to ProtoOutputStream
90 void writeAtomMetricStatsToStream(const std::pair<int64_t, StatsdStats::AtomMetricStats> &pair,
91 ProtoOutputStream *protoOutput);
92
93 template<class T>
parseProtoOutputStream(ProtoOutputStream & protoOutput,T * message)94 bool parseProtoOutputStream(ProtoOutputStream& protoOutput, T* message) {
95 std::string pbBytes;
96 sp<android::util::ProtoReader> reader = protoOutput.data();
97 while (reader->readBuffer() != NULL) {
98 size_t toRead = reader->currentToRead();
99 pbBytes.append(reinterpret_cast<const char*>(reader->readBuffer()), toRead);
100 reader->move(toRead);
101 }
102 return message->ParseFromArray(pbBytes.c_str(), pbBytes.size());
103 }
104
105 // Checks the truncate timestamp annotation as well as the restricted range of 300,000 - 304,999.
106 // Returns the truncated timestamp to the nearest 5 minutes if needed.
107 int64_t truncateTimestampIfNecessary(const LogEvent& event);
108
109 // Checks permission for given pid and uid.
110 bool checkPermissionForIds(const char* permission, pid_t pid, uid_t uid);
111
isVendorPulledAtom(int atomId)112 inline bool isVendorPulledAtom(int atomId) {
113 return atomId >= StatsdStats::kVendorPulledAtomStartTag && atomId < StatsdStats::kMaxAtomTag;
114 }
115
isPulledAtom(int atomId)116 inline bool isPulledAtom(int atomId) {
117 return atomId >= StatsdStats::kPullAtomStartTag && atomId < StatsdStats::kVendorAtomStartTag;
118 }
119
120 void mapIsolatedUidsToHostUidInLogEvent(const sp<UidMap> uidMap, LogEvent& event);
121
122 std::string toHexString(const vector<uint8_t>& bytes);
123
124 } // namespace statsd
125 } // namespace os
126 } // namespace android
127