bpftrace: Fail with "Too many open files" for script with few probes

What reproduces the bug? Provide code if possible.

Trying to track down an issue in PostgreSQL 17 I tried to use the following script:

uprobe:/usr/lib/postgresql/17/bin/postgres:MemoryContextSizeFailure {
  printf("%s: size=%lu, flags=%d\n", func, (uint64) arg1, (int32) arg2);
  @failed[tid] = true;
  print(ustack);
}

uprobe:/usr/lib/postgresql/17/bin/postgres:ReorderBufferAddInvalidations {
  @size[tid] = arg3;
}

uretprobe:/usr/lib/postgresql/17/bin/postgres:ReorderBufferAddInvalidations /@failed[tid]/ {
  printf("%s: size=%lu\n", func, (uint64) @size[tid]);
}

Idea is to print out caller information on a call to MemoryContextSizeFailure.

However, when executing it, it fails with

mats@fury:~/research/ebpf/postgres$ sudo bpftrace trace-mcxt.bt
Attaching 3 probes...
ERROR: failed to create map: '@failed': Too many open files
ERROR: failed to create map: '@size': Too many open files
ERROR: failed to create stack id map: Too many open files
ERROR: This might have happened because kernel.perf_event_max_stack is smaller than 127. Try to tweak this value with sysctl kernel.perf_event_max_stack=<new value>
ERROR: failed to create  map: Too many open files
ERROR: failed to create map: 'ringbuf_loss_counter': Too many open files
Creation of the required BPF maps has failed.
Make sure you have all the required permissions and are not confined (e.g. like
snapcraft does). `dmesg` will likely have useful output for further troubleshooting

Removing all probes but the probe on MemoryContextSizeFailure allow it to execute and print the stack for this case.

bpftrace --info output

System
  OS: Linux 6.8.0-62-generic #65-Ubuntu SMP PREEMPT_DYNAMIC Mon May 19 17:15:03 UTC 2025
  Arch: x86_64

Build
  version: v0.20.2
  LLVM: 18.1.3
  unsafe probe: no
  bfd: no
  libdw (DWARF support): yes

Kernel helpers
  probe_read: yes
  probe_read_str: yes
  probe_read_user: yes
  probe_read_user_str: yes
  probe_read_kernel: yes
  probe_read_kernel_str: yes
  get_current_cgroup_id: yes
  send_signal: yes
  override_return: yes
  get_boot_ns: yes
  dpath: no
  skboutput: no
  get_tai_ns: yes
  get_func_ip: yes
  jiffies64: yes

Kernel features
  Instruction limit: 1000000
  Loop support: yes
  btf: yes
  module btf: yes
  map batch: yes
  uprobe refcount (depends on Build:bcc bpf_attach_uprobe refcount): yes

Map types
  hash: yes
  percpu hash: yes
  array: yes
  percpu array: yes
  stack_trace: yes
  perf_event_array: yes
  ringbuf: yes

Probe types
  kprobe: yes
  tracepoint: yes
  perf_event: yes
  kfunc: yes
  kprobe_multi: yes
  uprobe_multi: yes
  raw_tp_special: yes
  iter: yes

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 15 (6 by maintainers)

Most upvoted comments

@pantaifengv0.19.0 是 bpftrace 的一个非常老的版本,大约两年前发布的。这个问题在新版本或 master 版本中还存在吗?

I tested version v0.23.5 and it seems that the issue of opening too many fd files in v0.23.5 has disappeared.

Setting the cache_user_symbol to "NONE" worked.

Full script is:

config = { cache_user_symbols = "NONE" }

uprobe:/usr/lib/postgresql/17/bin/postgres:MemoryContextSizeFailure {
  printf("%s: size=%lu, flags=%d\n", func, (uint64) arg1, (int32) arg2);
  @failed[tid] = 1;
  print(ustack);
}

uprobe:/usr/lib/postgresql/17/bin/postgres:ReorderBufferAddInvalidations {
  @size[tid] = arg3;
}

uretprobe:/usr/lib/postgresql/17/bin/postgres:ReorderBufferAddInvalidations /@failed[tid]/ {
  printf("%s: size=%lu\n", func, (uint64) @size[tid]);
}

Thanks @ajor and @jordalgo for the help.

Also I noticed you’re using true which is a boolean value that we haven’t added to the language yet. Try this @failed[tid] = 1;

This surprised me as well! Turns out that it’s importing an enum value named “true” from BTF! Better to use 1 for now as @jordalgo says.

The “Too many open files” error sounds like a problem with your environment though. @mkindahl Two things you could check:

  1. ulimit -n Have you got a very low limit set for the number of open files descriptors bpftrace is allowed to hold?
  2. How many files is bpftrace holding open at the point of the failure? Easiest way to check is probably to use either bpftrace or strace to monitor syscalls that bpftrace is making prior to the failure.