blob: 1ff95f7240aca9592899949ae7feca18362a6848 [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>
Ruchir Rastogi1fb525e2019-11-07 14:08:24 -080027#include <stats_event.h>
Yao Chen5110bed2017-10-23 12:50:02 -070028#include <utils/Errors.h>
Joe Onoratoc4dfae52017-10-17 23:38:21 -070029
30#include <string>
31#include <vector>
32
Howard Roa46b6582018-09-18 16:45:02 -070033using namespace android::frameworks::stats::V1_0;
34
Joe Onoratoc4dfae52017-10-17 23:38:21 -070035namespace android {
36namespace os {
37namespace statsd {
38
Yao Chen9c1debe2018-02-19 14:39:19 -080039struct AttributionNodeInternal {
40 void set_uid(int32_t id) {
41 mUid = id;
42 }
43
44 void set_tag(const std::string& value) {
45 mTag = value;
46 }
47
48 int32_t uid() const {
49 return mUid;
50 }
51
52 const std::string& tag() const {
53 return mTag;
54 }
55
56 int32_t mUid;
57 std::string mTag;
58};
Chenjie Yu97dbb202019-02-13 16:42:04 -080059
60struct InstallTrainInfo {
61 int64_t trainVersionCode;
Muhammad Qureshif4ca8242019-03-01 09:20:15 -080062 std::string trainName;
63 int32_t status;
Jeff Hamiltonfa2f91c2019-03-22 00:25:02 -040064 std::vector<int64_t> experimentIds;
Chenjie Yu97dbb202019-02-13 16:42:04 -080065};
Jeff Hamiltonfa2f91c2019-03-22 00:25:02 -040066
Joe Onoratoc4dfae52017-10-17 23:38:21 -070067/**
68 * Wrapper for the log_msg structure.
69 */
70class LogEvent {
71public:
72 /**
Ruchir Rastogi1736ba42019-11-04 14:37:13 -080073 * Read a LogEvent from the socket
Joe Onoratoc4dfae52017-10-17 23:38:21 -070074 */
Ruchir Rastogi1736ba42019-11-04 14:37:13 -080075 explicit LogEvent(uint8_t* msg, uint32_t len, uint32_t uid);
Joe Onoratoc4dfae52017-10-17 23:38:21 -070076
Chenjie Yud7e3a222018-11-28 21:29:44 +000077 /**
78 * Creates LogEvent from StatsLogEventWrapper.
79 */
80 static void createLogEvents(const StatsLogEventWrapper& statsLogEventWrapper,
81 std::vector<std::shared_ptr<LogEvent>>& logEvents);
82
83 /**
84 * Construct one LogEvent from a StatsLogEventWrapper with the i-th work chain. -1 if no chain.
85 */
86 explicit LogEvent(const StatsLogEventWrapper& statsLogEventWrapper, int workChainIndex);
Chenjie Yu12e5e672018-09-14 15:54:59 -070087
Joe Onoratoc4dfae52017-10-17 23:38:21 -070088 /**
Yao Chen80235402017-11-13 20:42:25 -080089 * Constructs a LogEvent with synthetic data for testing. Must call init() before reading.
Joe Onoratoc4dfae52017-10-17 23:38:21 -070090 */
Yangster-mac330af582018-02-08 15:24:38 -080091 explicit LogEvent(int32_t tagId, int64_t wallClockTimestampNs, int64_t elapsedTimestampNs);
92
93 // For testing. The timestamp is used as both elapsed real time and logd timestamp.
94 explicit LogEvent(int32_t tagId, int64_t timestampNs);
David Chen1481fe12017-10-16 13:16:34 -070095
Howard Ro1a2a3992018-10-22 22:51:57 -070096 // For testing. The timestamp is used as both elapsed real time and logd timestamp.
97 explicit LogEvent(int32_t tagId, int64_t timestampNs, int32_t uid);
98
Yangster-mac48b3d622018-08-18 12:38:11 -070099 /**
100 * Constructs a KeyValuePairsAtom LogEvent from value maps.
101 */
102 explicit LogEvent(int32_t tagId, int64_t wallClockTimestampNs, int64_t elapsedTimestampNs,
103 int32_t uid,
Howard Ro4078dd42018-09-27 17:41:08 -0700104 const std::map<int32_t, int32_t>& int_map,
105 const std::map<int32_t, int64_t>& long_map,
Yangster-mac48b3d622018-08-18 12:38:11 -0700106 const std::map<int32_t, std::string>& string_map,
107 const std::map<int32_t, float>& float_map);
108
Chenjie Yu6b1667c2019-01-18 10:09:33 -0800109 // Constructs a BinaryPushStateChanged LogEvent from API call.
110 explicit LogEvent(const std::string& trainName, int64_t trainVersionCode, bool requiresStaging,
111 bool rollbackEnabled, bool requiresLowLatencyMonitor, int32_t state,
112 const std::vector<uint8_t>& experimentIds, int32_t userId);
113
Howard Roa46b6582018-09-18 16:45:02 -0700114 explicit LogEvent(int64_t wallClockTimestampNs, int64_t elapsedTimestampNs,
Maggie White58174da2019-01-18 15:23:35 -0800115 const VendorAtom& vendorAtom);
116
Chenjie Yu97dbb202019-02-13 16:42:04 -0800117 explicit LogEvent(int64_t wallClockTimestampNs, int64_t elapsedTimestampNs,
118 const InstallTrainInfo& installTrainInfo);
119
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700120 ~LogEvent();
121
122 /**
123 * Get the timestamp associated with this event.
124 */
Yangster-mac330af582018-02-08 15:24:38 -0800125 inline int64_t GetLogdTimestampNs() const { return mLogdTimestampNs; }
126 inline int64_t GetElapsedTimestampNs() const { return mElapsedTimestampNs; }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700127
128 /**
129 * Get the tag for this event.
130 */
Yangster-mac68985802018-01-21 10:05:09 -0800131 inline int GetTagId() const { return mTagId; }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700132
Yangster-mac68985802018-01-21 10:05:09 -0800133 inline uint32_t GetUid() const {
Yao Chend10f7b12017-12-18 12:53:50 -0800134 return mLogUid;
135 }
136
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700137 /**
138 * Get the nth value, starting at 1.
139 *
140 * Returns BAD_INDEX if the index is larger than the number of elements.
141 * Returns BAD_TYPE if the index is available but the data is the wrong type.
142 */
143 int64_t GetLong(size_t key, status_t* err) const;
Chenjie Yu80f91122018-01-31 20:24:50 -0800144 int GetInt(size_t key, status_t* err) const;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700145 const char* GetString(size_t key, status_t* err) const;
146 bool GetBool(size_t key, status_t* err) const;
147 float GetFloat(size_t key, status_t* err) const;
148
149 /**
Yao Chen80235402017-11-13 20:42:25 -0800150 * Write test data to the LogEvent. This can only be used when the LogEvent is constructed
151 * using LogEvent(tagId, timestampNs). You need to call init() before you can read from it.
152 */
153 bool write(uint32_t value);
154 bool write(int32_t value);
155 bool write(uint64_t value);
156 bool write(int64_t value);
Yao Chen9c1debe2018-02-19 14:39:19 -0800157 bool write(const std::string& value);
Yao Chen80235402017-11-13 20:42:25 -0800158 bool write(float value);
Yao Chen9c1debe2018-02-19 14:39:19 -0800159 bool write(const std::vector<AttributionNodeInternal>& nodes);
160 bool write(const AttributionNodeInternal& node);
Tej Singha02bfab2019-10-01 19:03:24 -0700161 bool writeBytes(const std::string& value);
Howard Ro1a2a3992018-10-22 22:51:57 -0700162 bool writeKeyValuePairs(int32_t uid,
163 const std::map<int32_t, int32_t>& int_map,
Howard Ro4078dd42018-09-27 17:41:08 -0700164 const std::map<int32_t, int64_t>& long_map,
Yangster-mace124e422018-08-16 10:30:28 -0700165 const std::map<int32_t, std::string>& string_map,
166 const std::map<int32_t, float>& float_map);
Yao Chen80235402017-11-13 20:42:25 -0800167
168 /**
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700169 * Return a string representation of this event.
170 */
Yao Chen9c1debe2018-02-19 14:39:19 -0800171 std::string ToString() const;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700172
173 /**
Yao Chen5110bed2017-10-23 12:50:02 -0700174 * Write this object to a ProtoOutputStream.
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700175 */
Yao Chen5110bed2017-10-23 12:50:02 -0700176 void ToProto(android::util::ProtoOutputStream& out) const;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700177
David Chen1481fe12017-10-16 13:16:34 -0700178 /**
David Chen1481fe12017-10-16 13:16:34 -0700179 * Used with the constructor where tag is passed in. Converts the log_event_list to read mode
180 * and prepares the list for reading.
181 */
182 void init();
183
Chenjie Yua7259ab2017-12-10 08:31:05 -0800184 /**
Yangster-mac330af582018-02-08 15:24:38 -0800185 * Set elapsed timestamp if the original timestamp is missing.
Chenjie Yua7259ab2017-12-10 08:31:05 -0800186 */
Yangster-mac330af582018-02-08 15:24:38 -0800187 void setElapsedTimestampNs(int64_t timestampNs) {
188 mElapsedTimestampNs = timestampNs;
189 }
190
191 /**
192 * Set the timestamp if the original logd timestamp is missing.
193 */
194 void setLogdWallClockTimestampNs(int64_t timestampNs) {
195 mLogdTimestampNs = timestampNs;
196 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800197
Yangster-mac20877162017-12-22 17:19:39 -0800198 inline int size() const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800199 return mValues.size();
Chenjie Yud9dfda72017-12-11 17:41:20 -0800200 }
201
Yao Chen8a8d16c2018-02-08 14:50:40 -0800202 const std::vector<FieldValue>& getValues() const {
203 return mValues;
204 }
Yangster-macd40053e2018-01-09 16:29:22 -0800205
Yao Chen8a8d16c2018-02-08 14:50:40 -0800206 std::vector<FieldValue>* getMutableValues() {
207 return &mValues;
208 }
Yangster-mac20877162017-12-22 17:19:39 -0800209
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800210 bool isValid() {
211 return mValid;
212 }
213
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800214 inline LogEvent makeCopy() {
215 return LogEvent(*this);
216 }
217
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700218private:
219 /**
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800220 * Only use this if copy is absolutely needed.
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700221 */
Chih-Hung Hsieh3227aab2018-12-20 13:42:28 -0800222 LogEvent(const LogEvent&);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700223
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800224
225 /**
226 * Parsing function for new encoding scheme.
227 */
228 void initNew();
229
230 void parseInt32(int32_t* pos, int32_t depth, bool* last);
231 void parseInt64(int32_t* pos, int32_t depth, bool* last);
232 void parseString(int32_t* pos, int32_t depth, bool* last);
233 void parseFloat(int32_t* pos, int32_t depth, bool* last);
234 void parseBool(int32_t* pos, int32_t depth, bool* last);
235 void parseByteArray(int32_t* pos, int32_t depth, bool* last);
236 void parseKeyValuePairs(int32_t* pos, int32_t depth, bool* last);
237 void parseAttributionChain(int32_t* pos, int32_t depth, bool* last);
238
239 /**
240 * mBuf is a pointer to the current location in the buffer being parsed.
241 * Because the buffer lives on the StatsSocketListener stack, this pointer
242 * is only valid during the LogEvent constructor. It will be set to null at
243 * the end of initNew.
244 */
245 uint8_t* mBuf;
246
247 uint32_t mRemainingLen; // number of valid bytes left in the buffer being parsed
248 bool mValid = true; // stores whether the event we received from the socket is valid
249
250 /**
251 * Side-effects:
252 * If there is enough space in buffer to read value of type T
253 * - move mBuf past the value that was just read
254 * - decrement mRemainingLen by size of T
255 * Else
256 * - set mValid to false
257 */
258 template <class T>
259 T readNextValue() {
260 T value;
261 if (mRemainingLen < sizeof(T)) {
262 mValid = false;
263 value = 0; // all primitive types can successfully cast 0
264 } else {
265 value = *((T*)mBuf);
266 mBuf += sizeof(T);
267 mRemainingLen -= sizeof(T);
268 }
269 return value;
270 }
271
272 template <class T>
273 void addToValues(int32_t* pos, int32_t depth, T& value, bool* last) {
274 Field f = Field(mTagId, pos, depth);
275 // do not decorate last position at depth 0
276 for (int i = 1; i < depth; i++) {
277 if (last[i]) f.decorateLastPos(i);
278 }
279
280 Value v = Value(value);
281 mValues.push_back(FieldValue(f, v));
282 }
283
284 uint8_t getTypeId(uint8_t typeInfo);
285 uint8_t getNumAnnotations(uint8_t typeInfo);
286
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700287 /**
288 * Parses a log_msg into a LogEvent object.
289 */
Yao Chen80235402017-11-13 20:42:25 -0800290 void init(android_log_context context);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700291
Yao Chen8a8d16c2018-02-08 14:50:40 -0800292 // The items are naturally sorted in DFS order as we read them. this allows us to do fast
293 // matching.
294 std::vector<FieldValue> mValues;
Yao Chen80235402017-11-13 20:42:25 -0800295
Yangster-mac20877162017-12-22 17:19:39 -0800296 // This field is used when statsD wants to create log event object and write fields to it. After
297 // calling init() function, this object would be destroyed to save memory usage.
298 // When the log event is created from log msg, this field is never initiated.
Yao Chen48d75182018-01-23 09:40:48 -0800299 android_log_context mContext = NULL;
Yao Chen93fe3a32017-11-02 13:52:59 -0700300
Yangster-mac330af582018-02-08 15:24:38 -0800301 // The timestamp set by the logd.
302 int64_t mLogdTimestampNs;
303
304 // The elapsed timestamp set by statsd log writer.
305 int64_t mElapsedTimestampNs;
Yao Chen93fe3a32017-11-02 13:52:59 -0700306
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700307 int mTagId;
Yao Chend10f7b12017-12-18 12:53:50 -0800308
309 uint32_t mLogUid;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700310};
311
Jeff Hamiltonfa2f91c2019-03-22 00:25:02 -0400312void writeExperimentIdsToProto(const std::vector<int64_t>& experimentIds, std::vector<uint8_t>* protoOut);
313
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700314} // namespace statsd
315} // namespace os
316} // namespace android
317