Skip to content

Commit

Permalink
Extended MemoryAccess analyzer interface by instance_name references.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmuehlig committed Dec 2, 2024
1 parent 283c01c commit 83aa907
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 31 deletions.
118 changes: 90 additions & 28 deletions include/perfcpp/analyzer/memory_access.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,38 +126,77 @@ class MemoryAccess
*
* @param data_type_name Name of the (registered) data type.
* @param data_object Data object to annotate.
* @param tag Tag to differentiate multiple instances of the same type (optional).
* @param instance_name Tag to differentiate multiple instances of the same type (optional).
*/
template<typename T>
void annotate(const std::string_view data_type_name, T* data_object, std::string&& tag = "")
void annotate(const std::string_view data_type_name, T* data_object, std::string&& instance_name = "")
{
annotate(data_type_name, std::uintptr_t(data_object), tag);
annotate(data_type_name, std::uintptr_t(data_object), instance_name);
}

/**
* Annotates the given object with the given type.
*
* @param data_type_name Name of the (registered) data type.
* @param data_object Data object to annotate.
* @param tag Tag to differentiate multiple instances of the same type (optional).
* @param instance_name Tag to differentiate multiple instances of the same type (optional).
*/
template<typename T>
void annotate(const std::string_view data_type_name, const T* data_object, std::string&& tag = "")
void annotate(const std::string_view data_type_name, T* data_object, const std::string& instance_name)
{
annotate(data_type_name, std::uintptr_t(data_object), tag);
annotate(data_type_name, std::uintptr_t(data_object), instance_name);
}

/**
* Annotates the given object with the given type.
*
* @param data_type_name Name of the (registered) data type.
* @param data_object Data object to annotate.
* @param tag Tag to differentiate multiple instances of the same type (optional).
* @param instance_name Tag to differentiate multiple instances of the same type (optional).
*/
template<typename T>
void annotate(const std::string_view data_type_name, const T& data_object, std::string&& tag = "")
void annotate(const std::string_view data_type_name, const T* data_object, std::string&& instance_name = "")
{
annotate(data_type_name, std::uintptr_t(&data_object), tag);
annotate(data_type_name, std::uintptr_t(data_object), instance_name);
}

/**
* Annotates the given object with the given type.
*
* @param data_type_name Name of the (registered) data type.
* @param data_object Data object to annotate.
* @param instance_name Tag to differentiate multiple instances of the same type (optional).
*/
template<typename T>
void annotate(const std::string_view data_type_name, const T* data_object, const std::string& instance_name)
{
annotate(data_type_name, std::uintptr_t(data_object), instance_name);
}

/**
* Annotates the given object with the given type.
*
* @param data_type_name Name of the (registered) data type.
* @param data_object Data object to annotate.
* @param instance_name Tag to differentiate multiple instances of the same type (optional).
*/
template<typename T>
void annotate(const std::string_view data_type_name, const T& data_object, std::string&& instance_name = "")
{
annotate(data_type_name, std::uintptr_t(&data_object), instance_name);
}

/**
* Annotates the given object with the given type.
*
* @param data_type_name Name of the (registered) data type.
* @param data_object Data object to annotate.
* @param instance_name Tag to differentiate multiple instances of the same type (optional).
*/
template<typename T>
void annotate(const std::string_view data_type_name, const T& data_object, const std::string& instance_name)
{
annotate(data_type_name, std::uintptr_t(&data_object), instance_name);
}

/**
Expand All @@ -166,13 +205,16 @@ class MemoryAccess
* @param data_type_name Name of the (registered) data type.
* @param data_object Array of data objects to annotate.
* @param size Size of the array.
* @param tag Tag to differentiate multiple instances of the same type (optional).
* @param instance_name Tag to differentiate multiple instances of the same type (optional).
*/
template<typename T>
void annotate(const std::string_view data_type_name, const T* data_objects, std::size_t size, std::string&& tag = "")
void annotate(const std::string_view data_type_name,
const T* data_objects,
std::size_t size,
std::string&& instance_name = "")
{
for (auto i = 0ULL; i < size; ++i) {
annotate(data_type_name, std::uintptr_t(&data_objects[i]), tag);
annotate(data_type_name, std::uintptr_t(&data_objects[i]), instance_name);
}
}

Expand All @@ -182,13 +224,27 @@ class MemoryAccess
* @param data_type_name Name of the (registered) data type.
* @param begin Begin of the container.
* @param end End of the container.
* @param tag Tag to differentiate multiple instances of the same type (optional).
* @param instance_name Tag to differentiate multiple instances of the same type (optional).
*/
template<typename I>
void annotate(const std::string_view data_type_name, I begin, I end, std::string&& instance_name = "")
{
annotate(data_type_name, begin, end, instance_name);
}

/**
* Annotates container of objects with the given type.
*
* @param data_type_name Name of the (registered) data type.
* @param begin Begin of the container.
* @param end End of the container.
* @param instance_name Tag to differentiate multiple instances of the same type (optional).
*/
template<typename I>
void annotate(const std::string_view data_type_name, I begin, I end, std::string&& tag = "")
void annotate(const std::string_view data_type_name, I begin, I end, const std::string& instance_name)
{
for (auto iterator = begin; iterator != end; ++iterator) {
annotate(data_type_name, std::uintptr_t(&*iterator), tag);
annotate(data_type_name, std::uintptr_t(&*iterator), instance_name);
}
}

Expand All @@ -197,28 +253,30 @@ class MemoryAccess
*
* @param data_type_name Name of the (registered) data type.
* @param data_objects Data objects to annotate.
* @param tag Tag to differentiate multiple instances of the same type (optional).
* @param instance_name Tag to differentiate multiple instances of the same type (optional).
*/
template<typename T>
void annotate(const std::string_view data_type_name, const std::vector<T>& data_objects, std::string&& tag = "")
void annotate(const std::string_view data_type_name,
const std::vector<T>& data_objects,
std::string&& instance_name = "")
{
annotate(data_type_name, data_objects.cbegin(), data_objects.cend(), std::move(tag));
annotate(data_type_name, data_objects.cbegin(), data_objects.cend(), instance_name);
}

/**
* Annotates the given objects with the given type.
*
* @param data_type_name Name of the (registered) data type.
* @param data_objects Data objects to annotate.
* @param tag Tag to differentiate multiple instances of the same type (optional).
* @param instance_name Tag to differentiate multiple instances of the same type (optional).
*/
template<typename T>
void annotate(const std::string_view data_type_name,
const std::unordered_set<T>& data_objects,
std::string&& tag = "")
std::string&& instance_name = "")
{
for (const auto& data_object : data_objects) {
annotate(data_type_name, std::uintptr_t(&data_object), tag);
annotate(data_type_name, std::uintptr_t(&data_object), instance_name);
}
}

Expand All @@ -227,13 +285,15 @@ class MemoryAccess
*
* @param data_type_name Name of the (registered) data type.
* @param data_objects Data objects to annotate.
* @param tag Tag to differentiate multiple instances of the same type (optional).
* @param instance_name Tag to differentiate multiple instances of the same type (optional).
*/
template<typename T>
void annotate(const std::string_view data_type_name, const std::set<T>& data_objects, std::string&& tag = "")
void annotate(const std::string_view data_type_name,
const std::set<T>& data_objects,
std::string&& instance_name = "")
{
for (const auto& data_object : data_objects) {
annotate(data_type_name, std::uintptr_t(&data_object), tag);
annotate(data_type_name, std::uintptr_t(&data_object), instance_name);
}
}

Expand All @@ -242,12 +302,14 @@ class MemoryAccess
*
* @param data_type_name Name of the (registered) data type.
* @param data_objects Data objects to annotate.
* @param tag Tag to differentiate multiple instances of the same type (optional).
* @param instance_name Tag to differentiate multiple instances of the same type (optional).
*/
template<typename T>
void annotate(const std::string_view data_type_name, const std::list<T>& data_objects, std::string&& tag = "")
void annotate(const std::string_view data_type_name,
const std::list<T>& data_objects,
std::string&& instance_name = "")
{
annotate(data_type_name, data_objects.cbegin(), data_objects.cend(), std::move(tag));
annotate(data_type_name, data_objects.cbegin(), data_objects.cend(), instance_name);
}

/**
Expand All @@ -272,7 +334,7 @@ class MemoryAccess
std::vector<std::pair<DataType, std::unordered_map<std::string, std::vector<std::uintptr_t>>>>::iterator find(
std::string_view data_type_name) noexcept;

void annotate(std::string_view data_type_name, std::uintptr_t data_object, const std::string& tag);
void annotate(std::string_view data_type_name, std::uintptr_t data_object, const std::string& instance_name);

/**
* Fills up the data objects with members in wholes (e.g., space between to members or space between the last member
Expand Down
6 changes: 3 additions & 3 deletions src/analyzer/memory_access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ perf::analyzer::MemoryAccess::add(perf::analyzer::DataType&& data_type)
void
perf::analyzer::MemoryAccess::annotate(const std::string_view data_type_name,
const std::uintptr_t data_object,
const std::string& tag)
const std::string& instance_name)
{
if (auto type_iterator = this->find(data_type_name); type_iterator != this->_data_type_instances.end()) {
auto& type_instances = type_iterator->second;

/// Check if the data object already contains the tag.
if (auto tag_iterator = type_instances.find(tag); tag_iterator != type_instances.end()) {
if (auto tag_iterator = type_instances.find(instance_name); tag_iterator != type_instances.end()) {
tag_iterator->second.push_back(data_object);
} else {
/// If the tag did not exist, add it as a new map tag -> [addresses].
auto instances = std::vector<std::uintptr_t>{};
instances.reserve(2048U);
instances.push_back(data_object);
type_instances.insert(std::make_pair(tag, std::move(instances)));
type_instances.insert(std::make_pair(instance_name, std::move(instances)));
}
} else {
throw DataTypeNotRegisteredError{ data_type_name };
Expand Down

0 comments on commit 83aa907

Please sign in to comment.