LLDB mainline
|
Thin wrapper of the perf_event_open API. More...
#include <Perf.h>
Public Member Functions | |
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 PerfEvent::GetMetadataPage() , PerfEvent::GetDataBuffer() and PerfEvent::GetAuxBuffer(). | |
long | GetFd () const |
Get the file descriptor associated with the perf event. | |
perf_event_mmap_page & | GetMetadataPage () const |
Get the metadata page from the data section's mmap buffer. | |
llvm::ArrayRef< uint8_t > | GetDataBuffer () const |
Get the data buffer from the data section's mmap buffer. | |
llvm::ArrayRef< uint8_t > | GetAuxBuffer () const |
Get the AUX buffer. | |
llvm::Expected< std::vector< uint8_t > > | GetReadOnlyAuxBuffer () |
Read the aux buffer managed by this perf event assuming it was configured with PROT_READ permissions only, which indicates that the buffer is automatically wrapped and overwritten by the kernel or hardware. | |
llvm::Expected< std::vector< uint8_t > > | GetReadOnlyDataBuffer () |
Read the data buffer managed by this perf even assuming it was configured with PROT_READ permissions only, which indicates that the buffer is automatically wrapped and overwritten by the kernel or hardware. | |
llvm::Error | DisableWithIoctl () |
Use the ioctl API to disable the perf event and all the events in its group. | |
llvm::Error | EnableWithIoctl () |
Use the ioctl API to enable the perf event and all the events in its group. | |
size_t | GetEffectiveDataBufferSize () const |
bool | IsEnabled () const |
Static Public Member Functions | |
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. | |
static llvm::Expected< PerfEvent > | Init (perf_event_attr &attr, std::optional< lldb::pid_t > pid, std::optional< lldb::cpu_id_t > core=std::nullopt) |
Create a new performance monitoring event via the perf_event_open syscall with "default" values for the cpu, group_fd and flags arguments. | |
Private Member Functions | |
PerfEvent (long fd, bool enabled) | |
Create new PerfEvent. | |
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::Error | MmapMetadataAndDataBuffer (size_t num_data_pages, bool data_buffer_write) |
Mmap the data buffer of the perf event. | |
llvm::Error | MmapAuxBuffer (size_t num_aux_pages) |
Mmap the aux buffer of the perf event. | |
Private Attributes | |
resource_handle::FileDescriptorUP | m_fd |
The file descriptor representing the perf event. | |
resource_handle::MmapUP | m_metadata_data_base |
Metadata page and data section where perf samples are stored. | |
resource_handle::MmapUP | m_aux_base |
AUX buffer is a separate region for high-bandwidth data streams such as IntelPT. | |
bool | m_enabled |
The state of the underlying perf_event. | |
Thin wrapper of the perf_event_open API.
Exposes the metadata page and data and aux buffers of a perf event. Handles the management of the event's file descriptor and mmap'ed regions.
|
inlineprivate |
Error PerfEvent::DisableWithIoctl | ( | ) |
Use the ioctl API to disable the perf event and all the events in its group.
This doesn't terminate the perf event.
This is no-op if the perf event is already disabled.
Definition at line 292 of file Perf.cpp.
Referenced by lldb_private::process_linux::IntelPTSingleBufferTrace::Pause().
|
private |
Wrapper for mmap to provide custom error messages.
The parameters are directly forwarded to a mmap syscall, for information on the parameters visit https://man7.org/linux/man-pages/man2/mmap.2.html.
The value of GetFd() is passed as the fd argument to mmap.
Error PerfEvent::EnableWithIoctl | ( | ) |
Use the ioctl API to enable the perf event and all the events in its group.
This is no-op if the perf event is already enabled.
Definition at line 307 of file Perf.cpp.
Referenced by lldb_private::process_linux::IntelPTSingleBufferTrace::Resume().
ArrayRef< uint8_t > PerfEvent::GetAuxBuffer | ( | ) | const |
Get the AUX buffer.
AUX buffer is a region for high-bandwidth data streams such as IntelPT. This is separate from the metadata and data buffer.
This should be called only after PerfEvent::MmapMetadataAndBuffers, otherwise a failure might happen.
Definition at line 188 of file Perf.cpp.
Referenced by lldb_private::process_linux::IntelPTSingleBufferTrace::GetIptTraceSize().
ArrayRef< uint8_t > PerfEvent::GetDataBuffer | ( | ) | const |
Get the data buffer from the data section's mmap buffer.
The data buffer is the region of the data section's mmap buffer where perf sample data is located.
This should be called only after PerfEvent::MmapMetadataAndBuffers, otherwise a failure might happen.
size_t PerfEvent::GetEffectiveDataBufferSize | ( | ) | const |
Definition at line 320 of file Perf.cpp.
Referenced by lldb_private::process_linux::IntelPTMultiCoreTrace::GetState().
long PerfEvent::GetFd | ( | ) | const |
Get the file descriptor associated with the perf event.
Definition at line 171 of file Perf.cpp.
Referenced by lldb_private::process_linux::CreateContextSwitchTracePerfEvent().
perf_event_mmap_page & PerfEvent::GetMetadataPage | ( | ) | const |
Get the metadata page from the data section's mmap buffer.
The metadata page is always mmap'ed, even when num_data_pages is 0.
This should be called only after PerfEvent::MmapMetadataAndBuffers, otherwise a failure might happen.
Expected< std::vector< uint8_t > > PerfEvent::GetReadOnlyAuxBuffer | ( | ) |
Read the aux buffer managed by this perf event assuming it was configured with PROT_READ permissions only, which indicates that the buffer is automatically wrapped and overwritten by the kernel or hardware.
To ensure that the data is up-to-date and is not corrupted by read-write race conditions, the underlying perf_event is paused during read, and later it's returned to its initial state. The returned data will be linear, i.e. it will fix the circular wrapping the might exist in the buffer.
Definition at line 247 of file Perf.cpp.
Referenced by lldb_private::process_linux::IntelPTSingleBufferTrace::GetIptTrace().
Expected< std::vector< uint8_t > > PerfEvent::GetReadOnlyDataBuffer | ( | ) |
Read the data buffer managed by this perf even assuming it was configured with PROT_READ permissions only, which indicates that the buffer is automatically wrapped and overwritten by the kernel or hardware.
To ensure that the data is up-to-date and is not corrupted by read-write race conditions, the underlying perf_event is paused during read, and later it's returned to its initial state. The returned data will be linear, i.e. it will fix the circular wrapping the might exist int he buffer.
|
static |
Create a new performance monitoring event via the perf_event_open syscall with "default" values for the cpu, group_fd and flags arguments.
Convenience method to be used when the perf event requires minimal configuration. It handles the default values of all other arguments.
[in] | attr | Configuration information for the event. |
[in] | pid | The process or thread to be monitored by the event. If std::nullopt, then all threads and processes are monitored. |
|
static |
Create a new performance monitoring event via the perf_event_open syscall.
The parameters are directly forwarded to a perf_event_open syscall, for additional information on the parameters visit https://man7.org/linux/man-pages/man2/perf_event_open.2.html.
[in] | attr | Configuration information for the event. |
[in] | pid | The process or thread to be monitored by the event. If None, then all processes and threads are monitored. |
[in] | cpu | The cpu to be monitored by the event. If None, then all cpus are monitored. |
[in] | group_fd | File descriptor of the group leader. If None, then this perf_event doesn't belong to a preexisting group. |
[in] | flags | Bitmask of additional configuration flags. |
Definition at line 78 of file Perf.cpp.
Referenced by lldb_private::process_linux::CreateContextSwitchTracePerfEvent(), lldb_private::process_linux::LoadPerfTscConversionParameters(), and lldb_private::process_linux::IntelPTSingleBufferTrace::Start().
bool PerfEvent::IsEnabled | ( | ) | const |
Definition at line 305 of file Perf.cpp.
Referenced by lldb_private::process_linux::CreateContextSwitchTracePerfEvent().
|
private |
llvm::Error PerfEvent::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 PerfEvent::GetMetadataPage() , PerfEvent::GetDataBuffer() and PerfEvent::GetAuxBuffer().
This uses mmap underneath, which means that the number of pages mmap'ed must be less than the actual data available by the kernel. The metadata page is always mmap'ed.
Mmap is needed because the underlying data might be changed by the kernel dynamically.
[in] | num_data_pages | Number of pages in the data buffer to mmap, must be a power of 2. A value of 0 is useful for "dummy" events that only want to access the metadata, perf_event_mmap_page, or the aux buffer. |
[in] | num_aux_pages | Number of pages in the aux buffer to mmap, must be a power of 2. A value of 0 effectively is a no-op and no data is mmap'ed for this buffer. |
[in] | data_buffer_write | Whether to mmap the data buffer with WRITE permissions. This changes the behavior of how the kernel writes to the data buffer. |
|
private |
Mmap the data buffer of the perf event.
[in] | num_data_pages | Number of pages in the data buffer to mmap, must be a power of 2. A value of 0 is useful for "dummy" events that only want to access the metadata, perf_event_mmap_page, or the aux buffer. |
[in] | data_buffer_write | Whether to mmap the data buffer with WRITE permissions. This changes the behavior of how the kernel writes to the data buffer. |
Definition at line 115 of file Perf.cpp.
References PROT_READ, and PROT_WRITE.
|
private |
|
private |
|
private |
|
private |