Skip to content

Commit

Permalink
Standardize async id creation and setting
Browse files Browse the repository at this point in the history
This will make sure we don't forget to reset async ids
in 'create_reset_ids' and also makes sure we're incrementing
the id whenever we get it (instead of doing it at some
later, random instruction).

This is follow up to a conversation here:
bpftrace#3249 (comment)
  • Loading branch information
Jordan Rome authored and viktormalik committed Jul 1, 2024
1 parent 8b0a62a commit 7b9b4e9
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 97 deletions.
54 changes: 54 additions & 0 deletions src/ast/async_ids.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#pragma once

namespace bpftrace {
namespace ast {

// Add new ids here
#define FOR_LIST_OF_ASYNC_IDS(DO) \
DO(cat) \
DO(cgroup_path) \
DO(helper_error) \
DO(join) \
DO(mapped_printf) \
DO(non_map_print) \
DO(printf) \
DO(skb_output) \
DO(strftime) \
DO(str) \
DO(system) \
DO(time) \
DO(watchpoint)

#define DEFINE_MEMBER_VAR(id, ...) int _##id = 0;
#define DEFINE_ACCESS_METHOD(id, ...) \
int id() \
{ \
return _##id++; \
}
#define LOCAL_SAVE(id, ...) local_##id = _##id,
#define LOCAL_RESTORE(id, ...) this->_##id = local_##id;

class AsyncIds {
public:
explicit AsyncIds() = default;

FOR_LIST_OF_ASYNC_IDS(DEFINE_ACCESS_METHOD)

/*
* For 'create_reset_ids' return a lambda that has captured-by-value
* CodegenLLVM's async id state. Running the returned lambda will restore
* `CodegenLLVM`s async id state back to when this function was first called.
*/
std::function<void()> create_reset_ids()
{
return [FOR_LIST_OF_ASYNC_IDS(LOCAL_SAVE) this] {
FOR_LIST_OF_ASYNC_IDS(LOCAL_RESTORE)
};
}

private:
FOR_LIST_OF_ASYNC_IDS(DEFINE_MEMBER_VAR)
};

} // namespace ast
} // namespace bpftrace
10 changes: 7 additions & 3 deletions src/ast/irbuilderbpf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,12 @@ StructType *IRBuilderBPF::GetStructType(

IRBuilderBPF::IRBuilderBPF(LLVMContext &context,
Module &module,
BPFtrace &bpftrace)
: IRBuilder<>(context), module_(module), bpftrace_(bpftrace)
BPFtrace &bpftrace,
AsyncIds &async_ids)
: IRBuilder<>(context),
module_(module),
bpftrace_(bpftrace),
async_ids_(async_ids)
{
// Declare external LLVM function
FunctionType *pseudo_func_type = FunctionType::get(
Expand Down Expand Up @@ -2246,7 +2250,7 @@ void IRBuilderBPF::CreateHelperError(Value *ctx,
(bpftrace_.helper_check_level_ == 1 && return_zero_if_err(func_id)))
return;

int error_id = helper_error_id_++;
int error_id = async_ids_.helper_error();
bpftrace_.resources.helper_error_info[error_id] = { .func_id = func_id,
.loc = loc };

Expand Down
8 changes: 6 additions & 2 deletions src/ast/irbuilderbpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <optional>

#include "ast/ast.h"
#include "ast/async_ids.h"
#include "bpftrace.h"
#include "types.h"

Expand Down Expand Up @@ -51,7 +52,10 @@ using namespace llvm;

class IRBuilderBPF : public IRBuilder<> {
public:
IRBuilderBPF(LLVMContext &context, Module &module, BPFtrace &bpftrace);
IRBuilderBPF(LLVMContext &context,
Module &module,
BPFtrace &bpftrace,
AsyncIds &async_ids);

AllocaInst *CreateAllocaBPF(llvm::Type *ty, const std::string &name = "");
AllocaInst *CreateAllocaBPF(const SizedType &stype,
Expand Down Expand Up @@ -279,14 +283,14 @@ class IRBuilderBPF : public IRBuilder<> {
// both branches here:
// BEGIN { if (nsecs > 0) { $a = 1 } else { $a = 2 } print($a); exit() }
void hoist(const std::function<void()> &functor);
int helper_error_id_ = 0;

// Returns the integer type used to represent pointers in traced code.
llvm::Type *getPointerStorageTy(AddrSpace as);

private:
Module &module_;
BPFtrace &bpftrace_;
AsyncIds &async_ids_;

Value *CreateUSDTReadArgument(Value *ctx,
struct bcc_usdt_argument *argument,
Expand Down
Loading

0 comments on commit 7b9b4e9

Please sign in to comment.