Skip to content

Commit

Permalink
hist: Support values creater than 4G
Browse files Browse the repository at this point in the history
Before we only supported values up to 4G. Unclear why that was. We have
a whole u64 to store bucket position. Supporting up to INT64_MAX still
only takes a u8. So might as well support up to INT64_MAX.

This fixes #856.
  • Loading branch information
danobi committed Nov 10, 2023
1 parent c589452 commit 006e68d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 17 deletions.
12 changes: 9 additions & 3 deletions src/ast/passes/codegen_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3066,7 +3066,7 @@ Function *CodegenLLVM::createLog2Function()
// result++;
// if (n == 0) return result;
// result++;
// for (int i = 4; i >= 0; i--)
// for (int i = 5; i >= 0; i--)
// {
// shift = (n >= (1<<(1<<i))) << i;
// n >>= shift;
Expand Down Expand Up @@ -3114,10 +3114,16 @@ Function *CodegenLLVM::createLog2Function()

// power-of-2 index, offset by +2
b_.CreateStore(b_.getInt64(2), result);
for (int i = 4; i >= 0; i--)
for (int i = 5; i >= 0; i--)
{
Value *n = b_.CreateLoad(b_.getInt64Ty(), n_alloc);
Value *shift = b_.CreateShl(b_.CreateIntCast(b_.CreateICmpSGE(b_.CreateIntCast(n, b_.getInt64Ty(), false), b_.getInt64(1 << (1<<i))), b_.getInt64Ty(), false), i);
Value *shift = b_.CreateShl(
b_.CreateIntCast(
b_.CreateICmpSGE(b_.CreateIntCast(n, b_.getInt64Ty(), false),
b_.getInt64(1ULL << (1 << i))),
b_.getInt64Ty(),
false),
i);
b_.CreateStore(b_.CreateLShr(n, shift), n_alloc);
b_.CreateStore(b_.CreateAdd(b_.CreateLoad(b_.getInt64Ty(), result), shift),
result);
Expand Down
4 changes: 2 additions & 2 deletions src/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,8 +712,8 @@ std::string JsonOutput::hist_to_str(const std::vector<uint64_t> &values,
}
else
{
long low = 1 << (i-2);
long high = (1 << (i-2+1)) - 1;
long low = 1ULL << (i - 2);
long high = (1ULL << (i - 2 + 1)) - 1;
res << "\"min\": " << low << ", \"max\": " << high << ", ";
}
res << "\"count\": " << values.at(i) / div;
Expand Down
33 changes: 21 additions & 12 deletions tests/codegen/llvm/call_hist.ll
Original file line number Diff line number Diff line change
Expand Up @@ -81,52 +81,61 @@ hist.is_zero: ; preds = %hist.is_not_less_th
hist.is_not_zero: ; preds = %hist.is_not_less_than_zero
store i64 2, i64* %1, align 8
%11 = load i64, i64* %2, align 8
%12 = icmp sge i64 %11, 65536
%12 = icmp sge i64 %11, 4294967296
%13 = zext i1 %12 to i64
%14 = shl i64 %13, 4
%14 = shl i64 %13, 5
%15 = lshr i64 %11, %14
store i64 %15, i64* %2, align 8
%16 = load i64, i64* %1, align 8
%17 = add i64 %16, %14
store i64 %17, i64* %1, align 8
%18 = load i64, i64* %2, align 8
%19 = icmp sge i64 %18, 256
%19 = icmp sge i64 %18, 65536
%20 = zext i1 %19 to i64
%21 = shl i64 %20, 3
%21 = shl i64 %20, 4
%22 = lshr i64 %18, %21
store i64 %22, i64* %2, align 8
%23 = load i64, i64* %1, align 8
%24 = add i64 %23, %21
store i64 %24, i64* %1, align 8
%25 = load i64, i64* %2, align 8
%26 = icmp sge i64 %25, 16
%26 = icmp sge i64 %25, 256
%27 = zext i1 %26 to i64
%28 = shl i64 %27, 2
%28 = shl i64 %27, 3
%29 = lshr i64 %25, %28
store i64 %29, i64* %2, align 8
%30 = load i64, i64* %1, align 8
%31 = add i64 %30, %28
store i64 %31, i64* %1, align 8
%32 = load i64, i64* %2, align 8
%33 = icmp sge i64 %32, 4
%33 = icmp sge i64 %32, 16
%34 = zext i1 %33 to i64
%35 = shl i64 %34, 1
%35 = shl i64 %34, 2
%36 = lshr i64 %32, %35
store i64 %36, i64* %2, align 8
%37 = load i64, i64* %1, align 8
%38 = add i64 %37, %35
store i64 %38, i64* %1, align 8
%39 = load i64, i64* %2, align 8
%40 = icmp sge i64 %39, 2
%40 = icmp sge i64 %39, 4
%41 = zext i1 %40 to i64
%42 = shl i64 %41, 0
%42 = shl i64 %41, 1
%43 = lshr i64 %39, %42
store i64 %43, i64* %2, align 8
%44 = load i64, i64* %1, align 8
%45 = add i64 %44, %42
store i64 %45, i64* %1, align 8
%46 = load i64, i64* %1, align 8
ret i64 %46
%46 = load i64, i64* %2, align 8
%47 = icmp sge i64 %46, 2
%48 = zext i1 %47 to i64
%49 = shl i64 %48, 0
%50 = lshr i64 %46, %49
store i64 %50, i64* %2, align 8
%51 = load i64, i64* %1, align 8
%52 = add i64 %51, %49
store i64 %52, i64* %1, align 8
%53 = load i64, i64* %1, align 8
ret i64 %53
}

; Function Attrs: argmemonly nofree nosync nounwind willreturn
Expand Down
5 changes: 5 additions & 0 deletions tests/runtime/call
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ EXPECT @bytes: *\n[\[(].*
AFTER ./testprogs/syscall read
TIMEOUT 5

NAME hist_10g
RUN {{BPFTRACE}} -q -f json -e 'BEGIN { @ = hist(10 * 1024 * 1024 * 1024); exit(); }' | python3 -c 'import sys,json; print(json.load(sys.stdin) == json.load(open("runtime/outputs/hist_10g.json")))'
EXPECT ^True$
TIMEOUT 1

NAME lhist
PROG kretprobe:vfs_read { @bytes = lhist(retval, 0, 10000, 1000); exit()}
EXPECT @bytes: *\n[\[(].*
Expand Down
12 changes: 12 additions & 0 deletions tests/runtime/outputs/hist_10g.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"type": "hist",
"data": {
"@": [
{
"min": 8589934592,
"max": 17179869183,
"count": 1
}
]
}
}

0 comments on commit 006e68d

Please sign in to comment.