bpftrace icon indicating copy to clipboard operation
bpftrace copied to clipboard

Move all script-dependent stack allocations to scratch maps

Open danobi opened this issue 1 year ago • 1 comments

Obviously at the program level the aggregate stack usage is always script dependent. This issue is specific to individual stack allocations that are script dependent. This attempts to tackle the issue of big strings being "infectious", in that it can cause other language construct stack allocations to balloon.

To that end, I audited every CreateAllocaBPF() call in the codebase and compiled the following list of script-dependent allocas. The easily fixable ones are marked as such. The rest need to use scratch maps.

  • Reading from a map https://github.com/bpftrace/bpftrace/blob/b2e255870ba010d4a7e4852bffcf1c567b016fd0/src/ast/irbuilderbpf.cpp#L584
  • Instantiating a tuple https://github.com/bpftrace/bpftrace/blob/b2e255870ba010d4a7e4852bffcf1c567b016fd0/src/ast/passes/codegen_llvm.cpp#L2248
  • Deleting a non-clearable map element https://github.com/bpftrace/bpftrace/blob/b2e255870ba010d4a7e4852bffcf1c567b016fd0/src/ast/passes/codegen_llvm.cpp#L765
  • Updating a map element https://github.com/bpftrace/bpftrace/blob/b2e255870ba010d4a7e4852bffcf1c567b016fd0/src/ast/passes/codegen_llvm.cpp#L2259-L2316
    • Some of these allocations are only word sized, though. But the others are script-dependent
  • Getting a map key https://github.com/bpftrace/bpftrace/blob/b2e255870ba010d4a7e4852bffcf1c567b016fd0/src/ast/passes/codegen_llvm.cpp#L2854 https://github.com/bpftrace/bpftrace/blob/b2e255870ba010d4a7e4852bffcf1c567b016fd0/src/ast/passes/codegen_llvm.cpp#L2867 https://github.com/bpftrace/bpftrace/blob/b2e255870ba010d4a7e4852bffcf1c567b016fd0/src/ast/passes/codegen_llvm.cpp#L2907
  • Using a ternary https://github.com/bpftrace/bpftrace/blob/b2e255870ba010d4a7e4852bffcf1c567b016fd0/src/ast/passes/codegen_llvm.cpp#L1885-L1890
    • The stack allocations here seem unnecessary. Can probably remove them.
  • Reading an element from a data structure https://github.com/bpftrace/bpftrace/blob/b2e255870ba010d4a7e4852bffcf1c567b016fd0/src/ast/passes/codegen_llvm.cpp#L3963 https://github.com/bpftrace/bpftrace/blob/b2e255870ba010d4a7e4852bffcf1c567b016fd0/src/ast/passes/codegen_llvm.cpp#L4003
    • Pre-existing and unrelated to big strings
  • Dereferencing a pointer https://github.com/bpftrace/bpftrace/blob/b2e255870ba010d4a7e4852bffcf1c567b016fd0/src/ast/passes/codegen_llvm.cpp#L1840
    • This one is pre-existing and is unrelated to big strings

danobi avatar Aug 30 '24 20:08 danobi

Good news is the issue is not too bad yet. Three broad categories:

  • map keys
  • map values
  • tuples

They are all amenable to current scratch map approach

danobi avatar Aug 30 '24 20:08 danobi

That was the last one, yay!

danobi avatar Nov 08 '24 00:11 danobi