Skip to content

Commit

Permalink
Monitoring global for Pools + API enhancements. (#230)
Browse files Browse the repository at this point in the history
* Monitoring global for Pools + API enhancements.

* Another layer of API improvements.

* small snafu

* Typo :fp:
  • Loading branch information
gbin authored Jan 20, 2025
1 parent 5345980 commit eca6039
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 113 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,8 @@ tempfile = "3.14.0"
# rerun
rerun = "0.21.0"

# smallvec to avoid heap allocations
smallvec = { version = "1.13.2", features = ["serde"] }

# [profile.release]
# lto = true
12 changes: 11 additions & 1 deletion components/sources/cu_v4l/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,18 @@ mod linux_impl {
let mut stream = CuV4LStream::with_buffers(
&dev,
Type::VideoCapture,
actual_fmt.size as usize,
req_buffers,
CuHostMemoryPool::new(
format!("V4L Host Pool {}", v4l_device).as_str(),
req_buffers as usize + 1,
|| vec![0; actual_fmt.size as usize],
)
.map_err(|e| {
CuError::new_with_cause(
"Could not create host memory pool backing the V4lStream",
e,
)
})?,
)
.map_err(|e| CuError::new_with_cause("Could not create the V4lStream", e))?;
debug!("V4L: Set timeout to {} ms", req_timeout.as_millis() as u64);
Expand Down
34 changes: 28 additions & 6 deletions components/sources/cu_v4l/src/v4lstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,55 @@ use v4l::{v4l2, Device};
pub struct CuV4LStream {
v4l_handle: Arc<Handle>,
v4l_buf_type: Type,
memory_pool: CuHostMemoryPool<Vec<u8>>,
pool: Arc<CuHostMemoryPool<Vec<u8>>>,
// Arena matching the vl42 metadata and the Copper Buffers
arena: Vec<(Metadata, Option<CuHandle<Vec<u8>>>)>,
arena_last_freed_up_index: usize,
timeout: Option<i32>,
active: bool,
}

use std::fs;
use std::os::fd::RawFd;
use std::path::PathBuf;

fn get_original_dev_path(fd: RawFd) -> Option<PathBuf> {
let link_path = format!("/proc/self/fd/{}", fd);

if let Ok(path) = fs::read_link(link_path) {
if path.to_string_lossy().starts_with("/dev/video") {
return Some(path);
}
}
None
}

impl CuV4LStream {
#[allow(dead_code)]
pub fn new(dev: &Device, buf_size: usize, buf_type: Type) -> io::Result<Self> {
CuV4LStream::with_buffers(dev, buf_type, buf_size, 4)
let original_path = get_original_dev_path(dev.handle().fd()).unwrap();
let pool = CuHostMemoryPool::new(
format!("V4L Host Pool {}", original_path.display()).as_str(),
4,
|| vec![0; buf_size],
)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;

CuV4LStream::with_buffers(dev, buf_type, 4, pool)
}

pub fn with_buffers(
dev: &Device,
buf_type: Type,
buf_size: usize,
buf_count: u32,
pool: Arc<CuHostMemoryPool<Vec<u8>>>,
) -> io::Result<Self> {
let memory_pool = CuHostMemoryPool::new(buf_count as usize + 1, || vec![0; buf_size]); // +1 to be able to queue one last buffer before zapping the first
let mut arena = Vec::new();
arena.resize(buf_count as usize, (Metadata::default(), None));

let mut result = CuV4LStream {
v4l_handle: dev.handle(),
memory_pool,
pool,
arena,
arena_last_freed_up_index: 0,
v4l_buf_type: buf_type,
Expand Down Expand Up @@ -191,7 +213,7 @@ impl Stream for CuV4LStream {

impl CaptureStream<'_> for CuV4LStream {
fn queue(&mut self, index: usize) -> io::Result<()> {
let buffer_handle = self.memory_pool.acquire().unwrap();
let buffer_handle = self.pool.acquire().unwrap();
self.arena[index] = (Metadata::default(), Some(buffer_handle.clone()));
let mut v4l2_buf = buffer_handle.with_inner_mut(|inner| {
let destination: &mut [u8] = inner;
Expand Down
2 changes: 1 addition & 1 deletion core/cu29_log_runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ cu29-log = { workspace = true }
cu29-traits = { workspace = true }
cu29-clock = { workspace = true }
bincode = { workspace = true }
smallvec = "1.13.2"
smallvec = { workspace = true }
log = "0.4.22"

[dev-dependencies]
Expand Down
1 change: 1 addition & 0 deletions core/cu29_runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ cu29-clock = { workspace = true }
clap = { workspace = true }
tempfile = { workspace = true }
arrayvec = "0.7.6"
smallvec = { workspace = true }
ron = "0.8.1"
hdrhistogram = "7.5.4"
petgraph = { version = "0.7.1", features = ["serde", "serde-1", "serde_derive"] }
Expand Down
Loading

0 comments on commit eca6039

Please sign in to comment.