Skip to content

Commit

Permalink
in progress trying to add events for allocate and deallocate with res…
Browse files Browse the repository at this point in the history
…ource
  • Loading branch information
kab163 committed Oct 23, 2024
1 parent 3f4792e commit dd88e46
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/umpire/Allocator.inl
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,8 @@ inline void* Allocator::do_resource_allocate(camp::resources::Resource const& r,
registerAllocation(ret, bytes, m_allocator);
}

// TODO: Add resource to event
// umpire::event::record<umpire::event::named_allocate>(
// [&](auto& event) { event.name(name).size(bytes).ref((void*)m_allocator).ptr(ret); });
umpire::event::record<umpire::event::allocate_resource>(
[&](auto& event) { event.size(bytes).ref((void*)m_allocator).ptr(ret).res(r); });
return ret;
}

Expand Down Expand Up @@ -149,8 +148,7 @@ inline void Allocator::do_deallocate(void* ptr)

inline void Allocator::do_resource_deallocate(camp::resources::Resource const& r, void* ptr)
{
// TODO: Add resource to event
// umpire::event::record<umpire::event::deallocate>([&](auto& event) { event.ref((void*)m_allocator).ptr(ptr); });
umpire::event::record<umpire::event::deallocate_resource>([&](auto& event) { event.ref((void*)m_allocator).ptr(ptr).res(r); });

UMPIRE_LOG(Debug, "(" << ptr << ")");

Expand Down
83 changes: 83 additions & 0 deletions src/umpire/event/event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,27 @@ struct named_allocate {
std::chrono::time_point<std::chrono::system_clock> timestamp{std::chrono::system_clock::now()};
};

struct allocate_resource {
std::size_t size;
void* ref;
void* ptr;
camp::resources::Resource res;
std::chrono::time_point<std::chrono::system_clock> timestamp{std::chrono::system_clock::now()};
};

struct deallocate {
void* ref;
void* ptr;
std::chrono::time_point<std::chrono::system_clock> timestamp{std::chrono::system_clock::now()};
};

struct deallocate_resource {
void* ref;
void* ptr;
camp::resources::Resource res;
std::chrono::time_point<std::chrono::system_clock> timestamp{std::chrono::system_clock::now()};
};

template <typename E = event>
class builder {
public:
Expand Down Expand Up @@ -283,6 +298,43 @@ class builder<named_allocate> {
named_allocate e;
};

template <>
class builder<allocate_resource> {
public:
builder& size(std::size_t size)
{
e.size = size;
return *this;
}

builder& ref(void* ref)
{
e.ref = ref;
return *this;
}

builder& ptr(void* ptr)
{
e.ptr = ptr;
return *this;
}

builder& res(camp::resources::Resource res)
{
e.res = res;
return *this;
}

template <typename Recorder = decltype(recorder_factory::get_recorder())>
void record(Recorder r = recorder_factory::get_recorder())
{
r.record(e);
}

private:
allocate_resource e;
};

template <>
class builder<deallocate> {
public:
Expand All @@ -308,6 +360,37 @@ class builder<deallocate> {
deallocate e;
};

template <>
class builder<deallocate_resource> {
public:
builder& ref(void* ref)
{
e.ref = ref;
return *this;
}

builder& ptr(void* ptr)
{
e.ptr = ptr;
return *this;
}

builder& res(camp::resources::Resource res)
{
e.res = res;
return *this;
}

template <typename Recorder = decltype(recorder_factory::get_recorder())>
void record(Recorder r = recorder_factory::get_recorder())
{
r.record(e);
}

private:
deallocate_resource e;
};

template <typename Lambda>
void record(Lambda&& l)
{
Expand Down
4 changes: 4 additions & 0 deletions src/umpire/event/event_store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ namespace event {
struct event;
struct allocate;
struct named_allocate;
struct allocate_resource;
struct deallocate;
struct deallocate_resource;

class event_store {
public:
virtual void insert(const event& e) = 0;
virtual void insert(const allocate& e) = 0;
virtual void insert(const named_allocate& e) = 0;
virtual void insert(const allocate_resource& e) = 0;
virtual void insert(const deallocate& e) = 0;
virtual void insert(const deallocate_resource& e) = 0;

virtual std::vector<event> get_events() = 0;
};
Expand Down
10 changes: 10 additions & 0 deletions src/umpire/event/event_store_recorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,20 @@ void event_store_recorder::record(const named_allocate& e)
m_database->insert(e);
}

void event_store_recorder::record(const allocate_resource& e)
{
m_database->insert(e);
}

void event_store_recorder::record(const deallocate& e)
{
m_database->insert(e);
}

void event_store_recorder::record(const deallocate_resource& e)
{
m_database->insert(e);
}

} // namespace event
} // namespace umpire
4 changes: 4 additions & 0 deletions src/umpire/event/event_store_recorder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ namespace event {

struct allocate;
struct named_allocate;
struct allocate_resource;
struct deallocate;
struct deallocate_resource;
struct event;

class event_store_recorder {
Expand All @@ -26,7 +28,9 @@ class event_store_recorder {
void record(const event& e);
void record(const allocate& e);
void record(const named_allocate& e);
void record(const allocate_resource& e);
void record(const deallocate& e);
void record(const deallocate_resource& e);

private:
event_store* m_database;
Expand Down
27 changes: 27 additions & 0 deletions src/umpire/event/json_file_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ void json_file_store::insert(const named_allocate& e)
std::chrono::time_point_cast<std::chrono::nanoseconds>(e.timestamp).time_since_epoch().count()));
}

void json_file_store::insert(const allocate_resource& e)
{
fprintf(m_fstream,
R"({"category":"operation","name":"allocate_resource")"
R"(,"numeric_args":{"size":%ld})"
R"(,"string_args":{"allocator_ref":"%p","pointer":"%p","resource":"%s"})"
R"(,"tags":{"replay":"true"})"
R"(,"timestamp":%lld})"
"\n",
e.size, e.ref, e.ptr, camp::resources::to_string(e.res),
static_cast<long long>(
std::chrono::time_point_cast<std::chrono::nanoseconds>(e.timestamp).time_since_epoch().count()));
}

void json_file_store::insert(const deallocate& e)
{
fprintf(m_fstream,
Expand All @@ -77,6 +91,19 @@ void json_file_store::insert(const deallocate& e)
std::chrono::time_point_cast<std::chrono::nanoseconds>(e.timestamp).time_since_epoch().count()));
}

void json_file_store::insert(const deallocate_resource& e)
{
fprintf(m_fstream,
R"({"category":"operation","name":"deallocate_resource")"
R"(,"string_args":{"allocator_ref":"%p","pointer":"%p", "resource":"%s"})"
R"(,"tags":{"replay":"true"})"
R"(,"timestamp":%lld})"
"\n",
e.ref, e.ptr, camp::resources::to_string(e.res),
static_cast<long long>(
std::chrono::time_point_cast<std::chrono::nanoseconds>(e.timestamp).time_since_epoch().count()));
}

std::vector<event> json_file_store::get_events()
#if !defined(_MSC_VER)
{
Expand Down
4 changes: 4 additions & 0 deletions src/umpire/event/json_file_store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ namespace event {
struct event;
struct allocate;
struct named_allocate;
struct allocate_resource;
struct deallocate;
struct deallocate_resource;

class json_file_store : public event_store {
public:
Expand All @@ -29,7 +31,9 @@ class json_file_store : public event_store {
virtual void insert(const event& e);
virtual void insert(const allocate& e);
virtual void insert(const named_allocate& e);
virtual void insert(const allocate_resource& e);
virtual void insert(const deallocate& e);
virtual void insert(const deallocate_resource& e);

virtual std::vector<event> get_events();

Expand Down
8 changes: 8 additions & 0 deletions src/umpire/event/quest_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,18 @@ void quest_database::insert(const named_allocate&)
{
}

void quest_database::insert(const allocate_resource&)
{
}

void quest_database::insert(const deallocate&)
{
}

void quest_database::insert(const deallocate_resource&)
{
}

std::vector<event> quest_database::get_events()
{
std::vector<event> events;
Expand Down
4 changes: 4 additions & 0 deletions src/umpire/event/quest_database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ namespace event {
struct event;
struct allocate;
struct named_allocate;
struct allocate_resource;
struct deallocate;
struct deallocate_resource;

class quest_database : public event_store {
public:
Expand All @@ -30,7 +32,9 @@ class quest_database : public event_store {
void insert(const event& e) override final;
void insert(const allocate& e) override final;
void insert(const named_allocate& e) override final;
void insert(const allocate_resource& e) override final;
void insert(const deallocate& e) override final;
void insert(const deallocate_resource& e) override final;

std::vector<event> get_events() override final;

Expand Down
37 changes: 37 additions & 0 deletions src/umpire/event/sqlite_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,25 @@ void sqlite_database::insert(const named_allocate& e)
UMP_SQ3_EXE(m_database, buffer, NULL, 0);
}

void sqlite_database::insert(const allocate_resource& e)
{
char buffer[512];

sprintf(buffer,
"INSERT INTO EVENTS VALUES(json('"
R"({"category":"operation","name":"allocate_resource")"
R"(,"numeric_args":{"size":%ld})"
R"(,"string_args":{"allocator_ref":"%p","pointer":"%p","resource":"%p"})"
R"(,"tags":{"replay":"true"})"
R"(,"timestamp":%lld})"
"'));",
e.size, e.ref, e.ptr, e.res,
static_cast<long long>(
std::chrono::time_point_cast<std::chrono::nanoseconds>(e.timestamp).time_since_epoch().count()));

UMP_SQ3_EXE(m_database, buffer, NULL, 0);
}

void sqlite_database::insert(const deallocate& e)
{
char buffer[512];
Expand All @@ -117,6 +136,24 @@ void sqlite_database::insert(const deallocate& e)
UMP_SQ3_EXE(m_database, buffer, NULL, 0);
}

void sqlite_database::insert(const deallocate_resource& e)
{
char buffer[512];

sprintf(buffer,
"INSERT INTO EVENTS VALUES(json('"
R"({"category":"operation","name":"deallocate")"
R"(,"string_args":{"allocator_ref":"%p","pointer":"%p", "resource":"%p"})"
R"(,"tags":{"replay":"true"})"
R"(,"timestamp":%lld})"
"'));",
e.ref, e.ptr, e.res,
static_cast<long long>(
std::chrono::time_point_cast<std::chrono::nanoseconds>(e.timestamp).time_since_epoch().count()));

UMP_SQ3_EXE(m_database, buffer, NULL, 0);
}

std::vector<event> sqlite_database::get_events()
{
std::vector<event> events;
Expand Down
4 changes: 4 additions & 0 deletions src/umpire/event/sqlite_database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ namespace event {
struct event;
struct allocate;
struct named_allocate;
struct allocate_resource;
struct deallocate;
struct deallocate_resource;

class sqlite_database : public event_store {
public:
Expand All @@ -29,7 +31,9 @@ class sqlite_database : public event_store {
virtual void insert(const event& e) override final;
virtual void insert(const allocate& e) override final;
virtual void insert(const named_allocate& e) override final;
virtual void insert(const allocate_resource& e) override final;
virtual void insert(const deallocate& e) override final;
virtual void insert(const deallocate_resource& e) override final;

std::vector<event> get_events() override final;

Expand Down

0 comments on commit dd88e46

Please sign in to comment.