Skip to content

Commit

Permalink
Add features, semantic analyzer and runtime tests for jiffies builtin
Browse files Browse the repository at this point in the history
  • Loading branch information
plxty committed Sep 19, 2023
1 parent 0d931b4 commit e94a931
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/reference_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -1943,7 +1943,7 @@ NetworkManager:1155 /var/lib/sss/mc/passwd (deleted)
- `uid` - User ID
- `gid` - Group ID
- `nsecs` - Nanosecond timestamp. Alias of [`nsecs()`](#36-nsecs-timestamps-and-time-deltas)
- `jiffies` - Jiffies in kernel
- `jiffies` - Jiffies of the kernel. In 32-bit system, using this builtin might be slower.
- `elapsed` - Nanoseconds since bpftrace initialization
- `numaid` - NUMA Node ID
- `cpu` - Processor ID
Expand Down
4 changes: 2 additions & 2 deletions man/adoc/bpftrace.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1047,9 +1047,9 @@ For string arguments use the `str()` call to retrieve the value.

| jiffies
| uint64
| 5.6
| 5.9
| get_jiffies_64
| Jiffies in kernel
| Jiffies of the kernel. In 32-bit system, using this builtin might be slower.

| pid
| uint64
Expand Down
2 changes: 2 additions & 0 deletions src/ast/irbuilderbpf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,8 @@ CallInst *IRBuilderBPF::CreateGetNs(TimestampMode ts, const location &loc)

CallInst *IRBuilderBPF::CreateJiffies64(const location &loc)
{
// u64 bpf_jiffies64()
// Return: jiffies (BITS_PER_LONG == 64) or jiffies_64 (otherwise)
FunctionType *jiffies64_func_type = FunctionType::get(getInt64Ty(), false);
return CreateHelperCall(libbpf::BPF_FUNC_jiffies64,
jiffies64_func_type,
Expand Down
6 changes: 6 additions & 0 deletions src/ast/passes/semantic_analyser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,12 @@ void SemanticAnalyser::visit(Builtin &builtin)
<< "BPF_FUNC_get_current_cgroup_id is not available for your kernel "
"version";
}
else if (builtin.ident == "jiffies" &&
!bpftrace_.feature_->has_helper_jiffies64())
{
LOG(ERROR, builtin.loc, err_)
<< "BPF_FUNC_jiffies64 is not available for your kernel version";
}
}
else if (builtin.ident == "curtask")
{
Expand Down
1 change: 1 addition & 0 deletions src/bpffeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ std::string BPFfeature::report(void)
<< " skboutput: " << to_str(has_skb_output())
<< " get_tai_ns: " << to_str(has_helper_ktime_get_tai_ns())
<< " get_func_ip: " << to_str(has_helper_get_func_ip())
<< " jiffies64: " << to_str(has_helper_jiffies64())

<< std::endl;

Expand Down
1 change: 1 addition & 0 deletions src/bpffeature.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class BPFfeature
DEFINE_HELPER_TEST(ktime_get_boot_ns, libbpf::BPF_PROG_TYPE_KPROBE);
DEFINE_HELPER_TEST(ktime_get_tai_ns, libbpf::BPF_PROG_TYPE_KPROBE);
DEFINE_HELPER_TEST(get_func_ip, libbpf::BPF_PROG_TYPE_TRACING);
DEFINE_HELPER_TEST(jiffies64, libbpf::BPF_PROG_TYPE_KPROBE);
DEFINE_PROG_TEST(kprobe, libbpf::BPF_PROG_TYPE_KPROBE);
DEFINE_PROG_TEST(tracepoint, libbpf::BPF_PROG_TYPE_TRACEPOINT);
DEFINE_PROG_TEST(perf_event, libbpf::BPF_PROG_TYPE_PERF_EVENT);
Expand Down
1 change: 1 addition & 0 deletions tests/mocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class MockBPFfeature : public BPFfeature
map_ringbuf_ = std::make_optional<bool>(has_features);
has_ktime_get_tai_ns_ = std::make_optional<bool>(has_features);
has_get_func_ip_ = std::make_optional<bool>(has_features);
has_jiffies64_ = std::make_optional<bool>(has_features);
};

void has_loop(bool has)
Expand Down
7 changes: 7 additions & 0 deletions tests/runtime/builtin
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,10 @@ EXPECT @[{.n=0x[0-9a-f]+,.c=120}]: 1
REQUIRES_FEATURE dwarf
TIMEOUT 5
BEFORE ./testprogs/uprobe_test

NAME jiffies
PROG i:ms:1 { printf("SUCCESS %d\n", jiffies); exit(); }
EXPECT SUCCESS -?[0-9][0-9]*
REQUIRES_FEATURE jiffies64
TIMEOUT 5
MIN_KERNEL 5.9
1 change: 1 addition & 0 deletions tests/runtime/engine/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ def __read_test_struct(test, test_suite):
"skboutput",
"get_tai_ns",
"get_func_ip",
"jiffies64",
}

for f in line.split(" "):
Expand Down
1 change: 1 addition & 0 deletions tests/runtime/engine/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ def __get_bpffeature():
bpffeature["skboutput"] = output.find("skboutput: yes") != -1
bpffeature["get_tai_ns"] = output.find("get_ktime_ns: yes") != -1
bpffeature["get_func_ip"] = output.find("get_func_ip: yes") != -1
bpffeature["jiffies64"] = output.find("jiffies64: yes") != -1
return bpffeature

@staticmethod
Expand Down
2 changes: 2 additions & 0 deletions tests/semantic_analyser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,11 @@ TEST(semantic_analyser, builtin_variables)
test("kprobe:f { probe }", 0);
test("tracepoint:a:b { args }", 0);
test("kprobe:f { fake }", 1);
test("kprobe:f { jiffies }", 0);

MockBPFfeature feature(false);
test(feature, "k:f { cgroup }", 1);
test(feature, "k:f { jiffies }", 1);
}

TEST(semantic_analyser, builtin_cpid)
Expand Down

0 comments on commit e94a931

Please sign in to comment.