blob: 3d5b2abc7e3a12365ccf874e1b2c91e222c50f53 [file] [log] [blame]
Joe Onoratoc4dfae52017-10-17 23:38:21 -07001/*
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 Chen8a8d16c2018-02-08 14:50:40 -080019#include "FieldValue.h"
Joe Onoratoc4dfae52017-10-17 23:38:21 -070020
Howard Roa46b6582018-09-18 16:45:02 -070021#include <android/frameworks/stats/1.0/types.h>
Chenjie Yu12e5e672018-09-14 15:54:59 -070022#include <android/os/StatsLogEventWrapper.h>
Yao Chen5110bed2017-10-23 12:50:02 -070023#include <android/util/ProtoOutputStream.h>
Joe Onoratoc4dfae52017-10-17 23:38:21 -070024#include <log/log_event_list.h>
25#include <log/log_read.h>
Yao Chen80235402017-11-13 20:42:25 -080026#include <private/android_logger.h>
Yao Chen5110bed2017-10-23 12:50:02 -070027#include <utils/Errors.h>
Joe Onoratoc4dfae52017-10-17 23:38:21 -070028
29#include <string>
30#include <vector>
31
Howard Roa46b6582018-09-18 16:45:02 -070032using namespace android::frameworks::stats::V1_0;
33
Joe Onoratoc4dfae52017-10-17 23:38:21 -070034namespace android {
35namespace os {
36namespace statsd {
37
Yao Chen9c1debe2018-02-19 14:39:19 -080038struct 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};
Joe Onoratoc4dfae52017-10-17 23:38:21 -070058/**
59 * Wrapper for the log_msg structure.
60 */
61class LogEvent {
62public:
63 /**
64 * Read a LogEvent from a log_msg.
65 */
David Chena3bf0502017-11-01 16:57:43 -070066 explicit LogEvent(log_msg& msg);
Joe Onoratoc4dfae52017-10-17 23:38:21 -070067
Chenjie Yu12e5e672018-09-14 15:54:59 -070068 explicit LogEvent(const StatsLogEventWrapper& statsLogEventWrapper);
69
Joe Onoratoc4dfae52017-10-17 23:38:21 -070070 /**
Yao Chen80235402017-11-13 20:42:25 -080071 * Constructs a LogEvent with synthetic data for testing. Must call init() before reading.
Joe Onoratoc4dfae52017-10-17 23:38:21 -070072 */
Yangster-mac330af582018-02-08 15:24:38 -080073 explicit LogEvent(int32_t tagId, int64_t wallClockTimestampNs, int64_t elapsedTimestampNs);
74
75 // For testing. The timestamp is used as both elapsed real time and logd timestamp.
76 explicit LogEvent(int32_t tagId, int64_t timestampNs);
David Chen1481fe12017-10-16 13:16:34 -070077
Howard Ro1a2a3992018-10-22 22:51:57 -070078 // For testing. The timestamp is used as both elapsed real time and logd timestamp.
79 explicit LogEvent(int32_t tagId, int64_t timestampNs, int32_t uid);
80
Yangster-mac48b3d622018-08-18 12:38:11 -070081 /**
82 * Constructs a KeyValuePairsAtom LogEvent from value maps.
83 */
84 explicit LogEvent(int32_t tagId, int64_t wallClockTimestampNs, int64_t elapsedTimestampNs,
85 int32_t uid,
Howard Ro4078dd42018-09-27 17:41:08 -070086 const std::map<int32_t, int32_t>& int_map,
87 const std::map<int32_t, int64_t>& long_map,
Yangster-mac48b3d622018-08-18 12:38:11 -070088 const std::map<int32_t, std::string>& string_map,
89 const std::map<int32_t, float>& float_map);
90
Howard Roa46b6582018-09-18 16:45:02 -070091 explicit LogEvent(int64_t wallClockTimestampNs, int64_t elapsedTimestampNs,
92 const SpeakerImpedance& speakerImpedance);
93
94 explicit LogEvent(int64_t wallClockTimestampNs, int64_t elapsedTimestampNs,
95 const HardwareFailed& hardwareFailed);
96
97 explicit LogEvent(int64_t wallClockTimestampNs, int64_t elapsedTimestampNs,
98 const PhysicalDropDetected& physicalDropDetected);
99
100 explicit LogEvent(int64_t wallClockTimestampNs, int64_t elapsedTimestampNs,
101 const ChargeCycles& chargeCycles);
102
103 explicit LogEvent(int64_t wallClockTimestampNs, int64_t elapsedTimestampNs,
104 const BatteryHealthSnapshotArgs& batteryHealthSnapshotArgs);
105
106 explicit LogEvent(int64_t wallClockTimestampNs, int64_t elapsedTimestampNs,
107 const SlowIo& slowIo);
108
109 explicit LogEvent(int64_t wallClockTimestampNs, int64_t elapsedTimestampNs,
110 const BatteryCausedShutdown& batteryCausedShutdown);
111
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700112 ~LogEvent();
113
114 /**
115 * Get the timestamp associated with this event.
116 */
Yangster-mac330af582018-02-08 15:24:38 -0800117 inline int64_t GetLogdTimestampNs() const { return mLogdTimestampNs; }
118 inline int64_t GetElapsedTimestampNs() const { return mElapsedTimestampNs; }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700119
120 /**
121 * Get the tag for this event.
122 */
Yangster-mac68985802018-01-21 10:05:09 -0800123 inline int GetTagId() const { return mTagId; }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700124
Yangster-mac68985802018-01-21 10:05:09 -0800125 inline uint32_t GetUid() const {
Yao Chend10f7b12017-12-18 12:53:50 -0800126 return mLogUid;
127 }
128
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700129 /**
130 * Get the nth value, starting at 1.
131 *
132 * Returns BAD_INDEX if the index is larger than the number of elements.
133 * Returns BAD_TYPE if the index is available but the data is the wrong type.
134 */
135 int64_t GetLong(size_t key, status_t* err) const;
Chenjie Yu80f91122018-01-31 20:24:50 -0800136 int GetInt(size_t key, status_t* err) const;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700137 const char* GetString(size_t key, status_t* err) const;
138 bool GetBool(size_t key, status_t* err) const;
139 float GetFloat(size_t key, status_t* err) const;
140
141 /**
Yao Chen80235402017-11-13 20:42:25 -0800142 * Write test data to the LogEvent. This can only be used when the LogEvent is constructed
143 * using LogEvent(tagId, timestampNs). You need to call init() before you can read from it.
144 */
145 bool write(uint32_t value);
146 bool write(int32_t value);
147 bool write(uint64_t value);
148 bool write(int64_t value);
Yao Chen9c1debe2018-02-19 14:39:19 -0800149 bool write(const std::string& value);
Yao Chen80235402017-11-13 20:42:25 -0800150 bool write(float value);
Yao Chen9c1debe2018-02-19 14:39:19 -0800151 bool write(const std::vector<AttributionNodeInternal>& nodes);
152 bool write(const AttributionNodeInternal& node);
Howard Ro1a2a3992018-10-22 22:51:57 -0700153 bool writeKeyValuePairs(int32_t uid,
154 const std::map<int32_t, int32_t>& int_map,
Howard Ro4078dd42018-09-27 17:41:08 -0700155 const std::map<int32_t, int64_t>& long_map,
Yangster-mace124e422018-08-16 10:30:28 -0700156 const std::map<int32_t, std::string>& string_map,
157 const std::map<int32_t, float>& float_map);
Yao Chen80235402017-11-13 20:42:25 -0800158
159 /**
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700160 * Return a string representation of this event.
161 */
Yao Chen9c1debe2018-02-19 14:39:19 -0800162 std::string ToString() const;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700163
164 /**
Yao Chen5110bed2017-10-23 12:50:02 -0700165 * Write this object to a ProtoOutputStream.
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700166 */
Yao Chen5110bed2017-10-23 12:50:02 -0700167 void ToProto(android::util::ProtoOutputStream& out) const;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700168
David Chen1481fe12017-10-16 13:16:34 -0700169 /**
David Chen1481fe12017-10-16 13:16:34 -0700170 * Used with the constructor where tag is passed in. Converts the log_event_list to read mode
171 * and prepares the list for reading.
172 */
173 void init();
174
Chenjie Yua7259ab2017-12-10 08:31:05 -0800175 /**
Yangster-mac330af582018-02-08 15:24:38 -0800176 * Set elapsed timestamp if the original timestamp is missing.
Chenjie Yua7259ab2017-12-10 08:31:05 -0800177 */
Yangster-mac330af582018-02-08 15:24:38 -0800178 void setElapsedTimestampNs(int64_t timestampNs) {
179 mElapsedTimestampNs = timestampNs;
180 }
181
182 /**
183 * Set the timestamp if the original logd timestamp is missing.
184 */
185 void setLogdWallClockTimestampNs(int64_t timestampNs) {
186 mLogdTimestampNs = timestampNs;
187 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800188
Yangster-mac20877162017-12-22 17:19:39 -0800189 inline int size() const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800190 return mValues.size();
Chenjie Yud9dfda72017-12-11 17:41:20 -0800191 }
192
Yao Chen8a8d16c2018-02-08 14:50:40 -0800193 const std::vector<FieldValue>& getValues() const {
194 return mValues;
195 }
Yangster-macd40053e2018-01-09 16:29:22 -0800196
Yao Chen8a8d16c2018-02-08 14:50:40 -0800197 std::vector<FieldValue>* getMutableValues() {
198 return &mValues;
199 }
Yangster-mac20877162017-12-22 17:19:39 -0800200
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700201private:
202 /**
203 * Don't copy, it's slower. If we really need this we can add it but let's try to
204 * avoid it.
205 */
206 explicit LogEvent(const LogEvent&);
207
208 /**
209 * Parses a log_msg into a LogEvent object.
210 */
Yao Chen80235402017-11-13 20:42:25 -0800211 void init(android_log_context context);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700212
Yao Chen8a8d16c2018-02-08 14:50:40 -0800213 // The items are naturally sorted in DFS order as we read them. this allows us to do fast
214 // matching.
215 std::vector<FieldValue> mValues;
Yao Chen80235402017-11-13 20:42:25 -0800216
Yangster-mac20877162017-12-22 17:19:39 -0800217 // This field is used when statsD wants to create log event object and write fields to it. After
218 // calling init() function, this object would be destroyed to save memory usage.
219 // When the log event is created from log msg, this field is never initiated.
Yao Chen48d75182018-01-23 09:40:48 -0800220 android_log_context mContext = NULL;
Yao Chen93fe3a32017-11-02 13:52:59 -0700221
Yangster-mac330af582018-02-08 15:24:38 -0800222 // The timestamp set by the logd.
223 int64_t mLogdTimestampNs;
224
225 // The elapsed timestamp set by statsd log writer.
226 int64_t mElapsedTimestampNs;
Yao Chen93fe3a32017-11-02 13:52:59 -0700227
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700228 int mTagId;
Yao Chend10f7b12017-12-18 12:53:50 -0800229
230 uint32_t mLogUid;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700231};
232
233} // namespace statsd
234} // namespace os
235} // namespace android
236