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" |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 22 | #include "frameworks/base/cmds/statsd/src/stats_log_common.pb.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); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 32 | void writeDimensionToProto(const HashableDimensionKey& dimension, |
| 33 | util::ProtoOutputStream* protoOutput); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 34 | |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 35 | // Convert the TimeUnit enum to the bucket size in millis with a guardrail on |
| 36 | // bucket size. |
| 37 | int64_t TimeUnitToBucketSizeInMillisGuardrailed(int uid, TimeUnit unit); |
| 38 | |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 39 | // Convert the TimeUnit enum to the bucket size in millis. |
| 40 | int64_t TimeUnitToBucketSizeInMillis(TimeUnit unit); |
| 41 | |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 42 | // Gets the elapsed timestamp in ns. |
| 43 | int64_t getElapsedRealtimeNs(); |
| 44 | |
| 45 | // Gets the elapsed timestamp in millis. |
| 46 | int64_t getElapsedRealtimeMillis(); |
| 47 | |
| 48 | // Gets the elapsed timestamp in seconds. |
| 49 | int64_t getElapsedRealtimeSec(); |
| 50 | |
| 51 | // Gets the wall clock timestamp in ns. |
| 52 | int64_t getWallClockNs(); |
| 53 | |
| 54 | // Gets the wall clock timestamp in millis. |
| 55 | int64_t getWallClockMillis(); |
| 56 | |
| 57 | // Gets the wall clock timestamp in seconds. |
| 58 | int64_t getWallClockSec(); |
| 59 | |
Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame] | 60 | // Helper function to write PulledAtomStats to ProtoOutputStream |
| 61 | void writePullerStatsToStream(const std::pair<int, StatsdStats::PulledAtomStats>& pair, |
| 62 | util::ProtoOutputStream* protoOutput); |
Yangster-mac | 87718e2 | 2018-01-11 16:16:26 -0800 | [diff] [blame] | 63 | |
| 64 | template<class T> |
| 65 | bool parseProtoOutputStream(util::ProtoOutputStream& protoOutput, T* message) { |
| 66 | std::string pbBytes; |
| 67 | auto iter = protoOutput.data(); |
| 68 | while (iter.readBuffer() != NULL) { |
| 69 | size_t toRead = iter.currentToRead(); |
| 70 | pbBytes.append(reinterpret_cast<const char*>(iter.readBuffer()), toRead); |
| 71 | iter.rp()->move(toRead); |
| 72 | } |
| 73 | return message->ParseFromArray(pbBytes.c_str(), pbBytes.size()); |
| 74 | } |
| 75 | |
Yangster-mac | d5c3562 | 2018-02-02 10:33:25 -0800 | [diff] [blame] | 76 | // Returns the truncated timestamp. |
| 77 | int64_t truncateTimestampNsToFiveMinutes(int64_t timestampNs); |
| 78 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 79 | } // namespace statsd |
| 80 | } // namespace os |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 81 | } // namespace android |