Skip to content

Commit

Permalink
Add builtin numaid, example:
Browse files Browse the repository at this point in the history
NUMA node information:

    $ numactl --hardware
    available: 2 nodes (0-1)
    node 0 cpus: 0 1 2 3 4 5 6 7 ...
    [...]
    node 1 cpus: 20 21 22 23 24 25 ...
    [...]
    node distances:
    node   0   1
    0:  10  21
    1:  21  10

bpftrace script code net_action.bt:
    kprobe:net_tx_action,
    kprobe:net_rx_action
    {
        @[numaid, comm] = count();
    }

Example1:
    ------------------------------------------------------------
    iperf3 server:
    $ taskset -c 1 iperf3 -s
    [...]
    [  5]   0.00-1.00   sec  6.04 GBytes  51.9 Gbits/sec
    [...]

    iperf3 client:
    $ taskset -c 2 iperf3 -c 0
    [...]
    [  5]   0.00-1.00   sec  6.04 GBytes  51.9 Gbits/sec    0   1.19 MBytes
    [...]

    net_action.bt output:
    @[0, iperf3]: 2430857

    Because both cpu1 and cpu2 are on the numa node 0.

Example2:
    ------------------------------------------------------------
    iperf3 server:
    $ taskset -c 1 iperf3 -s
    [...]
    [  5]   0.00-1.00   sec  2.79 GBytes  24.0 Gbits/sec
    [...]

    iperf3 client:
    $ taskset -c 21 iperf3 -c 0
    [...]
    [  5]   0.00-1.00   sec  2.79 GBytes  24.0 Gbits/sec    0   1023 KBytes
    [...]

    net_action.bt output:
    @[0, iperf3]: 456193
    @[1, iperf3]: 684853

Because cpu1 on numa node0, cpu21 on numa node1, Processes that
need to communicate running on different nodes cause poor performance,
It is very easy to get numa with `numaid`.
  • Loading branch information
Rtoax committed Mar 31, 2022
1 parent c56676c commit afa7801
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to
## Unreleased

#### Added
- Add builtin: `numaid`
- [#2177](https://github.com/iovisor/bpftrace/pull/2177)
- Add builtin function: `cgroup_path`
- [#2055](https://github.com/iovisor/bpftrace/pull/2055)
- Limit number of generated BPF programs
Expand Down
1 change: 1 addition & 0 deletions docs/reference_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -1831,6 +1831,7 @@ NetworkManager:1155 /var/lib/sss/mc/passwd (deleted)
- `gid` - Group ID
- `nsecs` - Nanosecond timestamp
- `elapsed` - Nanoseconds since bpftrace initialization
- `numaid` - NUMA Node ID
- `cpu` - Processor ID
- `comm` - Process name
- `kstack` - Kernel stack trace
Expand Down
6 changes: 6 additions & 0 deletions man/adoc/bpftrace.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,12 @@ For string arguments use the `str()` call to retrieve the value.
| n/a
| PID of the child process

| numaid
| uint32
| 5.8
| numa_node_id
| ID of the NUMA node executing the BPF program

| cpu
| uint32
| 4.1
Expand Down
13 changes: 13 additions & 0 deletions src/ast/irbuilderbpf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,19 @@ CallInst *IRBuilderBPF::CreateGetUidGid()
return createCall(getuidgid_func, {}, "get_uid_gid");
}

CallInst *IRBuilderBPF::CreateGetNumaId()
{
// u32 bpf_get_numa_node_id(void)
// Return: NUMA Node ID
FunctionType *getnumaid_func_type = FunctionType::get(getInt64Ty(), false);
PointerType *getnumaid_func_ptr_type = PointerType::get(getnumaid_func_type, 0);
Constant *getnumaid_func = ConstantExpr::getCast(
Instruction::IntToPtr,
getInt64(libbpf::BPF_FUNC_get_numa_node_id),
getnumaid_func_ptr_type);
return createCall(getnumaid_func, {}, "get_numa_id");
}

CallInst *IRBuilderBPF::CreateGetCpuId()
{
// u32 bpf_raw_smp_processor_id(void)
Expand Down
1 change: 1 addition & 0 deletions src/ast/irbuilderbpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class IRBuilderBPF : public IRBuilder<>
CallInst *CreateGetPidTgid();
CallInst *CreateGetCurrentCgroupId();
CallInst *CreateGetUidGid();
CallInst *CreateGetNumaId();
CallInst *CreateGetCpuId();
CallInst *CreateGetCurrentTask();
CallInst *CreateGetRandom();
Expand Down
4 changes: 4 additions & 0 deletions src/ast/passes/codegen_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ void CodegenLLVM::visit(Builtin &builtin)
expr_ = b_.CreateLShr(uidgid, 32);
}
}
else if (builtin.ident == "numaid")
{
expr_ = b_.CreateGetNumaId();
}
else if (builtin.ident == "cpu")
{
expr_ = b_.CreateGetCpuId();
Expand Down
2 changes: 1 addition & 1 deletion src/ast/passes/semantic_analyser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ void SemanticAnalyser::visit(Builtin &builtin)
builtin.ident == "pid" || builtin.ident == "tid" ||
builtin.ident == "cgroup" || builtin.ident == "uid" ||
builtin.ident == "gid" || builtin.ident == "cpu" ||
builtin.ident == "rand")
builtin.ident == "rand" || builtin.ident == "numaid")
{
builtin.type = CreateUInt64();
if (builtin.ident == "cgroup" &&
Expand Down
2 changes: 1 addition & 1 deletion src/lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ hspace [ \t]
vspace [\n\r]
space {hspace}|{vspace}
path :(\\.|[_\-\./a-zA-Z0-9#\*])+
builtin arg[0-9]|args|cgroup|comm|cpid|cpu|ctx|curtask|elapsed|func|gid|nsecs|pid|probe|rand|retval|sarg[0-9]|tid|uid|username
builtin arg[0-9]|args|cgroup|comm|cpid|numaid|cpu|ctx|curtask|elapsed|func|gid|nsecs|pid|probe|rand|retval|sarg[0-9]|tid|uid|username
call avg|buf|cat|cgroupid|clear|count|delete|exit|hist|join|kaddr|kptr|ksym|lhist|macaddr|max|min|ntop|override|print|printf|cgroup_path|reg|signal|sizeof|stats|str|strftime|strncmp|sum|system|time|uaddr|uptr|usym|zero|path|unwatch

/* Don't add to this! Use builtin OR call not both */
Expand Down
16 changes: 16 additions & 0 deletions tests/codegen/builtin_numaid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "common.h"

namespace bpftrace {
namespace test {
namespace codegen {

TEST(codegen, builtin_numaid)
{
test("kprobe:f { @x = numaid }",

NAME);
}

} // namespace codegen
} // namespace test
} // namespace bpftrace
1 change: 1 addition & 0 deletions tests/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ TEST(Parser, builtin_variables)
test("kprobe:f { gid }", "Program\n kprobe:f\n builtin: gid\n");
test("kprobe:f { nsecs }", "Program\n kprobe:f\n builtin: nsecs\n");
test("kprobe:f { elapsed }", "Program\n kprobe:f\n builtin: elapsed\n");
test("kprobe:f { numaid }", "Program\n kprobe:f\n builtin: numaid\n");
test("kprobe:f { cpu }", "Program\n kprobe:f\n builtin: cpu\n");
test("kprobe:f { curtask }", "Program\n kprobe:f\n builtin: curtask\n");
test("kprobe:f { rand }", "Program\n kprobe:f\n builtin: rand\n");
Expand Down
1 change: 1 addition & 0 deletions tests/semantic_analyser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ TEST(semantic_analyser, builtin_variables)
test("kprobe:f { gid }", 0);
test("kprobe:f { nsecs }", 0);
test("kprobe:f { elapsed }", 0);
test("kprobe:f { numaid }", 0);
test("kprobe:f { cpu }", 0);
test("kprobe:f { curtask }", 0);
test("kprobe:f { rand }", 0);
Expand Down

0 comments on commit afa7801

Please sign in to comment.