blob: 73e6572db50f141d323fe65c4ada0bbad146d88e [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
Tej Singh484524a2018-02-01 15:10:05 -080017#define DEBUG false // STOPSHIP if true
Joe Onoratoc4dfae52017-10-17 23:38:21 -070018#include "logd/LogEvent.h"
19
Yangster-mac20877162017-12-22 17:19:39 -080020#include "stats_log_util.h"
Yangster-mac48b3d622018-08-18 12:38:11 -070021#include "statslog.h"
Joe Onoratoc4dfae52017-10-17 23:38:21 -070022
23namespace android {
24namespace os {
25namespace statsd {
26
yro24809bd2017-10-31 23:06:53 -070027using namespace android::util;
Yao Chen9c1debe2018-02-19 14:39:19 -080028using android::util::ProtoOutputStream;
David Chen1481fe12017-10-16 13:16:34 -070029using std::string;
Yao Chen9c1debe2018-02-19 14:39:19 -080030using std::vector;
Joe Onoratoc4dfae52017-10-17 23:38:21 -070031
Yao Chen80235402017-11-13 20:42:25 -080032LogEvent::LogEvent(log_msg& msg) {
Chenjie Yu3ca36832018-01-22 15:10:54 -080033 mContext =
Yao Chen80235402017-11-13 20:42:25 -080034 create_android_log_parser(msg.msg() + sizeof(uint32_t), msg.len() - sizeof(uint32_t));
Yangster-mac330af582018-02-08 15:24:38 -080035 mLogdTimestampNs = msg.entry_v1.sec * NS_PER_SEC + msg.entry_v1.nsec;
Yao Chend10f7b12017-12-18 12:53:50 -080036 mLogUid = msg.entry_v4.uid;
Chenjie Yu3ca36832018-01-22 15:10:54 -080037 init(mContext);
38 if (mContext) {
Yao Chen48d75182018-01-23 09:40:48 -080039 // android_log_destroy will set mContext to NULL
Chenjie Yu3ca36832018-01-22 15:10:54 -080040 android_log_destroy(&mContext);
Yangster-mac20877162017-12-22 17:19:39 -080041 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -070042}
43
Yangster-mac330af582018-02-08 15:24:38 -080044LogEvent::LogEvent(int32_t tagId, int64_t wallClockTimestampNs, int64_t elapsedTimestampNs) {
45 mLogdTimestampNs = wallClockTimestampNs;
Yao Chen80235402017-11-13 20:42:25 -080046 mTagId = tagId;
Yangster-mac20877162017-12-22 17:19:39 -080047 mLogUid = 0;
Yao Chen80235402017-11-13 20:42:25 -080048 mContext = create_android_logger(1937006964); // the event tag shared by all stats logs
49 if (mContext) {
Yangster-mac330af582018-02-08 15:24:38 -080050 android_log_write_int64(mContext, elapsedTimestampNs);
51 android_log_write_int32(mContext, tagId);
52 }
53}
54
Yangster-mac48b3d622018-08-18 12:38:11 -070055LogEvent::LogEvent(int32_t tagId, int64_t wallClockTimestampNs, int64_t elapsedTimestampNs,
56 int32_t uid,
57 const std::map<int32_t, int64_t>& int_map,
58 const std::map<int32_t, std::string>& string_map,
59 const std::map<int32_t, float>& float_map) {
60 mLogdTimestampNs = wallClockTimestampNs;
61 mElapsedTimestampNs = elapsedTimestampNs;
62 mTagId = android::util::KEY_VALUE_PAIRS_ATOM;
63 mLogUid = uid;
64
65 int pos[] = {1, 1, 1};
66
67 mValues.push_back(FieldValue(Field(mTagId, pos, 0 /* depth */), Value(uid)));
68 pos[0]++;
69 for (const auto&itr : int_map) {
70 pos[2] = 1;
71 mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.first)));
72 pos[2] = 2;
73 mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.second)));
74 mValues.back().mField.decorateLastPos(2);
75 pos[1]++;
76 }
77
78 for (const auto&itr : string_map) {
79 pos[2] = 1;
80 mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.first)));
81 pos[2] = 3;
82 mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.second)));
83 mValues.back().mField.decorateLastPos(2);
84 pos[1]++;
85 }
86
87 for (const auto&itr : float_map) {
88 pos[2] = 1;
89 mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.first)));
90 pos[2] = 4;
91 mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.second)));
92 mValues.back().mField.decorateLastPos(2);
93 pos[1]++;
94 }
95 if (!mValues.empty()) {
96 mValues.back().mField.decorateLastPos(1);
97 mValues.at(mValues.size() - 2).mField.decorateLastPos(1);
98 }
99}
100
Yangster-mac330af582018-02-08 15:24:38 -0800101LogEvent::LogEvent(int32_t tagId, int64_t timestampNs) {
102 mLogdTimestampNs = timestampNs;
103 mTagId = tagId;
104 mLogUid = 0;
105 mContext = create_android_logger(1937006964); // the event tag shared by all stats logs
106 if (mContext) {
107 android_log_write_int64(mContext, timestampNs);
Yao Chen80235402017-11-13 20:42:25 -0800108 android_log_write_int32(mContext, tagId);
109 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700110}
111
David Chen1481fe12017-10-16 13:16:34 -0700112void LogEvent::init() {
Yao Chen80235402017-11-13 20:42:25 -0800113 if (mContext) {
114 const char* buffer;
115 size_t len = android_log_write_list_buffer(mContext, &buffer);
116 // turns to reader mode
Chenjie Yuc1fe6f42018-02-01 23:14:18 -0800117 android_log_context contextForRead = create_android_log_parser(buffer, len);
118 if (contextForRead) {
119 init(contextForRead);
120 // destroy the context to save memory.
Yao Chen48d75182018-01-23 09:40:48 -0800121 // android_log_destroy will set mContext to NULL
Chenjie Yuc1fe6f42018-02-01 23:14:18 -0800122 android_log_destroy(&contextForRead);
Yao Chen48d75182018-01-23 09:40:48 -0800123 }
Chenjie Yuc1fe6f42018-02-01 23:14:18 -0800124 android_log_destroy(&mContext);
Yangster-mac20877162017-12-22 17:19:39 -0800125 }
126}
127
128LogEvent::~LogEvent() {
129 if (mContext) {
Yao Chen48d75182018-01-23 09:40:48 -0800130 // This is for the case when LogEvent is created using the test interface
131 // but init() isn't called.
Yangster-mac20877162017-12-22 17:19:39 -0800132 android_log_destroy(&mContext);
Yao Chen80235402017-11-13 20:42:25 -0800133 }
134}
135
136bool LogEvent::write(int32_t value) {
137 if (mContext) {
138 return android_log_write_int32(mContext, value) >= 0;
139 }
140 return false;
141}
142
143bool LogEvent::write(uint32_t value) {
144 if (mContext) {
145 return android_log_write_int32(mContext, value) >= 0;
146 }
147 return false;
148}
149
Chenjie Yud9dfda72017-12-11 17:41:20 -0800150bool LogEvent::write(int64_t value) {
151 if (mContext) {
152 return android_log_write_int64(mContext, value) >= 0;
153 }
154 return false;
155}
156
Yao Chen80235402017-11-13 20:42:25 -0800157bool LogEvent::write(uint64_t value) {
158 if (mContext) {
159 return android_log_write_int64(mContext, value) >= 0;
160 }
161 return false;
162}
163
164bool LogEvent::write(const string& value) {
165 if (mContext) {
166 return android_log_write_string8_len(mContext, value.c_str(), value.length()) >= 0;
167 }
168 return false;
169}
170
171bool LogEvent::write(float value) {
172 if (mContext) {
173 return android_log_write_float32(mContext, value) >= 0;
174 }
175 return false;
176}
177
Yao Chen9c1debe2018-02-19 14:39:19 -0800178bool LogEvent::write(const std::vector<AttributionNodeInternal>& nodes) {
Yao Chen80235402017-11-13 20:42:25 -0800179 if (mContext) {
Yangster-mac20877162017-12-22 17:19:39 -0800180 if (android_log_write_list_begin(mContext) < 0) {
181 return false;
182 }
183 for (size_t i = 0; i < nodes.size(); ++i) {
184 if (!write(nodes[i])) {
185 return false;
186 }
187 }
188 if (android_log_write_list_end(mContext) < 0) {
189 return false;
190 }
191 return true;
192 }
193 return false;
194}
195
Yao Chen9c1debe2018-02-19 14:39:19 -0800196bool LogEvent::write(const AttributionNodeInternal& node) {
Yangster-mac20877162017-12-22 17:19:39 -0800197 if (mContext) {
198 if (android_log_write_list_begin(mContext) < 0) {
199 return false;
200 }
201 if (android_log_write_int32(mContext, node.uid()) < 0) {
202 return false;
203 }
204 if (android_log_write_string8(mContext, node.tag().c_str()) < 0) {
205 return false;
206 }
Yangster-mac20877162017-12-22 17:19:39 -0800207 if (android_log_write_list_end(mContext) < 0) {
208 return false;
209 }
210 return true;
211 }
212 return false;
213}
214
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700215/**
216 * The elements of each log event are stored as a vector of android_log_list_elements.
217 * The goal is to do as little preprocessing as possible, because we read a tiny fraction
218 * of the elements that are written to the log.
Yao Chen8a8d16c2018-02-08 14:50:40 -0800219 *
220 * The idea here is to read through the log items once, we get as much information we need for
221 * matching as possible. Because this log will be matched against lots of matchers.
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700222 */
Yao Chen80235402017-11-13 20:42:25 -0800223void LogEvent::init(android_log_context context) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700224 android_log_list_element elem;
Yao Chen80235402017-11-13 20:42:25 -0800225 int i = 0;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800226 int depth = -1;
227 int pos[] = {1, 1, 1};
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700228 do {
Yao Chen80235402017-11-13 20:42:25 -0800229 elem = android_log_read_next(context);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700230 switch ((int)elem.type) {
231 case EVENT_TYPE_INT:
Yangster-mac330af582018-02-08 15:24:38 -0800232 // elem at [0] is EVENT_TYPE_LIST, [1] is the timestamp, [2] is tag id.
233 if (i == 2) {
Yao Chen80235402017-11-13 20:42:25 -0800234 mTagId = elem.data.int32;
Yangster-mac20877162017-12-22 17:19:39 -0800235 } else {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800236 if (depth < 0 || depth > 2) {
Yangster-mac20877162017-12-22 17:19:39 -0800237 return;
238 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800239
240 mValues.push_back(
241 FieldValue(Field(mTagId, pos, depth), Value((int32_t)elem.data.int32)));
242
243 pos[depth]++;
Yangster-mac20877162017-12-22 17:19:39 -0800244 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700245 break;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800246 case EVENT_TYPE_FLOAT: {
247 if (depth < 0 || depth > 2) {
248 ALOGE("Depth > 2. Not supported!");
249 return;
250 }
251
252 mValues.push_back(FieldValue(Field(mTagId, pos, depth), Value(elem.data.float32)));
253
254 pos[depth]++;
255
256 } break;
257 case EVENT_TYPE_STRING: {
258 if (depth < 0 || depth > 2) {
259 ALOGE("Depth > 2. Not supported!");
260 return;
261 }
262
263 mValues.push_back(FieldValue(Field(mTagId, pos, depth),
264 Value(string(elem.data.string, elem.len))));
265
266 pos[depth]++;
267
268 } break;
269 case EVENT_TYPE_LONG: {
Yangster-mac330af582018-02-08 15:24:38 -0800270 if (i == 1) {
271 mElapsedTimestampNs = elem.data.int64;
272 } else {
273 if (depth < 0 || depth > 2) {
274 ALOGE("Depth > 2. Not supported!");
275 return;
276 }
277 mValues.push_back(
278 FieldValue(Field(mTagId, pos, depth), Value((int64_t)elem.data.int64)));
279
280 pos[depth]++;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800281 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800282 } break;
283 case EVENT_TYPE_LIST:
284 depth++;
285 if (depth > 2) {
286 ALOGE("Depth > 2. Not supported!");
287 return;
288 }
289 pos[depth] = 1;
290
291 break;
292 case EVENT_TYPE_LIST_STOP: {
293 int prevDepth = depth;
294 depth--;
295 if (depth >= 0 && depth < 2) {
296 // Now go back to decorate the previous items that are last at prevDepth.
297 // So that we can later easily match them with Position=Last matchers.
298 pos[prevDepth]--;
299 int path = getEncodedField(pos, prevDepth, false);
Yao Chendb43afc2018-02-13 09:37:27 -0800300 for (auto it = mValues.rbegin(); it != mValues.rend(); ++it) {
301 if (it->mField.getDepth() >= prevDepth &&
302 it->mField.getPath(prevDepth) == path) {
303 it->mField.decorateLastPos(prevDepth);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800304 } else {
305 // Safe to break, because the items are in DFS order.
306 break;
307 }
Yangster-mac20877162017-12-22 17:19:39 -0800308 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800309 pos[depth]++;
Yangster-mac20877162017-12-22 17:19:39 -0800310 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700311 break;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800312 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700313 case EVENT_TYPE_UNKNOWN:
314 break;
315 default:
316 break;
317 }
Yao Chen80235402017-11-13 20:42:25 -0800318 i++;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700319 } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete);
320}
321
322int64_t LogEvent::GetLong(size_t key, status_t* err) const {
Yao Chen5bfffb52018-06-21 16:58:51 -0700323 // TODO(b/110561208): encapsulate the magical operations in Field struct as static functions
Yao Chen8a8d16c2018-02-08 14:50:40 -0800324 int field = getSimpleField(key);
325 for (const auto& value : mValues) {
326 if (value.mField.getField() == field) {
Yao Chenab92a1f2018-02-13 15:17:55 -0800327 if (value.mValue.getType() == LONG) {
328 return value.mValue.long_value;
329 } else if (value.mValue.getType() == INT) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800330 return value.mValue.int_value;
331 } else {
332 *err = BAD_TYPE;
333 return 0;
334 }
335 }
336 if ((size_t)value.mField.getPosAtDepth(0) > key) {
337 break;
Yangster-mac20877162017-12-22 17:19:39 -0800338 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700339 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800340
341 *err = BAD_INDEX;
342 return 0;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700343}
344
Chenjie Yu80f91122018-01-31 20:24:50 -0800345int LogEvent::GetInt(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800346 int field = getSimpleField(key);
347 for (const auto& value : mValues) {
348 if (value.mField.getField() == field) {
349 if (value.mValue.getType() == INT) {
350 return value.mValue.int_value;
351 } else {
352 *err = BAD_TYPE;
353 return 0;
354 }
355 }
356 if ((size_t)value.mField.getPosAtDepth(0) > key) {
357 break;
358 }
359 }
360
Chenjie Yu80f91122018-01-31 20:24:50 -0800361 *err = BAD_INDEX;
362 return 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800363}
364
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700365const char* LogEvent::GetString(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800366 int field = getSimpleField(key);
367 for (const auto& value : mValues) {
368 if (value.mField.getField() == field) {
369 if (value.mValue.getType() == STRING) {
370 return value.mValue.str_value.c_str();
371 } else {
372 *err = BAD_TYPE;
373 return 0;
374 }
375 }
376 if ((size_t)value.mField.getPosAtDepth(0) > key) {
377 break;
Yangster-mac20877162017-12-22 17:19:39 -0800378 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700379 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800380
381 *err = BAD_INDEX;
382 return NULL;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700383}
384
385bool LogEvent::GetBool(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800386 int field = getSimpleField(key);
387 for (const auto& value : mValues) {
388 if (value.mField.getField() == field) {
389 if (value.mValue.getType() == INT) {
390 return value.mValue.int_value != 0;
391 } else if (value.mValue.getType() == LONG) {
392 return value.mValue.long_value != 0;
393 } else {
394 *err = BAD_TYPE;
395 return false;
396 }
397 }
398 if ((size_t)value.mField.getPosAtDepth(0) > key) {
399 break;
Yangster-mac20877162017-12-22 17:19:39 -0800400 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700401 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800402
403 *err = BAD_INDEX;
404 return false;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700405}
406
407float LogEvent::GetFloat(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800408 int field = getSimpleField(key);
409 for (const auto& value : mValues) {
410 if (value.mField.getField() == field) {
411 if (value.mValue.getType() == FLOAT) {
412 return value.mValue.float_value;
413 } else {
414 *err = BAD_TYPE;
415 return 0.0;
416 }
417 }
418 if ((size_t)value.mField.getPosAtDepth(0) > key) {
419 break;
Yangster-mac20877162017-12-22 17:19:39 -0800420 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700421 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700422
Yao Chen8a8d16c2018-02-08 14:50:40 -0800423 *err = BAD_INDEX;
424 return 0.0;
Yangster-macd40053e2018-01-09 16:29:22 -0800425}
426
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700427string LogEvent::ToString() const {
Yao Chen20e9e622018-02-28 11:18:51 -0800428 string result;
429 result += StringPrintf("{ %lld %lld (%d)", (long long)mLogdTimestampNs,
430 (long long)mElapsedTimestampNs, mTagId);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800431 for (const auto& value : mValues) {
Yao Chen20e9e622018-02-28 11:18:51 -0800432 result +=
433 StringPrintf("%#x", value.mField.getField()) + "->" + value.mValue.toString() + " ";
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700434 }
Yao Chen20e9e622018-02-28 11:18:51 -0800435 result += " }";
436 return result;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700437}
438
Yangster-mac20877162017-12-22 17:19:39 -0800439void LogEvent::ToProto(ProtoOutputStream& protoOutput) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800440 writeFieldValueTreeToStream(mTagId, getValues(), &protoOutput);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700441}
442
443} // namespace statsd
444} // namespace os
445} // namespace android