blob: d282b86f52bdf7a9459a734fae567ab1b32f600c [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
20#include <sstream>
Yangster-mac20877162017-12-22 17:19:39 -080021
Yangster-mac20877162017-12-22 17:19:39 -080022#include "stats_log_util.h"
Joe Onoratoc4dfae52017-10-17 23:38:21 -070023
24namespace android {
25namespace os {
26namespace statsd {
27
yro24809bd2017-10-31 23:06:53 -070028using namespace android::util;
Yao Chen9c1debe2018-02-19 14:39:19 -080029using android::util::ProtoOutputStream;
Joe Onoratoc4dfae52017-10-17 23:38:21 -070030using std::ostringstream;
David Chen1481fe12017-10-16 13:16:34 -070031using std::string;
Yao Chen9c1debe2018-02-19 14:39:19 -080032using std::vector;
Joe Onoratoc4dfae52017-10-17 23:38:21 -070033
Yao Chen80235402017-11-13 20:42:25 -080034LogEvent::LogEvent(log_msg& msg) {
Chenjie Yu3ca36832018-01-22 15:10:54 -080035 mContext =
Yao Chen80235402017-11-13 20:42:25 -080036 create_android_log_parser(msg.msg() + sizeof(uint32_t), msg.len() - sizeof(uint32_t));
Yangster-mac330af582018-02-08 15:24:38 -080037 mLogdTimestampNs = msg.entry_v1.sec * NS_PER_SEC + msg.entry_v1.nsec;
Yao Chend10f7b12017-12-18 12:53:50 -080038 mLogUid = msg.entry_v4.uid;
Chenjie Yu3ca36832018-01-22 15:10:54 -080039 init(mContext);
40 if (mContext) {
Yao Chen48d75182018-01-23 09:40:48 -080041 // android_log_destroy will set mContext to NULL
Chenjie Yu3ca36832018-01-22 15:10:54 -080042 android_log_destroy(&mContext);
Yangster-mac20877162017-12-22 17:19:39 -080043 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -070044}
45
Yangster-mac330af582018-02-08 15:24:38 -080046LogEvent::LogEvent(int32_t tagId, int64_t wallClockTimestampNs, int64_t elapsedTimestampNs) {
47 mLogdTimestampNs = wallClockTimestampNs;
Yao Chen80235402017-11-13 20:42:25 -080048 mTagId = tagId;
Yangster-mac20877162017-12-22 17:19:39 -080049 mLogUid = 0;
Yao Chen80235402017-11-13 20:42:25 -080050 mContext = create_android_logger(1937006964); // the event tag shared by all stats logs
51 if (mContext) {
Yangster-mac330af582018-02-08 15:24:38 -080052 android_log_write_int64(mContext, elapsedTimestampNs);
53 android_log_write_int32(mContext, tagId);
54 }
55}
56
57LogEvent::LogEvent(int32_t tagId, int64_t timestampNs) {
58 mLogdTimestampNs = timestampNs;
59 mTagId = tagId;
60 mLogUid = 0;
61 mContext = create_android_logger(1937006964); // the event tag shared by all stats logs
62 if (mContext) {
63 android_log_write_int64(mContext, timestampNs);
Yao Chen80235402017-11-13 20:42:25 -080064 android_log_write_int32(mContext, tagId);
65 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -070066}
67
David Chen1481fe12017-10-16 13:16:34 -070068void LogEvent::init() {
Yao Chen80235402017-11-13 20:42:25 -080069 if (mContext) {
70 const char* buffer;
71 size_t len = android_log_write_list_buffer(mContext, &buffer);
72 // turns to reader mode
Chenjie Yuc1fe6f42018-02-01 23:14:18 -080073 android_log_context contextForRead = create_android_log_parser(buffer, len);
74 if (contextForRead) {
75 init(contextForRead);
76 // destroy the context to save memory.
Yao Chen48d75182018-01-23 09:40:48 -080077 // android_log_destroy will set mContext to NULL
Chenjie Yuc1fe6f42018-02-01 23:14:18 -080078 android_log_destroy(&contextForRead);
Yao Chen48d75182018-01-23 09:40:48 -080079 }
Chenjie Yuc1fe6f42018-02-01 23:14:18 -080080 android_log_destroy(&mContext);
Yangster-mac20877162017-12-22 17:19:39 -080081 }
82}
83
84LogEvent::~LogEvent() {
85 if (mContext) {
Yao Chen48d75182018-01-23 09:40:48 -080086 // This is for the case when LogEvent is created using the test interface
87 // but init() isn't called.
Yangster-mac20877162017-12-22 17:19:39 -080088 android_log_destroy(&mContext);
Yao Chen80235402017-11-13 20:42:25 -080089 }
90}
91
92bool LogEvent::write(int32_t value) {
93 if (mContext) {
94 return android_log_write_int32(mContext, value) >= 0;
95 }
96 return false;
97}
98
99bool LogEvent::write(uint32_t value) {
100 if (mContext) {
101 return android_log_write_int32(mContext, value) >= 0;
102 }
103 return false;
104}
105
Chenjie Yud9dfda72017-12-11 17:41:20 -0800106bool LogEvent::write(int64_t value) {
107 if (mContext) {
108 return android_log_write_int64(mContext, value) >= 0;
109 }
110 return false;
111}
112
Yao Chen80235402017-11-13 20:42:25 -0800113bool LogEvent::write(uint64_t value) {
114 if (mContext) {
115 return android_log_write_int64(mContext, value) >= 0;
116 }
117 return false;
118}
119
120bool LogEvent::write(const string& value) {
121 if (mContext) {
122 return android_log_write_string8_len(mContext, value.c_str(), value.length()) >= 0;
123 }
124 return false;
125}
126
127bool LogEvent::write(float value) {
128 if (mContext) {
129 return android_log_write_float32(mContext, value) >= 0;
130 }
131 return false;
132}
133
Yao Chen9c1debe2018-02-19 14:39:19 -0800134bool LogEvent::write(const std::vector<AttributionNodeInternal>& nodes) {
Yao Chen80235402017-11-13 20:42:25 -0800135 if (mContext) {
Yangster-mac20877162017-12-22 17:19:39 -0800136 if (android_log_write_list_begin(mContext) < 0) {
137 return false;
138 }
139 for (size_t i = 0; i < nodes.size(); ++i) {
140 if (!write(nodes[i])) {
141 return false;
142 }
143 }
144 if (android_log_write_list_end(mContext) < 0) {
145 return false;
146 }
147 return true;
148 }
149 return false;
150}
151
Yao Chen9c1debe2018-02-19 14:39:19 -0800152bool LogEvent::write(const AttributionNodeInternal& node) {
Yangster-mac20877162017-12-22 17:19:39 -0800153 if (mContext) {
154 if (android_log_write_list_begin(mContext) < 0) {
155 return false;
156 }
157 if (android_log_write_int32(mContext, node.uid()) < 0) {
158 return false;
159 }
160 if (android_log_write_string8(mContext, node.tag().c_str()) < 0) {
161 return false;
162 }
Yangster-mac20877162017-12-22 17:19:39 -0800163 if (android_log_write_list_end(mContext) < 0) {
164 return false;
165 }
166 return true;
167 }
168 return false;
169}
170
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700171/**
172 * The elements of each log event are stored as a vector of android_log_list_elements.
173 * The goal is to do as little preprocessing as possible, because we read a tiny fraction
174 * of the elements that are written to the log.
Yao Chen8a8d16c2018-02-08 14:50:40 -0800175 *
176 * The idea here is to read through the log items once, we get as much information we need for
177 * matching as possible. Because this log will be matched against lots of matchers.
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700178 */
Yao Chen80235402017-11-13 20:42:25 -0800179void LogEvent::init(android_log_context context) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700180 android_log_list_element elem;
Yao Chen80235402017-11-13 20:42:25 -0800181 int i = 0;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800182 int depth = -1;
183 int pos[] = {1, 1, 1};
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700184 do {
Yao Chen80235402017-11-13 20:42:25 -0800185 elem = android_log_read_next(context);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700186 switch ((int)elem.type) {
187 case EVENT_TYPE_INT:
Yangster-mac330af582018-02-08 15:24:38 -0800188 // elem at [0] is EVENT_TYPE_LIST, [1] is the timestamp, [2] is tag id.
189 if (i == 2) {
Yao Chen80235402017-11-13 20:42:25 -0800190 mTagId = elem.data.int32;
Yangster-mac20877162017-12-22 17:19:39 -0800191 } else {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800192 if (depth < 0 || depth > 2) {
Yangster-mac20877162017-12-22 17:19:39 -0800193 return;
194 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800195
196 mValues.push_back(
197 FieldValue(Field(mTagId, pos, depth), Value((int32_t)elem.data.int32)));
198
199 pos[depth]++;
Yangster-mac20877162017-12-22 17:19:39 -0800200 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700201 break;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800202 case EVENT_TYPE_FLOAT: {
203 if (depth < 0 || depth > 2) {
204 ALOGE("Depth > 2. Not supported!");
205 return;
206 }
207
208 mValues.push_back(FieldValue(Field(mTagId, pos, depth), Value(elem.data.float32)));
209
210 pos[depth]++;
211
212 } break;
213 case EVENT_TYPE_STRING: {
214 if (depth < 0 || depth > 2) {
215 ALOGE("Depth > 2. Not supported!");
216 return;
217 }
218
219 mValues.push_back(FieldValue(Field(mTagId, pos, depth),
220 Value(string(elem.data.string, elem.len))));
221
222 pos[depth]++;
223
224 } break;
225 case EVENT_TYPE_LONG: {
Yangster-mac330af582018-02-08 15:24:38 -0800226 if (i == 1) {
227 mElapsedTimestampNs = elem.data.int64;
228 } else {
229 if (depth < 0 || depth > 2) {
230 ALOGE("Depth > 2. Not supported!");
231 return;
232 }
233 mValues.push_back(
234 FieldValue(Field(mTagId, pos, depth), Value((int64_t)elem.data.int64)));
235
236 pos[depth]++;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800237 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800238 } break;
239 case EVENT_TYPE_LIST:
240 depth++;
241 if (depth > 2) {
242 ALOGE("Depth > 2. Not supported!");
243 return;
244 }
245 pos[depth] = 1;
246
247 break;
248 case EVENT_TYPE_LIST_STOP: {
249 int prevDepth = depth;
250 depth--;
251 if (depth >= 0 && depth < 2) {
252 // Now go back to decorate the previous items that are last at prevDepth.
253 // So that we can later easily match them with Position=Last matchers.
254 pos[prevDepth]--;
255 int path = getEncodedField(pos, prevDepth, false);
Yao Chendb43afc2018-02-13 09:37:27 -0800256 for (auto it = mValues.rbegin(); it != mValues.rend(); ++it) {
257 if (it->mField.getDepth() >= prevDepth &&
258 it->mField.getPath(prevDepth) == path) {
259 it->mField.decorateLastPos(prevDepth);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800260 } else {
261 // Safe to break, because the items are in DFS order.
262 break;
263 }
Yangster-mac20877162017-12-22 17:19:39 -0800264 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800265 pos[depth]++;
Yangster-mac20877162017-12-22 17:19:39 -0800266 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700267 break;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800268 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700269 case EVENT_TYPE_UNKNOWN:
270 break;
271 default:
272 break;
273 }
Yao Chen80235402017-11-13 20:42:25 -0800274 i++;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700275 } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete);
276}
277
278int64_t LogEvent::GetLong(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800279 // TODO: encapsulate the magical operations all in Field struct as a static function.
280 int field = getSimpleField(key);
281 for (const auto& value : mValues) {
282 if (value.mField.getField() == field) {
Yao Chenab92a1f2018-02-13 15:17:55 -0800283 if (value.mValue.getType() == LONG) {
284 return value.mValue.long_value;
285 } else if (value.mValue.getType() == INT) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800286 return value.mValue.int_value;
287 } else {
288 *err = BAD_TYPE;
289 return 0;
290 }
291 }
292 if ((size_t)value.mField.getPosAtDepth(0) > key) {
293 break;
Yangster-mac20877162017-12-22 17:19:39 -0800294 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700295 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800296
297 *err = BAD_INDEX;
298 return 0;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700299}
300
Chenjie Yu80f91122018-01-31 20:24:50 -0800301int LogEvent::GetInt(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800302 int field = getSimpleField(key);
303 for (const auto& value : mValues) {
304 if (value.mField.getField() == field) {
305 if (value.mValue.getType() == INT) {
306 return value.mValue.int_value;
307 } else {
308 *err = BAD_TYPE;
309 return 0;
310 }
311 }
312 if ((size_t)value.mField.getPosAtDepth(0) > key) {
313 break;
314 }
315 }
316
Chenjie Yu80f91122018-01-31 20:24:50 -0800317 *err = BAD_INDEX;
318 return 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800319}
320
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700321const char* LogEvent::GetString(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800322 int field = getSimpleField(key);
323 for (const auto& value : mValues) {
324 if (value.mField.getField() == field) {
325 if (value.mValue.getType() == STRING) {
326 return value.mValue.str_value.c_str();
327 } else {
328 *err = BAD_TYPE;
329 return 0;
330 }
331 }
332 if ((size_t)value.mField.getPosAtDepth(0) > key) {
333 break;
Yangster-mac20877162017-12-22 17:19:39 -0800334 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700335 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800336
337 *err = BAD_INDEX;
338 return NULL;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700339}
340
341bool LogEvent::GetBool(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800342 int field = getSimpleField(key);
343 for (const auto& value : mValues) {
344 if (value.mField.getField() == field) {
345 if (value.mValue.getType() == INT) {
346 return value.mValue.int_value != 0;
347 } else if (value.mValue.getType() == LONG) {
348 return value.mValue.long_value != 0;
349 } else {
350 *err = BAD_TYPE;
351 return false;
352 }
353 }
354 if ((size_t)value.mField.getPosAtDepth(0) > key) {
355 break;
Yangster-mac20877162017-12-22 17:19:39 -0800356 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700357 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800358
359 *err = BAD_INDEX;
360 return false;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700361}
362
363float LogEvent::GetFloat(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800364 int field = getSimpleField(key);
365 for (const auto& value : mValues) {
366 if (value.mField.getField() == field) {
367 if (value.mValue.getType() == FLOAT) {
368 return value.mValue.float_value;
369 } else {
370 *err = BAD_TYPE;
371 return 0.0;
372 }
373 }
374 if ((size_t)value.mField.getPosAtDepth(0) > key) {
375 break;
Yangster-mac20877162017-12-22 17:19:39 -0800376 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700377 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700378
Yao Chen8a8d16c2018-02-08 14:50:40 -0800379 *err = BAD_INDEX;
380 return 0.0;
Yangster-macd40053e2018-01-09 16:29:22 -0800381}
382
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700383string LogEvent::ToString() const {
384 ostringstream result;
Yangster-mac330af582018-02-08 15:24:38 -0800385 result << "{ " << mLogdTimestampNs << " " << mElapsedTimestampNs << " (" << mTagId << ")";
Yao Chen8a8d16c2018-02-08 14:50:40 -0800386 for (const auto& value : mValues) {
387 result << StringPrintf("%#x", value.mField.getField());
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700388 result << "->";
Yao Chen8a8d16c2018-02-08 14:50:40 -0800389 result << value.mValue.toString();
Yangster-mac20877162017-12-22 17:19:39 -0800390 result << " ";
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700391 }
392 result << " }";
393 return result.str();
394}
395
Yangster-mac20877162017-12-22 17:19:39 -0800396void LogEvent::ToProto(ProtoOutputStream& protoOutput) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800397 writeFieldValueTreeToStream(mTagId, getValues(), &protoOutput);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700398}
399
400} // namespace statsd
401} // namespace os
402} // namespace android