blob: 49a6e330c590daaaf2f13ae4c681425aaa997ae9 [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) {
Yangster-mac20877162017-12-22 17:19:39 -080039 android_log_context context =
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;
Yangster-mac20877162017-12-22 17:19:39 -080043 init(context);
44 if (context) {
45 android_log_destroy(&context);
46 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -070047}
48
Yao Chen80235402017-11-13 20:42:25 -080049LogEvent::LogEvent(int32_t tagId, uint64_t timestampNs) {
50 mTimestampNs = timestampNs;
51 mTagId = tagId;
Yangster-mac20877162017-12-22 17:19:39 -080052 mLogUid = 0;
Yao Chen80235402017-11-13 20:42:25 -080053 mContext = create_android_logger(1937006964); // the event tag shared by all stats logs
54 if (mContext) {
55 android_log_write_int32(mContext, tagId);
56 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -070057}
58
David Chen1481fe12017-10-16 13:16:34 -070059void LogEvent::init() {
Yao Chen80235402017-11-13 20:42:25 -080060 if (mContext) {
61 const char* buffer;
62 size_t len = android_log_write_list_buffer(mContext, &buffer);
63 // turns to reader mode
64 mContext = create_android_log_parser(buffer, len);
65 init(mContext);
Yangster-mac20877162017-12-22 17:19:39 -080066 // destroy the context to save memory.
67 android_log_destroy(&mContext);
68 }
69}
70
71LogEvent::~LogEvent() {
72 if (mContext) {
73 android_log_destroy(&mContext);
Yao Chen80235402017-11-13 20:42:25 -080074 }
75}
76
77bool LogEvent::write(int32_t value) {
78 if (mContext) {
79 return android_log_write_int32(mContext, value) >= 0;
80 }
81 return false;
82}
83
84bool LogEvent::write(uint32_t value) {
85 if (mContext) {
86 return android_log_write_int32(mContext, value) >= 0;
87 }
88 return false;
89}
90
Chenjie Yud9dfda72017-12-11 17:41:20 -080091bool LogEvent::write(int64_t value) {
92 if (mContext) {
93 return android_log_write_int64(mContext, value) >= 0;
94 }
95 return false;
96}
97
Yao Chen80235402017-11-13 20:42:25 -080098bool LogEvent::write(uint64_t value) {
99 if (mContext) {
100 return android_log_write_int64(mContext, value) >= 0;
101 }
102 return false;
103}
104
105bool LogEvent::write(const string& value) {
106 if (mContext) {
107 return android_log_write_string8_len(mContext, value.c_str(), value.length()) >= 0;
108 }
109 return false;
110}
111
112bool LogEvent::write(float value) {
113 if (mContext) {
114 return android_log_write_float32(mContext, value) >= 0;
115 }
116 return false;
117}
118
Yangster-mac20877162017-12-22 17:19:39 -0800119bool LogEvent::write(const std::vector<AttributionNode>& nodes) {
Yao Chen80235402017-11-13 20:42:25 -0800120 if (mContext) {
Yangster-mac20877162017-12-22 17:19:39 -0800121 if (android_log_write_list_begin(mContext) < 0) {
122 return false;
123 }
124 for (size_t i = 0; i < nodes.size(); ++i) {
125 if (!write(nodes[i])) {
126 return false;
127 }
128 }
129 if (android_log_write_list_end(mContext) < 0) {
130 return false;
131 }
132 return true;
133 }
134 return false;
135}
136
137bool LogEvent::write(const AttributionNode& node) {
138 if (mContext) {
139 if (android_log_write_list_begin(mContext) < 0) {
140 return false;
141 }
142 if (android_log_write_int32(mContext, node.uid()) < 0) {
143 return false;
144 }
145 if (android_log_write_string8(mContext, node.tag().c_str()) < 0) {
146 return false;
147 }
148 if (android_log_write_int32(mContext, node.uid()) < 0) {
149 return false;
150 }
151 if (android_log_write_list_end(mContext) < 0) {
152 return false;
153 }
154 return true;
155 }
156 return false;
157}
158
159namespace {
160
161void increaseField(Field *field, bool is_child) {
162 if (is_child) {
163 if (field->child_size() <= 0) {
164 field->add_child();
165 }
166 } else {
167 field->clear_child();
168 }
169 Field* curr = is_child ? field->mutable_child(0) : field;
170 if (!curr->has_field()) {
171 curr->set_field(1);
172 } else {
173 curr->set_field(curr->field() + 1);
Yao Chen80235402017-11-13 20:42:25 -0800174 }
David Chen1481fe12017-10-16 13:16:34 -0700175}
176
Yangster-mac20877162017-12-22 17:19:39 -0800177} // namespace
178
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700179/**
180 * The elements of each log event are stored as a vector of android_log_list_elements.
181 * The goal is to do as little preprocessing as possible, because we read a tiny fraction
182 * of the elements that are written to the log.
183 */
Yao Chen80235402017-11-13 20:42:25 -0800184void LogEvent::init(android_log_context context) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700185 android_log_list_element elem;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700186 // TODO: The log is actually structured inside one list. This is convenient
187 // because we'll be able to use it to put the attribution (WorkSource) block first
188 // without doing our own tagging scheme. Until that change is in, just drop the
189 // list-related log elements and the order we get there is our index-keyed data
190 // structure.
Yao Chen80235402017-11-13 20:42:25 -0800191 int i = 0;
Yangster-mac20877162017-12-22 17:19:39 -0800192
193 int seenListStart = 0;
194
195 Field field;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700196 do {
Yao Chen80235402017-11-13 20:42:25 -0800197 elem = android_log_read_next(context);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700198 switch ((int)elem.type) {
199 case EVENT_TYPE_INT:
Yangster-mac20877162017-12-22 17:19:39 -0800200 // elem at [0] is EVENT_TYPE_LIST, [1] is the tag id.
Yao Chen80235402017-11-13 20:42:25 -0800201 if (i == 1) {
202 mTagId = elem.data.int32;
Yangster-mac20877162017-12-22 17:19:39 -0800203 } else {
204 increaseField(&field, seenListStart > 0/* is_child */);
205 DimensionsValue dimensionsValue;
206 dimensionsValue.set_value_int(elem.data.int32);
207 setFieldInLeafValueProto(field, &dimensionsValue);
208 mFieldValueMap.insert(
209 std::make_pair(buildAtomField(mTagId, field), dimensionsValue));
Yao Chen80235402017-11-13 20:42:25 -0800210 }
Yangster-mac20877162017-12-22 17:19:39 -0800211 break;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700212 case EVENT_TYPE_FLOAT:
Yangster-mac20877162017-12-22 17:19:39 -0800213 {
214 increaseField(&field, seenListStart > 0/* is_child */);
215 DimensionsValue dimensionsValue;
216 dimensionsValue.set_value_float(elem.data.float32);
217 setFieldInLeafValueProto(field, &dimensionsValue);
218 mFieldValueMap.insert(
219 std::make_pair(buildAtomField(mTagId, field), dimensionsValue));
220 }
221 break;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700222 case EVENT_TYPE_STRING:
Yangster-mac20877162017-12-22 17:19:39 -0800223 {
224 increaseField(&field, seenListStart > 0/* is_child */);
225 DimensionsValue dimensionsValue;
226 dimensionsValue.set_value_str(string(elem.data.string, elem.len).c_str());
227 setFieldInLeafValueProto(field, &dimensionsValue);
228 mFieldValueMap.insert(
229 std::make_pair(buildAtomField(mTagId, field), dimensionsValue));
230 }
231 break;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700232 case EVENT_TYPE_LONG:
Yangster-mac20877162017-12-22 17:19:39 -0800233 {
234 increaseField(&field, seenListStart > 0 /* is_child */);
235 DimensionsValue dimensionsValue;
236 dimensionsValue.set_value_long(elem.data.int64);
237 setFieldInLeafValueProto(field, &dimensionsValue);
238 mFieldValueMap.insert(
239 std::make_pair(buildAtomField(mTagId, field), dimensionsValue));
240 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700241 break;
242 case EVENT_TYPE_LIST:
Yangster-mac20877162017-12-22 17:19:39 -0800243 if (i >= 1) {
244 if (seenListStart > 0) {
245 increasePosition(&field);
246 } else {
247 increaseField(&field, false /* is_child */);
248 }
249 seenListStart++;
250 if (seenListStart >= 3) {
251 ALOGE("Depth > 2. Not supported!");
252 return;
253 }
254 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700255 break;
256 case EVENT_TYPE_LIST_STOP:
Yangster-mac20877162017-12-22 17:19:39 -0800257 seenListStart--;
258 if (seenListStart == 0) {
259 field.clear_position_index();
260 } else {
261 if (field.child_size() > 0) {
262 field.mutable_child(0)->clear_field();
263 }
264 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700265 break;
266 case EVENT_TYPE_UNKNOWN:
267 break;
268 default:
269 break;
270 }
Yao Chen80235402017-11-13 20:42:25 -0800271 i++;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700272 } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete);
273}
274
275int64_t LogEvent::GetLong(size_t key, status_t* err) const {
Yangster-mac20877162017-12-22 17:19:39 -0800276 DimensionsValue value;
277 if (!GetSimpleAtomDimensionsValueProto(key, &value)) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700278 *err = BAD_INDEX;
279 return 0;
280 }
Yangster-mac20877162017-12-22 17:19:39 -0800281 const DimensionsValue* leafValue = getSingleLeafValue(&value);
282 switch (leafValue->value_case()) {
283 case DimensionsValue::ValueCase::kValueInt:
284 return (int64_t)leafValue->value_int();
285 case DimensionsValue::ValueCase::kValueLong:
286 return leafValue->value_long();
287 case DimensionsValue::ValueCase::kValueBool:
288 return leafValue->value_bool() ? 1 : 0;
289 case DimensionsValue::ValueCase::kValueFloat:
290 return (int64_t)leafValue->value_float();
291 case DimensionsValue::ValueCase::kValueTuple:
292 case DimensionsValue::ValueCase::kValueStr:
293 case DimensionsValue::ValueCase::VALUE_NOT_SET: {
294 *err = BAD_TYPE;
295 return 0;
296 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700297 }
298}
299
300const char* LogEvent::GetString(size_t key, status_t* err) const {
Yangster-mac20877162017-12-22 17:19:39 -0800301 DimensionsValue value;
302 if (!GetSimpleAtomDimensionsValueProto(key, &value)) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700303 *err = BAD_INDEX;
Yangster-mac20877162017-12-22 17:19:39 -0800304 return 0;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700305 }
Yangster-mac20877162017-12-22 17:19:39 -0800306 const DimensionsValue* leafValue = getSingleLeafValue(&value);
307 switch (leafValue->value_case()) {
308 case DimensionsValue::ValueCase::kValueStr:
309 return leafValue->value_str().c_str();
310 case DimensionsValue::ValueCase::kValueInt:
311 case DimensionsValue::ValueCase::kValueLong:
312 case DimensionsValue::ValueCase::kValueBool:
313 case DimensionsValue::ValueCase::kValueFloat:
314 case DimensionsValue::ValueCase::kValueTuple:
315 case DimensionsValue::ValueCase::VALUE_NOT_SET: {
316 *err = BAD_TYPE;
317 return 0;
318 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700319 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700320}
321
322bool LogEvent::GetBool(size_t key, status_t* err) const {
Yangster-mac20877162017-12-22 17:19:39 -0800323 DimensionsValue value;
324 if (!GetSimpleAtomDimensionsValueProto(key, &value)) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700325 *err = BAD_INDEX;
326 return 0;
327 }
Yangster-mac20877162017-12-22 17:19:39 -0800328 const DimensionsValue* leafValue = getSingleLeafValue(&value);
329 switch (leafValue->value_case()) {
330 case DimensionsValue::ValueCase::kValueInt:
331 return leafValue->value_int() != 0;
332 case DimensionsValue::ValueCase::kValueLong:
333 return leafValue->value_long() != 0;
334 case DimensionsValue::ValueCase::kValueBool:
335 return leafValue->value_bool();
336 case DimensionsValue::ValueCase::kValueFloat:
337 return leafValue->value_float() != 0;
338 case DimensionsValue::ValueCase::kValueTuple:
339 case DimensionsValue::ValueCase::kValueStr:
340 case DimensionsValue::ValueCase::VALUE_NOT_SET: {
341 *err = BAD_TYPE;
342 return 0;
343 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700344 }
345}
346
347float LogEvent::GetFloat(size_t key, status_t* err) const {
Yangster-mac20877162017-12-22 17:19:39 -0800348 DimensionsValue value;
349 if (!GetSimpleAtomDimensionsValueProto(key, &value)) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700350 *err = BAD_INDEX;
351 return 0;
352 }
Yangster-mac20877162017-12-22 17:19:39 -0800353 const DimensionsValue* leafValue = getSingleLeafValue(&value);
354 switch (leafValue->value_case()) {
355 case DimensionsValue::ValueCase::kValueInt:
356 return (float)leafValue->value_int();
357 case DimensionsValue::ValueCase::kValueLong:
358 return (float)leafValue->value_long();
359 case DimensionsValue::ValueCase::kValueBool:
360 return leafValue->value_bool() ? 1.0f : 0.0f;
361 case DimensionsValue::ValueCase::kValueFloat:
362 return leafValue->value_float();
363 case DimensionsValue::ValueCase::kValueTuple:
364 case DimensionsValue::ValueCase::kValueStr:
365 case DimensionsValue::ValueCase::VALUE_NOT_SET: {
366 *err = BAD_TYPE;
367 return 0;
368 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700369 }
370}
371
Yangster-mac20877162017-12-22 17:19:39 -0800372void LogEvent::GetAtomDimensionsValueProtos(const FieldMatcher& matcher,
373 std::vector<DimensionsValue> *dimensionsValues) const {
374 findDimensionsValues(mFieldValueMap, matcher, dimensionsValues);
375}
Yao Chen729093d2017-10-16 10:33:26 -0700376
Yangster-mac20877162017-12-22 17:19:39 -0800377bool LogEvent::GetAtomDimensionsValueProto(const FieldMatcher& matcher,
378 DimensionsValue* dimensionsValue) const {
379 std::vector<DimensionsValue> rootDimensionsValues;
380 findDimensionsValues(mFieldValueMap, matcher, &rootDimensionsValues);
381 if (rootDimensionsValues.size() != 1) {
382 return false;
Yao Chen729093d2017-10-16 10:33:26 -0700383 }
Yangster-mac20877162017-12-22 17:19:39 -0800384 *dimensionsValue = rootDimensionsValues.front();
385 return true;
386}
387
388bool LogEvent::GetSimpleAtomDimensionsValueProto(size_t atomField,
389 DimensionsValue* dimensionsValue) const {
390 return GetAtomDimensionsValueProto(
391 buildSimpleAtomFieldMatcher(mTagId, atomField), dimensionsValue);
392}
393
394DimensionsValue LogEvent::GetSimpleAtomDimensionsValueProto(size_t atomField) const {
395 DimensionsValue dimensionsValue;
396 GetSimpleAtomDimensionsValueProto(atomField, &dimensionsValue);
397 return dimensionsValue;
Yao Chen729093d2017-10-16 10:33:26 -0700398}
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