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" |
Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame] | 22 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
| 23 | #include "guardrail/StatsdStats.h" |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 24 | #include "statslog.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 | |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 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 | |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 47 | // Convert the TimeUnit enum to the bucket size in millis. |
| 48 | int64_t TimeUnitToBucketSizeInMillis(TimeUnit unit); |
| 49 | |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 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 | |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 68 | int64_t NanoToMillis(const int64_t nano); |
| 69 | |
| 70 | int64_t MillisToNano(const int64_t millis); |
| 71 | |
Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame] | 72 | // Helper function to write PulledAtomStats to ProtoOutputStream |
| 73 | void writePullerStatsToStream(const std::pair<int, StatsdStats::PulledAtomStats>& pair, |
| 74 | util::ProtoOutputStream* protoOutput); |
Yangster-mac | 87718e2 | 2018-01-11 16:16:26 -0800 | [diff] [blame] | 75 | |
Misha Wagner | 1eee221 | 2019-01-22 11:47:11 +0000 | [diff] [blame^] | 76 | // Helper function to write AtomMetricStats to ProtoOutputStream |
| 77 | void writeAtomMetricStatsToStream(const std::pair<int, StatsdStats::AtomMetricStats> &pair, |
| 78 | util::ProtoOutputStream *protoOutput); |
| 79 | |
Yangster-mac | 87718e2 | 2018-01-11 16:16:26 -0800 | [diff] [blame] | 80 | template<class T> |
| 81 | bool parseProtoOutputStream(util::ProtoOutputStream& protoOutput, T* message) { |
| 82 | std::string pbBytes; |
| 83 | auto iter = protoOutput.data(); |
| 84 | while (iter.readBuffer() != NULL) { |
| 85 | size_t toRead = iter.currentToRead(); |
| 86 | pbBytes.append(reinterpret_cast<const char*>(iter.readBuffer()), toRead); |
| 87 | iter.rp()->move(toRead); |
| 88 | } |
| 89 | return message->ParseFromArray(pbBytes.c_str(), pbBytes.size()); |
| 90 | } |
| 91 | |
Yangster-mac | d5c3562 | 2018-02-02 10:33:25 -0800 | [diff] [blame] | 92 | // Returns the truncated timestamp. |
| 93 | int64_t truncateTimestampNsToFiveMinutes(int64_t timestampNs); |
| 94 | |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 95 | inline bool isPushedAtom(int atomId) { |
| 96 | return atomId <= util::kMaxPushedAtomId && atomId > 1; |
| 97 | } |
| 98 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 99 | } // namespace statsd |
| 100 | } // namespace os |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 101 | } // namespace android |