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