fn initialize_bpf()

in src/samplers/disk/mod.rs [131:214]


    fn initialize_bpf(&mut self) -> Result<(), anyhow::Error> {
        #[cfg(feature = "bpf")]
        {
            if self.enabled() && self.bpf_enabled() {
                debug!("initializing bpf");
                // load the code and compile
                let code = include_str!("bpf.c");
                let code = code.replace(
                    "VALUE_TO_INDEX2_FUNC",
                    include_str!("../../common/value_to_index2.c"),
                );
                let mut bpf = bcc::BPF::new(&code)?;
                // collect the set of probes required from the statistics enabled.
                let mut probes = HashSet::new();
                for statistic in &self.statistics {
                    for probe in statistic.bpf_probes_required() {
                        probes.insert(probe);
                    }
                }

                // load + attach the kernel probes that are required to the bpf instance.
                for probe in probes {
                    match &probe.name[..] {
                        "blk_start_request" => {
                            // attach only if 'blk_start_request' can be found.
                            if let Ok(results) = bpf.get_kprobe_functions("blk_start_request") {
                                if !results.is_empty() {
                                    if self.common.config.fault_tolerant() {
                                        let _ = probe.try_attach_to_bpf(&mut bpf);
                                    } else {
                                        probe.try_attach_to_bpf(&mut bpf)?;
                                    }
                                }
                            }
                        }
                        "blk_account_io_completion" =>
                        // if 'blk_account_io_completion' exists, we attach this probe.
                        {
                            if let Ok(results) =
                                bpf.get_kprobe_functions("blk_account_io_completion")
                            {
                                if !results.is_empty() {
                                    if self.common.config.fault_tolerant() {
                                        if let Err(e) = probe.try_attach_to_bpf(&mut bpf) {
                                            warn!("skipping {} with error: {}", probe.name, e);
                                        }
                                    } else {
                                        probe.try_attach_to_bpf(&mut bpf)?;
                                    }
                                }
                            }
                        }
                        "blk_account_io_done" =>
                        // if 'blk_account_io_completion' does exist, we attach blk_account_io_done.
                        {
                            if let Ok(results) =
                                bpf.get_kprobe_functions("blk_account_io_completion")
                            {
                                if results.is_empty() {
                                    if self.common.config.fault_tolerant() {
                                        let _ = probe.try_attach_to_bpf(&mut bpf);
                                    } else {
                                        probe.try_attach_to_bpf(&mut bpf)?;
                                    }
                                }
                            }
                        }
                        _ => {
                            // load + attach the kernel probes that are required to the bpf instance.
                            if self.common.config.fault_tolerant() {
                                let _ = probe.try_attach_to_bpf(&mut bpf);
                            } else {
                                probe.try_attach_to_bpf(&mut bpf)?;
                            }
                        }
                    }
                }

                self.bpf = Some(Arc::new(Mutex::new(BPF { inner: bpf })));
            }
        }

        Ok(())
    }