fix: unit conversion bug in to timeval (backport #24) #2
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.
Description
select関数のタイムアウト時間について、単位変換の誤りを修正する。
fork元の以下のPRの取り込みです。
autowarefoundation#24
発生していた問題事象
sockec_can_receiverについて、interval_secを0.02(単位:sec)で呼び出すと、CPU使用率が100%になり、1秒間隔でWarningが表示される。
# Warningメッセージ [socket_can_receiver_node_exe-1] [WARN] [1686291362.684238888] [socket_can_receiver]: Error receiving CAN message: canIMU - Resource temporarily unavailable
発生メカニズム
select関数はソケットの準備待ち(*1)をするが、この時のタイムアウト時間はROS2のパラメータで渡される値を使う。
パラメータはdouble型(単位:sec)に対し、select関数はtimeval型でタイムアウトを設定する事から単位変換が発生する。
単位変換の途中にnanosecからmicrosecへの変換が含まれるが、1000で除算するところを乗算しており、意図しない値が代入されていた。
結果、意図しないタイムアウト時間でselect関数が呼び出され問題が生じていた。
変換の流れは以下の通り。timeval型は整数部と小数点以下を別々の構造体メンバーで保持する。
小数点以下の部分は内部で次のように変換されている。
正:0.01(s) -> 10_000_000(ns) -> 10_000(ms)
誤:0.01(s) -> 10_000_000(ns) -> 10_000_000_000(ms)
小数部分を格納する領域に、小数の範囲を超える値が格納され意図しない動作になっていると考えられる。
*1 sockec_can_receiverはデータ読み込み可能になったか、socket_can_senderはデータ書き込み可能になったかを指す。
Related links
Tests performed
事前準備
パターン1:interval_sec > 受信間隔
# Terminal-1 cangen vcan0 -g 19
# Terminal-2 top
上記を実施した状態で以下を確認する。
socket_can_receiver
のCPU使用率が100%付近でないこと。[socket_can_receiver_node_exe-1] [WARN] [実行時の時間] [socket_can_receiver]: Error receiving CAN message: vcan0 - CAN Receive Timeout
が出力されないこと。パターン2:interval_sec < 受信間隔
# Terminal-1 cangen vcan0 -g 21
# Terminal-2 top
上記を実施した状態で以下を確認する。
socket_can_receiver
のCPU使用率が100%付近でないこと。[socket_can_receiver_node_exe-1] [WARN] [実行時の時間] [socket_can_receiver]: Error receiving CAN message: vcan0 - CAN Receive Timeout
が出力されること。パターン3:timeout_sec > 送信間隔
# Terminal-2 top
上記を実施した状態で以下を確認する。
socket_can_sender
のCPU使用率が100%付近でないこと。Notes for reviewers
None.
Pre-review checklist for the PR author
The PR author must check the checkboxes below when creating the PR.
In-review checklist for the PR reviewers
The PR reviewers must check the checkboxes below before approval.
Post-review checklist for the PR author
The PR author must check the checkboxes below before merging.
After all checkboxes are checked, anyone who has write access can merge the PR.