15#ifndef LLDB_SOURCE_PLUGINS_PROCESS_LINUX_PERF_H
16#define LLDB_SOURCE_PLUGINS_PROCESS_LINUX_PERF_H
20#include "llvm/Support/Error.h"
23#include <linux/perf_event.h>
26namespace process_linux {
27namespace resource_handle {
70 std::unique_ptr<long, resource_handle::FileDescriptorDeleter>;
71using MmapUP = std::unique_ptr<void, resource_handle::MmapDeleter>;
109 static llvm::Expected<PerfEvent>
Init(perf_event_attr &attr,
110 std::optional<lldb::pid_t> pid,
111 std::optional<lldb::cpu_id_t> cpu,
112 std::optional<long> group_fd,
113 unsigned long flags);
127 static llvm::Expected<PerfEvent>
128 Init(perf_event_attr &attr, std::optional<lldb::pid_t> pid,
129 std::optional<lldb::cpu_id_t> core = std::nullopt);
160 size_t num_aux_pages,
161 bool data_buffer_write);
261 :
m_fd(new long(fd), resource_handle::FileDescriptorDeleter()),
271 llvm::Expected<resource_handle::MmapUP>
DoMmap(
void *addr,
size_t length,
274 llvm::StringRef buffer_name);
287 bool data_buffer_write);
316llvm::Expected<PerfEvent>
318 const PerfEvent *parent_perf_event =
nullptr);
Thin wrapper of the perf_event_open API.
llvm::Error MmapAuxBuffer(size_t num_aux_pages)
Mmap the aux buffer of the perf event.
llvm::Expected< std::vector< uint8_t > > GetReadOnlyAuxBuffer()
Read the aux buffer managed by this perf event assuming it was configured with PROT_READ permissions ...
resource_handle::MmapUP m_metadata_data_base
Metadata page and data section where perf samples are stored.
bool m_enabled
The state of the underlying perf_event.
llvm::Expected< std::vector< uint8_t > > GetReadOnlyDataBuffer()
Read the data buffer managed by this perf even assuming it was configured with PROT_READ permissions ...
perf_event_mmap_page & GetMetadataPage() const
Get the metadata page from the data section's mmap buffer.
resource_handle::MmapUP m_aux_base
AUX buffer is a separate region for high-bandwidth data streams such as IntelPT.
llvm::Error DisableWithIoctl()
Use the ioctl API to disable the perf event and all the events in its group.
size_t GetEffectiveDataBufferSize() const
llvm::Error MmapMetadataAndBuffers(size_t num_data_pages, size_t num_aux_pages, bool data_buffer_write)
Mmap the metadata page and the data and aux buffers of the perf event and expose them through PerfEve...
resource_handle::FileDescriptorUP m_fd
The file descriptor representing the perf event.
llvm::Error EnableWithIoctl()
Use the ioctl API to enable the perf event and all the events in its group.
long GetFd() const
Get the file descriptor associated with the perf event.
llvm::Error MmapMetadataAndDataBuffer(size_t num_data_pages, bool data_buffer_write)
Mmap the data buffer of the perf event.
llvm::ArrayRef< uint8_t > GetAuxBuffer() const
Get the AUX buffer.
llvm::Expected< resource_handle::MmapUP > DoMmap(void *addr, size_t length, int prot, int flags, long int offset, llvm::StringRef buffer_name)
Wrapper for mmap to provide custom error messages.
llvm::ArrayRef< uint8_t > GetDataBuffer() const
Get the data buffer from the data section's mmap buffer.
static llvm::Expected< PerfEvent > Init(perf_event_attr &attr, std::optional< lldb::pid_t > pid, std::optional< lldb::cpu_id_t > cpu, std::optional< long > group_fd, unsigned long flags)
Create a new performance monitoring event via the perf_event_open syscall.
PerfEvent(long fd, bool enabled)
Create new PerfEvent.
Custom deleter for a file descriptor.
void operator()(long *ptr)
Close and free the memory associated with the file descriptor pointer.
Custom deleter for the pointer returned by mmap.
void operator()(void *ptr)
Unmap the mmap'ed region.
size_t m_bytes
Size of the mmap'ed region, in bytes, to be unmapped.
MmapDeleter(size_t bytes=0)
Construct new MmapDeleter.
std::unique_ptr< long, resource_handle::FileDescriptorDeleter > FileDescriptorUP
std::unique_ptr< void, resource_handle::MmapDeleter > MmapUP
llvm::Expected< LinuxPerfZeroTscConversion > LoadPerfTscConversionParameters()
Load PerfTscConversionParameters from perf_event_mmap_page, if available.
llvm::Expected< PerfEvent > CreateContextSwitchTracePerfEvent(lldb::cpu_id_t cpu_id, const PerfEvent *parent_perf_event=nullptr)
Create a perf event that tracks context switches on a cpu.
A class that represents a running process on the host machine.