blob: aec09561b7c6f126f9b830f8cf29dd99d9b6e962 [file] [log] [blame]
Yangster-mac20877162017-12-22 17:19:39 -08001/*
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>
Yao Chen8a8d16c2018-02-08 14:50:40 -080020#include "FieldValue.h"
21#include "HashableDimensionKey.h"
Muhammad Qureshic8e22662019-11-20 17:18:03 -080022#include "atoms_info.h"
Chenjie Yub038b702017-12-18 15:15:34 -080023#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
24#include "guardrail/StatsdStats.h"
Yangster-mac20877162017-12-22 17:19:39 -080025
26namespace android {
27namespace os {
28namespace statsd {
29
Yao Chen8a8d16c2018-02-08 14:50:40 -080030void writeFieldValueTreeToStream(int tagId, const std::vector<FieldValue>& values,
Chenjie Yub038b702017-12-18 15:15:34 -080031 util::ProtoOutputStream* protoOutput);
Yangster-mac9def8e32018-04-17 13:55:51 -070032void writeDimensionToProto(const HashableDimensionKey& dimension, std::set<string> *str_set,
Yao Chen8a8d16c2018-02-08 14:50:40 -080033 util::ProtoOutputStream* protoOutput);
Yangster-mac20877162017-12-22 17:19:39 -080034
Yangster-mac9def8e32018-04-17 13:55:51 -070035void writeDimensionLeafNodesToProto(const HashableDimensionKey& dimension,
36 const int dimensionLeafFieldId,
37 std::set<string> *str_set,
38 util::ProtoOutputStream* protoOutput);
39
40void writeDimensionPathToProto(const std::vector<Matcher>& fieldMatchers,
41 util::ProtoOutputStream* protoOutput);
42
tsaichristine69000e62019-10-18 17:34:52 -070043void writeStateToProto(const FieldValue& state, util::ProtoOutputStream* protoOutput);
44
yro59cc24d2018-02-13 20:17:32 -080045// Convert the TimeUnit enum to the bucket size in millis with a guardrail on
46// bucket size.
47int64_t TimeUnitToBucketSizeInMillisGuardrailed(int uid, TimeUnit unit);
48
Yangster-macb8144812018-01-04 10:56:23 -080049// Convert the TimeUnit enum to the bucket size in millis.
50int64_t TimeUnitToBucketSizeInMillis(TimeUnit unit);
51
Yangster-mac330af582018-02-08 15:24:38 -080052// Gets the elapsed timestamp in ns.
53int64_t getElapsedRealtimeNs();
54
55// Gets the elapsed timestamp in millis.
56int64_t getElapsedRealtimeMillis();
57
58// Gets the elapsed timestamp in seconds.
59int64_t getElapsedRealtimeSec();
60
61// Gets the wall clock timestamp in ns.
62int64_t getWallClockNs();
63
64// Gets the wall clock timestamp in millis.
65int64_t getWallClockMillis();
66
67// Gets the wall clock timestamp in seconds.
68int64_t getWallClockSec();
69
Yangster-mac9def8e32018-04-17 13:55:51 -070070int64_t NanoToMillis(const int64_t nano);
71
72int64_t MillisToNano(const int64_t millis);
73
Chenjie Yub038b702017-12-18 15:15:34 -080074// Helper function to write PulledAtomStats to ProtoOutputStream
75void writePullerStatsToStream(const std::pair<int, StatsdStats::PulledAtomStats>& pair,
76 util::ProtoOutputStream* protoOutput);
Yangster-mac87718e22018-01-11 16:16:26 -080077
Misha Wagner1eee2212019-01-22 11:47:11 +000078// Helper function to write AtomMetricStats to ProtoOutputStream
Olivier Gaillardf48fb042019-02-13 11:03:20 +000079void writeAtomMetricStatsToStream(const std::pair<int64_t, StatsdStats::AtomMetricStats> &pair,
Misha Wagner1eee2212019-01-22 11:47:11 +000080 util::ProtoOutputStream *protoOutput);
81
Yangster-mac87718e22018-01-11 16:16:26 -080082template<class T>
83bool parseProtoOutputStream(util::ProtoOutputStream& protoOutput, T* message) {
84 std::string pbBytes;
Joe Onorato99598ee2019-02-11 15:55:13 +000085 sp<android::util::ProtoReader> reader = protoOutput.data();
86 while (reader->readBuffer() != NULL) {
87 size_t toRead = reader->currentToRead();
88 pbBytes.append(reinterpret_cast<const char*>(reader->readBuffer()), toRead);
89 reader->move(toRead);
Yangster-mac87718e22018-01-11 16:16:26 -080090 }
91 return message->ParseFromArray(pbBytes.c_str(), pbBytes.size());
92}
93
Tej Singh29ac6012019-06-05 18:10:12 -070094// Checks the blacklist of atoms as well as the blacklisted range of 300,000 - 304,999.
95// Returns the truncated timestamp to the nearest 5 minutes if needed.
96int64_t truncateTimestampIfNecessary(int atomId, int64_t timestampNs);
Yangster-macd5c35622018-02-02 10:33:25 -080097
Jonathan Nguyena0e6de12020-01-28 18:33:55 -080098// Checks permission for given pid and uid.
99bool checkPermissionForIds(const char* permission, pid_t pid, uid_t uid);
100
Tej Singhde473b52019-04-10 19:25:03 -0700101inline bool isVendorPulledAtom(int atomId) {
102 return atomId >= StatsdStats::kVendorPulledAtomStartTag && atomId < StatsdStats::kMaxAtomTag;
103}
104
Tej Singh97db3ff2020-01-27 16:52:17 -0800105inline bool isPulledAtom(int atomId) {
106 return atomId >= StatsdStats::kPullAtomStartTag && atomId < StatsdStats::kVendorAtomStartTag;
107}
108
Yangster-mac20877162017-12-22 17:19:39 -0800109} // namespace statsd
110} // namespace os
yro59cc24d2018-02-13 20:17:32 -0800111} // namespace android