blob: f1f45a2d3bc71666172ae2a675472c49be7c9514 [file] [log] [blame]
Joe Onoratoc4dfae52017-10-17 23:38:21 -07001/*
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
Yao Chen8a8d16c2018-02-08 14:50:40 -080019#include "FieldValue.h"
Joe Onoratoc4dfae52017-10-17 23:38:21 -070020
Howard Roa46b6582018-09-18 16:45:02 -070021#include <android/frameworks/stats/1.0/types.h>
Chenjie Yu12e5e672018-09-14 15:54:59 -070022#include <android/os/StatsLogEventWrapper.h>
Yao Chen5110bed2017-10-23 12:50:02 -070023#include <android/util/ProtoOutputStream.h>
Joe Onoratoc4dfae52017-10-17 23:38:21 -070024#include <log/log_read.h>
Yao Chen80235402017-11-13 20:42:25 -080025#include <private/android_logger.h>
Tej Singha02bfab2019-10-01 19:03:24 -070026#include <stats_event_list.h>
Yao Chen5110bed2017-10-23 12:50:02 -070027#include <utils/Errors.h>
Joe Onoratoc4dfae52017-10-17 23:38:21 -070028
29#include <string>
30#include <vector>
31
Howard Roa46b6582018-09-18 16:45:02 -070032using namespace android::frameworks::stats::V1_0;
33
Joe Onoratoc4dfae52017-10-17 23:38:21 -070034namespace android {
35namespace os {
36namespace statsd {
37
Yao Chen9c1debe2018-02-19 14:39:19 -080038struct AttributionNodeInternal {
39 void set_uid(int32_t id) {
40 mUid = id;
41 }
42
43 void set_tag(const std::string& value) {
44 mTag = value;
45 }
46
47 int32_t uid() const {
48 return mUid;
49 }
50
51 const std::string& tag() const {
52 return mTag;
53 }
54
55 int32_t mUid;
56 std::string mTag;
57};
Chenjie Yu97dbb202019-02-13 16:42:04 -080058
59struct InstallTrainInfo {
60 int64_t trainVersionCode;
Muhammad Qureshif4ca8242019-03-01 09:20:15 -080061 std::string trainName;
62 int32_t status;
Jeff Hamiltonfa2f91c2019-03-22 00:25:02 -040063 std::vector<int64_t> experimentIds;
Chenjie Yu97dbb202019-02-13 16:42:04 -080064};
Jeff Hamiltonfa2f91c2019-03-22 00:25:02 -040065
Joe Onoratoc4dfae52017-10-17 23:38:21 -070066/**
67 * Wrapper for the log_msg structure.
68 */
69class LogEvent {
70public:
71 /**
72 * Read a LogEvent from a log_msg.
73 */
David Chena3bf0502017-11-01 16:57:43 -070074 explicit LogEvent(log_msg& msg);
Joe Onoratoc4dfae52017-10-17 23:38:21 -070075
Chenjie Yud7e3a222018-11-28 21:29:44 +000076 /**
77 * Creates LogEvent from StatsLogEventWrapper.
78 */
79 static void createLogEvents(const StatsLogEventWrapper& statsLogEventWrapper,
80 std::vector<std::shared_ptr<LogEvent>>& logEvents);
81
82 /**
83 * Construct one LogEvent from a StatsLogEventWrapper with the i-th work chain. -1 if no chain.
84 */
85 explicit LogEvent(const StatsLogEventWrapper& statsLogEventWrapper, int workChainIndex);
Chenjie Yu12e5e672018-09-14 15:54:59 -070086
Joe Onoratoc4dfae52017-10-17 23:38:21 -070087 /**
Yao Chen80235402017-11-13 20:42:25 -080088 * Constructs a LogEvent with synthetic data for testing. Must call init() before reading.
Joe Onoratoc4dfae52017-10-17 23:38:21 -070089 */
Yangster-mac330af582018-02-08 15:24:38 -080090 explicit LogEvent(int32_t tagId, int64_t wallClockTimestampNs, int64_t elapsedTimestampNs);
91
92 // For testing. The timestamp is used as both elapsed real time and logd timestamp.
93 explicit LogEvent(int32_t tagId, int64_t timestampNs);
David Chen1481fe12017-10-16 13:16:34 -070094
Howard Ro1a2a3992018-10-22 22:51:57 -070095 // For testing. The timestamp is used as both elapsed real time and logd timestamp.
96 explicit LogEvent(int32_t tagId, int64_t timestampNs, int32_t uid);
97
Yangster-mac48b3d622018-08-18 12:38:11 -070098 /**
99 * Constructs a KeyValuePairsAtom LogEvent from value maps.
100 */
101 explicit LogEvent(int32_t tagId, int64_t wallClockTimestampNs, int64_t elapsedTimestampNs,
102 int32_t uid,
Howard Ro4078dd42018-09-27 17:41:08 -0700103 const std::map<int32_t, int32_t>& int_map,
104 const std::map<int32_t, int64_t>& long_map,
Yangster-mac48b3d622018-08-18 12:38:11 -0700105 const std::map<int32_t, std::string>& string_map,
106 const std::map<int32_t, float>& float_map);
107
Chenjie Yu6b1667c2019-01-18 10:09:33 -0800108 // Constructs a BinaryPushStateChanged LogEvent from API call.
109 explicit LogEvent(const std::string& trainName, int64_t trainVersionCode, bool requiresStaging,
110 bool rollbackEnabled, bool requiresLowLatencyMonitor, int32_t state,
111 const std::vector<uint8_t>& experimentIds, int32_t userId);
112
Howard Roa46b6582018-09-18 16:45:02 -0700113 explicit LogEvent(int64_t wallClockTimestampNs, int64_t elapsedTimestampNs,
Maggie White58174da2019-01-18 15:23:35 -0800114 const VendorAtom& vendorAtom);
115
Chenjie Yu97dbb202019-02-13 16:42:04 -0800116 explicit LogEvent(int64_t wallClockTimestampNs, int64_t elapsedTimestampNs,
117 const InstallTrainInfo& installTrainInfo);
118
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700119 ~LogEvent();
120
121 /**
122 * Get the timestamp associated with this event.
123 */
Yangster-mac330af582018-02-08 15:24:38 -0800124 inline int64_t GetLogdTimestampNs() const { return mLogdTimestampNs; }
125 inline int64_t GetElapsedTimestampNs() const { return mElapsedTimestampNs; }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700126
127 /**
128 * Get the tag for this event.
129 */
Yangster-mac68985802018-01-21 10:05:09 -0800130 inline int GetTagId() const { return mTagId; }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700131
Yangster-mac68985802018-01-21 10:05:09 -0800132 inline uint32_t GetUid() const {
Yao Chend10f7b12017-12-18 12:53:50 -0800133 return mLogUid;
134 }
135
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700136 /**
137 * Get the nth value, starting at 1.
138 *
139 * Returns BAD_INDEX if the index is larger than the number of elements.
140 * Returns BAD_TYPE if the index is available but the data is the wrong type.
141 */
142 int64_t GetLong(size_t key, status_t* err) const;
Chenjie Yu80f91122018-01-31 20:24:50 -0800143 int GetInt(size_t key, status_t* err) const;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700144 const char* GetString(size_t key, status_t* err) const;
145 bool GetBool(size_t key, status_t* err) const;
146 float GetFloat(size_t key, status_t* err) const;
147
148 /**
Yao Chen80235402017-11-13 20:42:25 -0800149 * Write test data to the LogEvent. This can only be used when the LogEvent is constructed
150 * using LogEvent(tagId, timestampNs). You need to call init() before you can read from it.
151 */
152 bool write(uint32_t value);
153 bool write(int32_t value);
154 bool write(uint64_t value);
155 bool write(int64_t value);
Yao Chen9c1debe2018-02-19 14:39:19 -0800156 bool write(const std::string& value);
Yao Chen80235402017-11-13 20:42:25 -0800157 bool write(float value);
Yao Chen9c1debe2018-02-19 14:39:19 -0800158 bool write(const std::vector<AttributionNodeInternal>& nodes);
159 bool write(const AttributionNodeInternal& node);
Tej Singha02bfab2019-10-01 19:03:24 -0700160 bool writeBytes(const std::string& value);
Howard Ro1a2a3992018-10-22 22:51:57 -0700161 bool writeKeyValuePairs(int32_t uid,
162 const std::map<int32_t, int32_t>& int_map,
Howard Ro4078dd42018-09-27 17:41:08 -0700163 const std::map<int32_t, int64_t>& long_map,
Yangster-mace124e422018-08-16 10:30:28 -0700164 const std::map<int32_t, std::string>& string_map,
165 const std::map<int32_t, float>& float_map);
Yao Chen80235402017-11-13 20:42:25 -0800166
167 /**
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700168 * Return a string representation of this event.
169 */
Yao Chen9c1debe2018-02-19 14:39:19 -0800170 std::string ToString() const;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700171
172 /**
Yao Chen5110bed2017-10-23 12:50:02 -0700173 * Write this object to a ProtoOutputStream.
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700174 */
Yao Chen5110bed2017-10-23 12:50:02 -0700175 void ToProto(android::util::ProtoOutputStream& out) const;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700176
David Chen1481fe12017-10-16 13:16:34 -0700177 /**
David Chen1481fe12017-10-16 13:16:34 -0700178 * Used with the constructor where tag is passed in. Converts the log_event_list to read mode
179 * and prepares the list for reading.
180 */
181 void init();
182
Chenjie Yua7259ab2017-12-10 08:31:05 -0800183 /**
Yangster-mac330af582018-02-08 15:24:38 -0800184 * Set elapsed timestamp if the original timestamp is missing.
Chenjie Yua7259ab2017-12-10 08:31:05 -0800185 */
Yangster-mac330af582018-02-08 15:24:38 -0800186 void setElapsedTimestampNs(int64_t timestampNs) {
187 mElapsedTimestampNs = timestampNs;
188 }
189
190 /**
191 * Set the timestamp if the original logd timestamp is missing.
192 */
193 void setLogdWallClockTimestampNs(int64_t timestampNs) {
194 mLogdTimestampNs = timestampNs;
195 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800196
Yangster-mac20877162017-12-22 17:19:39 -0800197 inline int size() const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800198 return mValues.size();
Chenjie Yud9dfda72017-12-11 17:41:20 -0800199 }
200
Yao Chen8a8d16c2018-02-08 14:50:40 -0800201 const std::vector<FieldValue>& getValues() const {
202 return mValues;
203 }
Yangster-macd40053e2018-01-09 16:29:22 -0800204
Yao Chen8a8d16c2018-02-08 14:50:40 -0800205 std::vector<FieldValue>* getMutableValues() {
206 return &mValues;
207 }
Yangster-mac20877162017-12-22 17:19:39 -0800208
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800209 inline LogEvent makeCopy() {
210 return LogEvent(*this);
211 }
212
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700213private:
214 /**
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800215 * Only use this if copy is absolutely needed.
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700216 */
Chih-Hung Hsieh3227aab2018-12-20 13:42:28 -0800217 LogEvent(const LogEvent&);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700218
219 /**
220 * Parses a log_msg into a LogEvent object.
221 */
Yao Chen80235402017-11-13 20:42:25 -0800222 void init(android_log_context context);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700223
Yao Chen8a8d16c2018-02-08 14:50:40 -0800224 // The items are naturally sorted in DFS order as we read them. this allows us to do fast
225 // matching.
226 std::vector<FieldValue> mValues;
Yao Chen80235402017-11-13 20:42:25 -0800227
Yangster-mac20877162017-12-22 17:19:39 -0800228 // This field is used when statsD wants to create log event object and write fields to it. After
229 // calling init() function, this object would be destroyed to save memory usage.
230 // When the log event is created from log msg, this field is never initiated.
Yao Chen48d75182018-01-23 09:40:48 -0800231 android_log_context mContext = NULL;
Yao Chen93fe3a32017-11-02 13:52:59 -0700232
Yangster-mac330af582018-02-08 15:24:38 -0800233 // The timestamp set by the logd.
234 int64_t mLogdTimestampNs;
235
236 // The elapsed timestamp set by statsd log writer.
237 int64_t mElapsedTimestampNs;
Yao Chen93fe3a32017-11-02 13:52:59 -0700238
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700239 int mTagId;
Yao Chend10f7b12017-12-18 12:53:50 -0800240
241 uint32_t mLogUid;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700242};
243
Jeff Hamiltonfa2f91c2019-03-22 00:25:02 -0400244void writeExperimentIdsToProto(const std::vector<int64_t>& experimentIds, std::vector<uint8_t>* protoOut);
245
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700246} // namespace statsd
247} // namespace os
248} // namespace android
249