SQLHIST(1)

# SQLHIST(1) Manual Page

## NAME

sqlhist - Tool that uses SQL language to create / show creation of tracefs histograms and synthetic events.

## SYNOPSIS

sqlhist [OPTIONS] [SQL-select-command]

## DESCRIPTION

The sqlhist(1) will take an SQL like statement to create tracefs histograms and synthetic events that can perform various actions for various handling of the data.

The tracefs file system interfaces with the Linux tracing infrastructure that has various dynamic and static events through out the kernel. Each of these events can have a "histogram" attached to it, where the fields of the event will define the buckets of the histogram.

A synthetic event is a way to attach two separate events and use the fields and time stamps of those events to create a new dynamic event. This new dynamic event is call a synthetic event. The fields of each event can have simple calculations done on them where, for example, the delta between a field of one event to a field of the other event can be taken. This also works for the time stamps of the events where the time delta between the two events can also be extracted and placed into the synthetic event.

Other actions can be done from the fields of the events. A snapshot can be taken of the kernel ring buffer a variable used in the synthetic event creating hits a max, or simply changes.

The commands to create histograms and synthetic events are complex and not easy to remember. sqlhist is used to convert SQL syntax into the commands needed to create the histogram or synthetic event.

The SQL-select-command is a SQL string defined by tracefs_sql(3).

Note, this must be run as root (or sudo) as interacting with the tracefs directory requires root privilege, unless the -t option is given with a copy of the tracefs directory and its events.

The sqlhist is a simple program where its code actual exists in the tracefs_sql(3) man page.

## OPTIONS

-n name

  The name of the synthetic event to create. This event can then be used like any other event, and enabled via trace-cmd(1).

-t tracefs-dir

  In order to test this out as non root user, a copy of the tracefs directory can be used, and passing that directory with this option will allow the program to work. Obviously, -e will not work as non-root because it will not be able to execute.

      # mkdir /tmp/tracing
      # cp -r /sys/kernel/tracing/events /tmp/tracing
      # exit
      $ ./sqlhist -t /tmp/tracing ...

-e

  Not only display the commands to create the histogram, but also execute them. This requires root privilege.

-f file

  Instead of reading the SQL commands from the command line, read them from file. If file is - then read from standard input.

-m var

  Do the given action when the variable var hits a new maximum. This can not be used with -c.

-c var

  Do the given action when the variable var changes its value. This can not be used with -m.

-s

  Perform a snapshot instead of calling the synthetic event.

-T

  Perform both a snapshot and trace the synthetic event.

-S fields[,fields]

  Save the given fields. The fields must be fields of the "end" event given in the SQL-select-command

-B instance

  For simple statements that only produce a histogram, the instance given here will be where the histogram will be created. This is ignored for full synthetic event creation, as sythetic events have a global affect on all tracing instances, where as, histograms only affect a single instance.

## EXAMPLES

Create the sqlhist executable:

As described above, for testing purposes, make a copy of the event directory:

For an example of simple histogram output using the copy of the tracefs directory.

Produces the output:

Which could be used by root:

Note, although the examples use uppercase for the SQL keywords, they do not have to be. SELECT could also be select or even sElEcT.

By using the full SQL language, synthetic events can be made and processed. For example, using sqlhist along with trace-cmd(1), wake up latency can be recorded by creating a synthetic event by attaching the sched_waking and the sched_switch events.

Here’s the options for sqlhist explained:

-n wakeup_lat

  Name the synthetic event to use wakeup_lat.

-e

  Execute the commands that are printed.

-T

  Perform both a trace action and then a snapshot action (swap the buffer into the static snapshot buffer).

-m lat

  Trigger the actions whenever lat hits a new maximum value.

Now a breakdown of the SQL statement:

end.next_comm AS comm

  Save the sched_switch field next_comm and place it into the comm field of the wakeup_lat synthetic event.

(end.TIMESTAMP_USECS - start.TIMESTAMP_USECS) AS lat

  Take the delta of the time stamps from the sched_switch event and the sched_waking event. As time stamps are usually recorded in nanoseconds, TIMESTAMP would give the full nanosecond time stamp, but here, the TIMESTAMP_USECS will truncate it into microseconds. The value is saved in the variable lat, which will also be recorded in the synthetic event.

FROM sched_waking AS start JOIN sched_switch AS end ON start.pid = end.next_pid

  Create the synthetic event by joining sched_waking to sched_switch, matching the sched_waking pid field with the sched_switch next_pid field. Also make start an alias for sched_waking and end an alias for sched_switch which then an use start and end as a subsitute for sched_waking and sched_switch respectively through out the rest of the SQL statement.

WHERE end.next_prio < 100 && end.next_comm == "cyclictest"

  Filter the logic where it executes only if sched_waking next_prio field is less than 100. (Note, in the Kernel, priorities are inverse, and the real-time priorities are represented from 0-100 where 0 is the highest priority). Also only trace when the next_comm (the task scheduling in) of the sched_switch event has the name "cyclictest".

For the trace-cmd(3) command:

trace-cmd start

  Enables tracing (does not record to a file).

-e all

  Enable all events

-e wakeup_lat -R stacktrace

  have the "wakeup_lat" event (our synthetic event) enable the stacktrace trigger, were for every instance of the "wakeup_lat" event, a kernel stack trace will be recorded in the ring buffer.

After calling cyclictest (a real-time tool to measure wakeup latency), read the snapshot buffer.

trace-cmd show -s

  trace-cmd show reads the kernel ring buffer, and the -s option will read the snapshot buffer instead of the normal one.

    We see on the "wakeup_lat" event happened on CPU 2, with a wake up latency 17 microseconds.

This can be extracted into a trace.dat file that trace-cmd(3) can read and do further analysis, as well as kernelshark.

## BUGS

As sqlhist is just example code from a man page, it is guaranteed to contain lots of bugs. For one thing, not all error paths are covered properly.

## SEE ALSO

trace-cmd(1), tracefs_sql(3)

## AUTHOR

Written by Steven Rostedt, <rostedt@goodmis.org>

## RESOURCES

https://git.kernel.org/pub/scm/utils/trace-cmd/trace-cmd.git/

## COPYING

Copyright (C) 2021 , Inc. Free use of this software is granted under the terms of the GNU Public License (GPL).