Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 1 | #include "aos/events/ping_lib.h" |
| 2 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 3 | #include "gflags/gflags.h" |
| 4 | #include "glog/logging.h" |
| 5 | |
James Kuszmaul | 6f255b2 | 2023-11-06 13:46:54 -0800 | [diff] [blame] | 6 | #include "aos/events/ping_static.h" |
| 7 | #include "aos/events/pong_static.h" |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 8 | #include "aos/json_to_flatbuffer.h" |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 9 | |
James Kuszmaul | 60bb868 | 2023-08-07 07:39:34 -0700 | [diff] [blame] | 10 | DEFINE_int32(sleep_us, 10000, "Time to sleep between pings"); |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 11 | |
| 12 | namespace aos { |
| 13 | |
| 14 | namespace chrono = std::chrono; |
| 15 | |
Sanjay Narayanan | 5ec0023 | 2022-07-08 15:21:30 -0700 | [diff] [blame] | 16 | Ping::Ping(EventLoop *event_loop, std::string_view channel_name) |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 17 | : event_loop_(event_loop), |
James Kuszmaul | 6f255b2 | 2023-11-06 13:46:54 -0800 | [diff] [blame] | 18 | sender_(event_loop_->MakeSender<examples::PingStatic>(channel_name)) { |
Austin Schuh | 5d89cf5 | 2019-12-28 16:27:42 -0800 | [diff] [blame] | 19 | timer_handle_ = event_loop_->AddTimer([this]() { SendPing(); }); |
| 20 | timer_handle_->set_name("ping"); |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 21 | |
Sanjay Narayanan | 5ec0023 | 2022-07-08 15:21:30 -0700 | [diff] [blame] | 22 | if (event_loop_->HasChannel<examples::Pong>(channel_name)) { |
| 23 | event_loop_->MakeWatcher( |
| 24 | channel_name, [this](const examples::Pong &pong) { HandlePong(pong); }); |
| 25 | } |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 26 | |
| 27 | event_loop_->OnRun([this]() { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 28 | timer_handle_->Schedule(event_loop_->monotonic_now(), |
James Kuszmaul | 60bb868 | 2023-08-07 07:39:34 -0700 | [diff] [blame] | 29 | chrono::microseconds(FLAGS_sleep_us)); |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 30 | }); |
| 31 | |
| 32 | event_loop_->SetRuntimeRealtimePriority(5); |
| 33 | } |
| 34 | |
| 35 | void Ping::SendPing() { |
Sarah Newman | 2f98cf0 | 2022-04-01 16:42:00 -0700 | [diff] [blame] | 36 | if (last_pong_value_ != count_ && (!quiet_ || VLOG_IS_ON(1))) { |
| 37 | LOG(WARNING) << "Did not receive response to " << count_ << " within " |
James Kuszmaul | 60bb868 | 2023-08-07 07:39:34 -0700 | [diff] [blame] | 38 | << FLAGS_sleep_us << "us."; |
Sarah Newman | 2f98cf0 | 2022-04-01 16:42:00 -0700 | [diff] [blame] | 39 | } |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 40 | ++count_; |
James Kuszmaul | 6f255b2 | 2023-11-06 13:46:54 -0800 | [diff] [blame] | 41 | aos::Sender<examples::PingStatic>::StaticBuilder builder = |
| 42 | sender_.MakeStaticBuilder(); |
James Kuszmaul | dde6563 | 2023-12-07 16:12:26 -0800 | [diff] [blame] | 43 | builder->set_value(count_); |
| 44 | builder->set_send_time( |
| 45 | event_loop_->monotonic_now().time_since_epoch().count()); |
James Kuszmaul | 6f255b2 | 2023-11-06 13:46:54 -0800 | [diff] [blame] | 46 | builder.CheckOk(builder.Send()); |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 47 | VLOG(2) << "Sending ping"; |
| 48 | } |
| 49 | |
| 50 | void Ping::HandlePong(const examples::Pong &pong) { |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 51 | const aos::monotonic_clock::time_point monotonic_send_time( |
| 52 | chrono::nanoseconds(pong.initial_send_time())); |
| 53 | const aos::monotonic_clock::time_point monotonic_now = |
| 54 | event_loop_->monotonic_now(); |
| 55 | |
| 56 | const chrono::nanoseconds round_trip_time = |
| 57 | monotonic_now - monotonic_send_time; |
| 58 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 59 | if (last_pong_value_ + 1 != pong.value() && (!quiet_ || VLOG_IS_ON(1))) { |
Sarah Newman | 2f98cf0 | 2022-04-01 16:42:00 -0700 | [diff] [blame] | 60 | LOG(WARNING) << "Unexpected pong value, wanted " << last_pong_value_ + 1 |
| 61 | << ", got " << pong.value(); |
Austin Schuh | 5d89cf5 | 2019-12-28 16:27:42 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 64 | if (pong.value() == count_) { |
| 65 | VLOG(1) << "Elapsed time " << round_trip_time.count() << " ns " |
| 66 | << FlatbufferToJson(&pong); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 67 | } else if (!quiet_ || VLOG_IS_ON(1)) { |
Sarah Newman | 2f98cf0 | 2022-04-01 16:42:00 -0700 | [diff] [blame] | 68 | LOG(WARNING) << "Unexpected pong response, got " << FlatbufferToJson(&pong) |
| 69 | << " expected " << count_ << ", elapsed time " |
| 70 | << round_trip_time.count() << " ns "; |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 71 | } |
Austin Schuh | 5d89cf5 | 2019-12-28 16:27:42 -0800 | [diff] [blame] | 72 | |
| 73 | last_pong_value_ = pong.value(); |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 74 | } |
Austin Schuh | 5d89cf5 | 2019-12-28 16:27:42 -0800 | [diff] [blame] | 75 | |
Austin Schuh | 6b9c415 | 2019-11-29 12:45:24 -0800 | [diff] [blame] | 76 | } // namespace aos |