blob: 1dcd8534db79fb76170cdb89e0f2b2140811b65e [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
Chenjie Yuc1fe6f42018-02-01 23:14:18 -080065 android_log_context contextForRead = create_android_log_parser(buffer, len);
66 if (contextForRead) {
67 init(contextForRead);
68 // destroy the context to save memory.
Yao Chen48d75182018-01-23 09:40:48 -080069 // android_log_destroy will set mContext to NULL
Chenjie Yuc1fe6f42018-02-01 23:14:18 -080070 android_log_destroy(&contextForRead);
Yao Chen48d75182018-01-23 09:40:48 -080071 }
Chenjie Yuc1fe6f42018-02-01 23:14:18 -080072 android_log_destroy(&mContext);
Yangster-mac20877162017-12-22 17:19:39 -080073 }
74}
75
76LogEvent::~LogEvent() {
77 if (mContext) {
Yao Chen48d75182018-01-23 09:40:48 -080078 // This is for the case when LogEvent is created using the test interface
79 // but init() isn't called.
Yangster-mac20877162017-12-22 17:19:39 -080080 android_log_destroy(&mContext);
Yao Chen80235402017-11-13 20:42:25 -080081 }
82}
83
84bool LogEvent::write(int32_t value) {
85 if (mContext) {
86 return android_log_write_int32(mContext, value) >= 0;
87 }
88 return false;
89}
90
91bool LogEvent::write(uint32_t value) {
92 if (mContext) {
93 return android_log_write_int32(mContext, value) >= 0;
94 }
95 return false;
96}
97
Chenjie Yud9dfda72017-12-11 17:41:20 -080098bool LogEvent::write(int64_t value) {
99 if (mContext) {
100 return android_log_write_int64(mContext, value) >= 0;
101 }
102 return false;
103}
104
Yao Chen80235402017-11-13 20:42:25 -0800105bool LogEvent::write(uint64_t value) {
106 if (mContext) {
107 return android_log_write_int64(mContext, value) >= 0;
108 }
109 return false;
110}
111
112bool LogEvent::write(const string& value) {
113 if (mContext) {
114 return android_log_write_string8_len(mContext, value.c_str(), value.length()) >= 0;
115 }
116 return false;
117}
118
119bool LogEvent::write(float value) {
120 if (mContext) {
121 return android_log_write_float32(mContext, value) >= 0;
122 }
123 return false;
124}
125
Yangster-mac20877162017-12-22 17:19:39 -0800126bool LogEvent::write(const std::vector<AttributionNode>& nodes) {
Yao Chen80235402017-11-13 20:42:25 -0800127 if (mContext) {
Yangster-mac20877162017-12-22 17:19:39 -0800128 if (android_log_write_list_begin(mContext) < 0) {
129 return false;
130 }
131 for (size_t i = 0; i < nodes.size(); ++i) {
132 if (!write(nodes[i])) {
133 return false;
134 }
135 }
136 if (android_log_write_list_end(mContext) < 0) {
137 return false;
138 }
139 return true;
140 }
141 return false;
142}
143
144bool LogEvent::write(const AttributionNode& node) {
145 if (mContext) {
146 if (android_log_write_list_begin(mContext) < 0) {
147 return false;
148 }
149 if (android_log_write_int32(mContext, node.uid()) < 0) {
150 return false;
151 }
152 if (android_log_write_string8(mContext, node.tag().c_str()) < 0) {
153 return false;
154 }
155 if (android_log_write_int32(mContext, node.uid()) < 0) {
156 return false;
157 }
158 if (android_log_write_list_end(mContext) < 0) {
159 return false;
160 }
161 return true;
162 }
163 return false;
164}
165
166namespace {
167
168void increaseField(Field *field, bool is_child) {
169 if (is_child) {
170 if (field->child_size() <= 0) {
171 field->add_child();
172 }
173 } else {
174 field->clear_child();
175 }
176 Field* curr = is_child ? field->mutable_child(0) : field;
177 if (!curr->has_field()) {
178 curr->set_field(1);
179 } else {
180 curr->set_field(curr->field() + 1);
Yao Chen80235402017-11-13 20:42:25 -0800181 }
David Chen1481fe12017-10-16 13:16:34 -0700182}
183
Yangster-mac20877162017-12-22 17:19:39 -0800184} // namespace
185
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700186/**
187 * The elements of each log event are stored as a vector of android_log_list_elements.
188 * The goal is to do as little preprocessing as possible, because we read a tiny fraction
189 * of the elements that are written to the log.
190 */
Yao Chen80235402017-11-13 20:42:25 -0800191void LogEvent::init(android_log_context context) {
Chenjie Yuc1fe6f42018-02-01 23:14:18 -0800192 if (!context) {
193 return;
194 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700195 android_log_list_element elem;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700196 // TODO: The log is actually structured inside one list. This is convenient
197 // because we'll be able to use it to put the attribution (WorkSource) block first
198 // without doing our own tagging scheme. Until that change is in, just drop the
199 // list-related log elements and the order we get there is our index-keyed data
200 // structure.
Yao Chen80235402017-11-13 20:42:25 -0800201 int i = 0;
Yangster-mac20877162017-12-22 17:19:39 -0800202
203 int seenListStart = 0;
204
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800205 Field fieldTree;
206 Field* atomField = fieldTree.add_child();
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700207 do {
Yao Chen80235402017-11-13 20:42:25 -0800208 elem = android_log_read_next(context);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700209 switch ((int)elem.type) {
210 case EVENT_TYPE_INT:
Yangster-mac20877162017-12-22 17:19:39 -0800211 // elem at [0] is EVENT_TYPE_LIST, [1] is the tag id.
Yao Chen80235402017-11-13 20:42:25 -0800212 if (i == 1) {
213 mTagId = elem.data.int32;
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800214 fieldTree.set_field(mTagId);
Yangster-mac20877162017-12-22 17:19:39 -0800215 } else {
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800216 increaseField(atomField, seenListStart > 0/* is_child */);
217 mFieldValueMap[fieldTree].set_value_int(elem.data.int32);
Yao Chen80235402017-11-13 20:42:25 -0800218 }
Yangster-mac20877162017-12-22 17:19:39 -0800219 break;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700220 case EVENT_TYPE_FLOAT:
Yangster-mac20877162017-12-22 17:19:39 -0800221 {
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800222 increaseField(atomField, seenListStart > 0/* is_child */);
223 mFieldValueMap[fieldTree].set_value_float(elem.data.float32);
Yangster-mac20877162017-12-22 17:19:39 -0800224 }
225 break;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700226 case EVENT_TYPE_STRING:
Yangster-mac20877162017-12-22 17:19:39 -0800227 {
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800228 increaseField(atomField, seenListStart > 0/* is_child */);
229 mFieldValueMap[fieldTree].set_value_str(
230 string(elem.data.string, elem.len).c_str());
Yangster-mac20877162017-12-22 17:19:39 -0800231 }
232 break;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700233 case EVENT_TYPE_LONG:
Yangster-mac20877162017-12-22 17:19:39 -0800234 {
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800235 increaseField(atomField, seenListStart > 0 /* is_child */);
236 mFieldValueMap[fieldTree].set_value_long(elem.data.int64);
Yangster-mac20877162017-12-22 17:19:39 -0800237 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700238 break;
239 case EVENT_TYPE_LIST:
Yangster-mac20877162017-12-22 17:19:39 -0800240 if (i >= 1) {
241 if (seenListStart > 0) {
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800242 increasePosition(atomField);
Yangster-mac20877162017-12-22 17:19:39 -0800243 } else {
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800244 increaseField(atomField, false /* is_child */);
Yangster-mac20877162017-12-22 17:19:39 -0800245 }
246 seenListStart++;
247 if (seenListStart >= 3) {
248 ALOGE("Depth > 2. Not supported!");
249 return;
250 }
251 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700252 break;
253 case EVENT_TYPE_LIST_STOP:
Yangster-mac20877162017-12-22 17:19:39 -0800254 seenListStart--;
255 if (seenListStart == 0) {
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800256 atomField->clear_position_index();
Yangster-mac20877162017-12-22 17:19:39 -0800257 } else {
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800258 if (atomField->child_size() > 0) {
259 atomField->mutable_child(0)->clear_field();
Yangster-mac20877162017-12-22 17:19:39 -0800260 }
261 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700262 break;
263 case EVENT_TYPE_UNKNOWN:
264 break;
265 default:
266 break;
267 }
Yao Chen80235402017-11-13 20:42:25 -0800268 i++;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700269 } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete);
270}
271
272int64_t LogEvent::GetLong(size_t key, status_t* err) const {
Yangster-mac20877162017-12-22 17:19:39 -0800273 DimensionsValue value;
274 if (!GetSimpleAtomDimensionsValueProto(key, &value)) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700275 *err = BAD_INDEX;
276 return 0;
277 }
Yangster-mac20877162017-12-22 17:19:39 -0800278 const DimensionsValue* leafValue = getSingleLeafValue(&value);
279 switch (leafValue->value_case()) {
280 case DimensionsValue::ValueCase::kValueInt:
281 return (int64_t)leafValue->value_int();
282 case DimensionsValue::ValueCase::kValueLong:
283 return leafValue->value_long();
284 case DimensionsValue::ValueCase::kValueBool:
285 return leafValue->value_bool() ? 1 : 0;
286 case DimensionsValue::ValueCase::kValueFloat:
287 return (int64_t)leafValue->value_float();
288 case DimensionsValue::ValueCase::kValueTuple:
289 case DimensionsValue::ValueCase::kValueStr:
290 case DimensionsValue::ValueCase::VALUE_NOT_SET: {
291 *err = BAD_TYPE;
292 return 0;
293 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700294 }
295}
296
297const char* LogEvent::GetString(size_t key, status_t* err) const {
Yangster-mac20877162017-12-22 17:19:39 -0800298 DimensionsValue value;
299 if (!GetSimpleAtomDimensionsValueProto(key, &value)) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700300 *err = BAD_INDEX;
Yangster-mac20877162017-12-22 17:19:39 -0800301 return 0;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700302 }
Yangster-mac20877162017-12-22 17:19:39 -0800303 const DimensionsValue* leafValue = getSingleLeafValue(&value);
304 switch (leafValue->value_case()) {
305 case DimensionsValue::ValueCase::kValueStr:
306 return leafValue->value_str().c_str();
307 case DimensionsValue::ValueCase::kValueInt:
308 case DimensionsValue::ValueCase::kValueLong:
309 case DimensionsValue::ValueCase::kValueBool:
310 case DimensionsValue::ValueCase::kValueFloat:
311 case DimensionsValue::ValueCase::kValueTuple:
312 case DimensionsValue::ValueCase::VALUE_NOT_SET: {
313 *err = BAD_TYPE;
314 return 0;
315 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700316 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700317}
318
319bool LogEvent::GetBool(size_t key, status_t* err) const {
Yangster-mac20877162017-12-22 17:19:39 -0800320 DimensionsValue value;
321 if (!GetSimpleAtomDimensionsValueProto(key, &value)) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700322 *err = BAD_INDEX;
323 return 0;
324 }
Yangster-mac20877162017-12-22 17:19:39 -0800325 const DimensionsValue* leafValue = getSingleLeafValue(&value);
326 switch (leafValue->value_case()) {
327 case DimensionsValue::ValueCase::kValueInt:
328 return leafValue->value_int() != 0;
329 case DimensionsValue::ValueCase::kValueLong:
330 return leafValue->value_long() != 0;
331 case DimensionsValue::ValueCase::kValueBool:
332 return leafValue->value_bool();
333 case DimensionsValue::ValueCase::kValueFloat:
334 return leafValue->value_float() != 0;
335 case DimensionsValue::ValueCase::kValueTuple:
336 case DimensionsValue::ValueCase::kValueStr:
337 case DimensionsValue::ValueCase::VALUE_NOT_SET: {
338 *err = BAD_TYPE;
339 return 0;
340 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700341 }
342}
343
344float LogEvent::GetFloat(size_t key, status_t* err) const {
Yangster-mac20877162017-12-22 17:19:39 -0800345 DimensionsValue value;
346 if (!GetSimpleAtomDimensionsValueProto(key, &value)) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700347 *err = BAD_INDEX;
348 return 0;
349 }
Yangster-mac20877162017-12-22 17:19:39 -0800350 const DimensionsValue* leafValue = getSingleLeafValue(&value);
351 switch (leafValue->value_case()) {
352 case DimensionsValue::ValueCase::kValueInt:
353 return (float)leafValue->value_int();
354 case DimensionsValue::ValueCase::kValueLong:
355 return (float)leafValue->value_long();
356 case DimensionsValue::ValueCase::kValueBool:
357 return leafValue->value_bool() ? 1.0f : 0.0f;
358 case DimensionsValue::ValueCase::kValueFloat:
359 return leafValue->value_float();
360 case DimensionsValue::ValueCase::kValueTuple:
361 case DimensionsValue::ValueCase::kValueStr:
362 case DimensionsValue::ValueCase::VALUE_NOT_SET: {
363 *err = BAD_TYPE;
364 return 0;
365 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700366 }
367}
368
Yangster-mac20877162017-12-22 17:19:39 -0800369void LogEvent::GetAtomDimensionsValueProtos(const FieldMatcher& matcher,
370 std::vector<DimensionsValue> *dimensionsValues) const {
371 findDimensionsValues(mFieldValueMap, matcher, dimensionsValues);
372}
Yao Chen729093d2017-10-16 10:33:26 -0700373
Yangster-mac20877162017-12-22 17:19:39 -0800374bool LogEvent::GetAtomDimensionsValueProto(const FieldMatcher& matcher,
375 DimensionsValue* dimensionsValue) const {
376 std::vector<DimensionsValue> rootDimensionsValues;
377 findDimensionsValues(mFieldValueMap, matcher, &rootDimensionsValues);
378 if (rootDimensionsValues.size() != 1) {
379 return false;
Yao Chen729093d2017-10-16 10:33:26 -0700380 }
Yangster-mac20877162017-12-22 17:19:39 -0800381 *dimensionsValue = rootDimensionsValues.front();
382 return true;
383}
384
385bool LogEvent::GetSimpleAtomDimensionsValueProto(size_t atomField,
386 DimensionsValue* dimensionsValue) const {
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800387 FieldMatcher matcher;
388 buildSimpleAtomFieldMatcher(mTagId, atomField, &matcher);
389 return GetAtomDimensionsValueProto(matcher, dimensionsValue);
Yao Chen729093d2017-10-16 10:33:26 -0700390}
391
Yangster-macd40053e2018-01-09 16:29:22 -0800392DimensionsValue* LogEvent::findFieldValueOrNull(const Field& field) {
393 auto it = mFieldValueMap.find(field);
394 if (it == mFieldValueMap.end()) {
395 return nullptr;
396 }
397 return &it->second;
398}
399
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700400string LogEvent::ToString() const {
401 ostringstream result;
402 result << "{ " << mTimestampNs << " (" << mTagId << ")";
Yangster-mac20877162017-12-22 17:19:39 -0800403 for (const auto& itr : mFieldValueMap) {
404 result << FieldToString(itr.first);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700405 result << "->";
Yangster-mac20877162017-12-22 17:19:39 -0800406 result << DimensionsValueToString(itr.second);
407 result << " ";
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700408 }
409 result << " }";
410 return result.str();
411}
412
Yangster-mac20877162017-12-22 17:19:39 -0800413void LogEvent::ToProto(ProtoOutputStream& protoOutput) const {
414 writeFieldValueTreeToStream(getFieldValueMap(), &protoOutput);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700415}
416
417} // namespace statsd
418} // namespace os
419} // namespace android