Skip to content

Commit 0d1457c

Browse files
kcossett-amddgaliffiAMD
authored andcommitted
[rocprofiler-systems] [ROCpd] Add OMPT callbacks to ROCpd (#1016)
* Add OMPT to ROCpd * Use correct category * Added wrapper functions for future control * Formatting * Fix naming * Comment change * Remove ompt_get_cb_args * Switched to using region_sample for OMPT * Remove relic function * Remove get_use_rocpd that was used in this pr (one still remains) * Rename ompt_get_args_string and reuse in tool_tracing_callback_stop * Make lock init and destroy cb instant * [Prototype] ROCPD Name fix * [Prototype] ROCPD Name fix P1 * [Prototype] ROCPD Name fix P2 * ROCPD Name fix * Var name changes * Rewrite cb overwrite to single function * [Important] Use parallel_data as key for parallel callback map * Fix workflow failure * Make cpp USE_ROCM consistent with hpp and use default constructor if USE_ROCM = 0 * Add missing ROCPROFILER_VERSION check * Improve readability * Make ompt storage maps thread local * Part 1: Variable name fix, memory cleanup, and fixed asserts * Part 2: Add comments * Part 3: Add CI_THROW * Part 4: Formatting * Part 5: Move #include to cpp
1 parent 98b100e commit 0d1457c

3 files changed

Lines changed: 359 additions & 36 deletions

File tree

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

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
// SOFTWARE.
2222

2323
#include "metadata_registry.hpp"
24+
#include "core/debug.hpp"
2425
#include <algorithm>
2526
#include <cstdint>
2627

@@ -210,7 +211,7 @@ metadata_registry::get_string_list() const
210211
return result;
211212
}
212213

213-
#if ROCPROFSYS_USE_ROCM
214+
#if ROCPROFSYS_USE_ROCM > 0
214215

215216
void
216217
metadata_registry::add_code_object(
@@ -278,6 +279,102 @@ metadata_registry::get_kernel_symbol_list() const
278279
return result;
279280
}
280281

282+
// As the underlying implementation of callback_name_info_t resizes the category storage
283+
// during emplace, this special method is required
284+
void
285+
metadata_registry::overwrite_callback_names(
286+
std::initializer_list<
287+
std::pair<rocprofiler_callback_tracing_kind_t, callback_rename_map_t>>
288+
rename_table)
289+
{
290+
if(rename_table.size() == 0) return;
291+
292+
using callback_kind_t = rocprofiler_callback_tracing_kind_t;
293+
using operation_names_t = std::vector<std::string_view>;
294+
295+
auto category_names = std::vector<std::string_view>{};
296+
auto modified_ops = std::map<callback_kind_t, operation_names_t>{};
297+
298+
auto extract_operations = [&](callback_kind_t cat) -> operation_names_t {
299+
auto items = m_callback_tracing_info.items();
300+
const auto* target_category = items[static_cast<size_t>(cat)];
301+
302+
auto operations_data = target_category->items();
303+
operation_names_t operation_names;
304+
operation_names.reserve(operations_data.size());
305+
306+
for(const auto& [op_idx, op_name] : operations_data)
307+
operation_names.push_back(*op_name);
308+
309+
return operation_names;
310+
};
311+
312+
// Store category names
313+
category_names.resize(ROCPROFILER_CALLBACK_TRACING_LAST);
314+
for(callback_kind_t i = ROCPROFILER_CALLBACK_TRACING_NONE;
315+
i < ROCPROFILER_CALLBACK_TRACING_LAST;
316+
i = static_cast<callback_kind_t>(static_cast<int>(i) + 1))
317+
{
318+
category_names[i] = m_callback_tracing_info.at(i);
319+
}
320+
321+
// Process list
322+
for(const auto& category_info : rename_table)
323+
{
324+
auto callback_kind = category_info.first;
325+
// Store operations of all following categories
326+
// as they will be deleted
327+
for(callback_kind_t i =
328+
static_cast<callback_kind_t>(static_cast<int>(callback_kind) + 1);
329+
i < ROCPROFILER_CALLBACK_TRACING_LAST;
330+
i = static_cast<callback_kind_t>(static_cast<int>(i) + 1))
331+
{
332+
if(modified_ops.find(i) != modified_ops.end()) break;
333+
modified_ops[i] = extract_operations(i);
334+
}
335+
336+
ROCPROFSYS_CI_THROW(modified_ops.find(callback_kind) != modified_ops.end(),
337+
"Overwriting a previously overwritten entry is forbidden");
338+
339+
ROCPROFSYS_CI_THROW(!modified_ops.empty() &&
340+
callback_kind >= modified_ops.begin()->first,
341+
"Category must have a larger enum value than all previously "
342+
"modified_ops categories");
343+
344+
// Overwrite desired category
345+
auto operation_names = extract_operations(callback_kind);
346+
for(const auto& [index, new_value] : category_info.second)
347+
{
348+
ROCPROFSYS_CI_THROW(index < 0 ||
349+
static_cast<size_t>(index) >= operation_names.size(),
350+
"Index is invalid");
351+
operation_names[index] = new_value;
352+
}
353+
modified_ops[callback_kind] = std::move(operation_names);
354+
}
355+
if(modified_ops.empty()) return;
356+
357+
// Emplace the changed category operations
358+
for(callback_kind_t i = modified_ops.begin()->first;
359+
i < ROCPROFILER_CALLBACK_TRACING_LAST;
360+
i = static_cast<callback_kind_t>(static_cast<int>(i) + 1))
361+
{
362+
auto renaming_entry = modified_ops.find(i);
363+
364+
ROCPROFSYS_CI_THROW(renaming_entry == modified_ops.end(),
365+
"A category that needs to be emplaced is missing");
366+
367+
const auto& operations_vec = renaming_entry->second;
368+
m_callback_tracing_info.emplace(i, category_names.at(i).data());
369+
for(size_t op_idx = 0; op_idx < operations_vec.size(); ++op_idx)
370+
{
371+
m_callback_tracing_info.emplace(
372+
i, static_cast<rocprofiler_tracing_operation_t>(op_idx),
373+
operations_vec[op_idx].data());
374+
}
375+
}
376+
}
377+
281378
rocprofiler::sdk::buffer_name_info_t<const char*>
282379
metadata_registry::get_buffer_name_info() const
283380
{
@@ -292,5 +389,20 @@ metadata_registry::get_callback_tracing_info() const
292389

293390
#endif
294391

392+
metadata_registry::metadata_registry()
393+
{
394+
#if ROCPROFSYS_USE_ROCM > 0
395+
overwrite_callback_names({
396+
# if(ROCPROFILER_VERSION >= 600)
397+
{ ROCPROFILER_CALLBACK_TRACING_OMPT,
398+
{ { ROCPROFILER_OMPT_ID_thread_begin, "omp_thread" },
399+
{ ROCPROFILER_OMPT_ID_thread_end, "omp_thread" },
400+
{ ROCPROFILER_OMPT_ID_parallel_begin, "omp_parallel" },
401+
{ ROCPROFILER_OMPT_ID_parallel_end, "omp_parallel" } } }
402+
# endif
403+
});
404+
#endif
405+
}
406+
295407
} // namespace trace_cache
296408
} // namespace rocprofsys

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
# include <rocprofiler-sdk/callback_tracing.h>
3636
# include <rocprofiler-sdk/cxx/name_info.hpp>
3737
#endif
38+
#include <initializer_list>
39+
#include <map>
3840
#include <set>
3941
#include <sstream>
4042
#include <stdint.h>
@@ -215,7 +217,7 @@ struct metadata_registry
215217

216218
private:
217219
friend class cache_manager;
218-
metadata_registry() = default;
220+
metadata_registry();
219221
common::synchronized<info::process> m_process;
220222
common::synchronized<
221223
std::unordered_set<info::pmc, info::pmc_info_hash, info::pmc_info_equal>>
@@ -240,6 +242,14 @@ struct metadata_registry
240242
rocprofiler::sdk::callback_name_info_t<const char*> m_callback_tracing_info{
241243
rocprofiler::sdk::get_callback_tracing_names<const char*>()
242244
};
245+
246+
using callback_rename_map_t =
247+
std::map<rocprofiler_tracing_operation_t, std::string_view>;
248+
249+
void overwrite_callback_names(
250+
std::initializer_list<
251+
std::pair<rocprofiler_callback_tracing_kind_t, callback_rename_map_t>>
252+
rename_table);
243253
#endif
244254
};
245255

0 commit comments

Comments
 (0)