npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bpfcc

v1.0.2

Published

Frontend / bindings for BPF Compiler Collection (BCC)

Downloads

27

Readme

bpfcc

Node.JS frontend (aka bindings) for iovisor's BPF Compiler Collection (BCC).

💡 Examples  •  📚 API reference

Usage

Installing

First you need to install BCC on your system. You don't need to install everything, only the C library & development files; for instance, on Ubuntu the following should be enough:

sudo apt install libbpfcc-dev

Then install this module and bpf, which is required as a peer dependency:

npm install bpfcc bpf

Loading & attaching programs

To use it, first pass your program to load or loadSync to compile it:

const { loadSync } = require('bpfcc')

const bpf = loadSync(`
    #include <uapi/linux/ptrace.h>
    #include <linux/blkdev.h>

    BPF_HISTOGRAM(dist);
    BPF_HISTOGRAM(dist_linear);

    int kprobe__blk_account_io_done(struct pt_regs *ctx, struct request *req) {
        dist.increment(bpf_log2l(req->__data_len / 1024));
        dist_linear.increment(req->__data_len / 1024);
        return 0;
    }
`)

Then you need to load & attach your functions to kernel events using the attach* methods:

bpf.attachKprobe('blk_account_io_done', 'kprobe__blk_account_io_done')

Note: By default, functions starting with prefixes like kprobe__ are automatically detected and attached, so the above isn't necessary in this case.

At a later point, if you no longer need it, you can use bpf.detachAll() to detach and unload everything from the kernel. If you don't, it might get called by the GC at some point, but it's not recommended to rely on this.

Accessing maps

Once tracing has started, we can communicate with our eBPF program by accessing its maps (using the get*Map methods). In our case we have two array maps, with uint32 values:

const dist = bpf.getRawArrayMap('dist')
const distLinear = bpf.getRawArrayMap('dist_linear')

// Retrieve current values & parse them
const ys = [...dist].map(x => x.readUInt32LE(0))
console.log(ys)

getRaw*Map methods provide a raw interface which returns Buffers, so we had to parse the values ourselves. But there are also high-level versions that take a conversion object. For convenience, bpf provides a conversion for uint32, so we can write:

const { u32type } = require('bpf')

const dist = bpf.getArrayMap('dist', u32type)
const distLinear = bpf.getArrayMap('dist_linear', u32type)

console.log( [...dist] )

Refer to the bpf module for details on the interface.

The full source code of this example is in bitehist.ts. Remember you'll probably need root to run.

Troubleshooting

Remember that not all features may be available in the kernel you are running, ve if they're present in the API and typings. Trying to use a non-available feature will generally result in an EINVAL error.

A reference of eBPF features and minimum kernel versions required for them can be found here.