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