Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Get values from per-cpu maps #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/map/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,20 @@ export class RawMap implements IMap<Buffer, Buffer> {
return out
}

// Get all values from per-cpu maps
getPerCPU(key: Buffer, flags: number = 0, out?: Buffer): Buffer | undefined {
this._kBuf(key)
const nr_cpus = native.getNumPossibleCPUs()
checkStatus('libbpf_num_possible_cpus', nr_cpus)
// allocate large enough Buffer to store values read from all CPUs
out = this._getBuf(this.ref.valueSize * nr_cpus, out)
const status = native.mapLookupElem(this.ref.fd, key, out, flags)
if (status == -ENOENT)
return undefined
checkStatus('bpf_map_lookup_elem_flags', status)
return out
}

getDelete(key: Buffer, out?: Buffer): Buffer | undefined {
this._kBuf(key)
out = this._vOrBuf(out)
Expand Down
7 changes: 7 additions & 0 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <sys/utsname.h>

#include <bpf.h>
#include <libbpf.h>
#include <errno.h>

#include <napi.h>
Expand Down Expand Up @@ -315,6 +316,11 @@ Napi::Value BpfObjGet(const CallbackInfo& info) {
return ToStatus(env, bpf_obj_get(path.c_str()));
}

Napi::Value GetNumPossibleCPUs(const CallbackInfo& info) {
Napi::Env env = info.Env();
return ToStatus(env, libbpf_num_possible_cpus());
Copy link
Owner

@mildsunrise mildsunrise Jan 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we can't use ToStatus here, the function returns the error code directly (not through errno)

}

#define EXPOSE_FUNCTION(NAME, METHOD) exports.Set(NAME, Napi::Function::New(env, METHOD, NAME))

Napi::Object Init(Napi::Env env, Napi::Object exports) {
Expand Down Expand Up @@ -345,6 +351,7 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
EXPOSE_FUNCTION("getMapInfo", GetMapInfo);
EXPOSE_FUNCTION("mapGetFdById", MapGetFdById);
EXPOSE_FUNCTION("bpfObjGet", BpfObjGet);
EXPOSE_FUNCTION("getNumPossibleCPUs", GetNumPossibleCPUs);

return exports;
}
Expand Down