GithubHelp home page GithubHelp logo

Add tracepoint example about rust-bcc HOT 2 CLOSED

poonai avatar poonai commented on August 11, 2024
Add tracepoint example

from rust-bcc.

Comments (2)

brayniac avatar brayniac commented on August 11, 2024 2

Have a look at the softirqs example. https://github.com/jvns/rust-bcc/blob/master/examples/softirqs.rs

from rust-bcc.

poonai avatar poonai commented on August 11, 2024

Hey @brayniac
I'm trying perf output with tracepoint, I'm getting the output as todo: oh no
my code :

#include <uapi/linux/ptrace.h>

BPF_PERF_OUTPUT(events);

struct kmalloc_data
{
    u32 pid;
    u64 byte_alloc;
};

int kmalloc_collector(struct tracepoint__kmem__kmalloc *args)
{
    u32 pid = bpf_get_current_pid_tgid();
    u64 alloc = args->bytes_alloc;
    struct kmalloc_data data = {};
    data.pid = pid;
    data.byte_alloc = alloc;
    events.perf_submit(args, &data, sizeof(data));
    return 0;
}
extern crate bcc;
extern crate failure;
use bcc::core::BPF;
use bcc::perf::init_perf_map;
use failure::Error;
use std::{mem, ptr, thread, time};

fn do_main() -> Result<(), Error> {
    let code = include_str!("kmalloc.c");
    let mut module = match BPF::new(code) {
        Ok(x) => x,
        Err(x) => return Err(x),
    };
    let kmalloc_collector = module.load_tracepoint("kmalloc_collector")?;
    module.attach_tracepoint("kmem", "kmalloc", kmalloc_collector)?;
    let table = module.table("events");
    let mut perfmap = init_perf_map(table, perf_callback)?;
    loop {
        perfmap.poll(200);
    }
    Ok(())
}

fn perf_callback() -> Box<FnMut(&[u8]) + Send> {
    Box::new(|x| {
        let data = parse_struct(x);
        println!("pid {} bytes allocated {}", data.pid, data.byte_alloc);
    })
}

#[repr(C)]
struct kmalloc_data {
    pid: u32,
    byte_alloc: u64,
}
fn parse_struct(x: &[u8]) -> kmalloc_data {
    unsafe { ptr::read(x.as_ptr() as *const kmalloc_data) }
}

fn main() {
    match do_main() {
        Ok(()) => println!("{}", "sup?"),
        Err(x) => eprintln!("{}", x),
    }
}

But it is working for HASH
PS: I just started rust so, my code may not be idiomatic

from rust-bcc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.