Yao Chen | caf339d | 2017-10-06 16:01:10 -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 | |
| 17 | #include <log/log_event_list.h> |
| 18 | #include "stats_util.h" |
| 19 | |
| 20 | namespace android { |
| 21 | namespace os { |
| 22 | namespace statsd { |
| 23 | |
| 24 | static inline uint32_t get4LE(const char* src) { |
| 25 | return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24); |
| 26 | } |
| 27 | |
| 28 | int getTagId(log_msg msg) { |
| 29 | return get4LE(msg.msg()); |
| 30 | } |
| 31 | |
| 32 | EventMetricData parse(log_msg msg) { |
| 33 | // dump all statsd logs to dropbox for now. |
| 34 | // TODO: Add filtering, aggregation, etc. |
| 35 | EventMetricData eventMetricData; |
| 36 | |
| 37 | // set tag. |
| 38 | int tag = getTagId(msg); |
| 39 | // TODO: Replace the following line when we can serialize on the fly. |
| 40 | // eventMetricData.set_tag(tag); |
| 41 | |
| 42 | // set timestamp of the event. |
| 43 | eventMetricData.set_timestamp_nanos(msg.entry_v1.sec * NS_PER_SEC + msg.entry_v1.nsec); |
| 44 | |
| 45 | // start iterating k,v pairs. |
| 46 | android_log_context context = |
| 47 | create_android_log_parser(const_cast<log_msg*>(&msg)->msg() + sizeof(uint32_t), |
| 48 | const_cast<log_msg*>(&msg)->len() - sizeof(uint32_t)); |
| 49 | android_log_list_element elem; |
| 50 | |
| 51 | if (context) { |
| 52 | memset(&elem, 0, sizeof(elem)); |
| 53 | size_t index = 0; |
| 54 | int32_t key = -1; |
| 55 | |
| 56 | do { |
| 57 | elem = android_log_read_next(context); |
| 58 | switch ((int)elem.type) { |
| 59 | case EVENT_TYPE_INT: |
| 60 | if (index % 2 == 0) { |
| 61 | key = elem.data.int32; |
| 62 | } else { |
| 63 | // TODO: Fix the following lines when we can serialize on the fly. |
| 64 | /* |
| 65 | int32_t val = elem.data.int32; |
| 66 | KeyValuePair* keyValuePair = eventMetricData.add_key_value_pair(); |
| 67 | keyValuePair->set_key(key); |
| 68 | keyValuePair->set_value_int(val); |
| 69 | */ |
| 70 | } |
| 71 | index++; |
| 72 | break; |
| 73 | case EVENT_TYPE_FLOAT: |
| 74 | if (index % 2 == 1) { |
| 75 | // TODO: Fix the following lines when we can serialize on the fly. |
| 76 | /* |
| 77 | float val = elem.data.float32; |
| 78 | KeyValuePair* keyValuePair = eventMetricData.add_key_value_pair(); |
| 79 | keyValuePair->set_key(key); |
| 80 | keyValuePair->set_value_float(val); |
| 81 | */ |
| 82 | } |
| 83 | index++; |
| 84 | break; |
| 85 | case EVENT_TYPE_STRING: |
| 86 | if (index % 2 == 1) { |
| 87 | // TODO: Fix the following lines when we can serialize on the fly. |
| 88 | /* |
| 89 | char* val = elem.data.string; |
| 90 | KeyValuePair* keyValuePair = eventMetricData.add_key_value_pair(); |
| 91 | keyValuePair->set_key(key); |
| 92 | keyValuePair->set_value_str(val); |
| 93 | */ |
| 94 | } |
| 95 | index++; |
| 96 | break; |
| 97 | case EVENT_TYPE_LONG: |
| 98 | if (index % 2 == 1) { |
| 99 | // TODO: Fix the following lines when we can serialize on the fly. |
| 100 | /* |
| 101 | int64_t val = elem.data.int64; |
| 102 | KeyValuePair* keyValuePair = eventMetricData.add_key_value_pair(); |
| 103 | keyValuePair->set_key(key); |
| 104 | keyValuePair->set_value_int(val); |
| 105 | */ |
| 106 | } |
| 107 | index++; |
| 108 | break; |
| 109 | case EVENT_TYPE_LIST: |
| 110 | break; |
| 111 | case EVENT_TYPE_LIST_STOP: |
| 112 | break; |
| 113 | case EVENT_TYPE_UNKNOWN: |
| 114 | break; |
| 115 | default: |
| 116 | elem.complete = true; |
| 117 | break; |
| 118 | } |
| 119 | |
| 120 | if (elem.complete) { |
| 121 | break; |
| 122 | } |
| 123 | } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete); |
| 124 | |
| 125 | android_log_destroy(&context); |
| 126 | } |
| 127 | |
| 128 | return eventMetricData; |
| 129 | } |
| 130 | |
| 131 | StatsdConfig buildFakeConfig() { |
| 132 | // HACK: Hard code a test metric for counting screen on events... |
| 133 | StatsdConfig config; |
| 134 | config.set_config_id(12345L); |
| 135 | |
| 136 | // One count metric to count screen on |
| 137 | CountMetric* metric = config.add_count_metric(); |
| 138 | metric->set_metric_id(20150717L); |
| 139 | metric->set_what("SCREEN_IS_ON"); |
| 140 | metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L); |
| 141 | |
| 142 | // One count metric to count PHOTO_CHANGE_OR_CHROME_CRASH |
| 143 | metric = config.add_count_metric(); |
| 144 | metric->set_metric_id(20150718L); |
| 145 | metric->set_what("PHOTO_PROCESS_STATE_CHANGE"); |
| 146 | metric->mutable_bucket()->set_bucket_size_millis(60 * 1000L); |
| 147 | metric->set_condition("SCREEN_IS_ON"); |
| 148 | |
| 149 | |
| 150 | LogEntryMatcher* eventMatcher = config.add_log_entry_matcher(); |
| 151 | eventMatcher->set_name("SCREEN_IS_ON"); |
| 152 | |
| 153 | SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
| 154 | simpleLogEntryMatcher->add_tag(2 /*SCREEN_STATE_CHANGE*/); |
| 155 | simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key( |
| 156 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/); |
| 157 | simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int( |
| 158 | 2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/); |
| 159 | |
| 160 | |
| 161 | |
| 162 | eventMatcher = config.add_log_entry_matcher(); |
| 163 | eventMatcher->set_name("SCREEN_IS_OFF"); |
| 164 | |
| 165 | simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
| 166 | simpleLogEntryMatcher->add_tag(2 /*SCREEN_STATE_CHANGE*/); |
| 167 | simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key( |
| 168 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/); |
| 169 | simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int( |
| 170 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_OFF*/); |
| 171 | |
| 172 | |
| 173 | |
| 174 | LogEntryMatcher* procEventMatcher = config.add_log_entry_matcher(); |
| 175 | procEventMatcher->set_name("PHOTO_CRASH"); |
| 176 | |
| 177 | SimpleLogEntryMatcher* simpleLogMatcher2 = procEventMatcher->mutable_simple_log_entry_matcher(); |
| 178 | simpleLogMatcher2->add_tag(1112 /*PROCESS_STATE_CHANGE*/); |
| 179 | KeyValueMatcher* keyValueMatcher = simpleLogMatcher2->add_key_value_matcher(); |
| 180 | keyValueMatcher->mutable_key_matcher()->set_key( |
| 181 | 1002 /*pkg*/); |
| 182 | keyValueMatcher->set_eq_string( |
| 183 | "com.google.android.apps.photos" /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/); |
| 184 | |
| 185 | keyValueMatcher = simpleLogMatcher2->add_key_value_matcher(); |
| 186 | keyValueMatcher->mutable_key_matcher()->set_key( |
| 187 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/); |
| 188 | keyValueMatcher->set_eq_int(2); |
| 189 | |
| 190 | |
| 191 | procEventMatcher = config.add_log_entry_matcher(); |
| 192 | procEventMatcher->set_name("PHOTO_START"); |
| 193 | |
| 194 | simpleLogMatcher2 = procEventMatcher->mutable_simple_log_entry_matcher(); |
| 195 | simpleLogMatcher2->add_tag(1112 /*PROCESS_STATE_CHANGE*/); |
| 196 | keyValueMatcher = simpleLogMatcher2->add_key_value_matcher(); |
| 197 | keyValueMatcher->mutable_key_matcher()->set_key( |
| 198 | 1002 /*pkg*/); |
| 199 | keyValueMatcher->set_eq_string( |
| 200 | "com.google.android.apps.photos" /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/); |
| 201 | |
| 202 | keyValueMatcher = simpleLogMatcher2->add_key_value_matcher(); |
| 203 | keyValueMatcher->mutable_key_matcher()->set_key( |
| 204 | 1 /*STATE*/); |
| 205 | keyValueMatcher->set_eq_int(1); |
| 206 | |
| 207 | |
| 208 | procEventMatcher = config.add_log_entry_matcher(); |
| 209 | procEventMatcher->set_name("PHOTO_PROCESS_STATE_CHANGE"); |
| 210 | LogEntryMatcher_Combination* combinationMatcher = procEventMatcher->mutable_combination(); |
| 211 | combinationMatcher->set_operation(LogicalOperation::OR); |
| 212 | combinationMatcher->add_matcher("PHOTO_START"); |
| 213 | combinationMatcher->add_matcher("PHOTO_CRASH"); |
| 214 | |
| 215 | |
| 216 | procEventMatcher = config.add_log_entry_matcher(); |
| 217 | procEventMatcher->set_name("CHROME_CRASH"); |
| 218 | |
| 219 | simpleLogMatcher2 = procEventMatcher->mutable_simple_log_entry_matcher(); |
| 220 | simpleLogMatcher2->add_tag(1112 /*PROCESS_STATE_CHANGE*/); |
| 221 | keyValueMatcher = simpleLogMatcher2->add_key_value_matcher(); |
| 222 | keyValueMatcher->mutable_key_matcher()->set_key( |
| 223 | 1002 /*pkg*/); |
| 224 | keyValueMatcher->set_eq_string( |
| 225 | "com.android.chrome" /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/); |
| 226 | |
| 227 | keyValueMatcher = simpleLogMatcher2->add_key_value_matcher(); |
| 228 | keyValueMatcher->mutable_key_matcher()->set_key( |
| 229 | 1 /*STATE*/); |
| 230 | keyValueMatcher->set_eq_int(2); |
| 231 | |
| 232 | |
| 233 | |
| 234 | procEventMatcher = config.add_log_entry_matcher(); |
| 235 | procEventMatcher->set_name("PHOTO_CHANGE_OR_CHROME_CRASH"); |
| 236 | combinationMatcher = procEventMatcher->mutable_combination(); |
| 237 | combinationMatcher->set_operation(LogicalOperation::OR); |
| 238 | combinationMatcher->add_matcher("PHOTO_PROCESS_STATE_CHANGE"); |
| 239 | combinationMatcher->add_matcher("CHROME_CRASH"); |
| 240 | |
| 241 | |
| 242 | |
| 243 | Condition* condition = config.add_condition(); |
| 244 | condition->set_name("SCREEN_IS_ON"); |
| 245 | SimpleCondition* simpleCondition = condition->mutable_simple_condition(); |
| 246 | simpleCondition->set_start("SCREEN_IS_ON"); |
| 247 | simpleCondition->set_stop("SCREEN_IS_OFF"); |
| 248 | |
| 249 | |
| 250 | condition = config.add_condition(); |
| 251 | condition->set_name("PHOTO_STARTED"); |
| 252 | |
| 253 | simpleCondition = condition->mutable_simple_condition(); |
| 254 | simpleCondition->set_start("PHOTO_START"); |
| 255 | simpleCondition->set_stop("PHOTO_CRASH"); |
| 256 | |
| 257 | |
| 258 | condition = config.add_condition(); |
| 259 | condition->set_name("SCREEN_IS_OFF"); |
| 260 | |
| 261 | simpleCondition = condition->mutable_simple_condition(); |
| 262 | simpleCondition->set_start("SCREEN_IS_OFF"); |
| 263 | simpleCondition->set_stop("SCREEN_IS_ON"); |
| 264 | |
| 265 | |
| 266 | condition = config.add_condition(); |
| 267 | condition->set_name("SCREEN_IS_EITHER_ON_OFF"); |
| 268 | |
| 269 | Condition_Combination* combination = condition->mutable_combination(); |
| 270 | combination->set_operation(LogicalOperation::OR); |
| 271 | combination->add_condition("SCREEN_IS_ON"); |
| 272 | combination->add_condition("SCREEN_IS_OFF"); |
| 273 | |
| 274 | |
| 275 | condition = config.add_condition(); |
| 276 | condition->set_name("SCREEN_IS_NEITHER_ON_OFF"); |
| 277 | |
| 278 | combination = condition->mutable_combination(); |
| 279 | combination->set_operation(LogicalOperation::NOR); |
| 280 | combination->add_condition("SCREEN_IS_ON"); |
| 281 | combination->add_condition("SCREEN_IS_OFF"); |
| 282 | |
| 283 | return config; |
| 284 | } |
| 285 | |
| 286 | |
| 287 | } // namespace statsd |
| 288 | } // namespace os |
| 289 | } // namespace android |