Skip to content

Commit

Permalink
Move event definitions to include/perf/types.hpp
Browse files Browse the repository at this point in the history
Not everything that wants to interact with perf events is a derived
class of EventReader, such as the process memory map handling code. So,
move relevant event definitions into neutral ground.
  • Loading branch information
cvonelm committed Nov 7, 2024
1 parent 462e37a commit a622be3
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 39 deletions.
42 changes: 3 additions & 39 deletions include/lo2s/perf/event_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <lo2s/config.hpp>
#include <lo2s/error.hpp>
#include <lo2s/log.hpp>
#include <lo2s/mmap.hpp>
#include <lo2s/perf/types.hpp>
#include <lo2s/platform.hpp>
#include <lo2s/shared_memory.hpp>
#include <lo2s/util.hpp>
Expand Down Expand Up @@ -66,32 +66,8 @@ class EventReader
using RecordUnknownType = perf_event_header;

using RecordMmapType = lo2s::RecordMmapType;

struct RecordMmap2Type
{
// BAD things happen if you try this
RecordMmap2Type() = delete;
RecordMmap2Type(const RecordMmap2Type&) = delete;
RecordMmap2Type& operator=(const RecordMmap2Type&) = delete;
RecordMmap2Type(RecordMmap2Type&&) = delete;
RecordMmap2Type& operator=(RecordMmap2Type&&) = delete;

struct perf_event_header header;
uint32_t pid;
uint32_t tid;
uint64_t addr;
uint64_t len;
uint64_t pgoff;
uint32_t maj;
uint32_t min;
uint64_t ino;
uint64_t ino_generation;
uint32_t prot;
uint32_t flags;
// Note ISO C++ forbids zero-size array, but this struct is exclusively used as pointer
char filename[1];
// struct sample_id sample_id;
};
using RecordMmap2Type = lo2s::RecordMmap2Type;
using RecordCommType = lo2s::RecordCommType;

struct RecordLostType
{
Expand Down Expand Up @@ -138,18 +114,6 @@ class EventReader
// struct sample_id sample_id;
};

/**
* \brief structure for PERF_RECORD_COMM events
**/
struct RecordCommType
{
struct perf_event_header header;
uint32_t pid;
uint32_t tid;
char comm[1]; // ISO C++ forbits zero-size array
// struct sample_id sample_id;
};

EventReader() = default;

EventReader(EventReader<T>&) = delete;
Expand Down
133 changes: 133 additions & 0 deletions include/lo2s/perf/types.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* This file is part of the lo2s software.
* Linux OTF2 sampling
*
* Copyright (c) 2016,
* Technische Universitaet Dresden, Germany
*
* lo2s is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* lo2s is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with lo2s. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <deque>
#include <memory>

#include <cstdint>
#include <cstring>

extern "C"
{
#include <linux/perf_event.h>
}

namespace lo2s
{

template <class T>
class PerfEventCache
{
public:
PerfEventCache() = delete;
PerfEventCache(const PerfEventCache&) = delete;
PerfEventCache& operator=(const PerfEventCache&) = delete;

PerfEventCache(const T* event, size_t size) : data_(std::make_unique<std::byte[]>(size))
{
memcpy(data_.get(), event, size);
}

T* get()
{
return reinterpret_cast<T*>(data_.get());
}

const T* get() const
{
return reinterpret_cast<T*>(data_.get());
}

private:
std::unique_ptr<std::byte[]> data_;
};

struct RecordMmapType
{
// BAD things happen if you try this
RecordMmapType() = delete;
RecordMmapType(const RecordMmapType&) = delete;
RecordMmapType& operator=(const RecordMmapType&) = delete;
RecordMmapType(RecordMmapType&&) = delete;
RecordMmapType& operator=(RecordMmapType&&) = delete;

struct perf_event_header header;
uint32_t pid, tid;
uint64_t addr;
uint64_t len;
uint64_t pgoff;
// Note ISO C++ forbids zero-size array, but this struct is exclusively used as pointer
char filename[1];
// struct sample_id id;
};

/**
* \brief structure for PERF_RECORD_COMM events
**/

struct __attribute((__packed__)) sample_id
{
uint32_t pid, tid;
uint64_t time;
uint32_t cpu, res;
};

struct __attribute((__packed__)) RecordCommType
{
struct perf_event_header header;
uint32_t pid;
uint32_t tid;
char comm[1]; // ISO C++ forbits zero-size array
// struct sample_id id;
};

struct __attribute((__packed__)) RecordMmap2Type
{
// BAD things happen if you try this
RecordMmap2Type() = delete;
RecordMmap2Type(const RecordMmap2Type&) = delete;
RecordMmap2Type& operator=(const RecordMmap2Type&) = delete;
RecordMmap2Type(RecordMmap2Type&&) = delete;
RecordMmap2Type& operator=(RecordMmap2Type&&) = delete;

struct perf_event_header header;
uint32_t pid;
uint32_t tid;
uint64_t addr;
uint64_t len;
uint64_t pgoff;
uint32_t maj;
uint32_t min;
uint64_t ino;
uint64_t ino_generation;
uint32_t prot;
uint32_t flags;
// Note ISO C++ forbids zero-size array, but this struct is exclusively used as pointer
char filename[1];
// struct sample_id sample_id;
};

using RawMemoryMapCache = std::deque<PerfEventCache<RecordMmap2Type>>;
using RawCommCache = std::deque<PerfEventCache<RecordCommType>>;

} // namespace lo2s

0 comments on commit a622be3

Please sign in to comment.