Skip to content

Commit

Permalink
New and improved recipe, now with 120% less bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
cvonelm committed Aug 22, 2023
1 parent 1d6bc6c commit 08d18de
Show file tree
Hide file tree
Showing 19 changed files with 161 additions and 249 deletions.
2 changes: 1 addition & 1 deletion include/lo2s/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ struct Config
std::vector<std::string> x86_adapt_knobs;
#endif
bool use_sensors;
int cgroup_fd = -1;
Fd cgroup_fd = Fd::invalid();

Check failure on line 61 in include/lo2s/config.hpp

View check run for this annotation

Code Style Turtle / Code Formatting Test

include/lo2s/config.hpp#L61

- Fd cgroup_fd = Fd::invalid(); + Fd cgroup_fd = Fd::invalid();
// OTF2
std::string trace_path;
// perf
Expand Down
33 changes: 6 additions & 27 deletions include/lo2s/perf/bio/reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ class Reader : public PullReader

attr.sample_period = 1;
attr.sample_type = PERF_SAMPLE_RAW | PERF_SAMPLE_TIME;
fd_ = perf_event_open(&attr, cpu_.as_scope(), -1, 0);
if (fd_ < 0)
fd_ = perf_event_open(&attr, cpu_.as_scope());
if (!fd_.is_valid())
{
Log::error() << "perf_event_open for raw tracepoint failed.";
throw_errno();
Expand All @@ -137,40 +137,19 @@ class Reader : public PullReader

try
{
if (fcntl(fd_, F_SETFL, O_NONBLOCK))
{
throw_errno();
}

init_mmap(fd_);
init_mmap(fd_.as_int());
Log::debug() << "perf_tracepoint_reader mmap initialized";

auto ret = ioctl(fd_, PERF_EVENT_IOC_ENABLE);
Log::debug() << "perf_tracepoint_reader ioctl(fd, PERF_EVENT_IOC_ENABLE) = " << ret;
if (ret == -1)
{
throw_errno();
}
}
catch (...)
{
Log::error() << "Couldn't initialize block:rq_insert reading";
close(fd_);
throw;
}
}

~Reader()
{
if (fd_ != -1)
{
close(fd_);
}
}

void stop()
{
auto ret = ioctl(fd_, PERF_EVENT_IOC_DISABLE);
auto ret = ioctl(fd_.as_int(), PERF_EVENT_IOC_DISABLE);
Log::debug() << "perf_tracepoint_reader ioctl(fd, PERF_EVENT_IOC_DISABLE) = " << ret;
if (ret == -1)
{
Expand All @@ -185,7 +164,7 @@ class Reader : public PullReader

int fd() const
{
return fd_;
return fd_.as_int();

Check failure on line 167 in include/lo2s/perf/bio/reader.hpp

View check run for this annotation

Code Style Turtle / Code Formatting Test

include/lo2s/perf/bio/reader.hpp#L167

- return fd_.as_int(); + return fd_.as_int();
}

Reader& operator=(const Reader&) = delete;
Expand All @@ -211,7 +190,7 @@ class Reader : public PullReader

private:
Cpu cpu_;
int fd_ = -1;
Fd fd_ = Fd::invalid();

Check failure on line 193 in include/lo2s/perf/bio/reader.hpp

View check run for this annotation

Code Style Turtle / Code Formatting Test

include/lo2s/perf/bio/reader.hpp#L193

- Fd fd_ = Fd::invalid(); + Fd fd_ = Fd::invalid();
};
} // namespace bio
} // namespace perf
Expand Down
16 changes: 2 additions & 14 deletions include/lo2s/perf/counter/group/reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,9 @@ class Reader : public EventReader<T>
struct GroupReadFormat v;
};

~Reader()
{
for (int fd : counter_fds_)
{
if (fd != -1)
{
::close(fd);
}
}
::close(group_leader_fd_);
}

protected:
int group_leader_fd_;
std::vector<int> counter_fds_;
Fd group_leader_fd_ = Fd::invalid();

Check failure on line 62 in include/lo2s/perf/counter/group/reader.hpp

View check run for this annotation

Code Style Turtle / Code Formatting Test

include/lo2s/perf/counter/group/reader.hpp#L62

- Fd group_leader_fd_ = Fd::invalid(); + Fd group_leader_fd_ = Fd::invalid();
std::vector<Fd> counter_fds_;
CounterCollection counter_collection_;
GroupCounterBuffer counter_buffer_;
};
Expand Down
2 changes: 1 addition & 1 deletion include/lo2s/perf/counter/userspace/reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Reader
}

protected:
std::vector<int> counter_fds_;
std::vector<Fd> counter_fds_;
CounterCollection counter_collection_;
UserspaceCounterBuffer counter_buffer_;
int timer_fd_;
Expand Down
29 changes: 23 additions & 6 deletions include/lo2s/perf/event_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@

extern "C"
{
#include <fcntl.h>
#include <sys/ioctl.h>

Check failure on line 43 in include/lo2s/perf/event_reader.hpp

View check run for this annotation

Code Style Turtle / Code Formatting Test

include/lo2s/perf/event_reader.hpp#L42-L43

- #include <fcntl.h> -#include <sys/ioctl.h> +#include <fcntl.h>
#include <linux/perf_event.h>

Check failure on line 44 in include/lo2s/perf/event_reader.hpp

View check run for this annotation

Code Style Turtle / Code Formatting Test

include/lo2s/perf/event_reader.hpp#L44

+#include <sys/ioctl.h>
#include <sys/mman.h>
}

/* perf sample has 16 bits size limit */
} /* perf sample has 16 bits size limit */
#define PERF_SAMPLE_MAX_SIZE (1 << 16)

namespace lo2s
Expand Down Expand Up @@ -76,8 +76,7 @@ class EventReader
RecordMmap2Type& operator=(RecordMmap2Type&&) = delete;

struct perf_event_header header;
uint32_t pid;
uint32_t tid;
uint32_t pid; uint32_t tid;

Check failure on line 79 in include/lo2s/perf/event_reader.hpp

View check run for this annotation

Code Style Turtle / Code Formatting Test

include/lo2s/perf/event_reader.hpp#L79

- uint32_t pid; uint32_t tid; + uint32_t pid; + uint32_t tid;
uint64_t addr;
uint64_t len;
uint64_t pgoff;
Expand Down Expand Up @@ -156,8 +155,15 @@ class EventReader
}

protected:
void init_mmap(int fd)
void init_mmap(int fd, bool enable_on_exec = false)

Check failure on line 158 in include/lo2s/perf/event_reader.hpp

View check run for this annotation

Code Style Turtle / Code Formatting Test

include/lo2s/perf/event_reader.hpp#L158

- void init_mmap(int fd, bool enable_on_exec = false) + void init_mmap(int fd, bool enable_on_exec = false)
{

// asynchronous delivery
// if (fcntl(fd, F_SETFL, O_ASYNC | O_NONBLOCK))
if (fcntl(fd_, F_SETFL, O_NONBLOCK))
{
throw_errno();
}

Check failure on line 166 in include/lo2s/perf/event_reader.hpp

View check run for this annotation

Code Style Turtle / Code Formatting Test

include/lo2s/perf/event_reader.hpp#L160-L166

- - // asynchronous delivery - // if (fcntl(fd, F_SETFL, O_ASYNC | O_NONBLOCK)) - if (fcntl(fd_, F_SETFL, O_NONBLOCK)) - { - throw_errno(); - } + + // asynchronous delivery + // if (fcntl(fd, F_SETFL, O_ASYNC | O_NONBLOCK)) + if (fcntl(fd_, F_SETFL, O_NONBLOCK)) + { + throw_errno(); + }
fd_ = fd;

mmap_pages_ = config().mmap_pages;
Expand All @@ -173,6 +179,17 @@ class EventReader
"perf_event_mlock_kb";
throw_errno();
}


if (!enable_on_exec)

Check failure on line 184 in include/lo2s/perf/event_reader.hpp

View check run for this annotation

Code Style Turtle / Code Formatting Test

include/lo2s/perf/event_reader.hpp#L183-L184

- - if (!enable_on_exec) + if (!enable_on_exec) + { + auto ret = ioctl(fd_, PERF_EVENT_IOC_ENABLE); + Log::debug() << "ioctl(fd, PERF_EVENT_IOC_ENABLE) = " << ret; + if (ret == -1)
{
auto ret = ioctl(fd_, PERF_EVENT_IOC_ENABLE);
Log::debug() << "ioctl(fd, PERF_EVENT_IOC_ENABLE) = " << ret;
if (ret == -1)
{
throw_errno();
}

Check failure on line 191 in include/lo2s/perf/event_reader.hpp

View check run for this annotation

Code Style Turtle / Code Formatting Test

include/lo2s/perf/event_reader.hpp#L186-L191

- auto ret = ioctl(fd_, PERF_EVENT_IOC_ENABLE); - Log::debug() << "ioctl(fd, PERF_EVENT_IOC_ENABLE) = " << ret; - if (ret == -1) - { - throw_errno(); - } + throw_errno();
}

Check failure on line 192 in include/lo2s/perf/event_reader.hpp

View check run for this annotation

Code Style Turtle / Code Formatting Test

include/lo2s/perf/event_reader.hpp#L192

+ }
}

public:
Expand Down
51 changes: 5 additions & 46 deletions include/lo2s/perf/sample/reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,9 @@

extern "C"
{
#include <fcntl.h>
#include <unistd.h>

#include <linux/perf_event.h>

#include <sys/ioctl.h>
#include <sys/mman.h>
}

namespace lo2s
Expand Down Expand Up @@ -143,7 +139,7 @@ class Reader : public EventReader<T>
* and the value of it is greater than the initial value */
do
{
fd_ = perf_event_open(&perf_attr, scope, -1, 0, config().cgroup_fd);
fd_ = perf_event_open(&perf_attr, scope, Fd::invalid(), 0, config().cgroup_fd);

Check failure on line 142 in include/lo2s/perf/sample/reader.hpp

View check run for this annotation

Code Style Turtle / Code Formatting Test

include/lo2s/perf/sample/reader.hpp#L142

- fd_ = perf_event_open(&perf_attr, scope, Fd::invalid(), 0, config().cgroup_fd); + fd_ = perf_event_open(&perf_attr, scope, Fd::invalid(), 0, config().cgroup_fd);

if (errno == EACCES && !perf_attr.exclude_kernel && perf_event_paranoid() > 1)
{
Expand All @@ -159,9 +155,9 @@ class Reader : public EventReader<T>
break;
else
perf_attr.precise_ip--;
} while (fd_ <= 0);
} while (!fd_.is_valid());

if (fd_ < 0)
if (!fd_.is_valid())
{
Log::error() << "perf_event_open for sampling failed";
if (perf_attr.use_clockid)
Expand All @@ -172,34 +168,8 @@ class Reader : public EventReader<T>
}
Log::debug() << "Using precise_ip level: " << perf_attr.precise_ip;

// Exception safe, so much wow!
try
{
// asynchronous delivery
// if (fcntl(fd, F_SETFL, O_ASYNC | O_NONBLOCK))
if (fcntl(fd_, F_SETFL, O_NONBLOCK))
{
throw_errno();
}

init_mmap(fd_);
init_mmap(fd_.as_int(), enable_on_exec);
Log::debug() << "mmap initialized";

Check failure on line 172 in include/lo2s/perf/sample/reader.hpp

View check run for this annotation

Code Style Turtle / Code Formatting Test

include/lo2s/perf/sample/reader.hpp#L171-L172

- init_mmap(fd_.as_int(), enable_on_exec); - Log::debug() << "mmap initialized"; + init_mmap(fd_.as_int(), enable_on_exec); + Log::debug() << "mmap initialized";

if (!enable_on_exec)
{
auto ret = ioctl(fd_, PERF_EVENT_IOC_ENABLE);
Log::debug() << "ioctl(fd, PERF_EVENT_IOC_ENABLE) = " << ret;
if (ret == -1)
{
throw_errno();
}
}
}
catch (...)
{
close();
throw;
}
}

Reader(const Reader&) = delete;
Expand All @@ -220,24 +190,13 @@ class Reader : public EventReader<T>
"extend to prevent the kernel from doing that"
" or you can increase your sampling period.";
}
close();
}

public:
void close()
{
if (fd_ != -1)
{
::close(fd_);
fd_ = -1;
}
}

protected:
bool has_cct_;

private:
int fd_ = -1;
Fd fd_ = Fd::invalid();

Check failure on line 199 in include/lo2s/perf/sample/reader.hpp

View check run for this annotation

Code Style Turtle / Code Formatting Test

include/lo2s/perf/sample/reader.hpp#L199

- Fd fd_ = Fd::invalid(); + Fd fd_ = Fd::invalid();
};
} // namespace sample
} // namespace perf
Expand Down
Loading

0 comments on commit 08d18de

Please sign in to comment.