blob: 5d6d02b4948b1f7675228fb5ff0fadf051e0d93b [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
Yangster-mac20877162017-12-22 17:19:39 -080020#include "stats_log_util.h"
Joe Onoratoc4dfae52017-10-17 23:38:21 -070021
22namespace android {
23namespace os {
24namespace statsd {
25
yro24809bd2017-10-31 23:06:53 -070026using namespace android::util;
Yao Chen9c1debe2018-02-19 14:39:19 -080027using android::util::ProtoOutputStream;
David Chen1481fe12017-10-16 13:16:34 -070028using std::string;
Yao Chen9c1debe2018-02-19 14:39:19 -080029using std::vector;
Joe Onoratoc4dfae52017-10-17 23:38:21 -070030
Yao Chen80235402017-11-13 20:42:25 -080031LogEvent::LogEvent(log_msg& msg) {
Chenjie Yu3ca36832018-01-22 15:10:54 -080032 mContext =
Yao Chen80235402017-11-13 20:42:25 -080033 create_android_log_parser(msg.msg() + sizeof(uint32_t), msg.len() - sizeof(uint32_t));
Yangster-mac330af582018-02-08 15:24:38 -080034 mLogdTimestampNs = msg.entry_v1.sec * NS_PER_SEC + msg.entry_v1.nsec;
Yao Chend10f7b12017-12-18 12:53:50 -080035 mLogUid = msg.entry_v4.uid;
Chenjie Yu3ca36832018-01-22 15:10:54 -080036 init(mContext);
37 if (mContext) {
Yao Chen48d75182018-01-23 09:40:48 -080038 // android_log_destroy will set mContext to NULL
Chenjie Yu3ca36832018-01-22 15:10:54 -080039 android_log_destroy(&mContext);
Yangster-mac20877162017-12-22 17:19:39 -080040 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -070041}
42
Yangster-mac330af582018-02-08 15:24:38 -080043LogEvent::LogEvent(int32_t tagId, int64_t wallClockTimestampNs, int64_t elapsedTimestampNs) {
44 mLogdTimestampNs = wallClockTimestampNs;
Yao Chen80235402017-11-13 20:42:25 -080045 mTagId = tagId;
Yangster-mac20877162017-12-22 17:19:39 -080046 mLogUid = 0;
Yao Chen80235402017-11-13 20:42:25 -080047 mContext = create_android_logger(1937006964); // the event tag shared by all stats logs
48 if (mContext) {
Yangster-mac330af582018-02-08 15:24:38 -080049 android_log_write_int64(mContext, elapsedTimestampNs);
50 android_log_write_int32(mContext, tagId);
51 }
52}
53
54LogEvent::LogEvent(int32_t tagId, int64_t timestampNs) {
55 mLogdTimestampNs = timestampNs;
56 mTagId = tagId;
57 mLogUid = 0;
58 mContext = create_android_logger(1937006964); // the event tag shared by all stats logs
59 if (mContext) {
60 android_log_write_int64(mContext, timestampNs);
Yao Chen80235402017-11-13 20:42:25 -080061 android_log_write_int32(mContext, tagId);
62 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -070063}
64
David Chen1481fe12017-10-16 13:16:34 -070065void LogEvent::init() {
Yao Chen80235402017-11-13 20:42:25 -080066 if (mContext) {
67 const char* buffer;
68 size_t len = android_log_write_list_buffer(mContext, &buffer);
69 // turns to reader mode
Chenjie Yuc1fe6f42018-02-01 23:14:18 -080070 android_log_context contextForRead = create_android_log_parser(buffer, len);
71 if (contextForRead) {
72 init(contextForRead);
73 // destroy the context to save memory.
Yao Chen48d75182018-01-23 09:40:48 -080074 // android_log_destroy will set mContext to NULL
Chenjie Yuc1fe6f42018-02-01 23:14:18 -080075 android_log_destroy(&contextForRead);
Yao Chen48d75182018-01-23 09:40:48 -080076 }
Chenjie Yuc1fe6f42018-02-01 23:14:18 -080077 android_log_destroy(&mContext);
Yangster-mac20877162017-12-22 17:19:39 -080078 }
79}
80
81LogEvent::~LogEvent() {
82 if (mContext) {
Yao Chen48d75182018-01-23 09:40:48 -080083 // This is for the case when LogEvent is created using the test interface
84 // but init() isn't called.
Yangster-mac20877162017-12-22 17:19:39 -080085 android_log_destroy(&mContext);
Yao Chen80235402017-11-13 20:42:25 -080086 }
87}
88
89bool LogEvent::write(int32_t value) {
90 if (mContext) {
91 return android_log_write_int32(mContext, value) >= 0;
92 }
93 return false;
94}
95
96bool LogEvent::write(uint32_t value) {
97 if (mContext) {
98 return android_log_write_int32(mContext, value) >= 0;
99 }
100 return false;
101}
102
Chenjie Yud9dfda72017-12-11 17:41:20 -0800103bool LogEvent::write(int64_t value) {
104 if (mContext) {
105 return android_log_write_int64(mContext, value) >= 0;
106 }
107 return false;
108}
109
Yao Chen80235402017-11-13 20:42:25 -0800110bool LogEvent::write(uint64_t value) {
111 if (mContext) {
112 return android_log_write_int64(mContext, value) >= 0;
113 }
114 return false;
115}
116
117bool LogEvent::write(const string& value) {
118 if (mContext) {
119 return android_log_write_string8_len(mContext, value.c_str(), value.length()) >= 0;
120 }
121 return false;
122}
123
124bool LogEvent::write(float value) {
125 if (mContext) {
126 return android_log_write_float32(mContext, value) >= 0;
127 }
128 return false;
129}
130
Yao Chen9c1debe2018-02-19 14:39:19 -0800131bool LogEvent::write(const std::vector<AttributionNodeInternal>& nodes) {
Yao Chen80235402017-11-13 20:42:25 -0800132 if (mContext) {
Yangster-mac20877162017-12-22 17:19:39 -0800133 if (android_log_write_list_begin(mContext) < 0) {
134 return false;
135 }
136 for (size_t i = 0; i < nodes.size(); ++i) {
137 if (!write(nodes[i])) {
138 return false;
139 }
140 }
141 if (android_log_write_list_end(mContext) < 0) {
142 return false;
143 }
144 return true;
145 }
146 return false;
147}
148
Yao Chen9c1debe2018-02-19 14:39:19 -0800149bool LogEvent::write(const AttributionNodeInternal& node) {
Yangster-mac20877162017-12-22 17:19:39 -0800150 if (mContext) {
151 if (android_log_write_list_begin(mContext) < 0) {
152 return false;
153 }
154 if (android_log_write_int32(mContext, node.uid()) < 0) {
155 return false;
156 }
157 if (android_log_write_string8(mContext, node.tag().c_str()) < 0) {
158 return false;
159 }
Yangster-mac20877162017-12-22 17:19:39 -0800160 if (android_log_write_list_end(mContext) < 0) {
161 return false;
162 }
163 return true;
164 }
165 return false;
166}
167
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700168/**
169 * The elements of each log event are stored as a vector of android_log_list_elements.
170 * The goal is to do as little preprocessing as possible, because we read a tiny fraction
171 * of the elements that are written to the log.
Yao Chen8a8d16c2018-02-08 14:50:40 -0800172 *
173 * The idea here is to read through the log items once, we get as much information we need for
174 * matching as possible. Because this log will be matched against lots of matchers.
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700175 */
Yao Chen80235402017-11-13 20:42:25 -0800176void LogEvent::init(android_log_context context) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700177 android_log_list_element elem;
Yao Chen80235402017-11-13 20:42:25 -0800178 int i = 0;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800179 int depth = -1;
180 int pos[] = {1, 1, 1};
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700181 do {
Yao Chen80235402017-11-13 20:42:25 -0800182 elem = android_log_read_next(context);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700183 switch ((int)elem.type) {
184 case EVENT_TYPE_INT:
Yangster-mac330af582018-02-08 15:24:38 -0800185 // elem at [0] is EVENT_TYPE_LIST, [1] is the timestamp, [2] is tag id.
186 if (i == 2) {
Yao Chen80235402017-11-13 20:42:25 -0800187 mTagId = elem.data.int32;
Yangster-mac20877162017-12-22 17:19:39 -0800188 } else {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800189 if (depth < 0 || depth > 2) {
Yangster-mac20877162017-12-22 17:19:39 -0800190 return;
191 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800192
193 mValues.push_back(
194 FieldValue(Field(mTagId, pos, depth), Value((int32_t)elem.data.int32)));
195
196 pos[depth]++;
Yangster-mac20877162017-12-22 17:19:39 -0800197 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700198 break;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800199 case EVENT_TYPE_FLOAT: {
200 if (depth < 0 || depth > 2) {
201 ALOGE("Depth > 2. Not supported!");
202 return;
203 }
204
205 mValues.push_back(FieldValue(Field(mTagId, pos, depth), Value(elem.data.float32)));
206
207 pos[depth]++;
208
209 } break;
210 case EVENT_TYPE_STRING: {
211 if (depth < 0 || depth > 2) {
212 ALOGE("Depth > 2. Not supported!");
213 return;
214 }
215
216 mValues.push_back(FieldValue(Field(mTagId, pos, depth),
217 Value(string(elem.data.string, elem.len))));
218
219 pos[depth]++;
220
221 } break;
222 case EVENT_TYPE_LONG: {
Yangster-mac330af582018-02-08 15:24:38 -0800223 if (i == 1) {
224 mElapsedTimestampNs = elem.data.int64;
225 } else {
226 if (depth < 0 || depth > 2) {
227 ALOGE("Depth > 2. Not supported!");
228 return;
229 }
230 mValues.push_back(
231 FieldValue(Field(mTagId, pos, depth), Value((int64_t)elem.data.int64)));
232
233 pos[depth]++;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800234 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800235 } break;
236 case EVENT_TYPE_LIST:
237 depth++;
238 if (depth > 2) {
239 ALOGE("Depth > 2. Not supported!");
240 return;
241 }
242 pos[depth] = 1;
243
244 break;
245 case EVENT_TYPE_LIST_STOP: {
246 int prevDepth = depth;
247 depth--;
248 if (depth >= 0 && depth < 2) {
249 // Now go back to decorate the previous items that are last at prevDepth.
250 // So that we can later easily match them with Position=Last matchers.
251 pos[prevDepth]--;
252 int path = getEncodedField(pos, prevDepth, false);
Yao Chendb43afc2018-02-13 09:37:27 -0800253 for (auto it = mValues.rbegin(); it != mValues.rend(); ++it) {
254 if (it->mField.getDepth() >= prevDepth &&
255 it->mField.getPath(prevDepth) == path) {
256 it->mField.decorateLastPos(prevDepth);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800257 } else {
258 // Safe to break, because the items are in DFS order.
259 break;
260 }
Yangster-mac20877162017-12-22 17:19:39 -0800261 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800262 pos[depth]++;
Yangster-mac20877162017-12-22 17:19:39 -0800263 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700264 break;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800265 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700266 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 {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800276 // TODO: encapsulate the magical operations all in Field struct as a static function.
277 int field = getSimpleField(key);
278 for (const auto& value : mValues) {
279 if (value.mField.getField() == field) {
Yao Chenab92a1f2018-02-13 15:17:55 -0800280 if (value.mValue.getType() == LONG) {
281 return value.mValue.long_value;
282 } else if (value.mValue.getType() == INT) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800283 return value.mValue.int_value;
284 } else {
285 *err = BAD_TYPE;
286 return 0;
287 }
288 }
289 if ((size_t)value.mField.getPosAtDepth(0) > key) {
290 break;
Yangster-mac20877162017-12-22 17:19:39 -0800291 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700292 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800293
294 *err = BAD_INDEX;
295 return 0;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700296}
297
Chenjie Yu80f91122018-01-31 20:24:50 -0800298int LogEvent::GetInt(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800299 int field = getSimpleField(key);
300 for (const auto& value : mValues) {
301 if (value.mField.getField() == field) {
302 if (value.mValue.getType() == INT) {
303 return value.mValue.int_value;
304 } else {
305 *err = BAD_TYPE;
306 return 0;
307 }
308 }
309 if ((size_t)value.mField.getPosAtDepth(0) > key) {
310 break;
311 }
312 }
313
Chenjie Yu80f91122018-01-31 20:24:50 -0800314 *err = BAD_INDEX;
315 return 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800316}
317
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700318const char* LogEvent::GetString(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800319 int field = getSimpleField(key);
320 for (const auto& value : mValues) {
321 if (value.mField.getField() == field) {
322 if (value.mValue.getType() == STRING) {
323 return value.mValue.str_value.c_str();
324 } else {
325 *err = BAD_TYPE;
326 return 0;
327 }
328 }
329 if ((size_t)value.mField.getPosAtDepth(0) > key) {
330 break;
Yangster-mac20877162017-12-22 17:19:39 -0800331 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700332 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800333
334 *err = BAD_INDEX;
335 return NULL;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700336}
337
338bool LogEvent::GetBool(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800339 int field = getSimpleField(key);
340 for (const auto& value : mValues) {
341 if (value.mField.getField() == field) {
342 if (value.mValue.getType() == INT) {
343 return value.mValue.int_value != 0;
344 } else if (value.mValue.getType() == LONG) {
345 return value.mValue.long_value != 0;
346 } else {
347 *err = BAD_TYPE;
348 return false;
349 }
350 }
351 if ((size_t)value.mField.getPosAtDepth(0) > key) {
352 break;
Yangster-mac20877162017-12-22 17:19:39 -0800353 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700354 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800355
356 *err = BAD_INDEX;
357 return false;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700358}
359
360float LogEvent::GetFloat(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800361 int field = getSimpleField(key);
362 for (const auto& value : mValues) {
363 if (value.mField.getField() == field) {
364 if (value.mValue.getType() == FLOAT) {
365 return value.mValue.float_value;
366 } else {
367 *err = BAD_TYPE;
368 return 0.0;
369 }
370 }
371 if ((size_t)value.mField.getPosAtDepth(0) > key) {
372 break;
Yangster-mac20877162017-12-22 17:19:39 -0800373 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700374 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700375
Yao Chen8a8d16c2018-02-08 14:50:40 -0800376 *err = BAD_INDEX;
377 return 0.0;
Yangster-macd40053e2018-01-09 16:29:22 -0800378}
379
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700380string LogEvent::ToString() const {
Yao Chen20e9e622018-02-28 11:18:51 -0800381 string result;
Yao Chen2822b4f2018-11-29 09:39:45 -0800382 result += StringPrintf("{ uid(%d) %lld %lld (%d)", mLogUid, (long long)mLogdTimestampNs,
Yao Chen20e9e622018-02-28 11:18:51 -0800383 (long long)mElapsedTimestampNs, mTagId);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800384 for (const auto& value : mValues) {
Yao Chen20e9e622018-02-28 11:18:51 -0800385 result +=
386 StringPrintf("%#x", value.mField.getField()) + "->" + value.mValue.toString() + " ";
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700387 }
Yao Chen20e9e622018-02-28 11:18:51 -0800388 result += " }";
389 return result;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700390}
391
Yangster-mac20877162017-12-22 17:19:39 -0800392void LogEvent::ToProto(ProtoOutputStream& protoOutput) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800393 writeFieldValueTreeToStream(mTagId, getValues(), &protoOutput);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700394}
395
396} // namespace statsd
397} // namespace os
398} // namespace android