# libtracefs(3) Manual Page

## NAME

tracefs_cpu_open_mapped, tracefs_cpu_is_mapped, tracefs_mapped_is_supported, tracefs_cpu_map, tracefs_cpu_unmap - Memory mapping of the ring buffer

## SYNOPSIS

    #include <tracefs.h>

    bool tracefs_cpu_is_mapped(struct tracefs_cpu *tcpu);
    bool tracefs_mapped_is_supported(void);
    int tracefs_cpu_map(struct tracefs_cpu *tcpu);
    void tracefs_cpu_unmap(struct tracefs_cpu *tcpu);
    struct tracefs_cpu *tracefs_cpu_open_mapped(struct tracefs_instance *instance,
                                                int cpu, bool nonblock);

## DESCRIPTION

If the trace_pipe_raw supports memory mapping, this is usually a more efficient method to stream data from the kernel ring buffer than by reading it, as it does not require copying the memory that is being read.

If memory mapping is supported by the kernel and the application asks to use the memory mapping via either tracefs_cpu_map() or by tracefs_cpu_open_mapped() then the functions tracefs_cpu_read(3) and tracefs_cpu_read_buf(3) will use the mapping directly instead of calling the read system call.

Note, mapping will cause tracefs_cpu_buffered_read(3) and tracefs_cpu_buffered_read_buf(3) to act just like tracefs_cpu_read(3) and tracefs_cpu_read_buf(3) respectively as it doesn’t make sense to use a splice pipe when mapped. The kernel will do a copy for splice reads on mapping, and then another copy in the function when it can avoid the copying if the ring buffer is memory mapped.

If the tcpu is memory mapped it will also force tracefs_cpu_write(3) and tracefs_cpu_pipe(3) to copy from the mapping instead of using splice. Thus care must be used when determining to map the ring buffer or not, and why it does not get mapped by default.

The tracefs_cpu_is_mapped() function will return true if tcpu currently has its ring buffer memory mapped and false otherwise. This does not return whether or not that the kernel supports memory mapping, but that can usually be determined by calling tracefs_cpu_map().

The tracefs_mapped_is_supported() returns true if the ring buffer can be memory mapped.

The tracefs_cpu_map() function will attempt to map the ring buffer associated to tcpu if it is not already mapped.

The tracefs_cpu_unmap() function will unmap the ring buffer associated to tcpu if it is mapped.

The tracefs_cpu_open_mapped() is equivalent to calling tracefs_cpu_open(3) followed by tracefs_cpu_map() on the returned tcpu of tracefs_cpu_open(3). Note, this will still succeed if the mapping fails, in which case it acts the same as tracefs_cpu_open(3). If knowing if the mapping succeed or not, tracefs_cpu_is_mapped() should be called on the return tcpu.

## RETURN VALUE

tracefs_cpu_is_mapped() returns true if the given tcpu has its ring buffer memory mapped or false otherwise.

tracefs_mapped_is_supported() returns true if the tracing ring buffer can be memory mapped or false if it cannot be or an error occurred.

tracefs_cpu_map() returns 0 on success and -1 on error in mapping. If 0 is returned then tracefs_cpu_is_mapped() will return true afterward, or false if the mapping failed.

tracefs_cpu_open_mapped() returns an allocated tracefs_cpu on success of creation regardless if it succeed in mapping the ring buffer or not. It returns NULL for the same reasons tracefs_cpu_open(3) returns NULL. If success of mapping is to be known, then calling tracefs_cpu_is_mapped() afterward is required.

## 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

tracefs_cpu_open(3), tracefs_cpu_read(3), tracefs_cpu_read_buf(3), tracefs_cpu_buffered_read(3), tracefs_cpu_buffered_read_buf(3), libtracefs(3), libtraceevent(3), trace-cmd(1)

## AUTHOR

    Steven Rostedt <rostedt@goodmis.org>

## 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) 2022 Google, Inc. Free use of this software is granted under the terms of the GNU Public License (GPL).