Files
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

165 lines
5.2 KiB
C++

// Copyright (c) Advanced Micro Devices, Inc.
// SPDX-License-Identifier: MIT
#pragma once
#include "traits.hpp"
#include "logger.hpp"
#include <cstddef>
#include <functional>
#include <mutex>
#include <optional>
#include <stdexcept>
#include <string>
#include <string_view>
#include <unordered_map>
namespace profiler_hub
{
template <typename EntityContainerType, typename PrimaryKey = size_t>
class entity_utility
{
public:
template <typename Entity>
[[nodiscard]] bool is_entry_registered(const Entity& entity) const noexcept
{
const typename EntityContainerType::key_type key{ entity };
const std::lock_guard<std::mutex> lock(m_mutex);
return m_entity_container.count(key) > 0;
}
template <typename... Args>
void emplace_entity(Args&&... args)
{
std::lock_guard<std::mutex> const lock(m_mutex);
m_entity_container.emplace(std::forward<Args>(args)...);
m_last_key.reset();
}
template <typename Entity>
[[nodiscard]] PrimaryKey get_primary_key_value_for_entity(const Entity& entity) const
{
const std::lock_guard<std::mutex> lock(m_mutex);
if constexpr(common::traits::is_unordered_map_v<EntityContainerType>)
{
const typename EntityContainerType::key_type key{ entity };
if(m_last_key && *m_last_key == key)
{
return m_last_value;
}
auto it = m_entity_container.find(key);
if(it == m_entity_container.end())
{
throw std::runtime_error(fmt::format("Primary key not found for entity"));
}
m_last_key = key;
m_last_value = it->second;
return it->second;
}
else
{
static_assert(common::traits::is_unordered_map_v<EntityContainerType>,
"EntityContainerType is not an unordered map");
}
}
private:
EntityContainerType m_entity_container;
mutable std::mutex m_mutex;
/// Single-slot last-resolved cache. Bulk inserts with an unchanging
/// trace_environment_t (the dominant pattern) hit this 100% on the hot
/// path and skip the unordered_map find/at.
mutable std::optional<typename EntityContainerType::key_type> m_last_key;
mutable PrimaryKey m_last_value{};
};
/// Specialization for string-keyed maps enabling lookup from
/// std::string_view without per-lookup heap allocation.
///
/// Uses a pre-allocated std::string buffer (protected by the existing mutex)
/// to convert string_view to string for find(). After the buffer reaches
/// steady-state capacity, assign() is a memcpy with zero allocation.
template <typename ValueType, typename PrimaryKey>
class entity_utility<std::unordered_map<std::string, ValueType>, PrimaryKey>
{
static constexpr size_t initial_lookup_buffer_capacity = 64;
using container_type_t = std::unordered_map<std::string, ValueType>;
public:
entity_utility() { m_lookup_buffer.reserve(initial_lookup_buffer_capacity); }
[[nodiscard]] bool is_entry_registered(const std::string& key) const noexcept
{
const std::lock_guard<std::mutex> lock(m_mutex);
return m_entity_container.count(key) > 0;
}
[[nodiscard]] bool is_entry_registered(std::string_view key) const noexcept
{
const std::lock_guard<std::mutex> lock(m_mutex);
m_lookup_buffer.assign(key.data(), key.size());
return m_entity_container.count(m_lookup_buffer) > 0;
}
template <typename... Args>
void emplace_entity(Args&&... args)
{
std::lock_guard<std::mutex> const lock(m_mutex);
m_entity_container.emplace(std::forward<Args>(args)...);
m_last_key.reset();
}
[[nodiscard]] PrimaryKey get_primary_key_value_for_entity(
const std::string& key) const
{
const std::lock_guard<std::mutex> lock(m_mutex);
if(m_last_key && *m_last_key == key)
{
return m_last_value;
}
auto it = m_entity_container.find(key);
if(it == m_entity_container.end())
{
throw std::runtime_error(
fmt::format("Primary key not found for key: {}", key));
}
m_last_key = key;
m_last_value = it->second;
return it->second;
}
[[nodiscard]] PrimaryKey get_primary_key_value_for_entity(std::string_view key) const
{
const std::lock_guard<std::mutex> lock(m_mutex);
if(m_last_key && *m_last_key == key)
{
return m_last_value;
}
m_lookup_buffer.assign(key.data(), key.size());
auto it = m_entity_container.find(m_lookup_buffer);
if(it == m_entity_container.end())
{
throw std::runtime_error(
fmt::format("Primary key not found for key: {}", key));
}
m_last_key = m_lookup_buffer;
m_last_value = it->second;
return it->second;
}
private:
container_type_t m_entity_container;
mutable std::string m_lookup_buffer;
mutable std::mutex m_mutex;
mutable std::optional<std::string> m_last_key;
mutable PrimaryKey m_last_value{};
};
} // namespace profiler_hub