[rocprofv3] SQLite3 database output (rocpd) support + rocprofiler-sdk-rocpd (#403)
* [rocprofv3] rocpd SQLite3 database output support
* Move counters xml and yaml to source/share/rocprofiler-sdk
- more representative of install hierarchy
* Add share/rocprofiler-sdk/rocpd SQL files
* Experimental rocprofiler-sdk SQL API
* rocprofv3 default output format is rocpd
* Fix rocpd event ids for counter collection w/o kernel dispatch
* Remove fktable entries from rocpd_tables.sql
* Fix rocpd schema path
* Fix install component for roctx python bindings
* rocprofiler-sdk-rocpd
- create include/rocprofiler-sdk-rocpd
- create rocprofiler-sdk-rocpd library, package, etc.
- default all "guid" fields to "{{guid}}" in tables
- remove "{{view_uuid}}" support (always unused)
* Migrate rocprofv3 to use rocprofiler-sdk-rocpd
* Fix missing foreign key reference
* Revert change
* Fix cmake comment
* Fix maybe-uninitialized compiler warning
* Fix maybe-uninitialized compiler warning
* Add logging to rocpd_sql_load_schema
* Improve string sanitization when inserting json strings
* Initialize rocpd logging on rocprofiler-sdk-rocpd library load
* Revert lib/output/generatePerfetto.cpp changes
* [temporary] Tweak rocprofv3-test-list-avail-trace-execute test log level
* Update get_install_path for lib/rocprofiler-sdk-rocpd/sql.cpp
- try to resolve issues on RHEL/SLES for dladdr
* Update lib/common/logging.cpp
- enable environ overrides
* dlsym for rocpd_sql_load_schema
* Make dl_info.dli_fname lexically normal
* Implement node_info alternatives if /etc/machine-id does not exist
* Misc include fixes
* SHA256 and UUIDv7 support
* Implement UUIDv7 in generateRocpd.cpp
* Support push/pop environment variables
* Minor tweak
* Fix glog segfaults when unsetting glog env
* Updated CHANGELOG
* Updates tests/pytest-packages
- rocpd_reader.py: RocpdReader
* Update tests / marker_views.sql
- add test_rocpd_data
* Update rocpd_tables.sql
- Use AUTOINCREMENT
- insert "uuid" and "guid" into rocpd_metadata
* Minor updates to generateRocpd.cpp
- don't quote GUID
- use sqlite3_open_v2
- use sqlite3_close_v2
* Update execute_raw_sql_statements_impl
- uses sqlite3_last_insert_rowid for autoincrement
* Update SQL deferred_transaction
- CI check for nullptr to connection
* Apply suggestions from code review
Co-authored-by: Welton, Benjamin <Benjamin.Welton@amd.com>
* Code review updates
- formatting
- replace if with switch
- remove loop for {{uuid}}
* Fix pmc_groups handling in rocprofv3
* Address code review feedback
- Include rocm_version in rocprofv3 version info
- Note `--version` option for `rocprofv3` in CHANGELOG.md
- remove commented out code
* Fix packaging dependencies
* Fix install package step of CI workflow
* Fix install package step of CI workflow
---------
Co-authored-by: Jonathan R. Madsen <jonathanrmadsen@gmail.com>
Co-authored-by: Welton, Benjamin <Benjamin.Welton@amd.com>
Bu işleme şunda yer alıyor:
işlemeyi yapan:
GitHub
ebeveyn
dbb2e52216
işleme
7afedc63be
@@ -9,11 +9,13 @@ set(common_sources
|
||||
environment.cpp
|
||||
logging.cpp
|
||||
md5sum.cpp
|
||||
sha256.cpp
|
||||
simple_timer.cpp
|
||||
static_object.cpp
|
||||
static_tl_object.cpp
|
||||
string_entry.cpp
|
||||
utility.cpp)
|
||||
utility.cpp
|
||||
uuid_v7.cpp)
|
||||
set(common_headers
|
||||
abi.hpp
|
||||
defines.hpp
|
||||
@@ -26,6 +28,7 @@ set(common_headers
|
||||
md5sum.hpp
|
||||
mpl.hpp
|
||||
scope_destructor.hpp
|
||||
sha256.hpp
|
||||
simple_timer.hpp
|
||||
static_object.hpp
|
||||
static_tl_object.hpp
|
||||
@@ -33,7 +36,8 @@ set(common_headers
|
||||
stringize_arg.hpp
|
||||
synchronized.hpp
|
||||
units.hpp
|
||||
utility.hpp)
|
||||
utility.hpp
|
||||
uuid_v7.hpp)
|
||||
|
||||
add_library(rocprofiler-sdk-common-library STATIC)
|
||||
add_library(rocprofiler-sdk::rocprofiler-sdk-common-library ALIAS
|
||||
|
||||
@@ -151,5 +151,56 @@ SPECIALIZE_SET_ENV(std::string_view)
|
||||
SPECIALIZE_SET_ENV(float)
|
||||
SPECIALIZE_SET_ENV(double)
|
||||
} // namespace impl
|
||||
|
||||
env_store::env_store(std::initializer_list<env_config>&& _container)
|
||||
{
|
||||
for(const auto& itr : _container)
|
||||
{
|
||||
m_original.emplace_back(env_config{itr.env_name, get_env(itr.env_name, ""), 1});
|
||||
m_modified.emplace_back(env_config{itr.env_name, itr.env_value, 1});
|
||||
}
|
||||
}
|
||||
|
||||
env_store::~env_store() { pop(); }
|
||||
|
||||
bool
|
||||
env_store::push()
|
||||
{
|
||||
// not that push ignored bc already pushed
|
||||
if(m_pushed) return false;
|
||||
|
||||
for(const auto& itr : m_modified)
|
||||
itr();
|
||||
|
||||
m_pushed = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
env_store::pop(bool unset_if_empty)
|
||||
{
|
||||
if(!m_pushed) return false;
|
||||
|
||||
for(const auto& itr : m_original)
|
||||
{
|
||||
auto _current = get_env(itr.env_name, "");
|
||||
if(!unset_if_empty && itr.env_value.empty())
|
||||
continue;
|
||||
else if(_current == itr.env_value)
|
||||
continue;
|
||||
else if(_current != itr.env_value)
|
||||
{
|
||||
ROCP_INFO << fmt::format("[rocprofiler][env][pop] {}=\"{}\" => {}=\"{}\"",
|
||||
itr.env_name,
|
||||
_current,
|
||||
itr.env_name,
|
||||
itr.env_value);
|
||||
}
|
||||
itr();
|
||||
}
|
||||
|
||||
m_pushed = false;
|
||||
return true;
|
||||
}
|
||||
} // namespace common
|
||||
} // namespace rocprofiler
|
||||
|
||||
@@ -88,11 +88,49 @@ struct env_config
|
||||
|
||||
auto operator()(bool _verbose = false) const
|
||||
{
|
||||
if(env_name.empty()) return -1;
|
||||
ROCP_INFO_IF(_verbose) << "[rocprofiler][set_env] setenv(\"" << env_name << "\", \""
|
||||
<< env_value << "\", " << overwrite << ")\n";
|
||||
return setenv(env_name.c_str(), env_value.c_str(), overwrite);
|
||||
if(env_name.empty())
|
||||
return -1;
|
||||
else if(_verbose)
|
||||
{
|
||||
ROCP_INFO << "[rocprofiler][set_env] setenv(\"" << env_name << "\", \"" << env_value
|
||||
<< "\", " << overwrite << ")\n";
|
||||
}
|
||||
return (env_value.empty() && overwrite > 0)
|
||||
? unsetenv(env_name.c_str())
|
||||
: setenv(env_name.c_str(), env_value.c_str(), overwrite);
|
||||
}
|
||||
};
|
||||
|
||||
struct env_store
|
||||
{
|
||||
template <template <typename, typename...> class ContainerT, typename... TailT>
|
||||
explicit env_store(ContainerT<env_config, TailT...>&& _container);
|
||||
explicit env_store(std::initializer_list<env_config>&& _container);
|
||||
|
||||
~env_store();
|
||||
env_store(const env_store&) = default;
|
||||
env_store(env_store&&) noexcept = default;
|
||||
env_store& operator=(const env_store&) = default;
|
||||
env_store& operator=(env_store&&) noexcept = default;
|
||||
|
||||
bool push();
|
||||
bool pop(bool unset_if_empty = true);
|
||||
bool is_pushed() const { return m_pushed; }
|
||||
|
||||
private:
|
||||
bool m_pushed = false;
|
||||
std::vector<env_config> m_original = {};
|
||||
std::vector<env_config> m_modified = {};
|
||||
};
|
||||
|
||||
template <template <typename, typename...> class ContainerT, typename... TailT>
|
||||
env_store::env_store(ContainerT<env_config, TailT...>&& _container)
|
||||
{
|
||||
for(const auto& itr : _container)
|
||||
{
|
||||
m_original.emplace_back(env_config{itr.env_name, get_env(itr.env_name, ""), 1});
|
||||
m_modified.emplace_back(env_config{itr.env_name, itr.env_value, 1});
|
||||
}
|
||||
}
|
||||
} // namespace common
|
||||
} // namespace rocprofiler
|
||||
|
||||
@@ -53,6 +53,34 @@ struct log_level_info
|
||||
int32_t google_level = 0;
|
||||
int32_t verbose_level = 0;
|
||||
};
|
||||
|
||||
env_store
|
||||
get_glog_env_config(const logging_config& cfg)
|
||||
{
|
||||
auto as_env_config = [](std::string_view _var, auto _val) {
|
||||
return env_config{std::string{_var}, fmt::format("{}", _val), 1};
|
||||
};
|
||||
|
||||
auto _data = std::vector<env_config>{
|
||||
as_env_config("GLOG_minloglevel", cfg.loglevel),
|
||||
as_env_config("GLOG_logtostderr", cfg.logtostderr ? 1 : 0),
|
||||
as_env_config("GLOG_alsologtostderr", cfg.alsologtostderr ? 1 : 0),
|
||||
as_env_config("GLOG_stderrthreshold", cfg.loglevel),
|
||||
as_env_config("GLOG_v", cfg.vlog_level),
|
||||
};
|
||||
|
||||
if(!cfg.logdir.empty())
|
||||
{
|
||||
_data.emplace_back(as_env_config("GOOGLE_LOG_DIR", cfg.logdir));
|
||||
_data.emplace_back(as_env_config("GLOG_log_dir", cfg.logdir));
|
||||
}
|
||||
if(!cfg.vlog_modules.empty())
|
||||
{
|
||||
_data.emplace_back(as_env_config("GLOG_vmodule", cfg.vlog_modules));
|
||||
}
|
||||
|
||||
return env_store{std::move(_data)};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void
|
||||
@@ -139,7 +167,10 @@ init_logging(std::string_view env_prefix, logging_config cfg)
|
||||
}
|
||||
}
|
||||
|
||||
update_logging(cfg, !google::IsGoogleLoggingInitialized());
|
||||
auto _env_store = get_glog_env_config(cfg);
|
||||
|
||||
update_logging(cfg);
|
||||
_env_store.push();
|
||||
|
||||
if(!google::IsGoogleLoggingInitialized())
|
||||
{
|
||||
@@ -158,11 +189,13 @@ init_logging(std::string_view env_prefix, logging_config cfg)
|
||||
|
||||
ROCP_INFO << "logging initialized via " << fmt::format("{}_LOG_LEVEL", env_prefix)
|
||||
<< ". Log Level: " << loglvl << ". Verbose Log Level: " << vlog_level;
|
||||
|
||||
_env_store.pop(false);
|
||||
});
|
||||
}
|
||||
|
||||
void
|
||||
update_logging(const logging_config& cfg, bool setup_env, int env_override)
|
||||
update_logging(const logging_config& cfg)
|
||||
{
|
||||
static auto _mtx = std::mutex{};
|
||||
auto _lk = std::unique_lock<std::mutex>{_mtx};
|
||||
@@ -192,22 +225,6 @@ update_logging(const logging_config& cfg, bool setup_env, int env_override)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(setup_env)
|
||||
{
|
||||
common::set_env("GLOG_minloglevel", cfg.loglevel, env_override);
|
||||
common::set_env("GLOG_logtostderr", cfg.logtostderr ? 1 : 0, env_override);
|
||||
common::set_env("GLOG_alsologtostderr", cfg.alsologtostderr ? 1 : 0, env_override);
|
||||
common::set_env("GLOG_stderrthreshold", cfg.loglevel, env_override);
|
||||
common::set_env("GLOG_v", cfg.vlog_level, env_override);
|
||||
if(!cfg.logdir.empty())
|
||||
{
|
||||
common::set_env("GOOGLE_LOG_DIR", cfg.logdir, env_override);
|
||||
common::set_env("GLOG_log_dir", cfg.logdir, env_override);
|
||||
}
|
||||
if(!cfg.vlog_modules.empty())
|
||||
common::set_env("GLOG_vmodule", cfg.vlog_modules, env_override);
|
||||
}
|
||||
}
|
||||
} // namespace common
|
||||
} // namespace rocprofiler
|
||||
|
||||
@@ -79,6 +79,6 @@ void
|
||||
init_logging(std::string_view env_prefix, logging_config cfg = logging_config{});
|
||||
|
||||
void
|
||||
update_logging(const logging_config& cfg, bool setup_env = false, int env_override = 0);
|
||||
update_logging(const logging_config& cfg);
|
||||
} // namespace common
|
||||
} // namespace rocprofiler
|
||||
|
||||
@@ -0,0 +1,229 @@
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2025 Advanced Micro Devices, Inc.
|
||||
//
|
||||
// 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/common/sha256.hpp"
|
||||
#include "lib/common/defines.hpp"
|
||||
#include "lib/common/logging.hpp"
|
||||
#include "lib/common/mpl.hpp"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace rocprofiler
|
||||
{
|
||||
namespace common
|
||||
{
|
||||
sha256::sha256() { reset(); }
|
||||
|
||||
sha256::sha256(const std::string& data)
|
||||
{
|
||||
reset();
|
||||
update(data);
|
||||
finalize();
|
||||
}
|
||||
|
||||
void
|
||||
sha256::update(const uint8_t* data, size_t len)
|
||||
{
|
||||
ROCP_CI_LOG_IF(INFO, m_finalized) << "attempt to update sha256 after finalized";
|
||||
if(m_finalized) return;
|
||||
for(size_t i = 0; i < len; ++i)
|
||||
{
|
||||
m_data[m_datalen++] = data[i];
|
||||
if(m_datalen == 64)
|
||||
{
|
||||
transform();
|
||||
m_bitlen += 512;
|
||||
m_datalen = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
sha256::update(const std::string& data)
|
||||
{
|
||||
ROCP_CI_LOG_IF(INFO, m_finalized) << "attempt to update sha256 after finalized";
|
||||
if(m_finalized) return;
|
||||
update(reinterpret_cast<const uint8_t*>(data.data()), data.size());
|
||||
}
|
||||
|
||||
void
|
||||
sha256::finalize()
|
||||
{
|
||||
if(m_finalized) return;
|
||||
|
||||
uint32_t idx = m_datalen;
|
||||
if(m_datalen < 56)
|
||||
{
|
||||
m_data[idx++] = 0x80;
|
||||
while(idx < 56)
|
||||
m_data[idx++] = 0x00;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_data[idx++] = 0x80;
|
||||
while(idx < 64)
|
||||
m_data[idx++] = 0x00;
|
||||
transform();
|
||||
std::memset(m_data.data(), 0, 56);
|
||||
}
|
||||
|
||||
m_bitlen += m_datalen * 8;
|
||||
for(int j = 0; j < 8; ++j)
|
||||
m_data[63 - j] = static_cast<uint8_t>((m_bitlen >> (8 * j)) & 0xFF);
|
||||
|
||||
transform();
|
||||
|
||||
m_finalized = true;
|
||||
}
|
||||
|
||||
std::string
|
||||
sha256::hexdigest()
|
||||
{
|
||||
finalize();
|
||||
|
||||
auto oss = std::ostringstream{};
|
||||
for(int j = 0; j < 8; ++j)
|
||||
oss << std::hex << std::setfill('0') << std::setw(8) << m_state[j];
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
std::array<uint32_t, 8>
|
||||
sha256::rawdigest()
|
||||
{
|
||||
finalize();
|
||||
|
||||
return m_state;
|
||||
}
|
||||
|
||||
uint32_t
|
||||
sha256::rotr(uint32_t x, uint32_t n)
|
||||
{
|
||||
return (x >> n) | (x << (32 - n));
|
||||
}
|
||||
|
||||
uint32_t
|
||||
sha256::ch(uint32_t x, uint32_t y, uint32_t z)
|
||||
{
|
||||
return (x & y) ^ (~x & z);
|
||||
}
|
||||
|
||||
uint32_t
|
||||
sha256::maj(uint32_t x, uint32_t y, uint32_t z)
|
||||
{
|
||||
return (x & y) ^ (x & z) ^ (y & z);
|
||||
}
|
||||
|
||||
uint32_t
|
||||
sha256::sig0(uint32_t x)
|
||||
{
|
||||
return rotr(x, 2) ^ rotr(x, 13) ^ rotr(x, 22);
|
||||
}
|
||||
|
||||
uint32_t
|
||||
sha256::sig1(uint32_t x)
|
||||
{
|
||||
return rotr(x, 6) ^ rotr(x, 11) ^ rotr(x, 25);
|
||||
}
|
||||
|
||||
uint32_t
|
||||
sha256::theta0(uint32_t x)
|
||||
{
|
||||
return rotr(x, 7) ^ rotr(x, 18) ^ (x >> 3);
|
||||
}
|
||||
|
||||
uint32_t
|
||||
sha256::theta1(uint32_t x)
|
||||
{
|
||||
return rotr(x, 17) ^ rotr(x, 19) ^ (x >> 10);
|
||||
}
|
||||
|
||||
void
|
||||
sha256::transform()
|
||||
{
|
||||
uint32_t m[64];
|
||||
for(int i = 0; i < 16; ++i)
|
||||
{
|
||||
m[i] = (m_data[i * 4] << 24) | (m_data[i * 4 + 1] << 16) | (m_data[i * 4 + 2] << 8) |
|
||||
(m_data[i * 4 + 3]);
|
||||
}
|
||||
for(int i = 16; i < 64; ++i)
|
||||
{
|
||||
m[i] = theta1(m[i - 2]) + m[i - 7] + theta0(m[i - 15]) + m[i - 16];
|
||||
}
|
||||
|
||||
uint32_t a = m_state[0];
|
||||
uint32_t b = m_state[1];
|
||||
uint32_t c = m_state[2];
|
||||
uint32_t d = m_state[3];
|
||||
uint32_t e = m_state[4];
|
||||
uint32_t f = m_state[5];
|
||||
uint32_t g = m_state[6];
|
||||
uint32_t h = m_state[7];
|
||||
|
||||
for(int i = 0; i < 64; ++i)
|
||||
{
|
||||
uint32_t t1 = h + sig1(e) + ch(e, f, g) + m_k[i] + m[i];
|
||||
uint32_t t2 = sig0(a) + maj(a, b, c);
|
||||
h = g;
|
||||
g = f;
|
||||
f = e;
|
||||
e = d + t1;
|
||||
d = c;
|
||||
c = b;
|
||||
b = a;
|
||||
a = t1 + t2;
|
||||
}
|
||||
|
||||
m_state[0] += a;
|
||||
m_state[1] += b;
|
||||
m_state[2] += c;
|
||||
m_state[3] += d;
|
||||
m_state[4] += e;
|
||||
m_state[5] += f;
|
||||
m_state[6] += g;
|
||||
m_state[7] += h;
|
||||
}
|
||||
|
||||
void
|
||||
sha256::reset()
|
||||
{
|
||||
m_state = {0x6a09e667,
|
||||
0xbb67ae85,
|
||||
0x3c6ef372,
|
||||
0xa54ff53a,
|
||||
0x510e527f,
|
||||
0x9b05688c,
|
||||
0x1f83d9ab,
|
||||
0x5be0cd19};
|
||||
m_datalen = 0;
|
||||
m_bitlen = 0;
|
||||
}
|
||||
} // namespace common
|
||||
} // namespace rocprofiler
|
||||
@@ -0,0 +1,81 @@
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2025 Advanced Micro Devices, Inc.
|
||||
//
|
||||
// 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/defines.hpp"
|
||||
#include "lib/common/mpl.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
namespace rocprofiler
|
||||
{
|
||||
namespace common
|
||||
{
|
||||
// --- SHA-256 Implementation ---
|
||||
class sha256
|
||||
{
|
||||
public:
|
||||
sha256();
|
||||
explicit sha256(const std::string& data);
|
||||
|
||||
void update(const uint8_t* data, size_t len);
|
||||
void update(const std::string& data);
|
||||
|
||||
void finalize();
|
||||
std::string hexdigest();
|
||||
std::array<uint32_t, 8> rawdigest();
|
||||
|
||||
private:
|
||||
bool m_finalized = false;
|
||||
std::array<uint8_t, 64> m_data = {};
|
||||
std::array<uint32_t, 8> m_state = {};
|
||||
uint32_t m_datalen = 0;
|
||||
uint64_t m_bitlen = 0;
|
||||
|
||||
static constexpr std::array<uint32_t, 64> m_k = {
|
||||
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4,
|
||||
0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe,
|
||||
0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f,
|
||||
0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
|
||||
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc,
|
||||
0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
|
||||
0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116,
|
||||
0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
||||
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7,
|
||||
0xc67178f2};
|
||||
|
||||
static uint32_t rotr(uint32_t x, uint32_t n);
|
||||
static uint32_t ch(uint32_t x, uint32_t y, uint32_t z);
|
||||
static uint32_t maj(uint32_t x, uint32_t y, uint32_t z);
|
||||
static uint32_t sig0(uint32_t x);
|
||||
static uint32_t sig1(uint32_t x);
|
||||
static uint32_t theta0(uint32_t x);
|
||||
static uint32_t theta1(uint32_t x);
|
||||
|
||||
void transform();
|
||||
void reset();
|
||||
};
|
||||
} // namespace common
|
||||
} // namespace rocprofiler
|
||||
@@ -0,0 +1,151 @@
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2025 Advanced Micro Devices, Inc.
|
||||
//
|
||||
// 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/common/uuid_v7.hpp"
|
||||
#include "lib/common/defines.hpp"
|
||||
#include "lib/common/logging.hpp"
|
||||
#include "lib/common/mpl.hpp"
|
||||
#include "lib/common/sha256.hpp"
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <unistd.h>
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <random>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace rocprofiler
|
||||
{
|
||||
namespace common
|
||||
{
|
||||
uint64_t
|
||||
get_process_start_ticks_since_boot(pid_t pid)
|
||||
{
|
||||
auto line = std::string{};
|
||||
|
||||
// Read the stat file
|
||||
if(auto stat_file = std::ifstream{fmt::format("/proc/{}/stat", pid)}; !stat_file.is_open())
|
||||
{
|
||||
ROCP_CI_LOG(WARNING) << fmt::format("failed to open /proc/{}/stat for process start time",
|
||||
pid);
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Read entire line
|
||||
std::getline(stat_file, line);
|
||||
}
|
||||
|
||||
// Locate the end of the comm field (")")
|
||||
size_t rparen = line.rfind(')');
|
||||
if(rparen == std::string::npos)
|
||||
{
|
||||
ROCP_CI_LOG(WARNING) << fmt::format("Malformed stat file for pid {}", pid);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Tokenize fields after ") "
|
||||
auto iss = std::istringstream{line.substr(rparen + 2)};
|
||||
auto token = std::string{};
|
||||
// Skip fields 3 through 21
|
||||
for(int i = 0; i < 20; ++i)
|
||||
{
|
||||
if(!(iss >> token))
|
||||
{
|
||||
ROCP_CI_LOG(WARNING) << fmt::format("Unexpected end of /proc/{}/stat", pid);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Field 22: starttime in clock ticks since boot
|
||||
uint64_t start_ticks = 0;
|
||||
if(!(iss >> start_ticks))
|
||||
{
|
||||
ROCP_CI_LOG(WARNING) << fmt::format(
|
||||
"Unexpected end of /proc/{}/stat. Failed to read start ticks", pid);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return start_ticks;
|
||||
}
|
||||
|
||||
uint64_t
|
||||
compute_system_seed(std::string_view machine_id, pid_t pid, pid_t ppid, uint64_t pstart_ticks)
|
||||
{
|
||||
// If no machine_id provided, read from /etc/machine-id
|
||||
ROCP_CI_LOG_IF(WARNING, machine_id.empty())
|
||||
<< fmt::format("compute_system_seed provided empty machine id");
|
||||
|
||||
// Hash for seed value
|
||||
return std::hash<std::string>{}(
|
||||
sha256{fmt::format("{}|{}|{}|{}", machine_id, pid, ppid, pstart_ticks)}.hexdigest());
|
||||
}
|
||||
|
||||
std::string
|
||||
generate_uuid_v7(uint64_t timestamp_ns, uint64_t seed, std::string_view delim)
|
||||
{
|
||||
constexpr auto nanosec_per_millisec = std::nano::den / std::milli::den;
|
||||
auto timestamp_ms = timestamp_ns / nanosec_per_millisec;
|
||||
|
||||
auto uuid = std::array<uint8_t, 16>{};
|
||||
|
||||
// First 6 bytes = timestamp
|
||||
for(int i = 0; i < 6; ++i)
|
||||
{
|
||||
uuid[i] = static_cast<uint8_t>((timestamp_ms >> (40 - 8 * i)) & 0xFF);
|
||||
}
|
||||
|
||||
// Set version to 7 with ordering based on timestamp.
|
||||
uuid[6] = static_cast<uint8_t>((timestamp_ms >> 8) & 0x0F);
|
||||
uuid[6] |= 0x70;
|
||||
|
||||
uuid[7] = static_cast<uint8_t>(timestamp_ms & 0xFF);
|
||||
|
||||
// Seeded RNG
|
||||
auto rand64 = std::mt19937_64{seed}();
|
||||
|
||||
for(int i = 0; i < 8; ++i)
|
||||
{
|
||||
uuid[8 + i] = static_cast<uint8_t>((rand64 >> (56 - 8 * i)) & 0xFF);
|
||||
}
|
||||
|
||||
// Set variant to RFC 4122
|
||||
uuid[8] = (uuid[8] & 0x3F) | 0x80;
|
||||
|
||||
// Format as UUID string
|
||||
auto oss = std::ostringstream{};
|
||||
oss << std::hex << std::setfill('0');
|
||||
for(int i = 0; i < 16; ++i)
|
||||
{
|
||||
oss << std::setw(2) << static_cast<int>(uuid[i]);
|
||||
if(i == 3 || i == 5 || i == 7 || i == 9) oss << delim;
|
||||
}
|
||||
|
||||
return oss.str();
|
||||
}
|
||||
} // namespace common
|
||||
} // namespace rocprofiler
|
||||
@@ -0,0 +1,49 @@
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2025 Advanced Micro Devices, Inc.
|
||||
//
|
||||
// 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/defines.hpp"
|
||||
#include "lib/common/mpl.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <random>
|
||||
#include <string>
|
||||
|
||||
namespace rocprofiler
|
||||
{
|
||||
namespace common
|
||||
{
|
||||
uint64_t
|
||||
get_process_start_ticks_since_boot(pid_t pid);
|
||||
|
||||
// use this function to create a deterministic random number seed for the system and process
|
||||
uint64_t
|
||||
compute_system_seed(std::string_view machine_id, pid_t pid, pid_t ppid, uint64_t pstart_ticks);
|
||||
|
||||
std::string
|
||||
generate_uuid_v7(uint64_t timestamp_ns,
|
||||
uint64_t seed = std::random_device{}(),
|
||||
std::string_view delim = "-");
|
||||
} // namespace common
|
||||
} // namespace rocprofiler
|
||||
Yeni konuda referans
Bir kullanıcı engelle