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>
20#include "FieldValue.h"
21#include "HashableDimensionKey.h"
22#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
23#include "guardrail/StatsdStats.h"
24
25namespace android {
26namespace os {
27namespace statsd {
28
29void writeFieldValueTreeToStream(int tagId, const std::vector<FieldValue>& values,
30                                 util::ProtoOutputStream* protoOutput);
31void writeDimensionToProto(const HashableDimensionKey& dimension, std::set<string> *str_set,
32                           util::ProtoOutputStream* protoOutput);
33
34void writeDimensionLeafNodesToProto(const HashableDimensionKey& dimension,
35                                    const int dimensionLeafFieldId,
36                                    std::set<string> *str_set,
37                                    util::ProtoOutputStream* protoOutput);
38
39void writeDimensionPathToProto(const std::vector<Matcher>& fieldMatchers,
40                               util::ProtoOutputStream* protoOutput);
41
42// Convert the TimeUnit enum to the bucket size in millis with a guardrail on
43// bucket size.
44int64_t TimeUnitToBucketSizeInMillisGuardrailed(int uid, TimeUnit unit);
45
46// Convert the TimeUnit enum to the bucket size in millis.
47int64_t TimeUnitToBucketSizeInMillis(TimeUnit unit);
48
49// Gets the elapsed timestamp in ns.
50int64_t getElapsedRealtimeNs();
51
52// Gets the elapsed timestamp in millis.
53int64_t getElapsedRealtimeMillis();
54
55// Gets the elapsed timestamp in seconds.
56int64_t getElapsedRealtimeSec();
57
58// Gets the wall clock timestamp in ns.
59int64_t getWallClockNs();
60
61// Gets the wall clock timestamp in millis.
62int64_t getWallClockMillis();
63
64// Gets the wall clock timestamp in seconds.
65int64_t getWallClockSec();
66
67int64_t NanoToMillis(const int64_t nano);
68
69int64_t MillisToNano(const int64_t millis);
70
71// Helper function to write PulledAtomStats to ProtoOutputStream
72void writePullerStatsToStream(const std::pair<int, StatsdStats::PulledAtomStats>& pair,
73                              util::ProtoOutputStream* protoOutput);
74
75template<class T>
76bool parseProtoOutputStream(util::ProtoOutputStream& protoOutput, T* message) {
77    std::string pbBytes;
78    auto iter = protoOutput.data();
79    while (iter.readBuffer() != NULL) {
80        size_t toRead = iter.currentToRead();
81         pbBytes.append(reinterpret_cast<const char*>(iter.readBuffer()), toRead);
82        iter.rp()->move(toRead);
83    }
84    return message->ParseFromArray(pbBytes.c_str(), pbBytes.size());
85}
86
87// Returns the truncated timestamp.
88int64_t truncateTimestampNsToFiveMinutes(int64_t timestampNs);
89
90}  // namespace statsd
91}  // namespace os
92}  // namespace android
93