# libtracefs(3) Manual Page

## NAME

tracefs_hist_add_sort_key, tracefs_hist_set_sort_key, tracefs_hist_sort_key_direction, tracefs_hist_add_name, tracefs_hist_append_filter, tracefs_hist_echo_cmd, tracefs_hist_command, tracefs_hist_get_name, tracefs_hist_get_event, tracefs_hist_get_system - Update and describe an event histogram

## SYNOPSIS

    #include <tracefs.h>

    int tracefs_hist_add_sort_key(struct tracefs_hist *hist,
                                  const char *sort_key);

    int tracefs_hist_set_sort_key(struct tracefs_hist *hist,
                                  const char *sort_key, …);
    int tracefs_hist_sort_key_direction(struct tracefs_hist *hist,
                                        const char *sort_key,
                                        enum tracefs_hist_sort_direction dir);

    int tracefs_hist_add_name(struct tracefs_hist *hist, const char *name);

    int tracefs_hist_append_filter(struct tracefs_hist *hist,
                                   enum tracefs_filter type,
                                   const char *field,
                                   enum tracefs_compare compare,
                                   const char *val);

    int tracefs_hist_echo_cmd(struct trace_seq *s, struct tracefs_instance *instance,
                              struct tracefs_hist *hist,
                              enum tracefs_hist_command command);

    int tracefs_hist_command(struct tracefs_instance *instance,
                             struct tracefs_hist *hist,
                             enum tracefs_hist_command command);

    const char *tracefs_hist_get_name(struct tracefs_hist *hist);

    const char *tracefs_hist_get_event(struct tracefs_hist *hist);

    const char *tracefs_hist_get_system(struct tracefs_hist *hist);

## DESCRIPTION

Event histograms are created by the trigger file in the event directory. The syntax can be complex and difficult to get correct. This API handles the syntax, and facilitates the creation and interaction with the event histograms. See https://www.kernel.org/doc/html/latest/trace/histogram.html for more information.

tracefs_hist_add_sort_key() will add a key to sort on. The hist is the histogram descriptor to add the sort key to. The sort_key is a string that must match either an already defined key of the histogram, or an already defined value. If hist already has sorting keys (previously added) the new sort_key will have lower priority(be secondary or so on) when sorting.

tracefs_hist_set_sort_key() will reset the list of key to sort on. The hist is the histogram descriptor to reset the sort key to. The sort_key is a string that must match either an already defined key of the histogram, or an already defined value. Multiple sort keys may be added to denote a secondary, sort order and so on, but all sort keys must match an existing key or value, or be TRACEFS_HIST_HITCOUNT. The last parameter of tracefs_hist_add_sort_key() must be NULL.

tracefs_hist_sort_key_direction() allows to change the direction of an existing sort key of hist. The sort_key is the sort key to change, and dir can be either TRACEFS_HIST_SORT_ASCENDING or TRACEFS_HIST_SORT_DESCENDING, to make the direction of the sort key either ascending or descending respectively.

tracefs_hist_add_name() adds a name to a histogram. A histogram may be named and if the name matches between more than one event, and they have compatible keys, the multiple histograms with the same name will be merged into a single histogram (shown by either event’s hist file). The hist is the histogram to name, and the name is the name to give it.

tracefs_hist_append_filter() creates a filter or appends to it for the histogram event. Depending on type, it will build a string of tokens for parenthesis or logic statements, or it may add a comparison of field to val based on compare.

If type is: TRACEFS_FILTER_COMPARE - See below TRACEFS_FILTER_AND - Append "&&" to the filter TRACEFS_FILTER_OR - Append "||" to the filter TRACEFS_FILTER_NOT - Append "!" to the filter TRACEFS_FILTER_OPEN_PAREN - Append "(" to the filter TRACEFS_FILTER_CLOSE_PAREN - Append ")" to the filter

field, compare, and val are ignored unless type is equal to TRACEFS_FILTER_COMPARE, then compare will be used for the following:

TRACEFS_COMPARE_EQ - field == val

TRACEFS_COMPARE_NE - field != val

TRACEFS_COMPARE_GT - field > val

TRACEFS_COMPARE_GE - field >= val

TRACEFS_COMPARE_LT - field < val

TRACEFS_COMPARE_LE - field <= val

TRACEFS_COMPARE_RE - field ~ "val" : where field is a string.

TRACEFS_COMPARE_AND - field & val : where field is a flags field.

trace_hist_echo_cmd() prints the commands needed to create the given histogram in the given instance, or NULL for the top level, into the seq. The command that is printed is described by command and shows the functionality that would be done by tracefs_hist_command(3).

tracefs_hist_command() is called to process a command on the histogram hist for its event in the given instance, or NULL for the top level. The cmd can be one of:

TRACEFS_HIST_CMD_START or zero to start execution of the histogram.

TRACEFS_HIST_CMD_PAUSE to pause the given histogram.

TRACEFS_HIST_CMD_CONT to continue a paused histogram.

TRACEFS_HIST_CMD_CLEAR to reset the values of a histogram.

TRACEFS_HIST_CMD_DESTROY to destroy the histogram (undo a START).

The below functions are wrappers to tracefs_hist_command() to make the calling conventions a bit easier to understand what is happening.

## KEY TYPES

tracefs_hist_alloc_nd() and tracefs_hist_add_key() both add a key and requires that key to have a type. The types may be:

TRACEFS_HIST_KEY_NORMAL or zero (0) which is to not modify the type.

TRACEFS_HIST_KEY_HEX to display the key in hex.

TRACEFS_HIST_KEY_SYM to display the key as a kernel symbol (if found). If the key is an address, this is useful as it will display the function names instead of just a number.

TRACEFS_HIST_KEY_SYM_OFFSET similar to TRACEFS_HIST_KEY_SYM but will also include the offset of the function to match the exact address.

TRACEFS_HIST_KEY_SYSCALL If the key is a system call "id" (the number passed from user space to the kernel to tell it what system call it is calling), then the name of the system call is displayed.

TRACEFS_HIST_KEY_EXECNAME If "common_pid" is the key (the pid of the executing task), instead of showing the number, show the name of the running task.

TRACEFS_HIST_KEY_LOG will display the key in a binary logarithmic scale.

TRACEFS_HIST_KEY_USECS for use with "common_timestamp" or TRACEFS_HIST_TIMESTAMP, in which case it will show the timestamp in microseconds instead of nanoseconds.

## RETURN VALUE

tracefs_hist_get_name() returns the name of the histogram or NULL on error. The returned string belongs to the histogram object and is freed with the histogram by tracefs_hist_free().

tracefs_hist_get_event() returns the event name of the histogram or NULL on error. The returned string belongs to the histogram object and is freed with the histogram by tracefs_hist_free().

tracefs_hist_get_system() returns the system name of the histogram or NULL on error. The returned string belongs to the histogram object and is freed with the histogram by tracefs_hist_free().

tracefs_hist_alloc_nd() returns an allocated histogram descriptor which must be freed by tracefs_hist_free() or NULL on error.

tracefs_hist_get_name(), tracefs_hist_get_event() and tracefs_hist_get_system() return strings owned by the histogram object.

All the other functions return zero on success or -1 on error.

## EXAMPLE

## FILES

    tracefs.h
            Header file to include in order to have access to the library APIs.
    -ltracefs
            Linker switch to add when building a program that uses the library.

## SEE ALSO

libtracefs(3), libtraceevent(3), trace-cmd(1), tracefs_hist_pause(3), tracefs_hist_continue(3), tracefs_hist_reset(3)

## AUTHOR

    Steven Rostedt <rostedt@goodmis.org>
    Tzvetomir Stoyanov <tz.stoyanov@gmail.com>
    sameeruddin shaik <sameeruddin.shaik8@gmail.com>

## REPORTING BUGS

Report bugs to <linux-trace-devel@vger.kernel.org>

## LICENSE

libtracefs is Free Software licensed under the GNU LGPL 2.1

## RESOURCES

https://git.kernel.org/pub/scm/libs/libtrace/libtracefs.git/

## COPYING

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