blob: 1ca793c818783d638e5341078cd5a5d6fe9eb307 [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
Chenjie Yub3dda412017-10-24 13:41:59 -070017#define DEBUG true // 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 "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
21
22#include <set>
Joe Onoratoc4dfae52017-10-17 23:38:21 -070023#include <sstream>
Yangster-mac20877162017-12-22 17:19:39 -080024
25#include "field_util.h"
26#include "dimension.h"
27#include "stats_log_util.h"
Joe Onoratoc4dfae52017-10-17 23:38:21 -070028
29namespace android {
30namespace os {
31namespace statsd {
32
yro24809bd2017-10-31 23:06:53 -070033using namespace android::util;
Joe Onoratoc4dfae52017-10-17 23:38:21 -070034using std::ostringstream;
David Chen1481fe12017-10-16 13:16:34 -070035using std::string;
Yao Chen5110bed2017-10-23 12:50:02 -070036using android::util::ProtoOutputStream;
Joe Onoratoc4dfae52017-10-17 23:38:21 -070037
Yao Chen80235402017-11-13 20:42:25 -080038LogEvent::LogEvent(log_msg& msg) {
Chenjie Yu3ca36832018-01-22 15:10:54 -080039 mContext =
Yao Chen80235402017-11-13 20:42:25 -080040 create_android_log_parser(msg.msg() + sizeof(uint32_t), msg.len() - sizeof(uint32_t));
41 mTimestampNs = msg.entry_v1.sec * NS_PER_SEC + msg.entry_v1.nsec;
Yao Chend10f7b12017-12-18 12:53:50 -080042 mLogUid = msg.entry_v4.uid;
Chenjie Yu3ca36832018-01-22 15:10:54 -080043 init(mContext);
44 if (mContext) {
Yao Chen48d75182018-01-23 09:40:48 -080045 // android_log_destroy will set mContext to NULL
Chenjie Yu3ca36832018-01-22 15:10:54 -080046 android_log_destroy(&mContext);
Yangster-mac20877162017-12-22 17:19:39 -080047 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -070048}
49
Yao Chen80235402017-11-13 20:42:25 -080050LogEvent::LogEvent(int32_t tagId, uint64_t timestampNs) {
51 mTimestampNs = timestampNs;
52 mTagId = tagId;
Yangster-mac20877162017-12-22 17:19:39 -080053 mLogUid = 0;
Yao Chen80235402017-11-13 20:42:25 -080054 mContext = create_android_logger(1937006964); // the event tag shared by all stats logs
55 if (mContext) {
56 android_log_write_int32(mContext, tagId);
57 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -070058}
59
David Chen1481fe12017-10-16 13:16:34 -070060void LogEvent::init() {
Yao Chen80235402017-11-13 20:42:25 -080061 if (mContext) {
62 const char* buffer;
63 size_t len = android_log_write_list_buffer(mContext, &buffer);
64 // turns to reader mode
65 mContext = create_android_log_parser(buffer, len);
66 init(mContext);
Yangster-mac20877162017-12-22 17:19:39 -080067 // destroy the context to save memory.
Yao Chen48d75182018-01-23 09:40:48 -080068 if (mContext) {
69 // android_log_destroy will set mContext to NULL
70 android_log_destroy(&mContext);
71 }
Yangster-mac20877162017-12-22 17:19:39 -080072 }
73}
74
75LogEvent::~LogEvent() {
76 if (mContext) {
Yao Chen48d75182018-01-23 09:40:48 -080077 // This is for the case when LogEvent is created using the test interface
78 // but init() isn't called.
Yangster-mac20877162017-12-22 17:19:39 -080079 android_log_destroy(&mContext);
Yao Chen80235402017-11-13 20:42:25 -080080 }
81}
82
83bool LogEvent::write(int32_t value) {
84 if (mContext) {
85 return android_log_write_int32(mContext, value) >= 0;
86 }
87 return false;
88}
89
90bool LogEvent::write(uint32_t value) {
91 if (mContext) {
92 return android_log_write_int32(mContext, value) >= 0;
93 }
94 return false;
95}
96
Chenjie Yud9dfda72017-12-11 17:41:20 -080097bool LogEvent::write(int64_t value) {
98 if (mContext) {
99 return android_log_write_int64(mContext, value) >= 0;
100 }
101 return false;
102}
103
Yao Chen80235402017-11-13 20:42:25 -0800104bool LogEvent::write(uint64_t value) {
105 if (mContext) {
106 return android_log_write_int64(mContext, value) >= 0;
107 }
108 return false;
109}
110
111bool LogEvent::write(const string& value) {
112 if (mContext) {
113 return android_log_write_string8_len(mContext, value.c_str(), value.length()) >= 0;
114 }
115 return false;
116}
117
118bool LogEvent::write(float value) {
119 if (mContext) {
120 return android_log_write_float32(mContext, value) >= 0;
121 }
122 return false;
123}
124
Yangster-mac20877162017-12-22 17:19:39 -0800125bool LogEvent::write(const std::vector<AttributionNode>& nodes) {
Yao Chen80235402017-11-13 20:42:25 -0800126 if (mContext) {
Yangster-mac20877162017-12-22 17:19:39 -0800127 if (android_log_write_list_begin(mContext) < 0) {
128 return false;
129 }
130 for (size_t i = 0; i < nodes.size(); ++i) {
131 if (!write(nodes[i])) {
132 return false;
133 }
134 }
135 if (android_log_write_list_end(mContext) < 0) {
136 return false;
137 }
138 return true;
139 }
140 return false;
141}
142
143bool LogEvent::write(const AttributionNode& node) {
144 if (mContext) {
145 if (android_log_write_list_begin(mContext) < 0) {
146 return false;
147 }
148 if (android_log_write_int32(mContext, node.uid()) < 0) {
149 return false;
150 }
151 if (android_log_write_string8(mContext, node.tag().c_str()) < 0) {
152 return false;
153 }
154 if (android_log_write_int32(mContext, node.uid()) < 0) {
155 return false;
156 }
157 if (android_log_write_list_end(mContext) < 0) {
158 return false;
159 }
160 return true;
161 }
162 return false;
163}
164
165namespace {
166
167void increaseField(Field *field, bool is_child) {
168 if (is_child) {
169 if (field->child_size() <= 0) {
170 field->add_child();
171 }
172 } else {
173 field->clear_child();
174 }
175 Field* curr = is_child ? field->mutable_child(0) : field;
176 if (!curr->has_field()) {
177 curr->set_field(1);
178 } else {
179 curr->set_field(curr->field() + 1);
Yao Chen80235402017-11-13 20:42:25 -0800180 }
David Chen1481fe12017-10-16 13:16:34 -0700181}
182
Yangster-mac20877162017-12-22 17:19:39 -0800183} // namespace
184
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700185/**
186 * The elements of each log event are stored as a vector of android_log_list_elements.
187 * The goal is to do as little preprocessing as possible, because we read a tiny fraction
188 * of the elements that are written to the log.
189 */
Yao Chen80235402017-11-13 20:42:25 -0800190void LogEvent::init(android_log_context context) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700191 android_log_list_element elem;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700192 // TODO: The log is actually structured inside one list. This is convenient
193 // because we'll be able to use it to put the attribution (WorkSource) block first
194 // without doing our own tagging scheme. Until that change is in, just drop the
195 // list-related log elements and the order we get there is our index-keyed data
196 // structure.
Yao Chen80235402017-11-13 20:42:25 -0800197 int i = 0;
Yangster-mac20877162017-12-22 17:19:39 -0800198
199 int seenListStart = 0;
200
201 Field field;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700202 do {
Yao Chen80235402017-11-13 20:42:25 -0800203 elem = android_log_read_next(context);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700204 switch ((int)elem.type) {
205 case EVENT_TYPE_INT:
Yangster-mac20877162017-12-22 17:19:39 -0800206 // elem at [0] is EVENT_TYPE_LIST, [1] is the tag id.
Yao Chen80235402017-11-13 20:42:25 -0800207 if (i == 1) {
208 mTagId = elem.data.int32;
Yangster-mac20877162017-12-22 17:19:39 -0800209 } else {
210 increaseField(&field, seenListStart > 0/* is_child */);
211 DimensionsValue dimensionsValue;
212 dimensionsValue.set_value_int(elem.data.int32);
213 setFieldInLeafValueProto(field, &dimensionsValue);
214 mFieldValueMap.insert(
215 std::make_pair(buildAtomField(mTagId, field), dimensionsValue));
Yao Chen80235402017-11-13 20:42:25 -0800216 }
Yangster-mac20877162017-12-22 17:19:39 -0800217 break;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700218 case EVENT_TYPE_FLOAT:
Yangster-mac20877162017-12-22 17:19:39 -0800219 {
220 increaseField(&field, seenListStart > 0/* is_child */);
221 DimensionsValue dimensionsValue;
222 dimensionsValue.set_value_float(elem.data.float32);
223 setFieldInLeafValueProto(field, &dimensionsValue);
224 mFieldValueMap.insert(
225 std::make_pair(buildAtomField(mTagId, field), dimensionsValue));
226 }
227 break;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700228 case EVENT_TYPE_STRING:
Yangster-mac20877162017-12-22 17:19:39 -0800229 {
230 increaseField(&field, seenListStart > 0/* is_child */);
231 DimensionsValue dimensionsValue;
232 dimensionsValue.set_value_str(string(elem.data.string, elem.len).c_str());
233 setFieldInLeafValueProto(field, &dimensionsValue);
234 mFieldValueMap.insert(
235 std::make_pair(buildAtomField(mTagId, field), dimensionsValue));
236 }
237 break;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700238 case EVENT_TYPE_LONG:
Yangster-mac20877162017-12-22 17:19:39 -0800239 {
240 increaseField(&field, seenListStart > 0 /* is_child */);
241 DimensionsValue dimensionsValue;
242 dimensionsValue.set_value_long(elem.data.int64);
243 setFieldInLeafValueProto(field, &dimensionsValue);
244 mFieldValueMap.insert(
245 std::make_pair(buildAtomField(mTagId, field), dimensionsValue));
246 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700247 break;
248 case EVENT_TYPE_LIST:
Yangster-mac20877162017-12-22 17:19:39 -0800249 if (i >= 1) {
250 if (seenListStart > 0) {
251 increasePosition(&field);
252 } else {
253 increaseField(&field, false /* is_child */);
254 }
255 seenListStart++;
256 if (seenListStart >= 3) {
257 ALOGE("Depth > 2. Not supported!");
258 return;
259 }
260 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700261 break;
262 case EVENT_TYPE_LIST_STOP:
Yangster-mac20877162017-12-22 17:19:39 -0800263 seenListStart--;
264 if (seenListStart == 0) {
265 field.clear_position_index();
266 } else {
267 if (field.child_size() > 0) {
268 field.mutable_child(0)->clear_field();
269 }
270 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700271 break;
272 case EVENT_TYPE_UNKNOWN:
273 break;
274 default:
275 break;
276 }
Yao Chen80235402017-11-13 20:42:25 -0800277 i++;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700278 } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete);
279}
280
281int64_t LogEvent::GetLong(size_t key, status_t* err) const {
Yangster-mac20877162017-12-22 17:19:39 -0800282 DimensionsValue value;
283 if (!GetSimpleAtomDimensionsValueProto(key, &value)) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700284 *err = BAD_INDEX;
285 return 0;
286 }
Yangster-mac20877162017-12-22 17:19:39 -0800287 const DimensionsValue* leafValue = getSingleLeafValue(&value);
288 switch (leafValue->value_case()) {
289 case DimensionsValue::ValueCase::kValueInt:
290 return (int64_t)leafValue->value_int();
291 case DimensionsValue::ValueCase::kValueLong:
292 return leafValue->value_long();
293 case DimensionsValue::ValueCase::kValueBool:
294 return leafValue->value_bool() ? 1 : 0;
295 case DimensionsValue::ValueCase::kValueFloat:
296 return (int64_t)leafValue->value_float();
297 case DimensionsValue::ValueCase::kValueTuple:
298 case DimensionsValue::ValueCase::kValueStr:
299 case DimensionsValue::ValueCase::VALUE_NOT_SET: {
300 *err = BAD_TYPE;
301 return 0;
302 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700303 }
304}
305
306const char* LogEvent::GetString(size_t key, status_t* err) const {
Yangster-mac20877162017-12-22 17:19:39 -0800307 DimensionsValue value;
308 if (!GetSimpleAtomDimensionsValueProto(key, &value)) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700309 *err = BAD_INDEX;
Yangster-mac20877162017-12-22 17:19:39 -0800310 return 0;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700311 }
Yangster-mac20877162017-12-22 17:19:39 -0800312 const DimensionsValue* leafValue = getSingleLeafValue(&value);
313 switch (leafValue->value_case()) {
314 case DimensionsValue::ValueCase::kValueStr:
315 return leafValue->value_str().c_str();
316 case DimensionsValue::ValueCase::kValueInt:
317 case DimensionsValue::ValueCase::kValueLong:
318 case DimensionsValue::ValueCase::kValueBool:
319 case DimensionsValue::ValueCase::kValueFloat:
320 case DimensionsValue::ValueCase::kValueTuple:
321 case DimensionsValue::ValueCase::VALUE_NOT_SET: {
322 *err = BAD_TYPE;
323 return 0;
324 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700325 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700326}
327
328bool LogEvent::GetBool(size_t key, status_t* err) const {
Yangster-mac20877162017-12-22 17:19:39 -0800329 DimensionsValue value;
330 if (!GetSimpleAtomDimensionsValueProto(key, &value)) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700331 *err = BAD_INDEX;
332 return 0;
333 }
Yangster-mac20877162017-12-22 17:19:39 -0800334 const DimensionsValue* leafValue = getSingleLeafValue(&value);
335 switch (leafValue->value_case()) {
336 case DimensionsValue::ValueCase::kValueInt:
337 return leafValue->value_int() != 0;
338 case DimensionsValue::ValueCase::kValueLong:
339 return leafValue->value_long() != 0;
340 case DimensionsValue::ValueCase::kValueBool:
341 return leafValue->value_bool();
342 case DimensionsValue::ValueCase::kValueFloat:
343 return leafValue->value_float() != 0;
344 case DimensionsValue::ValueCase::kValueTuple:
345 case DimensionsValue::ValueCase::kValueStr:
346 case DimensionsValue::ValueCase::VALUE_NOT_SET: {
347 *err = BAD_TYPE;
348 return 0;
349 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700350 }
351}
352
353float LogEvent::GetFloat(size_t key, status_t* err) const {
Yangster-mac20877162017-12-22 17:19:39 -0800354 DimensionsValue value;
355 if (!GetSimpleAtomDimensionsValueProto(key, &value)) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700356 *err = BAD_INDEX;
357 return 0;
358 }
Yangster-mac20877162017-12-22 17:19:39 -0800359 const DimensionsValue* leafValue = getSingleLeafValue(&value);
360 switch (leafValue->value_case()) {
361 case DimensionsValue::ValueCase::kValueInt:
362 return (float)leafValue->value_int();
363 case DimensionsValue::ValueCase::kValueLong:
364 return (float)leafValue->value_long();
365 case DimensionsValue::ValueCase::kValueBool:
366 return leafValue->value_bool() ? 1.0f : 0.0f;
367 case DimensionsValue::ValueCase::kValueFloat:
368 return leafValue->value_float();
369 case DimensionsValue::ValueCase::kValueTuple:
370 case DimensionsValue::ValueCase::kValueStr:
371 case DimensionsValue::ValueCase::VALUE_NOT_SET: {
372 *err = BAD_TYPE;
373 return 0;
374 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700375 }
376}
377
Yangster-mac20877162017-12-22 17:19:39 -0800378void LogEvent::GetAtomDimensionsValueProtos(const FieldMatcher& matcher,
379 std::vector<DimensionsValue> *dimensionsValues) const {
380 findDimensionsValues(mFieldValueMap, matcher, dimensionsValues);
381}
Yao Chen729093d2017-10-16 10:33:26 -0700382
Yangster-mac20877162017-12-22 17:19:39 -0800383bool LogEvent::GetAtomDimensionsValueProto(const FieldMatcher& matcher,
384 DimensionsValue* dimensionsValue) const {
385 std::vector<DimensionsValue> rootDimensionsValues;
386 findDimensionsValues(mFieldValueMap, matcher, &rootDimensionsValues);
387 if (rootDimensionsValues.size() != 1) {
388 return false;
Yao Chen729093d2017-10-16 10:33:26 -0700389 }
Yangster-mac20877162017-12-22 17:19:39 -0800390 *dimensionsValue = rootDimensionsValues.front();
391 return true;
392}
393
394bool LogEvent::GetSimpleAtomDimensionsValueProto(size_t atomField,
395 DimensionsValue* dimensionsValue) const {
396 return GetAtomDimensionsValueProto(
397 buildSimpleAtomFieldMatcher(mTagId, atomField), dimensionsValue);
398}
399
400DimensionsValue LogEvent::GetSimpleAtomDimensionsValueProto(size_t atomField) const {
401 DimensionsValue dimensionsValue;
402 GetSimpleAtomDimensionsValueProto(atomField, &dimensionsValue);
403 return dimensionsValue;
Yao Chen729093d2017-10-16 10:33:26 -0700404}
405
Yangster-macd40053e2018-01-09 16:29:22 -0800406DimensionsValue* LogEvent::findFieldValueOrNull(const Field& field) {
407 auto it = mFieldValueMap.find(field);
408 if (it == mFieldValueMap.end()) {
409 return nullptr;
410 }
411 return &it->second;
412}
413
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700414string LogEvent::ToString() const {
415 ostringstream result;
416 result << "{ " << mTimestampNs << " (" << mTagId << ")";
Yangster-mac20877162017-12-22 17:19:39 -0800417 for (const auto& itr : mFieldValueMap) {
418 result << FieldToString(itr.first);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700419 result << "->";
Yangster-mac20877162017-12-22 17:19:39 -0800420 result << DimensionsValueToString(itr.second);
421 result << " ";
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700422 }
423 result << " }";
424 return result.str();
425}
426
Yangster-mac20877162017-12-22 17:19:39 -0800427void LogEvent::ToProto(ProtoOutputStream& protoOutput) const {
428 writeFieldValueTreeToStream(getFieldValueMap(), &protoOutput);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700429}
430
431} // namespace statsd
432} // namespace os
433} // namespace android