Skip to content

Commit

Permalink
Revert "completely remove config() dependency, use event_provider as …
Browse files Browse the repository at this point in the history
…Event factory"

This reverts commit d73cc73.
  • Loading branch information
cvonelm authored Dec 8, 2024
1 parent c4a85c6 commit 9878889
Show file tree
Hide file tree
Showing 28 changed files with 526 additions and 688 deletions.
8 changes: 0 additions & 8 deletions include/lo2s/measurement_scope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ enum class MeasurementScopeType
BIO,
SYSCALL,
CUDA,
TRACEPOINT,
UNKNOWN
};

Expand Down Expand Up @@ -86,11 +85,6 @@ struct MeasurementScope
return { MeasurementScopeType::CUDA, s };
}

static MeasurementScope tracepoint(ExecutionScope s)
{
return { MeasurementScopeType::TRACEPOINT, s };
}

friend bool operator==(const MeasurementScope& lhs, const MeasurementScope& rhs)
{
return (lhs.scope == rhs.scope) && lhs.type == rhs.type;
Expand Down Expand Up @@ -125,8 +119,6 @@ struct MeasurementScope
return fmt::format("syscall events for {}", scope.name());
case lo2s::MeasurementScopeType::CUDA:
return fmt::format("cuda kernel events for {}", scope.name());
case MeasurementScopeType::TRACEPOINT:
return fmt::format("tracepoints for {}", scope.name());
default:
throw new std::runtime_error("Unknown ExecutionScopeType!");
}
Expand Down
11 changes: 3 additions & 8 deletions include/lo2s/perf/bio/writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,9 @@ class Writer

std::vector<perf::tracepoint::TracepointEvent> get_tracepoints()
{
bio_queue_ =
perf::EventProvider::instance().create_tracepoint_event("block:block_bio_queue");
bio_issue_ =
perf::EventProvider::instance().create_tracepoint_event("block:block_rq_issue");
bio_complete_ =
perf::EventProvider::instance().create_tracepoint_event("block:block_rq_complete");
bio_queue_ = perf::tracepoint::TracepointEvent("block:block_bio_queue");
bio_issue_ = perf::tracepoint::TracepointEvent("block:block_rq_issue");
bio_complete_ = perf::tracepoint::TracepointEvent("block:block_rq_complete");

return { bio_queue_, bio_issue_, bio_complete_ };
}
Expand All @@ -184,11 +181,9 @@ class Writer
trace::Trace& trace_;
time::Converter& time_converter_;

// Unabailable until get_tracepoints() is called
perf::tracepoint::TracepointEvent bio_queue_;
perf::tracepoint::TracepointEvent bio_issue_;
perf::tracepoint::TracepointEvent bio_complete_;

// The unit "sector" is always 512 bit large, regardless of the actual sector size of the device
static constexpr int SECTOR_SIZE = 512;
};
Expand Down
4 changes: 2 additions & 2 deletions include/lo2s/perf/counter/counter_collection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ namespace counter
{
struct CounterCollection
{
Event leader;
std::vector<Event> counters;
PerfEvent leader;
std::vector<PerfEvent> counters;

double get_scale(int index) const
{
Expand Down
10 changes: 4 additions & 6 deletions include/lo2s/perf/counter/counter_provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#include <lo2s/measurement_scope.hpp>
#include <lo2s/perf/counter/counter_collection.hpp>
#include <lo2s/perf/tracepoint/event.hpp>
#include <lo2s/perf/event.hpp>

#include <vector>

Expand All @@ -49,7 +49,6 @@ class CounterProvider
void initialize_group_counters(const std::string& leader,
const std::vector<std::string>& counters);
void initialize_userspace_counters(const std::vector<std::string>& counters);
void initialize_tracepoints(const std::vector<std::string>& tracepoints);

bool has_group_counters(ExecutionScope scope);
bool has_userspace_counters(ExecutionScope scope);
Expand All @@ -58,10 +57,9 @@ class CounterProvider
std::vector<std::string> get_tracepoint_event_names();

private:
Event group_leader_;
std::vector<Event> group_events_;
std::vector<Event> userspace_events_;
std::vector<tracepoint::TracepointEvent> tracepoint_events_;
PerfEvent group_leader_;
std::vector<PerfEvent> group_events_;
std::vector<PerfEvent> userspace_events_;
};
} // namespace counter
} // namespace perf
Expand Down
4 changes: 2 additions & 2 deletions include/lo2s/perf/counter/group/reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ class Reader : public EventReader<T>
};

protected:
EventGuard counter_leader_;
std::vector<EventGuard> counters_;
PerfEventGuard counter_leader_;
std::vector<PerfEventGuard> counters_;
CounterCollection counter_collection_;
GroupCounterBuffer counter_buffer_;
};
Expand Down
4 changes: 2 additions & 2 deletions include/lo2s/perf/counter/userspace/reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <lo2s/execution_scope.hpp>
#include <lo2s/perf/counter/counter_collection.hpp>
#include <lo2s/perf/counter/userspace/userspace_counter_buffer.hpp>
#include <lo2s/perf/event_provider.hpp>
#include <lo2s/perf/event.hpp>
#include <lo2s/trace/trace.hpp>

#include <cstdint>
Expand Down Expand Up @@ -60,7 +60,7 @@ class Reader
UserspaceCounterBuffer counter_buffer_;
int timer_fd_;

std::vector<EventGuard> counters_;
std::vector<PerfEventGuard> counters_;
std::vector<UserspaceReadFormat> data_;
};
} // namespace userspace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ namespace userspace
{
struct UserspaceReadFormat
{
UserspaceReadFormat() : value(0), time_enabled(0), time_running(0)
{
}

uint64_t value;
uint64_t time_enabled;
uint64_t time_running;
Expand Down
105 changes: 41 additions & 64 deletions include/lo2s/perf/event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,34 @@ enum class Availability
UNIVERSAL
};

class EventGuard;
class PerfEventGuard;

/**
* Base class for all Event types
* contains common attributes
*/
class Event
class PerfEvent
{
public:
Event([[maybe_unused]] uint64_t addr, bool enable_on_exec = false);
Event(std::string name, perf_type_id type, std::uint64_t config, std::uint64_t config1 = 0);
Event();
PerfEvent(const std::string& ev_name, bool enable_on_exec = false);
PerfEvent([[maybe_unused]] uint64_t addr, bool enable_on_exec = false);
PerfEvent(std::string name, perf_type_id type, std::uint64_t config, std::uint64_t config1 = 0,
std::set<Cpu> cpus = std::set<Cpu>());
PerfEvent();

/**
* returns an opened instance of any Event object
* returns an opened instance of any PerfEvent object
*/
EventGuard open(std::variant<Cpu, Thread> location, int cgroup_fd = -1);
EventGuard open(ExecutionScope location, int cgroup_fd = -1);
PerfEventGuard open(std::variant<Cpu, Thread> location, int cgroup_fd = config().cgroup_fd);
PerfEventGuard open(ExecutionScope location, int cgroup_fd = config().cgroup_fd);

/**
* returns an opened instance of a Event object after formating it as a leader Event
* returns an opened instance of a PerfEvent object after formating it as a leader Event
*/
EventGuard open_as_group_leader(ExecutionScope location, int cgroup_fd = -1);
PerfEventGuard open_as_group_leader(std::variant<Cpu, Thread> location,
int cgroup_fd = config().cgroup_fd);
PerfEventGuard open_as_group_leader(ExecutionScope location,
int cgroup_fd = config().cgroup_fd);

const Availability& get_availability() const
{
Expand Down Expand Up @@ -123,37 +128,12 @@ class Event
unit_ = unit;
}

void set_clock_attrs(const bool& use_clockid, const clockid_t& clockid)
{
#ifndef USE_HW_BREAKPOINT_COMPAT
attr_.use_clockid = use_clockid;
attr_.clockid = clockid;
#endif
}

// When we poll on the fd given by perf_event_open, wakeup, when our buffer is 80% full
// Default behaviour is to wakeup on every event, which is horrible performance wise
void set_watermark(const size_t& mmap_pages)
{
attr_.watermark = 1;
attr_.wakeup_watermark = static_cast<uint32_t>(0.8 * mmap_pages * sysconf(_SC_PAGESIZE));
}

void set_exclude_kernel(const bool& exclude_kernel)
{
attr_.exclude_kernel = exclude_kernel;
}

void set_sample_period(const int& period);
void set_sample_freq(const uint64_t& freq);
void event_attr_update(std::uint64_t value, const std::string& format);
void set_sample_freq();
void set_availability();

void parse_pmu_path(const std::string& ev_name);
void parse_cpus();
const std::set<Cpu>& supported_cpus() const;

bool is_valid() const;
bool event_is_openable();

bool is_available_in(ExecutionScope scope) const
{
Expand All @@ -164,23 +144,22 @@ class Event

bool degrade_precision();

friend bool operator==(const Event& lhs, const Event& rhs)
friend bool operator==(const PerfEvent& lhs, const PerfEvent& rhs)
{
return !memcmp(&lhs.attr_, &rhs.attr_, sizeof(struct perf_event_attr));
}

friend bool operator<(const Event& lhs, const Event& rhs)
friend bool operator<(const PerfEvent& lhs, const PerfEvent& rhs)
{
return memcmp(&lhs.attr_, &rhs.attr_, sizeof(struct perf_event_attr));
}

friend bool operator>(const Event& lhs, const Event& rhs)
friend bool operator>(const PerfEvent& lhs, const PerfEvent& rhs)
{
return memcmp(&lhs.attr_, &rhs.attr_, sizeof(struct perf_event_attr));
}

protected:
void update_availability();
void set_common_attrs(bool enable_on_exec);

struct perf_event_attr attr_;
Expand All @@ -190,45 +169,41 @@ class Event
std::string name_ = "";
std::set<Cpu> cpus_;
Availability availability_ = Availability::UNAVAILABLE;

std::filesystem::path pmu_path_;
std::string pmu_name_;
};

/**
* Contains an event parsed from sysfs
* @note call on use_sampling_options() after creation to get a valid
* @note call on as_sample() after creation to get a valid
* event, otherwise the availability will be set to UNAVAILABLE
*/
class SysfsEvent : public Event
class SysfsEvent : public PerfEvent
{
public:
using Event::Event;
SysfsEvent(const std::string& ev_name, bool enable_on_exec = false);
using PerfEvent::PerfEvent;

void use_sampling_options(const bool& use_pebs, const bool& sampling, const bool& enable_cct);
void as_sample();
};

/**
* Contains an opened instance of Event.
* Use any Event.open() method to construct an object
* Contains an opened instance of PerfEvent.
* Use any PerfEvent.open() method to construct an object
*/
class EventGuard
class PerfEventGuard
{
public:
EventGuard();
EventGuard(Event& ev, std::variant<Cpu, Thread> location, int group_fd, int cgroup_fd);
PerfEventGuard();
PerfEventGuard(PerfEvent& ev, std::variant<Cpu, Thread> location, int group_fd, int cgroup_fd);

EventGuard(EventGuard&) = delete;
EventGuard& operator=(const EventGuard&) = delete;
PerfEventGuard(PerfEventGuard&) = delete;
PerfEventGuard& operator=(const PerfEventGuard&) = delete;

EventGuard(EventGuard&& other)
PerfEventGuard(PerfEventGuard&& other)
{
std::swap(fd_, other.fd_);
std::swap(ev_, other.ev_);
}

EventGuard& operator=(EventGuard&& other)
PerfEventGuard& operator=(PerfEventGuard&& other)
{
std::swap(fd_, other.fd_);
std::swap(ev_, other.ev_);
Expand All @@ -238,7 +213,10 @@ class EventGuard
/**
* opens child as a counter of the calling (leader) event
*/
EventGuard open_child(Event child, ExecutionScope location, int cgroup_fd = -1);
PerfEventGuard open_child(PerfEvent child, std::variant<Cpu, Thread> location,
int cgroup_fd = config().cgroup_fd);
PerfEventGuard open_child(PerfEvent child, ExecutionScope location,
int cgroup_fd = config().cgroup_fd);

void enable();
void disable();
Expand All @@ -248,8 +226,8 @@ class EventGuard
ioctl(fd_, PERF_EVENT_IOC_ID, &id);
}

void set_output(const EventGuard& other_ev);
void set_syscall_filter(const std::vector<int64_t>& filter);
void set_output(const PerfEventGuard& other_ev);
void set_syscall_filter();

int get_fd() const
{
Expand All @@ -264,7 +242,6 @@ class EventGuard
template <class T>
T read()
{
static_assert(std::is_pod_v<T> == true);
T val;

if (::read(fd_, &val, sizeof(val)) == -1)
Expand All @@ -275,14 +252,14 @@ class EventGuard
return val;
}

~EventGuard()
~PerfEventGuard()
{
close(fd_);
}

protected:
int fd_;
Event ev_;
PerfEvent ev_;
};

} // namespace perf
Expand Down
Loading

0 comments on commit 9878889

Please sign in to comment.