[MDEV-17930] Optimizer trace: what's on the execution path when trace=off? Created: 2018-12-07  Updated: 2019-04-10  Resolved: 2019-04-10

Status: Closed
Project: MariaDB Server
Component/s: Optimizer
Fix Version/s: N/A

Type: Task Priority: Major
Reporter: Sergei Petrunia Assignee: Sergei Petrunia
Resolution: Done Votes: 0
Labels: None

Issue Links:
PartOf
is part of MDEV-6111 optimizer trace Closed

 Description   

Need to check again what is on the execution path (and so adds CPU overhead), when the tracing is not enabled.

The original tracing proposal had everything wrapped inside if-statements, something like

#define OPT_TRACE(thd_arg, X)  if (unlikely(thd_arg->trace_enabled))  { X; }

With tracing code using RAII objects, we can't have everything inside if-s.
MySQL doesn't have everything inside if-statements either.
What is the added overhead?



 Comments   
Comment by Sergei Petrunia [ 2018-12-11 ]

Examples from MySQL 8.0 codebase:

static TRP_RANGE *get_key_scans_params(PARAM *param, SEL_TREE *tree,
...
{
  ...
  Opt_trace_array ota(trace, "range_scan_alternatives");

Opt_trace_array ctor:

  Opt_trace_array(..):
      : Opt_trace_struct(ctx, false, key, feature) {}

Opt_trace struct constructor:

  Opt_trace_struct(Opt_trace_context *ctx_arg, ...
  {
    // A first inlined test
    if (unlikely(ctx_arg->is_started())) {
       ...

The destructor is also on the execution path

  ~Opt_trace_struct() {
    if (unlikely(started)) do_destruct();
  }

Comment by Sergei Petrunia [ 2018-12-12 ]

Besides creating/deleting the RAII tracing objects, the code also has calls to print things into trace:

      Opt_trace_object trace_idx(trace);
...
      trace_idx.add_utf8("index", param->table->key_info[keynr].name);
...
      trace_idx.add("chosen", true);

The printing methods start with a check of whether the trace is enabled:

  Opt_trace_struct &add(const char *key, bool value) {
    if (likely(!started)) return *this;
    return do_add(key, value);
  }

Comment by Sergei Petrunia [ 2018-12-12 ]

Summary

When the optimizer trace is off, the following things are on the execution path:

  • Construction/destruction of RAII tracing objects. Both constructor and destructor start with if (tracing_enabled), so no actions are performed if the trace is disabled.
  • Calls to print named values. The printing functions start with if (tracing_enabled).
  • Checks if tracing is enabled if (thd->opt_trace.is_started()) which wrap printing of complex structures.

I think this is as good as one can reasonably get.

Comment by Sergei Petrunia [ 2019-04-10 ]

Closing as "Done"

Generated at Thu Feb 08 08:40:13 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.