Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 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> |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 20 | #include "FieldValue.h" |
| 21 | #include "HashableDimensionKey.h" |
Muhammad Qureshi | c8e2266 | 2019-11-20 17:18:03 -0800 | [diff] [blame] | 22 | #include "atoms_info.h" |
Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame] | 23 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
| 24 | #include "guardrail/StatsdStats.h" |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | namespace os { |
| 28 | namespace statsd { |
| 29 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 30 | void writeFieldValueTreeToStream(int tagId, const std::vector<FieldValue>& values, |
Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame] | 31 | util::ProtoOutputStream* protoOutput); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 32 | void writeDimensionToProto(const HashableDimensionKey& dimension, std::set<string> *str_set, |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 33 | util::ProtoOutputStream* protoOutput); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 34 | |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 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 | |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 43 | void writeStateToProto(const FieldValue& state, util::ProtoOutputStream* protoOutput); |
| 44 | |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 45 | // Convert the TimeUnit enum to the bucket size in millis with a guardrail on |
| 46 | // bucket size. |
| 47 | int64_t TimeUnitToBucketSizeInMillisGuardrailed(int uid, TimeUnit unit); |
| 48 | |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 49 | // Convert the TimeUnit enum to the bucket size in millis. |
| 50 | int64_t TimeUnitToBucketSizeInMillis(TimeUnit unit); |
| 51 | |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 52 | // Gets the elapsed timestamp in ns. |
| 53 | int64_t getElapsedRealtimeNs(); |
| 54 | |
| 55 | // Gets the elapsed timestamp in millis. |
| 56 | int64_t getElapsedRealtimeMillis(); |
| 57 | |
| 58 | // Gets the elapsed timestamp in seconds. |
| 59 | int64_t getElapsedRealtimeSec(); |
| 60 | |
| 61 | // Gets the wall clock timestamp in ns. |
| 62 | int64_t getWallClockNs(); |
| 63 | |
| 64 | // Gets the wall clock timestamp in millis. |
| 65 | int64_t getWallClockMillis(); |
| 66 | |
| 67 | // Gets the wall clock timestamp in seconds. |
| 68 | int64_t getWallClockSec(); |
| 69 | |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 70 | int64_t NanoToMillis(const int64_t nano); |
| 71 | |
| 72 | int64_t MillisToNano(const int64_t millis); |
| 73 | |
Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame] | 74 | // Helper function to write PulledAtomStats to ProtoOutputStream |
| 75 | void writePullerStatsToStream(const std::pair<int, StatsdStats::PulledAtomStats>& pair, |
| 76 | util::ProtoOutputStream* protoOutput); |
Yangster-mac | 87718e2 | 2018-01-11 16:16:26 -0800 | [diff] [blame] | 77 | |
Misha Wagner | 1eee221 | 2019-01-22 11:47:11 +0000 | [diff] [blame] | 78 | // Helper function to write AtomMetricStats to ProtoOutputStream |
Olivier Gaillard | f48fb04 | 2019-02-13 11:03:20 +0000 | [diff] [blame] | 79 | void writeAtomMetricStatsToStream(const std::pair<int64_t, StatsdStats::AtomMetricStats> &pair, |
Misha Wagner | 1eee221 | 2019-01-22 11:47:11 +0000 | [diff] [blame] | 80 | util::ProtoOutputStream *protoOutput); |
| 81 | |
Yangster-mac | 87718e2 | 2018-01-11 16:16:26 -0800 | [diff] [blame] | 82 | template<class T> |
| 83 | bool parseProtoOutputStream(util::ProtoOutputStream& protoOutput, T* message) { |
| 84 | std::string pbBytes; |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 85 | 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-mac | 87718e2 | 2018-01-11 16:16:26 -0800 | [diff] [blame] | 90 | } |
| 91 | return message->ParseFromArray(pbBytes.c_str(), pbBytes.size()); |
| 92 | } |
| 93 | |
Tej Singh | 29ac601 | 2019-06-05 18:10:12 -0700 | [diff] [blame] | 94 | // 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. |
| 96 | int64_t truncateTimestampIfNecessary(int atomId, int64_t timestampNs); |
Yangster-mac | d5c3562 | 2018-02-02 10:33:25 -0800 | [diff] [blame] | 97 | |
Jonathan Nguyen | a0e6de1 | 2020-01-28 18:33:55 -0800 | [diff] [blame] | 98 | // Checks permission for given pid and uid. |
| 99 | bool checkPermissionForIds(const char* permission, pid_t pid, uid_t uid); |
| 100 | |
Tej Singh | de473b5 | 2019-04-10 19:25:03 -0700 | [diff] [blame] | 101 | inline bool isVendorPulledAtom(int atomId) { |
| 102 | return atomId >= StatsdStats::kVendorPulledAtomStartTag && atomId < StatsdStats::kMaxAtomTag; |
| 103 | } |
| 104 | |
Tej Singh | 97db3ff | 2020-01-27 16:52:17 -0800 | [diff] [blame] | 105 | inline bool isPulledAtom(int atomId) { |
| 106 | return atomId >= StatsdStats::kPullAtomStartTag && atomId < StatsdStats::kVendorAtomStartTag; |
| 107 | } |
| 108 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 109 | } // namespace statsd |
| 110 | } // namespace os |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 111 | } // namespace android |