Skip to content

Commit

Permalink
Add builtin 'numaid'
Browse files Browse the repository at this point in the history
Example:
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 May 25, 2022
1 parent 40e6de0 commit e9b7cd3
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to
## Unreleased

#### Added
- Add builtin: `numaid`
- [#2177](https://github.com/iovisor/bpftrace/pull/2177)

#### Changed
#### Deprecated
#### Removed
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()
{
// long bpf_get_numa_node_id(void)
// Return: NUMA Node ID
FunctionType *getnumaid_func_type = FunctionType::get(getInt32Ty(), 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
5 changes: 5 additions & 0 deletions src/ast/passes/codegen_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ void CodegenLLVM::visit(Builtin &builtin)
expr_ = b_.CreateLShr(uidgid, 32);
}
}
else if (builtin.ident == "numaid")
{
Value *tmp = b_.CreateGetNumaId();
expr_ = b_.CreateZExt(tmp, b_.getInt64Ty());
}
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|bswap

/* 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
35 changes: 35 additions & 0 deletions tests/codegen/llvm/builtin_numaid.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
; ModuleID = 'bpftrace'
source_filename = "bpftrace"
target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
target triple = "bpf-pc-linux"

; Function Attrs: nounwind
declare i64 @llvm.bpf.pseudo(i64 %0, i64 %1) #0

define i64 @"kprobe:f"(i8* nocapture readnone %0) local_unnamed_addr section "s_kprobe:f_1" {
entry:
%"@x_val" = alloca i64, align 8
%"@x_key" = alloca i64, align 8
%get_numa_id = tail call i32 inttoptr (i64 42 to i32 ()*)()
%1 = zext i32 %get_numa_id to i64
%2 = bitcast i64* %"@x_key" to i8*
call void @llvm.lifetime.start.p0i8(i64 -1, i8* nonnull %2)
store i64 0, i64* %"@x_key", align 8
%3 = bitcast i64* %"@x_val" to i8*
call void @llvm.lifetime.start.p0i8(i64 -1, i8* nonnull %3)
store i64 %1, i64* %"@x_val", align 8
%pseudo = tail call i64 @llvm.bpf.pseudo(i64 1, i64 0)
%update_elem = call i64 inttoptr (i64 2 to i64 (i64, i64*, i64*, i64)*)(i64 %pseudo, i64* nonnull %"@x_key", i64* nonnull %"@x_val", i64 0)
call void @llvm.lifetime.end.p0i8(i64 -1, i8* nonnull %3)
call void @llvm.lifetime.end.p0i8(i64 -1, i8* nonnull %2)
ret i64 0
}

; Function Attrs: argmemonly mustprogress nofree nosync nounwind willreturn
declare void @llvm.lifetime.start.p0i8(i64 immarg %0, i8* nocapture %1) #1

; Function Attrs: argmemonly mustprogress nofree nosync nounwind willreturn
declare void @llvm.lifetime.end.p0i8(i64 immarg %0, i8* nocapture %1) #1

attributes #0 = { nounwind }
attributes #1 = { argmemonly mustprogress nofree nosync nounwind willreturn }
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
5 changes: 5 additions & 0 deletions tests/runtime/builtin
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ PROG i:ms:1 { printf("SUCCESS %d\n", elapsed); exit(); }
EXPECT SUCCESS -?[0-9][0-9]*
TIMEOUT 5

NAME numaid
PROG i:ms:1 { printf("SUCCESS %d\n", numaid); exit(); }
EXPECT SUCCESS -?[0-9][0-9]*
TIMEOUT 5

NAME cpu
PROG i:ms:1 { printf("SUCCESS %d\n", cpu); exit(); }
EXPECT SUCCESS -?[0-9][0-9]*
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 e9b7cd3

Please sign in to comment.