blob: cf04ee3e17ecccb414dd89df631c2554f30513a5 [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
Yangster-mace124e422018-08-16 10:30:28 -0700178
179
180bool LogEvent::writeKeyValuePairs(const std::map<int32_t, int64_t>& int_map,
181 const std::map<int32_t, std::string>& string_map,
182 const std::map<int32_t, float>& float_map) {
183 if (mContext) {
184 if (android_log_write_list_begin(mContext) < 0) {
185 return false;
186 }
187 for (const auto& itr : int_map) {
188 if (android_log_write_list_begin(mContext) < 0) {
189 return false;
190 }
191 write(itr.first);
192 write(itr.second);
193 if (android_log_write_list_end(mContext) < 0) {
194 return false;
195 }
196 }
197
198 for (const auto& itr : string_map) {
199 if (android_log_write_list_begin(mContext) < 0) {
200 return false;
201 }
202 write(itr.first);
203 write(itr.second.c_str());
204 if (android_log_write_list_end(mContext) < 0) {
205 return false;
206 }
207 }
208
209 for (const auto& itr : float_map) {
210 if (android_log_write_list_begin(mContext) < 0) {
211 return false;
212 }
213 write(itr.first);
214 write(itr.second);
215 if (android_log_write_list_end(mContext) < 0) {
216 return false;
217 }
218 }
219
220 if (android_log_write_list_end(mContext) < 0) {
221 return false;
222 }
223 return true;
224 }
225 return false;
226}
227
Yao Chen9c1debe2018-02-19 14:39:19 -0800228bool LogEvent::write(const std::vector<AttributionNodeInternal>& nodes) {
Yao Chen80235402017-11-13 20:42:25 -0800229 if (mContext) {
Yangster-mac20877162017-12-22 17:19:39 -0800230 if (android_log_write_list_begin(mContext) < 0) {
231 return false;
232 }
233 for (size_t i = 0; i < nodes.size(); ++i) {
234 if (!write(nodes[i])) {
235 return false;
236 }
237 }
238 if (android_log_write_list_end(mContext) < 0) {
239 return false;
240 }
241 return true;
242 }
243 return false;
244}
245
Yao Chen9c1debe2018-02-19 14:39:19 -0800246bool LogEvent::write(const AttributionNodeInternal& node) {
Yangster-mac20877162017-12-22 17:19:39 -0800247 if (mContext) {
248 if (android_log_write_list_begin(mContext) < 0) {
249 return false;
250 }
251 if (android_log_write_int32(mContext, node.uid()) < 0) {
252 return false;
253 }
254 if (android_log_write_string8(mContext, node.tag().c_str()) < 0) {
255 return false;
256 }
Yangster-mac20877162017-12-22 17:19:39 -0800257 if (android_log_write_list_end(mContext) < 0) {
258 return false;
259 }
260 return true;
261 }
262 return false;
263}
264
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700265/**
266 * The elements of each log event are stored as a vector of android_log_list_elements.
267 * The goal is to do as little preprocessing as possible, because we read a tiny fraction
268 * of the elements that are written to the log.
Yao Chen8a8d16c2018-02-08 14:50:40 -0800269 *
270 * The idea here is to read through the log items once, we get as much information we need for
271 * matching as possible. Because this log will be matched against lots of matchers.
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700272 */
Yao Chen80235402017-11-13 20:42:25 -0800273void LogEvent::init(android_log_context context) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700274 android_log_list_element elem;
Yao Chen80235402017-11-13 20:42:25 -0800275 int i = 0;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800276 int depth = -1;
277 int pos[] = {1, 1, 1};
Yangster-mace124e422018-08-16 10:30:28 -0700278 bool isKeyValuePairAtom = false;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700279 do {
Yao Chen80235402017-11-13 20:42:25 -0800280 elem = android_log_read_next(context);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700281 switch ((int)elem.type) {
282 case EVENT_TYPE_INT:
Yangster-mac330af582018-02-08 15:24:38 -0800283 // elem at [0] is EVENT_TYPE_LIST, [1] is the timestamp, [2] is tag id.
284 if (i == 2) {
Yao Chen80235402017-11-13 20:42:25 -0800285 mTagId = elem.data.int32;
Yangster-mace124e422018-08-16 10:30:28 -0700286 isKeyValuePairAtom = (mTagId == android::util::KEY_VALUE_PAIRS_ATOM);
Yangster-mac20877162017-12-22 17:19:39 -0800287 } else {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800288 if (depth < 0 || depth > 2) {
Yangster-mac20877162017-12-22 17:19:39 -0800289 return;
290 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800291
292 mValues.push_back(
293 FieldValue(Field(mTagId, pos, depth), Value((int32_t)elem.data.int32)));
294
295 pos[depth]++;
Yangster-mac20877162017-12-22 17:19:39 -0800296 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700297 break;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800298 case EVENT_TYPE_FLOAT: {
299 if (depth < 0 || depth > 2) {
300 ALOGE("Depth > 2. Not supported!");
301 return;
302 }
303
Yangster-mace124e422018-08-16 10:30:28 -0700304 // Handles the oneof field in KeyValuePair atom.
305 if (isKeyValuePairAtom && depth == 2) {
306 pos[depth] = 4;
307 }
308
Yao Chen8a8d16c2018-02-08 14:50:40 -0800309 mValues.push_back(FieldValue(Field(mTagId, pos, depth), Value(elem.data.float32)));
310
311 pos[depth]++;
312
313 } break;
314 case EVENT_TYPE_STRING: {
315 if (depth < 0 || depth > 2) {
316 ALOGE("Depth > 2. Not supported!");
317 return;
318 }
319
Yangster-mace124e422018-08-16 10:30:28 -0700320 // Handles the oneof field in KeyValuePair atom.
321 if (isKeyValuePairAtom && depth == 2) {
322 pos[depth] = 3;
323 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800324 mValues.push_back(FieldValue(Field(mTagId, pos, depth),
325 Value(string(elem.data.string, elem.len))));
326
327 pos[depth]++;
328
329 } break;
330 case EVENT_TYPE_LONG: {
Yangster-mac330af582018-02-08 15:24:38 -0800331 if (i == 1) {
332 mElapsedTimestampNs = elem.data.int64;
333 } else {
334 if (depth < 0 || depth > 2) {
335 ALOGE("Depth > 2. Not supported!");
336 return;
337 }
Yangster-mace124e422018-08-16 10:30:28 -0700338 // Handles the oneof field in KeyValuePair atom.
339 if (isKeyValuePairAtom && depth == 2) {
340 pos[depth] = 2;
341 }
Yangster-mac330af582018-02-08 15:24:38 -0800342 mValues.push_back(
343 FieldValue(Field(mTagId, pos, depth), Value((int64_t)elem.data.int64)));
344
345 pos[depth]++;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800346 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800347 } break;
348 case EVENT_TYPE_LIST:
349 depth++;
350 if (depth > 2) {
351 ALOGE("Depth > 2. Not supported!");
352 return;
353 }
354 pos[depth] = 1;
355
356 break;
357 case EVENT_TYPE_LIST_STOP: {
358 int prevDepth = depth;
359 depth--;
360 if (depth >= 0 && depth < 2) {
361 // Now go back to decorate the previous items that are last at prevDepth.
362 // So that we can later easily match them with Position=Last matchers.
363 pos[prevDepth]--;
364 int path = getEncodedField(pos, prevDepth, false);
Yao Chendb43afc2018-02-13 09:37:27 -0800365 for (auto it = mValues.rbegin(); it != mValues.rend(); ++it) {
366 if (it->mField.getDepth() >= prevDepth &&
367 it->mField.getPath(prevDepth) == path) {
368 it->mField.decorateLastPos(prevDepth);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800369 } else {
370 // Safe to break, because the items are in DFS order.
371 break;
372 }
Yangster-mac20877162017-12-22 17:19:39 -0800373 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800374 pos[depth]++;
Yangster-mac20877162017-12-22 17:19:39 -0800375 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700376 break;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800377 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700378 case EVENT_TYPE_UNKNOWN:
379 break;
380 default:
381 break;
382 }
Yao Chen80235402017-11-13 20:42:25 -0800383 i++;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700384 } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete);
385}
386
387int64_t LogEvent::GetLong(size_t key, status_t* err) const {
Yao Chen5bfffb52018-06-21 16:58:51 -0700388 // TODO(b/110561208): encapsulate the magical operations in Field struct as static functions
Yao Chen8a8d16c2018-02-08 14:50:40 -0800389 int field = getSimpleField(key);
390 for (const auto& value : mValues) {
391 if (value.mField.getField() == field) {
Yao Chenab92a1f2018-02-13 15:17:55 -0800392 if (value.mValue.getType() == LONG) {
393 return value.mValue.long_value;
394 } else if (value.mValue.getType() == INT) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800395 return value.mValue.int_value;
396 } else {
397 *err = BAD_TYPE;
398 return 0;
399 }
400 }
401 if ((size_t)value.mField.getPosAtDepth(0) > key) {
402 break;
Yangster-mac20877162017-12-22 17:19:39 -0800403 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700404 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800405
406 *err = BAD_INDEX;
407 return 0;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700408}
409
Chenjie Yu80f91122018-01-31 20:24:50 -0800410int LogEvent::GetInt(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800411 int field = getSimpleField(key);
412 for (const auto& value : mValues) {
413 if (value.mField.getField() == field) {
414 if (value.mValue.getType() == INT) {
415 return value.mValue.int_value;
416 } else {
417 *err = BAD_TYPE;
418 return 0;
419 }
420 }
421 if ((size_t)value.mField.getPosAtDepth(0) > key) {
422 break;
423 }
424 }
425
Chenjie Yu80f91122018-01-31 20:24:50 -0800426 *err = BAD_INDEX;
427 return 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800428}
429
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700430const char* LogEvent::GetString(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800431 int field = getSimpleField(key);
432 for (const auto& value : mValues) {
433 if (value.mField.getField() == field) {
434 if (value.mValue.getType() == STRING) {
435 return value.mValue.str_value.c_str();
436 } else {
437 *err = BAD_TYPE;
438 return 0;
439 }
440 }
441 if ((size_t)value.mField.getPosAtDepth(0) > key) {
442 break;
Yangster-mac20877162017-12-22 17:19:39 -0800443 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700444 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800445
446 *err = BAD_INDEX;
447 return NULL;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700448}
449
450bool LogEvent::GetBool(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800451 int field = getSimpleField(key);
452 for (const auto& value : mValues) {
453 if (value.mField.getField() == field) {
454 if (value.mValue.getType() == INT) {
455 return value.mValue.int_value != 0;
456 } else if (value.mValue.getType() == LONG) {
457 return value.mValue.long_value != 0;
458 } else {
459 *err = BAD_TYPE;
460 return false;
461 }
462 }
463 if ((size_t)value.mField.getPosAtDepth(0) > key) {
464 break;
Yangster-mac20877162017-12-22 17:19:39 -0800465 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700466 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800467
468 *err = BAD_INDEX;
469 return false;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700470}
471
472float LogEvent::GetFloat(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800473 int field = getSimpleField(key);
474 for (const auto& value : mValues) {
475 if (value.mField.getField() == field) {
476 if (value.mValue.getType() == FLOAT) {
477 return value.mValue.float_value;
478 } else {
479 *err = BAD_TYPE;
480 return 0.0;
481 }
482 }
483 if ((size_t)value.mField.getPosAtDepth(0) > key) {
484 break;
Yangster-mac20877162017-12-22 17:19:39 -0800485 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700486 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700487
Yao Chen8a8d16c2018-02-08 14:50:40 -0800488 *err = BAD_INDEX;
489 return 0.0;
Yangster-macd40053e2018-01-09 16:29:22 -0800490}
491
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700492string LogEvent::ToString() const {
Yao Chen20e9e622018-02-28 11:18:51 -0800493 string result;
494 result += StringPrintf("{ %lld %lld (%d)", (long long)mLogdTimestampNs,
495 (long long)mElapsedTimestampNs, mTagId);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800496 for (const auto& value : mValues) {
Yao Chen20e9e622018-02-28 11:18:51 -0800497 result +=
498 StringPrintf("%#x", value.mField.getField()) + "->" + value.mValue.toString() + " ";
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700499 }
Yao Chen20e9e622018-02-28 11:18:51 -0800500 result += " }";
501 return result;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700502}
503
Yangster-mac20877162017-12-22 17:19:39 -0800504void LogEvent::ToProto(ProtoOutputStream& protoOutput) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800505 writeFieldValueTreeToStream(mTagId, getValues(), &protoOutput);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700506}
507
508} // namespace statsd
509} // namespace os
510} // namespace android