blob: dcea0e653e22631c6a06af1ce5e04b54b8841b73 [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"
Chenjie Yub038b702017-12-18 15:15:34 -080022#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
23#include "guardrail/StatsdStats.h"
Chenjie Yuc715b9e2018-10-19 07:52:12 -070024#include "statslog.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
yro59cc24d2018-02-13 20:17:32 -080043// Convert the TimeUnit enum to the bucket size in millis with a guardrail on
44// bucket size.
45int64_t TimeUnitToBucketSizeInMillisGuardrailed(int uid, TimeUnit unit);
46
Yangster-macb8144812018-01-04 10:56:23 -080047// Convert the TimeUnit enum to the bucket size in millis.
48int64_t TimeUnitToBucketSizeInMillis(TimeUnit unit);
49
Yangster-mac330af582018-02-08 15:24:38 -080050// Gets the elapsed timestamp in ns.
51int64_t getElapsedRealtimeNs();
52
53// Gets the elapsed timestamp in millis.
54int64_t getElapsedRealtimeMillis();
55
56// Gets the elapsed timestamp in seconds.
57int64_t getElapsedRealtimeSec();
58
59// Gets the wall clock timestamp in ns.
60int64_t getWallClockNs();
61
62// Gets the wall clock timestamp in millis.
63int64_t getWallClockMillis();
64
65// Gets the wall clock timestamp in seconds.
66int64_t getWallClockSec();
67
Yangster-mac9def8e32018-04-17 13:55:51 -070068int64_t NanoToMillis(const int64_t nano);
69
70int64_t MillisToNano(const int64_t millis);
71
Chenjie Yub038b702017-12-18 15:15:34 -080072// Helper function to write PulledAtomStats to ProtoOutputStream
73void writePullerStatsToStream(const std::pair<int, StatsdStats::PulledAtomStats>& pair,
74 util::ProtoOutputStream* protoOutput);
Yangster-mac87718e22018-01-11 16:16:26 -080075
Misha Wagner1eee2212019-01-22 11:47:11 +000076// Helper function to write AtomMetricStats to ProtoOutputStream
77void writeAtomMetricStatsToStream(const std::pair<int, StatsdStats::AtomMetricStats> &pair,
78 util::ProtoOutputStream *protoOutput);
79
Yangster-mac87718e22018-01-11 16:16:26 -080080template<class T>
81bool 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-macd5c35622018-02-02 10:33:25 -080092// Returns the truncated timestamp.
93int64_t truncateTimestampNsToFiveMinutes(int64_t timestampNs);
94
Chenjie Yuc715b9e2018-10-19 07:52:12 -070095inline bool isPushedAtom(int atomId) {
96 return atomId <= util::kMaxPushedAtomId && atomId > 1;
97}
98
Yangster-mac20877162017-12-22 17:19:39 -080099} // namespace statsd
100} // namespace os
yro59cc24d2018-02-13 20:17:32 -0800101} // namespace android