Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2023 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 | #define LOG_TAG "InputDeviceMetricsCollector" |
| 18 | #include "InputDeviceMetricsCollector.h" |
| 19 | |
Asmita Poddar | 631a14e | 2023-10-03 10:22:07 +0000 | [diff] [blame] | 20 | #include "InputDeviceMetricsSource.h" |
Prabir Pradhan | ae10ee6 | 2023-05-12 19:44:18 +0000 | [diff] [blame] | 21 | |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 22 | #include <android-base/stringprintf.h> |
| 23 | #include <input/PrintTools.h> |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 24 | |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 25 | namespace android { |
| 26 | |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 27 | using android::base::StringPrintf; |
| 28 | using std::chrono::nanoseconds; |
Prabir Pradhan | 4429379 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 29 | using std::chrono_literals::operator""ns; |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 30 | |
| 31 | namespace { |
| 32 | |
Prabir Pradhan | 7adabad | 2023-06-28 16:16:27 +0000 | [diff] [blame] | 33 | constexpr nanoseconds DEFAULT_USAGE_SESSION_TIMEOUT = std::chrono::minutes(2); |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Log debug messages about metrics events logged to statsd. |
| 37 | * Enable this via "adb shell setprop log.tag.InputDeviceMetricsCollector DEBUG" (requires restart) |
| 38 | */ |
| 39 | const bool DEBUG = __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG, ANDROID_LOG_INFO); |
| 40 | |
Prabir Pradhan | 047695b | 2023-06-30 01:48:45 +0000 | [diff] [blame] | 41 | constexpr size_t INTERACTIONS_QUEUE_CAPACITY = 500; |
| 42 | |
Prabir Pradhan | 67d09ca | 2023-09-08 20:28:55 +0000 | [diff] [blame] | 43 | int32_t linuxBusToInputDeviceBusEnum(int32_t linuxBus, bool isUsiStylus) { |
| 44 | if (isUsiStylus) { |
| 45 | // This is a stylus connected over the Universal Stylus Initiative (USI) protocol. |
| 46 | // For metrics purposes, we treat this protocol as a separate bus. |
| 47 | return util::INPUT_DEVICE_USAGE_REPORTED__DEVICE_BUS__USI; |
| 48 | } |
| 49 | |
Harry Cutts | a34de52 | 2023-06-06 15:52:54 +0000 | [diff] [blame] | 50 | // When adding cases to this switch, also add them to the copy of this method in |
| 51 | // TouchpadInputMapper.cpp. |
| 52 | // TODO(b/286394420): deduplicate this method with the one in TouchpadInputMapper.cpp. |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 53 | switch (linuxBus) { |
| 54 | case BUS_USB: |
| 55 | return util::INPUT_DEVICE_USAGE_REPORTED__DEVICE_BUS__USB; |
| 56 | case BUS_BLUETOOTH: |
| 57 | return util::INPUT_DEVICE_USAGE_REPORTED__DEVICE_BUS__BLUETOOTH; |
| 58 | default: |
| 59 | return util::INPUT_DEVICE_USAGE_REPORTED__DEVICE_BUS__OTHER; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | class : public InputDeviceMetricsLogger { |
| 64 | nanoseconds getCurrentTime() override { return nanoseconds(systemTime(SYSTEM_TIME_MONOTONIC)); } |
| 65 | |
Prabir Pradhan | 0dd48ae | 2023-09-08 21:33:51 +0000 | [diff] [blame] | 66 | void logInputDeviceUsageReported(const MetricsDeviceInfo& info, |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 67 | const DeviceUsageReport& report) override { |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 68 | const int32_t durationMillis = |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 69 | std::chrono::duration_cast<std::chrono::milliseconds>(report.usageDuration).count(); |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 70 | const static std::vector<int32_t> empty; |
| 71 | |
Prabir Pradhan | 0dd48ae | 2023-09-08 21:33:51 +0000 | [diff] [blame] | 72 | ALOGD_IF(DEBUG, "Usage session reported for device id: %d", info.deviceId); |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 73 | ALOGD_IF(DEBUG, " Total duration: %dms", durationMillis); |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 74 | ALOGD_IF(DEBUG, " Source breakdown:"); |
| 75 | |
| 76 | std::vector<int32_t> sources; |
| 77 | std::vector<int32_t> durationsPerSource; |
| 78 | for (auto& [src, dur] : report.sourceBreakdown) { |
| 79 | sources.push_back(ftl::to_underlying(src)); |
| 80 | int32_t durMillis = std::chrono::duration_cast<std::chrono::milliseconds>(dur).count(); |
| 81 | durationsPerSource.emplace_back(durMillis); |
| 82 | ALOGD_IF(DEBUG, " - usageSource: %s\t duration: %dms", |
| 83 | ftl::enum_string(src).c_str(), durMillis); |
| 84 | } |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 85 | |
Prabir Pradhan | 4429379 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 86 | ALOGD_IF(DEBUG, " Uid breakdown:"); |
| 87 | |
| 88 | std::vector<int32_t> uids; |
| 89 | std::vector<int32_t> durationsPerUid; |
| 90 | for (auto& [uid, dur] : report.uidBreakdown) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 91 | uids.push_back(uid.val()); |
Prabir Pradhan | 4429379 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 92 | int32_t durMillis = std::chrono::duration_cast<std::chrono::milliseconds>(dur).count(); |
| 93 | durationsPerUid.push_back(durMillis); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 94 | ALOGD_IF(DEBUG, " - uid: %s\t duration: %dms", uid.toString().c_str(), |
| 95 | durMillis); |
Prabir Pradhan | 4429379 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 96 | } |
Prabir Pradhan | 0dd48ae | 2023-09-08 21:33:51 +0000 | [diff] [blame] | 97 | util::stats_write(util::INPUTDEVICE_USAGE_REPORTED, info.vendor, info.product, info.version, |
| 98 | linuxBusToInputDeviceBusEnum(info.bus, info.isUsiStylus), durationMillis, |
| 99 | sources, durationsPerSource, uids, durationsPerUid); |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 100 | } |
| 101 | } sStatsdLogger; |
| 102 | |
| 103 | bool isIgnoredInputDeviceId(int32_t deviceId) { |
| 104 | switch (deviceId) { |
| 105 | case INVALID_INPUT_DEVICE_ID: |
| 106 | case VIRTUAL_KEYBOARD_ID: |
| 107 | return true; |
| 108 | default: |
| 109 | return false; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | } // namespace |
| 114 | |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 115 | InputDeviceMetricsCollector::InputDeviceMetricsCollector(InputListenerInterface& listener) |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 116 | : InputDeviceMetricsCollector(listener, sStatsdLogger, DEFAULT_USAGE_SESSION_TIMEOUT) {} |
| 117 | |
| 118 | InputDeviceMetricsCollector::InputDeviceMetricsCollector(InputListenerInterface& listener, |
| 119 | InputDeviceMetricsLogger& logger, |
| 120 | nanoseconds usageSessionTimeout) |
Prabir Pradhan | 047695b | 2023-06-30 01:48:45 +0000 | [diff] [blame] | 121 | : mNextListener(listener), |
| 122 | mLogger(logger), |
| 123 | mUsageSessionTimeout(usageSessionTimeout), |
| 124 | mInteractionsQueue(INTERACTIONS_QUEUE_CAPACITY) {} |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 125 | |
| 126 | void InputDeviceMetricsCollector::notifyInputDevicesChanged( |
| 127 | const NotifyInputDevicesChangedArgs& args) { |
Prabir Pradhan | 95019ac | 2023-12-06 22:38:21 +0000 | [diff] [blame] | 128 | { |
| 129 | std::scoped_lock lock(mLock); |
| 130 | reportCompletedSessions(); |
| 131 | onInputDevicesChanged(args.inputDeviceInfos); |
| 132 | } |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 133 | mNextListener.notify(args); |
| 134 | } |
| 135 | |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 136 | void InputDeviceMetricsCollector::notifyKey(const NotifyKeyArgs& args) { |
Prabir Pradhan | 95019ac | 2023-12-06 22:38:21 +0000 | [diff] [blame] | 137 | { |
| 138 | std::scoped_lock lock(mLock); |
| 139 | reportCompletedSessions(); |
| 140 | const SourceProvider getSources = [&args](const MetricsDeviceInfo& info) { |
| 141 | return std::set{getUsageSourceForKeyArgs(info.keyboardType, args)}; |
| 142 | }; |
| 143 | onInputDeviceUsage(DeviceId{args.deviceId}, nanoseconds(args.eventTime), getSources); |
| 144 | } |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 145 | mNextListener.notify(args); |
| 146 | } |
| 147 | |
| 148 | void InputDeviceMetricsCollector::notifyMotion(const NotifyMotionArgs& args) { |
Prabir Pradhan | 95019ac | 2023-12-06 22:38:21 +0000 | [diff] [blame] | 149 | { |
| 150 | std::scoped_lock lock(mLock); |
| 151 | reportCompletedSessions(); |
| 152 | onInputDeviceUsage(DeviceId{args.deviceId}, nanoseconds(args.eventTime), |
| 153 | [&args](const auto&) { return getUsageSourcesForMotionArgs(args); }); |
| 154 | } |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 155 | |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 156 | mNextListener.notify(args); |
| 157 | } |
| 158 | |
| 159 | void InputDeviceMetricsCollector::notifySwitch(const NotifySwitchArgs& args) { |
Prabir Pradhan | 95019ac | 2023-12-06 22:38:21 +0000 | [diff] [blame] | 160 | { |
| 161 | std::scoped_lock lock(mLock); |
| 162 | reportCompletedSessions(); |
| 163 | } |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 164 | mNextListener.notify(args); |
| 165 | } |
| 166 | |
| 167 | void InputDeviceMetricsCollector::notifySensor(const NotifySensorArgs& args) { |
Prabir Pradhan | 95019ac | 2023-12-06 22:38:21 +0000 | [diff] [blame] | 168 | { |
| 169 | std::scoped_lock lock(mLock); |
| 170 | reportCompletedSessions(); |
| 171 | } |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 172 | mNextListener.notify(args); |
| 173 | } |
| 174 | |
| 175 | void InputDeviceMetricsCollector::notifyVibratorState(const NotifyVibratorStateArgs& args) { |
Prabir Pradhan | 95019ac | 2023-12-06 22:38:21 +0000 | [diff] [blame] | 176 | { |
| 177 | std::scoped_lock lock(mLock); |
| 178 | reportCompletedSessions(); |
| 179 | } |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 180 | mNextListener.notify(args); |
| 181 | } |
| 182 | |
| 183 | void InputDeviceMetricsCollector::notifyDeviceReset(const NotifyDeviceResetArgs& args) { |
Prabir Pradhan | 95019ac | 2023-12-06 22:38:21 +0000 | [diff] [blame] | 184 | { |
| 185 | std::scoped_lock lock(mLock); |
| 186 | reportCompletedSessions(); |
| 187 | } |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 188 | mNextListener.notify(args); |
| 189 | } |
| 190 | |
| 191 | void InputDeviceMetricsCollector::notifyPointerCaptureChanged( |
| 192 | const NotifyPointerCaptureChangedArgs& args) { |
Prabir Pradhan | 95019ac | 2023-12-06 22:38:21 +0000 | [diff] [blame] | 193 | { |
| 194 | std::scoped_lock lock(mLock); |
| 195 | reportCompletedSessions(); |
| 196 | } |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 197 | mNextListener.notify(args); |
| 198 | } |
| 199 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 200 | void InputDeviceMetricsCollector::notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 201 | const std::set<Uid>& uids) { |
Prabir Pradhan | 047695b | 2023-06-30 01:48:45 +0000 | [diff] [blame] | 202 | if (isIgnoredInputDeviceId(deviceId)) { |
| 203 | return; |
| 204 | } |
Prabir Pradhan | 95019ac | 2023-12-06 22:38:21 +0000 | [diff] [blame] | 205 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 206 | mInteractionsQueue.push(DeviceId{deviceId}, timestamp, uids); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 209 | void InputDeviceMetricsCollector::dump(std::string& dump) { |
Prabir Pradhan | 95019ac | 2023-12-06 22:38:21 +0000 | [diff] [blame] | 210 | std::scoped_lock lock(mLock); |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 211 | dump += "InputDeviceMetricsCollector:\n"; |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 212 | |
| 213 | dump += " Logged device IDs: " + dumpMapKeys(mLoggedDeviceInfos, &toString) + "\n"; |
| 214 | dump += " Devices with active usage sessions: " + |
| 215 | dumpMapKeys(mActiveUsageSessions, &toString) + "\n"; |
| 216 | } |
| 217 | |
Prabir Pradhan | 95019ac | 2023-12-06 22:38:21 +0000 | [diff] [blame] | 218 | void InputDeviceMetricsCollector::monitor() { |
| 219 | std::scoped_lock lock(mLock); |
| 220 | } |
| 221 | |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 222 | void InputDeviceMetricsCollector::onInputDevicesChanged(const std::vector<InputDeviceInfo>& infos) { |
Prabir Pradhan | 0dd48ae | 2023-09-08 21:33:51 +0000 | [diff] [blame] | 223 | std::map<DeviceId, MetricsDeviceInfo> newDeviceInfos; |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 224 | |
| 225 | for (const InputDeviceInfo& info : infos) { |
| 226 | if (isIgnoredInputDeviceId(info.getId())) { |
| 227 | continue; |
| 228 | } |
Prabir Pradhan | 0dd48ae | 2023-09-08 21:33:51 +0000 | [diff] [blame] | 229 | const auto& i = info.getIdentifier(); |
| 230 | newDeviceInfos.emplace(info.getId(), |
| 231 | MetricsDeviceInfo{ |
| 232 | .deviceId = info.getId(), |
| 233 | .vendor = i.vendor, |
| 234 | .product = i.product, |
| 235 | .version = i.version, |
| 236 | .bus = i.bus, |
| 237 | .isUsiStylus = info.getUsiVersion().has_value(), |
| 238 | .keyboardType = info.getKeyboardType(), |
| 239 | }); |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 242 | for (auto [deviceId, info] : mLoggedDeviceInfos) { |
| 243 | if (newDeviceInfos.count(deviceId) != 0) { |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 244 | continue; |
| 245 | } |
Prabir Pradhan | 67d09ca | 2023-09-08 20:28:55 +0000 | [diff] [blame] | 246 | onInputDeviceRemoved(deviceId, info); |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 249 | std::swap(newDeviceInfos, mLoggedDeviceInfos); |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | void InputDeviceMetricsCollector::onInputDeviceRemoved(DeviceId deviceId, |
Prabir Pradhan | 0dd48ae | 2023-09-08 21:33:51 +0000 | [diff] [blame] | 253 | const MetricsDeviceInfo& info) { |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 254 | auto it = mActiveUsageSessions.find(deviceId); |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 255 | if (it == mActiveUsageSessions.end()) { |
| 256 | return; |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 257 | } |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 258 | // Report usage for that device if there is an active session. |
| 259 | auto& [_, activeSession] = *it; |
Prabir Pradhan | 67d09ca | 2023-09-08 20:28:55 +0000 | [diff] [blame] | 260 | mLogger.logInputDeviceUsageReported(info, activeSession.finishSession()); |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 261 | mActiveUsageSessions.erase(it); |
| 262 | |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 263 | // We don't remove this from mLoggedDeviceInfos because it will be updated in |
| 264 | // onInputDevicesChanged(). |
| 265 | } |
| 266 | |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 267 | void InputDeviceMetricsCollector::onInputDeviceUsage(DeviceId deviceId, nanoseconds eventTime, |
| 268 | const SourceProvider& getSources) { |
| 269 | auto infoIt = mLoggedDeviceInfos.find(deviceId); |
| 270 | if (infoIt == mLoggedDeviceInfos.end()) { |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 271 | // Do not track usage for devices that are not logged. |
| 272 | return; |
| 273 | } |
| 274 | |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 275 | auto [sessionIt, _] = |
| 276 | mActiveUsageSessions.try_emplace(deviceId, mUsageSessionTimeout, eventTime); |
| 277 | for (InputDeviceUsageSource source : getSources(infoIt->second)) { |
| 278 | sessionIt->second.recordUsage(eventTime, source); |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
Prabir Pradhan | 4429379 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 282 | void InputDeviceMetricsCollector::onInputDeviceInteraction(const Interaction& interaction) { |
| 283 | auto activeSessionIt = mActiveUsageSessions.find(std::get<DeviceId>(interaction)); |
| 284 | if (activeSessionIt == mActiveUsageSessions.end()) { |
| 285 | return; |
| 286 | } |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 287 | |
Prabir Pradhan | 4429379 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 288 | activeSessionIt->second.recordInteraction(interaction); |
| 289 | } |
| 290 | |
| 291 | void InputDeviceMetricsCollector::reportCompletedSessions() { |
| 292 | // Process all pending interactions. |
| 293 | for (auto interaction = mInteractionsQueue.pop(); interaction; |
| 294 | interaction = mInteractionsQueue.pop()) { |
| 295 | onInputDeviceInteraction(*interaction); |
| 296 | } |
| 297 | |
| 298 | const auto currentTime = mLogger.getCurrentTime(); |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 299 | std::vector<DeviceId> completedUsageSessions; |
| 300 | |
Prabir Pradhan | 4429379 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 301 | // Process usages for all active session to determine if any sessions have expired. |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 302 | for (auto& [deviceId, activeSession] : mActiveUsageSessions) { |
| 303 | if (activeSession.checkIfCompletedAt(currentTime)) { |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 304 | completedUsageSessions.emplace_back(deviceId); |
| 305 | } |
| 306 | } |
| 307 | |
Prabir Pradhan | 4429379 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 308 | // Close out and log all expired usage sessions. |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 309 | for (DeviceId deviceId : completedUsageSessions) { |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 310 | const auto infoIt = mLoggedDeviceInfos.find(deviceId); |
| 311 | LOG_ALWAYS_FATAL_IF(infoIt == mLoggedDeviceInfos.end()); |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 312 | |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 313 | auto activeSessionIt = mActiveUsageSessions.find(deviceId); |
| 314 | LOG_ALWAYS_FATAL_IF(activeSessionIt == mActiveUsageSessions.end()); |
| 315 | auto& [_, activeSession] = *activeSessionIt; |
Prabir Pradhan | 67d09ca | 2023-09-08 20:28:55 +0000 | [diff] [blame] | 316 | mLogger.logInputDeviceUsageReported(infoIt->second, activeSession.finishSession()); |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 317 | mActiveUsageSessions.erase(activeSessionIt); |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 318 | } |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 321 | // --- InputDeviceMetricsCollector::ActiveSession --- |
| 322 | |
| 323 | InputDeviceMetricsCollector::ActiveSession::ActiveSession(nanoseconds usageSessionTimeout, |
| 324 | nanoseconds startTime) |
| 325 | : mUsageSessionTimeout(usageSessionTimeout), mDeviceSession({startTime, startTime}) {} |
| 326 | |
| 327 | void InputDeviceMetricsCollector::ActiveSession::recordUsage(nanoseconds eventTime, |
| 328 | InputDeviceUsageSource source) { |
| 329 | // We assume that event times for subsequent events are always monotonically increasing for each |
| 330 | // input device. |
| 331 | auto [activeSourceIt, inserted] = |
| 332 | mActiveSessionsBySource.try_emplace(source, eventTime, eventTime); |
| 333 | if (!inserted) { |
| 334 | activeSourceIt->second.end = eventTime; |
| 335 | } |
| 336 | mDeviceSession.end = eventTime; |
| 337 | } |
| 338 | |
Prabir Pradhan | 4429379 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 339 | void InputDeviceMetricsCollector::ActiveSession::recordInteraction(const Interaction& interaction) { |
| 340 | const auto sessionExpiryTime = mDeviceSession.end + mUsageSessionTimeout; |
| 341 | const auto timestamp = std::get<nanoseconds>(interaction); |
| 342 | if (timestamp >= sessionExpiryTime) { |
| 343 | // This interaction occurred after the device's current active session is set to expire. |
| 344 | // Ignore it. |
| 345 | return; |
| 346 | } |
| 347 | |
| 348 | for (Uid uid : std::get<std::set<Uid>>(interaction)) { |
| 349 | auto [activeUidIt, inserted] = mActiveSessionsByUid.try_emplace(uid, timestamp, timestamp); |
| 350 | if (!inserted) { |
| 351 | activeUidIt->second.end = timestamp; |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 356 | bool InputDeviceMetricsCollector::ActiveSession::checkIfCompletedAt(nanoseconds timestamp) { |
| 357 | const auto sessionExpiryTime = timestamp - mUsageSessionTimeout; |
| 358 | std::vector<InputDeviceUsageSource> completedSourceSessionsForDevice; |
| 359 | for (auto& [source, session] : mActiveSessionsBySource) { |
| 360 | if (session.end <= sessionExpiryTime) { |
| 361 | completedSourceSessionsForDevice.emplace_back(source); |
| 362 | } |
| 363 | } |
| 364 | for (InputDeviceUsageSource source : completedSourceSessionsForDevice) { |
| 365 | auto it = mActiveSessionsBySource.find(source); |
| 366 | const auto& [_, session] = *it; |
| 367 | mSourceUsageBreakdown.emplace_back(source, session.end - session.start); |
| 368 | mActiveSessionsBySource.erase(it); |
| 369 | } |
Prabir Pradhan | 4429379 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 370 | |
| 371 | std::vector<Uid> completedUidSessionsForDevice; |
| 372 | for (auto& [uid, session] : mActiveSessionsByUid) { |
| 373 | if (session.end <= sessionExpiryTime) { |
| 374 | completedUidSessionsForDevice.emplace_back(uid); |
| 375 | } |
| 376 | } |
| 377 | for (Uid uid : completedUidSessionsForDevice) { |
| 378 | auto it = mActiveSessionsByUid.find(uid); |
| 379 | const auto& [_, session] = *it; |
| 380 | mUidUsageBreakdown.emplace_back(uid, session.end - session.start); |
| 381 | mActiveSessionsByUid.erase(it); |
| 382 | } |
| 383 | |
| 384 | // This active session has expired if there are no more active source sessions tracked. |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 385 | return mActiveSessionsBySource.empty(); |
| 386 | } |
| 387 | |
| 388 | InputDeviceMetricsLogger::DeviceUsageReport |
| 389 | InputDeviceMetricsCollector::ActiveSession::finishSession() { |
| 390 | const auto deviceUsageDuration = mDeviceSession.end - mDeviceSession.start; |
| 391 | |
| 392 | for (const auto& [source, sourceSession] : mActiveSessionsBySource) { |
| 393 | mSourceUsageBreakdown.emplace_back(source, sourceSession.end - sourceSession.start); |
| 394 | } |
| 395 | mActiveSessionsBySource.clear(); |
| 396 | |
Prabir Pradhan | 4429379 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 397 | for (const auto& [uid, uidSession] : mActiveSessionsByUid) { |
| 398 | mUidUsageBreakdown.emplace_back(uid, uidSession.end - uidSession.start); |
| 399 | } |
| 400 | mActiveSessionsByUid.clear(); |
| 401 | |
| 402 | return {deviceUsageDuration, mSourceUsageBreakdown, mUidUsageBreakdown}; |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 405 | } // namespace android |