Files
TheRock/rocm-systems/projects/rocprofiler-systems/source/lib/rocprof-sys/library/perf.hpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

195 lines
6.3 KiB
C++

// Copyright (c) Advanced Micro Devices, Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "common/defines.h"
#include "core/containers/c_array.hpp"
#include "core/locking.hpp"
#include "core/perf.hpp"
#include <timemory/backends/papi.hpp>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <linux/perf_event.h>
#include <regex>
#include <set>
#include <string>
#include <sys/types.h>
namespace rocprofsys
{
namespace perf
{
struct perf_event
{
static constexpr std::uint32_t max_batch_size = 32;
struct record;
struct sample_record;
class iterator;
/// Default constructor
perf_event() = default;
/// Move constructor
perf_event(perf_event&& other) noexcept;
/// Close the perf event file and unmap the ring buffer
~perf_event();
/// Move assignment is supported
perf_event& operator=(perf_event&& other) noexcept;
perf_event(const perf_event&) = delete;
perf_event& operator=(const perf_event&) = delete;
/// Open a perf_event file using the given options structure
std::optional<std::string> open(struct perf_event_attr& pe, pid_t pid = 0,
int cpu = -1);
std::optional<std::string> open(double, std::uint32_t = 0, pid_t pid = 0,
int cpu = -1);
/// Return file descriptor
long get_fileno() const;
/// Read event count
std::uint64_t get_count() const;
/// Get the batch size
std::uint32_t get_batch_size() const { return m_batch_size; }
/// Start counting events and collecting samples
bool start() const;
/// Stop counting events
bool stop() const;
/// Check if counting events and collecting samples
bool is_open() const;
/// Close the perf_event file and unmap the ring buffer
void close();
/// Configure the perf_event file to deliver a signal when samples are ready to be
/// processed
void set_ready_signal(int sig) const;
/// Check if this perf_event was configured to collect a type of sample data
inline bool is_sampling(sample s) const
{
return (m_sample_type & static_cast<std::uint64_t>(s)) != 0u;
}
/// Get the configuration for this perf_event's read format
inline std::uint64_t get_read_format() const { return m_read_format; }
/// A generic record type
struct record
{
friend class perf_event::iterator;
record() = default;
~record() = default;
record(const record&) = default;
record(record&&) noexcept = default;
record& operator=(const record&) = default;
record& operator=(record&&) noexcept = default;
bool is_valid() const { return (m_source != nullptr && m_header != nullptr); }
operator bool() const { return is_valid(); }
record_type get_type() const { return static_cast<record_type>(m_header->type); }
inline bool is_mmap() const { return get_type() == record_type::mmap; }
inline bool is_lost() const { return get_type() == record_type::lost; }
inline bool is_comm() const { return get_type() == record_type::comm; }
inline bool is_exit() const { return get_type() == record_type::exit; }
inline bool is_throttle() const { return get_type() == record_type::throttle; }
inline bool is_unthrottle() const
{
return get_type() == record_type::unthrottle;
}
inline bool is_fork() const { return get_type() == record_type::fork; }
inline bool is_read() const { return get_type() == record_type::read; }
inline bool is_sample() const { return get_type() == record_type::sample; }
inline bool is_mmap2() const { return get_type() == record_type::mmap2; }
std::uint64_t get_ip() const;
std::uint64_t get_pid() const;
std::uint64_t get_tid() const;
std::uint64_t get_time() const;
std::uint64_t get_period() const;
std::uint32_t get_cpu() const;
container::c_array<std::uint64_t> get_callchain() const;
private:
record(const perf_event* source, struct perf_event_header* header)
: m_source(source)
, m_header(header)
{}
template <sample SampleT, typename Tp = void*>
Tp locate_field() const;
const perf_event* m_source = nullptr;
struct perf_event_header* m_header = nullptr;
};
class iterator
{
public:
iterator(perf_event& source, struct perf_event_mmap_page* mapping);
~iterator();
void next();
record get();
bool has_data() const;
iterator& operator++();
record operator*() { return get(); }
bool operator!=(const iterator& other) const;
private:
perf_event& m_source;
size_t m_index = 0;
size_t m_head = 0;
struct perf_event_mmap_page* m_mapping = nullptr;
// Buffer to hold the current record. Just a hack until records play nice with the
// ring buffer
std::uint8_t _buf[4096];
};
/// Get an iterator to the beginning of the memory mapped ring buffer
iterator begin() { return iterator(*this, m_mapping); }
/// Get an iterator to the end of the memory mapped ring buffer
iterator end() { return iterator(*this, nullptr); }
private:
// Copy data out of the mmap ring buffer
static void copy_from_ring_buffer(struct perf_event_mmap_page* mapping,
ptrdiff_t index, void* dest, size_t bytes);
std::uint32_t m_batch_size = 10;
/// File descriptor for the perf event
long m_fd = -1;
/// Memory mapped perf event region
struct perf_event_mmap_page* m_mapping = nullptr;
/// The sample type from this perf_event's configuration
std::uint64_t m_sample_type = 0;
/// The read format from this perf event's configuration
std::uint64_t m_read_format = 0;
};
/// provides thread-local instance of perf_event
std::unique_ptr<perf_event>&
get_instance(std::int64_t _tid);
} // namespace perf
} // namespace rocprofsys