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