Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 1 | /* |
| 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 Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 17 | #define DEBUG true // STOPSHIP if true |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 18 | #include "logd/LogEvent.h" |
| 19 | |
| 20 | #include <sstream> |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 21 | #include "stats_util.h" |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 22 | |
| 23 | namespace android { |
| 24 | namespace os { |
| 25 | namespace statsd { |
| 26 | |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 27 | using namespace android::util; |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 28 | using std::ostringstream; |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 29 | using std::string; |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 30 | using android::util::ProtoOutputStream; |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 31 | |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 32 | LogEvent::LogEvent(log_msg& msg) { |
Yao Chen | ae6a83a | 2017-11-18 18:56:52 -0800 | [diff] [blame] | 33 | mContext = |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 34 | create_android_log_parser(msg.msg() + sizeof(uint32_t), msg.len() - sizeof(uint32_t)); |
| 35 | mTimestampNs = msg.entry_v1.sec * NS_PER_SEC + msg.entry_v1.nsec; |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame^] | 36 | mLogUid = msg.entry_v4.uid; |
Yao Chen | ae6a83a | 2017-11-18 18:56:52 -0800 | [diff] [blame] | 37 | init(mContext); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 40 | LogEvent::LogEvent(int32_t tagId, uint64_t timestampNs) { |
| 41 | mTimestampNs = timestampNs; |
| 42 | mTagId = tagId; |
| 43 | mContext = create_android_logger(1937006964); // the event tag shared by all stats logs |
| 44 | if (mContext) { |
| 45 | android_log_write_int32(mContext, tagId); |
| 46 | } |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 47 | } |
| 48 | |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 49 | void LogEvent::init() { |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 50 | if (mContext) { |
| 51 | const char* buffer; |
| 52 | size_t len = android_log_write_list_buffer(mContext, &buffer); |
| 53 | // turns to reader mode |
| 54 | mContext = create_android_log_parser(buffer, len); |
| 55 | init(mContext); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | bool LogEvent::write(int32_t value) { |
| 60 | if (mContext) { |
| 61 | return android_log_write_int32(mContext, value) >= 0; |
| 62 | } |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | bool LogEvent::write(uint32_t value) { |
| 67 | if (mContext) { |
| 68 | return android_log_write_int32(mContext, value) >= 0; |
| 69 | } |
| 70 | return false; |
| 71 | } |
| 72 | |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 73 | bool LogEvent::write(int64_t value) { |
| 74 | if (mContext) { |
| 75 | return android_log_write_int64(mContext, value) >= 0; |
| 76 | } |
| 77 | return false; |
| 78 | } |
| 79 | |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 80 | bool LogEvent::write(uint64_t value) { |
| 81 | if (mContext) { |
| 82 | return android_log_write_int64(mContext, value) >= 0; |
| 83 | } |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | bool LogEvent::write(const string& value) { |
| 88 | if (mContext) { |
| 89 | return android_log_write_string8_len(mContext, value.c_str(), value.length()) >= 0; |
| 90 | } |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | bool LogEvent::write(float value) { |
| 95 | if (mContext) { |
| 96 | return android_log_write_float32(mContext, value) >= 0; |
| 97 | } |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | LogEvent::~LogEvent() { |
| 102 | if (mContext) { |
| 103 | android_log_destroy(&mContext); |
| 104 | } |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 107 | /** |
| 108 | * The elements of each log event are stored as a vector of android_log_list_elements. |
| 109 | * The goal is to do as little preprocessing as possible, because we read a tiny fraction |
| 110 | * of the elements that are written to the log. |
| 111 | */ |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 112 | void LogEvent::init(android_log_context context) { |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 113 | mElements.clear(); |
| 114 | android_log_list_element elem; |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 115 | // TODO: The log is actually structured inside one list. This is convenient |
| 116 | // because we'll be able to use it to put the attribution (WorkSource) block first |
| 117 | // without doing our own tagging scheme. Until that change is in, just drop the |
| 118 | // list-related log elements and the order we get there is our index-keyed data |
| 119 | // structure. |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 120 | int i = 0; |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 121 | do { |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 122 | elem = android_log_read_next(context); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 123 | switch ((int)elem.type) { |
| 124 | case EVENT_TYPE_INT: |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 125 | // elem at [0] is EVENT_TYPE_LIST, [1] is the tag id. If we add WorkSource, it would |
| 126 | // be the list starting at [2]. |
| 127 | if (i == 1) { |
| 128 | mTagId = elem.data.int32; |
| 129 | break; |
| 130 | } |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 131 | case EVENT_TYPE_FLOAT: |
| 132 | case EVENT_TYPE_STRING: |
| 133 | case EVENT_TYPE_LONG: |
| 134 | mElements.push_back(elem); |
| 135 | break; |
| 136 | case EVENT_TYPE_LIST: |
| 137 | break; |
| 138 | case EVENT_TYPE_LIST_STOP: |
| 139 | break; |
| 140 | case EVENT_TYPE_UNKNOWN: |
| 141 | break; |
| 142 | default: |
| 143 | break; |
| 144 | } |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 145 | i++; |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 146 | } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete); |
| 147 | } |
| 148 | |
| 149 | int64_t LogEvent::GetLong(size_t key, status_t* err) const { |
| 150 | if (key < 1 || (key - 1) >= mElements.size()) { |
| 151 | *err = BAD_INDEX; |
| 152 | return 0; |
| 153 | } |
| 154 | key--; |
| 155 | const android_log_list_element& elem = mElements[key]; |
| 156 | if (elem.type == EVENT_TYPE_INT) { |
| 157 | return elem.data.int32; |
| 158 | } else if (elem.type == EVENT_TYPE_LONG) { |
| 159 | return elem.data.int64; |
| 160 | } else if (elem.type == EVENT_TYPE_FLOAT) { |
| 161 | return (int64_t)elem.data.float32; |
| 162 | } else { |
| 163 | *err = BAD_TYPE; |
| 164 | return 0; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | const char* LogEvent::GetString(size_t key, status_t* err) const { |
| 169 | if (key < 1 || (key - 1) >= mElements.size()) { |
| 170 | *err = BAD_INDEX; |
| 171 | return NULL; |
| 172 | } |
| 173 | key--; |
| 174 | const android_log_list_element& elem = mElements[key]; |
| 175 | if (elem.type != EVENT_TYPE_STRING) { |
| 176 | *err = BAD_TYPE; |
| 177 | return NULL; |
| 178 | } |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 179 | // Need to add the '/0' at the end by specifying the length of the string. |
| 180 | return string(elem.data.string, elem.len).c_str(); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | bool LogEvent::GetBool(size_t key, status_t* err) const { |
| 184 | if (key < 1 || (key - 1) >= mElements.size()) { |
| 185 | *err = BAD_INDEX; |
| 186 | return 0; |
| 187 | } |
| 188 | key--; |
| 189 | const android_log_list_element& elem = mElements[key]; |
| 190 | if (elem.type == EVENT_TYPE_INT) { |
| 191 | return elem.data.int32 != 0; |
| 192 | } else if (elem.type == EVENT_TYPE_LONG) { |
| 193 | return elem.data.int64 != 0; |
| 194 | } else if (elem.type == EVENT_TYPE_FLOAT) { |
| 195 | return elem.data.float32 != 0; |
| 196 | } else { |
| 197 | *err = BAD_TYPE; |
| 198 | return 0; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | float LogEvent::GetFloat(size_t key, status_t* err) const { |
| 203 | if (key < 1 || (key - 1) >= mElements.size()) { |
| 204 | *err = BAD_INDEX; |
| 205 | return 0; |
| 206 | } |
| 207 | key--; |
| 208 | const android_log_list_element& elem = mElements[key]; |
| 209 | if (elem.type == EVENT_TYPE_INT) { |
| 210 | return (float)elem.data.int32; |
| 211 | } else if (elem.type == EVENT_TYPE_LONG) { |
| 212 | return (float)elem.data.int64; |
| 213 | } else if (elem.type == EVENT_TYPE_FLOAT) { |
| 214 | return elem.data.float32; |
| 215 | } else { |
| 216 | *err = BAD_TYPE; |
| 217 | return 0; |
| 218 | } |
| 219 | } |
| 220 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 221 | KeyValuePair LogEvent::GetKeyValueProto(size_t key) const { |
| 222 | KeyValuePair pair; |
| 223 | pair.set_key(key); |
| 224 | // If the value is not valid, return the KeyValuePair without assigning the value. |
| 225 | // Caller can detect the error by checking the enum for "one of" proto type. |
| 226 | if (key < 1 || (key - 1) >= mElements.size()) { |
| 227 | return pair; |
| 228 | } |
| 229 | key--; |
| 230 | |
| 231 | const android_log_list_element& elem = mElements[key]; |
| 232 | if (elem.type == EVENT_TYPE_INT) { |
| 233 | pair.set_value_int(elem.data.int32); |
| 234 | } else if (elem.type == EVENT_TYPE_LONG) { |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 235 | pair.set_value_long(elem.data.int64); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 236 | } else if (elem.type == EVENT_TYPE_STRING) { |
| 237 | pair.set_value_str(elem.data.string); |
| 238 | } else if (elem.type == EVENT_TYPE_FLOAT) { |
| 239 | pair.set_value_float(elem.data.float32); |
| 240 | } |
| 241 | return pair; |
| 242 | } |
| 243 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 244 | string LogEvent::ToString() const { |
| 245 | ostringstream result; |
| 246 | result << "{ " << mTimestampNs << " (" << mTagId << ")"; |
| 247 | const size_t N = mElements.size(); |
| 248 | for (size_t i=0; i<N; i++) { |
| 249 | result << " "; |
| 250 | result << (i + 1); |
| 251 | result << "->"; |
| 252 | const android_log_list_element& elem = mElements[i]; |
| 253 | if (elem.type == EVENT_TYPE_INT) { |
| 254 | result << elem.data.int32; |
| 255 | } else if (elem.type == EVENT_TYPE_LONG) { |
| 256 | result << elem.data.int64; |
| 257 | } else if (elem.type == EVENT_TYPE_FLOAT) { |
| 258 | result << elem.data.float32; |
| 259 | } else if (elem.type == EVENT_TYPE_STRING) { |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 260 | // Need to add the '/0' at the end by specifying the length of the string. |
| 261 | result << string(elem.data.string, elem.len).c_str(); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | result << " }"; |
| 265 | return result.str(); |
| 266 | } |
| 267 | |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 268 | void LogEvent::ToProto(ProtoOutputStream& proto) const { |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 269 | long long atomToken = proto.start(FIELD_TYPE_MESSAGE | mTagId); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 270 | const size_t N = mElements.size(); |
| 271 | for (size_t i=0; i<N; i++) { |
| 272 | const int key = i + 1; |
| 273 | |
| 274 | const android_log_list_element& elem = mElements[i]; |
| 275 | if (elem.type == EVENT_TYPE_INT) { |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 276 | proto.write(FIELD_TYPE_INT32 | key, elem.data.int32); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 277 | } else if (elem.type == EVENT_TYPE_LONG) { |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 278 | proto.write(FIELD_TYPE_INT64 | key, (long long)elem.data.int64); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 279 | } else if (elem.type == EVENT_TYPE_FLOAT) { |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 280 | proto.write(FIELD_TYPE_FLOAT | key, elem.data.float32); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 281 | } else if (elem.type == EVENT_TYPE_STRING) { |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 282 | proto.write(FIELD_TYPE_STRING | key, elem.data.string); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 283 | } |
| 284 | } |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 285 | proto.end(atomToken); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | } // namespace statsd |
| 289 | } // namespace os |
| 290 | } // namespace android |