Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 "RecentEventLogger.h" |
| 18 | #include "SensorServiceUtils.h" |
| 19 | |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 20 | #include <android/util/ProtoOutputStream.h> |
| 21 | #include <frameworks/base/core/proto/android/service/sensor_service.proto.h> |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 22 | #include <utils/Timers.h> |
| 23 | |
| 24 | #include <inttypes.h> |
| 25 | |
| 26 | namespace android { |
| 27 | namespace SensorServiceUtil { |
| 28 | |
| 29 | namespace { |
| 30 | constexpr size_t LOG_SIZE = 10; |
Andrew Lehmer | aeef9d5 | 2018-09-24 13:23:16 -0700 | [diff] [blame] | 31 | constexpr size_t LOG_SIZE_MED = 30; // debugging for slower sensors |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 32 | constexpr size_t LOG_SIZE_LARGE = 50; // larger samples for debugging |
| 33 | }// unnamed namespace |
| 34 | |
| 35 | RecentEventLogger::RecentEventLogger(int sensorType) : |
| 36 | mSensorType(sensorType), mEventSize(eventSizeBySensorType(mSensorType)), |
Brian Stack | 0b858c1 | 2018-10-19 10:53:25 -0700 | [diff] [blame] | 37 | mRecentEvents(logSizeBySensorType(sensorType)), mMaskData(false), |
| 38 | mIsLastEventCurrent(false) { |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 39 | // blank |
| 40 | } |
| 41 | |
| 42 | void RecentEventLogger::addEvent(const sensors_event_t& event) { |
| 43 | std::lock_guard<std::mutex> lk(mLock); |
| 44 | mRecentEvents.emplace(event); |
Brian Stack | 0b858c1 | 2018-10-19 10:53:25 -0700 | [diff] [blame] | 45 | mIsLastEventCurrent = true; |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | bool RecentEventLogger::isEmpty() const { |
| 49 | return mRecentEvents.size() == 0; |
| 50 | } |
| 51 | |
Brian Stack | 0b858c1 | 2018-10-19 10:53:25 -0700 | [diff] [blame] | 52 | void RecentEventLogger::setLastEventStale() { |
| 53 | std::lock_guard<std::mutex> lk(mLock); |
| 54 | mIsLastEventCurrent = false; |
| 55 | } |
| 56 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 57 | std::string RecentEventLogger::dump() const { |
| 58 | std::lock_guard<std::mutex> lk(mLock); |
| 59 | |
| 60 | //TODO: replace String8 with std::string completely in this function |
| 61 | String8 buffer; |
| 62 | |
| 63 | buffer.appendFormat("last %zu events\n", mRecentEvents.size()); |
| 64 | int j = 0; |
| 65 | for (int i = mRecentEvents.size() - 1; i >= 0; --i) { |
| 66 | const auto& ev = mRecentEvents[i]; |
| 67 | struct tm * timeinfo = localtime(&(ev.mWallTime.tv_sec)); |
| 68 | buffer.appendFormat("\t%2d (ts=%.9f, wall=%02d:%02d:%02d.%03d) ", |
| 69 | ++j, ev.mEvent.timestamp/1e9, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, |
| 70 | (int) ns2ms(ev.mWallTime.tv_nsec)); |
| 71 | |
| 72 | // data |
Peng Xu | 2c238fc | 2016-09-08 12:36:59 -0700 | [diff] [blame] | 73 | if (!mMaskData) { |
| 74 | if (mSensorType == SENSOR_TYPE_STEP_COUNTER) { |
| 75 | buffer.appendFormat("%" PRIu64 ", ", ev.mEvent.u64.step_counter); |
| 76 | } else { |
| 77 | for (size_t k = 0; k < mEventSize; ++k) { |
| 78 | buffer.appendFormat("%.2f, ", ev.mEvent.data[k]); |
| 79 | } |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 80 | } |
Peng Xu | 2c238fc | 2016-09-08 12:36:59 -0700 | [diff] [blame] | 81 | } else { |
| 82 | buffer.append("[value masked]"); |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 83 | } |
| 84 | buffer.append("\n"); |
| 85 | } |
Tomasz Wasilczyk | 83aa0ab | 2023-08-11 00:06:51 +0000 | [diff] [blame] | 86 | return std::string(buffer.c_str()); |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 89 | /** |
| 90 | * Dump debugging information as android.service.SensorEventsProto protobuf message using |
| 91 | * ProtoOutputStream. |
| 92 | * |
| 93 | * See proto definition and some notes about ProtoOutputStream in |
| 94 | * frameworks/base/core/proto/android/service/sensor_service.proto |
| 95 | */ |
| 96 | void RecentEventLogger::dump(util::ProtoOutputStream* proto) const { |
| 97 | using namespace service::SensorEventsProto; |
| 98 | std::lock_guard<std::mutex> lk(mLock); |
| 99 | |
| 100 | proto->write(RecentEventsLog::RECENT_EVENTS_COUNT, int(mRecentEvents.size())); |
| 101 | for (int i = mRecentEvents.size() - 1; i >= 0; --i) { |
| 102 | const auto& ev = mRecentEvents[i]; |
| 103 | const uint64_t token = proto->start(RecentEventsLog::EVENTS); |
| 104 | proto->write(Event::TIMESTAMP_SEC, float(ev.mEvent.timestamp) / 1e9f); |
| 105 | proto->write(Event::WALL_TIMESTAMP_MS, ev.mWallTime.tv_sec * 1000LL |
| 106 | + ns2ms(ev.mWallTime.tv_nsec)); |
| 107 | |
| 108 | if (mMaskData) { |
| 109 | proto->write(Event::MASKED, true); |
| 110 | } else { |
| 111 | if (mSensorType == SENSOR_TYPE_STEP_COUNTER) { |
| 112 | proto->write(Event::INT64_DATA, int64_t(ev.mEvent.u64.step_counter)); |
| 113 | } else { |
| 114 | for (size_t k = 0; k < mEventSize; ++k) { |
| 115 | proto->write(Event::FLOAT_ARRAY, ev.mEvent.data[k]); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | proto->end(token); |
| 120 | } |
| 121 | } |
| 122 | |
Peng Xu | 2c238fc | 2016-09-08 12:36:59 -0700 | [diff] [blame] | 123 | void RecentEventLogger::setFormat(std::string format) { |
| 124 | if (format == "mask_data" ) { |
| 125 | mMaskData = true; |
| 126 | } else { |
| 127 | mMaskData = false; |
| 128 | } |
| 129 | } |
| 130 | |
Brian Stack | 0b858c1 | 2018-10-19 10:53:25 -0700 | [diff] [blame] | 131 | bool RecentEventLogger::populateLastEventIfCurrent(sensors_event_t *event) const { |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 132 | std::lock_guard<std::mutex> lk(mLock); |
| 133 | |
Brian Stack | 0b858c1 | 2018-10-19 10:53:25 -0700 | [diff] [blame] | 134 | if (mIsLastEventCurrent && mRecentEvents.size()) { |
Peng Xu | 45ca9f5 | 2016-05-02 14:15:50 -0700 | [diff] [blame] | 135 | // Index 0 contains the latest event emplace()'ed |
| 136 | *event = mRecentEvents[0].mEvent; |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 137 | return true; |
| 138 | } else { |
| 139 | return false; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | |
| 144 | size_t RecentEventLogger::logSizeBySensorType(int sensorType) { |
Andrew Lehmer | aeef9d5 | 2018-09-24 13:23:16 -0700 | [diff] [blame] | 145 | if (sensorType == SENSOR_TYPE_STEP_COUNTER || |
| 146 | sensorType == SENSOR_TYPE_SIGNIFICANT_MOTION || |
| 147 | sensorType == SENSOR_TYPE_ACCELEROMETER || |
| 148 | sensorType == SENSOR_TYPE_LIGHT) { |
| 149 | return LOG_SIZE_LARGE; |
| 150 | } |
| 151 | if (sensorType == SENSOR_TYPE_PROXIMITY) { |
| 152 | return LOG_SIZE_MED; |
| 153 | } |
| 154 | return LOG_SIZE; |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | RecentEventLogger::SensorEventLog::SensorEventLog(const sensors_event_t& e) : mEvent(e) { |
| 158 | clock_gettime(CLOCK_REALTIME, &mWallTime); |
| 159 | } |
| 160 | |
| 161 | } // namespace SensorServiceUtil |
| 162 | } // namespace android |