-
Notifications
You must be signed in to change notification settings - Fork 11.1k
add USB logging #38186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
elkoled
wants to merge
4
commits into
master
Choose a base branch
from
usb3
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
add USB logging #38186
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| #!/usr/bin/env python3 | ||
| from pathlib import Path | ||
|
|
||
| from cereal import messaging | ||
| from openpilot.common.realtime import Ratekeeper | ||
| from openpilot.common.swaglog import cloudlog | ||
| from openpilot.selfdrive.modeld.helpers import USBGPU_VID, USBGPU_PID | ||
|
|
||
| RATE = 10 # Hz | ||
|
|
||
|
|
||
| def find_device() -> Path | None: | ||
| # discover the eGPU by VID/PID | ||
| for d in Path("/sys/bus/usb/devices").glob("*"): | ||
| try: | ||
| if int((d / "idVendor").read_text(), 16) == USBGPU_VID and \ | ||
| int((d / "idProduct").read_text(), 16) == USBGPU_PID: | ||
| return d | ||
| except Exception: | ||
| pass | ||
| return None | ||
|
|
||
|
|
||
| def read(device: Path, attr: str) -> str | None: | ||
| try: | ||
| return (device / attr).read_text().strip() | ||
| except OSError: | ||
| return None | ||
|
|
||
|
|
||
| def over_current_count(device: Path) -> int: | ||
| # upstream root-hub port, e.g. device "4-1" -> usb4/4-0:1.0/usb4-port1 | ||
| bus, _, port = device.name.partition("-") | ||
| try: | ||
| return int((Path(f"/sys/bus/usb/devices/usb{bus}/{bus}-0:1.0/usb{bus}-port{port}") / "over_current_count").read_text()) | ||
| except (OSError, ValueError): | ||
| return 0 | ||
|
|
||
|
|
||
| def main(): | ||
| pm = messaging.PubMaster(['usbState']) | ||
| rk = Ratekeeper(RATE) | ||
| disconnect_count = 0 | ||
| was_connected = False | ||
|
|
||
| while True: | ||
| device = find_device() | ||
| connected = device is not None | ||
| if was_connected and not connected: | ||
| disconnect_count += 1 | ||
| cloudlog.event("usb_disconnected", count=disconnect_count) | ||
| elif connected and not was_connected: | ||
| cloudlog.event("usb_connected", speed=read(device, "speed")) | ||
| was_connected = connected | ||
|
|
||
| msg = messaging.new_message('usbState', valid=True) | ||
| state = msg.usbState | ||
| state.connected = connected | ||
| if device is not None: | ||
| speed = read(device, "speed") | ||
| state.speedMbps = int(speed) if (speed and speed.isdigit()) else 0 | ||
| state.pmActive = read(device, "power/runtime_status") == "active" | ||
| state.overCurrentCount = over_current_count(device) | ||
| state.disconnectCount = disconnect_count | ||
|
|
||
| pm.send('usbState', msg) | ||
| rk.keep_time() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think this just goes in
hardwaredThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about 100Hz+ usb state sampling inside usbd to catch transients but could do that in the kernel with interrupts and sample only the resulting counters in hardwared with 2Hz