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 | |
| 17 | #pragma once |
| 18 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 19 | #include "FieldValue.h" |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 20 | |
Howard Ro | a46b658 | 2018-09-18 16:45:02 -0700 | [diff] [blame] | 21 | #include <android/frameworks/stats/1.0/types.h> |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 22 | #include <android/os/StatsLogEventWrapper.h> |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 23 | #include <android/util/ProtoOutputStream.h> |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 24 | #include <log/log_event_list.h> |
| 25 | #include <log/log_read.h> |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 26 | #include <private/android_logger.h> |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 27 | #include <utils/Errors.h> |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 28 | |
| 29 | #include <string> |
| 30 | #include <vector> |
| 31 | |
Howard Ro | a46b658 | 2018-09-18 16:45:02 -0700 | [diff] [blame] | 32 | using namespace android::frameworks::stats::V1_0; |
| 33 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 34 | namespace android { |
| 35 | namespace os { |
| 36 | namespace statsd { |
| 37 | |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 38 | struct AttributionNodeInternal { |
| 39 | void set_uid(int32_t id) { |
| 40 | mUid = id; |
| 41 | } |
| 42 | |
| 43 | void set_tag(const std::string& value) { |
| 44 | mTag = value; |
| 45 | } |
| 46 | |
| 47 | int32_t uid() const { |
| 48 | return mUid; |
| 49 | } |
| 50 | |
| 51 | const std::string& tag() const { |
| 52 | return mTag; |
| 53 | } |
| 54 | |
| 55 | int32_t mUid; |
| 56 | std::string mTag; |
| 57 | }; |
Chenjie Yu | 97dbb20 | 2019-02-13 16:42:04 -0800 | [diff] [blame] | 58 | |
| 59 | struct InstallTrainInfo { |
| 60 | int64_t trainVersionCode; |
| 61 | std::vector<uint8_t> experimentIds; |
| 62 | }; |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 63 | /** |
| 64 | * Wrapper for the log_msg structure. |
| 65 | */ |
| 66 | class LogEvent { |
| 67 | public: |
| 68 | /** |
| 69 | * Read a LogEvent from a log_msg. |
| 70 | */ |
David Chen | a3bf050 | 2017-11-01 16:57:43 -0700 | [diff] [blame] | 71 | explicit LogEvent(log_msg& msg); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 72 | |
Chenjie Yu | d7e3a22 | 2018-11-28 21:29:44 +0000 | [diff] [blame] | 73 | /** |
| 74 | * Creates LogEvent from StatsLogEventWrapper. |
| 75 | */ |
| 76 | static void createLogEvents(const StatsLogEventWrapper& statsLogEventWrapper, |
| 77 | std::vector<std::shared_ptr<LogEvent>>& logEvents); |
| 78 | |
| 79 | /** |
| 80 | * Construct one LogEvent from a StatsLogEventWrapper with the i-th work chain. -1 if no chain. |
| 81 | */ |
| 82 | explicit LogEvent(const StatsLogEventWrapper& statsLogEventWrapper, int workChainIndex); |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 83 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 84 | /** |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 85 | * Constructs a LogEvent with synthetic data for testing. Must call init() before reading. |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 86 | */ |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 87 | explicit LogEvent(int32_t tagId, int64_t wallClockTimestampNs, int64_t elapsedTimestampNs); |
| 88 | |
| 89 | // For testing. The timestamp is used as both elapsed real time and logd timestamp. |
| 90 | explicit LogEvent(int32_t tagId, int64_t timestampNs); |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 91 | |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 92 | // For testing. The timestamp is used as both elapsed real time and logd timestamp. |
| 93 | explicit LogEvent(int32_t tagId, int64_t timestampNs, int32_t uid); |
| 94 | |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 95 | /** |
| 96 | * Constructs a KeyValuePairsAtom LogEvent from value maps. |
| 97 | */ |
| 98 | explicit LogEvent(int32_t tagId, int64_t wallClockTimestampNs, int64_t elapsedTimestampNs, |
| 99 | int32_t uid, |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 100 | const std::map<int32_t, int32_t>& int_map, |
| 101 | const std::map<int32_t, int64_t>& long_map, |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 102 | const std::map<int32_t, std::string>& string_map, |
| 103 | const std::map<int32_t, float>& float_map); |
| 104 | |
Chenjie Yu | 6b1667c | 2019-01-18 10:09:33 -0800 | [diff] [blame] | 105 | // Constructs a BinaryPushStateChanged LogEvent from API call. |
| 106 | explicit LogEvent(const std::string& trainName, int64_t trainVersionCode, bool requiresStaging, |
| 107 | bool rollbackEnabled, bool requiresLowLatencyMonitor, int32_t state, |
| 108 | const std::vector<uint8_t>& experimentIds, int32_t userId); |
| 109 | |
Howard Ro | a46b658 | 2018-09-18 16:45:02 -0700 | [diff] [blame] | 110 | explicit LogEvent(int64_t wallClockTimestampNs, int64_t elapsedTimestampNs, |
| 111 | const SpeakerImpedance& speakerImpedance); |
| 112 | |
| 113 | explicit LogEvent(int64_t wallClockTimestampNs, int64_t elapsedTimestampNs, |
| 114 | const HardwareFailed& hardwareFailed); |
| 115 | |
| 116 | explicit LogEvent(int64_t wallClockTimestampNs, int64_t elapsedTimestampNs, |
| 117 | const PhysicalDropDetected& physicalDropDetected); |
| 118 | |
| 119 | explicit LogEvent(int64_t wallClockTimestampNs, int64_t elapsedTimestampNs, |
| 120 | const ChargeCycles& chargeCycles); |
| 121 | |
| 122 | explicit LogEvent(int64_t wallClockTimestampNs, int64_t elapsedTimestampNs, |
| 123 | const BatteryHealthSnapshotArgs& batteryHealthSnapshotArgs); |
| 124 | |
| 125 | explicit LogEvent(int64_t wallClockTimestampNs, int64_t elapsedTimestampNs, |
| 126 | const SlowIo& slowIo); |
| 127 | |
| 128 | explicit LogEvent(int64_t wallClockTimestampNs, int64_t elapsedTimestampNs, |
| 129 | const BatteryCausedShutdown& batteryCausedShutdown); |
| 130 | |
Maggie White | fc1aa59 | 2018-11-28 21:55:23 -0800 | [diff] [blame] | 131 | explicit LogEvent(int64_t wallClockTimestampNs, int64_t elapsedTimestampNs, |
| 132 | const UsbPortOverheatEvent& usbPortOverheatEvent); |
| 133 | |
Carter Hsu | b8fd1e9 | 2019-01-11 15:24:45 +0800 | [diff] [blame] | 134 | explicit LogEvent(int64_t wallClockTimestampNs, int64_t elapsedTimestampNs, |
| 135 | const SpeechDspStat& speechDspStat); |
| 136 | |
Maggie White | 58174da | 2019-01-18 15:23:35 -0800 | [diff] [blame] | 137 | explicit LogEvent(int64_t wallClockTimestampNs, int64_t elapsedTimestampNs, |
| 138 | const VendorAtom& vendorAtom); |
| 139 | |
Chenjie Yu | 97dbb20 | 2019-02-13 16:42:04 -0800 | [diff] [blame] | 140 | explicit LogEvent(int64_t wallClockTimestampNs, int64_t elapsedTimestampNs, |
| 141 | const InstallTrainInfo& installTrainInfo); |
| 142 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 143 | ~LogEvent(); |
| 144 | |
| 145 | /** |
| 146 | * Get the timestamp associated with this event. |
| 147 | */ |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 148 | inline int64_t GetLogdTimestampNs() const { return mLogdTimestampNs; } |
| 149 | inline int64_t GetElapsedTimestampNs() const { return mElapsedTimestampNs; } |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 150 | |
| 151 | /** |
| 152 | * Get the tag for this event. |
| 153 | */ |
Yangster-mac | 6898580 | 2018-01-21 10:05:09 -0800 | [diff] [blame] | 154 | inline int GetTagId() const { return mTagId; } |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 155 | |
Yangster-mac | 6898580 | 2018-01-21 10:05:09 -0800 | [diff] [blame] | 156 | inline uint32_t GetUid() const { |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 157 | return mLogUid; |
| 158 | } |
| 159 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 160 | /** |
| 161 | * Get the nth value, starting at 1. |
| 162 | * |
| 163 | * Returns BAD_INDEX if the index is larger than the number of elements. |
| 164 | * Returns BAD_TYPE if the index is available but the data is the wrong type. |
| 165 | */ |
| 166 | int64_t GetLong(size_t key, status_t* err) const; |
Chenjie Yu | 80f9112 | 2018-01-31 20:24:50 -0800 | [diff] [blame] | 167 | int GetInt(size_t key, status_t* err) const; |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 168 | const char* GetString(size_t key, status_t* err) const; |
| 169 | bool GetBool(size_t key, status_t* err) const; |
| 170 | float GetFloat(size_t key, status_t* err) const; |
| 171 | |
| 172 | /** |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 173 | * Write test data to the LogEvent. This can only be used when the LogEvent is constructed |
| 174 | * using LogEvent(tagId, timestampNs). You need to call init() before you can read from it. |
| 175 | */ |
| 176 | bool write(uint32_t value); |
| 177 | bool write(int32_t value); |
| 178 | bool write(uint64_t value); |
| 179 | bool write(int64_t value); |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 180 | bool write(const std::string& value); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 181 | bool write(float value); |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 182 | bool write(const std::vector<AttributionNodeInternal>& nodes); |
| 183 | bool write(const AttributionNodeInternal& node); |
Howard Ro | 1a2a399 | 2018-10-22 22:51:57 -0700 | [diff] [blame] | 184 | bool writeKeyValuePairs(int32_t uid, |
| 185 | const std::map<int32_t, int32_t>& int_map, |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame] | 186 | const std::map<int32_t, int64_t>& long_map, |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 187 | const std::map<int32_t, std::string>& string_map, |
| 188 | const std::map<int32_t, float>& float_map); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 189 | |
| 190 | /** |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 191 | * Return a string representation of this event. |
| 192 | */ |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 193 | std::string ToString() const; |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 194 | |
| 195 | /** |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 196 | * Write this object to a ProtoOutputStream. |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 197 | */ |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 198 | void ToProto(android::util::ProtoOutputStream& out) const; |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 199 | |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 200 | /** |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 201 | * Used with the constructor where tag is passed in. Converts the log_event_list to read mode |
| 202 | * and prepares the list for reading. |
| 203 | */ |
| 204 | void init(); |
| 205 | |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 206 | /** |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 207 | * Set elapsed timestamp if the original timestamp is missing. |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 208 | */ |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 209 | void setElapsedTimestampNs(int64_t timestampNs) { |
| 210 | mElapsedTimestampNs = timestampNs; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Set the timestamp if the original logd timestamp is missing. |
| 215 | */ |
| 216 | void setLogdWallClockTimestampNs(int64_t timestampNs) { |
| 217 | mLogdTimestampNs = timestampNs; |
| 218 | } |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 219 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 220 | inline int size() const { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 221 | return mValues.size(); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 224 | const std::vector<FieldValue>& getValues() const { |
| 225 | return mValues; |
| 226 | } |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 227 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 228 | std::vector<FieldValue>* getMutableValues() { |
| 229 | return &mValues; |
| 230 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 231 | |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 232 | inline LogEvent makeCopy() { |
| 233 | return LogEvent(*this); |
| 234 | } |
| 235 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 236 | private: |
| 237 | /** |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 238 | * Only use this if copy is absolutely needed. |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 239 | */ |
Chih-Hung Hsieh | 3227aab | 2018-12-20 13:42:28 -0800 | [diff] [blame] | 240 | LogEvent(const LogEvent&); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 241 | |
| 242 | /** |
| 243 | * Parses a log_msg into a LogEvent object. |
| 244 | */ |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 245 | void init(android_log_context context); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 246 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 247 | // The items are naturally sorted in DFS order as we read them. this allows us to do fast |
| 248 | // matching. |
| 249 | std::vector<FieldValue> mValues; |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 250 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 251 | // This field is used when statsD wants to create log event object and write fields to it. After |
| 252 | // calling init() function, this object would be destroyed to save memory usage. |
| 253 | // When the log event is created from log msg, this field is never initiated. |
Yao Chen | 48d7518 | 2018-01-23 09:40:48 -0800 | [diff] [blame] | 254 | android_log_context mContext = NULL; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 255 | |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 256 | // The timestamp set by the logd. |
| 257 | int64_t mLogdTimestampNs; |
| 258 | |
| 259 | // The elapsed timestamp set by statsd log writer. |
| 260 | int64_t mElapsedTimestampNs; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 261 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 262 | int mTagId; |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 263 | |
| 264 | uint32_t mLogUid; |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 265 | }; |
| 266 | |
| 267 | } // namespace statsd |
| 268 | } // namespace os |
| 269 | } // namespace android |
| 270 | |