Skip to content

Commit a96489e

Browse files
adjordje-amdAleksandar Djordjevic
andauthored
Fix issue where all databases have the same UUID (#1499)
Co-authored-by: Aleksandar Djordjevic <[email protected]>
1 parent ddabf6d commit a96489e

7 files changed

Lines changed: 52 additions & 43 deletions

File tree

projects/rocprofiler-systems/source/lib/core/rocpd/data_processor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ data_processor::data_processor(std::shared_ptr<data_storage::database> database)
3939
throw std::invalid_argument("Provided pointer to a non-existing database!");
4040
}
4141
_database->initialize_schema();
42-
_upid = data_storage::database::get_upid();
42+
_upid = _database->get_upid();
4343

4444
initialize_event_stmt();
4545
initialize_pmc_event_stmt();

projects/rocprofiler-systems/source/lib/core/rocpd/data_processor.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626
#include <cstdint>
2727
#include <functional>
2828
#include <memory>
29-
#include <mutex>
3029
#include <optional>
31-
#include <set>
3230
#include <string>
3331
#include <unordered_map>
3432

projects/rocprofiler-systems/source/lib/core/rocpd/data_storage/database.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ namespace rocpd
5151
{
5252
namespace data_storage
5353
{
54-
database::database(const std::string& _tag)
54+
database::database(int pid, int ppid)
5555
{
56+
auto _tag = std::to_string(pid);
5657
auto db_name = std::string{ "rocpd" };
5758
auto abs_db_path = rocprofsys::get_database_absolute_path(db_name, _tag);
5859
create_directory_for_database_file(abs_db_path);
@@ -62,6 +63,7 @@ database::database(const std::string& _tag)
6263
"database open failed!");
6364
validate_sqlite3_result(sqlite3_open(abs_db_path.c_str(), &_sqlite3_db), "",
6465
"database open failed!");
66+
m_upid = generate_upid(pid, ppid);
6567
}
6668

6769
database::~database()
@@ -114,8 +116,10 @@ database::initialize_schema()
114116
std::regex guid_pattern("\\{\\{guid\\}\\}");
115117
std::regex view_upid_pattern("\\{\\{view_upid\\}\\}");
116118

117-
query = std::regex_replace(query, upid_pattern, "_" + get_upid());
118-
query = std::regex_replace(query, guid_pattern, get_upid());
119+
auto upid = get_upid();
120+
121+
query = std::regex_replace(query, upid_pattern, "_" + upid);
122+
query = std::regex_replace(query, guid_pattern, upid);
119123
query = std::regex_replace(query, view_upid_pattern, "");
120124

121125
validate_sqlite3_result(
@@ -135,12 +139,15 @@ database::execute_query(const std::string& query)
135139
std::string
136140
database::get_upid()
137141
{
138-
static std::string _upid = []() {
139-
auto n_info = node_info::get_instance();
140-
auto guid = common::md5sum{ n_info.id, getpid(), getppid() };
141-
return guid.hexdigest();
142-
}();
143-
return _upid;
142+
return m_upid;
143+
}
144+
145+
std::string
146+
database::generate_upid(const int pid, const int ppid)
147+
{
148+
auto n_info = node_info::get_instance();
149+
auto guid = common::md5sum{ n_info.id, pid, ppid };
150+
return guid.hexdigest();
144151
}
145152

146153
size_t

projects/rocprofiler-systems/source/lib/core/rocpd/data_storage/database.hpp

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static std::mutex _mutex;
3838
class database
3939
{
4040
public:
41-
explicit database(const std::string& _tag);
41+
explicit database(int pid, int ppid);
4242
database() = delete;
4343
database(database&) = delete;
4444
database& operator=(database&) = delete;
@@ -51,8 +51,8 @@ class database
5151

5252
private:
5353
template <typename... Args>
54-
inline void validate_sqlite3_result(int sqlite3_error_code, const char* query,
55-
Args&&... args)
54+
void validate_sqlite3_result(int sqlite3_error_code, const char* query,
55+
Args&&... args)
5656
{
5757
std::stringstream ss;
5858
ss << "\n===========================================================\n";
@@ -112,17 +112,16 @@ class database
112112
std::is_same_v<std::decay_t<T>, int32_t> ||
113113
std::is_same_v<std::decay_t<T>, uint32_t>),
114114
int> = 0>
115-
inline void bind_value([[maybe_unused]] sqlite3_stmt* stmt,
116-
[[maybe_unused]] int position, [[maybe_unused]] T& _value,
117-
[[maybe_unused]] const std::string& query)
115+
void bind_value([[maybe_unused]] sqlite3_stmt* stmt, [[maybe_unused]] int position,
116+
[[maybe_unused]] T& _value, [[maybe_unused]] const std::string& query)
118117
{
119118
throw std::runtime_error("Unsupported type for binding!");
120119
}
121120

122121
template <typename T,
123122
std::enable_if_t<common::traits::is_string_literal<T>(), int> = 0>
124-
inline void bind_value(sqlite3_stmt* stmt, int position, T&& _value,
125-
const std::string& query)
123+
void bind_value(sqlite3_stmt* stmt, int position, T&& _value,
124+
const std::string& query)
126125
{
127126
validate_sqlite3_result(
128127
sqlite3_bind_text(stmt, position, _value, -1, SQLITE_STATIC), query.c_str(),
@@ -131,8 +130,8 @@ class database
131130

132131
template <typename T,
133132
std::enable_if_t<std::is_floating_point_v<std::decay_t<T>>, int> = 0>
134-
inline void bind_value(sqlite3_stmt* stmt, int position, T&& _value,
135-
const std::string& query)
133+
void bind_value(sqlite3_stmt* stmt, int position, T&& _value,
134+
const std::string& query)
136135
{
137136
validate_sqlite3_result(
138137
sqlite3_bind_double(stmt, position, _value), query.c_str(),
@@ -142,8 +141,8 @@ class database
142141
template <typename T, std::enable_if_t<std::is_same_v<std::decay_t<T>, int64_t> ||
143142
std::is_same_v<std::decay_t<T>, uint64_t>,
144143
int> = 0>
145-
inline void bind_value(sqlite3_stmt* stmt, int position, T&& _value,
146-
const std::string& query)
144+
void bind_value(sqlite3_stmt* stmt, int position, T&& _value,
145+
const std::string& query)
147146
{
148147
validate_sqlite3_result(sqlite3_bind_int64(stmt, position, _value), query.c_str(),
149148
"Failed to bind int64_t/uint64_t! Position: ", position,
@@ -153,8 +152,8 @@ class database
153152
template <typename T, std::enable_if_t<std::is_same_v<std::decay_t<T>, int32_t> ||
154153
std::is_same_v<std::decay_t<T>, uint32_t>,
155154
int> = 0>
156-
inline void bind_value(sqlite3_stmt* stmt, int position, T&& _value,
157-
const std::string& query)
155+
void bind_value(sqlite3_stmt* stmt, int position, T&& _value,
156+
const std::string& query)
158157
{
159158
validate_sqlite3_result(sqlite3_bind_int(stmt, position, _value), query.c_str(),
160159
"Failed to bind int32_t/uint32_t! Position: ", position,
@@ -194,11 +193,16 @@ class database
194193
};
195194
}
196195

197-
static std::string get_upid();
196+
std::string get_upid();
198197

199198
private:
200-
sqlite3* _sqlite3_db{ nullptr };
201-
sqlite3* _sqlite3_db_temp{ nullptr };
199+
static std::string generate_upid(const int pid, const int ppid);
200+
201+
private:
202+
sqlite3* _sqlite3_db{ nullptr };
203+
sqlite3* _sqlite3_db_temp{ nullptr };
204+
std::string m_tag;
205+
std::string m_upid;
202206
};
203207

204208
} // namespace data_storage

projects/rocprofiler-systems/source/lib/core/trace_cache/cache_manager.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#include "trace_cache/metadata_registry.hpp"
3131
#include "trace_cache/rocpd_post_processing.hpp"
3232
#include <algorithm>
33-
#include <iterator>
3433
#include <memory>
3534
#include <vector>
3635

@@ -46,8 +45,7 @@ list_dir_files(const std::string& path)
4645
DIR* dir = opendir(path.c_str());
4746
if(dir == nullptr)
4847
{
49-
std::cerr << "Error opening directory: " << path << std::endl;
50-
return {};
48+
ROCPROFSYS_THROW("Error opening directory: %s", path.c_str());
5149
}
5250

5351
std::vector<std::string> result{};
@@ -140,8 +138,10 @@ cache_manager::post_process_bulk()
140138
ROCPROFSYS_SCOPED_SAMPLING_ON_CHILD_THREADS(false);
141139

142140
rocpd_threads.emplace_back([this]() {
141+
auto pid = getpid();
142+
auto ppid = get_root_process_id();
143143
rocpd_post_processing _post_processing(
144-
m_metadata, get_agent_manager_instance(), std::to_string(getpid()));
144+
m_metadata, get_agent_manager_instance(), pid, ppid);
145145
storage_parser _parser(
146146
get_buffered_storage_filename(get_root_process_id(), getpid()));
147147
_post_processing.register_parser_callback(_parser);
@@ -172,8 +172,9 @@ cache_manager::post_process_bulk()
172172
}
173173

174174
agent_manager _agent_manager{ _agents };
175+
auto ppid = get_root_process_id();
175176
rocpd_post_processing _post_processing(_metadata, _agent_manager,
176-
std::to_string(pid));
177+
pid, ppid);
177178
storage_parser _parser(files.buff_storage);
178179
_post_processing.register_parser_callback(_parser);
179180
_post_processing.post_process_metadata();

projects/rocprofiler-systems/source/lib/core/trace_cache/rocpd_post_processing.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ auto
5656
get_handle_from_code_object(
5757
const rocprofiler_callback_tracing_code_object_load_data_t& code_object)
5858
{
59-
# if(ROCPROFILER_VERSION >= 600)
59+
# if (ROCPROFILER_VERSION >= 600)
6060
return code_object.agent_id.handle;
6161
# else
6262
return code_object.rocp_agent.handle;
@@ -164,7 +164,7 @@ rocpd_post_processing::get_memory_copy_callback() const
164164
};
165165
}
166166

167-
#if(ROCPROFSYS_USE_ROCM > 0 && ROCPROFILER_VERSION >= 600)
167+
#if (ROCPROFSYS_USE_ROCM > 0 && ROCPROFILER_VERSION >= 600)
168168
postprocessing_callback
169169
rocpd_post_processing::get_memory_allocate_callback() const
170170
{
@@ -660,12 +660,11 @@ rocpd_post_processing::get_cpu_freq_sample_callback() const
660660
}
661661

662662
rocpd_post_processing::rocpd_post_processing(metadata_registry& md,
663-
agent_manager& agent_mngr,
664-
const std::string& _database_tag)
663+
agent_manager& agent_mngr, int pid, int ppid)
665664
: m_metadata(md)
666665
, m_agent_manager(agent_mngr)
667666
, m_data_processor(std::make_shared<rocpd::data_processor>(
668-
std::make_shared<rocpd::data_storage::database>(_database_tag)))
667+
std::make_shared<rocpd::data_storage::database>(pid, ppid)))
669668
{}
670669

671670
void
@@ -680,7 +679,7 @@ rocpd_post_processing::register_parser_callback([[maybe_unused]] storage_parser&
680679
parser.register_type_callback(entry_type::kernel_dispatch,
681680
get_kernel_dispatch_callback());
682681
parser.register_type_callback(entry_type::memory_copy, get_memory_copy_callback());
683-
# if(ROCPROFILER_VERSION >= 600)
682+
# if (ROCPROFILER_VERSION >= 600)
684683
parser.register_type_callback(entry_type::memory_alloc,
685684
get_memory_allocate_callback());
686685
# endif

projects/rocprofiler-systems/source/lib/core/trace_cache/rocpd_post_processing.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ namespace trace_cache
3535
class rocpd_post_processing
3636
{
3737
public:
38-
rocpd_post_processing(metadata_registry& metadata, agent_manager& agent_mngr,
39-
const std::string& _database_tag);
38+
rocpd_post_processing(metadata_registry& metadata, agent_manager& agent_mngr, int pid,
39+
int ppid);
4040

4141
void register_parser_callback(storage_parser& parser);
4242
void post_process_metadata();
@@ -51,7 +51,7 @@ class rocpd_post_processing
5151

5252
postprocessing_callback get_kernel_dispatch_callback() const;
5353
postprocessing_callback get_memory_copy_callback() const;
54-
#if(ROCPROFILER_VERSION >= 600)
54+
#if (ROCPROFILER_VERSION >= 600)
5555
postprocessing_callback get_memory_allocate_callback() const;
5656
#endif
5757
postprocessing_callback get_region_callback() const;

0 commit comments

Comments
 (0)