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 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 21 | #include <android/os/StatsLogEventWrapper.h> |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 22 | #include <android/util/ProtoOutputStream.h> |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 23 | #include <log/log_event_list.h> |
| 24 | #include <log/log_read.h> |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 25 | #include <private/android_logger.h> |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 26 | #include <utils/Errors.h> |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 27 | |
| 28 | #include <string> |
| 29 | #include <vector> |
| 30 | |
| 31 | namespace android { |
| 32 | namespace os { |
| 33 | namespace statsd { |
| 34 | |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 35 | struct AttributionNodeInternal { |
| 36 | void set_uid(int32_t id) { |
| 37 | mUid = id; |
| 38 | } |
| 39 | |
| 40 | void set_tag(const std::string& value) { |
| 41 | mTag = value; |
| 42 | } |
| 43 | |
| 44 | int32_t uid() const { |
| 45 | return mUid; |
| 46 | } |
| 47 | |
| 48 | const std::string& tag() const { |
| 49 | return mTag; |
| 50 | } |
| 51 | |
| 52 | int32_t mUid; |
| 53 | std::string mTag; |
| 54 | }; |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 55 | /** |
| 56 | * Wrapper for the log_msg structure. |
| 57 | */ |
| 58 | class LogEvent { |
| 59 | public: |
| 60 | /** |
| 61 | * Read a LogEvent from a log_msg. |
| 62 | */ |
David Chen | a3bf050 | 2017-11-01 16:57:43 -0700 | [diff] [blame] | 63 | explicit LogEvent(log_msg& msg); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 64 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 65 | explicit LogEvent(const StatsLogEventWrapper& statsLogEventWrapper); |
| 66 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 67 | /** |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 68 | * 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] | 69 | */ |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 70 | explicit LogEvent(int32_t tagId, int64_t wallClockTimestampNs, int64_t elapsedTimestampNs); |
| 71 | |
| 72 | // For testing. The timestamp is used as both elapsed real time and logd timestamp. |
| 73 | explicit LogEvent(int32_t tagId, int64_t timestampNs); |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 74 | |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 75 | /** |
| 76 | * Constructs a KeyValuePairsAtom LogEvent from value maps. |
| 77 | */ |
| 78 | explicit LogEvent(int32_t tagId, int64_t wallClockTimestampNs, int64_t elapsedTimestampNs, |
| 79 | int32_t uid, |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame^] | 80 | const std::map<int32_t, int32_t>& int_map, |
| 81 | const std::map<int32_t, int64_t>& long_map, |
Yangster-mac | 48b3d62 | 2018-08-18 12:38:11 -0700 | [diff] [blame] | 82 | const std::map<int32_t, std::string>& string_map, |
| 83 | const std::map<int32_t, float>& float_map); |
| 84 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 85 | ~LogEvent(); |
| 86 | |
| 87 | /** |
| 88 | * Get the timestamp associated with this event. |
| 89 | */ |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 90 | inline int64_t GetLogdTimestampNs() const { return mLogdTimestampNs; } |
| 91 | inline int64_t GetElapsedTimestampNs() const { return mElapsedTimestampNs; } |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * Get the tag for this event. |
| 95 | */ |
Yangster-mac | 6898580 | 2018-01-21 10:05:09 -0800 | [diff] [blame] | 96 | inline int GetTagId() const { return mTagId; } |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 97 | |
Yangster-mac | 6898580 | 2018-01-21 10:05:09 -0800 | [diff] [blame] | 98 | inline uint32_t GetUid() const { |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 99 | return mLogUid; |
| 100 | } |
| 101 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 102 | /** |
| 103 | * Get the nth value, starting at 1. |
| 104 | * |
| 105 | * Returns BAD_INDEX if the index is larger than the number of elements. |
| 106 | * Returns BAD_TYPE if the index is available but the data is the wrong type. |
| 107 | */ |
| 108 | int64_t GetLong(size_t key, status_t* err) const; |
Chenjie Yu | 80f9112 | 2018-01-31 20:24:50 -0800 | [diff] [blame] | 109 | int GetInt(size_t key, status_t* err) const; |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 110 | const char* GetString(size_t key, status_t* err) const; |
| 111 | bool GetBool(size_t key, status_t* err) const; |
| 112 | float GetFloat(size_t key, status_t* err) const; |
| 113 | |
| 114 | /** |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 115 | * Write test data to the LogEvent. This can only be used when the LogEvent is constructed |
| 116 | * using LogEvent(tagId, timestampNs). You need to call init() before you can read from it. |
| 117 | */ |
| 118 | bool write(uint32_t value); |
| 119 | bool write(int32_t value); |
| 120 | bool write(uint64_t value); |
| 121 | bool write(int64_t value); |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 122 | bool write(const std::string& value); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 123 | bool write(float value); |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 124 | bool write(const std::vector<AttributionNodeInternal>& nodes); |
| 125 | bool write(const AttributionNodeInternal& node); |
Howard Ro | 4078dd4 | 2018-09-27 17:41:08 -0700 | [diff] [blame^] | 126 | bool writeKeyValuePairs(const std::map<int32_t, int32_t>& int_map, |
| 127 | const std::map<int32_t, int64_t>& long_map, |
Yangster-mac | e124e42 | 2018-08-16 10:30:28 -0700 | [diff] [blame] | 128 | const std::map<int32_t, std::string>& string_map, |
| 129 | const std::map<int32_t, float>& float_map); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 130 | |
| 131 | /** |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 132 | * Return a string representation of this event. |
| 133 | */ |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 134 | std::string ToString() const; |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 135 | |
| 136 | /** |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 137 | * Write this object to a ProtoOutputStream. |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 138 | */ |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 139 | void ToProto(android::util::ProtoOutputStream& out) const; |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 140 | |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 141 | /** |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 142 | * Used with the constructor where tag is passed in. Converts the log_event_list to read mode |
| 143 | * and prepares the list for reading. |
| 144 | */ |
| 145 | void init(); |
| 146 | |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 147 | /** |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 148 | * Set elapsed timestamp if the original timestamp is missing. |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 149 | */ |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 150 | void setElapsedTimestampNs(int64_t timestampNs) { |
| 151 | mElapsedTimestampNs = timestampNs; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Set the timestamp if the original logd timestamp is missing. |
| 156 | */ |
| 157 | void setLogdWallClockTimestampNs(int64_t timestampNs) { |
| 158 | mLogdTimestampNs = timestampNs; |
| 159 | } |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 160 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 161 | inline int size() const { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 162 | return mValues.size(); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 165 | const std::vector<FieldValue>& getValues() const { |
| 166 | return mValues; |
| 167 | } |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 168 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 169 | std::vector<FieldValue>* getMutableValues() { |
| 170 | return &mValues; |
| 171 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 172 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 173 | private: |
| 174 | /** |
| 175 | * Don't copy, it's slower. If we really need this we can add it but let's try to |
| 176 | * avoid it. |
| 177 | */ |
| 178 | explicit LogEvent(const LogEvent&); |
| 179 | |
| 180 | /** |
| 181 | * Parses a log_msg into a LogEvent object. |
| 182 | */ |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 183 | void init(android_log_context context); |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 184 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 185 | // The items are naturally sorted in DFS order as we read them. this allows us to do fast |
| 186 | // matching. |
| 187 | std::vector<FieldValue> mValues; |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 188 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 189 | // This field is used when statsD wants to create log event object and write fields to it. After |
| 190 | // calling init() function, this object would be destroyed to save memory usage. |
| 191 | // 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] | 192 | android_log_context mContext = NULL; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 193 | |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 194 | // The timestamp set by the logd. |
| 195 | int64_t mLogdTimestampNs; |
| 196 | |
| 197 | // The elapsed timestamp set by statsd log writer. |
| 198 | int64_t mElapsedTimestampNs; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 199 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 200 | int mTagId; |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 201 | |
| 202 | uint32_t mLogUid; |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | } // namespace statsd |
| 206 | } // namespace os |
| 207 | } // namespace android |
| 208 | |