---
name: "tracefs_cpu_close"
section: "3"
platform: "linux"
package: "libtracefs-doc"
version: "1.8.1-1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/tracefs_cpu_open"
---

# tracefs\_cpu\_close(3)

Opening trace\_pipe\_raw data for reading

Part of [tracefs\_cpu\_open(3)](/tracefs_cpu_open/3.md).

## NAME

tracefs\_cpu\_open, tracefs\_cpu\_close, tracefs\_cpu\_alloc\_fd, tracefs\_cpu\_free\_fd, tracefs\_cpu\_snapshot\_open - Opening trace\_pipe\_raw data for reading

## SYNOPSIS

```
#include <tracefs.h>
struct tracefs_cpu *tracefs_cpu_open(struct tracefs_instance *instance,
                                     int cpu, bool nonblock);
void tracefs_cpu_close(struct tracefs_cpu *tcpu);
struct tracefs_cpu *tracefs_cpu_alloc_fd(int fd, int subbuf_size, bool nonblock);
void tracefs_cpu_free_fd(struct tracefs_cpu *tcpu);
struct tracefs_cpu *tracefs_cpu_snapshot_open(struct tracefs_instance *instance,
                                             int cpu, bool nonblock);
```

## DESCRIPTION

This set of APIs can be used to open the raw data from the trace\_pipe\_raw files in the tracefs file system in oder to read them with the [**tracefs\_cpu\_read**(3)](/tracefs_cpu_read/3.md) functions.

The **tracefs\_cpu\_open()** creates a descriptor that can read the tracefs trace\_pipe\_raw file for a given *cpu* in a given *instance*. If *instance* is NULL than the toplevel trace\_pipe\_raw file is used.

The **tracefs\_cpu\_close()** closes all the file descriptors associated to the trace\_pipe\_raw opened by **tracefs\_cpu\_open()**.

The **tracefs\_cpu\_alloc\_fd()** will create a tracefs\_cpu descriptor from an existing file descriptor *fd*. This is useful to use when connecting to a socket or pipe where the other end is feeding raw tracing data in the same format as the trace\_pipe\_raw file would (like in guest to host tracing). The caller is responsible for determining the *subbuf\_size* that will be used to break up the sub-buffers being read by the file descriptor. The *nonblock* is treated the same as the same parameter in **tracefs\_cpu\_open()**.

The **tracefs\_cpu\_free\_fd()** is used to free the descriptor returned by **tracefs\_cpu\_alloc\_fd()**. It does all the clean up that **tracefs\_cpu\_close()** performs, and that could also be used to free up the descriptor created by **tracefs\_cpu\_alloc\_fd()** but will also close the file descriptor passed in. Note that **tracefs\_cpu\_free\_fd()** should not be used on the descriptor returned by **tracefs\_cpu\_open()** as it will not close the file descriptor created by it.

The **tracefs\_cpu\_snapshot\_open()** is similar to **tracefs\_cpu\_open()** except that it opens the snapshot buffer (see [**tracefs\_snapshot\_snap**(3)](/tracefs_snapshot_snap/3.md)). The snapshot buffer does not have a writer to it, it is only created by a snapshot action that swaps the current ring buffer with the snapshot buffer. The *nonblock*, when false, acts a little differently here too. Reads are not affected by the "buffer\_percent" file. If the snapshot buffer is empty, it will block until a new snapshot happens.

## RETURN VALUE

The **tracefs\_cpu\_open()** and \*tracefs\_cpu\_snapshot\_open() both return a struct tracefs\_cpu descriptor that can be used by the other functions or NULL on error.

The **tracefs\_cpu\_alloc\_fd()** returns a struct tracefs\_cpu descriptor that can be used by the [**tracefs\_cpu\_read**(3)](/tracefs_cpu_read/3.md) related functions, where the descriptor will be reading the passed in *fd* file descriptor.

## EXAMPLE

See [**tracefs\_cpu\_read**(3)](/tracefs_cpu_read/3.md) for an 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)](/libtracefs/3.md), [**libtraceevent**(3)](/libtraceevent/3.md), [**trace-cmd**(1)](/trace-cmd/1.md)

## AUTHOR

```
Steven Rostedt <rostedt@goodmis.org[1]>
```

## REPORTING BUGS

Report bugs to \<**linux-trace-devel@vger.kernel.org**\[2\]\>

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

## NOTES

- 1\.

  rostedt@goodmis.org

  mailto:rostedt@goodmis.org

- 2\.

  linux-trace-devel@vger.kernel.org

  mailto:linux-trace-devel@vger.kernel.org
