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