Skip to content

Tracing example crashes with bad alloc due to small stack size #67

@jellehierck

Description

@jellehierck

When running the tracing_example, my program crashes when a trace session is stopped with the following error message:

terminate called after throwing an instance of 'St9bad_alloc'
  what():  std::bad_alloc
Aborted (core dumped)

The crash happens on line line 87: app.StopTraceSession(); When using the debugger, I found out that the program crashes when the trace aggregator thread is joined in app.cc:218.

After the crash, a data1.perfetto file is created but only contains ~30 loops for me. This made me suspect that the crash might be due to a small stack size as you mentioned in your blog (part 4).

Note that this crash also occurs when I try to implement tracing in my own code, i.e. not just the tracing_example program.

Fix: increase thread stack size

Edit: this "fix" might make the problem occur less often, but not disappear (see this comment).

The default stack size set in ThreadConfig is 8 MB. Increasing the stack size to 16 MB caused fixed the crash for me. I did this by adding the following line to the thread_config section in tracing_example/main.cc:64:

int main() {
  cactus_rt::CyclicThreadConfig thread_config;
  thread_config.period_ns = 1'000'000;
  thread_config.cpu_affinity = std::vector<size_t>{2};
  thread_config.SetFifoScheduler(80);
  thread_config.stack_size = 16 * 1024 * 1024;  // <--- set larger stack size

  ...

After this fix, the program does not crash for me anymore and a correct data1.perfetto file is created with the entire trace.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions