Add 'projects/rocprofiler-sdk/' from commit 'bf0fad1d5406fbc51403ba1aa9621a9d4a9bce2b'

git-subtree-dir: projects/rocprofiler-sdk
git-subtree-mainline: 50a90550e9
git-subtree-split: bf0fad1d54
This commit is contained in:
systems-assistant[bot]
2025-07-22 22:52:46 +00:00
1272 changed files with 230117 additions and 0 deletions
@@ -0,0 +1,8 @@
#
# add sql common sources to output library target
#
set(output_sql_headers common.hpp deferred_transaction.hpp extract_data_type.hpp)
set(output_sql_sources common.cpp deferred_transaction.cpp)
target_sources(rocprofiler-sdk-output-library PRIVATE ${output_sql_sources}
${output_sql_headers})
@@ -0,0 +1,205 @@
// MIT License
//
// Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "lib/output/sql/common.hpp"
#include "lib/output/kernel_symbol_info.hpp"
#include "lib/common/logging.hpp"
#include "lib/common/scope_destructor.hpp"
#include <rocprofiler-sdk/cxx/details/tokenize.hpp>
#include <rocprofiler-sdk/cxx/hash.hpp>
#include <rocprofiler-sdk/cxx/operators.hpp>
#include <fmt/format.h>
#include <sqlite3.h>
#include <iomanip>
#include <sstream>
#include <thread>
namespace rocprofiler
{
namespace tool
{
namespace sql
{
namespace sdk = ::rocprofiler::sdk;
void
check(std::string_view function, int status, std::string_view stmt)
{
if(status != SQLITE_OK)
{
ROCP_FATAL << "[" << function << "] " << stmt << " failed with error code " << status;
}
}
int
busy_handler(void* /*data*/, int count)
{
count = (count < 9) ? count : 8;
std::this_thread::sleep_for(std::chrono::microseconds{1000 * (0x1 << count)});
return 1;
}
// invoked during SELECT operation; unused but kept for reference
int
exec_callback(void* user_data, int ncols, char** coltext, char** colnames)
{
ROCP_INFO << "SQL callback invoked with " << ncols << " columns";
if(!coltext || !colnames) return SQLITE_OK;
auto header = std::stringstream{};
auto div = std::stringstream{};
auto content = std::stringstream{};
header << fmt::format(
"| {} |", fmt::join(std::vector<std::string_view>(colnames, colnames + ncols), " | "));
div << fmt::format("|-{}-|", std::string(header.str().size() - 4, '-'));
content << fmt::format(
"| {} |", fmt::join(std::vector<std::string_view>(coltext, coltext + ncols), " | "));
ROCP_WARNING << "SQL callback for " << ncols << " columns contents: "
<< "\n\t" << header.str() << "\n\t" << div.str() << "\n\t" << content.str();
return SQLITE_OK;
(void) user_data;
}
int64_t
execute_raw_sql_statements_impl(sqlite3* conn,
std::string_view stmts,
exec_callback_t callback,
void* data,
int line)
{
int64_t row_id = -1;
// NOLINTNEXTLINE(performance-for-range-copy)
for(auto stmt : sdk::parse::tokenize(stmts, ";"))
{
stmt = fmt::format("{};", std::move(stmt)); // make sure ends with semi-colon
ROCP_TRACE << "Executing SQLite3 statement: " << stmt;
char* msg = nullptr;
auto ret = sqlite3_exec(conn, stmt.c_str(), callback, data, &msg);
if(ret != SQLITE_OK)
{
// ensure no memory leak
auto dtor = common::scope_destructor{[msg]() {
if(msg) sqlite3_free(msg);
}};
static constexpr auto full_file = std::string_view{__FILE__};
static constexpr auto base_file = full_file.substr(full_file.find_last_of('/') + 1);
ROCP_FATAL << "SQLite3 error " << ret << ": " << ((msg) ? msg : "unknown error")
<< "\n\tSQLite3 error: " << base_file << ":" << line
<< "\n\tSQL Statement: " << stmt;
}
else
{
if(stmt.find("INSERT") != std::string::npos) row_id = sqlite3_last_insert_rowid(conn);
}
}
return row_id;
}
int64_t
execute_raw_sql_statements_impl(sqlite3* conn, std::string_view stmts, int line)
{
return execute_raw_sql_statements_impl(conn, stmts, exec_callback, nullptr, line);
}
std::string
extract_column_name(sqlite3_stmt* stmt, int32_t col)
{
return std::string{sqlite3_column_name(stmt, col)};
}
int64_t
extract_row_count(sqlite3* conn, std::string_view query)
{
auto _pos = query.find(';');
auto _query = (_pos == std::string_view::npos) ? query : query.substr(0, _pos);
auto _count_query = fmt::format("SELECT COUNT(*) AS count FROM ({}) x;", _query);
sqlite3_stmt* _stmt = nullptr;
if(sqlite3_prepare_v2(conn, _count_query.c_str(), -1, &_stmt, nullptr) != SQLITE_OK)
{
ROCP_CI_LOG(ERROR) << fmt::format(
"Error preparing select statement for '{}': {}", _count_query, sqlite3_errmsg(conn));
return 0;
}
ROCP_CI_LOG_IF(ERROR, _stmt == nullptr) << "Error preparing statment: " << query;
int64_t nrows = 0;
if(_stmt && sqlite3_column_count(_stmt) == 1 && sqlite3_step(_stmt) == SQLITE_ROW)
nrows = extract_column<int64_t>(_stmt, 0).value_or(0);
sqlite3_finalize(_stmt); // Finalize statement
ROCP_INFO << fmt::format("SQL query '{}' contains {} rows", query, nrows);
return nrows;
}
namespace
{
template <int Idx>
struct sql_data_type_info;
#define SPECIALIZE_SQL_DATA_TYPE_INFO(VALUE) \
template <> \
struct sql_data_type_info<VALUE> \
{ \
static constexpr auto value = VALUE; \
static constexpr auto name = #VALUE; \
};
SPECIALIZE_SQL_DATA_TYPE_INFO(SQLITE_INTEGER)
SPECIALIZE_SQL_DATA_TYPE_INFO(SQLITE_FLOAT)
SPECIALIZE_SQL_DATA_TYPE_INFO(SQLITE_BLOB)
SPECIALIZE_SQL_DATA_TYPE_INFO(SQLITE_NULL)
SPECIALIZE_SQL_DATA_TYPE_INFO(SQLITE_TEXT)
template <size_t Idx, size_t... Tail>
std::string_view
get_sql_data_type(int val, std::index_sequence<Idx, Tail...>)
{
using info_type = sql_data_type_info<Idx + 1>;
if(val == info_type::value) return std::string_view{info_type::name};
if constexpr(sizeof...(Tail) > 0) return get_sql_data_type(val, std::index_sequence<Tail...>{});
return std::string_view{"SQLITE_UNKNOWN"};
}
} // namespace
std::string_view
get_sql_data_type(int val)
{
return get_sql_data_type(val, std::make_index_sequence<SQLITE_NULL>{});
}
} // namespace sql
} // namespace tool
} // namespace rocprofiler
@@ -0,0 +1,129 @@
// MIT License
//
// Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
#include "lib/output/sql/extract_data_type.hpp"
#include "lib/common/container/ring_buffer.hpp"
#include "lib/common/mpl.hpp"
#include "lib/common/units.hpp"
#include <fmt/format.h>
#include <sqlite3.h>
#include <chrono>
#include <cstdint>
#include <string_view>
#include <type_traits>
namespace rocprofiler
{
namespace tool
{
namespace sql
{
using exec_callback_t = int (*)(void* user_data, int ncols, char** coltext, char** colnames);
template <typename Tp>
using ring_buffer_t = rocprofiler::common::container::ring_buffer<Tp>;
int
exec_callback(void* user_data, int ncols, char** coltext, char** colnames);
void
check(std::string_view function, int status, std::string_view stmt);
int
busy_handler(void* data, int count);
int64_t
execute_raw_sql_statements_impl(sqlite3* conn, std::string_view stmts, int line);
int64_t
execute_raw_sql_statements_impl(sqlite3* conn,
std::string_view stmts,
exec_callback_t callback,
void* data,
int line);
std::string
extract_column_name(sqlite3_stmt* stmt, int32_t col);
int64_t
extract_row_count(sqlite3* conn, std::string_view query);
std::string_view
get_sql_data_type(int val);
template <typename Tp>
auto
extract_column(sqlite3_stmt* stmt, int32_t col);
template <typename Tp, int ExpectedV>
bool
column_data_is_null(sqlite3_stmt* stmt, int32_t col);
//
//
// Template function definitions
//
//
template <typename Tp>
auto
extract_column(sqlite3_stmt* stmt, int32_t col)
{
return extract_data_type<Tp>{}(stmt, col);
}
template <typename Tp, int ExpectedV>
bool
column_data_is_null(sqlite3_stmt* stmt, int32_t col)
{
auto coltype = sqlite3_column_type(stmt, col);
if(coltype == SQLITE_NULL) return true;
std::string column_name = extract_column_name(stmt, col);
const char* sql_text = sqlite3_sql(stmt);
ROCP_CI_LOG_IF(WARNING, coltype != ExpectedV) << fmt::format(
"Data in SQL column {} ('{}') is neither NULL nor the expected data type ({} == {}). "
"Column data type is: ({} == {}), SQL: {}",
col,
column_name,
ExpectedV,
get_sql_data_type(ExpectedV),
coltype,
get_sql_data_type(coltype),
sql_text);
return false;
}
} // namespace sql
} // namespace tool
} // namespace rocprofiler
#define execute_raw_sql_statements(...) \
::rocprofiler::tool::sql::execute_raw_sql_statements_impl(__VA_ARGS__, __LINE__)
#define SQLITE3_CHECK(RESULT) \
::rocprofiler::tool::sql::check(__FUNCTION__, (RESULT), std::string_view{#RESULT})
@@ -0,0 +1,62 @@
// MIT License
//
// Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "lib/output/sql/deferred_transaction.hpp"
#include "lib/output/sql/common.hpp"
#include "lib/common/logging.hpp"
#include "lib/common/scope_destructor.hpp"
#include <fmt/format.h>
#include <sqlite3.h>
#include <iomanip>
#include <sstream>
#include <thread>
namespace rocprofiler
{
namespace tool
{
namespace sql
{
deferred_transaction::deferred_transaction(sqlite3* conn)
: m_conn{conn}
{
ROCP_CI_LOG_IF(INFO, m_conn == nullptr) << "rocprofiler::tool::sql::deferred_transaction "
"constructed will nullptr to sqlite3 connection";
if(m_conn)
{
execute_raw_sql_statements(m_conn, "BEGIN DEFERRED TRANSACTION");
}
}
deferred_transaction::~deferred_transaction()
{
if(m_conn)
{
execute_raw_sql_statements(m_conn, "END TRANSACTION");
}
}
} // namespace sql
} // namespace tool
} // namespace rocprofiler
@@ -0,0 +1,45 @@
// MIT License
//
// Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
#include "lib/output/sql/common.hpp"
#include <sqlite3.h>
namespace rocprofiler
{
namespace tool
{
namespace sql
{
struct deferred_transaction
{
explicit deferred_transaction(sqlite3* conn);
~deferred_transaction();
private:
sqlite3* m_conn = nullptr;
};
} // namespace sql
} // namespace tool
} // namespace rocprofiler
@@ -0,0 +1,141 @@
// MIT License
//
// Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
#include "lib/common/mpl.hpp"
#include <sqlite3.h>
#include <array>
#include <cstdint>
#include <optional>
#include <string>
#include <string_view>
#include <type_traits>
namespace rocprofiler
{
namespace tool
{
namespace sql
{
template <typename Tp, typename CondT = void>
struct extract_data_type;
template <typename Tp, int ExpectedV>
bool
column_data_is_null(sqlite3_stmt* stmt, int32_t col);
template <typename Tp>
struct extract_data_type<Tp, std::enable_if_t<std::is_integral<Tp>::value>>
{
static constexpr auto value = SQLITE_INTEGER;
std::optional<Tp> operator()(sqlite3_stmt* stmt, int32_t col) const
{
if(column_data_is_null<Tp, value>(stmt, col)) return std::nullopt;
if constexpr(std::is_signed<Tp>::value)
{
if constexpr(sizeof(Tp) > sizeof(int32_t))
return Tp{sqlite3_column_int64(stmt, col)};
else
return Tp{sqlite3_column_int(stmt, col)};
}
else
{
auto val = sqlite3_column_int64(stmt, col);
return static_cast<Tp>(val);
}
}
};
template <typename Tp>
struct extract_data_type<Tp, std::enable_if_t<std::is_floating_point<Tp>::value>>
{
static constexpr auto value = SQLITE_FLOAT;
std::optional<Tp> operator()(sqlite3_stmt* stmt, int32_t col) const
{
if(column_data_is_null<Tp, value>(stmt, col)) return std::nullopt;
return Tp{sqlite3_column_double(stmt, col)};
}
};
template <typename Tp>
struct extract_data_type<Tp, std::enable_if_t<common::mpl::is_string_type<Tp>::value>>
{
static constexpr auto value = SQLITE_TEXT;
std::optional<Tp> operator()(sqlite3_stmt* stmt, int32_t col) const
{
if(column_data_is_null<Tp, value>(stmt, col)) return std::nullopt;
const auto* ret = reinterpret_cast<const char*>(sqlite3_column_text(stmt, col));
if constexpr(std::is_constructible<Tp, const char*>::value) return Tp{ret};
return ret;
}
};
template <typename Tp, size_t N>
struct extract_data_type<
std::array<Tp, N>,
std::enable_if_t<std::is_integral<Tp>::value && sizeof(Tp) == sizeof(uint8_t)>>
{
static constexpr auto value = SQLITE_BLOB;
using value_type = std::array<Tp, N>;
std::optional<value_type> operator()(sqlite3_stmt* stmt, int32_t col) const
{
if(column_data_is_null<value_type, value>(stmt, col)) return std::nullopt;
const auto* val = reinterpret_cast<const Tp*>(sqlite3_column_blob(stmt, col));
auto ret = value_type{};
ret.fill(0);
uint64_t nbytes = std::min<uint64_t>(sqlite3_column_bytes(stmt, col), ret.size());
for(uint64_t i = 0; i < nbytes; ++i)
ret.at(i) = val[i];
return ret;
}
};
template <typename Tp>
struct extract_data_type<Tp, std::enable_if_t<std::is_same<Tp, std::nullptr_t>::value>>
{
static constexpr auto value = SQLITE_TEXT;
std::optional<Tp> operator()(sqlite3_stmt* stmt, int32_t col) const
{
if(column_data_is_null<Tp, value>(stmt, col)) return std::nullopt;
return std::nullptr_t{};
}
};
} // namespace sql
} // namespace tool
} // namespace rocprofiler