Skip to content

Commit

Permalink
initial xlink throughput statistics impllementation, have to glow it …
Browse files Browse the repository at this point in the history
…up a bit and maybe clean up the code
  • Loading branch information
zrezke committed May 15, 2023
1 parent 455d82b commit 91daf45
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 25 deletions.
4 changes: 4 additions & 0 deletions crates/re_log_types/src/component_types/xlink_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ use arrow2::array::Int128Array;
use arrow2_convert::{field::I128, ArrowDeserialize, ArrowField, ArrowSerialize};

// TODO(filip): Convert to use i128

/// Stats about the XLink connection throughput
#[derive(Clone, Debug, PartialEq, ArrowField, ArrowSerialize, ArrowDeserialize)]
pub struct XlinkStats {
/// Bytes read from the XLink by the host (PC)
pub bytes_read: i64,
/// Bytes written to the XLink by the host (PC)
pub bytes_written: i64,
}

Expand Down
63 changes: 48 additions & 15 deletions crates/re_viewer/src/ui/stats_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ pub struct StatsPanelState {
magnetometer_history: History<[f32; 3]>,
start_time: instant::Instant, // Time elapsed from spawning the app
imu_tab_visible: bool, // Used to subscribe and unsubscribe from the IMU data
xlink_stats_history: History<[i64; 2]>,
xlink_stats_history: History<[f64; 4]>, // [MB written in last time frame, MB read in last time frame, MB written total, MB read total]
avg_xlink_stats_plot_history: History<[f64; 2]>, // [Avg MB written, Avg MB read]
}

impl Default for StatsPanelState {
Expand All @@ -66,7 +67,8 @@ impl Default for StatsPanelState {
magnetometer_history: History::new(0..1000, 5.0),
start_time: instant::Instant::now(),
imu_tab_visible: false,
xlink_stats_history: History::new(0..1000, 5.0),
xlink_stats_history: History::new(0..1000, 1.0),
avg_xlink_stats_plot_history: History::new(0..1000, 5.0),
}
}
}
Expand All @@ -87,7 +89,7 @@ impl StatsPanelState {
imu_entity_path,
&[ImuData::name()],
) {
latest.visit1(|_inst, imu_data| {
let _ = latest.visit1(|_inst, imu_data| {
self.accel_history
.add(now, [imu_data.accel.x, imu_data.accel.y, imu_data.accel.z]);
self.gyro_history
Expand All @@ -108,9 +110,42 @@ impl StatsPanelState {
entity_path,
&[XlinkStats::name()],
) {
latest.visit1(|_inst, xlink_stats| {
self.xlink_stats_history
.add(now, [xlink_stats.bytes_written, xlink_stats.bytes_read]);
let _ = latest.visit1(|_inst, xlink_stats| {
let (mut written, mut read) = (
(xlink_stats.bytes_written / 1e6 as i64) as f64,
(xlink_stats.bytes_read / 1e6 as i64) as f64,
);
if let Some((time, [_, _, total_written, total_read])) =
self.xlink_stats_history.iter().last()
{
written = (written - total_written) / (now - time);
read = (read - total_read) / (now - time);
}

self.xlink_stats_history.add(
now,
[
written,
read,
(xlink_stats.bytes_written / 1e6 as i64) as f64,
(xlink_stats.bytes_read / 1e6 as i64) as f64,
],
);
self.avg_xlink_stats_plot_history.add(
now,
[
self.xlink_stats_history
.iter()
.map(|(_, [written, _, _, _])| written)
.sum::<f64>()
/ self.xlink_stats_history.len() as f64,
self.xlink_stats_history
.iter()
.map(|(_, [_, read, _, _])| read)
.sum::<f64>()
/ self.xlink_stats_history.len() as f64,
],
);
});
}
}
Expand Down Expand Up @@ -170,30 +205,28 @@ impl<'a, 'b> StatsTabs<'a, 'b> {
fn xlink_ui(&mut self, ui: &mut egui::Ui) {
ui.vertical(|ui| {
let max_width = ui.available_width();
let (history, display_name, unit) = (&mut self.state.xlink_stats_history, "Xlink", "");
let (history, display_name, unit) =
(&mut self.state.avg_xlink_stats_plot_history, "Xlink", "");
let Some(latest) = history.latest() else {
ui.label(format!("No {display_name} data yet"));
return;
};
ui.label(display_name);
let mut plot = Plot::new(display_name).show(ui, |plot_ui| {
Plot::new(display_name).show(ui, |plot_ui| {
plot_ui.line(
Line::new(PlotPoints::new(
history
.iter()
.map(|(t, v)| [t, v[0] as f64 / 1e6])
.map(|(t, [written, _])| [t, written])
.collect_vec(),
))
.color(egui::Color32::RED),
.color(egui::Color32::BLUE),
);
plot_ui.line(
Line::new(PlotPoints::new(
history
.iter()
.map(|(t, v)| [t, v[1] as f64 / 1e6])
.collect_vec(),
history.iter().map(|(t, [_, read])| [t, read]).collect_vec(),
))
.color(egui::Color32::BLUE),
.color(egui::Color32::RED),
);
});
});
Expand Down
2 changes: 2 additions & 0 deletions crates/re_viewer/src/ui/viewport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,8 @@ fn visibility_button_ui(

// ----------------------------------------------------------------------------

struct ViewportView {}

struct TabViewer<'a, 'b> {
ctx: &'a mut ViewerContext<'b>,
viewport: &'a mut Viewport,
Expand Down
4 changes: 2 additions & 2 deletions rerun_py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ dependencies = [
"numpy>=1.23",
"pyarrow==10.0.1",
"ahrs",
"depthai==2.21.2.0", # Atm python3 -m pip install --extra-index-url https://artifacts.luxonis.com/artifactory/luxonis-python-snapshot-local/ depthai==2.21.2.0.dev0+5004cc71950e6786feb36147b7919e146f4ef8da --force-reinstall # is required
"depthai-sdk==1.9.5", # Atm latest develop is required
# "depthai==2.21.2.0", # Atm python3 -m pip install --extra-index-url https://artifacts.luxonis.com/artifactory/luxonis-python-snapshot-local/ depthai==2.21.2.0.dev0+5004cc71950e6786feb36147b7919e146f4ef8da --force-reinstall # is required
# "depthai-sdk==1.9.5", # Atm latest develop is required
"websockets",
"pydantic",
]
Expand Down
17 changes: 15 additions & 2 deletions rerun_py/rerun_sdk/depthai_viewer_backend/back.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class SelectedDevice:
intrinsic_matrix: Dict[Tuple[int, int], np.ndarray] = {}
calibration_data: dai.CalibrationHandler = None
use_encoding: bool = False
_time_of_last_xlink_update: int = 0

_color: CameraComponent = None
_left: CameraComponent = None
Expand Down Expand Up @@ -211,6 +212,19 @@ def update_pipeline(
self.intrinsic_matrix = {}
return running, {"message": "Pipeline started" if running else "Couldn't start pipeline"}

def update(self):
self.oak_cam.poll()
if time.time_ns() - self._time_of_last_xlink_update >= 16e6:
self._time_of_last_xlink_update = time.time_ns()
xlink_stats = self.oak_cam.device.getProfilingData()
rr.log_xlink_stats(xlink_stats.numBytesWritten, xlink_stats.numBytesRead)


import rerun as rr

rr.init("Depthai Viewer")
rr.connect()


class DepthaiViewerBack:
_device: SelectedDevice = None
Expand Down Expand Up @@ -302,8 +316,7 @@ def run(self):
pass

if self._device and self._device.oak_cam:
self._device.oak_cam.poll()

self._device.update()
if self._device.oak_cam.device.isClosed():
# TODO(filip): Typehint the messages properly
self.on_reset()
Expand Down
1 change: 0 additions & 1 deletion rerun_py/rerun_sdk/depthai_viewer_backend/sdk_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ def on_color_frame(self, frame: FramePacket):
EntityPath.RGB_PINHOLE_CAMERA, child_from_parent=self._get_camera_intrinsics(w, h), width=w, height=h
)
rr.log_image(EntityPath.RGB_CAMERA_IMAGE, cv2.cvtColor(frame.frame, cv2.COLOR_BGR2RGB))
rr.log_xlink_stats(1,1)

def on_left_frame(self, frame: FramePacket):
if Topic.LeftMono not in self.store.subscriptions:
Expand Down
8 changes: 3 additions & 5 deletions rerun_py/rerun_sdk/rerun/log/xlink_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@


@log_decorator
def log_xlink_stats(
total_bytes_written: int, total_bytes_read: int
) -> None:
def log_xlink_stats(total_bytes_written: int, total_bytes_read: int) -> None:
"""
Log an XLink throughput statistic.
Parameters
----------
total_bytes_written:
Total bytes written by the device to the XLink.
Total bytes written to the XLink by the host.
total_bytes_read:
Total bytes read by the device from the XLink.
Total bytes read from the XLink by the host.
"""
instanced: Dict[str, Any] = {}
instanced["rerun.xlink_stats"] = XLinkStats.create(total_bytes_written, total_bytes_read) # type: ignore[arg-type]
Expand Down

0 comments on commit 91daf45

Please sign in to comment.