Start tracking allocations for a specific PID Returns a receiver channel that will receive allocation events. The receiver will continue to produce events until the tracker is dropped.
(&mut self, pid: i32)
| 53 | /// Returns a receiver channel that will receive allocation events. |
| 54 | /// The receiver will continue to produce events until the tracker is dropped. |
| 55 | pub fn track(&mut self, pid: i32) -> Result<Receiver<Event>> { |
| 56 | // Add the PID to track |
| 57 | self.bpf.add_tracked_pid(pid)?; |
| 58 | debug!("Tracking PID {pid}"); |
| 59 | |
| 60 | // Start polling with channel |
| 61 | let (_poller, event_rx) = self.bpf.start_polling_with_channel(10)?; |
| 62 | |
| 63 | // Keep the poller alive by moving it into the channel |
| 64 | // When the receiver is dropped, the poller will also be dropped |
| 65 | let (tx, rx) = mpsc::channel(); |
| 66 | std::thread::spawn(move || { |
| 67 | // Keep poller alive |
| 68 | let _p = _poller; |
| 69 | while let Ok(event) = event_rx.recv() { |
| 70 | if tx.send(event).is_err() { |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | }); |
| 75 | |
| 76 | Ok(rx) |
| 77 | } |
| 78 | |
| 79 | /// Bump RLIMIT_MEMLOCK for kernels older than 5.11. Newer kernels account BPF |
| 80 | /// memory against the cgroup, so a denied raise (no CAP_SYS_RESOURCE) is harmless. |