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