Skip to content

Commit

Permalink
Making ATT work with Profilerv2
Browse files Browse the repository at this point in the history
Change-Id: Ic9334aa80e40faaaf5c1a79ba37dbe52e8d31253
  • Loading branch information
bgopesh authored and ammarwa committed Mar 9, 2023
1 parent 6dda141 commit 03c305d
Show file tree
Hide file tree
Showing 28 changed files with 3,783 additions and 24 deletions.
93 changes: 91 additions & 2 deletions inc/rocprofiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ typedef enum {
/**
* Represents a ATT tracing record (Not available yet)
*/
ROCPROFILER_ATT_RECORD = 2,
ROCPROFILER_ATT_TRACER_RECORD = 2,
/**
* Represents a PC sampling record
*/
Expand Down Expand Up @@ -1046,6 +1046,66 @@ typedef struct {

} rocprofiler_record_spm_t;

/**
* struct to store the trace data from a shader engine.
*/
typedef struct {
void* buffer_ptr;
uint32_t buffer_size;
} rocprofiler_record_se_att_data_t;

/**
* ATT tracing record structure.
* This will represent all the information reported by the
* ATT tracer such as the kernel and its thread trace data.
* This record can be flushed to the user using
* ::rocmtools_buffer_callback_t
*/
typedef struct {
/**
* ROCMtool General Record base header to identify the id and kind of every
* record
*/
rocprofiler_record_header_t header;
/**
* Kernel Identifier to be used by the user to get the kernel info using
* ::rocmtools_query_kernel_info
*/
rocprofiler_kernel_id_t kernel_id;
/**
* Agent Identifier to be used by the user to get the Agent Information using
* ::rocmtools_query_agent_info
*/
rocprofiler_agent_id_t gpu_id;
/**
* Queue Identifier to be used by the user to get the Queue Information using
* ::rocmtools_query_agent_info
*/
rocprofiler_queue_id_t queue_id;
/**
* kernel properties, including the grid size, work group size,
* registers count, wave size and completion signal
*/
rocprofiler_kernel_properties_t kernel_properties;
/**
* Thread id
*/
rocprofiler_thread_id_t thread_id;
/**
* Queue Index - packet index in the queue
*/
rocprofiler_queue_index_t queue_idx;
/**
* ATT data output from each shader engine.
*/
rocprofiler_record_se_att_data_t* shader_engine_data;
/**
* The count of the shader engine ATT data
*/
uint64_t shader_engine_data_count;
} rocprofiler_record_att_tracer_t;



/** @} */

Expand Down Expand Up @@ -1721,7 +1781,7 @@ typedef enum {
/**
* ATT Tracing. (Not Yet Supported)
*/
ROCPROFILER_ATT_TRACE = 4,
ROCPROFILER_ATT_TRACE_COLLECTION = 4,
/**
* SPM collection. (Not Yet Supported)
*/
Expand Down Expand Up @@ -1764,6 +1824,31 @@ typedef enum {
typedef const char* rocprofiler_hip_function_name_t;
typedef const char* rocprofiler_hsa_function_name_t;

// ATT tracing parameter names
typedef enum {
ROCPROFILER_ATT_COMPUTE_UNIT_TARGET = 0,
ROCPROFILER_ATT_VM_ID_MASK = 1,
ROCPROFILER_ATT_MASK = 2,
ROCPROFILER_ATT_TOKEN_MASK = 3,
ROCPROFILER_ATT_TOKEN_MASK2 = 4,
ROCPROFILER_ATT_SE_MASK = 5,
ROCPROFILER_ATT_SAMPLE_RATE = 6,
ROCPROFILER_ATT_PERF_MASK = 240,
ROCPROFILER_ATT_PERF_CTRL = 241,
ROCPROFILER_ATT_PERFCOUNTER = 242,
ROCPROFILER_ATT_PERFCOUNTER_NAME = 243,
ROCPROFILER_ATT_MAXVALUE
} rocprofiler_att_parameter_name_t;

// att tracing parameters object
typedef struct {
rocprofiler_att_parameter_name_t parameter_name;
union {
uint32_t value;
const char* counter_name;
};
} rocprofiler_att_parameter_t;

/**
* Filter Data Type
* filter data will be used to report required and optional filters for the
Expand Down Expand Up @@ -1822,6 +1907,10 @@ typedef union {
* Counters to profile
*/
const char** counters_names;
/**
* att parameters
*/
rocprofiler_att_parameter_t* att_parameters;
/**
* spm counters parameters
*/
Expand Down
1 change: 1 addition & 0 deletions plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@
add_subdirectory(file)
add_subdirectory(perfetto)
add_subdirectory(ctf)
add_subdirectory(att)
56 changes: 56 additions & 0 deletions plugin/att/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# ###############################################################################
# # Copyright (c) 2022 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.
# ###############################################################################

# Building att plugin library
file(GLOB ROCMTOOLS_UTIL_SRC_FILES ${PROJECT_SOURCE_DIR}/src/utils/helper.cpp)
file(GLOB FILE_SOURCES att.cpp)
add_library(att_plugin SHARED ${FILE_SOURCES} ${ROCMTOOLS_UTIL_SRC_FILES})

set_target_properties(att_plugin PROPERTIES
CXX_VISIBILITY_PRESET hidden
LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/../exportmap
LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})

target_compile_definitions(att_plugin
PRIVATE HIP_PROF_HIP_API_STRING=1 __HIP_PLATFORM_HCC__=1)

target_include_directories(att_plugin PRIVATE ${PROJECT_SOURCE_DIR}/inc ${PROJECT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
target_link_options(att_plugin PRIVATE -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/../exportmap -Wl,--no-undefined)
target_link_libraries(att_plugin PRIVATE ${ROCPROFILER_TARGET} systemd hsa-runtime64::hsa-runtime64 stdc++fs)

install(TARGETS att_plugin LIBRARY
DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}
COMPONENT runtime)

configure_file(att.py att/att.py COPYONLY)
configure_file(trace_view.py att/trace_view.py COPYONLY)
#configure_file(t.db att/t.db COPYONLY)
configure_file(ui/index.html att/ui/index.html COPYONLY)
configure_file(ui/logo.svg att/ui/logo.svg COPYONLY)
configure_file(ui/styles.css att/ui/styles.css COPYONLY)
#configure_file(ui/trace.json att/ui/trace.json COPYONLY)
install(DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}/att
DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/rocprofiler
USE_SOURCE_PERMISSIONS
COMPONENT runtime)

189 changes: 189 additions & 0 deletions plugin/att/att.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/* Copyright (c) 2022 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 <cxxabi.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>

#include <cassert>
#include <cstddef>
#include <cstdint>
#include <experimental/filesystem>
#include <fstream>
#include <iostream>
#include <memory>
#include <optional>
#include <ostream>
#include <sstream>
#include <string>
#include <hsa/hsa.h>
#include <mutex>
#include <sys/stat.h>

#include "rocprofiler.h"
#include "rocprofiler_plugin.h"
#include "../utils.h"

namespace {

class att_plugin_t {
public:
att_plugin_t() {}

std::mutex writing_lock;
bool is_valid_{true};

inline bool att_file_exists(const std::string& name) {
struct stat buffer;
return stat(name.c_str(), &buffer) == 0;
}

bool IsValid() const { return is_valid_; }

void FlushATTRecord(const rocprofiler_record_att_tracer_t* att_tracer_record,
rocprofiler_session_id_t session_id, rocprofiler_buffer_id_t buffer_id) {
std::lock_guard<std::mutex> lock(writing_lock);

if (!att_tracer_record) {
printf("No att data buffer received\n");
return;
}

size_t name_length;
CHECK_ROCMTOOLS(rocprofiler_query_kernel_info_size(ROCPROFILER_KERNEL_NAME,
att_tracer_record->kernel_id, &name_length));
const char* kernel_name_c = static_cast<const char*>(malloc(name_length * sizeof(char)));
CHECK_ROCMTOOLS(rocprofiler_query_kernel_info(ROCPROFILER_KERNEL_NAME,
att_tracer_record->kernel_id, &kernel_name_c));

std::string name_demangled = rocmtools::truncate_name(rocmtools::cxx_demangle(kernel_name_c));

// Get the number of shader engine traces
int se_num = att_tracer_record->shader_engine_data_count;

// Find if this filename already exists. If so, increment vname.
int file_iteration = -1;
bool bIncrementVersion = true;
while(bIncrementVersion) {
file_iteration += 1;
std::string fss = name_demangled+"_v"+std::to_string(file_iteration);
bIncrementVersion = att_file_exists(fss + "_kernel.txt");
}

std::string fname = name_demangled+"_v"+std::to_string(file_iteration)+"_kernel.txt";
std::ofstream(fname.c_str()) << name_demangled << ": " << kernel_name_c << '\n';

// iterate over each shader engine att trace
for (int i = 0; i < se_num; i++) {
if (!att_tracer_record->shader_engine_data &&
!att_tracer_record->shader_engine_data[i].buffer_ptr)
continue;
printf("--------------collecting data for shader_engine %d---------------\n", i);
rocprofiler_record_se_att_data_t* se_att_trace = &att_tracer_record->shader_engine_data[i];
uint32_t size = se_att_trace->buffer_size;
const char* data_buffer_ptr = reinterpret_cast<char*>(se_att_trace->buffer_ptr);

// dump data in binary format
std::ostringstream oss;
oss << name_demangled << "_v" << file_iteration << "_se" << i << ".att";
std::ofstream out(oss.str().c_str(), std::ios::binary);
if (out.is_open()) {
out.write((char*)data_buffer_ptr, size);
out.close();
} else {
std::cerr << "\t" << __FUNCTION__ << " Failed to open file: " << oss.str().c_str() << '\n';
}
}
}

int WriteBufferRecords(const rocprofiler_record_header_t* begin,
const rocprofiler_record_header_t* end, rocprofiler_session_id_t session_id,
rocprofiler_buffer_id_t buffer_id) {
while (begin < end) {
if (!begin) return 0;
switch (begin->kind) {
case ROCPROFILER_PROFILER_RECORD:
case ROCPROFILER_TRACER_RECORD:
case ROCPROFILER_PC_SAMPLING_RECORD:
case ROCPROFILER_SPM_RECORD:
printf("Invalid record Kind: %d", begin->kind);
break;

case ROCPROFILER_ATT_TRACER_RECORD: {
rocprofiler_record_att_tracer_t* att_record = const_cast<rocprofiler_record_att_tracer_t*>(
reinterpret_cast<const rocprofiler_record_att_tracer_t*>(begin));
FlushATTRecord(att_record, session_id, buffer_id);
break;
}
}
rocprofiler_next_record(begin, &begin, session_id, buffer_id);
}

return 0;
}

private:
};

att_plugin_t* att_plugin = nullptr;

} // namespace

ROCPROFILER_EXPORT int rocprofiler_plugin_initialize(uint32_t rocprofiler_major_version,
uint32_t rocprofiler_minor_version) {
if (rocprofiler_major_version != ROCPROFILER_VERSION_MAJOR ||
rocprofiler_minor_version < ROCPROFILER_VERSION_MINOR)
return -1;

if (att_plugin != nullptr) return -1;

att_plugin = new att_plugin_t();
if (att_plugin->IsValid()) return 0;

// The plugin failed to initialied, destroy it and return an error.
delete att_plugin;
att_plugin = nullptr;
return -1;
}

ROCPROFILER_EXPORT void rocprofiler_plugin_finalize() {
if (!att_plugin) return;
delete att_plugin;
att_plugin = nullptr;
}

ROCPROFILER_EXPORT int rocprofiler_plugin_write_buffer_records(const rocprofiler_record_header_t* begin,
const rocprofiler_record_header_t* end,
rocprofiler_session_id_t session_id,
rocprofiler_buffer_id_t buffer_id) {
if (!att_plugin || !att_plugin->IsValid()) return -1;
return att_plugin->WriteBufferRecords(begin, end, session_id, buffer_id);
}

ROCPROFILER_EXPORT int rocprofiler_plugin_write_record(rocprofiler_record_tracer_t record,
rocprofiler_session_id_t session_id) {
if (!att_plugin || !att_plugin->IsValid()) return -1;
if (record.header.id.handle == 0) return 0;
return 0;
}
Loading

0 comments on commit 03c305d

Please sign in to comment.