blob: f07fc66dbcbb081968fb9d6c61cea9df1b8bf888 [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;
Joe Onoratoc4dfae52017-10-17 23:38:21 -070029using std::ostringstream;
David Chen1481fe12017-10-16 13:16:34 -070030using std::string;
Yao Chen5110bed2017-10-23 12:50:02 -070031using android::util::ProtoOutputStream;
Joe Onoratoc4dfae52017-10-17 23:38:21 -070032
Yao Chen80235402017-11-13 20:42:25 -080033LogEvent::LogEvent(log_msg& msg) {
Chenjie Yu3ca36832018-01-22 15:10:54 -080034 mContext =
Yao Chen80235402017-11-13 20:42:25 -080035 create_android_log_parser(msg.msg() + sizeof(uint32_t), msg.len() - sizeof(uint32_t));
Yangster-mac330af582018-02-08 15:24:38 -080036 mLogdTimestampNs = msg.entry_v1.sec * NS_PER_SEC + msg.entry_v1.nsec;
Yao Chend10f7b12017-12-18 12:53:50 -080037 mLogUid = msg.entry_v4.uid;
Chenjie Yu3ca36832018-01-22 15:10:54 -080038 init(mContext);
39 if (mContext) {
Yao Chen48d75182018-01-23 09:40:48 -080040 // android_log_destroy will set mContext to NULL
Chenjie Yu3ca36832018-01-22 15:10:54 -080041 android_log_destroy(&mContext);
Yangster-mac20877162017-12-22 17:19:39 -080042 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -070043}
44
Yangster-mac330af582018-02-08 15:24:38 -080045LogEvent::LogEvent(int32_t tagId, int64_t wallClockTimestampNs, int64_t elapsedTimestampNs) {
46 mLogdTimestampNs = wallClockTimestampNs;
Yao Chen80235402017-11-13 20:42:25 -080047 mTagId = tagId;
Yangster-mac20877162017-12-22 17:19:39 -080048 mLogUid = 0;
Yao Chen80235402017-11-13 20:42:25 -080049 mContext = create_android_logger(1937006964); // the event tag shared by all stats logs
50 if (mContext) {
Yangster-mac330af582018-02-08 15:24:38 -080051 android_log_write_int64(mContext, elapsedTimestampNs);
52 android_log_write_int32(mContext, tagId);
53 }
54}
55
56LogEvent::LogEvent(int32_t tagId, int64_t timestampNs) {
57 mLogdTimestampNs = timestampNs;
58 mTagId = tagId;
59 mLogUid = 0;
60 mContext = create_android_logger(1937006964); // the event tag shared by all stats logs
61 if (mContext) {
62 android_log_write_int64(mContext, timestampNs);
Yao Chen80235402017-11-13 20:42:25 -080063 android_log_write_int32(mContext, tagId);
64 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -070065}
66
David Chen1481fe12017-10-16 13:16:34 -070067void LogEvent::init() {
Yao Chen80235402017-11-13 20:42:25 -080068 if (mContext) {
69 const char* buffer;
70 size_t len = android_log_write_list_buffer(mContext, &buffer);
71 // turns to reader mode
Chenjie Yuc1fe6f42018-02-01 23:14:18 -080072 android_log_context contextForRead = create_android_log_parser(buffer, len);
73 if (contextForRead) {
74 init(contextForRead);
75 // destroy the context to save memory.
Yao Chen48d75182018-01-23 09:40:48 -080076 // android_log_destroy will set mContext to NULL
Chenjie Yuc1fe6f42018-02-01 23:14:18 -080077 android_log_destroy(&contextForRead);
Yao Chen48d75182018-01-23 09:40:48 -080078 }
Chenjie Yuc1fe6f42018-02-01 23:14:18 -080079 android_log_destroy(&mContext);
Yangster-mac20877162017-12-22 17:19:39 -080080 }
81}
82
83LogEvent::~LogEvent() {
84 if (mContext) {
Yao Chen48d75182018-01-23 09:40:48 -080085 // This is for the case when LogEvent is created using the test interface
86 // but init() isn't called.
Yangster-mac20877162017-12-22 17:19:39 -080087 android_log_destroy(&mContext);
Yao Chen80235402017-11-13 20:42:25 -080088 }
89}
90
91bool LogEvent::write(int32_t value) {
92 if (mContext) {
93 return android_log_write_int32(mContext, value) >= 0;
94 }
95 return false;
96}
97
98bool LogEvent::write(uint32_t value) {
99 if (mContext) {
100 return android_log_write_int32(mContext, value) >= 0;
101 }
102 return false;
103}
104
Chenjie Yud9dfda72017-12-11 17:41:20 -0800105bool LogEvent::write(int64_t value) {
106 if (mContext) {
107 return android_log_write_int64(mContext, value) >= 0;
108 }
109 return false;
110}
111
Yao Chen80235402017-11-13 20:42:25 -0800112bool LogEvent::write(uint64_t value) {
113 if (mContext) {
114 return android_log_write_int64(mContext, value) >= 0;
115 }
116 return false;
117}
118
119bool LogEvent::write(const string& value) {
120 if (mContext) {
121 return android_log_write_string8_len(mContext, value.c_str(), value.length()) >= 0;
122 }
123 return false;
124}
125
126bool LogEvent::write(float value) {
127 if (mContext) {
128 return android_log_write_float32(mContext, value) >= 0;
129 }
130 return false;
131}
132
Yangster-mac20877162017-12-22 17:19:39 -0800133bool LogEvent::write(const std::vector<AttributionNode>& nodes) {
Yao Chen80235402017-11-13 20:42:25 -0800134 if (mContext) {
Yangster-mac20877162017-12-22 17:19:39 -0800135 if (android_log_write_list_begin(mContext) < 0) {
136 return false;
137 }
138 for (size_t i = 0; i < nodes.size(); ++i) {
139 if (!write(nodes[i])) {
140 return false;
141 }
142 }
143 if (android_log_write_list_end(mContext) < 0) {
144 return false;
145 }
146 return true;
147 }
148 return false;
149}
150
151bool LogEvent::write(const AttributionNode& node) {
152 if (mContext) {
153 if (android_log_write_list_begin(mContext) < 0) {
154 return false;
155 }
156 if (android_log_write_int32(mContext, node.uid()) < 0) {
157 return false;
158 }
159 if (android_log_write_string8(mContext, node.tag().c_str()) < 0) {
160 return false;
161 }
Yangster-mac20877162017-12-22 17:19:39 -0800162 if (android_log_write_list_end(mContext) < 0) {
163 return false;
164 }
165 return true;
166 }
167 return false;
168}
169
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700170/**
171 * The elements of each log event are stored as a vector of android_log_list_elements.
172 * The goal is to do as little preprocessing as possible, because we read a tiny fraction
173 * of the elements that are written to the log.
Yao Chen8a8d16c2018-02-08 14:50:40 -0800174 *
175 * The idea here is to read through the log items once, we get as much information we need for
176 * matching as possible. Because this log will be matched against lots of matchers.
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700177 */
Yao Chen80235402017-11-13 20:42:25 -0800178void LogEvent::init(android_log_context context) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700179 android_log_list_element elem;
Yao Chen80235402017-11-13 20:42:25 -0800180 int i = 0;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800181 int depth = -1;
182 int pos[] = {1, 1, 1};
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700183 do {
Yao Chen80235402017-11-13 20:42:25 -0800184 elem = android_log_read_next(context);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700185 switch ((int)elem.type) {
186 case EVENT_TYPE_INT:
Yangster-mac330af582018-02-08 15:24:38 -0800187 // elem at [0] is EVENT_TYPE_LIST, [1] is the timestamp, [2] is tag id.
188 if (i == 2) {
Yao Chen80235402017-11-13 20:42:25 -0800189 mTagId = elem.data.int32;
Yangster-mac20877162017-12-22 17:19:39 -0800190 } else {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800191 if (depth < 0 || depth > 2) {
Yangster-mac20877162017-12-22 17:19:39 -0800192 return;
193 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800194
195 mValues.push_back(
196 FieldValue(Field(mTagId, pos, depth), Value((int32_t)elem.data.int32)));
197
198 pos[depth]++;
Yangster-mac20877162017-12-22 17:19:39 -0800199 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700200 break;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800201 case EVENT_TYPE_FLOAT: {
202 if (depth < 0 || depth > 2) {
203 ALOGE("Depth > 2. Not supported!");
204 return;
205 }
206
207 mValues.push_back(FieldValue(Field(mTagId, pos, depth), Value(elem.data.float32)));
208
209 pos[depth]++;
210
211 } break;
212 case EVENT_TYPE_STRING: {
213 if (depth < 0 || depth > 2) {
214 ALOGE("Depth > 2. Not supported!");
215 return;
216 }
217
218 mValues.push_back(FieldValue(Field(mTagId, pos, depth),
219 Value(string(elem.data.string, elem.len))));
220
221 pos[depth]++;
222
223 } break;
224 case EVENT_TYPE_LONG: {
Yangster-mac330af582018-02-08 15:24:38 -0800225 if (i == 1) {
226 mElapsedTimestampNs = elem.data.int64;
227 } else {
228 if (depth < 0 || depth > 2) {
229 ALOGE("Depth > 2. Not supported!");
230 return;
231 }
232 mValues.push_back(
233 FieldValue(Field(mTagId, pos, depth), Value((int64_t)elem.data.int64)));
234
235 pos[depth]++;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800236 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800237 } break;
238 case EVENT_TYPE_LIST:
239 depth++;
240 if (depth > 2) {
241 ALOGE("Depth > 2. Not supported!");
242 return;
243 }
244 pos[depth] = 1;
245
246 break;
247 case EVENT_TYPE_LIST_STOP: {
248 int prevDepth = depth;
249 depth--;
250 if (depth >= 0 && depth < 2) {
251 // Now go back to decorate the previous items that are last at prevDepth.
252 // So that we can later easily match them with Position=Last matchers.
253 pos[prevDepth]--;
254 int path = getEncodedField(pos, prevDepth, false);
Yao Chendb43afc2018-02-13 09:37:27 -0800255 for (auto it = mValues.rbegin(); it != mValues.rend(); ++it) {
256 if (it->mField.getDepth() >= prevDepth &&
257 it->mField.getPath(prevDepth) == path) {
258 it->mField.decorateLastPos(prevDepth);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800259 } else {
260 // Safe to break, because the items are in DFS order.
261 break;
262 }
Yangster-mac20877162017-12-22 17:19:39 -0800263 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800264 pos[depth]++;
Yangster-mac20877162017-12-22 17:19:39 -0800265 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700266 break;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800267 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700268 case EVENT_TYPE_UNKNOWN:
269 break;
270 default:
271 break;
272 }
Yao Chen80235402017-11-13 20:42:25 -0800273 i++;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700274 } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete);
275}
276
277int64_t LogEvent::GetLong(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800278 // TODO: encapsulate the magical operations all in Field struct as a static function.
279 int field = getSimpleField(key);
280 for (const auto& value : mValues) {
281 if (value.mField.getField() == field) {
Yao Chenab92a1f2018-02-13 15:17:55 -0800282 if (value.mValue.getType() == LONG) {
283 return value.mValue.long_value;
284 } else if (value.mValue.getType() == INT) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800285 return value.mValue.int_value;
286 } else {
287 *err = BAD_TYPE;
288 return 0;
289 }
290 }
291 if ((size_t)value.mField.getPosAtDepth(0) > key) {
292 break;
Yangster-mac20877162017-12-22 17:19:39 -0800293 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700294 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800295
296 *err = BAD_INDEX;
297 return 0;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700298}
299
Chenjie Yu80f91122018-01-31 20:24:50 -0800300int LogEvent::GetInt(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800301 int field = getSimpleField(key);
302 for (const auto& value : mValues) {
303 if (value.mField.getField() == field) {
304 if (value.mValue.getType() == INT) {
305 return value.mValue.int_value;
306 } else {
307 *err = BAD_TYPE;
308 return 0;
309 }
310 }
311 if ((size_t)value.mField.getPosAtDepth(0) > key) {
312 break;
313 }
314 }
315
Chenjie Yu80f91122018-01-31 20:24:50 -0800316 *err = BAD_INDEX;
317 return 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800318}
319
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700320const char* LogEvent::GetString(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800321 int field = getSimpleField(key);
322 for (const auto& value : mValues) {
323 if (value.mField.getField() == field) {
324 if (value.mValue.getType() == STRING) {
325 return value.mValue.str_value.c_str();
326 } else {
327 *err = BAD_TYPE;
328 return 0;
329 }
330 }
331 if ((size_t)value.mField.getPosAtDepth(0) > key) {
332 break;
Yangster-mac20877162017-12-22 17:19:39 -0800333 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700334 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800335
336 *err = BAD_INDEX;
337 return NULL;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700338}
339
340bool LogEvent::GetBool(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800341 int field = getSimpleField(key);
342 for (const auto& value : mValues) {
343 if (value.mField.getField() == field) {
344 if (value.mValue.getType() == INT) {
345 return value.mValue.int_value != 0;
346 } else if (value.mValue.getType() == LONG) {
347 return value.mValue.long_value != 0;
348 } else {
349 *err = BAD_TYPE;
350 return false;
351 }
352 }
353 if ((size_t)value.mField.getPosAtDepth(0) > key) {
354 break;
Yangster-mac20877162017-12-22 17:19:39 -0800355 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700356 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800357
358 *err = BAD_INDEX;
359 return false;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700360}
361
362float LogEvent::GetFloat(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800363 int field = getSimpleField(key);
364 for (const auto& value : mValues) {
365 if (value.mField.getField() == field) {
366 if (value.mValue.getType() == FLOAT) {
367 return value.mValue.float_value;
368 } else {
369 *err = BAD_TYPE;
370 return 0.0;
371 }
372 }
373 if ((size_t)value.mField.getPosAtDepth(0) > key) {
374 break;
Yangster-mac20877162017-12-22 17:19:39 -0800375 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700376 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700377
Yao Chen8a8d16c2018-02-08 14:50:40 -0800378 *err = BAD_INDEX;
379 return 0.0;
Yangster-macd40053e2018-01-09 16:29:22 -0800380}
381
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700382string LogEvent::ToString() const {
383 ostringstream result;
Yangster-mac330af582018-02-08 15:24:38 -0800384 result << "{ " << mLogdTimestampNs << " " << mElapsedTimestampNs << " (" << mTagId << ")";
Yao Chen8a8d16c2018-02-08 14:50:40 -0800385 for (const auto& value : mValues) {
386 result << StringPrintf("%#x", value.mField.getField());
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700387 result << "->";
Yao Chen8a8d16c2018-02-08 14:50:40 -0800388 result << value.mValue.toString();
Yangster-mac20877162017-12-22 17:19:39 -0800389 result << " ";
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700390 }
391 result << " }";
392 return result.str();
393}
394
Yangster-mac20877162017-12-22 17:19:39 -0800395void LogEvent::ToProto(ProtoOutputStream& protoOutput) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800396 writeFieldValueTreeToStream(mTagId, getValues(), &protoOutput);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700397}
398
399} // namespace statsd
400} // namespace os
401} // namespace android