Add 'projects/rocprofiler/' from commit '16ae2e90c6157e98e846d2bccbaaf533ca5e662a'
git-subtree-dir: projects/rocprofiler git-subtree-mainline:2a52e3974dgit-subtree-split:16ae2e90c6
Tento commit je obsažen v:
@@ -0,0 +1,43 @@
|
||||
################################################################################
|
||||
# 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.
|
||||
################################################################################
|
||||
|
||||
if(ROCPROFILER_BUILD_CODECOV)
|
||||
set(CMAKE_BUILD_TYPE "Coverage")
|
||||
endif()
|
||||
|
||||
# these two "native" plugins are always built
|
||||
add_subdirectory(cli)
|
||||
add_subdirectory(file)
|
||||
|
||||
if(ROCPROFILER_BUILD_PLUGIN_PERFETTO)
|
||||
add_subdirectory(perfetto)
|
||||
endif()
|
||||
|
||||
if(ROCPROFILER_BUILD_PLUGIN_ATT)
|
||||
add_subdirectory(att)
|
||||
endif()
|
||||
|
||||
if(ROCPROFILER_BUILD_PLUGIN_CTF)
|
||||
add_subdirectory(ctf)
|
||||
endif()
|
||||
|
||||
add_subdirectory(json)
|
||||
@@ -0,0 +1,67 @@
|
||||
# ##############################################################################
|
||||
# # 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.
|
||||
# ##############################################################################
|
||||
|
||||
find_library(
|
||||
ROCPROFV2_ATT rocprofv2_att
|
||||
HINTS ${CMAKE_INSTALL_PREFIX}
|
||||
PATHS ${ROCM_PATH}
|
||||
PATH_SUFFIXES hsa-amd-aqlprofile)
|
||||
|
||||
set(ENV{ROCPROFV2_ATT_LIB_PATH} $ROCPROFV2_ATT)
|
||||
|
||||
# Building att plugin library
|
||||
file(GLOB ROCPROFILER_UTIL_SRC_FILES ${PROJECT_SOURCE_DIR}/src/utils/helper.cpp)
|
||||
file(GLOB FILE_SOURCES att.cpp disassembly.cpp code_printing.cpp)
|
||||
add_library(att_plugin SHARED ${FILE_SOURCES} ${ROCPROFILER_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}/lib/rocprofiler
|
||||
INSTALL_RPATH "${ROCM_APPEND_PRIVLIB_RPATH}")
|
||||
|
||||
target_compile_definitions(att_plugin PRIVATE HIP_PROF_HIP_API_STRING=1
|
||||
__HIP_PLATFORM_AMD__=1)
|
||||
|
||||
target_include_directories(att_plugin PRIVATE ${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-v2 hsa-runtime64::hsa-runtime64 stdc++fs dw elf amd_comgr)
|
||||
|
||||
install(TARGETS att_plugin LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}
|
||||
COMPONENT asan)
|
||||
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(stitch.py att/stitch.py COPYONLY)
|
||||
configure_file(drawing.py att/drawing.py COPYONLY)
|
||||
configure_file(att_to_csv.py att/att_to_csv.py COPYONLY)
|
||||
configure_file(service.py att/service.py 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/httpserver.py att/ui/httpserver.py COPYONLY)
|
||||
install(
|
||||
DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/att
|
||||
DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/rocprofiler
|
||||
COMPONENT runtime)
|
||||
@@ -0,0 +1,309 @@
|
||||
/* 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 <fstream>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <hsa/hsa.h>
|
||||
#include <mutex>
|
||||
#include <sys/stat.h>
|
||||
#include <regex>
|
||||
|
||||
#include "rocprofiler.h"
|
||||
#include "rocprofiler_plugin.h"
|
||||
#include "../utils.h"
|
||||
#include "../../src/core/session/att/att_header.h"
|
||||
|
||||
#include "src/utils/filesystem.hpp"
|
||||
|
||||
#define ATT_FILENAME_MAXBYTES 90
|
||||
#define TEST_INVALID_KERNEL size_t(-1)
|
||||
|
||||
static bool env_var_search(std::string& s) {
|
||||
std::smatch m;
|
||||
std::regex e("(.*)\\%\\q\\{([^}]+)\\}(.*)");
|
||||
std::regex_match(s, m, e);
|
||||
|
||||
if (m.size() != 4) return false;
|
||||
|
||||
while (m.size() == 4) {
|
||||
const char* envvar = getenv(m[2].str().c_str());
|
||||
if (!envvar) envvar = "";
|
||||
s = m[1].str() + envvar + m[3].str();
|
||||
std::regex_match(s, m, e);
|
||||
};
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
class att_plugin_t {
|
||||
public:
|
||||
att_plugin_t(void* data) {
|
||||
std::vector<const char*> mpivars = {"MPI_RANK", "OMPI_COMM_WORLD_RANK", "MV2_COMM_WORLD_RANK"};
|
||||
|
||||
for (const char* envvar : mpivars)
|
||||
if (const char* env = getenv(envvar)) {
|
||||
MPI_RANK = atoi(env);
|
||||
MPI_ENABLE = true;
|
||||
break;
|
||||
}
|
||||
|
||||
header.raw = reinterpret_cast<uint64_t>(data);
|
||||
header.reserved = 0x11;
|
||||
}
|
||||
|
||||
bool MPI_ENABLE = false;
|
||||
int MPI_RANK = 0;
|
||||
static std::mutex writing_lock;
|
||||
bool is_valid_{true};
|
||||
rocprofiler::att_header_packet_t header{.raw = 0};
|
||||
std::string output_dir = ".";
|
||||
|
||||
bool CheckAddrMatches(uint64_t kernel_addr, uint64_t base_address, uint64_t size)
|
||||
{
|
||||
return (kernel_addr >= base_address) && (kernel_addr < base_address + size);
|
||||
}
|
||||
|
||||
void InitOutputDir()
|
||||
{
|
||||
static bool bIsInit = false;
|
||||
if (bIsInit) return;
|
||||
bIsInit = true;
|
||||
|
||||
if (const char* env = getenv("OUTPUT_PATH")) output_dir = std::string(env);
|
||||
env_var_search(output_dir);
|
||||
|
||||
if (!output_dir.size()) return;
|
||||
|
||||
try {
|
||||
rocprofiler::common::filesystem::create_directories(output_dir);
|
||||
} catch (...) {}
|
||||
output_dir += '/';
|
||||
}
|
||||
|
||||
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_; }
|
||||
|
||||
int FlushATTRecord(const rocprofiler_record_att_tracer_t* att_tracer_record,
|
||||
rocprofiler_session_id_t session_id, rocprofiler_buffer_id_t buffer_id) {
|
||||
|
||||
if (!att_tracer_record) return ROCPROFILER_STATUS_ERROR;
|
||||
InitOutputDir();
|
||||
|
||||
std::string kernel_name_mangled;
|
||||
// Found problem with rocprofiler API for invalid kernel_ids;
|
||||
if (att_tracer_record->kernel_id.handle != TEST_INVALID_KERNEL) {
|
||||
size_t name_length;
|
||||
CHECK_ROCPROFILER(rocprofiler_query_kernel_info_size(
|
||||
ROCPROFILER_KERNEL_NAME, att_tracer_record->kernel_id, &name_length));
|
||||
const char* kernel_name_c = nullptr;
|
||||
CHECK_ROCPROFILER(rocprofiler_query_kernel_info(
|
||||
ROCPROFILER_KERNEL_NAME, att_tracer_record->kernel_id, &kernel_name_c));
|
||||
|
||||
assert(kernel_name_c && "Rocprofv2 returned an invalid kernel name");
|
||||
|
||||
kernel_name_mangled = std::string(kernel_name_c);
|
||||
free(const_cast<char*>(kernel_name_c));
|
||||
} else { // Temporary. Adding a valid string.
|
||||
kernel_name_mangled = "test_kernel";
|
||||
}
|
||||
|
||||
|
||||
std::string name_demangled =
|
||||
rocprofiler::truncate_name(rocprofiler::cxx_demangle(kernel_name_mangled));
|
||||
|
||||
if (name_demangled.size() > ATT_FILENAME_MAXBYTES) // Limit filename size
|
||||
name_demangled = name_demangled.substr(0, ATT_FILENAME_MAXBYTES);
|
||||
|
||||
std::string outfilepath = output_dir + '/' + name_demangled;
|
||||
outfilepath.reserve(output_dir.size() + 128); // Max filename size
|
||||
if (MPI_ENABLE) outfilepath += "_rank" + std::to_string(MPI_RANK);
|
||||
outfilepath += "_v";
|
||||
|
||||
// Find if this filename already exists. If so, increment vname.
|
||||
int file_iteration = 0;
|
||||
while (att_file_exists(outfilepath + std::to_string(file_iteration) + "_kernel.txt"))
|
||||
file_iteration += 1;
|
||||
|
||||
outfilepath += std::to_string(file_iteration);
|
||||
auto writer_id = att_tracer_record->writer_id;
|
||||
|
||||
std::string fname = outfilepath + "_kernel.txt";
|
||||
std::ofstream kernel_txt_file((outfilepath + "_kernel.txt").c_str());
|
||||
kernel_txt_file << name_demangled << " dispatch[" << writer_id << "] GPU["
|
||||
<< att_tracer_record->gpu_id.handle << "]: " << kernel_name_mangled
|
||||
<< '\n';
|
||||
|
||||
// iterate over each shader engine att trace
|
||||
header.navi = !(att_tracer_record->intercept_list.userdata & 0x1);
|
||||
int se_num = att_tracer_record->shader_engine_data_count;
|
||||
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);
|
||||
header.SEID = i;
|
||||
rocprofiler_record_se_att_data_t* se_att_trace = &att_tracer_record->shader_engine_data[i];
|
||||
char* data_buffer_ptr = reinterpret_cast<char*>(se_att_trace->buffer_ptr);
|
||||
|
||||
// dump data in binary format
|
||||
std::ofstream out(outfilepath + "_se" + std::to_string(i) + ".att", std::ios::binary);
|
||||
if (!out.is_open()) {
|
||||
std::cerr << "ATT Failed to open file: " << outfilepath << "_se" << i << ".att\n";
|
||||
return ROCPROFILER_STATUS_ERROR;
|
||||
}
|
||||
if (header.enable && !header.navi)
|
||||
out.write((const char*)&header, sizeof(header.raw));
|
||||
out.write(data_buffer_ptr, se_att_trace->buffer_size);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < att_tracer_record->intercept_list.count; i++)
|
||||
{
|
||||
const auto& symbol = att_tracer_record->intercept_list.symbols[i];
|
||||
if (!symbol.filepath) continue;
|
||||
|
||||
std::string sfilepath(symbol.filepath);
|
||||
bool bCopiedData = symbol.data && symbol.data_size;
|
||||
|
||||
if (bCopiedData)
|
||||
{
|
||||
auto pos = sfilepath.find("://");
|
||||
auto rpos = sfilepath.rfind('/');
|
||||
|
||||
if (pos == std::string::npos || pos+3 >= sfilepath.size()) continue;
|
||||
|
||||
std::string type(sfilepath.begin(), sfilepath.begin()+pos);
|
||||
std::string cut(sfilepath.begin()+rpos+1, sfilepath.end());
|
||||
sfilepath = type + cut + ".out";
|
||||
}
|
||||
|
||||
kernel_txt_file << std::hex << "0x" << symbol.base_address << " 0x" << symbol.mem_size
|
||||
<< ' ' << std::dec << symbol.att_marker_id << ' ' << sfilepath << '\n';
|
||||
|
||||
sfilepath = output_dir + '/' + sfilepath;
|
||||
if (!bCopiedData || att_file_exists(sfilepath)) continue;
|
||||
|
||||
std::ofstream isafile(sfilepath, std::ios::binary);
|
||||
if (!isafile.is_open()) {
|
||||
std::cerr << "Could not open file: " << sfilepath << std::endl;
|
||||
return ROCPROFILER_STATUS_ERROR;
|
||||
}
|
||||
|
||||
isafile.write(symbol.data, symbol.data_size);
|
||||
}
|
||||
|
||||
return ROCPROFILER_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
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 ROCPROFILER_STATUS_ERROR;
|
||||
switch (begin->kind) {
|
||||
case ROCPROFILER_PROFILER_RECORD:
|
||||
case ROCPROFILER_TRACER_RECORD:
|
||||
case ROCPROFILER_PC_SAMPLING_RECORD:
|
||||
case ROCPROFILER_SPM_RECORD:
|
||||
case ROCPROFILER_COUNTERS_SAMPLER_RECORD:
|
||||
rocprofiler::warning("Invalid record Kind: %d\n", 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;
|
||||
}
|
||||
}
|
||||
int status = rocprofiler_next_record(begin, &begin, session_id, buffer_id);
|
||||
if (status != ROCPROFILER_STATUS_SUCCESS) return status;
|
||||
}
|
||||
|
||||
return ROCPROFILER_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
att_plugin_t* att_plugin = nullptr;
|
||||
std::mutex att_plugin_t::writing_lock;
|
||||
|
||||
ROCPROFILER_EXPORT int rocprofiler_plugin_initialize(uint32_t rocprofiler_major_version,
|
||||
uint32_t rocprofiler_minor_version,
|
||||
void* data) {
|
||||
if (rocprofiler_major_version != ROCPROFILER_VERSION_MAJOR ||
|
||||
rocprofiler_minor_version < ROCPROFILER_VERSION_MINOR)
|
||||
return ROCPROFILER_STATUS_ERROR;
|
||||
|
||||
std::lock_guard<std::mutex> lock(att_plugin_t::writing_lock);
|
||||
if (att_plugin != nullptr) return ROCPROFILER_STATUS_ERROR;
|
||||
|
||||
att_plugin = new att_plugin_t(data);
|
||||
if (att_plugin->IsValid()) return ROCPROFILER_STATUS_SUCCESS;
|
||||
|
||||
// The plugin failed to initialied, destroy it and return an error.
|
||||
delete att_plugin;
|
||||
att_plugin = nullptr;
|
||||
return ROCPROFILER_STATUS_ERROR;
|
||||
}
|
||||
|
||||
ROCPROFILER_EXPORT void rocprofiler_plugin_finalize() {
|
||||
std::lock_guard<std::mutex> lock(att_plugin_t::writing_lock);
|
||||
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) {
|
||||
std::lock_guard<std::mutex> lock(att_plugin_t::writing_lock);
|
||||
if (!att_plugin || !att_plugin->IsValid()) return ROCPROFILER_STATUS_ERROR;
|
||||
return att_plugin->WriteBufferRecords(begin, end, session_id, buffer_id);
|
||||
}
|
||||
|
||||
ROCPROFILER_EXPORT int rocprofiler_plugin_write_record(rocprofiler_record_tracer_t record) {
|
||||
std::lock_guard<std::mutex> lock(att_plugin_t::writing_lock);
|
||||
if (!att_plugin || !att_plugin->IsValid()) return ROCPROFILER_STATUS_ERROR;
|
||||
if (record.header.id.handle == 0) return ROCPROFILER_STATUS_SUCCESS;
|
||||
return ROCPROFILER_STATUS_SUCCESS;
|
||||
}
|
||||
Spustitelný soubor
+641
@@ -0,0 +1,641 @@
|
||||
#!/usr/bin/env python3
|
||||
import sys
|
||||
|
||||
if sys.version_info[0] < 3:
|
||||
raise Exception("Must be using Python 3")
|
||||
|
||||
import os
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
from ctypes import *
|
||||
import ctypes
|
||||
from copy import deepcopy
|
||||
from trace_view import view_trace
|
||||
import sys
|
||||
import glob
|
||||
import numpy as np
|
||||
import stitch
|
||||
import gc
|
||||
from collections import defaultdict
|
||||
import service
|
||||
|
||||
ATT_VERSION = 5
|
||||
|
||||
class pcInfo(ctypes.Structure):
|
||||
_fields_ = [
|
||||
("addr", c_uint64),
|
||||
("marker_id", c_uint64)
|
||||
]
|
||||
def to_v2_pc(self):
|
||||
if self.marker_id == 0:
|
||||
return self.addr
|
||||
return self.addr | (self.marker_id<<service.ID_OFFSET) | (1<<service.HEADER_OFFSET)
|
||||
|
||||
class TraceData(ctypes.Structure):
|
||||
_fields_ = [
|
||||
("type", c_uint64, 8),
|
||||
("hitcount", c_uint64, 56),
|
||||
("latency", c_uint64),
|
||||
("pc", pcInfo)
|
||||
]
|
||||
|
||||
class TraceDataTranslated:
|
||||
def __init__(self, inst):
|
||||
self.type = inst.type
|
||||
self.num_waves = inst.hitcount
|
||||
self.cycles = inst.latency
|
||||
if self.type == stitch.PCINFO:
|
||||
self.cycles = inst.pc.to_v2_pc()
|
||||
|
||||
class Trace:
|
||||
def __init__(self, traceid, tracesize, instructions_array):
|
||||
self.instructions = [TraceDataTranslated(instructions_array[k]) for k in range(tracesize)]
|
||||
self.traceid = traceid
|
||||
|
||||
class PerfEvent(ctypes.Structure):
|
||||
_fields_ = [
|
||||
("time", c_uint64),
|
||||
("event0", c_uint16),
|
||||
("event1", c_uint16),
|
||||
("event2", c_uint16),
|
||||
("event3", c_uint16),
|
||||
("cu", c_uint8),
|
||||
("bank", c_uint8),
|
||||
]
|
||||
|
||||
def toTuple(self):
|
||||
return (
|
||||
int(self.time),
|
||||
int(self.event0),
|
||||
int(self.event1),
|
||||
int(self.event2),
|
||||
int(self.event3),
|
||||
int(self.cu),
|
||||
int(self.bank),
|
||||
)
|
||||
|
||||
|
||||
class CodeWrapped(ctypes.Structure):
|
||||
""" Matches CodeWrapped on the python side """
|
||||
_fields_ = [('line', ctypes.c_char_p),
|
||||
('loc', ctypes.c_char_p),
|
||||
('to_line', ctypes.c_int),
|
||||
('value', ctypes.c_int),
|
||||
('index', ctypes.c_int),
|
||||
('line_num', ctypes.c_int),
|
||||
('addr', ctypes.c_int64)]
|
||||
|
||||
|
||||
class KvPair(ctypes.Structure):
|
||||
""" Matches pair<int, int> = (key, value) on the python side """
|
||||
_fields_ = [('key', ctypes.c_int),
|
||||
('value', ctypes.c_int)]
|
||||
|
||||
|
||||
class ReturnAssemblyInfo(ctypes.Structure):
|
||||
""" Matches ReturnAssemblyInfo on the python side """
|
||||
_fields_ = [('code', POINTER(CodeWrapped)),
|
||||
('jumps', POINTER(KvPair)),
|
||||
('code_len', ctypes.c_int),
|
||||
('jumps_len', ctypes.c_int)]
|
||||
|
||||
|
||||
class WaveState(ctypes.Structure):
|
||||
_fields_ = [
|
||||
("type", c_int32),
|
||||
("duration", c_int32)
|
||||
]
|
||||
|
||||
class WaveInstruction(ctypes.Structure):
|
||||
_fields_ = [
|
||||
("time", c_int64),
|
||||
("duration", c_int64)
|
||||
]
|
||||
|
||||
class Wave(ctypes.Structure):
|
||||
_fields_ = [
|
||||
("simd", ctypes.c_uint8),
|
||||
("wave_id", ctypes.c_uint8),
|
||||
("trap_status", ctypes.c_uint8),
|
||||
("reserved", ctypes.c_uint8),
|
||||
|
||||
# total VMEM/FLAT/LDS/SMEM instructions issued
|
||||
# VMEM Pipeline: instrs and stalls
|
||||
("num_vmem_instrs", ctypes.c_int),
|
||||
("num_vmem_stalls", ctypes.c_int),
|
||||
# FLAT instrs and stalls
|
||||
("num_flat_instrs", ctypes.c_int),
|
||||
("num_flat_stalls", ctypes.c_int),
|
||||
# LDS instr and stalls
|
||||
("num_lds_instrs", ctypes.c_int),
|
||||
("num_lds_stalls", ctypes.c_int),
|
||||
# SCA instrs stalls
|
||||
("num_salu_instrs", ctypes.c_int),
|
||||
("num_smem_instrs", ctypes.c_int),
|
||||
("num_salu_stalls", ctypes.c_int),
|
||||
("num_smem_stalls", ctypes.c_int),
|
||||
# Branch
|
||||
("num_branch_instrs", ctypes.c_int),
|
||||
("num_branch_taken_instrs", ctypes.c_int),
|
||||
("num_branch_stalls", ctypes.c_int),
|
||||
|
||||
# total issued memory instructions
|
||||
("num_mem_instrs", ctypes.c_int),
|
||||
# total valus insts and stalls
|
||||
("num_valu_stalls", ctypes.c_int),
|
||||
("num_valu_instrs", ctypes.c_size_t),
|
||||
# total issued instructions (compute + memory)
|
||||
("num_issued_instrs", ctypes.c_size_t),
|
||||
# Begin and end cycle
|
||||
("begin_time", ctypes.c_int64),
|
||||
("end_time", ctypes.c_int64),
|
||||
("traceid", ctypes.c_int64),
|
||||
|
||||
("timeline_size", ctypes.c_size_t),
|
||||
("instructions_size", ctypes.c_size_t),
|
||||
("timeline_array", POINTER(WaveState)),
|
||||
("instructions_array", POINTER(WaveInstruction)),
|
||||
]
|
||||
|
||||
|
||||
class PythonWave:
|
||||
def __init__(self, sourcew):
|
||||
for property, value in Wave._fields_:
|
||||
try:
|
||||
setattr(self, deepcopy(property), deepcopy(getattr(sourcew, property)))
|
||||
except:
|
||||
pass
|
||||
|
||||
self.timeline = [
|
||||
(int(sourcew.timeline_array[k].type), int(sourcew.timeline_array[k].duration))
|
||||
for k in range(self.timeline_size)
|
||||
]
|
||||
self.timeline_array = None
|
||||
|
||||
self.instructions = [
|
||||
(int(sourcew.instructions_array[k].time), int(sourcew.instructions_array[k].duration))
|
||||
for k in range(self.instructions_size)
|
||||
]
|
||||
self.instructions_array = None
|
||||
|
||||
|
||||
class ReturnInfo(ctypes.Structure):
|
||||
_fields_ = [
|
||||
("flags", ctypes.c_uint64),
|
||||
("binaryID", ctypes.c_uint64),
|
||||
("num_traces", ctypes.c_uint64),
|
||||
("tracesizes", POINTER(ctypes.c_uint64)),
|
||||
("traceIDs", POINTER(ctypes.c_int64)),
|
||||
("tracedata", POINTER(POINTER(TraceData))),
|
||||
|
||||
("num_events", ctypes.c_uint64),
|
||||
("perfevents", POINTER(PerfEvent)),
|
||||
("occupancy", POINTER(ctypes.c_uint64)),
|
||||
("num_occupancy", ctypes.c_uint64),
|
||||
("kernel_id_addr", POINTER(pcInfo)),
|
||||
("num_kernel_ids", ctypes.c_uint64),
|
||||
|
||||
("wavedata", POINTER(Wave)),
|
||||
("num_waves", ctypes.c_uint64),
|
||||
]
|
||||
|
||||
|
||||
rocprofv2_att_lib = os.getenv("ROCPROFV2_ATT_LIB_PATH")
|
||||
if rocprofv2_att_lib is None:
|
||||
print(
|
||||
"ATT Lib path not set. Use export ROCPROFV2_ATT_LIB_PATH=/path/to/librocprofv2_att.so"
|
||||
)
|
||||
quit()
|
||||
path_to_parser = os.path.abspath(rocprofv2_att_lib)
|
||||
SO = CDLL(path_to_parser)
|
||||
|
||||
SO.AnalyseBinary.restype = ReturnInfo
|
||||
SO.AnalyseBinary.argtypes = [ctypes.c_char_p]
|
||||
SO.wrapped_parse_binary.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
|
||||
SO.wrapped_parse_binary.restype = ReturnAssemblyInfo
|
||||
SO.FreeBinary.argtypes = [ctypes.c_uint64]
|
||||
|
||||
def parse_binary(filename, kernel=None):
|
||||
if kernel is None or kernel == "":
|
||||
kernel = ctypes.c_char_p(0)
|
||||
else:
|
||||
with open(glob.glob(kernel)[0], "r") as file:
|
||||
kernel = file.readlines()
|
||||
kernel = kernel[0].split(": ")[1].split(".kd")[0]
|
||||
kernel = str(kernel).encode("utf-8")
|
||||
filename = os.path.abspath(str(filename))
|
||||
info = SO.wrapped_parse_binary(str(filename).encode("utf-8"), kernel)
|
||||
|
||||
code = []
|
||||
for k in range(info.code_len):
|
||||
code_entry = info.code[k]
|
||||
|
||||
line = deepcopy(code_entry.line.decode("utf-8"))
|
||||
loc = deepcopy(code_entry.loc.decode("utf-8"))
|
||||
|
||||
to_line = int(code_entry.to_line) if (code_entry.to_line >= 0) else None
|
||||
loc = loc if len(loc) > 0 else None
|
||||
|
||||
# asm, inst_type, addr, loc, index, line_num, hitcount, cycles
|
||||
code.append([line, int(code_entry.value), to_line, loc, int(code_entry.index),
|
||||
int(code_entry.line_num), int(code_entry.addr), 0, 0])
|
||||
|
||||
jumps = {}
|
||||
for k in range(info.jumps_len):
|
||||
jumps[info.jumps[k].key] = info.jumps[k].value
|
||||
|
||||
return code, jumps
|
||||
|
||||
|
||||
def getWaves_binary(name):
|
||||
filename = os.path.abspath(str(name))
|
||||
info = SO.AnalyseBinary(filename.encode("utf-8"))
|
||||
|
||||
isValid = info.flags & 0x1
|
||||
if isValid == 0:
|
||||
print('Invalid trace ', name)
|
||||
return ([], [], [], [], None, [])
|
||||
flags = "navi" if (info.flags & 0x2) else "vega"
|
||||
kernel_addr = [info.kernel_id_addr[k].to_v2_pc() for k in range(info.num_kernel_ids)]
|
||||
events = [deepcopy(info.perfevents[k]) for k in range(info.num_events)]
|
||||
occupancy = [int(info.occupancy[k]) for k in range(int(info.num_occupancy))]
|
||||
|
||||
assert(((info.flags >> 3) & 0x1FFF == ATT_VERSION)) # Check ATT parser version
|
||||
|
||||
traces_python = {}
|
||||
for T in range(info.num_traces):
|
||||
if info.tracesizes[T] > 2:
|
||||
id = info.traceIDs[T]
|
||||
traces_python[id] = Trace(id, int(info.tracesizes[T]), info.tracedata[T])
|
||||
|
||||
waves_python = []
|
||||
for k in range(info.num_waves):
|
||||
if info.wavedata[k].instructions_size > 2:
|
||||
waves_python.append(PythonWave(info.wavedata[k]))
|
||||
|
||||
SO.FreeBinary(info.binaryID)
|
||||
|
||||
return (traces_python, waves_python, events, occupancy, flags, kernel_addr, info.flags & 0x4)
|
||||
|
||||
|
||||
def getWaves_stitch(traces, code, jumps, flags, latency_map, hitcount_map, bIsAuto, codeobjservice):
|
||||
for id in traces.keys():
|
||||
traces[id].instructions = stitch.stitch(traces[id].instructions, code, jumps, flags, bIsAuto, codeobjservice)
|
||||
if len(code) > hitcount_map.size:
|
||||
hitcount_map = np.pad(hitcount_map, [0,len(code)-hitcount_map.size])
|
||||
latency_map = np.pad(latency_map, [0,len(code)-latency_map.size])
|
||||
if traces[id].instructions is not None:
|
||||
for inst in traces[id].instructions[0]:
|
||||
hitcount_map[inst.asmline] += inst.num_waves
|
||||
latency_map[inst.asmline] += inst.cycles
|
||||
return hitcount_map, latency_map
|
||||
|
||||
|
||||
def persist(trace_file, SIMD, traces):
|
||||
trace = Path(trace_file).name
|
||||
simds, waves = [], []
|
||||
begin_time, end_time, timeline, instructions, trace_ids = [], [], [], [], []
|
||||
mem_ins, issued_ins, valu_ins, valu_stalls = [], [], [], []
|
||||
vmem_ins, vmem_stalls, flat_ins, flat_stalls = [], [], [], []
|
||||
lds_ins, lds_stalls, salu_ins, salu_stalls = [], [], [], []
|
||||
smem_ins, smem_stalls, br_ins, br_taken_ins, br_stalls = [], [], [], [], []
|
||||
|
||||
for wave in SIMD:
|
||||
try:
|
||||
if wave.instructions is None or traces[wave.traceid].instructions is None:
|
||||
continue
|
||||
except:
|
||||
continue
|
||||
simds.append(wave.simd)
|
||||
waves.append(wave.wave_id)
|
||||
begin_time.append(wave.begin_time)
|
||||
end_time.append(wave.end_time)
|
||||
trace_ids.append(wave.traceid)
|
||||
mem_ins.append(wave.num_mem_instrs)
|
||||
issued_ins.append(wave.num_issued_instrs)
|
||||
valu_ins.append(wave.num_valu_instrs)
|
||||
valu_stalls.append(wave.num_valu_stalls)
|
||||
vmem_ins.append(wave.num_vmem_instrs)
|
||||
vmem_stalls.append(wave.num_vmem_stalls)
|
||||
flat_ins.append(wave.num_flat_instrs)
|
||||
flat_stalls.append(wave.num_flat_stalls)
|
||||
lds_ins.append(wave.num_lds_instrs)
|
||||
lds_stalls.append(wave.num_lds_stalls)
|
||||
salu_ins.append(wave.num_salu_instrs)
|
||||
salu_stalls.append(wave.num_salu_stalls)
|
||||
smem_ins.append(wave.num_smem_instrs)
|
||||
smem_stalls.append(wave.num_smem_stalls)
|
||||
br_ins.append(wave.num_branch_instrs)
|
||||
br_taken_ins.append(wave.num_branch_taken_instrs)
|
||||
br_stalls.append(wave.num_branch_stalls)
|
||||
timeline.append(wave.timeline)
|
||||
|
||||
cc = 0
|
||||
insts = []
|
||||
skips = traces[wave.traceid].instructions[-1]
|
||||
try:
|
||||
for v in traces[wave.traceid].instructions[0]:
|
||||
while cc in skips:
|
||||
cc += 1
|
||||
t = wave.instructions[cc]
|
||||
insts.append((t[0], v.type, 0, t[1], v.asmline))
|
||||
cc += 1
|
||||
except:
|
||||
pass # Incomplete waves
|
||||
instructions.append((insts,) + traces[wave.traceid].instructions[1:-1])
|
||||
|
||||
df = {
|
||||
"name": [trace for _ in range(len(begin_time))],
|
||||
"id": [i for i in range(len(begin_time))],
|
||||
"simd": simds,
|
||||
"wave_slot": waves,
|
||||
"begin_time": begin_time,
|
||||
"end_time": end_time,
|
||||
"mem_ins": mem_ins,
|
||||
"issued_ins": issued_ins,
|
||||
"valu_ins": valu_ins,
|
||||
"valu_stalls": valu_stalls,
|
||||
"vmem_ins": vmem_ins,
|
||||
"vmem_stalls": vmem_stalls,
|
||||
"flat_ins": flat_ins,
|
||||
"flat_stalls": flat_stalls,
|
||||
"lds_ins": lds_ins,
|
||||
"lds_stalls": lds_stalls,
|
||||
"salu_ins": salu_ins,
|
||||
"salu_stalls": salu_stalls,
|
||||
"smem_ins": smem_ins,
|
||||
"smem_stalls": smem_stalls,
|
||||
"br_ins": br_ins,
|
||||
"br_taken_ins": br_taken_ins,
|
||||
"br_stalls": br_stalls,
|
||||
"timeline": timeline,
|
||||
"instructions": instructions,
|
||||
"traceids": trace_ids,
|
||||
}
|
||||
return df
|
||||
|
||||
|
||||
def mem_max(array):
|
||||
mem_dict = {}
|
||||
for SE in array:
|
||||
for wave in SE:
|
||||
for inst in wave:
|
||||
try:
|
||||
mem_dict[inst[0]][0] = max(mem_dict[inst[0]][0], inst[1])
|
||||
except:
|
||||
mem_dict[inst[0]] = inst[1:]
|
||||
assert mem_dict[inst[0]][1] == inst[2]
|
||||
|
||||
return mem_dict
|
||||
|
||||
|
||||
def lgk(count):
|
||||
return "lgkmcnt({0})".format(count)
|
||||
|
||||
|
||||
def vmc(count):
|
||||
return "vmcnt({0})".format(count)
|
||||
|
||||
|
||||
def both_cnt(count):
|
||||
return lgk(count) + " " + vmc(count)
|
||||
|
||||
|
||||
def insert_waitcnt(flight_count, assembly_code):
|
||||
flight_count = mem_max(flight_count)
|
||||
for key in sorted(flight_count):
|
||||
line_n = key
|
||||
(
|
||||
issue_amount,
|
||||
waitcnt_amount,
|
||||
) = flight_count[key]
|
||||
if "vmcnt" in assembly_code[line_n] and "lgkmcnt" in assembly_code[line_n]:
|
||||
counter_type = both_cnt
|
||||
elif "vmcnt" in assembly_code[line_n]:
|
||||
counter_type = vmc
|
||||
elif "lgkmcnt" in assembly_code[line_n]:
|
||||
counter_type = lgk
|
||||
else:
|
||||
print("Error: Line mismatch")
|
||||
exit(-1)
|
||||
|
||||
for count in range(waitcnt_amount + 1, issue_amount):
|
||||
print("Inserted line: " + str(line_n))
|
||||
as_index = line_n - count / (issue_amount + 1)
|
||||
assembly_code[as_index] = "\ts_waitcnt {0}\t\t; Timing analysis.".format(
|
||||
counter_type(count)
|
||||
)
|
||||
as_index += 0.5 / (issue_amount + 1)
|
||||
assembly_code[as_index] = "\ts_nop 0\t\t\t\t\t\t; Counters: " + str(
|
||||
issue_amount
|
||||
)
|
||||
|
||||
return assembly_code
|
||||
|
||||
|
||||
def gen_timelines(DBFILES):
|
||||
TIMELINES = [np.zeros(int(1E6), dtype=np.float32) for k in range(5)]
|
||||
TIME_RESOLUTION = 16
|
||||
for df in DBFILES:
|
||||
for T in range(len(df["timeline"])):
|
||||
timeline = df["timeline"][T]
|
||||
time_acc = 0
|
||||
tuples3 = [(0, df["begin_time"][T])] + [(int(t[0]), int(t[1])) for t in timeline]
|
||||
|
||||
for state in tuples3:
|
||||
t_end = (time_acc + state[1])//TIME_RESOLUTION
|
||||
if t_end > 1E8:
|
||||
print("Warning: Time limit reached for ", state[0], state[1])
|
||||
break
|
||||
elif t_end > TIMELINES[state[0]].size:
|
||||
TIMELINES[state[0]] = np.hstack(
|
||||
[TIMELINES[state[0]], np.zeros_like(TIMELINES[state[0]])]
|
||||
)
|
||||
TIMELINES[state[0]][time_acc//TIME_RESOLUTION : t_end] += 1
|
||||
time_acc += state[1]
|
||||
return TIMELINES
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pathenv = os.getenv("OUTPUT_PATH")
|
||||
if pathenv is None:
|
||||
pathenv = "."
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
"assembly_code", help="Path to the assembly code. Must be the first parameter."
|
||||
)
|
||||
parser.add_argument(
|
||||
"--trace_file", help="Filter for trace files", default=None, type=str
|
||||
)
|
||||
parser.add_argument(
|
||||
"--att_kernel", help="Kernel file", type=str, default=os.path.join(pathenv, "*_kernel.txt")
|
||||
)
|
||||
parser.add_argument("--ports", help="Server and websocket ports, default: 8000,18000")
|
||||
parser.add_argument(
|
||||
"--mode",
|
||||
help="""ATT analysis modes:\n
|
||||
off: Only run ATT collection, disable analysis.\n
|
||||
file: dump json files to disk.""",
|
||||
type=str,
|
||||
default="off",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
args.mode = args.mode.lower().split(',')
|
||||
|
||||
CSV_MODE = False
|
||||
FILE_MODE = False
|
||||
if 'csv' in args.mode:
|
||||
CSV_MODE = True
|
||||
if 'file' in args.mode:
|
||||
FILE_MODE = True
|
||||
|
||||
if not CSV_MODE and not FILE_MODE:
|
||||
print("Skipping analysis.")
|
||||
quit()
|
||||
|
||||
if os.getenv("COUNTERS_PATH"):
|
||||
with open(os.getenv("COUNTERS_PATH"), "r") as f:
|
||||
lines = [l.split("//")[0] for l in f.readlines()]
|
||||
|
||||
EVENT_NAMES = []
|
||||
clean = lambda x: x.split("=")[1].split(" ")[0].split("\n")[0]
|
||||
for line in lines:
|
||||
if "PERFCOUNTER_ID=" in line:
|
||||
EVENT_NAMES += ["id: " + clean(line)]
|
||||
for line in lines:
|
||||
if "PERFCOUNTER=" in line:
|
||||
EVENT_NAMES += [clean(line).split("SQ_")[1].lower()]
|
||||
|
||||
att_kernel_list = glob.glob(args.att_kernel)
|
||||
|
||||
if len(att_kernel_list) == 0:
|
||||
print("Could not find att output kernel:", args.att_kernel)
|
||||
quit()
|
||||
|
||||
for att_kernel in att_kernel_list:
|
||||
print('Parsing:', att_kernel)
|
||||
att_kernel_f = []
|
||||
with open(att_kernel, 'r') as f:
|
||||
for line in f:
|
||||
att_kernel_f.append(line.split('\n')[0])
|
||||
|
||||
# returns element index of the file:// or memory:// filepath in the string
|
||||
get_path_loc = lambda x: np.sum([len(m) for m in x.split(' ')[:3]])+3
|
||||
# adds the OUTPUT_PATH env variable to a filepath if necessary
|
||||
add_pathnv = lambda x: x[:get_path_loc(x)] + os.path.join(pathenv, x[get_path_loc(x):])
|
||||
# returns the memory address in the string
|
||||
get_addr = lambda x: int(x.split(' ')[0][2:], 16)
|
||||
# Sets a preference for 'file' paths to be added before 'memory' paths. Sorts by addr.
|
||||
get_addr_preference = lambda x: [0 if 'file' in x[get_path_loc(x):] else 1<<60][0]
|
||||
|
||||
# Get the GPU id in the string
|
||||
gpu_id = int(att_kernel_f[0].split(' ')[2].split('GPU[')[1].split(']')[0])
|
||||
# Eliminame first line in the att_kernel txt file and adds the OUTPUT_PATH as needed
|
||||
att_kernel_f = [add_pathnv(p) if '.out' == p[-4:] else p for p in att_kernel_f[1:]]
|
||||
# Sorts the list of codeobj by address, with 'file' given preference
|
||||
att_kernel_f = sorted(att_kernel_f, key=lambda x: get_addr(x)+get_addr_preference(x))
|
||||
assembly_code = deepcopy(args.assembly_code)
|
||||
|
||||
# Assembly parsing
|
||||
bIsAuto = False
|
||||
if assembly_code.lower().strip() == 'auto':
|
||||
bIsAuto = True
|
||||
code = [['; Begin ATT ASM', 100, 0, '', 0, 0, 0, 0, 0]]
|
||||
jumps = []
|
||||
elif not Path(assembly_code).is_file():
|
||||
print("Invalid assembly_code('{0}')!".format(assembly_code))
|
||||
sys.exit(1)
|
||||
else:
|
||||
code, jumps = parse_binary(assembly_code, att_kernel)
|
||||
|
||||
# Trace Parsing
|
||||
trace_instance_name = att_kernel.split("_kernel.txt")[0]
|
||||
if args.trace_file is None:
|
||||
filenames = glob.glob(trace_instance_name + "_*.att")
|
||||
else:
|
||||
filenames = glob.glob(args.trace_file)
|
||||
|
||||
if len(filenames) == 0:
|
||||
print("Could not find trace files for", att_kernel)
|
||||
continue
|
||||
|
||||
print('Att kernel:', att_kernel)
|
||||
|
||||
DBFILES = []
|
||||
EVENTS = []
|
||||
OCCUPANCY = []
|
||||
GFXV = []
|
||||
analysed_filenames = []
|
||||
occupancy_filenames = []
|
||||
kernel_addr = {}
|
||||
|
||||
latency_map = np.zeros((len(code)), dtype=np.int64)
|
||||
hitcount_map = np.zeros((len(code)), dtype=np.int32)
|
||||
|
||||
gc.collect()
|
||||
if bIsAuto:
|
||||
codeservice = service.CodeobjService(gpu_id, att_kernel_f, SO.classify_asm_line)
|
||||
else:
|
||||
codeservice = None
|
||||
|
||||
for name in filenames:
|
||||
traces, waves, perfevents, occupancy, gfxv, addrs, ftrace = getWaves_binary(name)
|
||||
if gfxv is None:
|
||||
continue
|
||||
if CSV_MODE == False and ftrace == 0:
|
||||
print('Generating occupancy information')
|
||||
|
||||
for id, addr in enumerate(addrs):
|
||||
kernel_addr[id] = addr
|
||||
if len(occupancy) > 1:
|
||||
OCCUPANCY.append( occupancy )
|
||||
occupancy_filenames.append(name)
|
||||
|
||||
if np.sum([0]+[len(s.instructions) for id, s in traces.items()]) == 0:
|
||||
print("No traces from", name)
|
||||
continue
|
||||
|
||||
hitcount_map, latency_map = getWaves_stitch(traces, code, jumps, gfxv, latency_map, hitcount_map, bIsAuto, codeservice)
|
||||
|
||||
analysed_filenames.append(name)
|
||||
EVENTS.append(perfevents)
|
||||
DBFILES.append( persist(name, waves, traces) )
|
||||
GFXV.append(gfxv)
|
||||
|
||||
gc.collect()
|
||||
for k in range(len(code)):
|
||||
code[k][-2] = int(hitcount_map[k])
|
||||
code[k][-1] = int(latency_map[k])
|
||||
try:
|
||||
code[k][-3] = codeservice.ToRawPC(code[k][-3])
|
||||
except:
|
||||
pass
|
||||
if k > 0 and code[k-1][-3] == 0:
|
||||
code[k-1][-3] = code[k][-3]
|
||||
|
||||
if CSV_MODE:
|
||||
from att_to_csv import dump_csv
|
||||
dump_csv(code, trace_instance_name, bIsAuto)
|
||||
|
||||
if FILE_MODE:
|
||||
try:
|
||||
dispatchNames = {id: codeservice.getSymbolName(addr) for id, addr in kernel_addr.items()}
|
||||
except:
|
||||
dispatchNames = {id: "#addr"+str(addr) for id, addr in kernel_addr.items()}
|
||||
drawinfo = {
|
||||
"TIMELINES": gen_timelines(DBFILES),
|
||||
"EVENTS": EVENTS,
|
||||
"EVENT_NAMES": EVENT_NAMES,
|
||||
"OCCUPANCY": OCCUPANCY,
|
||||
"ShaderNames": occupancy_filenames,
|
||||
"DispatchNames": dispatchNames
|
||||
}
|
||||
view_trace(
|
||||
code,
|
||||
DBFILES,
|
||||
analysed_filenames,
|
||||
0,
|
||||
gfxv,
|
||||
drawinfo,
|
||||
trace_instance_name
|
||||
)
|
||||
Spustitelný soubor
+24
@@ -0,0 +1,24 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import numpy as np
|
||||
import csv
|
||||
import os
|
||||
|
||||
def dump_csv(code, trace_instance_name, bIsAuto):
|
||||
outpath = os.getenv("OUT_FILE_NAME")
|
||||
if outpath is None:
|
||||
outpath = "att_output"
|
||||
elif os.path.dirname(outpath) != '':
|
||||
os.makedirs(os.path.dirname(outpath), exist_ok=True)
|
||||
|
||||
outpath += '_' + os.path.basename(trace_instance_name) + '.csv'
|
||||
print('Generating', outpath)
|
||||
|
||||
with open(outpath, 'w') as f:
|
||||
writer = csv.writer(f)
|
||||
if bIsAuto:
|
||||
writer.writerow(['Addr', 'Instruction', 'Hitcount', 'Cycles', 'C++ Reference'])
|
||||
[writer.writerow([hex(m[6]), m[0], m[7], m[8], m[3]]) for m in code]
|
||||
else:
|
||||
writer.writerow(['Line', 'Instruction', 'Hitcount', 'Cycles', 'C++ Reference'])
|
||||
[writer.writerow([m[5], m[0], m[7], m[8], m[3]]) for m in code]
|
||||
@@ -0,0 +1,383 @@
|
||||
/* 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 "code_printing.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <hsa/amd_hsa_elf.h>
|
||||
#include "../utils.h"
|
||||
#include <cxxabi.h>
|
||||
#include <elfutils/libdw.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
#include <atomic>
|
||||
|
||||
#define C_API_BEGIN try {
|
||||
|
||||
#define C_API_END(returndata) \
|
||||
} catch (std::exception& e) \
|
||||
{ \
|
||||
std::string s = e.what(); \
|
||||
if (s.find("memory protocol not supported!") == std::string::npos) \
|
||||
std::cerr << "Codeobj API lookup: " << e.what() << std::endl; \
|
||||
return returndata; \
|
||||
} \
|
||||
catch (std::string& s) \
|
||||
{ \
|
||||
if (s.find("memory protocol not supported!") == std::string::npos) \
|
||||
std::cerr << "Codeobj API lookup: " << s << std::endl; \
|
||||
return returndata; \
|
||||
} \
|
||||
catch (...) \
|
||||
{ \
|
||||
return returndata; \
|
||||
}
|
||||
|
||||
CodeObjDecoderComponent::CodeObjDecoderComponent(
|
||||
const char* codeobj_data,
|
||||
uint64_t codeobj_size,
|
||||
uint64_t gpu_id
|
||||
) {
|
||||
if (
|
||||
codeobj_size <= 4 ||
|
||||
codeobj_data[0] != ELFMAG0 ||
|
||||
codeobj_data[1] != ELFMAG1 ||
|
||||
codeobj_data[2] != ELFMAG2 ||
|
||||
codeobj_data[3] != ELFMAG3
|
||||
)
|
||||
throw std::invalid_argument("Invalid ELF file");
|
||||
|
||||
m_fd = -1;
|
||||
#if defined(_GNU_SOURCE) && defined(MFD_ALLOW_SEALING) && defined(MFD_CLOEXEC)
|
||||
m_fd = ::memfd_create(m_uri.c_str(), MFD_ALLOW_SEALING | MFD_CLOEXEC);
|
||||
#endif
|
||||
if (m_fd == -1) // If fail, attempt under /tmp
|
||||
m_fd = ::open("/tmp", O_TMPFILE | O_RDWR, 0666);
|
||||
|
||||
if (m_fd == -1) {
|
||||
printf("could not create a temporary file for code object\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (size_t size = ::write(m_fd, codeobj_data, codeobj_size); size != codeobj_size) {
|
||||
printf("could not write to the temporary file\n");
|
||||
return;
|
||||
}
|
||||
::lseek(m_fd, 0, SEEK_SET);
|
||||
fsync(m_fd);
|
||||
|
||||
m_line_number_map = {};
|
||||
|
||||
std::unique_ptr<Dwarf, void (*)(Dwarf*)> dbg(dwarf_begin(m_fd, DWARF_C_READ),
|
||||
[](Dwarf* dbg) { dwarf_end(dbg); });
|
||||
|
||||
/*if (!dbg) {
|
||||
rocprofiler::warning("Error opening Dwarf!\n");
|
||||
return;
|
||||
} */
|
||||
|
||||
if (dbg) {
|
||||
Dwarf_Off cu_offset{0}, next_offset;
|
||||
size_t header_size;
|
||||
|
||||
std::map<uint64_t, std::string> used_addrs;
|
||||
|
||||
while (!dwarf_nextcu(dbg.get(), cu_offset, &next_offset, &header_size, nullptr, nullptr,
|
||||
nullptr)) {
|
||||
Dwarf_Die die;
|
||||
if (!dwarf_offdie(dbg.get(), cu_offset + header_size, &die)) continue;
|
||||
|
||||
Dwarf_Lines* lines;
|
||||
size_t line_count;
|
||||
if (dwarf_getsrclines(&die, &lines, &line_count)) continue;
|
||||
|
||||
for (size_t i = 0; i < line_count; ++i) {
|
||||
Dwarf_Addr addr;
|
||||
int line_number;
|
||||
Dwarf_Line* line = dwarf_onesrcline(lines, i);
|
||||
|
||||
if (line && !dwarf_lineaddr(line, &addr) && !dwarf_lineno(line, &line_number) && line_number)
|
||||
{
|
||||
std::string src = dwarf_linesrc(line, nullptr, nullptr);
|
||||
auto dwarf_line = src + ':' + std::to_string(line_number);
|
||||
|
||||
if (used_addrs.find(addr) != used_addrs.end())
|
||||
{
|
||||
used_addrs.at(addr) += ' ' + dwarf_line;
|
||||
continue;
|
||||
}
|
||||
|
||||
used_addrs.emplace(addr, std::move(dwarf_line));
|
||||
}
|
||||
}
|
||||
cu_offset = next_offset;
|
||||
}
|
||||
|
||||
auto it = used_addrs.begin();
|
||||
if(it != used_addrs.end())
|
||||
{
|
||||
while(std::next(it) != used_addrs.end())
|
||||
{
|
||||
uint64_t delta = std::next(it)->first - it->first;
|
||||
auto segment = address_range_t{it->first, delta, 0};
|
||||
m_line_number_map.emplace(segment, std::move(it->second));
|
||||
it++;
|
||||
}
|
||||
auto segment = address_range_t{it->first, codeobj_size - it->first, 0};
|
||||
m_line_number_map.emplace(segment, std::move(it->second));
|
||||
}
|
||||
}
|
||||
|
||||
// Can throw
|
||||
disassembly = std::make_unique<DisassemblyInstance>(codeobj_data, codeobj_size, gpu_id);
|
||||
try {
|
||||
m_symbol_map = disassembly->GetKernelMap(); // Can throw
|
||||
} catch(...) {}
|
||||
|
||||
//disassemble_kernels();
|
||||
}
|
||||
|
||||
CodeObjDecoderComponent::~CodeObjDecoderComponent() {
|
||||
if (m_fd) ::close(m_fd);
|
||||
}
|
||||
|
||||
std::optional<SymbolInfo> CodeObjDecoderComponent::find_symbol(uint64_t vaddr) {
|
||||
/* Load the symbol table. */
|
||||
auto it = m_symbol_map.upper_bound(vaddr);
|
||||
if (it == m_symbol_map.begin())
|
||||
return std::nullopt;
|
||||
|
||||
auto&& [symbol_vaddr, symbol] = *std::prev(it);
|
||||
if (vaddr >= symbol_vaddr + symbol.mem_size)
|
||||
return std::nullopt;
|
||||
|
||||
std::string symbol_name = symbol.name;
|
||||
|
||||
int status = 0;
|
||||
auto* demangled_name = abi::__cxa_demangle(symbol_name.c_str(), nullptr, nullptr, &status);
|
||||
if (status == 0 && demangled_name)
|
||||
{
|
||||
symbol_name = demangled_name;
|
||||
free(demangled_name);
|
||||
}
|
||||
return SymbolInfo{symbol_name, symbol.faddr, symbol.mem_size};
|
||||
}
|
||||
|
||||
std::pair<instruction_instance_t, size_t>
|
||||
CodeObjDecoderComponent::disassemble_instruction(uint64_t faddr, uint64_t vaddr)
|
||||
{
|
||||
if (!disassembly)
|
||||
throw std::exception();
|
||||
|
||||
const char* cpp_line = nullptr;
|
||||
|
||||
auto it = m_line_number_map.find({vaddr, 0, 0});
|
||||
if(it != m_line_number_map.end()) cpp_line = it->second.data();
|
||||
|
||||
size_t size = disassembly->ReadInstruction(faddr, vaddr, cpp_line);
|
||||
return {disassembly->last_instruction, size};
|
||||
}
|
||||
|
||||
void CodeObjDecoderComponent::disassemble_kernel(uint64_t faddr, uint64_t vaddr)
|
||||
{
|
||||
if (!disassembly) return;
|
||||
auto symbol = find_symbol(vaddr);
|
||||
|
||||
if (!symbol)
|
||||
{
|
||||
std::cerr << "No symbol found at address 0x" << std::hex << faddr << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
std::cout << "Dumping ISA for " << symbol->name << std::endl;
|
||||
|
||||
uint64_t end_addr = faddr + symbol->mem_size;
|
||||
while (faddr < end_addr)
|
||||
{
|
||||
size_t size;
|
||||
instruction_instance_t inst;
|
||||
std::tie(inst, size) = this->disassemble_instruction(faddr, vaddr);
|
||||
instructions.push_back(inst);
|
||||
faddr += size;
|
||||
vaddr += size;
|
||||
}
|
||||
}
|
||||
|
||||
void CodeObjDecoderComponent::disassemble_kernels() {
|
||||
for (auto& [vaddr, v] : m_symbol_map) disassemble_kernel(v.faddr, vaddr);
|
||||
}
|
||||
|
||||
void CodeObjDecoderComponent::disassemble_single_kernel(uint64_t kaddr) {
|
||||
for (auto& [vaddr, v] : m_symbol_map)
|
||||
if (kaddr >= vaddr && kaddr < vaddr + v.mem_size)
|
||||
disassemble_kernel(v.faddr, vaddr);
|
||||
}
|
||||
|
||||
CodeobjDecoder::CodeobjDecoder(
|
||||
const char* filepath,
|
||||
uint64_t loadbase,
|
||||
uint64_t mem_size,
|
||||
uint64_t gpu_id
|
||||
): loadbase(loadbase), load_end(loadbase + mem_size)
|
||||
{
|
||||
if (!filepath) throw "Empty filepath.";
|
||||
|
||||
std::string_view fpath(filepath);
|
||||
|
||||
if (fpath.rfind(".out") + 4 == fpath.size())
|
||||
{
|
||||
std::ifstream file(filepath, std::ios::in | std::ios::binary);
|
||||
|
||||
if (!file.is_open())
|
||||
throw "Invalid filename " + std::string(filepath);
|
||||
|
||||
std::vector<char> buffer;
|
||||
file.seekg(0, file.end);
|
||||
buffer.resize(file.tellg());
|
||||
file.seekg(0, file.beg);
|
||||
file.read(buffer.data(), buffer.size());
|
||||
|
||||
decoder = std::make_unique<CodeObjDecoderComponent>(buffer.data(), buffer.size(), gpu_id);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::unique_ptr<CodeObjectBinary> binary = std::make_unique<CodeObjectBinary>(filepath);
|
||||
auto& buffer = binary->buffer;
|
||||
decoder = std::make_unique<CodeObjDecoderComponent>(buffer.data(), buffer.size(), gpu_id);
|
||||
}
|
||||
|
||||
auto elf_segments = decoder->disassembly->getSegments();
|
||||
}
|
||||
|
||||
bool CodeobjDecoder::add_to_map(uint64_t faddr, uint64_t vaddr, uint64_t voffset)
|
||||
{
|
||||
try
|
||||
{
|
||||
decoded_map[vaddr] = decoder->disassemble_instruction(faddr, voffset);
|
||||
}
|
||||
catch(std::exception& e)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CodeobjDecoder::decode_single_at_offset(uint64_t vaddr, uint64_t voffset)
|
||||
{
|
||||
auto faddr = decoder->disassembly->va2fo(voffset);
|
||||
if (!faddr)
|
||||
return false;
|
||||
|
||||
return add_to_map(*faddr, vaddr, voffset);
|
||||
}
|
||||
|
||||
bool CodeobjDecoder::decode_single(uint64_t vaddr)
|
||||
{
|
||||
if (!decoder || vaddr < loadbase) return false;
|
||||
return decode_single_at_offset(vaddr, vaddr-loadbase);
|
||||
}
|
||||
|
||||
std::pair<instruction_instance_t, size_t>& CodeobjDecoder::getDecoded(uint64_t addr)
|
||||
{
|
||||
if (decoded_map.find(addr) != decoded_map.end())
|
||||
return decoded_map[addr];
|
||||
|
||||
if (!decode_single(addr))
|
||||
{
|
||||
std::cerr << "Invalid addr: " << std::hex << addr << std::dec << std::endl;
|
||||
throw std::exception();
|
||||
}
|
||||
|
||||
return decoded_map[addr];
|
||||
}
|
||||
|
||||
#define PUBLIC_API __attribute__((visibility("default")))
|
||||
|
||||
CodeobjTableTranslation table;
|
||||
|
||||
extern "C"
|
||||
{
|
||||
PUBLIC_API int addDecoder(
|
||||
const char* filename,
|
||||
uint32_t id,
|
||||
uint64_t loadbase,
|
||||
uint64_t memsize,
|
||||
uint64_t gpu_id
|
||||
) {
|
||||
C_API_BEGIN
|
||||
|
||||
table.addDecoder(filename, id, loadbase, memsize, gpu_id);
|
||||
return 0;
|
||||
|
||||
C_API_END(1)
|
||||
}
|
||||
PUBLIC_API int removeDecoder(uint32_t id, uint64_t loadbase)
|
||||
{
|
||||
return table.removeDecoder(id, loadbase) != false;
|
||||
}
|
||||
PUBLIC_API instruction_info_t getInstructionFromAddr(uint64_t vaddr)
|
||||
{
|
||||
static instruction_info_t default_info{nullptr, nullptr, 0};
|
||||
C_API_BEGIN
|
||||
|
||||
return table.get(vaddr);
|
||||
|
||||
C_API_END(default_info)
|
||||
}
|
||||
PUBLIC_API instruction_info_t getInstructionFromID(uint32_t id, uint64_t offset)
|
||||
{
|
||||
static instruction_info_t default_info{nullptr, nullptr, 0};
|
||||
C_API_BEGIN
|
||||
|
||||
return table.get(id, offset);
|
||||
|
||||
C_API_END(default_info)
|
||||
}
|
||||
PUBLIC_API const char* getSymbolName(uint64_t addr)
|
||||
{
|
||||
C_API_BEGIN
|
||||
|
||||
return table.getSymbolName(addr);
|
||||
|
||||
C_API_END(nullptr)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
/* 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. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "rocprofiler.h"
|
||||
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "disassembly.hpp"
|
||||
#include "segment.hpp"
|
||||
|
||||
struct DSourceLine
|
||||
{
|
||||
uint64_t vaddr;
|
||||
uint64_t size;
|
||||
std::string str;
|
||||
uint64_t begin() const { return vaddr; }
|
||||
bool inrange(uint64_t addr) const { return addr >= vaddr && addr < vaddr+size; }
|
||||
};
|
||||
|
||||
class CodeObjDecoderComponent
|
||||
{
|
||||
public:
|
||||
std::optional<SymbolInfo> find_symbol(uint64_t address);
|
||||
|
||||
CodeObjDecoderComponent(const char* codeobj_data, uint64_t codeobj_size, uint64_t gpu_id);
|
||||
~CodeObjDecoderComponent();
|
||||
|
||||
std::pair<instruction_instance_t, size_t>
|
||||
disassemble_instruction(uint64_t faddr, uint64_t vaddr);
|
||||
void disassemble_kernel(uint64_t faddr, uint64_t vaddr);
|
||||
void disassemble_single_kernel(uint64_t kaddr);
|
||||
void disassemble_kernels();
|
||||
|
||||
int m_fd;
|
||||
|
||||
std::map<address_range_t, std::string> m_line_number_map{};
|
||||
std::map<uint64_t, SymbolInfo> m_symbol_map{};
|
||||
|
||||
std::string m_uri;
|
||||
std::vector<instruction_instance_t> instructions{};
|
||||
std::unique_ptr<DisassemblyInstance> disassembly{};
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
const char* inst;
|
||||
const char* cpp;
|
||||
size_t size;
|
||||
} instruction_info_t;
|
||||
|
||||
class CodeobjDecoder
|
||||
{
|
||||
public:
|
||||
CodeobjDecoder(const char* filepath, uint64_t loadbase, uint64_t memsize, uint64_t gpu_id);
|
||||
|
||||
bool decode_single(uint64_t vaddr);
|
||||
bool decode_single_at_offset(uint64_t vaddr, uint64_t voffset);
|
||||
bool add_to_map(uint64_t faddr, uint64_t vaddr, uint64_t voffset);
|
||||
|
||||
std::pair<instruction_instance_t, size_t>& getDecoded(uint64_t addr);
|
||||
const char* getInstruction(uint64_t addr) { return getDecoded(addr).first.instruction; }
|
||||
const char* getCppref(uint64_t addr) { return getDecoded(addr).first.cpp_reference; }
|
||||
size_t getSize(uint64_t addr) { return getDecoded(addr).second; }
|
||||
instruction_info_t get(uint64_t addr) {
|
||||
auto& inst = getDecoded(addr);
|
||||
return {inst.first.instruction, inst.first.cpp_reference, inst.second};
|
||||
}
|
||||
|
||||
uint64_t begin() const { return loadbase; };
|
||||
uint64_t end() const { return load_end; }
|
||||
uint64_t size() const { return load_end-loadbase; }
|
||||
bool inrange(uint64_t addr) const { return addr >= begin() && addr < end(); }
|
||||
|
||||
const char* getSymbolName(uint64_t addr) const {
|
||||
if (!decoder) return nullptr;
|
||||
|
||||
auto it = decoder->m_symbol_map.find(addr-loadbase);
|
||||
if (it != decoder->m_symbol_map.end())
|
||||
return it->second.name.data();
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
std::vector<std::pair<uint64_t, uint64_t>> elf_segments{};
|
||||
|
||||
private:
|
||||
const uint64_t loadbase;
|
||||
uint64_t load_end = 0;
|
||||
|
||||
std::unordered_map<uint64_t, std::pair<instruction_instance_t, size_t>> decoded_map;
|
||||
std::unique_ptr<CodeObjDecoderComponent> decoder{nullptr};
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Maps ID and offsets into instructions
|
||||
*/
|
||||
class CodeobjList
|
||||
{
|
||||
public:
|
||||
CodeobjList() = default;
|
||||
|
||||
virtual void addDecoder(
|
||||
const char* filepath,
|
||||
uint32_t id,
|
||||
uint64_t loadbase,
|
||||
uint64_t memsize,
|
||||
uint64_t gpu_id
|
||||
)
|
||||
{
|
||||
decoders[id] = std::make_shared<CodeobjDecoder>(filepath, loadbase, memsize, gpu_id);
|
||||
}
|
||||
|
||||
virtual bool removeDecoder(uint32_t id)
|
||||
{
|
||||
return decoders.erase(id) != 0;
|
||||
}
|
||||
|
||||
instruction_info_t get(uint32_t id, uint64_t offset)
|
||||
{
|
||||
auto& decoder = decoders.at(id);
|
||||
auto& inst = decoder->getDecoded(decoder->begin() + offset);
|
||||
return {inst.first.instruction, inst.first.cpp_reference, inst.second};
|
||||
}
|
||||
|
||||
const char* getSymbolName(uint32_t id, uint64_t offset)
|
||||
{
|
||||
auto& decoder = decoders.at(id);
|
||||
uint64_t vaddr = decoder->begin() + offset;
|
||||
if (decoder->inrange(vaddr))
|
||||
return decoder->getSymbolName(vaddr);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
protected:
|
||||
std::unordered_map<uint32_t, std::shared_ptr<CodeobjDecoder>> decoders{};
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Translates virtual addresses to elf file offsets
|
||||
*/
|
||||
class CodeobjTableTranslation : protected CodeobjList
|
||||
{
|
||||
using Super = CodeobjList;
|
||||
public:
|
||||
CodeobjTableTranslation() = default;
|
||||
|
||||
virtual void addDecoder(
|
||||
const char* filepath,
|
||||
uint32_t id,
|
||||
uint64_t loadbase,
|
||||
uint64_t memsize,
|
||||
uint64_t gpu_id
|
||||
) override
|
||||
{
|
||||
this->Super::addDecoder(filepath, id, loadbase, memsize, gpu_id);
|
||||
auto ptr = decoders.at(id);
|
||||
table.insert(address_range_t{ptr->begin(), static_cast<uint32_t>(ptr->size()), id});
|
||||
}
|
||||
|
||||
virtual bool removeDecoder(uint32_t id, uint64_t loadbase)
|
||||
{
|
||||
return table.remove(loadbase) && this->Super::removeDecoder(id);
|
||||
}
|
||||
|
||||
instruction_info_t get(uint64_t vaddr)
|
||||
{
|
||||
auto addr_range = table.find_codeobj_in_range(vaddr);
|
||||
return get(addr_range.id, vaddr - addr_range.addr);
|
||||
}
|
||||
instruction_info_t get(uint32_t id, uint64_t offset) { return this->Super::get(id, offset); }
|
||||
|
||||
const char* getSymbolName(uint64_t vaddr)
|
||||
{
|
||||
for (auto& [_, decoder] : decoders)
|
||||
{
|
||||
if (!decoder->inrange(vaddr)) continue;
|
||||
return decoder->getSymbolName(vaddr);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
private:
|
||||
CodeobjTableTranslator table;
|
||||
};
|
||||
@@ -0,0 +1,386 @@
|
||||
/* 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. */
|
||||
|
||||
#if !defined(_GNU_SOURCE) || !defined(_XOPEN_SOURCE)
|
||||
#define _XOPEN_SOURCE 700
|
||||
#endif
|
||||
|
||||
#include <sys/mman.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <elf.h>
|
||||
#include <cxxabi.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
#include <elfutils/libdw.h>
|
||||
#include "../utils.h"
|
||||
#include "code_printing.hpp"
|
||||
#include <hsa/amd_hsa_elf.h>
|
||||
|
||||
#define THROW_COMGR(call) \
|
||||
if (amd_comgr_status_s status = call) { \
|
||||
const char* reason = ""; \
|
||||
amd_comgr_status_string(status, &reason); \
|
||||
std::cerr << __FILE__ << ':' << __LINE__ << " code: " \
|
||||
<< status << " failed: " << reason << std::endl; \
|
||||
throw std::exception(); \
|
||||
}
|
||||
|
||||
#define RETURN_COMGR(call) \
|
||||
if (amd_comgr_status_s status = call) { \
|
||||
const char* reason = ""; \
|
||||
amd_comgr_status_string(status, &reason); \
|
||||
std::cerr << __FILE__ << ':' << __LINE__ << " code: " \
|
||||
<< status << " failed: " << reason << std::endl; \
|
||||
return AMD_COMGR_STATUS_ERROR; \
|
||||
}
|
||||
|
||||
std::unordered_map<uint64_t, std::string> DisassemblyInstance::agent_isa_name{};
|
||||
|
||||
CodeObjectBinary::CodeObjectBinary(const std::string& uri) : m_uri(uri) {
|
||||
const std::string protocol_delim{"://"};
|
||||
|
||||
size_t protocol_end = m_uri.find(protocol_delim);
|
||||
std::string protocol = m_uri.substr(0, protocol_end);
|
||||
protocol_end += protocol_delim.length();
|
||||
|
||||
std::transform(protocol.begin(), protocol.end(), protocol.begin(),
|
||||
[](unsigned char c) { return std::tolower(c); });
|
||||
|
||||
std::string path;
|
||||
size_t path_end = m_uri.find_first_of("#?", protocol_end);
|
||||
if (path_end != std::string::npos) {
|
||||
path = m_uri.substr(protocol_end, path_end++ - protocol_end);
|
||||
} else {
|
||||
path = m_uri.substr(protocol_end);
|
||||
}
|
||||
|
||||
/* %-decode the string. */
|
||||
std::string decoded_path;
|
||||
decoded_path.reserve(path.length());
|
||||
for (size_t i = 0; i < path.length(); ++i)
|
||||
{
|
||||
if (path[i] == '%' && std::isxdigit(path[i + 1]) && std::isxdigit(path[i + 2])) {
|
||||
decoded_path += std::stoi(path.substr(i + 1, 2), 0, 16);
|
||||
i += 2;
|
||||
} else {
|
||||
decoded_path += path[i];
|
||||
}
|
||||
}
|
||||
|
||||
/* Tokenize the query/fragment. */
|
||||
std::vector<std::string> tokens;
|
||||
size_t pos, last = path_end;
|
||||
while ((pos = m_uri.find('&', last)) != std::string::npos) {
|
||||
tokens.emplace_back(m_uri.substr(last, pos - last));
|
||||
last = pos + 1;
|
||||
}
|
||||
if (last != std::string::npos) {
|
||||
tokens.emplace_back(m_uri.substr(last));
|
||||
}
|
||||
|
||||
/* Create a tag-value map from the tokenized query/fragment. */
|
||||
std::unordered_map<std::string, std::string> params;
|
||||
std::for_each(tokens.begin(), tokens.end(), [&](std::string& token) {
|
||||
size_t delim = token.find('=');
|
||||
if (delim != std::string::npos) {
|
||||
params.emplace(token.substr(0, delim), token.substr(delim + 1));
|
||||
}
|
||||
});
|
||||
|
||||
buffer = std::vector<char>{};
|
||||
size_t offset{0}, size{0};
|
||||
|
||||
if (auto offset_it = params.find("offset"); offset_it != params.end()) {
|
||||
offset = std::stoul(offset_it->second, nullptr, 0);
|
||||
}
|
||||
|
||||
if (auto size_it = params.find("size"); size_it != params.end()) {
|
||||
if (!(size = std::stoul(size_it->second, nullptr, 0))) return;
|
||||
}
|
||||
|
||||
if (protocol != "file") throw protocol + " protocol not supported!";
|
||||
|
||||
std::ifstream file(decoded_path, std::ios::in | std::ios::binary);
|
||||
if (!file || !file.is_open()) throw "could not open " + decoded_path;
|
||||
|
||||
if (!size) {
|
||||
file.ignore(std::numeric_limits<std::streamsize>::max());
|
||||
size_t bytes = file.gcount();
|
||||
file.clear();
|
||||
|
||||
if (bytes < offset)
|
||||
throw "invalid uri " + decoded_path + " (file size < offset)";
|
||||
|
||||
size = bytes - offset;
|
||||
}
|
||||
|
||||
file.seekg(offset, std::ios_base::beg);
|
||||
buffer.resize(size);
|
||||
file.read(&buffer[0], size);
|
||||
}
|
||||
|
||||
DisassemblyInstance::DisassemblyInstance(
|
||||
const char* codeobj_data,
|
||||
uint64_t codeobj_size,
|
||||
uint64_t gpu_id
|
||||
)
|
||||
{
|
||||
if (
|
||||
codeobj_size <= 4 ||
|
||||
codeobj_data[0] != ELFMAG0 ||
|
||||
codeobj_data[1] != ELFMAG1 ||
|
||||
codeobj_data[2] != ELFMAG2 ||
|
||||
codeobj_data[3] != ELFMAG3
|
||||
)
|
||||
throw std::invalid_argument("Invalid ELF file");
|
||||
|
||||
buffer = std::vector<char>(codeobj_size, 0);
|
||||
std::memcpy(buffer.data(), codeobj_data, codeobj_size);
|
||||
|
||||
THROW_COMGR(amd_comgr_create_data(AMD_COMGR_DATA_KIND_EXECUTABLE, &data));
|
||||
THROW_COMGR(amd_comgr_set_data(data, buffer.size(), buffer.data()));
|
||||
|
||||
std::string input_isa{};
|
||||
if (agent_isa_name.find(gpu_id) == agent_isa_name.end())
|
||||
{
|
||||
size_t isa_size = 128;
|
||||
input_isa.resize(isa_size);
|
||||
THROW_COMGR(amd_comgr_get_data_isa_name(data, &isa_size, input_isa.data()));
|
||||
agent_isa_name[gpu_id] = input_isa;
|
||||
}
|
||||
else
|
||||
{
|
||||
input_isa = agent_isa_name.at(gpu_id);
|
||||
}
|
||||
|
||||
THROW_COMGR(amd_comgr_create_disassembly_info(
|
||||
input_isa.data(),
|
||||
&DisassemblyInstance::memory_callback, &DisassemblyInstance::inst_callback,
|
||||
[](uint64_t address, void* user_data) {}, &info));
|
||||
|
||||
}
|
||||
|
||||
static bool IsKernelType(amd_comgr_symbol_type_t type)
|
||||
{
|
||||
if (type == AMD_COMGR_SYMBOL_TYPE_FUNC)
|
||||
return true;
|
||||
#ifdef AMD_COMGR_SYMBOL_TYPE_AMDGPU_HSA_KERNEL // To be deprecated
|
||||
if (type == AMD_COMGR_SYMBOL_TYPE_AMDGPU_HSA_KERNEL)
|
||||
return true;
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
amd_comgr_status_t DisassemblyInstance::symbol_callback(amd_comgr_symbol_t symbol,
|
||||
void* user_data) {
|
||||
amd_comgr_symbol_type_t type;
|
||||
RETURN_COMGR(amd_comgr_symbol_get_info(symbol, AMD_COMGR_SYMBOL_INFO_TYPE, &type));
|
||||
|
||||
if (!IsKernelType(type))
|
||||
return AMD_COMGR_STATUS_SUCCESS;
|
||||
|
||||
uint64_t vaddr;
|
||||
uint64_t mem_size;
|
||||
uint64_t name_size;
|
||||
RETURN_COMGR(amd_comgr_symbol_get_info(symbol, AMD_COMGR_SYMBOL_INFO_VALUE, &vaddr));
|
||||
RETURN_COMGR(amd_comgr_symbol_get_info(symbol, AMD_COMGR_SYMBOL_INFO_SIZE, &mem_size));
|
||||
RETURN_COMGR(amd_comgr_symbol_get_info(symbol, AMD_COMGR_SYMBOL_INFO_NAME_LENGTH, &name_size));
|
||||
|
||||
std::string name;
|
||||
name.resize(name_size);
|
||||
|
||||
RETURN_COMGR(amd_comgr_symbol_get_info(symbol, AMD_COMGR_SYMBOL_INFO_NAME, name.data()));
|
||||
|
||||
DisassemblyInstance& instance = *static_cast<DisassemblyInstance*>(user_data);
|
||||
std::optional<uint64_t> faddr = instance.va2fo(vaddr);
|
||||
|
||||
if (faddr)
|
||||
instance.symbol_map[vaddr] = {name, *faddr, mem_size};
|
||||
return AMD_COMGR_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
std::map<uint64_t, SymbolInfo>& DisassemblyInstance::GetKernelMap() {
|
||||
symbol_map = {};
|
||||
THROW_COMGR(amd_comgr_iterate_symbols(data, &DisassemblyInstance::symbol_callback, this));
|
||||
|
||||
return symbol_map;
|
||||
}
|
||||
|
||||
DisassemblyInstance::~DisassemblyInstance() {
|
||||
amd_comgr_release_data(data);
|
||||
amd_comgr_destroy_disassembly_info(info);
|
||||
}
|
||||
|
||||
uint64_t DisassemblyInstance::ReadInstruction(uint64_t faddr, uint64_t vaddr, const char* cpp_line)
|
||||
{
|
||||
uint64_t size_read;
|
||||
uint64_t addr_in_buffer = reinterpret_cast<uint64_t>(buffer.data()) + faddr;
|
||||
|
||||
THROW_COMGR(amd_comgr_disassemble_instruction(info, addr_in_buffer, (void*)this, &size_read));
|
||||
|
||||
last_instruction.address = vaddr;
|
||||
last_instruction.cpp_reference = cpp_line;
|
||||
return size_read;
|
||||
}
|
||||
|
||||
uint64_t DisassemblyInstance::memory_callback(uint64_t from, char* to, uint64_t size,
|
||||
void* user_data) {
|
||||
DisassemblyInstance& instance = *static_cast<DisassemblyInstance*>(user_data);
|
||||
int64_t copysize = reinterpret_cast<int64_t>(instance.buffer.data())
|
||||
+ instance.buffer.size() - static_cast<int64_t>(from);
|
||||
copysize = std::min<int64_t>(size, copysize);
|
||||
std::memcpy(to, (char*)from, copysize);
|
||||
return copysize;
|
||||
}
|
||||
|
||||
void DisassemblyInstance::inst_callback(const char* instruction, void* user_data) {
|
||||
DisassemblyInstance& instance = *static_cast<DisassemblyInstance*>(user_data);
|
||||
instance.last_instruction.instruction = strdup(instruction);
|
||||
}
|
||||
|
||||
#define CHECK_VA2FO(x, msg) if (!(x)) { \
|
||||
std::cerr << __FILE__ << ' ' << __LINE__ << ' ' << msg << std::endl; \
|
||||
return std::nullopt; \
|
||||
}
|
||||
|
||||
// mem - input argument, start of the elf
|
||||
// va - input argument, virtual address
|
||||
// return file offset, if found
|
||||
std::optional<uint64_t> DisassemblyInstance::va2fo(uint64_t va)
|
||||
{
|
||||
CHECK_VA2FO(buffer.size(), "buffer is not large enough");
|
||||
|
||||
uint8_t *e_ident = (uint8_t*)buffer.data();
|
||||
CHECK_VA2FO(e_ident, "e_ident is nullptr");
|
||||
|
||||
CHECK_VA2FO(
|
||||
e_ident[EI_MAG0] == ELFMAG0 ||
|
||||
e_ident[EI_MAG1] == ELFMAG1 ||
|
||||
e_ident[EI_MAG2] == ELFMAG2 ||
|
||||
e_ident[EI_MAG3] == ELFMAG3, "unexpected ei_mag");
|
||||
|
||||
CHECK_VA2FO(e_ident[EI_CLASS] == ELFCLASS64, "unexpected ei_class");
|
||||
CHECK_VA2FO(e_ident[EI_DATA] == ELFDATA2LSB, "unexpected ei_data");
|
||||
CHECK_VA2FO(e_ident[EI_VERSION] == EV_CURRENT, "unexpected ei_version");
|
||||
CHECK_VA2FO(e_ident[EI_OSABI] == 64, "unexpected ei_osabi"); // ELFOSABI_AMDGPU_HSA
|
||||
|
||||
CHECK_VA2FO(
|
||||
e_ident[EI_ABIVERSION] == 2 || // ELFABIVERSION_AMDGPU_HSA_V4
|
||||
e_ident[EI_ABIVERSION] == 3 || // ELFABIVERSION_AMDGPU_HSA_V5
|
||||
e_ident[EI_ABIVERSION] == 4, "unexpected ei_abiversion"); // ELFABIVERSION_AMDGPU_HSA_V6
|
||||
|
||||
Elf64_Ehdr *ehdr = (Elf64_Ehdr*)buffer.data();
|
||||
CHECK_VA2FO(ehdr, "ehdr is nullptr");
|
||||
CHECK_VA2FO(ehdr->e_type == ET_DYN, "unexpected e_type");
|
||||
CHECK_VA2FO(ehdr->e_machine == ELF::EM_AMDGPU, "unexpected e_machine");
|
||||
|
||||
CHECK_VA2FO(buffer.size() > sizeof(Elf64_Ehdr), "buffer is not large enough");
|
||||
CHECK_VA2FO(ehdr->e_phoff != 0, "unexpected e_phoff");
|
||||
|
||||
CHECK_VA2FO(buffer.size() > ehdr->e_phoff + sizeof(Elf64_Phdr), "buffer is not large enough");
|
||||
|
||||
Elf64_Phdr *phdr = (Elf64_Phdr*)((uint8_t*)buffer.data() + ehdr->e_phoff);
|
||||
CHECK_VA2FO(phdr, "phdr is nullptr");
|
||||
|
||||
for (uint16_t i = 0; i < ehdr->e_phnum; ++i)
|
||||
{
|
||||
if (phdr[i].p_type != PT_LOAD)
|
||||
continue;
|
||||
if (va < phdr[i].p_vaddr || va >= (phdr[i].p_vaddr + phdr[i].p_memsz))
|
||||
continue;
|
||||
|
||||
return va + phdr[i].p_offset - phdr[i].p_vaddr;
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
#undef CHECK_VA2FO
|
||||
#define CHECK_VA2FO(x, msg) if (!(x)) { \
|
||||
std::cerr << __FILE__ << ' ' << __LINE__ << ' ' << msg << std::endl; \
|
||||
return {}; \
|
||||
}
|
||||
|
||||
std::vector<std::pair<uint64_t, uint64_t>> DisassemblyInstance::getSegments()
|
||||
{
|
||||
CHECK_VA2FO(buffer.size(), "buffer is not large enough");
|
||||
|
||||
uint8_t *e_ident = (uint8_t*)buffer.data();
|
||||
CHECK_VA2FO(e_ident, "e_ident is nullptr");
|
||||
|
||||
CHECK_VA2FO(
|
||||
e_ident[EI_MAG0] == ELFMAG0 ||
|
||||
e_ident[EI_MAG1] == ELFMAG1 ||
|
||||
e_ident[EI_MAG2] == ELFMAG2 ||
|
||||
e_ident[EI_MAG3] == ELFMAG3, "unexpected ei_mag");
|
||||
|
||||
CHECK_VA2FO(e_ident[EI_CLASS] == ELFCLASS64, "unexpected ei_class");
|
||||
CHECK_VA2FO(e_ident[EI_DATA] == ELFDATA2LSB, "unexpected ei_data");
|
||||
CHECK_VA2FO(e_ident[EI_VERSION] == EV_CURRENT, "unexpected ei_version");
|
||||
CHECK_VA2FO(e_ident[EI_OSABI] == 64, "unexpected ei_osabi"); // ELFOSABI_AMDGPU_HSA
|
||||
|
||||
CHECK_VA2FO(
|
||||
e_ident[EI_ABIVERSION] == 2 || // ELFABIVERSION_AMDGPU_HSA_V4
|
||||
e_ident[EI_ABIVERSION] == 3 || // ELFABIVERSION_AMDGPU_HSA_V5
|
||||
e_ident[EI_ABIVERSION] == 4, "unexpected ei_abiversion"); // ELFABIVERSION_AMDGPU_HSA_V6
|
||||
|
||||
Elf64_Ehdr *ehdr = (Elf64_Ehdr*)buffer.data();
|
||||
CHECK_VA2FO(ehdr, "ehdr is nullptr");
|
||||
CHECK_VA2FO(ehdr->e_type == ET_DYN, "unexpected e_type");
|
||||
CHECK_VA2FO(ehdr->e_machine == ELF::EM_AMDGPU, "unexpected e_machine");
|
||||
|
||||
CHECK_VA2FO(buffer.size() > sizeof(Elf64_Ehdr), "buffer is not large enough");
|
||||
CHECK_VA2FO(ehdr->e_phoff != 0, "unexpected e_phoff");
|
||||
|
||||
CHECK_VA2FO(buffer.size() > ehdr->e_phoff + sizeof(Elf64_Phdr), "buffer is not large enough");
|
||||
|
||||
Elf64_Phdr *phdr = (Elf64_Phdr*)((uint8_t*)buffer.data() + ehdr->e_phoff);
|
||||
CHECK_VA2FO(phdr, "phdr is nullptr");
|
||||
|
||||
std::vector<std::pair<uint64_t, uint64_t>> segments;
|
||||
for (Elf64_Half i = 0; i < ehdr->e_phnum; ++i)
|
||||
{
|
||||
if (phdr[i].p_type != PT_LOAD)
|
||||
continue;
|
||||
|
||||
segments.push_back({phdr[i].p_vaddr - phdr[i].p_offset, phdr[i].p_memsz});
|
||||
}
|
||||
|
||||
return segments;
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/* 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. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <amd_comgr/amd_comgr.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <limits>
|
||||
|
||||
typedef struct {
|
||||
const char* instruction;
|
||||
const char* cpp_reference;
|
||||
uint64_t address;
|
||||
} instruction_instance_t;
|
||||
|
||||
class CodeObjectBinary {
|
||||
public:
|
||||
CodeObjectBinary(const std::string& uri);
|
||||
std::string m_uri;
|
||||
std::vector<char> buffer;
|
||||
};
|
||||
|
||||
struct SymbolInfo
|
||||
{
|
||||
std::string name;
|
||||
uint64_t faddr;
|
||||
uint64_t mem_size;
|
||||
};
|
||||
|
||||
class DisassemblyInstance {
|
||||
public:
|
||||
DisassemblyInstance(
|
||||
const char* codeobj_data,
|
||||
uint64_t codeobj_size,
|
||||
uint64_t gpu_id
|
||||
);
|
||||
~DisassemblyInstance();
|
||||
|
||||
uint64_t ReadInstruction(uint64_t faddr, uint64_t vaddr, const char* cpp_line);
|
||||
std::map<uint64_t, SymbolInfo>& GetKernelMap();
|
||||
|
||||
static uint64_t memory_callback(uint64_t from, char* to, uint64_t size, void* user_data);
|
||||
static void inst_callback(const char* instruction, void* user_data);
|
||||
static amd_comgr_status_t symbol_callback(amd_comgr_symbol_t symbol, void* user_data);
|
||||
// Per-gpu_id isa_name
|
||||
static std::unordered_map<uint64_t, std::string> agent_isa_name;
|
||||
|
||||
std::optional<uint64_t> va2fo(uint64_t va);
|
||||
std::vector<std::pair<uint64_t, uint64_t>> getSegments();
|
||||
|
||||
std::vector<char> buffer;
|
||||
instruction_instance_t last_instruction;
|
||||
amd_comgr_disassembly_info_t info;
|
||||
amd_comgr_data_t data;
|
||||
std::map<uint64_t, SymbolInfo> symbol_map;
|
||||
};
|
||||
@@ -0,0 +1,361 @@
|
||||
#!/usr/bin/env python3
|
||||
import sys
|
||||
|
||||
if sys.version_info[0] < 3:
|
||||
raise Exception("Must be using Python 3")
|
||||
|
||||
import numpy as np
|
||||
from io import BytesIO
|
||||
import matplotlib.pyplot as plt
|
||||
from copy import deepcopy
|
||||
import json
|
||||
|
||||
COUNTERS_MAX_CAPTURES = 1 << 12
|
||||
|
||||
|
||||
class Readable:
|
||||
def __init__(self, jsonstring):
|
||||
self.jsonstr = json.dumps(jsonstring)
|
||||
self.seek = 0
|
||||
|
||||
def read(self, length=0):
|
||||
if length <= 0:
|
||||
return self.jsonstr
|
||||
else:
|
||||
if self.seek >= len(self):
|
||||
self.seek = 0
|
||||
return None
|
||||
response = self.jsonstr[self.seek : self.seek + length]
|
||||
self.seek += length
|
||||
return bytes(response, "utf-8")
|
||||
|
||||
def __len__(self):
|
||||
return len(self.jsonstr)
|
||||
|
||||
|
||||
class FileBytesIO:
|
||||
def __init__(self, iobytes):
|
||||
self.iobytes = deepcopy(iobytes)
|
||||
self.seek = 0
|
||||
|
||||
def __len__(self):
|
||||
return self.iobytes.getbuffer().nbytes
|
||||
|
||||
def read(self, length=0):
|
||||
if length <= 0:
|
||||
return bytes(self.iobytes.getbuffer())
|
||||
else:
|
||||
if self.seek >= self.iobytes.getbuffer().nbytes:
|
||||
self.seek = 0
|
||||
return None
|
||||
response = self.iobytes.getbuffer()[self.seek : self.seek + length]
|
||||
self.seek += length
|
||||
return bytes(response)
|
||||
|
||||
|
||||
def get_delta_time(events):
|
||||
try:
|
||||
CUS = [[e.time for e in events if e.cu == k and e.bank == 0] for k in range(16)]
|
||||
CUS = [np.asarray(c).astype(np.int64) for c in CUS if len(c) > 2]
|
||||
return np.min([np.min(abs(c[1:] - c[:-1])) for c in CUS])
|
||||
except:
|
||||
return 1
|
||||
|
||||
|
||||
def draw_wave_metrics(selections, normalize, TIMELINES, EVENTS, EVENT_NAMES):
|
||||
plt.figure(figsize=(15, 4))
|
||||
|
||||
delta_step = 8
|
||||
quad_delta_time = max(
|
||||
delta_step, int(0.5 + np.min([get_delta_time(events) for events in EVENTS]))
|
||||
)
|
||||
maxtime = (
|
||||
np.max([np.max([e.time for e in events]) for events in EVENTS]) / quad_delta_time
|
||||
+ 1
|
||||
)
|
||||
|
||||
if maxtime * delta_step >= COUNTERS_MAX_CAPTURES:
|
||||
delta_step = 1
|
||||
while maxtime >= COUNTERS_MAX_CAPTURES:
|
||||
quad_delta_time *= 2
|
||||
maxtime /= 2
|
||||
|
||||
maxtime = int(min(maxtime * delta_step, COUNTERS_MAX_CAPTURES))
|
||||
event_timeline = np.zeros((16, maxtime), dtype=np.int32)
|
||||
print("Delta:", quad_delta_time)
|
||||
print("Max_cycles:", maxtime * quad_delta_time * 4 // delta_step)
|
||||
|
||||
cycles = 4 * quad_delta_time // delta_step * np.arange(maxtime)
|
||||
kernel = len(EVENTS) * quad_delta_time
|
||||
|
||||
for events in EVENTS:
|
||||
for e in range(len(events) - 1):
|
||||
bk = events[e].bank * 4
|
||||
start = events[e].time // (quad_delta_time // delta_step)
|
||||
end = start + delta_step
|
||||
event_timeline[bk : bk + 4, start:end] += np.asarray(
|
||||
events[e].toTuple()[1:5]
|
||||
)[:, None]
|
||||
start = events[-1].time
|
||||
event_timeline[bk : bk + 4, start : start + delta_step] += np.asarray(
|
||||
events[-1].toTuple()[1:5]
|
||||
)[:, None]
|
||||
|
||||
event_timeline = [
|
||||
np.convolve(e, [kernel for k in range(3)])[1:-1] for e in event_timeline
|
||||
]
|
||||
# event_timeline = [e/kernel for e in event_timeline]
|
||||
|
||||
if normalize:
|
||||
event_timeline = [100 * e / max(e.max(), 1e-5) for e in event_timeline]
|
||||
|
||||
colors = [
|
||||
"blue",
|
||||
"green",
|
||||
"gray",
|
||||
"red",
|
||||
"orange",
|
||||
"cyan",
|
||||
"black",
|
||||
"darkviolet",
|
||||
"yellow",
|
||||
"darkred",
|
||||
"pink",
|
||||
"lime",
|
||||
"gold",
|
||||
"tan",
|
||||
"aqua",
|
||||
"olive",
|
||||
]
|
||||
[
|
||||
plt.plot(cycles, e, "-", label=n, color=c)
|
||||
for e, n, c, sel in zip(event_timeline, EVENT_NAMES, colors, selections)
|
||||
if sel
|
||||
]
|
||||
|
||||
plt.legend()
|
||||
if normalize:
|
||||
plt.ylabel("As % of maximum")
|
||||
else:
|
||||
plt.ylabel("Value")
|
||||
plt.xlabel("Cycle")
|
||||
plt.subplots_adjust(left=0.04, right=1, top=1, bottom=0.1)
|
||||
|
||||
figure_bytes = BytesIO()
|
||||
plt.savefig(figure_bytes, dpi=150)
|
||||
return EVENT_NAMES, FileBytesIO(figure_bytes)
|
||||
|
||||
|
||||
def draw_wave_states(selections, normalize, TIMELINES):
|
||||
plot_indices = [1, 2, 3, 4]
|
||||
STATES = [["Empty", "Idle", "Exec", "Wait", "Stall"][k] for k in plot_indices]
|
||||
colors = [["gray", "orange", "green", "red", "blue"][k] for k in plot_indices]
|
||||
|
||||
plt.figure(figsize=(15, 4))
|
||||
|
||||
|
||||
maxtime = max([np.max((TIMELINES[k]!=0)*np.arange(0,TIMELINES[k].size)) for k in plot_indices])
|
||||
maxtime = max(maxtime, 1)
|
||||
timelines = [deepcopy(TIMELINES[k][:maxtime]) for k in plot_indices]
|
||||
timelines = [np.pad(t, [0, maxtime - t.size]) for t in timelines]
|
||||
|
||||
if normalize:
|
||||
timelines = np.array(timelines) / np.maximum(np.sum(timelines, 0) * 1e-2, 1e-7)
|
||||
|
||||
trim = max(maxtime // 5000, 1)
|
||||
cycles = np.arange(0, timelines[0].size // trim, 1) * trim
|
||||
timelines = [
|
||||
time[: trim * (time.size // trim)].reshape((-1, trim)).mean(-1)
|
||||
if len(time) > 0
|
||||
else cycles * 0
|
||||
for time in timelines
|
||||
]
|
||||
kernsize = 15
|
||||
kernel = np.asarray([
|
||||
np.exp(-abs(10 * k / kernsize)) for k in range(-kernsize // 2, kernsize // 2 + 1)
|
||||
])
|
||||
kernel /= np.sum(kernel)
|
||||
|
||||
timelines = [
|
||||
np.convolve(time, kernel)[kernsize // 2 : -kernsize // 2]
|
||||
for time in timelines if len(time) > 0
|
||||
]
|
||||
maxtime *= 16
|
||||
cycles *= 16
|
||||
[
|
||||
plt.plot(cycles, t, label="State " + s, linewidth=1.1, color=c)
|
||||
for t, s, c, sel in zip(timelines, STATES, colors, selections)
|
||||
if sel
|
||||
]
|
||||
|
||||
plt.legend()
|
||||
if normalize:
|
||||
plt.ylabel("Waves state %")
|
||||
else:
|
||||
plt.ylabel("Waves state total")
|
||||
plt.xlabel("Cycle")
|
||||
plt.ylim(-1)
|
||||
plt.xlim(-maxtime // 200, maxtime + maxtime // 200 + 1)
|
||||
plt.subplots_adjust(left=0.04, right=1, top=1, bottom=0.1)
|
||||
figure_bytes = BytesIO()
|
||||
plt.savefig(figure_bytes, dpi=150)
|
||||
return STATES, FileBytesIO(figure_bytes)
|
||||
|
||||
|
||||
def draw_occupancy_per_dispatch(selections, normalize, OCCUPANCY, dispatchnames):
|
||||
plt.figure(figsize=(15, 4))
|
||||
maxtime = 1
|
||||
delta = 1
|
||||
|
||||
for k in range(len(OCCUPANCY)):
|
||||
if len(OCCUPANCY[k]) <= 1:
|
||||
continue
|
||||
for ev in OCCUPANCY[k]:
|
||||
maxtime = max(maxtime, ev[0])
|
||||
|
||||
NUM_DOTS = 1600 # number of points taken for graphing
|
||||
delta = max(1, maxtime // NUM_DOTS) # Spacing between data points. Waves will be averaged over this interval.
|
||||
# Holds occupancy data
|
||||
chart = np.zeros((len(dispatchnames), maxtime // delta + 2), dtype=np.float32)
|
||||
|
||||
for occ in OCCUPANCY:
|
||||
if len(occ) <= 1:
|
||||
continue
|
||||
# Number of waves multiplied by number of events
|
||||
small_chart = np.zeros_like(chart)
|
||||
# Holds number of events in that time period, for averaging.
|
||||
norm_fact = np.zeros_like(chart)
|
||||
norm_fact += 1E-5
|
||||
|
||||
# Holds last known state per dispatch
|
||||
current_time = [0 for k in range(len(dispatchnames))]
|
||||
# Holds occupancy per Dispatch
|
||||
total_value = [0 for k in range(len(dispatchnames))]
|
||||
|
||||
for time, en, kid in occ:
|
||||
b = current_time[kid]
|
||||
e = max(b + 1, time // delta)
|
||||
small_chart[kid][b:e] += total_value[kid]
|
||||
norm_fact[kid][b:e] += 1
|
||||
|
||||
# Enable = 1 means a new wave started, enable = 0 means a wave has ended on that kernel ID.
|
||||
total_value[kid] += 2*en - 1
|
||||
current_time[kid] = time // delta
|
||||
for small, norm, time, value in zip(small_chart, norm_fact, current_time, total_value):
|
||||
small[time] += value
|
||||
norm[time] += value
|
||||
|
||||
chart += small_chart/norm_fact # small_chart / norm_fact is the mean number of waves a tthat time point
|
||||
|
||||
for (id, name), occ in zip(dispatchnames.items(), chart):
|
||||
plt.plot(np.arange(occ.size) * delta * 8, occ, label=str(id)+'#'+name, linewidth=1.1)
|
||||
|
||||
plt.legend()
|
||||
if normalize:
|
||||
plt.ylabel("Occupancy %")
|
||||
else:
|
||||
plt.ylabel("Occupancy total")
|
||||
plt.xlabel("Cycle")
|
||||
plt.ylim(-1)
|
||||
plt.xlim(-maxtime // 200, maxtime + maxtime // 200 + delta + 1)
|
||||
plt.subplots_adjust(left=0.04, right=1, top=1, bottom=0.1)
|
||||
figure_bytes = BytesIO()
|
||||
plt.savefig(figure_bytes, dpi=150)
|
||||
return dispatchnames, FileBytesIO(figure_bytes)
|
||||
|
||||
|
||||
def draw_occupancy(selections, normalize, OCCUPANCY, shadernames, numdispatchid):
|
||||
plt.figure(figsize=(15, 4))
|
||||
names = []
|
||||
|
||||
g_maxtime = 1
|
||||
g_delta = 1
|
||||
for name, occ in zip(shadernames, OCCUPANCY):
|
||||
if len(occ) <= 1:
|
||||
continue
|
||||
|
||||
occ_values = [0]
|
||||
occ_times = [0]
|
||||
|
||||
for time, en, _ in occ:
|
||||
occ_times.append(time)
|
||||
occ_values.append(occ_values[-1] + 2*en - 1) # If enable = 1, increment. Else, decrement occupancy.
|
||||
|
||||
try:
|
||||
names.append('SE'+name.split('_se')[1].split('.att')[0])
|
||||
except:
|
||||
names.append(name)
|
||||
|
||||
NUM_DOTS = 1500 # Number of points taken for graphing
|
||||
maxtime = occ_times[-1]+1
|
||||
delta = max(1, maxtime // NUM_DOTS)
|
||||
g_maxtime = max(g_maxtime, maxtime)
|
||||
g_delta = max(g_delta, delta)
|
||||
chart = np.zeros((maxtime // delta + 1), dtype=np.float32)
|
||||
norm_fact = np.zeros_like(chart)
|
||||
norm_fact += 1E-6
|
||||
|
||||
for i in range(len(occ_times)-1):
|
||||
b = occ_times[i] // delta
|
||||
e = max(b + 1, occ_times[i + 1] // delta)
|
||||
chart[b:e] += occ_values[i]
|
||||
norm_fact[b:e] += 1
|
||||
|
||||
chart /= norm_fact
|
||||
if normalize:
|
||||
chart /= max(chart.max(), 1e-6)
|
||||
|
||||
plt.plot(np.arange(chart.size) * delta, chart, label=names[-1], linewidth=1.1)
|
||||
|
||||
plt.legend()
|
||||
if normalize:
|
||||
plt.ylabel("Occupancy %")
|
||||
else:
|
||||
plt.ylabel("Occupancy total")
|
||||
plt.xlabel("Cycle")
|
||||
plt.ylim(-1)
|
||||
plt.xlim(-g_maxtime // 200, g_maxtime + g_maxtime // 200 + g_delta + 1)
|
||||
plt.subplots_adjust(left=0.04, right=1, top=1, bottom=0.1)
|
||||
figure_bytes = BytesIO()
|
||||
plt.savefig(figure_bytes, dpi=150)
|
||||
return names, FileBytesIO(figure_bytes)
|
||||
|
||||
|
||||
def getocc(u):
|
||||
# Parser struct occupancy_info_t
|
||||
# Bits 23:63 Time= Time divided by 8
|
||||
# Bit 18 = Enable (Wave start if 1, Wave end if 0)
|
||||
# Bits 0:11 is the kernel ID running on that wave
|
||||
return 8*int(u>>23), (u>>18) & 1, u&0xFFF
|
||||
|
||||
|
||||
def GeneratePIC(drawinfo, selections=[True for k in range(16)], normalize=False):
|
||||
EVENTS = drawinfo["EVENTS"]
|
||||
|
||||
response = {}
|
||||
figures = {}
|
||||
|
||||
OCCUPANCY = drawinfo["OCCUPANCY"]
|
||||
# Transforms returned data into a array of events with each event being a tuple (time, enable, kernel ID)
|
||||
OCCUPANCY = [[getocc(u) for u in OCCUPANCY[k]] for k in range(len(OCCUPANCY))]
|
||||
|
||||
states, figure = draw_occupancy(selections, normalize, OCCUPANCY, drawinfo["ShaderNames"], len(drawinfo["DispatchNames"]))
|
||||
response["occupancy.png"] = states
|
||||
figures["occupancy.png"] = figure
|
||||
|
||||
states, figure = draw_occupancy_per_dispatch(selections, normalize, OCCUPANCY, drawinfo["DispatchNames"])
|
||||
response["dispatches.png"] = states
|
||||
figures["dispatches.png"] = figure
|
||||
|
||||
states, figure = draw_wave_states(selections, normalize, drawinfo["TIMELINES"])
|
||||
response["timeline.png"] = states
|
||||
figures["timeline.png"] = figure
|
||||
|
||||
if len(EVENTS) > 0 and np.sum([len(e) for e in EVENTS]) > 32:
|
||||
EVENT_NAMES, figure = draw_wave_metrics(
|
||||
selections, normalize, drawinfo["TIMELINES"], EVENTS, drawinfo["EVENT_NAMES"]
|
||||
)
|
||||
response["counters.png"] = EVENT_NAMES
|
||||
figures["counters.png"] = figure
|
||||
|
||||
return Readable(response), figures
|
||||
@@ -0,0 +1,77 @@
|
||||
/* Copyright (c) 2023 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 <string>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
#include <set>
|
||||
#include <algorithm>
|
||||
|
||||
struct address_range_t
|
||||
{
|
||||
uint64_t addr{0};
|
||||
uint64_t size{0};
|
||||
uint64_t id{0};
|
||||
|
||||
bool operator==(const address_range_t& other) const
|
||||
{
|
||||
return (addr >= other.addr && addr < other.addr + other.size) ||
|
||||
(other.addr >= addr && other.addr < addr + size);
|
||||
}
|
||||
bool operator<(const address_range_t& other) const
|
||||
{
|
||||
if(*this == other) return false;
|
||||
return addr < other.addr;
|
||||
}
|
||||
bool inrange(uint64_t _addr) const { return addr <= _addr && addr + size > _addr; };
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Finds a candidate codeobj for the given vaddr
|
||||
*/
|
||||
class CodeobjTableTranslator : public std::set<address_range_t>
|
||||
{
|
||||
using Super = std::set<address_range_t>;
|
||||
|
||||
public:
|
||||
address_range_t find_codeobj_in_range(uint64_t addr)
|
||||
{
|
||||
if(!cached_segment.inrange(addr))
|
||||
{
|
||||
auto it = this->find(address_range_t{addr, 0, 0});
|
||||
if(it == this->end()) throw std::exception();
|
||||
cached_segment = *it;
|
||||
}
|
||||
return cached_segment;
|
||||
}
|
||||
|
||||
void clear_cache() { cached_segment = {}; }
|
||||
bool remove(const address_range_t& range)
|
||||
{
|
||||
clear_cache();
|
||||
return this->erase(range) != 0;
|
||||
}
|
||||
bool remove(uint64_t addr) { return remove(address_range_t{addr, 0, 0}); }
|
||||
|
||||
private:
|
||||
address_range_t cached_segment{};
|
||||
};
|
||||
@@ -0,0 +1,141 @@
|
||||
#!/usr/bin/env python3
|
||||
import sys
|
||||
|
||||
if sys.version_info[0] < 3:
|
||||
raise Exception("Must be using Python 3")
|
||||
|
||||
import ctypes
|
||||
from ctypes import *
|
||||
import os
|
||||
|
||||
HEADER_OFFSET = 62
|
||||
HEADER_MASK = 0x3
|
||||
ID_OFFSET = 34
|
||||
ID_MASK = (1<<28)-1
|
||||
OFFSET_MASK = (1<<ID_OFFSET)-1
|
||||
|
||||
pluginpath = '../../../lib/rocprofiler/libatt_plugin.so'
|
||||
filedir = os.path.dirname(os.path.realpath(__file__))
|
||||
attplugin = CDLL(os.path.join(filedir, pluginpath))
|
||||
|
||||
attplugin.getSymbolName.restype = c_char_p
|
||||
attplugin.getSymbolName.argtypes = [c_uint64]
|
||||
|
||||
class instruction_info_t(ctypes.Structure):
|
||||
_fields_ = [('inst', c_char_p),
|
||||
('cpp', c_char_p),
|
||||
('size', c_size_t)]
|
||||
|
||||
attplugin.getInstructionFromAddr.restype = instruction_info_t
|
||||
attplugin.getInstructionFromAddr.argtypes = [c_uint64]
|
||||
|
||||
attplugin.getInstructionFromID.restype = instruction_info_t
|
||||
attplugin.getInstructionFromID.argtypes = [c_uint32, c_uint64]
|
||||
|
||||
attplugin.addDecoder.restype = c_int
|
||||
attplugin.addDecoder.argtypes = [c_char_p, c_uint32, c_uint64, c_uint64, c_uint64]
|
||||
|
||||
attplugin.removeDecoder.restype = c_int
|
||||
attplugin.removeDecoder.argtypes = [c_uint32, c_uint64]
|
||||
|
||||
def IsRawPC(addr):
|
||||
return addr >> HEADER_OFFSET == 0
|
||||
|
||||
def getID(addr):
|
||||
return (addr >> ID_OFFSET) & ID_MASK
|
||||
|
||||
def getOffset(addr):
|
||||
return addr & OFFSET_MASK
|
||||
|
||||
class CodeobjInstance:
|
||||
def __init__(self, gpu_id, line):
|
||||
tokens = line.split(' ')
|
||||
self.load_base = int(tokens[0], 16)
|
||||
self.memsize = int(tokens[1], 16)
|
||||
self.att_id = int(tokens[2])
|
||||
self.fpath = tokens[3]
|
||||
|
||||
path = self.fpath.encode('utf-8')
|
||||
self.error = attplugin.addDecoder(path, self.att_id, self.load_base, self.memsize, gpu_id)
|
||||
if self.error != 0:
|
||||
print('Warning: Could not open', line)
|
||||
raise
|
||||
|
||||
def release(self):
|
||||
attplugin.removeDecoder(self.att_id, self.load_base)
|
||||
|
||||
|
||||
class CodeobjService:
|
||||
def __init__(self, gpu_id, att_kernel_txt, cfunc):
|
||||
cfunc.restype = ctypes.c_int
|
||||
cfunc.argtypes = [ctypes.c_char_p, ctypes.c_size_t]
|
||||
|
||||
self.classifier = cfunc
|
||||
self.last_instance = None
|
||||
self.services = {}
|
||||
for line in att_kernel_txt:
|
||||
try:
|
||||
if 'memory://' == line[0:len('memory://')]:
|
||||
continue
|
||||
service = CodeobjInstance(gpu_id, line)
|
||||
self.services[service.att_id] = service
|
||||
except:
|
||||
pass
|
||||
|
||||
def ToRawPC(self, addr):
|
||||
if IsRawPC(addr):
|
||||
return addr
|
||||
return self.services[getID(addr)].load_base + getOffset(addr)
|
||||
|
||||
def release(self):
|
||||
for _, instance in self.services.items():
|
||||
instance.release()
|
||||
|
||||
def GetInstruction(self, addr):
|
||||
if not IsRawPC(addr):
|
||||
return self.GetInstructionFromID(getID(addr), getOffset(addr))
|
||||
else:
|
||||
return self.GetInstructionFromAddr(addr)
|
||||
|
||||
def GetInstructionFromAddr(self, addr):
|
||||
info_inst = attplugin.getInstructionFromAddr(addr)
|
||||
if info_inst.size == 0 or info_inst.inst is None:
|
||||
return None
|
||||
inst = info_inst.inst.decode()
|
||||
cpp = info_inst.cpp
|
||||
if cpp:
|
||||
cpp = cpp.decode()
|
||||
|
||||
while len(inst) and (inst[0] == '\t' or inst[0] == ' '):
|
||||
inst = inst[1:]
|
||||
while len(inst) and (inst[-1] == '\t' or inst[-1] == ' '):
|
||||
inst = inst[:-1]
|
||||
|
||||
return (self.classifier(info_inst.inst, len(inst)), inst, cpp, info_inst.size)
|
||||
|
||||
def GetInstructionFromID(self, id, offset):
|
||||
info_inst = attplugin.getInstructionFromID(id, offset)
|
||||
if info_inst.size == 0 or info_inst.inst is None:
|
||||
return None
|
||||
inst = info_inst.inst.decode()
|
||||
cpp = info_inst.cpp
|
||||
if cpp:
|
||||
cpp = cpp.decode()
|
||||
else:
|
||||
cpp = ''
|
||||
|
||||
while len(inst) and (inst[0] == '\t' or inst[0] == ' '):
|
||||
inst = inst[1:]
|
||||
while len(inst) and (inst[-1] == '\t' or inst[-1] == ' '):
|
||||
inst = inst[:-1]
|
||||
|
||||
return (self.classifier(info_inst.inst, len(inst)), inst, cpp, info_inst.size)
|
||||
|
||||
def getSymbolName(self, addr):
|
||||
try:
|
||||
name = attplugin.getSymbolName(self.ToRawPC(addr))
|
||||
if name:
|
||||
return name.decode()
|
||||
return "Addr #"+hex(self.ToRawPC(addr))
|
||||
except:
|
||||
return "Addr #"+hex(addr)
|
||||
@@ -0,0 +1,574 @@
|
||||
#!/usr/bin/env python3
|
||||
import sys
|
||||
|
||||
if sys.version_info[0] < 3:
|
||||
raise Exception("Must be using Python 3")
|
||||
|
||||
from collections import defaultdict
|
||||
from copy import deepcopy
|
||||
|
||||
MAX_STITCHED_TOKENS = 200000000
|
||||
MAX_FAILED_STITCHES = 256
|
||||
|
||||
SKIP = 0
|
||||
SMEM = 1
|
||||
SALU = 2
|
||||
VMEM = 3
|
||||
FLAT = 4
|
||||
LDS = 5
|
||||
VALU = 6
|
||||
JUMP = 7
|
||||
NEXT = 8
|
||||
IMMED = 9
|
||||
BRANCH = 10
|
||||
GETPC = 11
|
||||
SETPC = 12
|
||||
SWAPPC = 13
|
||||
LANEIO = 14
|
||||
PCINFO = 15
|
||||
WAVE_ENDED = 16
|
||||
DONT_KNOW = 100
|
||||
|
||||
# Keeps track of register states for hipcc-generated assembly
|
||||
class RegisterWatchList:
|
||||
def __init__(self, labels, code, jump_map, insts):
|
||||
self.registers = {"v" + str(k): [[] for m in range(64)] for k in range(64)}
|
||||
for k in range(128):
|
||||
self.registers["s" + str(k)] = []
|
||||
self.labels = labels
|
||||
|
||||
self.code = code
|
||||
self.jump_map = jump_map
|
||||
self.insts = insts
|
||||
|
||||
def jump(self, as_line):
|
||||
return self.jump_map[as_line[2]]
|
||||
|
||||
def getcode(self, line):
|
||||
return self.code[line], 1
|
||||
|
||||
def getincrement(self, line):
|
||||
return 1
|
||||
|
||||
def try_translate(self, tok):
|
||||
if tok[0] in ["s"]:
|
||||
return self.registers[self.range(tok)[0]]
|
||||
elif "@" in tok:
|
||||
return self.labels[tok.split("@")[0]] + 1
|
||||
|
||||
def range(self, r):
|
||||
reg = r.split(":")
|
||||
if len(reg) == 1:
|
||||
return reg
|
||||
else:
|
||||
r0 = reg[0].split("[")
|
||||
return [r0[0] + str(k) for k in range(int(r0[1]), int(reg[1][:-1]) + 1)]
|
||||
|
||||
def tokenize(self, line):
|
||||
return [
|
||||
u for u in [t.split(",")[0].strip() for t in line.split(" ")] if len(u) > 0
|
||||
]
|
||||
|
||||
def getpc(self, line, next_line):
|
||||
try:
|
||||
dst = line.split(" ")[1].strip()
|
||||
label_dests = []
|
||||
try:
|
||||
label_dests = next_line[0].split(", ")
|
||||
except:
|
||||
pass
|
||||
try:
|
||||
label_dests.append(next_line[0].split(", ")[-1].split("@")[0])
|
||||
except:
|
||||
pass
|
||||
|
||||
for label_dst in label_dests:
|
||||
try:
|
||||
cur_label = self.labels[label_dst]
|
||||
for reg in self.range(dst):
|
||||
self.registers[reg] = deepcopy(cur_label)
|
||||
except:
|
||||
pass
|
||||
except:
|
||||
pass
|
||||
|
||||
def swappc(self, line, line_num, inst_num):
|
||||
try:
|
||||
tokens = self.tokenize(line)
|
||||
dst = tokens[1]
|
||||
src = tokens[2]
|
||||
|
||||
popped = deepcopy(self.registers[self.range(src)[0]])
|
||||
self.registers[self.range(dst)[0]] = line_num + 1
|
||||
return popped
|
||||
except:
|
||||
return -1
|
||||
|
||||
def setpc(self, line, inst_num):
|
||||
try:
|
||||
src = line.split(' ')[1].strip()
|
||||
return deepcopy(self.registers[self.range(src)[0]])
|
||||
except:
|
||||
return -1
|
||||
|
||||
def scratch(self, line):
|
||||
try:
|
||||
tokens = self.tokenize(line)
|
||||
if "_load" in tokens[0]:
|
||||
dst = tokens[1]
|
||||
src = tokens[3] + tokens[4]
|
||||
else:
|
||||
src = tokens[2]
|
||||
dst = tokens[3] + tokens[4]
|
||||
self.registers[dst] = deepcopy(self.registers[src])
|
||||
except:
|
||||
pass
|
||||
|
||||
def move(self, line):
|
||||
try:
|
||||
tokens = self.tokenize(line)
|
||||
if tokens[2][0] in ["s", "d"] and tokens[1][0] in ["s", "d"]:
|
||||
self.registers[self.range(tokens[1])[0]] = deepcopy(
|
||||
self.registers[self.range(tokens[2])[0]]
|
||||
)
|
||||
except:
|
||||
pass
|
||||
|
||||
def updatelane(self, line):
|
||||
tokens = self.tokenize(line)
|
||||
try:
|
||||
if "v_readlane" in tokens[0]:
|
||||
self.registers[tokens[1]] = deepcopy(self.registers[tokens[2]][int(tokens[3])])
|
||||
elif "v_writelane" in tokens[0]:
|
||||
self.registers[tokens[1]][int(tokens[3])] = deepcopy(self.registers[tokens[2]])
|
||||
except:
|
||||
pass
|
||||
|
||||
# Matches tokens in reverse order
|
||||
def try_match_swapped(self, i, line, increment):
|
||||
try:
|
||||
return self.insts[i + 1].type == self.code[line][1] and self.insts[i].type == self.code[line + 1][1]
|
||||
except:
|
||||
return False
|
||||
|
||||
# Translates PC values to instructions, for auto captured ISA
|
||||
class PCTranslator:
|
||||
def __init__(self, insts, code, raw_code, reverse_map, codeservice):
|
||||
self.codeservice = codeservice
|
||||
|
||||
self.insts = insts
|
||||
self.addrmap = {c[-3] : (c, self.codeservice.GetInstruction(c[-3])[3]) for c in code if c[-3] > 0}
|
||||
|
||||
self.code = code
|
||||
self.raw_code = raw_code
|
||||
self.reverse_map = reverse_map
|
||||
self.jump_map = {c[-3] : self.getjump_loc(c) for c in code if c[1] == BRANCH}
|
||||
|
||||
def jump(self, as_line):
|
||||
return self.jump_map[as_line[-3]]
|
||||
|
||||
def addsymbol(self, addr):
|
||||
if addr in self.addrmap:
|
||||
return
|
||||
|
||||
symbol = self.codeservice.getSymbolName(addr)
|
||||
if symbol is None:
|
||||
symbol = "Unkown symbol at 0x" + hex(addr)
|
||||
|
||||
last_line = self.raw_code[-1]
|
||||
newline = ['; ' + symbol, DONT_KNOW, last_line[2], '', last_line[4], last_line[5], 0, 0, 0]
|
||||
self.raw_code.append(newline)
|
||||
|
||||
def getcode(self, addr):
|
||||
try:
|
||||
return self.addrmap[addr]
|
||||
except Exception as ex:
|
||||
new_inst = self.codeservice.GetInstruction(addr)
|
||||
if new_inst and new_inst[3]: # Check returned size > 0
|
||||
last_line = self.raw_code[-1]
|
||||
newline = [new_inst[1], new_inst[0], len(self.raw_code), new_inst[2], last_line[4]+1, last_line[5]+1, addr, 0, 0]
|
||||
if new_inst[0] == BRANCH:
|
||||
self.jump_map[addr] = self.getjump_loc(newline)
|
||||
self.addrmap[addr] = (newline, new_inst[3])
|
||||
|
||||
next = len(self.code)
|
||||
self.reverse_map[addr] = len(self.raw_code)
|
||||
self.raw_code.append(newline)
|
||||
self.code.append(newline)
|
||||
return newline, new_inst[3]
|
||||
else:
|
||||
raise ex
|
||||
|
||||
def jump(self, asm_line):
|
||||
try:
|
||||
return self.jump_map[asm_line[-3]]
|
||||
except:
|
||||
loc = self.getjump_loc(asm_line)
|
||||
self.jump_map[asm_line[-3]] = loc
|
||||
return loc
|
||||
|
||||
def getjump_loc(self, asm_line):
|
||||
try:
|
||||
dest = int(asm_line[0].split(' ')[-1])
|
||||
if dest >= 32768: dest -= 65536
|
||||
return asm_line[-3] + 4*dest+4
|
||||
except:
|
||||
return -1
|
||||
|
||||
def getincrement(self, addr):
|
||||
return self.getcode(addr)[1]
|
||||
|
||||
def try_translate(self, tok):
|
||||
pass
|
||||
def range(self, r):
|
||||
pass
|
||||
def tokenize(self, line):
|
||||
pass
|
||||
def getpc(self, line, next_line):
|
||||
pass
|
||||
def swappc(self, line, line_num, inst_index):
|
||||
try:
|
||||
inst_pos = inst_index+1
|
||||
while self.insts[inst_pos].type != PCINFO:
|
||||
inst_pos += 1
|
||||
return self.getcode(self.insts[inst_pos].cycles)[0][-3]
|
||||
except:
|
||||
print('SWAPPC warning: Could not find addr for', inst_index, line)
|
||||
return -1
|
||||
def setpc(self, line, inst_index):
|
||||
try:
|
||||
inst_pos = inst_index+1
|
||||
while self.insts[inst_pos].type != PCINFO:
|
||||
inst_pos += 1
|
||||
return self.getcode(self.insts[inst_pos].cycles)[0][-3]
|
||||
except:
|
||||
print('SETPC warning: Could not find addr for', inst_index, line)
|
||||
return -1
|
||||
def scratch(self, line):
|
||||
pass
|
||||
def move(self, line):
|
||||
pass
|
||||
def updatelane(self, line):
|
||||
pass
|
||||
|
||||
# Matches tokens in reverse order
|
||||
def try_match_swapped(self, i, addr, increment):
|
||||
try:
|
||||
return self.insts[i + 1].type == self.getcode(addr)[0][1] and \
|
||||
self.insts[i].type == self.getcode(addr + increment)[0][1]
|
||||
except Exception as e:
|
||||
return False
|
||||
|
||||
|
||||
def stitch(insts, raw_code, jumps, gfxv, bIsAuto, codeservice):
|
||||
bGFX9 = gfxv == 'vega'
|
||||
|
||||
result, i, loopCount = [], 0, defaultdict(int)
|
||||
|
||||
SMEM_INST = [] # scalar memory
|
||||
VLMEM_INST = [] # vector memory load
|
||||
VSMEM_INST = [] # vector memory store
|
||||
FLAT_INST = []
|
||||
NUM_SMEM = 0
|
||||
NUM_VLMEM = 0
|
||||
NUM_VSMEM = 0
|
||||
NUM_FLAT = 0
|
||||
skipped_immed = 0
|
||||
|
||||
mem_unroll = []
|
||||
flight_count = []
|
||||
|
||||
labels = {}
|
||||
jump_map = [0]
|
||||
|
||||
# Clean the code and remove comments
|
||||
code = [raw_code[0]]
|
||||
for c in raw_code[1:]:
|
||||
c = list(c)
|
||||
c[0] = c[0].split(";")[0].split("//")[0].strip()
|
||||
jump_map.append(len(code))
|
||||
|
||||
if c[1] != DONT_KNOW:
|
||||
code.append(c)
|
||||
elif ":" in c[0]:
|
||||
labels[c[0].split(":")[0]] = len(code)
|
||||
|
||||
reverse_map = {}
|
||||
if bIsAuto:
|
||||
for k, v in enumerate(jump_map):
|
||||
try:
|
||||
reverse_map[code[v][-3]] = k
|
||||
except:
|
||||
pass
|
||||
else:
|
||||
for k, v in enumerate(jump_map):
|
||||
reverse_map[v] = k
|
||||
|
||||
jumps = {jump_map[j] + 1: j for j in jumps}
|
||||
|
||||
# Checks if we have guaranteed ordering in memory operations
|
||||
smem_ordering = 0
|
||||
vlmem_ordering = 0
|
||||
vsmem_ordering = 0
|
||||
|
||||
num_failed_stitches = 0
|
||||
loops = 0
|
||||
maxline = 0
|
||||
|
||||
pcskip = []
|
||||
if bIsAuto:
|
||||
try:
|
||||
firstinst = insts[0]
|
||||
|
||||
if firstinst.type != PCINFO:
|
||||
print('Warning: Waves without PCINFO')
|
||||
return None
|
||||
elif firstinst.cycles == 0:
|
||||
print('Info: Some waves started before the trace')
|
||||
return None
|
||||
|
||||
watchlist = PCTranslator(insts, code, raw_code, reverse_map, codeservice)
|
||||
watchlist.addsymbol(firstinst.cycles)
|
||||
line = firstinst.cycles
|
||||
lineincrement = watchlist.getincrement(line)
|
||||
except KeyError as e:
|
||||
print('Warning: Waves from addr', hex(e.args[0]), 'have no codeobj info.')
|
||||
for i in range(len(insts)):
|
||||
insts[i].asmline = 0
|
||||
return [i for k, i in enumerate(insts) if i.type != PCINFO], [], [], [], 1, 0, [k for k, i in enumerate(insts) if i.type == PCINFO]
|
||||
except Exception as e:
|
||||
print('Unknown error', e)
|
||||
return None
|
||||
else:
|
||||
line = 0
|
||||
lineincrement = 1
|
||||
watchlist = RegisterWatchList(labels=labels, code=code, jump_map=jump_map, insts=insts)
|
||||
|
||||
N = len(insts)
|
||||
|
||||
while i < N and line >= 0 and loops < MAX_STITCHED_TOKENS:
|
||||
if insts[i].type == PCINFO:
|
||||
pcskip.append(i)
|
||||
i += 1
|
||||
continue
|
||||
loops += 1
|
||||
|
||||
inst = insts[i]
|
||||
try:
|
||||
as_line, lineincrement = watchlist.getcode(line)
|
||||
except:
|
||||
break
|
||||
|
||||
matched = True
|
||||
next = line + lineincrement
|
||||
|
||||
if not bIsAuto:
|
||||
if '_mov_' in as_line[0]:
|
||||
watchlist.move(as_line[0])
|
||||
elif 'scratch_' in as_line[0]:
|
||||
watchlist.scratch(as_line[0])
|
||||
|
||||
if as_line[1] == DONT_KNOW or (as_line[1] == SKIP and not bGFX9):
|
||||
matched = False
|
||||
elif as_line[1] == GETPC:
|
||||
try:
|
||||
watchlist.getpc(as_line[0], watchlist.getcode(next)[0])
|
||||
matched = inst.type in [SALU, JUMP]
|
||||
except:
|
||||
matched = False
|
||||
elif as_line[1] == LANEIO:
|
||||
watchlist.updatelane(as_line[0])
|
||||
matched = inst.type == VALU
|
||||
elif as_line[1] == SETPC:
|
||||
next = watchlist.setpc(as_line[0], i)
|
||||
matched = inst.type in [SALU, JUMP]
|
||||
i += 1
|
||||
pcskip.append(i)
|
||||
while bIsAuto and next < 0 and i+1 < len(insts):
|
||||
i += 1
|
||||
if insts[i].type == PCINFO:
|
||||
pcskip.append(i)
|
||||
next = watchlist.setpc(as_line[0], i-1)
|
||||
else:
|
||||
inst.cycles += insts[i].cycles
|
||||
if next < 0:
|
||||
print('Jump to unknown location in line', as_line[0])
|
||||
break
|
||||
elif as_line[1] == SWAPPC:
|
||||
matched = inst.type in [SALU, JUMP]
|
||||
next = watchlist.swappc(as_line[0], line, i)
|
||||
i += 1
|
||||
pcskip.append(i)
|
||||
while bIsAuto and next < 0 and i+1 < len(insts):
|
||||
i += 1
|
||||
if insts[i].type == PCINFO:
|
||||
next = watchlist.swappc(as_line[0], line, i-1)
|
||||
pcskip.append(i)
|
||||
else:
|
||||
inst.cycles += insts[i].cycles
|
||||
if next < 0:
|
||||
print('Jump to unknown location in line', as_line[0])
|
||||
break
|
||||
elif inst.type == as_line[1]:
|
||||
if line in jumps:
|
||||
loopCount[jumps[line] - 1] += 1
|
||||
num_inflight = NUM_FLAT + NUM_SMEM + NUM_VLMEM + NUM_VSMEM
|
||||
|
||||
if inst.type == SMEM or inst.type == LDS:
|
||||
smem_ordering = 1 if inst.type == SMEM else smem_ordering
|
||||
SMEM_INST.append([reverse_map[line], num_inflight])
|
||||
NUM_SMEM += 1
|
||||
elif inst.type == VMEM or (inst.type == FLAT and "global_" in as_line[0]):
|
||||
inc_ordering = False
|
||||
if "flat_" in as_line[0]:
|
||||
inc_ordering = True
|
||||
|
||||
if not bGFX9 and "store" in as_line[0]:
|
||||
VSMEM_INST.append([reverse_map[line], num_inflight])
|
||||
NUM_VSMEM += 1
|
||||
if inc_ordering:
|
||||
vsmem_ordering = 1
|
||||
else:
|
||||
VLMEM_INST.append([reverse_map[line], num_inflight])
|
||||
NUM_VLMEM += 1
|
||||
if inc_ordering:
|
||||
vlmem_ordering = 1
|
||||
elif inst.type == FLAT:
|
||||
smem_ordering = 1
|
||||
vlmem_ordering = 1
|
||||
vsmem_ordering = 1
|
||||
FLAT_INST.append([reverse_map[line], num_inflight])
|
||||
NUM_FLAT += 1
|
||||
elif inst.type == IMMED and "s_wait" in as_line[0] and not "s_wait_alu" in as_line[0]:
|
||||
if "lgkmcnt" in as_line[0] or "dscnt" in as_line[0] or "kmcnt" in as_line[0]:
|
||||
try:
|
||||
wait_N = int(as_line[0].split("lgkmcnt(")[1].split(")")[0])
|
||||
except:
|
||||
wait_N = 0
|
||||
flight_count.append([as_line[5], num_inflight, wait_N])
|
||||
if wait_N == 0:
|
||||
smem_ordering = 0
|
||||
if smem_ordering == 0:
|
||||
offset = len(SMEM_INST) - wait_N
|
||||
mem_unroll.append(
|
||||
[reverse_map[line], SMEM_INST[:offset] + FLAT_INST]
|
||||
)
|
||||
SMEM_INST = SMEM_INST[offset:]
|
||||
NUM_SMEM = len(SMEM_INST)
|
||||
FLAT_INST = []
|
||||
NUM_FLAT = 0
|
||||
else:
|
||||
NUM_SMEM = min(max(wait_N - NUM_FLAT, 0), NUM_SMEM)
|
||||
NUM_FLAT = min(max(wait_N - NUM_SMEM, 0), NUM_FLAT)
|
||||
num_inflight = NUM_FLAT + NUM_SMEM + NUM_VLMEM + NUM_VSMEM
|
||||
|
||||
if "vmcnt" in as_line[0] or "loadcnt" in as_line[0]:
|
||||
try:
|
||||
wait_N = int(as_line[0].split("vmcnt(")[1].split(")")[0])
|
||||
except:
|
||||
wait_N = 0
|
||||
flight_count.append([as_line[5], num_inflight, wait_N])
|
||||
if wait_N == 0:
|
||||
vlmem_ordering = 0
|
||||
if vlmem_ordering == 0:
|
||||
offset = len(VLMEM_INST) - wait_N
|
||||
mem_unroll.append(
|
||||
[reverse_map[line], VLMEM_INST[:offset] + FLAT_INST]
|
||||
)
|
||||
VLMEM_INST = VLMEM_INST[offset:]
|
||||
NUM_VLMEM = len(VLMEM_INST)
|
||||
FLAT_INST = []
|
||||
NUM_FLAT = 0
|
||||
else:
|
||||
NUM_VLMEM = min(max(wait_N - NUM_FLAT, 0), NUM_VLMEM)
|
||||
NUM_FLAT = min(max(wait_N - NUM_VLMEM, 0), NUM_FLAT)
|
||||
num_inflight = NUM_FLAT + NUM_SMEM + NUM_VLMEM + NUM_VSMEM
|
||||
|
||||
if "vscnt" in as_line[0] or (bGFX9 and "vmcnt" in as_line[0]) or "storecnt" in as_line[0]:
|
||||
try:
|
||||
wait_N = int(as_line[0].split('vscnt(')[1].split(')')[0])
|
||||
except:
|
||||
try:
|
||||
wait_N = int(as_line[0].split('vmcnt(')[1].split(')')[0])
|
||||
except:
|
||||
wait_N = 0
|
||||
flight_count.append([as_line[5], num_inflight, wait_N])
|
||||
if wait_N == 0:
|
||||
vsmem_ordering = 0
|
||||
if vsmem_ordering == 0:
|
||||
offset = len(VSMEM_INST) - wait_N
|
||||
mem_unroll.append(
|
||||
[reverse_map[line], VSMEM_INST[:offset] + FLAT_INST]
|
||||
)
|
||||
VSMEM_INST = VSMEM_INST[offset:]
|
||||
NUM_VSMEM = len(VSMEM_INST)
|
||||
FLAT_INST = []
|
||||
NUM_FLAT = 0
|
||||
else:
|
||||
NUM_VSMEM = min(max(wait_N - NUM_FLAT, 0), NUM_VSMEM)
|
||||
NUM_FLAT = min(max(wait_N - NUM_VSMEM, 0), NUM_FLAT)
|
||||
num_inflight = NUM_FLAT + NUM_SMEM + NUM_VLMEM + NUM_VSMEM
|
||||
|
||||
elif inst.type == JUMP and as_line[1] == BRANCH:
|
||||
next = watchlist.jump(as_line)
|
||||
if next is None or next == 0:
|
||||
print("Jump to unknown location!", as_line)
|
||||
break
|
||||
elif inst.type == NEXT and as_line[1] == BRANCH:
|
||||
pass
|
||||
else:
|
||||
matched = False
|
||||
if watchlist.try_match_swapped(i, line, lineincrement):
|
||||
temp = insts[i]
|
||||
insts[i] = insts[i + 1]
|
||||
insts[i + 1] = temp
|
||||
next = line
|
||||
else:
|
||||
hasWait = "s_wait" in as_line[0]
|
||||
if hasWait or "_load_" in as_line[0]:
|
||||
if skipped_immed > 0 and hasWait:
|
||||
matched = True
|
||||
skipped_immed -= 1
|
||||
elif 's_waitcnt' in as_line[0] and 'scratch_' not in as_line[0]:
|
||||
print('WARNING: Parsing terminated at:', as_line)
|
||||
break
|
||||
|
||||
if matched or as_line[1] != DONT_KNOW:
|
||||
if matched:
|
||||
inst.asmline = reverse_map[line]
|
||||
result.append(inst)
|
||||
i += 1
|
||||
num_failed_stitches = 0
|
||||
elif not bGFX9 and inst.type == IMMED and line != next and as_line[1] != SKIP:
|
||||
skipped_immed += 1
|
||||
inst.asmline = reverse_map[line]
|
||||
result.append(inst)
|
||||
if 's_barrier' in as_line[0]:
|
||||
next = line + lineincrement
|
||||
i += 1
|
||||
else:
|
||||
num_failed_stitches += 1
|
||||
|
||||
maxline = max(reverse_map[line], maxline)
|
||||
line = next
|
||||
|
||||
N = max(N, 1)
|
||||
if i != N and (insts[i].type == WAVE_ENDED or i == N-1):
|
||||
print('Warning - Wave ended.')
|
||||
elif i < N:
|
||||
print('Warning - Stitching rate: '+str(i * 100 / N)+'% matched', i, ' of ', N)
|
||||
try:
|
||||
print(line, code[line])
|
||||
except:
|
||||
pass
|
||||
else:
|
||||
while line < len(code):
|
||||
if "s_endpgm" in code[line]:
|
||||
mem_unroll.append(
|
||||
[reverse_map[line], SMEM_INST + VLMEM_INST + VSMEM_INST + FLAT_INST]
|
||||
)
|
||||
break
|
||||
line += 1
|
||||
print('Success: Parsed', i, 'tokens')
|
||||
|
||||
return result, loopCount, mem_unroll, flight_count, maxline, len(result), pcskip
|
||||
Spustitelný soubor
+270
@@ -0,0 +1,270 @@
|
||||
#!/usr/bin/env python3
|
||||
import sys
|
||||
|
||||
if sys.version_info[0] < 3:
|
||||
raise Exception("Must be using Python 3")
|
||||
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import socket
|
||||
from pathlib import Path
|
||||
from collections import defaultdict
|
||||
import http.server
|
||||
import socketserver
|
||||
import socket
|
||||
import asyncio
|
||||
import websockets
|
||||
from multiprocessing import Process, Manager
|
||||
import numpy as np
|
||||
from http import HTTPStatus
|
||||
from io import BytesIO
|
||||
from drawing import Readable, GeneratePIC
|
||||
from copy import deepcopy
|
||||
from shutil import copy2
|
||||
from glob import glob
|
||||
|
||||
JSON_GLOBAL_DICTIONARY = {}
|
||||
|
||||
def get_ip():
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
s.settimeout(0)
|
||||
try:
|
||||
hostname = socket.gethostname()
|
||||
IPAddr = socket.gethostbyname(hostname)
|
||||
s.connect(({IPAddr}, 1))
|
||||
except Exception:
|
||||
IPAddr = "127.0.0.1"
|
||||
finally:
|
||||
return IPAddr
|
||||
|
||||
|
||||
IPAddr = get_ip()
|
||||
PORT, WebSocketPort = 8000, 18000
|
||||
SP = "\u00A0"
|
||||
|
||||
|
||||
def get_top_n(code):
|
||||
TOP_N = 10
|
||||
top_n = sorted(deepcopy(code), key=lambda x: x[-1], reverse=True)[:TOP_N]
|
||||
return [
|
||||
(line_num, hitc, 0, run_time) for _, _, _, _, line_num, _, hitc, run_time in top_n
|
||||
]
|
||||
|
||||
|
||||
def wave_info(df, id):
|
||||
dic = {
|
||||
"Issue": df["issued_ins"][id],
|
||||
"Valu": df["valu_ins"][id],
|
||||
"Valu_stall": df["valu_stalls"][id],
|
||||
"Salu": df["salu_ins"][id],
|
||||
"Salu_stall": df["salu_stalls"][id],
|
||||
"Vmem": df["vmem_ins"][id],
|
||||
"Vmem_stall": df["vmem_stalls"][id],
|
||||
"Smem": df["smem_ins"][id],
|
||||
"Smem_stall": df["smem_stalls"][id],
|
||||
"Flat": df["flat_ins"][id],
|
||||
"Flat_stall": df["flat_stalls"][id],
|
||||
"Lds": df["lds_ins"][id],
|
||||
"Lds_stall": df["lds_stalls"][id],
|
||||
"Br": df["br_ins"][id],
|
||||
"Br_stall": df["br_stalls"][id],
|
||||
}
|
||||
dic["Issue_stall"] = int(np.sum([dic[key] for key in dic.keys() if "_STALL" in key]))
|
||||
return dic
|
||||
|
||||
|
||||
def extract_data(df, se_number):
|
||||
if len(df["id"]) == 0 or len(df["instructions"]) == 0 or len(df["timeline"]) == 0:
|
||||
return None
|
||||
|
||||
wave_filenames = []
|
||||
flight_count = []
|
||||
wave_slot_count = [
|
||||
{df["wave_slot"][wave_id]: 0 for wave_id in df["id"]} for k in range(4)
|
||||
]
|
||||
|
||||
print("Number of waves:", len(df["id"]))
|
||||
allwaves_maxline = 0
|
||||
|
||||
for wave_id in df["id"]:
|
||||
stitched, loopCount, mem_unroll, count, maxline, num_insts = df["instructions"][
|
||||
wave_id
|
||||
]
|
||||
timeline = df["timeline"][wave_id]
|
||||
|
||||
if len(stitched) == 0 or len(timeline) == 0:
|
||||
continue
|
||||
|
||||
allwaves_maxline = max(allwaves_maxline, maxline)
|
||||
flight_count.append(count)
|
||||
|
||||
wave_entry = {
|
||||
"id": int(df["id"][wave_id]),
|
||||
"simd": int(df["simd"][wave_id]),
|
||||
"slot": int(df["wave_slot"][wave_id]),
|
||||
"begin": int(df["begin_time"][wave_id]),
|
||||
"end": int(df["end_time"][wave_id]),
|
||||
"info": wave_info(df, wave_id),
|
||||
"instructions": stitched,
|
||||
"timeline": timeline,
|
||||
"waitcnt": mem_unroll,
|
||||
}
|
||||
data_obj = {
|
||||
"name": "SE".format(se_number),
|
||||
"duration": sum(dur for (_, dur) in timeline),
|
||||
"wave": wave_entry,
|
||||
"loop_count": loopCount,
|
||||
"top_n": [],
|
||||
"num_stitched": len(stitched),
|
||||
"num_insts": num_insts,
|
||||
"websocket_port": WebSocketPort,
|
||||
"generation_time": time.ctime(),
|
||||
}
|
||||
|
||||
simd_id = df["simd"][wave_id]
|
||||
slot_id = df["wave_slot"][wave_id]
|
||||
slot_count = wave_slot_count[simd_id][slot_id]
|
||||
wave_slot_count[simd_id][slot_id] += 1
|
||||
|
||||
OUT = (
|
||||
"se"
|
||||
+ str(se_number)
|
||||
+ "_sm"
|
||||
+ str(simd_id)
|
||||
+ "_sl"
|
||||
+ str(slot_id)
|
||||
+ "_wv"
|
||||
+ str(slot_count)
|
||||
+ ".json"
|
||||
)
|
||||
JSON_GLOBAL_DICTIONARY[OUT] = Readable(data_obj)
|
||||
wave_filenames.append((OUT, df["begin_time"][wave_id], df["end_time"][wave_id]))
|
||||
|
||||
data_obj = {
|
||||
"name": "SE".format(se_number),
|
||||
"websocket_port": WebSocketPort,
|
||||
"generation_time": time.ctime(),
|
||||
}
|
||||
se_filename = None
|
||||
if len(wave_filenames) > 0:
|
||||
se_filename = "se" + str(se_number) + "_info.json"
|
||||
JSON_GLOBAL_DICTIONARY[se_filename] = Readable(data_obj)
|
||||
|
||||
return flight_count, wave_filenames, se_filename, allwaves_maxline
|
||||
|
||||
|
||||
def call_picture_callback(return_dict, drawinfo):
|
||||
response, imagebytes = GeneratePIC(drawinfo)
|
||||
return_dict["graph_options.json"] = response
|
||||
for k, v in imagebytes.items():
|
||||
return_dict[k] = v
|
||||
|
||||
for n, m in enumerate(drawinfo["TIMELINES"]):
|
||||
return_dict["wstates" + str(n) + ".json"] = Readable(
|
||||
{"data": [int(n) for n in list(np.asarray(m))]}
|
||||
)
|
||||
for n, e in enumerate(drawinfo["EVENTS"]):
|
||||
return_dict["se" + str(n) + "_perfcounter.json"] = Readable(
|
||||
{"data": [v.toTuple() for v in e]}
|
||||
)
|
||||
|
||||
|
||||
def view_trace(
|
||||
code,
|
||||
dbnames,
|
||||
att_filenames,
|
||||
se_time_begin,
|
||||
gfxv,
|
||||
drawinfo,
|
||||
trace_instance_name
|
||||
):
|
||||
global JSON_GLOBAL_DICTIONARY
|
||||
pic_thread = None
|
||||
|
||||
manager = Manager()
|
||||
return_dict = manager.dict()
|
||||
occ_dict = {str(k): drawinfo["OCCUPANCY"][k] for k in range(len(drawinfo["OCCUPANCY"]))}
|
||||
occ_dict['dispatches'] = {}
|
||||
for id, name in drawinfo['DispatchNames'].items():
|
||||
occ_dict['dispatches'][id] = name
|
||||
occ_dict['names'] = drawinfo['ShaderNames']
|
||||
|
||||
JSON_GLOBAL_DICTIONARY["occupancy.json"] = Readable(occ_dict)
|
||||
pic_thread = Process(target=call_picture_callback, args=(return_dict, drawinfo))
|
||||
pic_thread.start()
|
||||
|
||||
att_filenames = [Path(f).name for f in att_filenames]
|
||||
se_numbers = [int(a.split("_se")[1].split(".att")[0]) for a in att_filenames]
|
||||
flight_count = []
|
||||
simd_wave_filenames = {}
|
||||
se_filenames = []
|
||||
|
||||
allse_maxline = 0
|
||||
for se_number, dbname in zip(se_numbers, dbnames):
|
||||
if len(dbname["id"]) == 0:
|
||||
continue
|
||||
|
||||
count, wv_filenames, se_filename, maxline = extract_data(dbname, se_number)
|
||||
if se_filename is None:
|
||||
continue
|
||||
allse_maxline = max(allse_maxline, maxline)
|
||||
se_filenames.append(se_filename)
|
||||
|
||||
if count is not None:
|
||||
flight_count.append(count)
|
||||
simd_wave_filenames[se_number] = wv_filenames
|
||||
|
||||
code_sel = [c[:-3]+c[-2:] for c in code]
|
||||
JSON_GLOBAL_DICTIONARY['code.json'] = Readable({"code": code_sel, "top_n": get_top_n(code_sel)})
|
||||
|
||||
for key in simd_wave_filenames.keys():
|
||||
wv_array = [
|
||||
[
|
||||
int(s[0].split("_sm")[1].split("_sl")[0]),
|
||||
int(s[0].split("_sl")[1].split("_wv")[0]),
|
||||
int(s[0].split("_wv")[1].split(".")[0]),
|
||||
s,
|
||||
]
|
||||
for s in simd_wave_filenames[key]
|
||||
]
|
||||
|
||||
wv_dict = {}
|
||||
for wv in wv_array:
|
||||
try:
|
||||
wv_dict[wv[0]][wv[1]][wv[2]] = wv[3]
|
||||
except:
|
||||
try:
|
||||
wv_dict[wv[0]][wv[1]] = {wv[2]: wv[3]}
|
||||
except:
|
||||
try:
|
||||
wv_dict[wv[0]] = {wv[1]: {wv[2]: wv[3]}}
|
||||
except:
|
||||
pass
|
||||
|
||||
simd_wave_filenames[key] = wv_dict
|
||||
|
||||
JSON_GLOBAL_DICTIONARY["filenames.json"] = Readable(
|
||||
{
|
||||
"wave_filenames": simd_wave_filenames,
|
||||
"se_filenames": se_filenames,
|
||||
"global_begin_time": int(se_time_begin),
|
||||
"gfxv": gfxv,
|
||||
}
|
||||
)
|
||||
|
||||
if pic_thread is not None:
|
||||
pic_thread.join()
|
||||
for k, v in return_dict.items():
|
||||
JSON_GLOBAL_DICTIONARY[k] = v
|
||||
|
||||
os.makedirs(trace_instance_name + "_ui/", exist_ok=True)
|
||||
JSON_GLOBAL_DICTIONARY["live.json"] = Readable({"live": 0})
|
||||
|
||||
ui_dir_files = glob(os.path.join(os.path.abspath(os.path.dirname(__file__)), "ui")+"/*")
|
||||
for f in ui_dir_files:
|
||||
copy2(f, trace_instance_name + "_ui/")
|
||||
|
||||
for k, v in JSON_GLOBAL_DICTIONARY.items():
|
||||
with open(os.path.join(trace_instance_name+"_ui", k), "w" if ".json" in k else "wb") as f:
|
||||
f.write(v.read())
|
||||
@@ -0,0 +1,70 @@
|
||||
#!/usr/bin/env python3
|
||||
import sys
|
||||
|
||||
if sys.version_info[0] < 3:
|
||||
raise Exception("Must be using Python 3")
|
||||
|
||||
import http.server
|
||||
import socketserver
|
||||
import socket
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
class NoCacheHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
|
||||
def end_headers(self):
|
||||
self.send_my_headers()
|
||||
http.server.SimpleHTTPRequestHandler.end_headers(self)
|
||||
|
||||
def send_my_headers(self):
|
||||
self.send_header("Cache-Control", "no-cache, no-store, must-revalidate")
|
||||
self.send_header("Pragma", "no-cache")
|
||||
self.send_header("Expires", "0")
|
||||
|
||||
def do_GET(self):
|
||||
if ".png?" in self.path:
|
||||
self.path = self.path.split(".png?")[0] + ".png"
|
||||
|
||||
http.server.SimpleHTTPRequestHandler.do_GET(self)
|
||||
|
||||
|
||||
class RocTCPServer(socketserver.TCPServer):
|
||||
def server_bind(self):
|
||||
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
self.socket.bind(self.server_address)
|
||||
|
||||
|
||||
def run_server():
|
||||
Handler = NoCacheHTTPRequestHandler
|
||||
os.chdir(os.path.join(os.path.dirname(os.path.abspath(__file__)), "."))
|
||||
try:
|
||||
with RocTCPServer((IPAddr, PORT), Handler) as httpd:
|
||||
httpd.serve_forever()
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
|
||||
|
||||
def get_ip():
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
s.settimeout(0)
|
||||
try:
|
||||
hostname = socket.gethostname()
|
||||
IPAddr = socket.gethostbyname(hostname)
|
||||
s.connect(({IPAddr}, 1))
|
||||
except Exception:
|
||||
IPAddr = "127.0.0.1"
|
||||
finally:
|
||||
return IPAddr
|
||||
|
||||
|
||||
IPAddr = get_ip()
|
||||
PORT = 8000
|
||||
|
||||
if len(sys.argv) > 1:
|
||||
PORT = int(sys.argv[1])
|
||||
print("serving at port: {0}".format(PORT))
|
||||
|
||||
try:
|
||||
run_server()
|
||||
except KeyboardInterrupt:
|
||||
print("Exitting.")
|
||||
Rozdílový obsah nebyl zobrazen, protože je příliš veliký
Načíst rozdílové porovnání
@@ -0,0 +1,132 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<!-- Created with Inkscape (http://www.inkscape.org/) -->
|
||||
|
||||
<svg
|
||||
width="240"
|
||||
height="26"
|
||||
viewBox="0 0 68.974872 7.4722778"
|
||||
version="1.1"
|
||||
id="svg5"
|
||||
inkscape:version="1.2.1 (9c6d41e410, 2022-07-14)"
|
||||
sodipodi:docname="logo.svg"
|
||||
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
|
||||
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
xmlns:svg="http://www.w3.org/2000/svg">
|
||||
<sodipodi:namedview
|
||||
id="namedview7"
|
||||
pagecolor="#ffffff"
|
||||
bordercolor="#000000"
|
||||
borderopacity="0.25"
|
||||
inkscape:showpageshadow="2"
|
||||
inkscape:pageopacity="0.0"
|
||||
inkscape:pagecheckerboard="0"
|
||||
inkscape:deskcolor="#d1d1d1"
|
||||
inkscape:document-units="mm"
|
||||
showgrid="false"
|
||||
inkscape:zoom="5.1659481"
|
||||
inkscape:cx="147.89154"
|
||||
inkscape:cy="75.10722"
|
||||
inkscape:window-width="3840"
|
||||
inkscape:window-height="2066"
|
||||
inkscape:window-x="-11"
|
||||
inkscape:window-y="-11"
|
||||
inkscape:window-maximized="1"
|
||||
inkscape:current-layer="layer1" />
|
||||
<defs
|
||||
id="defs2" />
|
||||
<g
|
||||
inkscape:label="Layer 1"
|
||||
inkscape:groupmode="layer"
|
||||
id="layer1">
|
||||
<rect
|
||||
style="fill:#217844;stroke:#00000f;stroke-width:0.0610255;stroke-miterlimit:1"
|
||||
id="rect864"
|
||||
width="37.346996"
|
||||
height="6.0453367"
|
||||
x="26.672634"
|
||||
y="0.91105449" />
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:6.35px;font-family:Calibri;-inkscape-font-specification:Calibri;fill:#165016;stroke:#00000f;stroke-width:0.0539999;stroke-miterlimit:1;stroke-dasharray:none"
|
||||
x="82.180176"
|
||||
y="95.625328"
|
||||
id="text3303"><tspan
|
||||
sodipodi:role="line"
|
||||
id="tspan3301"
|
||||
style="fill:#165016;stroke-width:0.054"
|
||||
x="82.180176"
|
||||
y="95.625328" /></text>
|
||||
<!-- <g
|
||||
id="g5730"
|
||||
transform="matrix(0.57568164,0,0,0.53998363,-62.239348,-52.859622)">
|
||||
<g
|
||||
aria-label="AMD"
|
||||
id="text954"
|
||||
style="font-size:6.35px;font-family:Calibri;-inkscape-font-specification:Calibri;fill:#000080;stroke:#00000f;stroke-width:0.0539999;stroke-miterlimit:1"
|
||||
transform="translate(27.503837,13.598552)">
|
||||
<path
|
||||
d="m 89.87666,89.436026 q 0.04779,0.05463 0.06348,0.09511 0.01746,0.03646 0.0022,0.06566 -0.01528,0.0292 -0.06797,0.05628 -0.0498,0.02596 -0.139412,0.0607 -0.08962,0.03475 -0.146787,0.05026 -0.05316,0.01729 -0.08543,0.01982 -0.0305,-0.0015 -0.05168,-0.01322 -0.01829,-0.01286 -0.03994,-0.0344 l -0.673965,-0.786215 -1.555301,0.60303 0.04216,1.017881 q 0.0014,0.02938 -0.0052,0.05522 -0.0077,0.02295 -0.03103,0.04861 -0.02154,0.02165 -0.07133,0.04761 -0.0469,0.02484 -0.127848,0.05622 -0.08384,0.0325 -0.142126,0.04513 -0.05428,0.01439 -0.08525,0.0031 -0.02808,-0.01239 -0.04265,-0.04997 -0.0128,-0.04159 -0.01433,-0.11416 l -0.09264,-3.961315 q -7.66e-4,-0.03628 0.0087,-0.06324 0.01239,-0.02808 0.0426,-0.0531 0.0331,-0.02613 0.08691,-0.05032 0.05558,-0.0282 0.142303,-0.06183 0.09251,-0.03587 0.155459,-0.05362 0.06183,-0.02065 0.103895,-0.02366 0.04206,-0.003 0.07127,0.01227 0.02808,0.01239 0.05198,0.03971 z m -2.799476,-2.442912 -0.0029,0.0011 0.07829,2.114582 1.300903,-0.504394 z"
|
||||
style="stroke-width:0.054"
|
||||
id="path4984" />
|
||||
<path
|
||||
d="m 94.99296,89.362526 q -2.68e-4,0.0248 -0.01287,0.04327 -0.0126,0.01847 -0.04374,0.03054 -0.0311,0.009 -0.08078,0.01463 -0.04661,0.0088 -0.124117,0.008 -0.07131,-7.69e-4 -0.123916,-0.01064 -0.04954,-0.0067 -0.08044,-0.01637 -0.02777,-0.0127 -0.03997,-0.03144 -0.0122,-0.01874 -0.01193,-0.04354 l 0.03766,-3.494157 -0.0062,-6.7e-5 -1.467119,3.494252 q -0.0095,0.0216 -0.0283,0.0369 -0.01567,0.01533 -0.04991,0.02737 -0.0311,0.009 -0.07764,0.01157 -0.04347,0.0057 -0.105481,0.0051 -0.06511,-7.02e-4 -0.111547,-0.0074 -0.04644,-0.0067 -0.07734,-0.01634 -0.03087,-0.01274 -0.04931,-0.02844 -0.01533,-0.01567 -0.02133,-0.03434 l -1.329447,-3.524398 -0.0031,-3.4e-5 -0.03767,3.494158 q -2.67e-4,0.0248 -0.01287,0.04327 -0.0126,0.01847 -0.04374,0.03054 -0.0311,0.009 -0.08078,0.01463 -0.04971,0.0088 -0.127216,0.0079 -0.07441,-8.02e-4 -0.123916,-0.01064 -0.04954,-0.0067 -0.08044,-0.01637 -0.02777,-0.0127 -0.03997,-0.03144 -0.0091,-0.0187 -0.0088,-0.04351 l 0.03977,-3.689482 q 0.0014,-0.130217 0.07022,-0.18529 0.06881,-0.05507 0.152521,-0.05417 l 0.322442,0.0035 q 0.09921,0.0011 0.173422,0.02047 0.07421,0.0194 0.129582,0.06031 0.05537,0.04091 0.09191,0.103316 0.03654,0.06241 0.06354,0.146421 l 1.131337,2.917613 0.0155,1.67e-4 1.240374,-2.882745 q 0.03511,-0.09265 0.07611,-0.157319 0.04101,-0.06467 0.08791,-0.101377 0.05004,-0.03977 0.109116,-0.05464 0.05911,-0.01797 0.136618,-0.01713 l 0.337944,0.0036 q 0.04651,5.01e-4 0.08665,0.01644 0.04327,0.01287 0.07084,0.04417 0.0307,0.02824 0.04881,0.07494 0.01813,0.04361 0.01743,0.108714 z"
|
||||
style="stroke-width:0.054"
|
||||
id="path4986" />
|
||||
<path
|
||||
d="m 99.688647,89.106481 q -0.202131,0.480082 -0.476607,0.788407 -0.273273,0.305468 -0.610318,0.442789 -0.332985,0.135666 -0.72523,0.10845 -0.392245,-0.02722 -0.866611,-0.22694 l -0.78299,-0.329666 q -0.06573,-0.02767 -0.107689,-0.0958 -0.0379,-0.06979 0.009,-0.181233 l 1.388446,-3.297704 q 0.04692,-0.111447 0.122123,-0.130248 0.07926,-0.02046 0.144986,0.0072 l 0.837285,0.352526 q 0.480082,0.202131 0.758628,0.467433 0.279749,0.262444 0.403985,0.590616 0.128297,0.326518 0.103035,0.706129 -0.0224,0.380814 -0.198064,0.798028 z m -0.525653,-0.197768 q 0.126331,-0.300051 0.160317,-0.58852 0.03519,-0.291326 -0.04543,-0.547307 -0.07776,-0.254777 -0.276143,-0.462778 -0.198382,-0.208 -0.581304,-0.369224 l -0.500085,-0.210553 -1.211581,2.877633 0.505801,0.212959 q 0.354346,0.149191 0.634542,0.162873 0.280196,0.01368 0.520083,-0.100626 0.239887,-0.114308 0.435404,-0.354952 0.199578,-0.242298 0.358396,-0.619505 z"
|
||||
style="stroke-width:0.054"
|
||||
id="path4988" />
|
||||
</g>
|
||||
<g
|
||||
aria-label="Research"
|
||||
id="text3463"
|
||||
style="font-size:6.35px;font-family:Calibri;-inkscape-font-specification:Calibri;fill:#165016;stroke:#00000f;stroke-width:0.0539999;stroke-miterlimit:1"
|
||||
transform="translate(28.562171,13.598552)">
|
||||
<path
|
||||
d="m 83.86747,96.36485 q 0,0.02481 -0.0093,0.04341 -0.0093,0.0186 -0.04031,0.03101 -0.031,0.0124 -0.08682,0.0186 -0.05581,0.0062 -0.148828,0.0062 -0.08062,0 -0.133325,-0.0062 -0.04961,-0.0062 -0.08062,-0.0186 -0.031,-0.0155 -0.04961,-0.04031 -0.0155,-0.0248 -0.0279,-0.06201 l -0.36897,-0.945679 q -0.06511,-0.161231 -0.136426,-0.294556 -0.06821,-0.136426 -0.164331,-0.232544 -0.09612,-0.09922 -0.226343,-0.151928 -0.130224,-0.05581 -0.313159,-0.05581 h -0.356567 v 1.708423 q 0,0.02481 -0.0155,0.04341 -0.0124,0.0186 -0.04341,0.03101 -0.02791,0.0093 -0.08062,0.0155 -0.04961,0.0093 -0.127124,0.0093 -0.07752,0 -0.130225,-0.0093 -0.04961,-0.0062 -0.08062,-0.0155 -0.03101,-0.0124 -0.04341,-0.03101 -0.0124,-0.0186 -0.0124,-0.04341 v -3.714502 q 0,-0.120923 0.06201,-0.167432 0.06511,-0.04961 0.136426,-0.04961 h 0.852661 q 0.151928,0 0.251147,0.0093 0.10232,0.0062 0.182935,0.0155 0.232544,0.04031 0.409277,0.127124 0.179834,0.08682 0.300757,0.220141 0.120923,0.133325 0.179834,0.306958 0.06201,0.170532 0.06201,0.378272 0,0.201538 -0.05581,0.362768 -0.05271,0.15813 -0.155029,0.282154 -0.10232,0.120922 -0.244947,0.210839 -0.142626,0.08992 -0.31936,0.151929 0.09922,0.04341 0.179834,0.111621 0.08061,0.06511 0.148828,0.15813 0.07131,0.09302 0.133325,0.213941 0.06201,0.120922 0.124024,0.272851 l 0.359668,0.883667 q 0.04341,0.111621 0.05581,0.15813 0.0124,0.04341 0.0124,0.06821 z m -0.803052,-2.818433 q 0,-0.235644 -0.10542,-0.396875 -0.10542,-0.164331 -0.353467,-0.235644 -0.07751,-0.0217 -0.176733,-0.03101 -0.09612,-0.0093 -0.254248,-0.0093 h -0.449585 v 1.351856 h 0.520898 q 0.21084,0 0.362769,-0.04961 0.155029,-0.05271 0.257348,-0.142627 0.10232,-0.09302 0.148829,-0.217041 0.04961,-0.124023 0.04961,-0.269751 z"
|
||||
style="stroke-width:0.054"
|
||||
id="path4967" />
|
||||
<path
|
||||
d="m 86.850233,94.851764 q 0,0.120923 -0.06201,0.173633 -0.05891,0.04961 -0.136426,0.04961 H 84.82245 q 0,0.232544 0.04651,0.418579 0.04651,0.186035 0.155029,0.319361 0.108521,0.133325 0.282154,0.204638 0.173633,0.07131 0.42478,0.07131 0.198437,0 0.353467,-0.03101 0.155029,-0.03411 0.26665,-0.07441 0.114722,-0.04031 0.186035,-0.07131 0.07441,-0.03411 0.111621,-0.03411 0.0217,0 0.03721,0.0124 0.0186,0.0093 0.02791,0.03101 0.0093,0.0217 0.0124,0.06201 0.0062,0.03721 0.0062,0.09302 0,0.04031 -0.0031,0.07131 -0.0031,0.02791 -0.0093,0.05271 -0.0031,0.0217 -0.0155,0.04031 -0.0093,0.0186 -0.0279,0.03721 -0.0155,0.0155 -0.09922,0.05581 -0.08372,0.03721 -0.217041,0.07441 -0.133325,0.03721 -0.310059,0.06511 -0.173633,0.03101 -0.37207,0.03101 -0.344165,0 -0.604614,-0.09612 -0.257349,-0.09612 -0.434082,-0.285254 -0.176734,-0.189136 -0.266651,-0.474389 -0.08992,-0.285254 -0.08992,-0.663526 0,-0.359668 0.09302,-0.644922 0.09302,-0.288354 0.26665,-0.486792 0.176734,-0.201538 0.424781,-0.306958 0.248046,-0.10852 0.555004,-0.10852 0.328663,0 0.558106,0.10542 0.232544,0.10542 0.381372,0.285254 0.148828,0.176733 0.217041,0.418579 0.07131,0.238745 0.07131,0.511596 z m -0.514697,-0.151929 q 0.0093,-0.403076 -0.179834,-0.632519 -0.186035,-0.229444 -0.555005,-0.229444 -0.189135,0 -0.331762,0.07131 -0.142627,0.07131 -0.238745,0.189136 -0.09612,0.117822 -0.148829,0.275952 -0.05271,0.155029 -0.05891,0.325561 z"
|
||||
style="stroke-width:0.054"
|
||||
id="path4969" />
|
||||
<path
|
||||
d="m 89.380312,95.608307 q 0,0.21394 -0.08062,0.381372 -0.07751,0.167432 -0.223242,0.282153 -0.145727,0.114722 -0.347265,0.173633 -0.201539,0.05891 -0.443384,0.05891 -0.148828,0 -0.285254,-0.0248 -0.133325,-0.02171 -0.241846,-0.05581 -0.10542,-0.03721 -0.179834,-0.07441 -0.07441,-0.04031 -0.10852,-0.07131 -0.03411,-0.031 -0.04961,-0.08682 -0.0155,-0.05581 -0.0155,-0.151929 0,-0.05891 0.0062,-0.09922 0.0062,-0.04031 0.0155,-0.06511 0.0093,-0.0248 0.02481,-0.03411 0.0186,-0.0124 0.04031,-0.0124 0.03411,0 0.09922,0.04341 0.06821,0.04031 0.164332,0.08992 0.09922,0.04961 0.232543,0.09302 0.133326,0.04031 0.306958,0.04031 0.130225,0 0.235645,-0.02791 0.10542,-0.02791 0.182935,-0.08062 0.07751,-0.05581 0.117822,-0.139526 0.04341,-0.08372 0.04341,-0.198437 0,-0.117823 -0.06201,-0.198438 -0.05891,-0.08061 -0.15813,-0.142627 -0.09922,-0.06201 -0.223242,-0.10852 -0.124023,-0.04961 -0.257348,-0.10232 -0.130225,-0.05271 -0.257349,-0.117822 -0.124023,-0.06821 -0.223242,-0.164331 -0.09922,-0.09612 -0.161231,-0.229443 -0.05891,-0.133326 -0.05891,-0.319361 0,-0.164331 0.06201,-0.313159 0.06511,-0.151929 0.192236,-0.26355 0.127124,-0.114721 0.31626,-0.182934 0.192236,-0.06821 0.446484,-0.06821 0.111621,0 0.223243,0.0186 0.111621,0.0186 0.201538,0.04651 0.08992,0.0279 0.151928,0.06201 0.06511,0.03101 0.09612,0.05581 0.03411,0.02481 0.04341,0.04341 0.0124,0.0186 0.0155,0.04341 0.0062,0.0217 0.0093,0.05581 0.0062,0.03411 0.0062,0.08372 0,0.05271 -0.0062,0.09302 -0.0031,0.03721 -0.0155,0.06201 -0.0093,0.02481 -0.0248,0.03721 -0.0155,0.0093 -0.03411,0.0093 -0.02791,0 -0.08062,-0.03411 -0.05271,-0.03411 -0.136426,-0.07131 -0.08371,-0.04031 -0.198437,-0.07441 -0.111621,-0.03411 -0.257349,-0.03411 -0.130224,0 -0.229443,0.03101 -0.09922,0.0279 -0.164331,0.08371 -0.06201,0.05271 -0.09612,0.127124 -0.03101,0.07442 -0.03101,0.161231 0,0.120923 0.06201,0.204639 0.06201,0.08061 0.16123,0.142627 0.09922,0.06201 0.226343,0.111621 0.127124,0.04961 0.257348,0.102319 0.133326,0.05271 0.26045,0.117822 0.130224,0.06511 0.229443,0.15813 0.09922,0.09302 0.15813,0.223242 0.06201,0.130225 0.06201,0.310059 z"
|
||||
style="stroke-width:0.054"
|
||||
id="path4971" />
|
||||
<path
|
||||
d="m 92.4933,94.851764 q 0,0.120923 -0.06201,0.173633 -0.05891,0.04961 -0.136426,0.04961 h -1.829346 q 0,0.232544 0.04651,0.418579 0.04651,0.186035 0.15503,0.319361 0.10852,0.133325 0.282153,0.204638 0.173633,0.07131 0.42478,0.07131 0.198438,0 0.353467,-0.03101 0.155029,-0.03411 0.26665,-0.07441 0.114722,-0.04031 0.186035,-0.07131 0.07442,-0.03411 0.111622,-0.03411 0.0217,0 0.03721,0.0124 0.0186,0.0093 0.02791,0.03101 0.0093,0.0217 0.0124,0.06201 0.0062,0.03721 0.0062,0.09302 0,0.04031 -0.0031,0.07131 -0.0031,0.02791 -0.0093,0.05271 -0.0031,0.0217 -0.0155,0.04031 -0.0093,0.0186 -0.02791,0.03721 -0.0155,0.0155 -0.09922,0.05581 -0.08372,0.03721 -0.217041,0.07441 -0.133325,0.03721 -0.310059,0.06511 -0.173632,0.03101 -0.37207,0.03101 -0.344165,0 -0.604614,-0.09612 -0.257349,-0.09612 -0.434082,-0.285254 -0.176734,-0.189136 -0.26665,-0.474389 -0.08992,-0.285254 -0.08992,-0.663526 0,-0.359668 0.09302,-0.644922 0.09302,-0.288354 0.26665,-0.486792 0.176734,-0.201538 0.424781,-0.306958 0.248047,-0.10852 0.555005,-0.10852 0.328662,0 0.558105,0.10542 0.232544,0.10542 0.381372,0.285254 0.148828,0.176733 0.217041,0.418579 0.07131,0.238745 0.07131,0.511596 z m -0.514698,-0.151929 q 0.0093,-0.403076 -0.179834,-0.632519 -0.186035,-0.229444 -0.555004,-0.229444 -0.189136,0 -0.331763,0.07131 -0.142627,0.07131 -0.238745,0.189136 -0.09612,0.117822 -0.148828,0.275952 -0.05271,0.155029 -0.05891,0.325561 z"
|
||||
style="stroke-width:0.054"
|
||||
id="path4973" />
|
||||
<path
|
||||
d="m 95.373744,96.371051 q 0,0.03721 -0.02481,0.05581 -0.0248,0.0186 -0.06821,0.02791 -0.04341,0.0093 -0.127124,0.0093 -0.08062,0 -0.130225,-0.0093 -0.04651,-0.0093 -0.06821,-0.02791 -0.0217,-0.0186 -0.0217,-0.05581 v -0.279053 q -0.182935,0.195337 -0.409277,0.303858 -0.223243,0.10852 -0.47439,0.10852 -0.220142,0 -0.399976,-0.05891 -0.176733,-0.05581 -0.303857,-0.164331 -0.124023,-0.10852 -0.195337,-0.26665 -0.06821,-0.15813 -0.06821,-0.359668 0,-0.235645 0.09612,-0.409278 0.09612,-0.173632 0.275952,-0.288354 0.179834,-0.114722 0.440284,-0.170532 0.260449,-0.05891 0.58601,-0.05891 h 0.384473 v -0.217042 q 0,-0.16123 -0.03411,-0.285253 -0.03411,-0.124024 -0.111621,-0.204639 -0.07442,-0.08372 -0.195337,-0.124024 -0.120923,-0.04341 -0.297657,-0.04341 -0.189135,0 -0.341064,0.04651 -0.148828,0.04341 -0.26355,0.09922 -0.111621,0.05271 -0.189136,0.09922 -0.07441,0.04341 -0.111621,0.04341 -0.0248,0 -0.04341,-0.0124 -0.0186,-0.0124 -0.03411,-0.03721 -0.0124,-0.0248 -0.0186,-0.06201 -0.0062,-0.04031 -0.0062,-0.08682 0,-0.07751 0.0093,-0.120923 0.0124,-0.04651 0.05271,-0.08682 0.04341,-0.04031 0.145727,-0.09302 0.10232,-0.05581 0.235645,-0.09922 0.133325,-0.04651 0.291455,-0.07441 0.15813,-0.03101 0.31936,-0.03101 0.300757,0 0.511597,0.06821 0.21084,0.06821 0.341064,0.201538 0.130225,0.130224 0.189136,0.325561 0.05891,0.195337 0.05891,0.455786 z M 94.865248,95.09671 h -0.437183 q -0.210839,0 -0.365869,0.03721 -0.155029,0.03411 -0.257348,0.10542 -0.10232,0.06821 -0.151929,0.167432 -0.04651,0.09612 -0.04651,0.223242 0,0.217041 0.136426,0.347266 0.139526,0.127124 0.387573,0.127124 0.201538,0 0.372071,-0.10232 0.173632,-0.102319 0.362768,-0.313159 z"
|
||||
style="stroke-width:0.054"
|
||||
id="path4975" />
|
||||
<path
|
||||
d="m 97.94413,93.760358 q 0,0.06821 -0.0031,0.114721 -0.0031,0.04651 -0.0124,0.07441 -0.0093,0.02481 -0.02481,0.04031 -0.0124,0.0124 -0.03721,0.0124 -0.02481,0 -0.06201,-0.0124 -0.03411,-0.0155 -0.08061,-0.02791 -0.04341,-0.0155 -0.09922,-0.0279 -0.05581,-0.0124 -0.120922,-0.0124 -0.07751,0 -0.151929,0.03101 -0.07441,0.03101 -0.15813,0.102319 -0.08061,0.07131 -0.170532,0.189136 -0.08992,0.117822 -0.198438,0.288355 v 1.835546 q 0,0.02481 -0.0124,0.04341 -0.0124,0.0155 -0.04031,0.0279 -0.02791,0.0124 -0.07751,0.0186 -0.04961,0.0062 -0.127124,0.0062 -0.07441,0 -0.124024,-0.0062 -0.04961,-0.0062 -0.08061,-0.0186 -0.02791,-0.0124 -0.04031,-0.0279 -0.0093,-0.0186 -0.0093,-0.04341 v -2.790527 q 0,-0.02481 0.0093,-0.04031 0.0093,-0.0186 0.03721,-0.03101 0.02791,-0.0155 0.07131,-0.0186 0.04341,-0.0062 0.114721,-0.0062 0.06821,0 0.111622,0.0062 0.04651,0.0031 0.07131,0.0186 0.02481,0.0124 0.03411,0.03101 0.0124,0.0155 0.0124,0.04031 V 93.9836 q 0.114722,-0.167432 0.21394,-0.272852 0.10232,-0.10542 0.192237,-0.164331 0.08992,-0.06201 0.176733,-0.08372 0.08992,-0.0248 0.179834,-0.0248 0.04031,0 0.08992,0.0062 0.05271,0.0031 0.10852,0.0155 0.05581,0.0124 0.09922,0.0279 0.04651,0.0155 0.06511,0.03101 0.0186,0.0155 0.0248,0.03101 0.0062,0.0124 0.0093,0.03411 0.0062,0.0217 0.0062,0.06511 0.0031,0.04031 0.0031,0.111621 z"
|
||||
style="stroke-width:0.054"
|
||||
id="path4977" />
|
||||
<path
|
||||
d="m 100.4401,95.94317 q 0,0.05271 -0.003,0.09302 -0.003,0.03721 -0.0124,0.06511 -0.006,0.02481 -0.0186,0.04651 -0.009,0.0186 -0.0496,0.05891 -0.0372,0.03721 -0.13023,0.09612 -0.093,0.05581 -0.21084,0.102319 -0.11472,0.04341 -0.251146,0.07131 -0.136426,0.0279 -0.282153,0.0279 -0.300757,0 -0.533301,-0.09922 -0.232544,-0.09922 -0.390674,-0.288354 -0.155029,-0.192236 -0.238745,-0.468188 -0.08061,-0.279053 -0.08061,-0.641822 0,-0.412378 0.09922,-0.706933 0.10232,-0.297657 0.275952,-0.486792 0.176734,-0.189136 0.412378,-0.279053 0.238746,-0.09302 0.514698,-0.09302 0.133325,0 0.257348,0.02481 0.127124,0.02481 0.232543,0.06511 0.10542,0.04031 0.18604,0.09302 0.0837,0.05271 0.12092,0.08992 0.0372,0.03721 0.0496,0.05891 0.0155,0.0217 0.0248,0.05271 0.009,0.02791 0.0124,0.06511 0.003,0.03721 0.003,0.09302 0,0.120923 -0.0279,0.170532 -0.0279,0.04651 -0.0682,0.04651 -0.0465,0 -0.10852,-0.04961 -0.0589,-0.05271 -0.15193,-0.114722 -0.09302,-0.06201 -0.226344,-0.111621 -0.130225,-0.05271 -0.310058,-0.05271 -0.36897,0 -0.567408,0.285254 -0.195337,0.282153 -0.195337,0.821655 0,0.269751 0.04961,0.47439 0.05271,0.201538 0.151928,0.337963 0.09922,0.136426 0.241846,0.204639 0.145728,0.06511 0.331763,0.06511 0.176733,0 0.310058,-0.05581 0.133326,-0.05581 0.229442,-0.120923 0.0992,-0.06821 0.16434,-0.120923 0.0682,-0.05581 0.10542,-0.05581 0.0217,0 0.0372,0.0124 0.0155,0.0124 0.0248,0.04341 0.0124,0.02791 0.0155,0.07441 0.006,0.04341 0.006,0.10542 z"
|
||||
style="stroke-width:0.054"
|
||||
id="path4979" />
|
||||
<path
|
||||
d="m 103.52208,96.36795 q 0,0.02481 -0.0124,0.04341 -0.0124,0.0155 -0.0403,0.0279 -0.0279,0.0124 -0.0775,0.0186 -0.0496,0.0062 -0.12402,0.0062 -0.0775,0 -0.12713,-0.0062 -0.0496,-0.0062 -0.0775,-0.0186 -0.0279,-0.0124 -0.0403,-0.0279 -0.0124,-0.0186 -0.0124,-0.04341 v -1.634008 q 0,-0.238745 -0.0372,-0.384473 -0.0372,-0.145728 -0.10852,-0.251147 -0.0713,-0.10542 -0.18603,-0.161231 -0.11163,-0.05581 -0.26045,-0.05581 -0.19224,0 -0.38448,0.136425 -0.19223,0.136426 -0.40307,0.399976 v 1.950268 q 0,0.02481 -0.0124,0.04341 -0.0124,0.0155 -0.0403,0.0279 -0.0279,0.0124 -0.0775,0.0186 -0.0496,0.0062 -0.12712,0.0062 -0.0744,0 -0.12402,-0.0062 -0.0496,-0.0062 -0.0806,-0.0186 -0.0279,-0.0124 -0.0403,-0.0279 -0.009,-0.0186 -0.009,-0.04341 v -4.142382 q 0,-0.02481 0.009,-0.04341 0.0124,-0.0186 0.0403,-0.031 0.031,-0.0124 0.0806,-0.0186 0.0496,-0.0062 0.12402,-0.0062 0.0775,0 0.12712,0.0062 0.0496,0.0062 0.0775,0.0186 0.0279,0.0124 0.0403,0.031 0.0124,0.0186 0.0124,0.04341 v 1.671215 q 0.22014,-0.232543 0.44338,-0.344165 0.22324,-0.114721 0.44959,-0.114721 0.27905,0 0.46818,0.09612 0.19224,0.09302 0.31006,0.251147 0.11783,0.15813 0.16743,0.372071 0.0527,0.21084 0.0527,0.511596 z"
|
||||
style="stroke-width:0.054"
|
||||
id="path4981" />
|
||||
</g>
|
||||
</g> -->
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:6.1311px;font-family:Calibri;-inkscape-font-specification:Calibri;fill:#f9f9f9;stroke:#00000f;stroke-width:0.0586557;stroke-miterlimit:1"
|
||||
x="27.642879"
|
||||
y="5.8916035"
|
||||
id="text1590"><tspan
|
||||
sodipodi:role="line"
|
||||
id="tspan1588"
|
||||
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:6.1311px;font-family:Calibri;-inkscape-font-specification:Calibri;fill:#f9f9f9;stroke-width:0.0586558"
|
||||
x="27.642879"
|
||||
y="5.8916035">MI Trace View</tspan></text>
|
||||
</g>
|
||||
</svg>
|
||||
|
Za Šířka: | Výška: | Velikost: 20 KiB |
@@ -0,0 +1,137 @@
|
||||
h2,h3,h4 {
|
||||
text-align: center;
|
||||
font-family: Calibri, Candara, Optima, Arial, 'Trebuchet MS', sans-serif;
|
||||
}
|
||||
|
||||
ul, ol {
|
||||
list-style-type: none;
|
||||
padding: 0;
|
||||
margin: 20px 5px;
|
||||
/*font-family: Calibri, Candara, Segoe, "Segoe UI", Optima, Arial, sans-serif; font-size: 15px; font-style: normal; font-variant: normal; */
|
||||
font-family: 'Courier New', monospace; font-size: 15px; font-style: normal;
|
||||
}
|
||||
|
||||
a {
|
||||
color: royalblue;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
div#flexbox {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
div#flexbox > div {
|
||||
flex: 50%;
|
||||
}
|
||||
|
||||
div#minimap {
|
||||
position: absolute;
|
||||
width: 350px;
|
||||
top: 340px;
|
||||
display: flex;
|
||||
justify-content: right;
|
||||
right: 5px;
|
||||
}
|
||||
|
||||
div#wave, div#cu_wave {
|
||||
overflow:scroll;
|
||||
overflow-y:hidden;
|
||||
}
|
||||
|
||||
div#ma_code {
|
||||
height: calc(100vh - 180px);
|
||||
overflow: auto;
|
||||
overflow-x: hidden;
|
||||
display: block;
|
||||
margin: 20px;
|
||||
}
|
||||
|
||||
nav {
|
||||
padding-right: 8px;
|
||||
position: absolute;
|
||||
top: 1rem;
|
||||
right: 1rem;
|
||||
background: #FFFDE3;
|
||||
z-index: 5;
|
||||
margin-left: 10px;
|
||||
width:330px;
|
||||
}
|
||||
|
||||
.highlight {
|
||||
background-color: lightgray;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.clickable {
|
||||
cursor: pointer;
|
||||
font-weight: 180;
|
||||
}
|
||||
|
||||
ul li {
|
||||
border-bottom: 1px dotted black;
|
||||
}
|
||||
|
||||
.tooltip {
|
||||
display: none;
|
||||
position: relative;
|
||||
left: 10px;
|
||||
z-index: 100;
|
||||
background: #333;
|
||||
border: 1px solid #c0c0c0;
|
||||
opacity: 0.8;
|
||||
color: white;
|
||||
}
|
||||
li:hover .tooltip {
|
||||
display: inline-block; /*block;*/
|
||||
}
|
||||
.loop {
|
||||
margin-left: 30px;
|
||||
background: lightseagreen;
|
||||
}
|
||||
|
||||
.btn {
|
||||
border: 1px solid black;
|
||||
background-color: #D7D7D7;
|
||||
color: black;
|
||||
padding: 3px 4px;
|
||||
font-size: 14px;
|
||||
cursor: pointer;
|
||||
border-style: ridge;
|
||||
border-radius: 6px;
|
||||
}
|
||||
|
||||
.btn:hover {
|
||||
color: blue;
|
||||
}
|
||||
|
||||
.dropbtn {
|
||||
border: 2px solid black;
|
||||
background-color: #D7D7D7;
|
||||
color: black;
|
||||
padding: 3px 4px;
|
||||
font-size: 15px;
|
||||
cursor: pointer;
|
||||
border-style: ridge;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.dropbtn:hover, .dropbtn:focus {
|
||||
color: blue;
|
||||
}
|
||||
|
||||
.dropdown {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.dropdown-content {
|
||||
display: none;
|
||||
position: absolute;
|
||||
background-color: #e0e0f0;
|
||||
min-width: 10px;
|
||||
box-shadow: 0px 8px 16px 0px rgba(0,0,0.1,0.1);
|
||||
z-index: 1;
|
||||
}
|
||||
|
||||
.dropdown-content a:hover {background-color: #ddd;}
|
||||
.show {display:inline-flex;}
|
||||
@@ -0,0 +1,50 @@
|
||||
# ###############################################################################
|
||||
# # 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.
|
||||
# ###############################################################################
|
||||
|
||||
file(GLOB ROCPROFILER_UTIL_SRC_FILES ${PROJECT_SOURCE_DIR}/src/utils/helper.cpp)
|
||||
|
||||
file(GLOB CLI_SOURCES "*.cpp")
|
||||
add_library(cli_plugin SHARED ${CLI_SOURCES} ${ROCPROFILER_UTIL_SRC_FILES})
|
||||
|
||||
set_target_properties(
|
||||
cli_plugin
|
||||
PROPERTIES CXX_VISIBILITY_PRESET hidden
|
||||
LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/../exportmap
|
||||
LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib/rocprofiler
|
||||
INSTALL_RPATH "${ROCM_APPEND_PRIVLIB_RPATH}")
|
||||
|
||||
target_compile_definitions(cli_plugin PRIVATE HIP_PROF_HIP_API_STRING=1
|
||||
__HIP_PLATFORM_AMD__=1)
|
||||
|
||||
target_include_directories(cli_plugin PRIVATE ${PROJECT_SOURCE_DIR})
|
||||
|
||||
target_link_options(
|
||||
cli_plugin PRIVATE -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/../exportmap
|
||||
-Wl,--no-undefined)
|
||||
|
||||
target_link_libraries(cli_plugin PRIVATE rocprofiler-v2 hsa-runtime64::hsa-runtime64
|
||||
stdc++fs atomic amd_comgr dl)
|
||||
|
||||
install(TARGETS cli_plugin LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}
|
||||
COMPONENT asan)
|
||||
install(TARGETS cli_plugin LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}
|
||||
COMPONENT runtime)
|
||||
@@ -0,0 +1,418 @@
|
||||
/* 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 <atomic>
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <hsa/hsa.h>
|
||||
#include <mutex>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "rocprofiler.h"
|
||||
#include "rocprofiler_plugin.h"
|
||||
#include "../utils.h"
|
||||
|
||||
#include "src/utils/filesystem.hpp"
|
||||
|
||||
namespace fs = rocprofiler::common::filesystem;
|
||||
|
||||
namespace {
|
||||
|
||||
static std::string output_file_name;
|
||||
class file_plugin_t {
|
||||
private:
|
||||
enum class output_type_t { COUNTER, TRACER, PC_SAMPLING };
|
||||
|
||||
class output_file_t {
|
||||
public:
|
||||
output_file_t(std::string name) : name_(std::move(name)) {}
|
||||
|
||||
std::string name() const { return name_; }
|
||||
|
||||
template <typename T> std::ostream& operator<<(T&& value) {
|
||||
if (!is_open()) open();
|
||||
return stream_ << std::forward<T>(value);
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& (*func)(std::ostream&)) {
|
||||
if (!is_open()) open();
|
||||
return stream_ << func;
|
||||
}
|
||||
|
||||
void open() {
|
||||
// If the stream is already in the failed state, there's no need to try
|
||||
// to open the file.
|
||||
if (fail()) return;
|
||||
|
||||
const char* output_dir = getenv("OUTPUT_PATH");
|
||||
output_file_name = getenv("OUT_FILE_NAME") ? std::string(getenv("OUT_FILE_NAME")) + "_" : "";
|
||||
|
||||
if (output_dir == nullptr && getenv("OUT_FILE_NAME") == nullptr) {
|
||||
stream_.copyfmt(std::cout);
|
||||
stream_.clear(std::cout.rdstate());
|
||||
stream_.basic_ios<char>::rdbuf(std::cout.rdbuf());
|
||||
return;
|
||||
}
|
||||
if (output_dir == nullptr) output_dir = "./";
|
||||
|
||||
fs::path output_prefix(output_dir);
|
||||
if (!fs::is_directory(fs::status(output_prefix))) {
|
||||
if (!stream_.fail()) rocprofiler::warning("Cannot open output directory '%s'", output_dir);
|
||||
stream_.setstate(std::ios_base::failbit);
|
||||
return;
|
||||
}
|
||||
|
||||
std::stringstream ss;
|
||||
ss << output_file_name << GetPid() << "_" << name_;
|
||||
stream_.open(output_prefix / ss.str());
|
||||
}
|
||||
|
||||
bool is_open() const { return stream_.is_open(); }
|
||||
bool fail() const { return stream_.fail(); }
|
||||
|
||||
private:
|
||||
const std::string name_;
|
||||
std::ofstream stream_;
|
||||
};
|
||||
|
||||
output_file_t* get_output_file(output_type_t output_type, uint32_t domain = 0) {
|
||||
switch (output_type) {
|
||||
case output_type_t::COUNTER:
|
||||
return &output_file_;
|
||||
case output_type_t::TRACER:
|
||||
switch (domain) {
|
||||
case ACTIVITY_DOMAIN_ROCTX:
|
||||
return &roctx_file_;
|
||||
case ACTIVITY_DOMAIN_HSA_API:
|
||||
return &hsa_api_file_;
|
||||
case ACTIVITY_DOMAIN_HIP_API:
|
||||
return &hip_api_file_;
|
||||
case ACTIVITY_DOMAIN_HIP_OPS:
|
||||
return &hip_activity_file_;
|
||||
case ACTIVITY_DOMAIN_HSA_OPS:
|
||||
return &hsa_async_copy_file_;
|
||||
default:
|
||||
assert(!"domain/op not supported!");
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case output_type_t::PC_SAMPLING:
|
||||
return &pc_sample_file_;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
public:
|
||||
file_plugin_t() {
|
||||
valid_ = true;
|
||||
kernel_filters_ = GetKernelFilters();
|
||||
}
|
||||
|
||||
std::mutex writing_lock;
|
||||
|
||||
const char* GetDomainName(rocprofiler_tracer_activity_domain_t domain) {
|
||||
switch (domain) {
|
||||
case ACTIVITY_DOMAIN_ROCTX:
|
||||
return "ROCTX_DOMAIN";
|
||||
break;
|
||||
case ACTIVITY_DOMAIN_HIP_API:
|
||||
return "HIP_API_DOMAIN";
|
||||
break;
|
||||
case ACTIVITY_DOMAIN_HIP_OPS:
|
||||
return "HIP_OPS_DOMAIN";
|
||||
break;
|
||||
case ACTIVITY_DOMAIN_HSA_API:
|
||||
return "HSA_API_DOMAIN";
|
||||
break;
|
||||
case ACTIVITY_DOMAIN_HSA_OPS:
|
||||
return "HSA_OPS_DOMAIN";
|
||||
break;
|
||||
case ACTIVITY_DOMAIN_HSA_EVT:
|
||||
return "HSA_EVT_DOMAIN";
|
||||
break;
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
void FlushTracerRecord(rocprofiler_record_tracer_t tracer_record,
|
||||
rocprofiler_session_id_t session_id,
|
||||
rocprofiler_buffer_id_t buffer_id = rocprofiler_buffer_id_t{0}) {
|
||||
std::lock_guard<std::mutex> lock(writing_lock);
|
||||
if (tracer_record.timestamps.end.value <= 0 && tracer_record.domain != ACTIVITY_DOMAIN_ROCTX)
|
||||
return;
|
||||
const char* operation_name_c = nullptr;
|
||||
// ROCTX domain Operation ID doesn't have a name
|
||||
// It depends on the user input of the roctx functions.
|
||||
// ROCTX message is the tracer_record.name
|
||||
if (tracer_record.domain != ACTIVITY_DOMAIN_ROCTX) {
|
||||
CHECK_ROCPROFILER(rocprofiler_query_tracer_operation_name(
|
||||
tracer_record.domain, tracer_record.operation_id, &operation_name_c));
|
||||
}
|
||||
|
||||
output_file_t* output_file = get_output_file(output_type_t::TRACER, tracer_record.domain);
|
||||
*output_file << "Domain(" << GetDomainName(tracer_record.domain) << "), ";
|
||||
if (operation_name_c)
|
||||
*output_file << ((tracer_record.domain == ACTIVITY_DOMAIN_HIP_API ||
|
||||
tracer_record.domain == ACTIVITY_DOMAIN_HSA_API)
|
||||
? "Function("
|
||||
: "Operation_Name(")
|
||||
<< operation_name_c << "), ";
|
||||
if (tracer_record.name && tracer_record.domain != ACTIVITY_DOMAIN_ROCTX)
|
||||
*output_file << "Kernel_Name("
|
||||
<< rocprofiler::truncate_name(rocprofiler::cxx_demangle(tracer_record.name))
|
||||
<< "), ";
|
||||
if (tracer_record.domain != ACTIVITY_DOMAIN_ROCTX) {
|
||||
*output_file << "Start_Timestamp(" << tracer_record.timestamps.begin.value << "), "
|
||||
<< "End_Timestamp(" << tracer_record.timestamps.end.value << "), "
|
||||
<< "Correlation_ID(" << tracer_record.correlation_id.value << ")";
|
||||
} else {
|
||||
*output_file << "Timestamp(" << tracer_record.timestamps.begin.value << "), ";
|
||||
}
|
||||
if (tracer_record.domain == ACTIVITY_DOMAIN_ROCTX && tracer_record.external_id.id >= 0)
|
||||
*output_file << "ROCTX_ID(" << tracer_record.external_id.id << "), ";
|
||||
if (tracer_record.name && tracer_record.domain == ACTIVITY_DOMAIN_ROCTX)
|
||||
*output_file << "ROCTX_Message(" << tracer_record.name << ")";
|
||||
*output_file << std::endl;
|
||||
}
|
||||
|
||||
void FlushProfilerRecord(const rocprofiler_record_profiler_t* profiler_record,
|
||||
rocprofiler_session_id_t session_id, rocprofiler_buffer_id_t buffer_id) {
|
||||
auto check_filter_cache = [&](const char * k_name) {
|
||||
auto str = std::string{k_name};
|
||||
auto f = kernel_filter_cache_.find(k_name);
|
||||
if (f == kernel_filter_cache_.end()) {
|
||||
bool found_match = false;
|
||||
for (const auto& filter : kernel_filters_) {
|
||||
if (str.find(filter) != std::string::npos) {
|
||||
found_match = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
f = kernel_filter_cache_.emplace(str, found_match).first;
|
||||
}
|
||||
return f->second;
|
||||
};
|
||||
|
||||
std::lock_guard<std::mutex> lock(writing_lock);
|
||||
size_t name_length = 0;
|
||||
output_file_t* output_file{nullptr};
|
||||
output_file = get_output_file(output_type_t::COUNTER);
|
||||
CHECK_ROCPROFILER(rocprofiler_query_kernel_info_size(ROCPROFILER_KERNEL_NAME,
|
||||
profiler_record->kernel_id, &name_length));
|
||||
// Taken from rocprofiler: The size hasn't changed in recent past
|
||||
static const uint32_t lds_block_size = 128 * 4;
|
||||
const char* kernel_name_c = nullptr;
|
||||
if (name_length > 1) {
|
||||
CHECK_ROCPROFILER(rocprofiler_query_kernel_info(ROCPROFILER_KERNEL_NAME,
|
||||
profiler_record->kernel_id, &kernel_name_c));
|
||||
if (kernel_name_c != nullptr && !kernel_filters_.empty() && !check_filter_cache(kernel_name_c)) {
|
||||
free(const_cast<char*>(kernel_name_c));
|
||||
return;
|
||||
}
|
||||
}
|
||||
*output_file << "Dispatch_ID(" << std::to_string(profiler_record->header.id.handle) << "), "
|
||||
<< "GPU_ID(" << std::to_string(profiler_record->gpu_id.handle) << "), "
|
||||
<< "Queue_ID(" << std::to_string(profiler_record->queue_id.handle) << "), "
|
||||
<< "Process_ID(" << std::to_string(GetPid()) << "), "
|
||||
<< "Thread_ID(" << std::to_string(profiler_record->thread_id.value) << "), "
|
||||
<< "Grid_Size(" << std::to_string(profiler_record->kernel_properties.grid_size)
|
||||
<< "), "
|
||||
<< "Workgroup_Size("
|
||||
<< std::to_string(profiler_record->kernel_properties.workgroup_size) << "), "
|
||||
<< "LDS_Per_Workgroup("
|
||||
<< std::to_string(
|
||||
((profiler_record->kernel_properties.lds_size + (lds_block_size - 1)) &
|
||||
~(lds_block_size - 1)))
|
||||
<< "), "
|
||||
<< "Scratch_Per_Workitem("
|
||||
<< std::to_string(profiler_record->kernel_properties.scratch_size) << "), "
|
||||
<< "Arch_VGPR("
|
||||
<< std::to_string(profiler_record->kernel_properties.arch_vgpr_count) << "), "
|
||||
<< "Accum_VGPR("
|
||||
<< std::to_string(profiler_record->kernel_properties.accum_vgpr_count) << "), "
|
||||
<< "SGPR(" << std::to_string(profiler_record->kernel_properties.sgpr_count)
|
||||
<< "), "
|
||||
<< "Wave_Size(" << std::to_string(profiler_record->kernel_properties.wave_size);
|
||||
std::string kernel_name = "";
|
||||
if (name_length > 1) {
|
||||
kernel_name = rocprofiler::truncate_name(rocprofiler::cxx_demangle(kernel_name_c));
|
||||
std::string key = "\"";
|
||||
std::size_t found = kernel_name.rfind(key);
|
||||
while (found != std::string::npos) {
|
||||
kernel_name.replace(found, key.length(), "'");
|
||||
found = kernel_name.rfind(key, found - 1);
|
||||
}
|
||||
}
|
||||
*output_file << "), "
|
||||
<< "Kernel_Name(\"" << kernel_name << "\"), "
|
||||
<< "Begin_Timestamp(" << std::to_string(profiler_record->timestamps.begin.value)
|
||||
<< "), "
|
||||
<< "End_Timestamp(" << std::to_string(profiler_record->timestamps.end.value)
|
||||
<< "), "
|
||||
<< "Correlation_ID(" << std::to_string(profiler_record->correlation_id.value)
|
||||
<< ")";
|
||||
|
||||
// For Counters
|
||||
if (profiler_record->counters) {
|
||||
*output_file << ", ";
|
||||
for (uint64_t i = 0; i < profiler_record->counters_count.value; i++) {
|
||||
if (profiler_record->counters[i].counter_handler.handle > 0) {
|
||||
size_t counter_name_length = 0;
|
||||
CHECK_ROCPROFILER(rocprofiler_query_counter_info_size(
|
||||
session_id, ROCPROFILER_COUNTER_NAME, profiler_record->counters[i].counter_handler,
|
||||
&counter_name_length));
|
||||
if (counter_name_length > 1) {
|
||||
const char* name_c = nullptr;
|
||||
CHECK_ROCPROFILER(rocprofiler_query_counter_info(
|
||||
session_id, ROCPROFILER_COUNTER_NAME, profiler_record->counters[i].counter_handler,
|
||||
&name_c));
|
||||
*output_file << name_c << "("
|
||||
<< std::to_string(profiler_record->counters[i].value.value) << ")";
|
||||
if (i < profiler_record->counters_count.value - 1) *output_file << ", ";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*output_file << std::endl;
|
||||
if (kernel_name_c) {
|
||||
free(const_cast<char*>(kernel_name_c));
|
||||
}
|
||||
}
|
||||
|
||||
void FlushPCSamplingRecord(const rocprofiler_record_pc_sample_t* pc_sampling_record) {
|
||||
output_file_t* output_file{nullptr};
|
||||
output_file = get_output_file(output_type_t::PC_SAMPLING);
|
||||
const auto& sample = pc_sampling_record->pc_sample;
|
||||
*output_file << "dispatch[" << sample.dispatch_id.value << "], "
|
||||
<< "timestamp(" << sample.timestamp.value << "), "
|
||||
<< "gpu_id(" << sample.gpu_id.handle << "), "
|
||||
<< "pc-sample(" << std::hex << std::showbase << sample.pc << "), "
|
||||
<< "se(" << sample.se << ')' << std::endl;
|
||||
}
|
||||
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: {
|
||||
const rocprofiler_record_profiler_t* profiler_record =
|
||||
reinterpret_cast<const rocprofiler_record_profiler_t*>(begin);
|
||||
FlushProfilerRecord(profiler_record, session_id, buffer_id);
|
||||
break;
|
||||
}
|
||||
case ROCPROFILER_TRACER_RECORD: {
|
||||
rocprofiler_record_tracer_t* tracer_record = const_cast<rocprofiler_record_tracer_t*>(
|
||||
reinterpret_cast<const rocprofiler_record_tracer_t*>(begin));
|
||||
FlushTracerRecord(*tracer_record, session_id, buffer_id);
|
||||
break;
|
||||
}
|
||||
case ROCPROFILER_ATT_TRACER_RECORD: {
|
||||
break;
|
||||
}
|
||||
case ROCPROFILER_PC_SAMPLING_RECORD: {
|
||||
[[deprecated("PC Sampling is deprecated")]]
|
||||
const rocprofiler_record_pc_sample_t* pc_sampling_record =
|
||||
reinterpret_cast<const rocprofiler_record_pc_sample_t*>(begin);
|
||||
FlushPCSamplingRecord(pc_sampling_record);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
rocprofiler_next_record(begin, &begin, session_id, buffer_id);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool is_valid() const { return valid_; }
|
||||
|
||||
private:
|
||||
bool valid_{false};
|
||||
std::set<std::string> kernel_filters_;
|
||||
std::map<std::string, bool> kernel_filter_cache_;
|
||||
std::atomic<bool> tracer_header_written_{false};
|
||||
std::atomic<bool> profiler_header_written_{false};
|
||||
|
||||
output_file_t roctx_file_{"roctx_trace.txt"}, hsa_api_file_{"hsa_api_trace.txt"},
|
||||
hip_api_file_{"hip_api_trace.txt"}, hip_activity_file_{"hcc_ops_trace.txt"},
|
||||
hsa_async_copy_file_{"async_copy_trace.txt"}, pc_sample_file_{"pcs_trace.txt"},
|
||||
output_file_{"results.txt"};
|
||||
};
|
||||
|
||||
file_plugin_t* file_plugin = nullptr;
|
||||
|
||||
} // namespace
|
||||
|
||||
ROCPROFILER_EXPORT int rocprofiler_plugin_initialize(uint32_t rocprofiler_major_version,
|
||||
uint32_t rocprofiler_minor_version,
|
||||
void* data) {
|
||||
if (rocprofiler_major_version != ROCPROFILER_VERSION_MAJOR ||
|
||||
rocprofiler_minor_version < ROCPROFILER_VERSION_MINOR)
|
||||
return -1;
|
||||
|
||||
if (file_plugin != nullptr) return -1;
|
||||
|
||||
file_plugin = new file_plugin_t();
|
||||
if (file_plugin->is_valid()) return 0;
|
||||
|
||||
// The plugin failed to initialized, destroy it and return an error.
|
||||
delete file_plugin;
|
||||
file_plugin = nullptr;
|
||||
return -1;
|
||||
}
|
||||
|
||||
ROCPROFILER_EXPORT void rocprofiler_plugin_finalize() {
|
||||
if (!file_plugin) return;
|
||||
delete file_plugin;
|
||||
file_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 (!file_plugin || !file_plugin->is_valid()) return -1;
|
||||
return file_plugin->WriteBufferRecords(begin, end, session_id, buffer_id);
|
||||
}
|
||||
|
||||
ROCPROFILER_EXPORT int rocprofiler_plugin_write_record(rocprofiler_record_tracer_t record) {
|
||||
if (!file_plugin || !file_plugin->is_valid()) return -1;
|
||||
if (record.header.id.handle == 0) return 0;
|
||||
file_plugin->FlushTracerRecord(record, rocprofiler_session_id_t{0}, rocprofiler_buffer_id_t{0});
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
README.html
|
||||
@@ -0,0 +1,177 @@
|
||||
################################################################################
|
||||
# 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.
|
||||
################################################################################
|
||||
|
||||
# Plugin shared object.
|
||||
add_library(
|
||||
ctf_plugin SHARED
|
||||
ctf.cpp
|
||||
plugin.cpp
|
||||
barectf.c
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/barectf.h"
|
||||
${PROJECT_SOURCE_DIR}/src/utils/helper.cpp
|
||||
hsa_begin.cpp.i
|
||||
hsa_end.cpp.i
|
||||
hip_begin.cpp.i
|
||||
hip_end.cpp.i)
|
||||
set_target_properties(
|
||||
ctf_plugin
|
||||
PROPERTIES CXX_VISIBILITY_PRESET hidden
|
||||
LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/../exportmap"
|
||||
LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib/rocprofiler"
|
||||
INSTALL_RPATH "${ROCM_APPEND_PRIVLIB_RPATH}")
|
||||
set(METADATA_STREAM_FILE_DIR "${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/plugin/ctf")
|
||||
target_compile_definitions(
|
||||
ctf_plugin
|
||||
PUBLIC AMD_INTERNAL_BUILD
|
||||
PRIVATE HIP_PROF_HIP_API_STRING=1 __HIP_PLATFORM_AMD__=1
|
||||
CTF_PLUGIN_METADATA_FILE_PATH="${METADATA_STREAM_FILE_DIR}/metadata"
|
||||
CTF_PLUGIN_INSTALL_PREFIX="${CMAKE_INSTALL_PREFIX}")
|
||||
|
||||
# Disabling this as it causes CI to fail, we need to make sure rocm-core is installed
|
||||
# before rocprofiler Add definitions for getROCmInstallPath() API
|
||||
# if(USE_GET_ROCM_PATH_API) target_compile_definitions( ctf_plugin PRIVATE
|
||||
# USE_GET_ROCM_PATH_API=1) endif()
|
||||
|
||||
target_include_directories(
|
||||
ctf_plugin PRIVATE "${PROJECT_SOURCE_DIR}" "${CMAKE_BINARY_DIR}/src/api"
|
||||
"${CMAKE_CURRENT_BINARY_DIR}")
|
||||
target_link_options(
|
||||
ctf_plugin PRIVATE "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/../exportmap"
|
||||
-Wl,--no-undefined)
|
||||
target_link_libraries(ctf_plugin PRIVATE rocprofiler-v2 hsa-runtime64::hsa-runtime64
|
||||
stdc++fs dl)
|
||||
|
||||
# Disabling this as it causes CI to fail, we need to make sure rocm-core is installed
|
||||
# before rocprofiler Add link dependencies for getROCmInstallPath() API
|
||||
# if(USE_GET_ROCM_PATH_API) find_library(ROCM_CORE_LIB rocm-core HINTS ${ROCM_PATH}/lib)
|
||||
# target_link_libraries(ctf_plugin PRIVATE ${ROCM_CORE_LIB}) endif()
|
||||
|
||||
install(TARGETS ctf_plugin LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}"
|
||||
COMPONENT plugins)
|
||||
|
||||
# `gen_api_files.py` and `gen_env_yaml.py` require Python 3, CppHeaderParser, PyYAML, and
|
||||
# barectf.
|
||||
if(NOT Python3_EXECUTABLE)
|
||||
find_package(
|
||||
Python3
|
||||
COMPONENTS Interpreter
|
||||
REQUIRED)
|
||||
endif()
|
||||
|
||||
message("Python: ${Python3_EXECUTABLE})")
|
||||
|
||||
execute_process(COMMAND Python3::Interpreter -c "print('hello')")
|
||||
|
||||
function(check_py3_pkg pkg_name)
|
||||
execute_process(
|
||||
COMMAND "${Python3_EXECUTABLE}" -c "import ${pkg_name}"
|
||||
RESULT_VARIABLE PY3_IMPORT_RES
|
||||
OUTPUT_QUIET)
|
||||
|
||||
if(NOT (${PY3_IMPORT_RES} EQUAL 0))
|
||||
message(FATAL_ERROR "Cannot find Python 3 package `${pkg_name}`")
|
||||
endif()
|
||||
|
||||
message(STATUS "Found Python 3 package `${pkg_name}`")
|
||||
endfunction()
|
||||
|
||||
check_py3_pkg(CppHeaderParser)
|
||||
check_py3_pkg(yaml)
|
||||
find_program(BARECTF_RES barectf REQUIRED HINTS "$ENV{HOME}/.local/bin")
|
||||
|
||||
# Generate barectf YAML and C++ files for HSA API.
|
||||
get_property(
|
||||
HSA_RUNTIME_INCLUDE_DIRS
|
||||
TARGET hsa-runtime64::hsa-runtime64
|
||||
PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
|
||||
find_file(
|
||||
HSA_H hsa.h
|
||||
PATHS ${HSA_RUNTIME_INCLUDE_DIRS}
|
||||
PATH_SUFFIXES hsa
|
||||
NO_DEFAULT_PATH REQUIRED)
|
||||
get_filename_component(HSA_RUNTIME_INC_PATH "${HSA_H}" DIRECTORY)
|
||||
add_custom_command(
|
||||
OUTPUT hsa_erts.yaml hsa_begin.cpp.i hsa_end.cpp.i
|
||||
COMMAND ${CMAKE_C_COMPILER} -E "${HSA_RUNTIME_INC_PATH}/hsa.h" -o hsa.h.i
|
||||
COMMAND ${CMAKE_C_COMPILER} -E "${HSA_RUNTIME_INC_PATH}/hsa_ext_amd.h" -o
|
||||
hsa_ext_amd.h.i
|
||||
COMMAND ${CMAKE_COMMAND} -E cat hsa.h.i hsa_ext_amd.h.i
|
||||
"${CMAKE_BINARY_DIR}/src/api/hsa_prof_str.h" > hsa_input.h
|
||||
COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/gen_api_files.py" hsa
|
||||
hsa_input.h
|
||||
BYPRODUCTS hsa.h.i hsa_ext_amd.h.i hsa_input.h
|
||||
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/gen_api_files.py" "${HSA_RUNTIME_INC_PATH}/hsa.h"
|
||||
"${HSA_RUNTIME_INC_PATH}/hsa_ext_amd.h"
|
||||
"${CMAKE_BINARY_DIR}/src/api/hsa_prof_str.h"
|
||||
COMMENT "Generating HSA API files for the `ctf` plugin...")
|
||||
|
||||
# Generate barectf YAML and C++ files for HIP API.
|
||||
get_property(
|
||||
HIP_INCLUDE_DIRS
|
||||
TARGET hip::amdhip64
|
||||
PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
|
||||
find_file(
|
||||
HIP_RUNTIME_API_H hip_runtime_api.h
|
||||
PATHS ${HIP_INCLUDE_DIRS}
|
||||
PATH_SUFFIXES hip
|
||||
NO_DEFAULT_PATH REQUIRED)
|
||||
find_file(
|
||||
HIP_PROF_STR_H hip_prof_str.h
|
||||
PATHS ${HIP_INCLUDE_DIRS}
|
||||
PATH_SUFFIXES hip hip/amd_detail
|
||||
NO_DEFAULT_PATH REQUIRED)
|
||||
list(TRANSFORM HIP_INCLUDE_DIRS PREPEND -I)
|
||||
add_custom_command(
|
||||
OUTPUT hip_erts.yaml hip_begin.cpp.i hip_end.cpp.i
|
||||
COMMAND
|
||||
${CMAKE_C_COMPILER} ${HIP_INCLUDE_DIRS} -E
|
||||
"${PROJECT_SOURCE_DIR}/src/core/session/tracer/src/hip_full_api.h"
|
||||
-D__HIP_PLATFORM_AMD__=1 -D__HIP_ROCclr__=1 -o hip_runtime_api.h.i
|
||||
COMMAND cat hip_runtime_api.h.i "${HIP_PROF_STR_H}" > hip_input.h
|
||||
BYPRODUCTS hip_runtime_api.h.i hip_input.h
|
||||
COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/gen_api_files.py" hip
|
||||
hip_input.h
|
||||
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/gen_api_files.py"
|
||||
"${PROJECT_SOURCE_DIR}/src/core/session/tracer/src/hip_full_api.h"
|
||||
"${HIP_PROF_STR_H}"
|
||||
COMMENT "Generating HIP API files for the `ctf` plugin...")
|
||||
|
||||
# Generate `env.yaml` (trace environment for barectf).
|
||||
add_custom_command(
|
||||
OUTPUT env.yaml
|
||||
COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/gen_env_yaml.py"
|
||||
${PROJECT_VERSION}
|
||||
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/gen_env_yaml.py"
|
||||
COMMENT "Generating `env.yaml`...")
|
||||
|
||||
# Generate raw CTF tracer with barectf.
|
||||
add_custom_command(
|
||||
OUTPUT barectf.c barectf.h barectf-bitfield.h metadata
|
||||
COMMAND "${BARECTF_RES}" gen "-I${CMAKE_CURRENT_BINARY_DIR}"
|
||||
"-I${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/config.yaml"
|
||||
DEPENDS hsa_erts.yaml hip_erts.yaml env.yaml "${CMAKE_CURRENT_SOURCE_DIR}/config.yaml"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/dst_base.yaml"
|
||||
COMMENT "Generating raw CTF tracer with barectf...")
|
||||
install(
|
||||
FILES "${CMAKE_CURRENT_BINARY_DIR}/metadata"
|
||||
DESTINATION "${METADATA_STREAM_FILE_DIR}"
|
||||
COMPONENT plugins)
|
||||
@@ -0,0 +1,260 @@
|
||||
= CTF plugin for ROCProfiler
|
||||
13 December 2022
|
||||
Philippe Proulx
|
||||
|
||||
This plugin writes the received ROCProfiler tracer and profiler records to
|
||||
a https://diamon.org/ctf/[CTF] trace.
|
||||
|
||||
== Build requirements
|
||||
|
||||
* Python ≥ 3.10
|
||||
* barectf ≥ 3.1.1 (`pip3 install barectf`)
|
||||
* PyYAML (`apt-get install python3-yaml`)
|
||||
* CppHeaderParser (`pip3 install CppHeaderParser`)
|
||||
|
||||
== Usage
|
||||
|
||||
Once installed, you may load this plugin with `rocprofv2` using
|
||||
the `--plugin ctf` command-line arguments.
|
||||
|
||||
This plugin honours the `OUTPUT_PATH` environment variable which
|
||||
`rocprofv2` sets with the `-d` option. If you pass `-d my-dir` to
|
||||
`rocprofv2`, then the plugin will write the CTF trace to the
|
||||
`my-dir/trace` directory.
|
||||
|
||||
IMPORTANT: This plugin performs important cleanup tasks at finalization
|
||||
time, so the resulting CTF trace could be corrupted if the plugin is
|
||||
never finalized.
|
||||
|
||||
Once the plugin is finalized, open the resulting trace directory with
|
||||
either https://babeltrace.org/[Babeltrace{nbsp}2] or
|
||||
https://www.eclipse.org/tracecompass/[Trace Compass] to view or analyze
|
||||
it.
|
||||
|
||||
=== Event record types
|
||||
|
||||
This plugin writes to different CTF data streams having different types.
|
||||
On the file system, the prefix of a data stream file name indicates the
|
||||
data stream type, that is:
|
||||
|
||||
`roctx_`::
|
||||
rocTX messages.
|
||||
+
|
||||
Each CTF event record is named `roctx` and corresponds to a rocTX
|
||||
tracer record.
|
||||
+
|
||||
The fields are:
|
||||
+
|
||||
--
|
||||
[horizontal]
|
||||
`thread_id`::
|
||||
Thread ID.
|
||||
|
||||
`id`::
|
||||
rocTX ID.
|
||||
|
||||
`msg`::
|
||||
rocTX message.
|
||||
--
|
||||
|
||||
`hsa_api_`::
|
||||
HSA API beginning and end function calls.
|
||||
+
|
||||
All CTF event records have the following common fields:
|
||||
+
|
||||
--
|
||||
[horizontal]
|
||||
`thread_id`::
|
||||
Thread ID.
|
||||
|
||||
`queue_id`::
|
||||
Queue ID.
|
||||
|
||||
`agent_id`::
|
||||
Agent ID.
|
||||
|
||||
`correlation_id`::
|
||||
Correlation ID.
|
||||
--
|
||||
+
|
||||
For each ROCProfiler HSA API tracer record for the HSA function named
|
||||
`__name__`, this plugin writes two event records:
|
||||
+
|
||||
`__name___begin`:::
|
||||
Beginning of the function call.
|
||||
+
|
||||
The event record contains fields which correspond to most of the
|
||||
parameters of the HSA function.
|
||||
|
||||
`__name___end`:::
|
||||
End of the function call.
|
||||
|
||||
`hip_api_`::
|
||||
HIP API beginning and end function calls.
|
||||
+
|
||||
All CTF event records have the following common fields:
|
||||
+
|
||||
--
|
||||
[horizontal]
|
||||
`thread_id`::
|
||||
Thread ID.
|
||||
|
||||
`queue_id`::
|
||||
Queue ID.
|
||||
|
||||
`agent_id`::
|
||||
Agent ID.
|
||||
|
||||
`correlation_id`::
|
||||
Correlation ID.
|
||||
|
||||
`kernel_name`::
|
||||
Kernel name (empty string if not available).
|
||||
--
|
||||
+
|
||||
For each ROCProfiler HIP API tracer record for the HIP function named
|
||||
`__name__`, this plugin writes two event records:
|
||||
+
|
||||
`__name__Begin`:::
|
||||
Beginning of the function call.
|
||||
+
|
||||
The event record contains fields which correspond to most of the
|
||||
parameters of the HIP function.
|
||||
|
||||
`__name__End`:::
|
||||
End of the function call.
|
||||
|
||||
`api_ops_`::
|
||||
HSA/HIP API beginning and end operations.
|
||||
+
|
||||
All CTF event records have the following common fields:
|
||||
+
|
||||
--
|
||||
[horizontal]
|
||||
`thread_id`::
|
||||
Thread ID.
|
||||
|
||||
`queue_id`::
|
||||
Queue ID.
|
||||
|
||||
`agent_id`::
|
||||
Agent ID.
|
||||
|
||||
`correlation_id`::
|
||||
Correlation ID.
|
||||
--
|
||||
+
|
||||
The possible CTF event records are:
|
||||
+
|
||||
`hsa_op_begin`:::
|
||||
HSA API operation beginning.
|
||||
|
||||
`hsa_op_end`:::
|
||||
HSA API operation end.
|
||||
|
||||
`hip_op_begin`:::
|
||||
HIP API operation beginning.
|
||||
+
|
||||
Such an event record also has the field `kernel_name` which is the
|
||||
kernel name (empty string if not available).
|
||||
|
||||
`hip_op_end`:::
|
||||
HIP API operation end.
|
||||
|
||||
`profiler_`::
|
||||
Profiler records.
|
||||
+
|
||||
All CTF event records have the following common fields:
|
||||
+
|
||||
--
|
||||
[horizontal]
|
||||
`dispatch`::
|
||||
Dispatch ID.
|
||||
|
||||
`gpu_id`::
|
||||
GPU ID.
|
||||
|
||||
`queue_id`::
|
||||
Queue ID.
|
||||
|
||||
`queue_index`::
|
||||
Queue index.
|
||||
|
||||
`process_id`::
|
||||
Process ID.
|
||||
|
||||
`thread_id`::
|
||||
Thread ID.
|
||||
|
||||
`kernel_id`::
|
||||
Kernel ID.
|
||||
|
||||
`kernel_name`::
|
||||
Kernel name (empty string if not available).
|
||||
|
||||
`counter_names`::
|
||||
Array of counter names, each one having a corresponding integral
|
||||
value in the `counter_values` field.
|
||||
|
||||
`counter_values`::
|
||||
Array of integers, each one being the value of a counter of which
|
||||
the name is a corresponding string in the `counter_names` field.
|
||||
--
|
||||
+
|
||||
The possible CTF event records are:
|
||||
+
|
||||
`profiler_record`:::
|
||||
Profiler record.
|
||||
|
||||
`profiler_record_with_kernel_properties`:::
|
||||
Profiler record with kernel properties.
|
||||
+
|
||||
Such an event record also has the following fields:
|
||||
+
|
||||
--
|
||||
`grid_size`::
|
||||
Grid size.
|
||||
|
||||
`workgroup_size`::
|
||||
Workgroup size.
|
||||
|
||||
`lds_size`::
|
||||
Local memory size.
|
||||
|
||||
`scratch_size`::
|
||||
Scratch size.
|
||||
|
||||
`arch_vgpr_count`::
|
||||
Architecture vector general purpose register count.
|
||||
|
||||
`accum_vgpr_count`::
|
||||
Accum. vector general purpose register count
|
||||
|
||||
`sgpr_count`::
|
||||
Scalar general purpose register count.
|
||||
|
||||
`wave_size`::
|
||||
Wavefront size.
|
||||
|
||||
`signal_handle`::
|
||||
Signal handle.
|
||||
--
|
||||
|
||||
`hsa_handles_`::
|
||||
HSA handle type mappings.
|
||||
+
|
||||
Each CTF event record is named `hsa_handle_type` and maps an HSA handle
|
||||
to a processor unit type (CPU or GPU).
|
||||
+
|
||||
The clock value of those event records is irrelevant (always{nbsp}0).
|
||||
+
|
||||
The fields are:
|
||||
+
|
||||
--
|
||||
[horizontal]
|
||||
`handle`::
|
||||
HSA handle.
|
||||
|
||||
`type`::
|
||||
Processor unit type (`CPU` or `GPU` enumeration label).
|
||||
--
|
||||
@@ -0,0 +1,67 @@
|
||||
/* 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. */
|
||||
|
||||
#ifndef PLUGIN_CTF_BARECTF_EVENT_RECORD_H
|
||||
#define PLUGIN_CTF_BARECTF_EVENT_RECORD_H
|
||||
|
||||
#include <memory>
|
||||
#include <cstdint>
|
||||
|
||||
struct barectf_default_ctx;
|
||||
|
||||
namespace rocm_ctf {
|
||||
|
||||
// Abstract base class of any barectf event record.
|
||||
//
|
||||
// A concrete event record class must implement Write() which must call
|
||||
// a corresponding barectf tracing function.
|
||||
//
|
||||
// `CtxT` is the specific type of the barectf context which Write()
|
||||
// receives.
|
||||
template <typename CtxT> class BarectfEventRecord {
|
||||
protected:
|
||||
// Builds a barectf event record having the clock value `clock_val`.
|
||||
explicit BarectfEventRecord(const std::uint64_t clock_val) noexcept : clock_val_{clock_val} {}
|
||||
|
||||
public:
|
||||
// Shared pointer to const barectf event record.
|
||||
using SP = std::shared_ptr<const BarectfEventRecord>;
|
||||
|
||||
virtual ~BarectfEventRecord() = default;
|
||||
|
||||
// Disabled copy operations to make this class simpler.
|
||||
BarectfEventRecord(const BarectfEventRecord&) = delete;
|
||||
BarectfEventRecord& operator=(const BarectfEventRecord&) = delete;
|
||||
|
||||
// Clock value of this event record.
|
||||
std::uint64_t GetClockVal() const noexcept { return clock_val_; }
|
||||
|
||||
// Calls a corresponding barectf tracing function using the barectf
|
||||
// context `barectf_ctx`.
|
||||
virtual void Write(CtxT& barectf_ctx) const = 0;
|
||||
|
||||
private:
|
||||
// Clock value.
|
||||
std::uint64_t clock_val_;
|
||||
};
|
||||
|
||||
} // namespace rocm_ctf
|
||||
|
||||
#endif // PLUGIN_CTF_BARECTF_EVENT_RECORD_H
|
||||
@@ -0,0 +1,192 @@
|
||||
/* 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. */
|
||||
|
||||
#ifndef PLUGIN_CTF_BARECTF_PLATFORM_H
|
||||
#define PLUGIN_CTF_BARECTF_PLATFORM_H
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include "src/utils/filesystem.hpp"
|
||||
|
||||
#include "barectf.h"
|
||||
|
||||
namespace rocm_ctf {
|
||||
|
||||
template <typename> class BarectfWriter;
|
||||
|
||||
// A barectf platform for any barectf writer.
|
||||
//
|
||||
// The user doesn't deal directly with such an object: it's closely
|
||||
// coupled with a barectf writer.
|
||||
//
|
||||
// Each platform takes care of a single CTF data stream file.
|
||||
//
|
||||
// After building such a platform, get the raw barectf context with
|
||||
// GetCtx() to call tracing functions. The platform must still exist
|
||||
// when calling a tracing function.
|
||||
//
|
||||
// Such a platform opens the data stream file on construction and closes
|
||||
// it on destruction.
|
||||
//
|
||||
// `DescrT` is the specific barectf platform descriptor. It must be a
|
||||
// structure having:
|
||||
//
|
||||
// `Ctx`:
|
||||
// Specific barectf context type.
|
||||
//
|
||||
// `static void OpenPacket(Ctx&)`:
|
||||
// Packet opening function.
|
||||
//
|
||||
// `static void ClosePacket(Ctx&)`:
|
||||
// Packet closing function.
|
||||
template <typename DescrT> class BarectfPlatform final {
|
||||
friend class BarectfWriter<DescrT>;
|
||||
|
||||
private:
|
||||
// Builds a barectf platform.
|
||||
//
|
||||
// The platform writes CTF packets of size `packet_size` bytes to the
|
||||
// CTF data stream file `data_stream_file_path`.
|
||||
//
|
||||
// For each event record to write, the platform reads `clock_val` to
|
||||
// know the current timestamp.
|
||||
explicit BarectfPlatform(const std::size_t packet_size,
|
||||
const rocprofiler::common::filesystem::path& data_stream_file_path,
|
||||
const std::uint64_t& clock_val)
|
||||
: clock_val_{&clock_val}, buffer_(packet_size) {
|
||||
// Initialize barectf callbacks.
|
||||
barectf_platform_callbacks callbacks;
|
||||
|
||||
callbacks.default_clock_get_value = GetClockCb;
|
||||
callbacks.is_backend_full = IsBackendFullCb;
|
||||
callbacks.open_packet = OpenPacketCb;
|
||||
callbacks.close_packet = ClosePacketCb;
|
||||
|
||||
// Configure exceptions so that stream operations throw instead of
|
||||
// just setting flags on error.
|
||||
output_.exceptions(std::ofstream::failbit | std::ofstream::badbit);
|
||||
|
||||
// Open CTF data stream output file in binary mode.
|
||||
output_.open(data_stream_file_path, std::ios_base::out | std::ios_base::binary);
|
||||
|
||||
// Initialize the raw barectf context.
|
||||
barectf_init(&ctx_, buffer_.data(), buffer_.size(), callbacks, this);
|
||||
|
||||
// Open the initial packet.
|
||||
OpenPacketCb();
|
||||
}
|
||||
|
||||
public:
|
||||
// Disabled copy operations to make this class simpler.
|
||||
BarectfPlatform(const BarectfPlatform&) = delete;
|
||||
BarectfPlatform& operator=(const BarectfPlatform&) = delete;
|
||||
|
||||
// Closes/writes any last CTF packet and closes the data stream file.
|
||||
~BarectfPlatform() {
|
||||
if (barectf_packet_is_open(&ctx_) && !barectf_packet_is_empty(&ctx_)) {
|
||||
// Close and write last CTF packet (not empty).
|
||||
ClosePacketCb();
|
||||
}
|
||||
|
||||
// Close data stream output file.
|
||||
output_.close();
|
||||
}
|
||||
|
||||
// Returns the raw barectf context of this platform.
|
||||
const typename DescrT::Ctx& GetCtx() const noexcept { return ctx_; }
|
||||
typename DescrT::Ctx& GetCtx() noexcept { return ctx_; }
|
||||
|
||||
private:
|
||||
static BarectfPlatform& AsPlatform(void* const data) noexcept {
|
||||
return *static_cast<BarectfPlatform*>(data);
|
||||
}
|
||||
|
||||
// Four callbacks for barectf.
|
||||
//
|
||||
// Those four functions receive an instance of this class as `data`.
|
||||
|
||||
static std::uint64_t GetClockCb(void* const data) noexcept {
|
||||
// Forward to instance method.
|
||||
return AsPlatform(data).GetClockCb();
|
||||
}
|
||||
|
||||
static int IsBackendFullCb(void* const data) noexcept {
|
||||
// Forward to instance method.
|
||||
return AsPlatform(data).IsBackendFullCb();
|
||||
}
|
||||
|
||||
static void OpenPacketCb(void* const data) {
|
||||
// Forward to instance method.
|
||||
AsPlatform(data).OpenPacketCb();
|
||||
}
|
||||
|
||||
static void ClosePacketCb(void* const data) {
|
||||
// Forward to instance method.
|
||||
AsPlatform(data).ClosePacketCb();
|
||||
}
|
||||
|
||||
// Instance version of the "get clock value" callback.
|
||||
std::uint64_t GetClockCb() noexcept { return *clock_val_; }
|
||||
|
||||
// Instance version of the "is the back end full?" callback.
|
||||
int IsBackendFullCb() noexcept {
|
||||
// Never full.
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Instance version of the "open packet" callback.
|
||||
void OpenPacketCb() {
|
||||
// Forward to user (descriptor) function.
|
||||
DescrT::OpenPacket(ctx_);
|
||||
}
|
||||
|
||||
// Instance version of the "close packet" callback.
|
||||
void ClosePacketCb() {
|
||||
// Forward to user (descriptor) function to finalize the packet.
|
||||
DescrT::ClosePacket(ctx_);
|
||||
|
||||
// Write to the data stream file.
|
||||
WriteCurrentPacket();
|
||||
}
|
||||
|
||||
// Writes the current CTF packet (`buffer_`) to the data stream file.
|
||||
void WriteCurrentPacket() {
|
||||
output_.write(reinterpret_cast<const char*>(buffer_.data()), buffer_.size());
|
||||
}
|
||||
|
||||
// Clock value pointer.
|
||||
const std::uint64_t* clock_val_;
|
||||
|
||||
// CTF data stream output file stream.
|
||||
std::ofstream output_;
|
||||
|
||||
// Raw barectf context.
|
||||
typename DescrT::Ctx ctx_;
|
||||
|
||||
// CTF packet buffer.
|
||||
std::vector<std::uint8_t> buffer_;
|
||||
};
|
||||
|
||||
} // namespace rocm_ctf
|
||||
|
||||
#endif // PLUGIN_CTF_BARECTF_PLATFORM_H
|
||||
@@ -0,0 +1,124 @@
|
||||
/* 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. */
|
||||
|
||||
#ifndef PLUGIN_CTF_BARECTF_TRACER_H
|
||||
#define PLUGIN_CTF_BARECTF_TRACER_H
|
||||
|
||||
#include <cstdlib>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include "src/utils/filesystem.hpp"
|
||||
|
||||
#include "barectf_event_record.h"
|
||||
#include "barectf_writer.h"
|
||||
|
||||
namespace rocm_ctf {
|
||||
|
||||
// A barectf tracer offers the AddEventRecord() method to add an event
|
||||
// record which it will ultimately write to some CTF data stream file
|
||||
// within some specified CTF trace directory.
|
||||
//
|
||||
// One important feature of such a tracer is that you don't need to add
|
||||
// event records in order of time. A barectf tracer manages one or more
|
||||
// barectf writers, each one managing a single barectf platform/context
|
||||
// (CTF data stream file).
|
||||
//
|
||||
// All the CTF data stream files which a barectf tracer indirectly
|
||||
// manages share a common specified prefix. You must not use the same
|
||||
// prefix for two barectf tracers writing to the same CTF trace
|
||||
// directory.
|
||||
//
|
||||
// `PlatformDescrT` is the specific barectf platform descriptor (see the
|
||||
// documentation of the `BarectfPlatform` class template).
|
||||
template <typename PlatformDescrT> class BarectfTracer final {
|
||||
public:
|
||||
// Specific barectf event record type.
|
||||
using EventRecord = typename BarectfWriter<PlatformDescrT>::EventRecord;
|
||||
|
||||
// Builds a barectf tracer to write CTF packets of size `packet_size`
|
||||
// bytes to CTF data stream files having the prefix
|
||||
// `data_stream_file_name_prefix` within the CTF trace directory
|
||||
// `trace_dir`.
|
||||
//
|
||||
// The internal barectf writers manage event record queues having a
|
||||
// maximum size of `max_writer_queue_size`. Increasing
|
||||
// `max_writer_queue_size` increases the memory footprint of the
|
||||
// tracer, but may reduce the number of required CTF data stream files
|
||||
// to ensure time-ordered event records.
|
||||
explicit BarectfTracer(const std::size_t packet_size,
|
||||
rocprofiler::common::filesystem::path trace_dir,
|
||||
const char* const data_stream_file_name_prefix,
|
||||
const std::size_t max_writer_queue_size = 200)
|
||||
: packet_size_{packet_size},
|
||||
trace_dir_{std::move(trace_dir)},
|
||||
data_stream_file_name_prefix_{data_stream_file_name_prefix},
|
||||
max_writer_queue_size_{max_writer_queue_size} {}
|
||||
|
||||
// Disabled copy operations to make this class simpler.
|
||||
BarectfTracer(const BarectfTracer&) = delete;
|
||||
BarectfTracer& operator=(const BarectfTracer&) = delete;
|
||||
|
||||
// Adds the event record `event_record` to this tracer.
|
||||
//
|
||||
// The clock value of `event_record` may be less than the clock value
|
||||
// of previously added event records.
|
||||
void AddEventRecord(typename EventRecord::SP event_record) {
|
||||
// Try to find a barectf writer to accept `event_record`.
|
||||
for (auto& writer : writers_) {
|
||||
if (writer->MayAddEventRecord(*event_record)) {
|
||||
// Found: add the event record to this writer and return.
|
||||
writer->AddEventRecord(std::move(event_record));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// No barectf writer found: create a new one.
|
||||
std::ostringstream ss;
|
||||
|
||||
ss << data_stream_file_name_prefix_ << writers_.size();
|
||||
writers_.emplace_back(new BarectfWriter<PlatformDescrT>{packet_size_, trace_dir_ / ss.str(),
|
||||
max_writer_queue_size_});
|
||||
|
||||
// Add the event record to this new barectf writer.
|
||||
assert(writers_.back()->MayAddEventRecord(*event_record));
|
||||
writers_.back()->AddEventRecord(std::move(event_record));
|
||||
}
|
||||
|
||||
private:
|
||||
// CTF packet size.
|
||||
std::size_t packet_size_;
|
||||
|
||||
// CTF trace directory.
|
||||
rocprofiler::common::filesystem::path trace_dir_;
|
||||
|
||||
// CTF data stream file name prefix.
|
||||
std::string data_stream_file_name_prefix_;
|
||||
|
||||
// Maximum event record queue size of a barectf writer.
|
||||
std::size_t max_writer_queue_size_;
|
||||
|
||||
// barectf writers.
|
||||
std::vector<std::unique_ptr<BarectfWriter<PlatformDescrT>>> writers_;
|
||||
};
|
||||
|
||||
} // namespace rocm_ctf
|
||||
|
||||
#endif // PLUGIN_CTF_BARECTF_TRACER_H
|
||||
@@ -0,0 +1,178 @@
|
||||
/* 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. */
|
||||
|
||||
#ifndef PLUGIN_CTF_BARECTF_WRITER_H
|
||||
#define PLUGIN_CTF_BARECTF_WRITER_H
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdlib>
|
||||
#include <cstdint>
|
||||
#include <cassert>
|
||||
#include <queue>
|
||||
#include <utility>
|
||||
#include "src/utils/filesystem.hpp"
|
||||
|
||||
#include "barectf_platform.h"
|
||||
#include "barectf_event_record.h"
|
||||
|
||||
namespace rocm_ctf {
|
||||
|
||||
template <typename> class BarectfTracer;
|
||||
|
||||
// A barectf writer manages a queue of event records, writing them
|
||||
// through barectf when needed.
|
||||
//
|
||||
// Such an object makes it possible to add some event record with a
|
||||
// clock value V and then some other event record of which the clock
|
||||
// value is less than V. The barectf writer ensures that actual barectf
|
||||
// tracing functions are called chronologically, a requirement of CTF.
|
||||
//
|
||||
// A barectf writer keeps event records in memory until its queue is
|
||||
// full (you provide the maximum queue size at construction time), in
|
||||
// which case it writes the oldest event record to some current CTF
|
||||
// packet through a barectf tracing function.
|
||||
//
|
||||
// Call MayAddEventRecord() to check whether or not you may add an event
|
||||
// record to the barectf writer, and then AddEventRecord() if you may.
|
||||
//
|
||||
// A barectf writer writes all its remaining event records on
|
||||
// destruction.
|
||||
//
|
||||
// `PlatformDescrT` is the specific barectf platform descriptor (see the
|
||||
// documentation of the `BarectfPlatform` class template).
|
||||
template <typename PlatformDescrT> class BarectfWriter final {
|
||||
friend class BarectfTracer<PlatformDescrT>;
|
||||
|
||||
public:
|
||||
// Specific barectf event record type.
|
||||
using EventRecord = BarectfEventRecord<typename PlatformDescrT::Ctx>;
|
||||
|
||||
private:
|
||||
// Builds a barectf writer to write CTF packets of size `packet_size`
|
||||
// bytes to the CTF data stream file `data_stream_file_path`.
|
||||
//
|
||||
// The built barectf writer manages an event record queue having a
|
||||
// maximum size of `max_queue_size`.
|
||||
explicit BarectfWriter(const std::size_t packet_size,
|
||||
const rocprofiler::common::filesystem::path& data_stream_file_path,
|
||||
const std::size_t max_queue_size)
|
||||
: platform_{packet_size, data_stream_file_path, clock_val_},
|
||||
max_queue_size_{max_queue_size} {}
|
||||
|
||||
public:
|
||||
// Writes all its remaining event records.
|
||||
~BarectfWriter() {
|
||||
// Write all the remaining event records from the oldest to the
|
||||
// newest.
|
||||
while (!queue_.empty()) {
|
||||
WriteOldestEventRecord();
|
||||
}
|
||||
}
|
||||
|
||||
// Disabled copy operations to make this class simpler.
|
||||
BarectfWriter(const BarectfWriter&) = delete;
|
||||
BarectfWriter& operator=(const BarectfWriter&) = delete;
|
||||
|
||||
// Whether or not you may add the event record `event_record` to this
|
||||
// writer with AddEventRecord().
|
||||
bool MayAddEventRecord(const EventRecord& event_record) const noexcept {
|
||||
if (queue_.empty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// One may only add an event record if its clock value is greater
|
||||
// than or equal to the clock value of the most recently written
|
||||
// event record.
|
||||
return event_record.GetClockVal() >= clock_val_;
|
||||
}
|
||||
|
||||
// Adds the event record `event_record` to this writer.
|
||||
//
|
||||
// `MayAddEventRecord(*event_record)` must return `true`.
|
||||
void AddEventRecord(typename EventRecord::SP event_record) {
|
||||
assert(MayAddEventRecord(*event_record) && "May add event record");
|
||||
|
||||
// Add event record to queue.
|
||||
queue_.emplace(std::move(event_record));
|
||||
|
||||
if (queue_.size() > max_queue_size_) {
|
||||
// Queue is too large: write the oldest event record now to
|
||||
// satisfy the requirement.
|
||||
WriteOldestEventRecord();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
// Comparison type for `queue_`.
|
||||
struct EventRecordQueueCompare final {
|
||||
bool operator()(const typename EventRecord::SP& left,
|
||||
const typename EventRecord::SP& right) const noexcept {
|
||||
// "Greater than" so that the top element of the queue is the
|
||||
// oldest event record.
|
||||
return left->GetClockVal() > right->GetClockVal();
|
||||
}
|
||||
};
|
||||
|
||||
// Oldest event record within `queue_`.
|
||||
//
|
||||
// `queue_` must not be empty.
|
||||
const EventRecord& GetOldestEventRecord() const noexcept {
|
||||
assert(!queue_.empty() && "Queue isn't empty");
|
||||
return *queue_.top();
|
||||
}
|
||||
|
||||
// Writes the oldest event record through a barectf tracing function
|
||||
// and removes it from the event record queue.
|
||||
void WriteOldestEventRecord() {
|
||||
auto& oldest_event_record = GetOldestEventRecord();
|
||||
|
||||
// When calling a barectf tracing function, it calls the clock value
|
||||
// accessor callback of the platform, which itself reads from
|
||||
// `clock_val_`.
|
||||
clock_val_ = oldest_event_record.GetClockVal();
|
||||
|
||||
// Forward to a barectf tracing function.
|
||||
oldest_event_record.Write(platform_.GetCtx());
|
||||
|
||||
// Remove from queue.
|
||||
queue_.pop();
|
||||
}
|
||||
|
||||
// barectf platform (manages file I/O).
|
||||
BarectfPlatform<PlatformDescrT> platform_;
|
||||
|
||||
// Current clock value for `platform_`.
|
||||
//
|
||||
// This is also the clock value of the most recently written event
|
||||
// record, therefore that MayAddEventRecord() can rely on this.
|
||||
std::uint64_t clock_val_ = 0;
|
||||
|
||||
// Maximum size of `queue_` below.
|
||||
std::size_t max_queue_size_;
|
||||
|
||||
// Event record queue.
|
||||
std::priority_queue<typename EventRecord::SP, std::vector<typename EventRecord::SP>,
|
||||
EventRecordQueueCompare>
|
||||
queue_;
|
||||
};
|
||||
|
||||
} // namespace rocm_ctf
|
||||
|
||||
#endif // PLUGIN_CTF_BARECTF_WRITER_H
|
||||
@@ -0,0 +1,165 @@
|
||||
################################################################################
|
||||
# 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.
|
||||
################################################################################
|
||||
|
||||
%YAML 1.2
|
||||
--- !<tag:barectf.org,2020/3/config>
|
||||
trace:
|
||||
$include:
|
||||
# Environment (generated file).
|
||||
- env.yaml
|
||||
type:
|
||||
$include:
|
||||
- stdint.yaml
|
||||
- stdmisc.yaml
|
||||
native-byte-order: little-endian
|
||||
clock-types:
|
||||
default:
|
||||
origin-is-unix-epoch: true
|
||||
$c-type: uint64_t
|
||||
data-stream-types:
|
||||
hsa_api:
|
||||
event-record-common-context-field-type:
|
||||
class: struct
|
||||
members:
|
||||
- _thread_id: uint32
|
||||
- _queue_id: uint32
|
||||
- _agent_id: uint32
|
||||
- _correlation_id: uint64
|
||||
$include:
|
||||
# Base.
|
||||
- dst_base.yaml
|
||||
|
||||
# HSA API event record types (generated file).
|
||||
- hsa_erts.yaml
|
||||
hip_api:
|
||||
event-record-common-context-field-type:
|
||||
class: struct
|
||||
members:
|
||||
- _thread_id: uint32
|
||||
- _queue_id: uint32
|
||||
- _agent_id: uint32
|
||||
- _correlation_id: uint64
|
||||
- _kernel_name: str
|
||||
$include:
|
||||
# Base.
|
||||
- dst_base.yaml
|
||||
|
||||
# HIP API event record types (generated file).
|
||||
- hip_erts.yaml
|
||||
roctx:
|
||||
$include:
|
||||
# Base
|
||||
- dst_base.yaml
|
||||
event-record-common-context-field-type:
|
||||
class: struct
|
||||
members:
|
||||
- _thread_id: uint32
|
||||
event-record-types:
|
||||
roctx:
|
||||
payload-field-type:
|
||||
class: struct
|
||||
members:
|
||||
- _id: sint64
|
||||
- _msg: str
|
||||
hsa_handles:
|
||||
$include:
|
||||
# Base.
|
||||
- dst_base.yaml
|
||||
event-record-types:
|
||||
hsa_handle_type:
|
||||
payload-field-type:
|
||||
class: struct
|
||||
members:
|
||||
- _handle: uint64
|
||||
- _type:
|
||||
field-type:
|
||||
class: uenum
|
||||
size: 8
|
||||
mappings:
|
||||
CPU: [0]
|
||||
GPU: [1]
|
||||
api_ops:
|
||||
$include:
|
||||
# Base.
|
||||
- dst_base.yaml
|
||||
event-record-common-context-field-type:
|
||||
class: struct
|
||||
members:
|
||||
- _thread_id: uint32
|
||||
- _queue_id: uint32
|
||||
- _agent_id: uint32
|
||||
- _correlation_id: uint64
|
||||
event-record-types:
|
||||
hsa_op_begin:
|
||||
payload-field-type:
|
||||
class: struct
|
||||
hsa_op_end:
|
||||
payload-field-type:
|
||||
class: struct
|
||||
hip_op_begin:
|
||||
payload-field-type:
|
||||
class: struct
|
||||
members:
|
||||
- _kernel_name: str
|
||||
hip_op_end:
|
||||
payload-field-type:
|
||||
class: struct
|
||||
profiler:
|
||||
$include:
|
||||
# Base.
|
||||
- dst_base.yaml
|
||||
event-record-common-context-field-type:
|
||||
class: struct
|
||||
members:
|
||||
- _dispatch: uint64
|
||||
- _gpu_id: uint64
|
||||
- _queue_id: uint64
|
||||
- _queue_index: uint64
|
||||
- _process_id: uint32
|
||||
- _thread_id: uint32
|
||||
- _kernel_id: uint64
|
||||
- _kernel_name: str
|
||||
- _counter_names:
|
||||
field-type:
|
||||
class: dynamic-array
|
||||
element-field-type: str
|
||||
- _counter_values:
|
||||
field-type:
|
||||
class: dynamic-array
|
||||
element-field-type: uint64
|
||||
event-record-types:
|
||||
profiler_record:
|
||||
payload-field-type:
|
||||
class: struct
|
||||
profiler_record_with_kernel_properties:
|
||||
payload-field-type:
|
||||
class: struct
|
||||
members:
|
||||
- _grid_size: uint64
|
||||
- _workgroup_size: uint64
|
||||
- _lds_size: uint64
|
||||
- _scratch_size: uint64
|
||||
- _arch_vgpr_count: uint64
|
||||
- _accum_vgpr_count: uint64
|
||||
- _sgpr_count: uint64
|
||||
- _wave_size: uint64
|
||||
- _signal_handle: uint64
|
||||
@@ -0,0 +1,171 @@
|
||||
/* 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 <dlfcn.h>
|
||||
#include <cassert>
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <link.h>
|
||||
#include <chrono>
|
||||
#include <regex>
|
||||
#include <unistd.h>
|
||||
#include "src/utils/filesystem.hpp"
|
||||
#include <type_traits>
|
||||
|
||||
#include "rocprofiler.h"
|
||||
#include "rocprofiler_plugin.h"
|
||||
|
||||
#include "plugin.h"
|
||||
#ifdef USE_GET_ROCM_PATH_API
|
||||
#include <rocm-core/rocm_getpath.h>
|
||||
#endif
|
||||
|
||||
namespace fs = rocprofiler::common::filesystem;
|
||||
|
||||
namespace {
|
||||
|
||||
// Global plugin instance
|
||||
rocm_ctf::Plugin* the_plugin = nullptr;
|
||||
|
||||
} // namespace
|
||||
|
||||
ROCPROFILER_EXPORT int rocprofiler_plugin_initialize(const uint32_t rocprofiler_major_version,
|
||||
const uint32_t rocprofiler_minor_version,
|
||||
void* data) {
|
||||
if (rocprofiler_major_version != ROCPROFILER_VERSION_MAJOR ||
|
||||
rocprofiler_minor_version < ROCPROFILER_VERSION_MINOR) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (the_plugin) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
auto output_dir = []() -> std::string {
|
||||
if (const char* output_dir_internal = getenv("OUTPUT_PATH"); output_dir_internal != nullptr) {
|
||||
return output_dir_internal;
|
||||
}
|
||||
return "./";
|
||||
}();
|
||||
|
||||
auto output_file = []() -> std::string {
|
||||
auto _v = getenv("OUTPUT_FILE");
|
||||
return (_v) ? _v : "trace-{PID}";
|
||||
}();
|
||||
|
||||
auto _replace = [&output_dir, &output_file](const char* _key, auto _value) {
|
||||
using value_type = std::remove_cv_t<std::remove_reference_t<std::decay_t<decltype(_value)>>>;
|
||||
auto _value_str = std::to_string(_value);
|
||||
|
||||
const auto _re = std::regex{_key, std::regex_constants::icase};
|
||||
output_dir = std::regex_replace(output_dir, _re, _value_str);
|
||||
output_file = std::regex_replace(output_file, _re, _value_str);
|
||||
};
|
||||
|
||||
_replace("\\{PID\\}", getpid());
|
||||
_replace("\\$ENV\\{PID\\}", getpid());
|
||||
_replace("\\{PPID\\}", getppid());
|
||||
_replace("\\$ENV\\{PPID\\}", getppid());
|
||||
|
||||
// Create the plugin instance.
|
||||
#ifdef USE_GET_ROCM_PATH_API
|
||||
char *installPath = nullptr;
|
||||
unsigned int installPathLen = 0;
|
||||
PathErrors_t retVal = PathSuccess;
|
||||
auto metadata_path = std::string{CTF_PLUGIN_METADATA_FILE_PATH};
|
||||
// Get ROCM install path
|
||||
retVal = getROCmInstallPath( &installPath, &installPathLen );
|
||||
if(PathSuccess == retVal){
|
||||
metadata_path = fs::path(installPath) / fs::path{CTF_PLUGIN_METADATA_FILE_PATH};
|
||||
}else {
|
||||
std::cout << "Failed to get ROCm Install Path: " << retVal << std::endl;
|
||||
}
|
||||
// free allocated memory
|
||||
if(nullptr != installPath) {
|
||||
free(installPath);
|
||||
}
|
||||
#else
|
||||
auto* this_plugin_handle = dlopen("libctf_plugin.so", RTLD_LAZY | RTLD_NOLOAD);
|
||||
auto* librocprofiler_handle = dlopen("librocprofiler64.so", RTLD_LAZY | RTLD_NOLOAD);
|
||||
auto metadata_path = std::string{CTF_PLUGIN_METADATA_FILE_PATH};
|
||||
struct link_map* _link_map = nullptr;
|
||||
if (this_plugin_handle && dlinfo(this_plugin_handle, RTLD_DI_LINKMAP, &_link_map) == 0) {
|
||||
metadata_path = fs::path{_link_map->l_name}.parent_path() / fs::path{"../.."} /
|
||||
CTF_PLUGIN_METADATA_FILE_PATH;
|
||||
} else if (librocprofiler_handle &&
|
||||
dlinfo(librocprofiler_handle, RTLD_DI_LINKMAP, &_link_map) == 0) {
|
||||
metadata_path =
|
||||
fs::path{_link_map->l_name}.parent_path() / ".." / CTF_PLUGIN_METADATA_FILE_PATH;
|
||||
}
|
||||
|
||||
if (!fs::exists(metadata_path)) {
|
||||
metadata_path = fs::path{CTF_PLUGIN_INSTALL_PREFIX} / CTF_PLUGIN_METADATA_FILE_PATH;
|
||||
}
|
||||
#endif // USE_GET_ROCM_PATH_API
|
||||
|
||||
try {
|
||||
the_plugin = new rocm_ctf::Plugin{256 * 1024, fs::path{output_dir} / output_file,
|
||||
fs::absolute(metadata_path)};
|
||||
} catch (const std::exception& exc) {
|
||||
std::cerr << "rocprofiler_plugin_initialize(): " << exc.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
ROCPROFILER_EXPORT void rocprofiler_plugin_finalize() {
|
||||
delete the_plugin;
|
||||
the_plugin = nullptr;
|
||||
}
|
||||
|
||||
ROCPROFILER_EXPORT int rocprofiler_plugin_write_buffer_records(
|
||||
const rocprofiler_record_header_t* const begin, const rocprofiler_record_header_t* const end,
|
||||
const rocprofiler_session_id_t session_id, const rocprofiler_buffer_id_t buffer_id) {
|
||||
assert(the_plugin);
|
||||
|
||||
try {
|
||||
the_plugin->HandleBufferRecords(begin, end, session_id, buffer_id);
|
||||
} catch (const std::exception& exc) {
|
||||
std::cerr << "rocprofiler_plugin_write_buffer_records(): " << exc.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
ROCPROFILER_EXPORT int rocprofiler_plugin_write_record(const rocprofiler_record_tracer_t record) {
|
||||
assert(the_plugin);
|
||||
|
||||
if (record.header.id.handle == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
try {
|
||||
the_plugin->HandleTracerRecord(record, rocprofiler_session_id_t{0});
|
||||
} catch (const std::exception& exc) {
|
||||
std::cerr << "rocprofiler_plugin_write_record(): " << exc.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
################################################################################
|
||||
# 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.
|
||||
################################################################################
|
||||
|
||||
$default-clock-type-name: default
|
||||
$features:
|
||||
packet:
|
||||
beginning-timestamp-field-type: false
|
||||
discarded-event-records-counter-snapshot-field-type: false
|
||||
end-timestamp-field-type: false
|
||||
@@ -0,0 +1,718 @@
|
||||
################################################################################
|
||||
# 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.
|
||||
################################################################################
|
||||
|
||||
import os
|
||||
import os.path
|
||||
import sys
|
||||
import re
|
||||
import yaml
|
||||
import CppHeaderParser
|
||||
|
||||
|
||||
# Numeric field type (abstract).
|
||||
class _NumericFt:
|
||||
# Returns the C++ expression to cast the expression `expr` to the C
|
||||
# type of this field type.
|
||||
def cast(self, expr):
|
||||
return f"static_cast<{self.c_type}>({expr})"
|
||||
|
||||
|
||||
# Integer field type (abstract).
|
||||
class _IntFt(_NumericFt):
|
||||
def __init__(self, size, pref_disp_base="dec"):
|
||||
self._size = size
|
||||
self._pref_disp_base = pref_disp_base
|
||||
|
||||
# Size (bits).
|
||||
@property
|
||||
def size(self):
|
||||
return self._size
|
||||
|
||||
# Preferred display base (`dec` or `hex`).
|
||||
@property
|
||||
def pref_disp_base(self):
|
||||
return self._pref_disp_base
|
||||
|
||||
# Equivalent barectf field type in YAML.
|
||||
@property
|
||||
def barectf_yaml(self):
|
||||
return {
|
||||
"size": self._size,
|
||||
"preferred-display-base": self._pref_disp_base,
|
||||
}
|
||||
|
||||
|
||||
# Signed integer field type.
|
||||
class _SIntFt(_IntFt):
|
||||
# Equivalent barectf field type in YAML.
|
||||
@property
|
||||
def barectf_yaml(self):
|
||||
ret = super().barectf_yaml
|
||||
ret["class"] = "sint"
|
||||
return ret
|
||||
|
||||
# Equivalent C type
|
||||
@property
|
||||
def c_type(self):
|
||||
return f"std::int{self._size}_t"
|
||||
|
||||
|
||||
# Unsigned integer field type.
|
||||
class _UIntFt(_IntFt):
|
||||
# Equivalent barectf field type in YAML.
|
||||
@property
|
||||
def barectf_yaml(self):
|
||||
ret = super().barectf_yaml
|
||||
ret["class"] = "uint"
|
||||
return ret
|
||||
|
||||
# Equivalent C type.
|
||||
@property
|
||||
def c_type(self):
|
||||
return f"std::uint{self._size}_t"
|
||||
|
||||
|
||||
# Pointer field type.
|
||||
class _PointerFt(_UIntFt):
|
||||
def __init__(self):
|
||||
super().__init__(64, "hex")
|
||||
|
||||
# Returns the C++ expression to cast the expression `expr` to the C
|
||||
# type of this field type.
|
||||
def cast(self, expr):
|
||||
return f"static_cast<{self.c_type}>(reinterpret_cast<std::uintptr_t>({expr}))"
|
||||
|
||||
|
||||
# Enumeration field type (abstract).
|
||||
class _EnumFt(_IntFt):
|
||||
def __init__(self, size, mappings):
|
||||
super().__init__(size)
|
||||
self._mappings = mappings.copy()
|
||||
|
||||
# Mappings (names to integers).
|
||||
@property
|
||||
def mappings(self):
|
||||
return self._mappings
|
||||
|
||||
# Equivalent barectf field type in YAML.
|
||||
@property
|
||||
def barectf_yaml(self):
|
||||
ret = super().barectf_yaml
|
||||
mappings = {}
|
||||
|
||||
for name, val in self._mappings.items():
|
||||
mappings[name] = [val]
|
||||
|
||||
ret["mappings"] = mappings
|
||||
return ret
|
||||
|
||||
|
||||
# Unsigned enumeration field type.
|
||||
class _UEnumFt(_EnumFt, _UIntFt):
|
||||
# Equivalent barectf field type in YAML.
|
||||
@property
|
||||
def barectf_yaml(self):
|
||||
ret = super().barectf_yaml
|
||||
ret["class"] = "uenum"
|
||||
return ret
|
||||
|
||||
|
||||
# Signed enumeration field type.
|
||||
class _SEnumFt(_EnumFt, _UIntFt):
|
||||
# Equivalent barectf field type in YAML.
|
||||
@property
|
||||
def barectf_yaml(self):
|
||||
ret = super().barectf_yaml
|
||||
ret["class"] = "senum"
|
||||
return ret
|
||||
|
||||
|
||||
# Optional string field type.
|
||||
class _OptStrFt:
|
||||
# Equivalent barectf field type in YAML.
|
||||
@property
|
||||
def barectf_yaml(self):
|
||||
return {
|
||||
"class": "str",
|
||||
}
|
||||
|
||||
|
||||
# String field type.
|
||||
class _StrFt(_OptStrFt):
|
||||
pass
|
||||
|
||||
|
||||
# Floating-point number field type.
|
||||
class _FloatFt(_NumericFt):
|
||||
def __init__(self, size):
|
||||
self._size = size
|
||||
|
||||
# Size (bits): 32 or 64.
|
||||
@property
|
||||
def size(self):
|
||||
return self._size
|
||||
|
||||
# Equivalent barectf field type in YAML.
|
||||
@property
|
||||
def barectf_yaml(self):
|
||||
return {
|
||||
"class": "real",
|
||||
"size": self._size,
|
||||
}
|
||||
|
||||
# Equivalent C type.
|
||||
@property
|
||||
def c_type(self):
|
||||
if self._size == 32:
|
||||
return "float"
|
||||
else:
|
||||
assert self._size == 64
|
||||
return "double"
|
||||
|
||||
|
||||
# Event record type.
|
||||
class _Ert:
|
||||
def __init__(self, api_func_name, members):
|
||||
self._api_func_name = api_func_name
|
||||
self._members = members
|
||||
|
||||
# API function name
|
||||
@property
|
||||
def api_func_name(self):
|
||||
return self._api_func_name
|
||||
|
||||
# Parameters of function (list of `_ErtMember`).
|
||||
@property
|
||||
def members(self):
|
||||
return self._members
|
||||
|
||||
|
||||
# Beginning event record type.
|
||||
class _BeginErt(_Ert):
|
||||
# Name of event record type depending on the API prefix.
|
||||
def name(self, api_prefix):
|
||||
suffix = "_begin" if api_prefix == "hsa" else "Begin"
|
||||
return f"{self._api_func_name}{suffix}"
|
||||
|
||||
|
||||
# End event record type.
|
||||
class _EndErt(_Ert):
|
||||
# Name of event record type depending on the API prefix.
|
||||
def name(self, api_prefix):
|
||||
suffix = "_end" if api_prefix == "hsa" else "End"
|
||||
return f"{self._api_func_name}{suffix}"
|
||||
|
||||
|
||||
# Event record type member.
|
||||
class _ErtMember:
|
||||
def __init__(self, access, member_names, ft):
|
||||
self._access = access
|
||||
self._member_names = member_names.copy()
|
||||
self._ft = ft
|
||||
|
||||
# C++ access expression.
|
||||
@property
|
||||
def access(self):
|
||||
return self._access
|
||||
|
||||
# List of member names.
|
||||
@property
|
||||
def member_names(self):
|
||||
return self._member_names
|
||||
|
||||
# Equivalent field type.
|
||||
@property
|
||||
def ft(self):
|
||||
return self._ft
|
||||
|
||||
|
||||
# Makes sure some condition is satisfied, or prints the error message
|
||||
# `error_msg` and quits with exit status 1 otherwise.
|
||||
#
|
||||
# This is an unconditional assertion.
|
||||
def _make_sure(cond, error_msg):
|
||||
if not cond:
|
||||
print(f"Error: {error_msg}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def _enumerator_effective_val(enum_val):
|
||||
# Try the value, but this value may be a string (an
|
||||
# enumerator/definition).
|
||||
val = enum_val.get("value")
|
||||
|
||||
if type(val) is int:
|
||||
return val
|
||||
|
||||
# Try the raw value.
|
||||
val = enum_val.get("raw_value")
|
||||
|
||||
if val is not None:
|
||||
if type(val) is int:
|
||||
# Raw value is already an integer.
|
||||
return val
|
||||
else:
|
||||
# Try to parse the raw value string as an integer.
|
||||
try:
|
||||
return int(val, 0)
|
||||
except:
|
||||
pass
|
||||
|
||||
_make_sure(False, f'Cannot get the integral value of enumerator `{enum_val["name"]}`')
|
||||
|
||||
|
||||
# Returns the equivalent field type of the C type `c_type`.
|
||||
def _number_ft_from_c_type(cpp_header, c_type):
|
||||
# Check for known enumeration.
|
||||
m = re.match(r"(?:enum\s+)?(\w+)", c_type)
|
||||
|
||||
if m:
|
||||
size = 32
|
||||
|
||||
for enum_info in cpp_header.enums:
|
||||
if m.group(1) == enum_info.get("name"):
|
||||
# Fill enumeration field type mappings.
|
||||
mappings = {
|
||||
str(v["name"]): _enumerator_effective_val(v)
|
||||
for v in enum_info["values"]
|
||||
}
|
||||
|
||||
if len(mappings) == 0:
|
||||
return _SIntFt(64)
|
||||
|
||||
if max(mappings.values()) >= 2**31 or min(mappings.values()) < -(
|
||||
2**31
|
||||
):
|
||||
size = 64
|
||||
|
||||
_make_sure(
|
||||
len(mappings) > 0, f'Enumeration `{enum_info["name"]}` is empty'
|
||||
)
|
||||
|
||||
# Create corresponding enumeration field type.
|
||||
return _SEnumFt(size, mappings)
|
||||
|
||||
# Find corresponding basic field type.
|
||||
is_unsigned = "unsigned" in c_type
|
||||
|
||||
if "long" in c_type:
|
||||
if is_unsigned:
|
||||
return _UIntFt(64)
|
||||
else:
|
||||
return _SIntFt(64)
|
||||
elif "short" in c_type:
|
||||
if is_unsigned:
|
||||
return _UIntFt(16)
|
||||
else:
|
||||
return _SIntFt(16)
|
||||
elif "char" in c_type:
|
||||
if is_unsigned:
|
||||
return _UIntFt(8)
|
||||
else:
|
||||
return _SIntFt(8)
|
||||
elif "float" in c_type:
|
||||
return _FloatFt(32)
|
||||
elif "double" in c_type:
|
||||
return _FloatFt(64)
|
||||
else:
|
||||
# Assume `int` (often an unresolved C enumeration).
|
||||
if is_unsigned:
|
||||
return _UIntFt(32)
|
||||
else:
|
||||
return _SIntFt(32)
|
||||
|
||||
|
||||
# Returns whether or not a property has a pointer type.
|
||||
def _prop_is_pointer(prop, c_type):
|
||||
if prop["pointer"] or prop["function_pointer"]:
|
||||
return True
|
||||
|
||||
if prop["array"] and "array_size" in prop:
|
||||
return True
|
||||
|
||||
if prop["unresolved"]:
|
||||
# HSA API function pointers.
|
||||
if prop["name"] in ("callback", "handler"):
|
||||
return True
|
||||
|
||||
# HIP API function pointers.
|
||||
if c_type.endswith("Fn_t"):
|
||||
return True
|
||||
|
||||
# Check the C type itself.
|
||||
if "*" in c_type or "*" in prop.get("raw_type", ""):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
# Returns a list of event record type member objects for the structure
|
||||
# `struct` considering the initial C++ access expression `access` and
|
||||
# member names `member_names`.
|
||||
def _get_ert_members_for_struct(cpp_header, struct, access, member_names):
|
||||
members = []
|
||||
member_names = member_names.copy()
|
||||
member_names.append(None)
|
||||
props = struct["properties"]["public"]
|
||||
|
||||
for index, prop in enumerate(props):
|
||||
# Property name.
|
||||
name = prop["name"]
|
||||
|
||||
# Member names, access, and C type.
|
||||
member_names[-1] = str(name)
|
||||
this_access = f"{access}.{name}"
|
||||
c_type = prop["type"]
|
||||
aliases = prop["aliases"]
|
||||
|
||||
# Skip no type.
|
||||
if c_type == "":
|
||||
continue
|
||||
|
||||
# Skip unnamed or union.
|
||||
if name == "" or "union" in name or re.match(r"\bunion\b", c_type):
|
||||
continue
|
||||
|
||||
# Check for known C type alias.
|
||||
while True:
|
||||
c_type_alias = cpp_header.typedefs.get(c_type)
|
||||
|
||||
if c_type_alias is None:
|
||||
break
|
||||
|
||||
c_type = c_type_alias
|
||||
|
||||
# Check for C string.
|
||||
if re.match(r"^((const\s+char)|(char\s+const)|char)\s*\*$", c_type.strip()):
|
||||
members.append(_ErtMember(this_access, member_names, _OptStrFt()))
|
||||
continue
|
||||
|
||||
# Check for pointer.
|
||||
if _prop_is_pointer(prop, c_type):
|
||||
# Pointer: use numeric value.
|
||||
members.append(_ErtMember(this_access, member_names, _PointerFt()))
|
||||
continue
|
||||
|
||||
# Check for substructure.
|
||||
sub_struct = cpp_header.classes.get(c_type)
|
||||
|
||||
if sub_struct is None and len(aliases) == 1:
|
||||
sub_struct = cpp_header.classes.get(aliases[0])
|
||||
|
||||
if sub_struct is not None:
|
||||
members += _get_ert_members_for_struct(
|
||||
cpp_header, sub_struct, this_access, member_names
|
||||
)
|
||||
continue
|
||||
|
||||
# Use a basic field type.
|
||||
members.append(
|
||||
_ErtMember(
|
||||
this_access, member_names, _number_ft_from_c_type(cpp_header, c_type)
|
||||
)
|
||||
)
|
||||
|
||||
return members
|
||||
|
||||
|
||||
# Returns the beginning and end event record type objects for the
|
||||
# callback data structure `struct`.
|
||||
def _erts_from_cb_data_struct(api_prefix, cpp_header, retval_info, struct):
|
||||
# The location of the `args` union within the nested structures of
|
||||
# `struct`.
|
||||
args_nested_cls_index = 0
|
||||
|
||||
# Create return value members (to be used later).
|
||||
if retval_info is not None:
|
||||
args_nested_cls_index = 1
|
||||
retval_members = {}
|
||||
nested_classes = struct["nested_classes"]
|
||||
_make_sure(
|
||||
len(nested_classes) >= 1,
|
||||
f"Return value union doesn't exist in `{struct['name']}`",
|
||||
)
|
||||
retval_union = nested_classes[0]
|
||||
|
||||
for prop in retval_union["properties"]["public"]:
|
||||
name = str(prop["name"])
|
||||
member = _ErtMember(
|
||||
f"GetApiData().{name}",
|
||||
["retval"],
|
||||
_number_ft_from_c_type(cpp_header, prop["type"]),
|
||||
)
|
||||
retval_members[prop["name"]] = member
|
||||
|
||||
# Make sure we have everything we need.
|
||||
for api_func_name, retval_name in retval_info.items():
|
||||
if retval_name is not None:
|
||||
_make_sure(
|
||||
retval_name in retval_members,
|
||||
f"Return value union member `{retval_name}` doesn't exist (function {api_func_name}())",
|
||||
)
|
||||
|
||||
# Create beginning/end event record type objects.
|
||||
begin_erts = []
|
||||
end_erts = []
|
||||
nested_classes = struct["nested_classes"][args_nested_cls_index]["nested_classes"]
|
||||
props = struct["nested_classes"][args_nested_cls_index]["properties"]["public"]
|
||||
_make_sure(
|
||||
len(nested_classes) == len(props),
|
||||
f'Mismatch between nested structure and member count in `{struct["name"]}`',
|
||||
)
|
||||
|
||||
for index, prop in enumerate(props):
|
||||
# API function name is the name of the member.
|
||||
api_func_name = str(prop["name"])
|
||||
|
||||
# Get the parameters.
|
||||
members = _get_ert_members_for_struct(
|
||||
cpp_header, nested_classes[index], f"GetApiData().args.{api_func_name}", []
|
||||
)
|
||||
|
||||
# Append new beginning event record type object.
|
||||
begin_erts.append(_BeginErt(api_func_name, members))
|
||||
|
||||
# Append new end event record type object if possible.
|
||||
ret_members = []
|
||||
|
||||
if retval_info is not None:
|
||||
retval_type = retval_info.get(api_func_name)
|
||||
|
||||
if retval_type is not None:
|
||||
ret_members.append(retval_members[retval_type])
|
||||
|
||||
end_erts.append(_EndErt(api_func_name, ret_members))
|
||||
|
||||
return begin_erts, end_erts
|
||||
|
||||
|
||||
# Creates and returns the return value information dictionary.
|
||||
#
|
||||
# This dictionary maps API function names to the member to get within
|
||||
# the callback data structure.
|
||||
#
|
||||
# This only applies to the HSA API: for other APIs, this function
|
||||
# returns `None`.
|
||||
def _get_retval_info(path):
|
||||
if "hsa" not in os.path.basename(path):
|
||||
return
|
||||
|
||||
retval_info = {}
|
||||
cur_api_func_name = None
|
||||
|
||||
with open(path) as f:
|
||||
for line in f:
|
||||
if 'out << ")' in line and cur_api_func_name is not None:
|
||||
m = re.search(r"api_data.(\w+_retval)", line)
|
||||
retval_info[cur_api_func_name] = m.group(1) if m else None
|
||||
else:
|
||||
m = re.search(r'out << "(hsa_\w+)\(";', line)
|
||||
|
||||
if m:
|
||||
cur_api_func_name = m.group(1)
|
||||
|
||||
return retval_info
|
||||
|
||||
|
||||
# Returns a partial barectf data stream type in YAML with the event
|
||||
# record types `erts`.
|
||||
def _yaml_dst_from_erts(api_prefix, erts):
|
||||
# Base.
|
||||
yaml_erts = {}
|
||||
yaml_dst = {
|
||||
"event-record-types": yaml_erts,
|
||||
}
|
||||
|
||||
# Create one event record type per API function.
|
||||
for ert in erts:
|
||||
# Base.
|
||||
yaml_members = []
|
||||
yaml_ert = {
|
||||
"payload-field-type": {
|
||||
"class": "struct",
|
||||
"members": yaml_members,
|
||||
},
|
||||
}
|
||||
|
||||
# Create one structure field type member per member.
|
||||
for member in ert.members:
|
||||
# barectf doesn't support nested CTF structures, so join
|
||||
# individual member names with `__` to flatten.
|
||||
yaml_members.append(
|
||||
{
|
||||
"_"
|
||||
+ "__".join(member.member_names): {
|
||||
"field-type": member.ft.barectf_yaml,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
# Add event record type.
|
||||
yaml_erts[ert.name(api_prefix)] = yaml_ert
|
||||
|
||||
# Convert to YAML.
|
||||
return yaml.dump(yaml_dst)
|
||||
|
||||
|
||||
# Returns the C++ switch statement which calls the correct barectf
|
||||
# tracing function depending on the API function operation ID.
|
||||
def _cpp_switch_statement_from_erts(api_prefix, erts):
|
||||
lines = []
|
||||
lines.append("switch (GetOp()) {")
|
||||
|
||||
for ert in erts:
|
||||
if api_prefix == 'hip' and 'R0600' in ert.api_func_name:
|
||||
continue
|
||||
lines.append(f" case {api_prefix.upper()}_API_ID_{ert.api_func_name}:")
|
||||
lines.append(f" barectf_{api_prefix}_api_trace_{ert.name(api_prefix)}(")
|
||||
lines.append(f" &barectf_ctx,")
|
||||
lines.append(f" GetThreadId(),")
|
||||
lines.append(f" GetQueueId(),")
|
||||
lines.append(f" GetAgentId(),")
|
||||
lines.append(f" GetCorrelationId(),")
|
||||
|
||||
if api_prefix == "hip":
|
||||
lines.append(f" GetKernelName().c_str(),")
|
||||
|
||||
if len(ert.members) == 0:
|
||||
# Remove last comma.
|
||||
lines[-1] = lines[-1].replace(",", "")
|
||||
|
||||
for index, member in enumerate(ert.members):
|
||||
if type(member.ft) is _OptStrFt:
|
||||
# Only dereference C string if not null, otherwise use
|
||||
# an empty string.
|
||||
lines.append(f' {member.access} ? {member.access} : ""')
|
||||
elif type(member.ft) is _StrFt:
|
||||
lines.append(f" {member.access}")
|
||||
else:
|
||||
lines.append(f" {member.ft.cast(member.access)}")
|
||||
|
||||
if index + 1 < len(ert.members):
|
||||
lines[-1] += ","
|
||||
|
||||
lines.append(" );")
|
||||
lines.append(" break;")
|
||||
|
||||
lines.append("}")
|
||||
return lines
|
||||
|
||||
|
||||
# Returns a set of expected API function names based on the
|
||||
# enumerators of the `*_api_id_t` enumeration.
|
||||
def _api_func_names(api_prefix, cpp_header):
|
||||
# Find the `*_api_id_t` enumeration.
|
||||
for enum in cpp_header.enums:
|
||||
if enum.get('name') == f'{api_prefix}_api_id_t':
|
||||
break
|
||||
|
||||
# Create the set of API function names based on enumerators.
|
||||
func_names = set()
|
||||
pat = re.compile(rf'{api_prefix.upper()}_API_ID_(_*{api_prefix}.+)$')
|
||||
|
||||
for entry in enum['values']:
|
||||
if type(entry['value']) is str and 'API_ID_NONE' in entry['value']:
|
||||
# An enumerator may have the value `*_API_ID_NONE` which
|
||||
# means the corresponding API function is not available.
|
||||
continue
|
||||
|
||||
m = pat.match(entry['name'])
|
||||
|
||||
if m is not None:
|
||||
func_names.add(m.group(1))
|
||||
|
||||
# Return API function names
|
||||
return func_names
|
||||
|
||||
|
||||
# Processes the complete API header file `path`.
|
||||
def _process_file(api_prefix, path):
|
||||
# Create `CppHeader` object.
|
||||
try:
|
||||
cpp_header = CppHeaderParser.CppHeader(path)
|
||||
except CppHeaderParser.CppParseError as exc:
|
||||
print(exc, file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
# Get return value information dictionary.
|
||||
retval_info = _get_retval_info(path)
|
||||
|
||||
# add support for structures defined inside union.
|
||||
new_items = []
|
||||
for struct_name, struct in cpp_header.classes.items():
|
||||
# Check if the struct_name starts with 'union '.
|
||||
if re.match(r'^union \w+', struct_name) is not None:
|
||||
parts = struct_name.split('::')
|
||||
simplified = parts[-1]
|
||||
if simplified != "union ":
|
||||
new_items.append((simplified, cpp_header.classes.get(struct_name)))
|
||||
|
||||
for key, value in new_items:
|
||||
cpp_header.classes[key] = value
|
||||
|
||||
# Find callback data structure.
|
||||
for struct_name, struct in cpp_header.classes.items():
|
||||
if re.match(r'^' + api_prefix + r'_api_data\w+$', struct_name) is not None:
|
||||
break
|
||||
|
||||
# Process callback data structure.
|
||||
begin_erts, end_erts = _erts_from_cb_data_struct(api_prefix,
|
||||
cpp_header,
|
||||
retval_info,
|
||||
struct)
|
||||
|
||||
# API functions without parameters are not part of the callback data
|
||||
# structure, but they have an ID in the `*_api_id_t` enumeration.
|
||||
#
|
||||
# Add missing event record types to `begin_erts` and `end_erts`
|
||||
# considering the `*_api_id_t` enumeration.
|
||||
processed_api_func_names = set([ert.api_func_name for ert in begin_erts])
|
||||
|
||||
for func_name in _api_func_names(api_prefix, cpp_header):
|
||||
if func_name not in processed_api_func_names:
|
||||
begin_erts.append(_BeginErt(func_name, []))
|
||||
end_erts.append(_EndErt(func_name, []))
|
||||
|
||||
# Write barectf YAML file.
|
||||
with open(f'{api_prefix}_erts.yaml', 'w') as f:
|
||||
f.write(_yaml_dst_from_erts(api_prefix, begin_erts + end_erts))
|
||||
|
||||
# Write C++ code (beginning event record).
|
||||
with open(f'{api_prefix}_begin.cpp.i', 'w') as f:
|
||||
f.write('\n'.join(_cpp_switch_statement_from_erts(api_prefix,
|
||||
begin_erts)))
|
||||
|
||||
# Write C++ code (end event record).
|
||||
with open(f'{api_prefix}_end.cpp.i', 'w') as f:
|
||||
f.write('\n'.join(_cpp_switch_statement_from_erts(api_prefix,
|
||||
end_erts)))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Disable `CppHeaderParser` printing to standard output.
|
||||
CppHeaderParser.CppHeaderParser.print_warnings = 0
|
||||
CppHeaderParser.CppHeaderParser.print_errors = 0
|
||||
CppHeaderParser.CppHeaderParser.debug = 0
|
||||
CppHeaderParser.CppHeaderParser.debug_trace = 0
|
||||
|
||||
# Process the complete API header file.
|
||||
_process_file(sys.argv[1], sys.argv[2])
|
||||
@@ -0,0 +1,37 @@
|
||||
################################################################################
|
||||
# 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.
|
||||
################################################################################
|
||||
|
||||
import sys
|
||||
import yaml
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
with open("env.yaml", "w") as f:
|
||||
f.write(
|
||||
yaml.dump(
|
||||
{
|
||||
"environment": {
|
||||
"rocprofiler_version": sys.argv[1],
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
||||
@@ -0,0 +1,813 @@
|
||||
/* 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 <cassert>
|
||||
#include <cstdlib>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
#include <utility>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <limits>
|
||||
#include <fstream>
|
||||
#include <time.h>
|
||||
|
||||
#include <hsa/hsa.h>
|
||||
#include <hsa/hsa_ext_amd.h>
|
||||
#include "hsa_prof_str.h"
|
||||
|
||||
#include <hip/hip_runtime.h>
|
||||
#include <hip/amd_detail/hip_prof_str.h>
|
||||
|
||||
#include "rocprofiler.h"
|
||||
#include "rocprofiler_plugin.h"
|
||||
#include "../utils.h"
|
||||
|
||||
#include "barectf.h"
|
||||
#include "barectf_event_record.h"
|
||||
#include "barectf_tracer.h"
|
||||
#include "plugin.h"
|
||||
|
||||
#include "src/utils/filesystem.hpp"
|
||||
|
||||
namespace fs = rocprofiler::common::filesystem;
|
||||
|
||||
namespace rocm_ctf {
|
||||
namespace {
|
||||
|
||||
// Abstract tracer event record using the barectf context type `CtxT`.
|
||||
template <typename CtxT> class TracerEventRecord : public BarectfEventRecord<CtxT> {
|
||||
protected:
|
||||
explicit TracerEventRecord(const rocprofiler_record_tracer_t& record,
|
||||
const std::uint64_t clock_val)
|
||||
: BarectfEventRecord<CtxT>{clock_val},
|
||||
op_{record.operation_id.id},
|
||||
thread_id_{record.thread_id.value},
|
||||
queue_id_{record.queue_id.handle},
|
||||
agent_id_{record.agent_id.handle},
|
||||
correlation_id_{record.correlation_id.value} {}
|
||||
|
||||
std::uint32_t GetOp() const noexcept { return op_; }
|
||||
std::uint32_t GetThreadId() const noexcept { return thread_id_; }
|
||||
std::uint64_t GetQueueId() const noexcept { return queue_id_; }
|
||||
std::uint64_t GetAgentId() const noexcept { return agent_id_; }
|
||||
std::uint64_t GetCorrelationId() const noexcept { return correlation_id_; }
|
||||
|
||||
private:
|
||||
std::uint32_t op_;
|
||||
std::uint32_t thread_id_;
|
||||
std::uint64_t queue_id_;
|
||||
std::uint64_t agent_id_;
|
||||
std::uint64_t correlation_id_;
|
||||
};
|
||||
|
||||
// Returns the beginning clock value of the tracer or profiler record
|
||||
// `record`.
|
||||
template <typename RecordT> std::uint64_t GetRecordBeginClockVal(const RecordT& record) {
|
||||
return record.timestamps.begin.value;
|
||||
}
|
||||
|
||||
// Returns the end clock value of the tracer or profiler record
|
||||
// `record`.
|
||||
template <typename RecordT> std::uint64_t GetRecordEndClockVal(const RecordT& record) {
|
||||
return record.timestamps.end.value;
|
||||
}
|
||||
|
||||
// Queries allocated string data using the size query function
|
||||
// `query_size_func` and the data query function `query_data_func`,
|
||||
// returning the corresponding string and freeing temporary allocated
|
||||
// memory.
|
||||
//
|
||||
// Returns an empty string if anything goes wrong.
|
||||
template <typename QuerySizeFuncT, typename QueryDataFuncT>
|
||||
std::string QueryAllocStr(QuerySizeFuncT&& query_size_func, QueryDataFuncT&& query_data_func) {
|
||||
// Query size first.
|
||||
std::size_t size = 0;
|
||||
[[maybe_unused]] auto ret = query_size_func(&size);
|
||||
|
||||
assert(ret == ROCPROFILER_STATUS_SUCCESS && "Query size");
|
||||
|
||||
if (size == 0) {
|
||||
// No size: return empty string.
|
||||
return {};
|
||||
}
|
||||
|
||||
// Query data (allocated by query_data_func()).
|
||||
char* alloc_str = nullptr;
|
||||
|
||||
ret = query_data_func(&alloc_str);
|
||||
assert(ret == ROCPROFILER_STATUS_SUCCESS && "Query data");
|
||||
|
||||
if (!alloc_str) {
|
||||
// No data: return empty string.
|
||||
return {};
|
||||
}
|
||||
|
||||
// Allocate return value.
|
||||
std::string str_ret{alloc_str};
|
||||
|
||||
// Free allocated data.
|
||||
std::free(alloc_str);
|
||||
|
||||
// Return string object.
|
||||
return str_ret;
|
||||
}
|
||||
|
||||
// rocTX event record.
|
||||
class RocTxEventRecord final : public TracerEventRecord<barectf_roctx_ctx> {
|
||||
public:
|
||||
explicit RocTxEventRecord(const rocprofiler_record_tracer_t& record,
|
||||
const rocprofiler_session_id_t session_id)
|
||||
: TracerEventRecord<barectf_roctx_ctx>{record, GetRecordBeginClockVal(record)},
|
||||
id_{record.external_id.id} {
|
||||
msg_ = record.name ? record.name : "";
|
||||
}
|
||||
|
||||
void Write(barectf_roctx_ctx& barectf_ctx) const override {
|
||||
barectf_roctx_trace_roctx(&barectf_ctx, GetThreadId(), id_, msg_.c_str());
|
||||
}
|
||||
|
||||
private:
|
||||
std::uint64_t id_;
|
||||
std::string msg_;
|
||||
};
|
||||
|
||||
// Abstract HSA API event record.
|
||||
class HsaApiEventRecord : public TracerEventRecord<barectf_hsa_api_ctx> {
|
||||
protected:
|
||||
explicit HsaApiEventRecord(const rocprofiler_record_tracer_t& record,
|
||||
const rocprofiler_session_id_t session_id,
|
||||
const std::uint64_t clock_val)
|
||||
: TracerEventRecord<barectf_hsa_api_ctx>{record, clock_val} {
|
||||
if (record.api_data.hsa) api_data_ = *(record.api_data.hsa);
|
||||
}
|
||||
explicit HsaApiEventRecord(const rocprofiler_record_tracer_t& record,
|
||||
const std::uint64_t clock_val, hsa_api_data_t& api_data)
|
||||
: TracerEventRecord<barectf_hsa_api_ctx>{record, clock_val}, api_data_(api_data) {}
|
||||
const hsa_api_data_t& GetApiData() const noexcept { return api_data_; }
|
||||
|
||||
private:
|
||||
hsa_api_data_t api_data_;
|
||||
};
|
||||
|
||||
// HSA API event record (beginning).
|
||||
class HsaApiEventRecordBegin final : public HsaApiEventRecord {
|
||||
public:
|
||||
explicit HsaApiEventRecordBegin(const rocprofiler_record_tracer_t& record,
|
||||
const rocprofiler_session_id_t session_id)
|
||||
: HsaApiEventRecord{record, session_id, GetRecordBeginClockVal(record)} {}
|
||||
explicit HsaApiEventRecordBegin(const rocprofiler_record_tracer_t& record,
|
||||
hsa_api_data_t& api_data)
|
||||
: HsaApiEventRecord{record, GetRecordBeginClockVal(record), api_data} {}
|
||||
|
||||
void Write(barectf_hsa_api_ctx& barectf_ctx) const override {
|
||||
// Include generated switch statement.
|
||||
#include "hsa_begin.cpp.i"
|
||||
}
|
||||
};
|
||||
|
||||
// HSA API event record (end).
|
||||
class HsaApiEventRecordEnd final : public HsaApiEventRecord {
|
||||
public:
|
||||
explicit HsaApiEventRecordEnd(const rocprofiler_record_tracer_t& record,
|
||||
const rocprofiler_session_id_t session_id)
|
||||
: HsaApiEventRecord{record, session_id, GetRecordEndClockVal(record)} {}
|
||||
explicit HsaApiEventRecordEnd(const rocprofiler_record_tracer_t& record, hsa_api_data_t& api_data)
|
||||
: HsaApiEventRecord{record, GetRecordBeginClockVal(record), api_data} {}
|
||||
|
||||
void Write(barectf_hsa_api_ctx& barectf_ctx) const override {
|
||||
// Include generated switch statement.
|
||||
#include "hsa_end.cpp.i"
|
||||
}
|
||||
};
|
||||
|
||||
// Abstract HIP API event record.
|
||||
class HipApiEventRecord : public TracerEventRecord<barectf_hip_api_ctx> {
|
||||
protected:
|
||||
explicit HipApiEventRecord(const rocprofiler_record_tracer_t& record,
|
||||
const rocprofiler_session_id_t session_id,
|
||||
const std::uint64_t clock_val)
|
||||
: TracerEventRecord<barectf_hip_api_ctx>{record, clock_val},
|
||||
api_data_{record.api_data.hip ? *(record.api_data.hip) : hip_api_data_t{}},
|
||||
kernel_name_{record.name ? record.name : std::string{}} {}
|
||||
explicit HipApiEventRecord(const rocprofiler_record_tracer_t& record,
|
||||
const std::uint64_t clock_val, hip_api_data_t& api_data,
|
||||
std::string kernel_name)
|
||||
: TracerEventRecord<barectf_hip_api_ctx>{record, clock_val},
|
||||
api_data_{api_data},
|
||||
kernel_name_{kernel_name} {}
|
||||
const hip_api_data_t& GetApiData() const noexcept { return api_data_; }
|
||||
const std::string& GetKernelName() const noexcept { return kernel_name_; }
|
||||
|
||||
private:
|
||||
hip_api_data_t api_data_;
|
||||
std::string kernel_name_;
|
||||
};
|
||||
|
||||
// HIP API event record (beginning).
|
||||
class HipApiEventRecordBegin final : public HipApiEventRecord {
|
||||
public:
|
||||
explicit HipApiEventRecordBegin(const rocprofiler_record_tracer_t& record,
|
||||
const rocprofiler_session_id_t session_id)
|
||||
: HipApiEventRecord{record, session_id, GetRecordBeginClockVal(record)} {}
|
||||
explicit HipApiEventRecordBegin(const rocprofiler_record_tracer_t& record,
|
||||
hip_api_data_t& api_data, std::string kernel_name)
|
||||
: HipApiEventRecord{record, GetRecordBeginClockVal(record), api_data, kernel_name} {}
|
||||
|
||||
void Write(barectf_hip_api_ctx& barectf_ctx) const override {
|
||||
// Include generated switch statement.
|
||||
#include "hip_begin.cpp.i"
|
||||
}
|
||||
};
|
||||
|
||||
// HIP API event record (end).
|
||||
class HipApiEventRecordEnd final : public HipApiEventRecord {
|
||||
public:
|
||||
explicit HipApiEventRecordEnd(const rocprofiler_record_tracer_t& record,
|
||||
const rocprofiler_session_id_t session_id)
|
||||
: HipApiEventRecord{record, session_id, GetRecordEndClockVal(record)} {}
|
||||
explicit HipApiEventRecordEnd(const rocprofiler_record_tracer_t& record, hip_api_data_t& api_data,
|
||||
std::string kernel_name)
|
||||
: HipApiEventRecord{record, GetRecordBeginClockVal(record), api_data, kernel_name} {}
|
||||
|
||||
void Write(barectf_hip_api_ctx& barectf_ctx) const override {
|
||||
// Include generated switch statement.
|
||||
#include "hip_end.cpp.i"
|
||||
}
|
||||
};
|
||||
|
||||
// HSA API handle type event record.
|
||||
class HsaHandleTypeEventRecord final : public BarectfEventRecord<barectf_hsa_handles_ctx> {
|
||||
public:
|
||||
enum class Type {
|
||||
CPU = 0,
|
||||
GPU = 1,
|
||||
};
|
||||
|
||||
explicit HsaHandleTypeEventRecord(const std::uint64_t handle, const Type type)
|
||||
: BarectfEventRecord<barectf_hsa_handles_ctx>{0}, handle_{handle}, type_{type} {}
|
||||
|
||||
void Write(barectf_hsa_handles_ctx& barectf_ctx) const override {
|
||||
barectf_hsa_handles_trace_hsa_handle_type(&barectf_ctx, handle_,
|
||||
static_cast<std::uint8_t>(type_));
|
||||
}
|
||||
|
||||
private:
|
||||
std::uint64_t handle_;
|
||||
Type type_;
|
||||
};
|
||||
|
||||
// Abstract API operation event record.
|
||||
class ApiOpEventRecord : public TracerEventRecord<barectf_api_ops_ctx> {
|
||||
protected:
|
||||
explicit ApiOpEventRecord(const rocprofiler_record_tracer_t& record,
|
||||
const std::uint64_t clock_val)
|
||||
: TracerEventRecord<barectf_api_ops_ctx>{record, clock_val} {}
|
||||
};
|
||||
|
||||
// HSA API operation event record (beginning).
|
||||
class HsaOpEventRecordBegin final : public ApiOpEventRecord {
|
||||
public:
|
||||
explicit HsaOpEventRecordBegin(const rocprofiler_record_tracer_t& record)
|
||||
: ApiOpEventRecord{record, GetRecordBeginClockVal(record)} {}
|
||||
|
||||
void Write(barectf_api_ops_ctx& barectf_ctx) const override {
|
||||
barectf_api_ops_trace_hsa_op_begin(&barectf_ctx, GetThreadId(), GetQueueId(), GetAgentId(),
|
||||
GetCorrelationId());
|
||||
}
|
||||
};
|
||||
|
||||
// HSA API operation event record (end).
|
||||
class HsaOpEventRecordEnd final : public ApiOpEventRecord {
|
||||
public:
|
||||
explicit HsaOpEventRecordEnd(const rocprofiler_record_tracer_t& record)
|
||||
: ApiOpEventRecord{record, GetRecordEndClockVal(record)} {}
|
||||
|
||||
void Write(barectf_api_ops_ctx& barectf_ctx) const override {
|
||||
barectf_api_ops_trace_hsa_op_end(&barectf_ctx, GetThreadId(), GetQueueId(), GetAgentId(),
|
||||
GetCorrelationId());
|
||||
}
|
||||
};
|
||||
|
||||
// HIP API operation event record (beginning).
|
||||
class HipOpEventRecordBegin final : public ApiOpEventRecord {
|
||||
public:
|
||||
explicit HipOpEventRecordBegin(const rocprofiler_record_tracer_t& record)
|
||||
: ApiOpEventRecord{record, GetRecordBeginClockVal(record)},
|
||||
kernel_name_{QueryKernelName(record)} {}
|
||||
|
||||
void Write(barectf_api_ops_ctx& barectf_ctx) const override {
|
||||
barectf_api_ops_trace_hip_op_begin(&barectf_ctx, GetThreadId(), GetQueueId(), GetAgentId(),
|
||||
GetCorrelationId(), kernel_name_.c_str());
|
||||
}
|
||||
|
||||
private:
|
||||
// Queries and returns the kernel name of the record `record`.
|
||||
//
|
||||
// Returns an empty string if not available.
|
||||
static std::string QueryKernelName(const rocprofiler_record_tracer_t& record) {
|
||||
if (record.name) {
|
||||
// Return demangled version.
|
||||
return rocprofiler::cxx_demangle(record.name);
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
std::string kernel_name_;
|
||||
};
|
||||
|
||||
// HIP API operation event record (end).
|
||||
class HipOpEventRecordEnd final : public ApiOpEventRecord {
|
||||
public:
|
||||
explicit HipOpEventRecordEnd(const rocprofiler_record_tracer_t& record)
|
||||
: ApiOpEventRecord{record, GetRecordEndClockVal(record)} {}
|
||||
|
||||
void Write(barectf_api_ops_ctx& barectf_ctx) const override {
|
||||
barectf_api_ops_trace_hip_op_end(&barectf_ctx, GetThreadId(), GetQueueId(), GetAgentId(),
|
||||
GetCorrelationId());
|
||||
}
|
||||
};
|
||||
|
||||
// Profiler record base.
|
||||
class ProfilerEventRecord : public BarectfEventRecord<barectf_profiler_ctx> {
|
||||
public:
|
||||
explicit ProfilerEventRecord(const rocprofiler_record_profiler_t& record,
|
||||
const rocprofiler_session_id_t session_id)
|
||||
: BarectfEventRecord<barectf_profiler_ctx>{GetRecordBeginClockVal(record)},
|
||||
dispatch_{record.header.id.handle},
|
||||
gpu_id_{record.gpu_id.handle},
|
||||
queue_id_{record.queue_id.handle},
|
||||
queue_index_{record.queue_idx.value},
|
||||
process_id_{GetPid()},
|
||||
thread_id_{record.thread_id.value},
|
||||
kernel_id_{record.kernel_id.handle},
|
||||
kernel_name_{QueryKernelName(record)},
|
||||
counter_infos_{QueryCounterInfos(record, session_id)} {}
|
||||
|
||||
void Write(barectf_profiler_ctx& barectf_ctx) const override {
|
||||
barectf_profiler_trace_profiler_record(
|
||||
&barectf_ctx, dispatch_, gpu_id_, queue_id_, queue_index_, process_id_, thread_id_,
|
||||
kernel_id_, kernel_name_.c_str(), counter_infos_.names.size(), counter_infos_.names.data(),
|
||||
counter_infos_.values.size(), counter_infos_.values.data());
|
||||
}
|
||||
|
||||
protected:
|
||||
// Counter infos.
|
||||
//
|
||||
// `names[i]` names the counter value `values[i]`.
|
||||
struct CounterInfos final {
|
||||
// `names_storage` owns the strings while the elements of `names`
|
||||
// point to the internal C strings of `names_storage`.
|
||||
//
|
||||
// This is needed because barectf expects an array of contiguous
|
||||
// C string pointers.
|
||||
std::vector<std::string> names_storage;
|
||||
std::vector<const char*> names;
|
||||
|
||||
// Counter values.
|
||||
std::vector<std::uint64_t> values;
|
||||
};
|
||||
|
||||
std::uint64_t GetDispatch() const noexcept { return dispatch_; }
|
||||
std::uint64_t GetGpuId() const noexcept { return gpu_id_; }
|
||||
std::uint64_t GetQueueId() const noexcept { return queue_id_; }
|
||||
std::uint64_t GetQueueIndex() const noexcept { return queue_index_; }
|
||||
std::uint32_t GetProcessId() const noexcept { return process_id_; }
|
||||
std::uint32_t GetThreadId() const noexcept { return thread_id_; }
|
||||
std::uint64_t GetKernelId() const noexcept { return kernel_id_; }
|
||||
const std::string& GetKernelName() const noexcept { return kernel_name_; }
|
||||
const CounterInfos& GetCounterInfos() const noexcept { return counter_infos_; }
|
||||
|
||||
private:
|
||||
// Queries and returns the kernel name of the record `record`.
|
||||
//
|
||||
// Returns an empty string if not available.
|
||||
static std::string QueryKernelName(const rocprofiler_record_profiler_t& record) {
|
||||
const auto kernel_name = QueryAllocStr(
|
||||
[&record](const auto size) {
|
||||
return rocprofiler_query_kernel_info_size(ROCPROFILER_KERNEL_NAME, record.kernel_id,
|
||||
size);
|
||||
},
|
||||
[&record](const auto str) {
|
||||
return rocprofiler_query_kernel_info(ROCPROFILER_KERNEL_NAME, record.kernel_id,
|
||||
const_cast<const char**>(str));
|
||||
});
|
||||
|
||||
if (kernel_name.size() <= 1) {
|
||||
return {};
|
||||
}
|
||||
|
||||
// Return truncated and demangled version.
|
||||
return rocprofiler::truncate_name(rocprofiler::cxx_demangle(kernel_name));
|
||||
}
|
||||
|
||||
// Queries and returns the counter infos of the record `record` and
|
||||
// session ID `session_id`.
|
||||
static CounterInfos QueryCounterInfos(const rocprofiler_record_profiler_t& record,
|
||||
const rocprofiler_session_id_t session_id) {
|
||||
if (!record.counters) {
|
||||
// No counters.
|
||||
return {};
|
||||
}
|
||||
|
||||
CounterInfos infos;
|
||||
|
||||
for (std::size_t i = 0; i < record.counters_count.value; ++i) {
|
||||
auto& counter = record.counters[i];
|
||||
|
||||
if (counter.counter_handler.handle == 0) {
|
||||
// Not available: continue.
|
||||
continue;
|
||||
}
|
||||
|
||||
// Query counter name size first
|
||||
std::size_t counter_name_size = 0;
|
||||
[[maybe_unused]] auto ret = rocprofiler_query_counter_info_size(
|
||||
session_id, ROCPROFILER_COUNTER_NAME, counter.counter_handler, &counter_name_size);
|
||||
|
||||
assert(ret == ROCPROFILER_STATUS_SUCCESS && "Query counter name size");
|
||||
|
||||
if (counter_name_size == 0) {
|
||||
// No size: continue.
|
||||
continue;
|
||||
}
|
||||
|
||||
// Query counter name (borrowed from `record`: no need to free).
|
||||
const char* counter_name = nullptr;
|
||||
|
||||
ret = rocprofiler_query_counter_info(session_id, ROCPROFILER_COUNTER_NAME,
|
||||
counter.counter_handler, &counter_name);
|
||||
assert(ret == ROCPROFILER_STATUS_SUCCESS && "Query counter name");
|
||||
|
||||
if (!counter_name) {
|
||||
// Not available: continue.
|
||||
continue;
|
||||
}
|
||||
|
||||
// Push back infos.
|
||||
infos.names_storage.emplace_back(counter_name);
|
||||
infos.names.push_back(infos.names_storage.back().c_str());
|
||||
infos.values.push_back(counter.value.value);
|
||||
}
|
||||
|
||||
return infos;
|
||||
}
|
||||
|
||||
std::uint64_t dispatch_;
|
||||
std::uint64_t gpu_id_;
|
||||
std::uint64_t queue_id_;
|
||||
std::uint64_t queue_index_;
|
||||
std::uint32_t process_id_;
|
||||
std::uint32_t thread_id_;
|
||||
std::uint64_t kernel_id_;
|
||||
std::string kernel_name_;
|
||||
CounterInfos counter_infos_;
|
||||
};
|
||||
|
||||
// Profiler record base.
|
||||
class ProfilerWithKernelPropsEventRecord final : public ProfilerEventRecord {
|
||||
private:
|
||||
// According to `plugin/file/file.cpp`:
|
||||
//
|
||||
// > Taken from rocprofiler: The size hasn't changed in recent past
|
||||
static constexpr std::uint32_t lds_block_size_ = 128 * 4;
|
||||
|
||||
public:
|
||||
explicit ProfilerWithKernelPropsEventRecord(const rocprofiler_record_profiler_t& record,
|
||||
const rocprofiler_session_id_t session_id)
|
||||
: ProfilerEventRecord{record, session_id},
|
||||
grid_size_{record.kernel_properties.grid_size},
|
||||
workgroup_size_{record.kernel_properties.workgroup_size},
|
||||
lds_size_{
|
||||
((record.kernel_properties.lds_size + (lds_block_size_ - 1)) & ~(lds_block_size_ - 1))},
|
||||
scratch_size_{record.kernel_properties.scratch_size},
|
||||
arch_vgpr_count_{record.kernel_properties.arch_vgpr_count},
|
||||
accum_vgpr_count_{record.kernel_properties.accum_vgpr_count},
|
||||
sgpr_count_{record.kernel_properties.sgpr_count},
|
||||
wave_size_{record.kernel_properties.wave_size},
|
||||
signal_handle_{record.kernel_properties.signal_handle} {}
|
||||
|
||||
void Write(barectf_profiler_ctx& barectf_ctx) const override {
|
||||
barectf_profiler_trace_profiler_record_with_kernel_properties(
|
||||
&barectf_ctx, GetDispatch(), GetGpuId(), GetQueueId(), GetQueueIndex(), GetProcessId(),
|
||||
GetThreadId(), GetKernelId(), GetKernelName().c_str(), GetCounterInfos().names.size(),
|
||||
GetCounterInfos().names.data(), GetCounterInfos().values.size(),
|
||||
GetCounterInfos().values.data(), grid_size_, workgroup_size_, lds_size_, scratch_size_,
|
||||
arch_vgpr_count_, accum_vgpr_count_, sgpr_count_, wave_size_, signal_handle_);
|
||||
}
|
||||
|
||||
private:
|
||||
std::uint64_t grid_size_;
|
||||
std::uint64_t workgroup_size_;
|
||||
std::uint64_t lds_size_;
|
||||
std::uint64_t scratch_size_;
|
||||
std::uint64_t arch_vgpr_count_;
|
||||
std::uint64_t accum_vgpr_count_;
|
||||
std::uint64_t sgpr_count_;
|
||||
std::uint64_t wave_size_;
|
||||
std::uint64_t signal_handle_;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
Plugin::Plugin(const std::size_t packet_size, const fs::path& trace_dir,
|
||||
const fs::path& metadata_stream_path)
|
||||
: roctx_tracer_{packet_size, trace_dir, "roctx_"},
|
||||
hsa_api_tracer_{packet_size, trace_dir, "hsa_api_"},
|
||||
hip_api_tracer_{packet_size, trace_dir, "hip_api_"},
|
||||
api_ops_tracer_{packet_size, trace_dir, "api_ops_"},
|
||||
hsa_handles_tracer_{packet_size, trace_dir, "hsa_handles_"},
|
||||
profiler_tracer_{packet_size, trace_dir, "profiler_"} {
|
||||
// Make sure the trace directory doesn't exist.
|
||||
if (fs::exists(trace_dir)) {
|
||||
std::ostringstream ss;
|
||||
|
||||
ss << "CTF trace directory `" << trace_dir.string() << "` already exists";
|
||||
throw std::runtime_error{ss.str()};
|
||||
}
|
||||
|
||||
// Make sure the metadata stream file exists.
|
||||
if (!fs::exists(metadata_stream_path)) {
|
||||
std::ostringstream ss;
|
||||
|
||||
ss << "CTF metadata stream file `" << metadata_stream_path.string() << "` doesn't exist";
|
||||
throw std::runtime_error{ss.str()};
|
||||
}
|
||||
|
||||
// Create trace directory.
|
||||
if (!fs::create_directory(trace_dir)) {
|
||||
std::ostringstream ss;
|
||||
|
||||
ss << "Cannot create the CTF trace directory `" << trace_dir.string() << "`";
|
||||
throw std::runtime_error{ss.str()};
|
||||
}
|
||||
|
||||
// Copy adjusted metadata stream file to trace directory.
|
||||
try {
|
||||
CopyAdjustedMetadataStreamFile(metadata_stream_path, trace_dir);
|
||||
} catch (const std::exception& exc) {
|
||||
std::ostringstream ss;
|
||||
|
||||
ss << "Cannot adjust and copy metadata stream file `" << metadata_stream_path.string()
|
||||
<< "` to the CTF trace directory `" << trace_dir.string() << "`: " << exc.what();
|
||||
throw std::runtime_error{ss.str()};
|
||||
}
|
||||
|
||||
// Write HSA handle type event records.
|
||||
WriteHsaHandleTypes();
|
||||
}
|
||||
|
||||
void Plugin::HandleTracerRecord(const rocprofiler_record_tracer_t& record,
|
||||
const rocprofiler_session_id_t session_id) {
|
||||
std::lock_guard<std::mutex> lock{lock_};
|
||||
|
||||
// Depending on the domain, create and add an event record to the
|
||||
// corresponding tracer.
|
||||
switch (record.domain) {
|
||||
case ACTIVITY_DOMAIN_ROCTX:
|
||||
roctx_tracer_.AddEventRecord(std::make_shared<const RocTxEventRecord>(record, session_id));
|
||||
break;
|
||||
case ACTIVITY_DOMAIN_HSA_API: {
|
||||
/*If data is nullptr then the call is asynchromous*/
|
||||
if (record.api_data.hsa == nullptr) {
|
||||
hsa_api_tracer_.AddEventRecord(
|
||||
std::make_shared<const HsaApiEventRecordBegin>(record, session_id));
|
||||
hsa_api_tracer_.AddEventRecord(
|
||||
std::make_shared<const HsaApiEventRecordEnd>(record, session_id));
|
||||
} else {
|
||||
hsa_api_data_t hsa_api_data = *(record.api_data.hsa);
|
||||
hsa_api_tracer_.AddEventRecord(
|
||||
std::make_shared<const HsaApiEventRecordBegin>(record, hsa_api_data));
|
||||
hsa_api_tracer_.AddEventRecord(
|
||||
std::make_shared<const HsaApiEventRecordEnd>(record, hsa_api_data));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ACTIVITY_DOMAIN_HIP_API: {
|
||||
/*If data is nullptr then the call is asynchromous*/
|
||||
if (record.api_data.hip == nullptr) {
|
||||
hip_api_tracer_.AddEventRecord(
|
||||
std::make_shared<const HipApiEventRecordBegin>(record, session_id));
|
||||
hip_api_tracer_.AddEventRecord(
|
||||
std::make_shared<const HipApiEventRecordEnd>(record, session_id));
|
||||
} else {
|
||||
std::string kernel_name;
|
||||
hip_api_data_t hip_api_data = *(record.api_data.hip);
|
||||
if (record.name != nullptr)
|
||||
kernel_name = rocprofiler::truncate_name(rocprofiler::cxx_demangle(std::string(record.name)));
|
||||
else
|
||||
kernel_name = "";
|
||||
hip_api_tracer_.AddEventRecord(
|
||||
std::make_shared<const HipApiEventRecordBegin>(record, hip_api_data, kernel_name));
|
||||
hip_api_tracer_.AddEventRecord(
|
||||
std::make_shared<const HipApiEventRecordEnd>(record, hip_api_data, kernel_name));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ACTIVITY_DOMAIN_HSA_OPS:
|
||||
api_ops_tracer_.AddEventRecord(std::make_shared<const HsaOpEventRecordBegin>(record));
|
||||
api_ops_tracer_.AddEventRecord(std::make_shared<const HsaOpEventRecordEnd>(record));
|
||||
break;
|
||||
case ACTIVITY_DOMAIN_HIP_OPS:
|
||||
api_ops_tracer_.AddEventRecord(std::make_shared<const HipOpEventRecordBegin>(record));
|
||||
api_ops_tracer_.AddEventRecord(std::make_shared<const HipOpEventRecordEnd>(record));
|
||||
break;
|
||||
default:
|
||||
// Warn
|
||||
std::cerr << "rocm_ctf::Plugin::HandleTracerRecord(): "
|
||||
<< "ignoring record for unknown domain #" << record.domain << std::endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Plugin::HandleProfilerRecord(const rocprofiler_record_profiler_t& record,
|
||||
const rocprofiler_session_id_t session_id) {
|
||||
std::lock_guard<std::mutex> lock{lock_};
|
||||
profiler_tracer_.AddEventRecord(
|
||||
std::make_shared<const ProfilerWithKernelPropsEventRecord>(record, session_id));
|
||||
}
|
||||
|
||||
void Plugin::HandleBufferRecords(const rocprofiler_record_header_t* begin,
|
||||
const rocprofiler_record_header_t* const end,
|
||||
const rocprofiler_session_id_t session_id,
|
||||
const rocprofiler_buffer_id_t buffer_id) {
|
||||
while (begin && begin < end) {
|
||||
if (begin->kind == ROCPROFILER_TRACER_RECORD) {
|
||||
HandleTracerRecord(*reinterpret_cast<const rocprofiler_record_tracer_t*>(begin), session_id);
|
||||
} else {
|
||||
assert(begin->kind == ROCPROFILER_PROFILER_RECORD);
|
||||
HandleProfilerRecord(*reinterpret_cast<const rocprofiler_record_profiler_t*>(begin),
|
||||
session_id);
|
||||
}
|
||||
|
||||
rocprofiler_next_record(begin, &begin, session_id, buffer_id);
|
||||
}
|
||||
}
|
||||
|
||||
void Plugin::WriteHsaHandleTypes() {
|
||||
[[maybe_unused]] const auto status = hsa_iterate_agents(
|
||||
[](const auto agent, const auto user_data) {
|
||||
auto& tracer = *static_cast<HsaHandlesTracer*>(user_data);
|
||||
hsa_device_type_t type;
|
||||
|
||||
if (hsa_agent_get_info(agent, HSA_AGENT_INFO_DEVICE, &type) != HSA_STATUS_SUCCESS) {
|
||||
return HSA_STATUS_ERROR;
|
||||
}
|
||||
|
||||
using Type = HsaHandleTypeEventRecord::Type;
|
||||
|
||||
auto event_record = std::make_shared<HsaHandleTypeEventRecord>(
|
||||
agent.handle, type == HSA_DEVICE_TYPE_CPU ? Type::CPU : Type::GPU);
|
||||
|
||||
tracer.AddEventRecord(std::move(event_record));
|
||||
return HSA_STATUS_SUCCESS;
|
||||
},
|
||||
&hsa_handles_tracer_);
|
||||
|
||||
assert(status == HSA_STATUS_SUCCESS && "Iterate HSA agents");
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr std::uint64_t ns_per_s = 1'000'000'000ULL;
|
||||
|
||||
// Samples the ROCProfiler clock and returns the value.
|
||||
std::uint64_t GetClkVal() {
|
||||
rocprofiler_timestamp_t ts;
|
||||
[[maybe_unused]] const auto ret = rocprofiler_get_timestamp(&ts);
|
||||
|
||||
assert(ret == ROCPROFILER_STATUS_SUCCESS && "Get timestamp");
|
||||
return ts.value;
|
||||
}
|
||||
|
||||
// Updates `offset` and `delta`, if needed, to a more accurate clock
|
||||
// class offset and a smaller ROCProfiler clock value delta.
|
||||
//
|
||||
// This function samples the ROCProfiler clock twice, also sampling the
|
||||
// real-time clock in between, and uses the average ROCProfiler clock
|
||||
// value to approximate the actual clock class offset.
|
||||
//
|
||||
// This strategy is based on the measure_single_clock_offset() function
|
||||
// of the LTTng-tools project <https://lttng.org/>.
|
||||
void UpdateClkClsOffsetAndDelta(std::uint64_t& offset, std::uint64_t& delta) {
|
||||
// Sample ROCProfiler clock (first time).
|
||||
const auto rocm_clk_val1 = GetClkVal();
|
||||
|
||||
// Sample real-time clock.
|
||||
timespec realtime_spec = {0, 0};
|
||||
[[maybe_unused]] const auto ret = clock_gettime(CLOCK_REALTIME, &realtime_spec);
|
||||
|
||||
assert(ret == 0);
|
||||
|
||||
// Sample ROCProfiler clock (second time).
|
||||
const auto rocm_clk_val2 = GetClkVal();
|
||||
|
||||
// Compute the current ROCProfiler clock value delta.
|
||||
const auto this_delta = rocm_clk_val2 - rocm_clk_val1;
|
||||
|
||||
if (this_delta > delta) {
|
||||
// Discard larger delta.
|
||||
return;
|
||||
}
|
||||
|
||||
// Compute the average ROCProfiler clock value.
|
||||
const auto rocm_clk_val_avg = (rocm_clk_val1 + rocm_clk_val2) >> 1;
|
||||
|
||||
// Compute the real-time clock value in nanoseconds.
|
||||
const auto realtime_ns =
|
||||
(static_cast<std::uint64_t>(realtime_spec.tv_sec) * ns_per_s) + realtime_spec.tv_nsec;
|
||||
|
||||
// Update clock class offset and delta.
|
||||
assert(rocm_clk_val_avg < realtime_ns);
|
||||
offset = realtime_ns - rocm_clk_val_avg;
|
||||
delta = this_delta;
|
||||
}
|
||||
|
||||
// Computes and returns the most possible accurate clock class offset.
|
||||
std::uint64_t GetMetadataClkClsOffset() {
|
||||
std::uint64_t offset = 0;
|
||||
std::uint64_t delta = std::numeric_limits<std::uint64_t>::max();
|
||||
|
||||
// Best effort to find the most accurate offset.
|
||||
for (auto i = 0U; i < 50U; ++i) {
|
||||
UpdateClkClsOffsetAndDelta(offset, delta);
|
||||
}
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
static const char* LOOP_MPI_RANK(const std::vector<const char*>& mpivars) {
|
||||
for (const char* env : mpivars)
|
||||
if (const char* envvar = getenv(env)) return envvar;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static void insert_meta_to_stream(std::stringstream& stream, const char* field, const char* value) {
|
||||
if (!field || !value) return;
|
||||
stream << "\n\t" << std::string(field) << " = " << std::string(value) << ';';
|
||||
}
|
||||
|
||||
void Plugin::CopyAdjustedMetadataStreamFile(const fs::path& metadata_stream_path,
|
||||
const fs::path& trace_dir) {
|
||||
// Load installed metadata stream file contents.
|
||||
std::string metadata;
|
||||
std::getline(std::ifstream{metadata_stream_path}, metadata, '\0');
|
||||
|
||||
// Replace the original `offset` property.
|
||||
{
|
||||
static constexpr auto offset_term = "offset = 0;";
|
||||
std::ostringstream ss;
|
||||
|
||||
ss << "offset = " << GetMetadataClkClsOffset() << ';';
|
||||
metadata.replace(metadata.find(offset_term), std::strlen(offset_term), ss.str());
|
||||
}
|
||||
|
||||
std::stringstream data_stream;
|
||||
const char* rank = LOOP_MPI_RANK({"MPI_RANK", "OMPI_COMM_WORLD_RANK", "MV2_COMM_WORLD_RANK"});
|
||||
// Add MPI information to metadata
|
||||
if (rank) {
|
||||
insert_meta_to_stream(data_stream, "rank", rank);
|
||||
insert_meta_to_stream(data_stream, "node_rank", getenv("OMPI_COMM_WORLD_NODE_RANK"));
|
||||
|
||||
const char* local = LOOP_MPI_RANK({"OMPI_COMM_WORLD_LOCAL_RANK", "MV2_COMM_WORLD_LOCAL_RANK"});
|
||||
insert_meta_to_stream(data_stream, "local_rank", local);
|
||||
|
||||
std::string data_ins = data_stream.str();
|
||||
size_t env_pos = metadata.find("env {");
|
||||
if (env_pos != std::string::npos)
|
||||
metadata.insert(metadata.begin() + env_pos + 5, data_ins.begin(), data_ins.end());
|
||||
else
|
||||
std::cerr << "Failed to insert MPI metadata!" << std::endl;
|
||||
}
|
||||
|
||||
// Write adjusted metadata stream to trace directory.
|
||||
{
|
||||
std::ofstream output{trace_dir / "metadata"};
|
||||
|
||||
output.write(metadata.data(), metadata.size());
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace rocm_ctf
|
||||
@@ -0,0 +1,148 @@
|
||||
/* 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. */
|
||||
|
||||
#ifndef PLUGIN_CTF_PLUGIN_H
|
||||
#define PLUGIN_CTF_PLUGIN_H
|
||||
|
||||
#include <mutex>
|
||||
#include <cstdlib>
|
||||
|
||||
#include "src/utils/filesystem.hpp"
|
||||
|
||||
#include "rocprofiler.h"
|
||||
#include "rocprofiler_plugin.h"
|
||||
|
||||
#include "barectf.h"
|
||||
#include "barectf_tracer.h"
|
||||
|
||||
namespace rocm_ctf {
|
||||
|
||||
// CTF plugin.
|
||||
//
|
||||
// Build a plugin instance, and then call HandleTracerRecord(),
|
||||
// HandleProfilerRecord(), and HandleBufferRecords() to add event
|
||||
// records.
|
||||
//
|
||||
// A plugin instance performs important tasks at destruction time.
|
||||
class Plugin final {
|
||||
public:
|
||||
// Builds a plugin instance to write a CTF trace in the `trace_dir`
|
||||
// directory with packets of size `packet_size` bytes.
|
||||
//
|
||||
// `trace_dir` must not exist.
|
||||
//
|
||||
// This constructor immediately adjusts and copies the metadata stream
|
||||
// file `metadata_stream_path` to the trace directory (`trace_dir`).
|
||||
explicit Plugin(std::size_t packet_size, const rocprofiler::common::filesystem::path& trace_dir,
|
||||
const rocprofiler::common::filesystem::path& metadata_stream_path);
|
||||
|
||||
// Handles a tracer record.
|
||||
void HandleTracerRecord(const rocprofiler_record_tracer_t& record,
|
||||
rocprofiler_session_id_t session_id);
|
||||
|
||||
|
||||
// Handles a profiler record.
|
||||
void HandleProfilerRecord(const rocprofiler_record_profiler_t& record,
|
||||
rocprofiler_session_id_t session_id);
|
||||
|
||||
// Handles tracer or profiler records from `begin` to `end`
|
||||
// (excluded).
|
||||
void HandleBufferRecords(const rocprofiler_record_header_t* begin,
|
||||
const rocprofiler_record_header_t* end,
|
||||
rocprofiler_session_id_t session_id, rocprofiler_buffer_id_t buffer_id);
|
||||
|
||||
private:
|
||||
// rocTX barectf platform descriptor.
|
||||
struct RocTxPlatformDescr final {
|
||||
using Ctx = barectf_roctx_ctx;
|
||||
|
||||
static void OpenPacket(Ctx& ctx) { barectf_roctx_open_packet(&ctx); }
|
||||
static void ClosePacket(Ctx& ctx) { barectf_roctx_close_packet(&ctx); }
|
||||
};
|
||||
|
||||
// HSA API barectf platform descriptor.
|
||||
struct HsaApiPlatformDescr final {
|
||||
using Ctx = barectf_hsa_api_ctx;
|
||||
|
||||
static void OpenPacket(Ctx& ctx) { barectf_hsa_api_open_packet(&ctx); }
|
||||
static void ClosePacket(Ctx& ctx) { barectf_hsa_api_close_packet(&ctx); }
|
||||
};
|
||||
|
||||
// HIP API barectf platform descriptor.
|
||||
struct HipApiPlatformDescr final {
|
||||
using Ctx = barectf_hip_api_ctx;
|
||||
|
||||
static void OpenPacket(Ctx& ctx) { barectf_hip_api_open_packet(&ctx); }
|
||||
static void ClosePacket(Ctx& ctx) { barectf_hip_api_close_packet(&ctx); }
|
||||
};
|
||||
|
||||
// HSA handles barectf platform descriptor.
|
||||
struct HsaHandlesPlatformDescr final {
|
||||
using Ctx = barectf_hsa_handles_ctx;
|
||||
|
||||
static void OpenPacket(Ctx& ctx) { barectf_hsa_handles_open_packet(&ctx); }
|
||||
static void ClosePacket(Ctx& ctx) { barectf_hsa_handles_close_packet(&ctx); }
|
||||
};
|
||||
|
||||
// API operations barectf platform descriptor.
|
||||
struct ApiOpsPlatformDescr final {
|
||||
using Ctx = barectf_api_ops_ctx;
|
||||
|
||||
static void OpenPacket(Ctx& ctx) { barectf_api_ops_open_packet(&ctx); }
|
||||
static void ClosePacket(Ctx& ctx) { barectf_api_ops_close_packet(&ctx); }
|
||||
};
|
||||
|
||||
// Profiler barectf platform descriptor.
|
||||
struct ProfilerPlatformDescr final {
|
||||
using Ctx = barectf_profiler_ctx;
|
||||
|
||||
static void OpenPacket(Ctx& ctx) { barectf_profiler_open_packet(&ctx); }
|
||||
static void ClosePacket(Ctx& ctx) { barectf_profiler_close_packet(&ctx); }
|
||||
};
|
||||
|
||||
// barectf tracer for HSA handle mappings.
|
||||
using HsaHandlesTracer = BarectfTracer<HsaHandlesPlatformDescr>;
|
||||
|
||||
// Writes the HSA handle type mappings to a dedicated data stream
|
||||
// file.
|
||||
void WriteHsaHandleTypes();
|
||||
|
||||
// Loads the existing metadata stream file `metadata_stream_path`,
|
||||
// adjusts the `offset` property of its single clock class, and writes
|
||||
// the result to the `metadata` file within the `trace_dir` directory.
|
||||
void CopyAdjustedMetadataStreamFile(
|
||||
const rocprofiler::common::filesystem::path& metadata_stream_path,
|
||||
const rocprofiler::common::filesystem::path& trace_dir);
|
||||
|
||||
// Dedicated tracers.
|
||||
BarectfTracer<RocTxPlatformDescr> roctx_tracer_;
|
||||
BarectfTracer<HsaApiPlatformDescr> hsa_api_tracer_;
|
||||
BarectfTracer<HipApiPlatformDescr> hip_api_tracer_;
|
||||
BarectfTracer<ApiOpsPlatformDescr> api_ops_tracer_;
|
||||
HsaHandlesTracer hsa_handles_tracer_;
|
||||
BarectfTracer<ProfilerPlatformDescr> profiler_tracer_;
|
||||
|
||||
// Locks any operation performed on the data of this.
|
||||
std::mutex lock_;
|
||||
};
|
||||
|
||||
} // namespace rocm_ctf
|
||||
|
||||
#endif // PLUGIN_CTF_PLUGIN_H
|
||||
@@ -0,0 +1,12 @@
|
||||
ROCPROFILER_PLUGINS_1.0.0{
|
||||
global: rocprofiler_plugin_initialize;
|
||||
rocprofiler_plugin_finalize;
|
||||
rocprofiler_plugin_write_buffer_records;
|
||||
rocprofiler_plugin_write_record;
|
||||
addDecoder;
|
||||
removeDecoder;
|
||||
getInstructionFromAddr;
|
||||
getInstructionFromID;
|
||||
getSymbolName;
|
||||
local: *;
|
||||
};
|
||||
@@ -0,0 +1,93 @@
|
||||
# ###############################################################################
|
||||
# # 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.
|
||||
# ###############################################################################
|
||||
|
||||
file(GLOB ROCPROFILER_UTIL_SRC_FILES ${PROJECT_SOURCE_DIR}/src/utils/helper.cpp)
|
||||
|
||||
file(GLOB FILE_SOURCES_V1 "file_v1.cpp")
|
||||
add_library(file_plugin_v1 SHARED ${FILE_SOURCES_V1} ${ROCPROFILER_UTIL_SRC_FILES})
|
||||
|
||||
set_target_properties(
|
||||
file_plugin_v1
|
||||
PROPERTIES CXX_VISIBILITY_PRESET hidden
|
||||
DEFINE_SYMBOL "ROCPROFILER_EXPORTS"
|
||||
LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/../exportmap
|
||||
LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib/rocprofiler/v1
|
||||
INSTALL_RPATH "${ROCM_APPEND_PRIVLIB_RPATH}"
|
||||
OUTPUT_NAME file_plugin
|
||||
VERSION 1.0.0
|
||||
SOVERSION 1)
|
||||
|
||||
target_compile_definitions(file_plugin_v1 PRIVATE HIP_PROF_HIP_API_STRING=1
|
||||
__HIP_PLATFORM_HCC__=1)
|
||||
|
||||
target_include_directories(file_plugin_v1 PRIVATE ${PROJECT_SOURCE_DIR})
|
||||
|
||||
target_link_options(
|
||||
file_plugin_v1 PRIVATE -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/../exportmap
|
||||
-Wl,--no-undefined)
|
||||
|
||||
target_link_libraries(file_plugin_v1 PRIVATE rocprofiler-v2 hsa-runtime64::hsa-runtime64
|
||||
stdc++fs amd_comgr dl)
|
||||
|
||||
# Prepare Name Link SO files for V1 file plugin Library
|
||||
add_custom_command(
|
||||
TARGET file_plugin_v1
|
||||
POST_BUILD
|
||||
COMMAND
|
||||
${CMAKE_COMMAND} -E copy
|
||||
${PROJECT_BINARY_DIR}/lib/rocprofiler/v1/libfile_plugin.so.1*
|
||||
${PROJECT_BINARY_DIR}/lib/rocprofiler/.)
|
||||
|
||||
install(TARGETS file_plugin_v1 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}
|
||||
COMPONENT asan NAMELINK_SKIP)
|
||||
install(TARGETS file_plugin_v1 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}
|
||||
COMPONENT runtime NAMELINK_SKIP)
|
||||
|
||||
file(GLOB FILE_SOURCES "file.cpp")
|
||||
add_library(file_plugin SHARED ${FILE_SOURCES} ${ROCPROFILER_UTIL_SRC_FILES})
|
||||
|
||||
set_target_properties(
|
||||
file_plugin
|
||||
PROPERTIES CXX_VISIBILITY_PRESET hidden
|
||||
DEFINE_SYMBOL "ROCPROFILER_EXPORTS"
|
||||
LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/../exportmap
|
||||
LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib/rocprofiler
|
||||
INSTALL_RPATH "${ROCM_APPEND_PRIVLIB_RPATH}"
|
||||
VERSION 2.0.0
|
||||
SOVERSION 2)
|
||||
|
||||
target_compile_definitions(file_plugin PRIVATE HIP_PROF_HIP_API_STRING=1
|
||||
__HIP_PLATFORM_AMD__=1)
|
||||
|
||||
target_include_directories(file_plugin PRIVATE ${PROJECT_SOURCE_DIR})
|
||||
|
||||
target_link_options(
|
||||
file_plugin PRIVATE -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/../exportmap
|
||||
-Wl,--no-undefined)
|
||||
|
||||
target_link_libraries(file_plugin PRIVATE rocprofiler-v2 hsa-runtime64::hsa-runtime64
|
||||
stdc++fs amd_comgr dl)
|
||||
|
||||
install(TARGETS file_plugin LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}
|
||||
COMPONENT asan)
|
||||
install(TARGETS file_plugin LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}
|
||||
COMPONENT runtime)
|
||||
@@ -0,0 +1,545 @@
|
||||
/* 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 <atomic>
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <hsa/hsa.h>
|
||||
#include <mutex>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "rocprofiler.h"
|
||||
#include "rocprofiler_plugin.h"
|
||||
#include "../utils.h"
|
||||
|
||||
#include "src/utils/filesystem.hpp"
|
||||
|
||||
namespace fs = rocprofiler::common::filesystem;
|
||||
|
||||
namespace {
|
||||
|
||||
std::vector<std::string> GetCounterNames() {
|
||||
std::vector<std::string> counters;
|
||||
const char* line_c_str = getenv("ROCPROFILER_COUNTERS");
|
||||
if (line_c_str) {
|
||||
std::string line = line_c_str;
|
||||
// skip commented lines
|
||||
auto found = line.find_first_not_of(" \t");
|
||||
if (found != std::string::npos) {
|
||||
if (line[found] == '#') return {};
|
||||
}
|
||||
if (line.find("pmc") == std::string::npos) return counters;
|
||||
char seperator = ' ';
|
||||
std::string::size_type prev_pos = 0, pos = line.find(seperator, prev_pos);
|
||||
prev_pos = ++pos;
|
||||
if (pos != std::string::npos) {
|
||||
while ((pos = line.find(seperator, pos)) != std::string::npos) {
|
||||
std::string substring(line.substr(prev_pos, pos - prev_pos));
|
||||
if (substring.length() > 0 && substring != ":") {
|
||||
counters.push_back(substring);
|
||||
}
|
||||
prev_pos = ++pos;
|
||||
}
|
||||
if (!line.substr(prev_pos, pos - prev_pos).empty()) {
|
||||
counters.push_back(line.substr(prev_pos, pos - prev_pos));
|
||||
}
|
||||
}
|
||||
}
|
||||
return counters;
|
||||
}
|
||||
|
||||
class file_plugin_t {
|
||||
private:
|
||||
enum class output_type_t { COUNTER, TRACER, PC_SAMPLING };
|
||||
|
||||
class output_file_t {
|
||||
public:
|
||||
output_file_t(std::string name, bool bOpenOnInit = false) : name_(std::move(name)) {
|
||||
if (bOpenOnInit) open();
|
||||
}
|
||||
|
||||
std::string name() const { return name_; }
|
||||
|
||||
template <typename T> std::ostream& operator<<(T&& value) {
|
||||
if (!is_open()) open();
|
||||
return stream_ << std::forward<T>(value);
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& (*func)(std::ostream&)) {
|
||||
if (!is_open()) open();
|
||||
return stream_ << func;
|
||||
}
|
||||
|
||||
void open() {
|
||||
// If the stream is already in the failed state, there's no need to try
|
||||
// to open the file.
|
||||
if (fail()) return;
|
||||
|
||||
const char* output_dir = getenv("OUTPUT_PATH");
|
||||
const char* output_file_name_env = getenv("OUT_FILE_NAME");
|
||||
std::string output_file_name = output_file_name_env ? output_file_name_env : "";
|
||||
|
||||
if (output_dir == nullptr && output_file_name.size() == 0) {
|
||||
stream_.copyfmt(std::cout);
|
||||
stream_.clear(std::cout.rdstate());
|
||||
stream_.basic_ios<char>::rdbuf(std::cout.rdbuf());
|
||||
bPrintToStdout = true;
|
||||
return;
|
||||
}
|
||||
if (output_dir == nullptr) output_dir = ".";
|
||||
|
||||
fs::path output_prefix(output_dir);
|
||||
if (!fs::is_directory(fs::status(output_prefix))) {
|
||||
if (!stream_.fail()) rocprofiler::warning("Cannot open output directory '%s'", output_dir);
|
||||
stream_.setstate(std::ios_base::failbit);
|
||||
return;
|
||||
}
|
||||
|
||||
std::stringstream ss;
|
||||
ss << name_ << "_" << ((output_file_name.empty()) ? std::to_string(GetPid()) : "")
|
||||
<< output_file_name << ".csv";
|
||||
std::cout << "Results File: " << output_prefix / ss.str() << std::endl;
|
||||
stream_.open(output_prefix / ss.str());
|
||||
}
|
||||
|
||||
bool is_open() const { return stream_.is_open(); }
|
||||
bool fail() const { return stream_.fail(); }
|
||||
bool isStdOut() const { return bPrintToStdout; }
|
||||
|
||||
private:
|
||||
const std::string name_;
|
||||
std::ofstream stream_;
|
||||
bool bPrintToStdout = false;
|
||||
};
|
||||
|
||||
output_file_t* get_output_file(output_type_t output_type, uint32_t domain = 0) {
|
||||
switch (output_type) {
|
||||
case output_type_t::COUNTER:
|
||||
return &output_file_;
|
||||
case output_type_t::TRACER:
|
||||
switch (domain) {
|
||||
case ACTIVITY_DOMAIN_ROCTX:
|
||||
return &roctx_file_;
|
||||
case ACTIVITY_DOMAIN_HSA_API:
|
||||
return &hsa_api_file_;
|
||||
case ACTIVITY_DOMAIN_HIP_API:
|
||||
return &hip_api_file_;
|
||||
case ACTIVITY_DOMAIN_HIP_OPS:
|
||||
return &hip_activity_file_;
|
||||
case ACTIVITY_DOMAIN_HSA_OPS:
|
||||
return &hsa_async_copy_file_;
|
||||
default:
|
||||
assert(!"domain/op not supported!");
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case output_type_t::PC_SAMPLING:
|
||||
return &pc_sample_file_;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
public:
|
||||
file_plugin_t(void* data) {
|
||||
if (data) counter_names_ = GetCounterNames();
|
||||
|
||||
kernel_filters_ = GetKernelFilters();
|
||||
const char* str = getenv("ROCPROFILER_INDIVIDUAL_XCC_MODE");
|
||||
if (str != NULL) is_individual_xcc_mode = (atol(str) > 0);
|
||||
|
||||
valid_ = true;
|
||||
}
|
||||
|
||||
void WriteHeader(output_type_t type, rocprofiler_tracer_activity_domain_t domain) {
|
||||
output_file_t* output_file;
|
||||
std::stringstream ss;
|
||||
switch (domain) {
|
||||
case ACTIVITY_DOMAIN_HSA_API: {
|
||||
if (hsa_api_header_written_.load(std::memory_order_relaxed)) return;
|
||||
output_file = get_output_file(output_type_t::TRACER, ACTIVITY_DOMAIN_HSA_API);
|
||||
ss << "Domain,Function,Start_Timestamp,End_Timestamp,Correlation_ID\n\n";
|
||||
*output_file << ss.str();
|
||||
hsa_api_header_written_.exchange(true, std::memory_order_release);
|
||||
return;
|
||||
}
|
||||
case ACTIVITY_DOMAIN_HIP_API: {
|
||||
if (hip_api_header_written_.load(std::memory_order_relaxed)) return;
|
||||
output_file = get_output_file(output_type_t::TRACER, ACTIVITY_DOMAIN_HIP_API);
|
||||
ss << "Domain,Function,Start_Timestamp,End_Timestamp,Correlation_ID\n\n";
|
||||
*output_file << ss.str();
|
||||
hip_api_header_written_.exchange(true, std::memory_order_release);
|
||||
return;
|
||||
}
|
||||
case ACTIVITY_DOMAIN_ROCTX: {
|
||||
if (roctx_header_written_.load(std::memory_order_relaxed)) return;
|
||||
output_file = get_output_file(output_type_t::TRACER, ACTIVITY_DOMAIN_ROCTX);
|
||||
ss << "Domain,ROCTX_ID,Message,Timestamp\n\n";
|
||||
*output_file << ss.str();
|
||||
roctx_header_written_.exchange(true, std::memory_order_release);
|
||||
return;
|
||||
}
|
||||
case ACTIVITY_DOMAIN_HSA_OPS: {
|
||||
if (hsa_async_copy_header_written_.load(std::memory_order_relaxed)) return;
|
||||
output_file = get_output_file(output_type_t::TRACER, ACTIVITY_DOMAIN_HSA_OPS);
|
||||
ss << "Domain,Operation,Start_Timestamp,Stop_Timestamp,Correlation_ID\n\n";
|
||||
*output_file << ss.str();
|
||||
hsa_async_copy_header_written_.exchange(true, std::memory_order_release);
|
||||
return;
|
||||
}
|
||||
case ACTIVITY_DOMAIN_HIP_OPS: {
|
||||
if (hip_activity_header_written_.load(std::memory_order_relaxed)) return;
|
||||
output_file = get_output_file(output_type_t::TRACER, ACTIVITY_DOMAIN_HIP_OPS);
|
||||
ss << "Domain,Operation,Kernel_Name,Start_Timestamp,Stop_Timestamp,"
|
||||
"Correlation_ID\n\n";
|
||||
*output_file << ss.str();
|
||||
hip_activity_header_written_.exchange(true, std::memory_order_release);
|
||||
return;
|
||||
}
|
||||
default: {
|
||||
if (type == output_type_t::COUNTER) {
|
||||
if (kernel_dispatches_header_written_.load(std::memory_order_relaxed)) return;
|
||||
output_file = get_output_file(output_type_t::COUNTER);
|
||||
ss << "Dispatch_ID,GPU_ID,Queue_ID,PID,TID,Grid_Size,Workgroup_Size,LDS_Per_Workgroup,"
|
||||
"Scratch_Per_Workitem,Arch_VGPR,"
|
||||
"Accum_VGPR,SGPR,Wave_Size,Kernel_Name,Start_Timestamp,End_Timestamp,"
|
||||
"Correlation_ID";
|
||||
*output_file << ss.str();
|
||||
kernel_dispatches_header_written_.exchange(true, std::memory_order_release);
|
||||
return;
|
||||
} else if (type == output_type_t::PC_SAMPLING) {
|
||||
if (pc_sample_header_written_.load(std::memory_order_relaxed)) return;
|
||||
output_file = get_output_file(output_type_t::PC_SAMPLING);
|
||||
ss << "Dispatch_ID,Timestamp,GPU_ID,PC_Sample,Shader_Engines\n\n";
|
||||
*output_file << ss.str();
|
||||
pc_sample_header_written_.exchange(true, std::memory_order_release);
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::mutex writing_lock;
|
||||
|
||||
const char* GetDomainName(rocprofiler_tracer_activity_domain_t domain) {
|
||||
switch (domain) {
|
||||
case ACTIVITY_DOMAIN_ROCTX:
|
||||
return "ROCTX_DOMAIN";
|
||||
break;
|
||||
case ACTIVITY_DOMAIN_HIP_API:
|
||||
return "HIP_API_DOMAIN";
|
||||
break;
|
||||
case ACTIVITY_DOMAIN_HIP_OPS:
|
||||
return "HIP_OPS_DOMAIN";
|
||||
break;
|
||||
case ACTIVITY_DOMAIN_HSA_API:
|
||||
return "HSA_API_DOMAIN";
|
||||
break;
|
||||
case ACTIVITY_DOMAIN_HSA_OPS:
|
||||
return "HSA_OPS_DOMAIN";
|
||||
break;
|
||||
case ACTIVITY_DOMAIN_HSA_EVT:
|
||||
return "HSA_EVT_DOMAIN";
|
||||
break;
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
void FlushTracerRecord(rocprofiler_record_tracer_t tracer_record,
|
||||
rocprofiler_session_id_t session_id,
|
||||
rocprofiler_buffer_id_t buffer_id = rocprofiler_buffer_id_t{0}) {
|
||||
std::lock_guard<std::mutex> lock(writing_lock);
|
||||
std::stringstream ss;
|
||||
if (tracer_record.timestamps.end.value <= 0 && tracer_record.domain != ACTIVITY_DOMAIN_ROCTX)
|
||||
return;
|
||||
WriteHeader(output_type_t::TRACER, tracer_record.domain);
|
||||
std::string roctx_message;
|
||||
if (tracer_record.domain == ACTIVITY_DOMAIN_ROCTX && tracer_record.name) {
|
||||
roctx_message = tracer_record.name;
|
||||
}
|
||||
|
||||
const char* operation_name_c = nullptr;
|
||||
// ROCTX domain Operation ID doesn't have a name
|
||||
// It depends on the user input of the roctx functions.
|
||||
// ROCTX message is the tracer_record.name
|
||||
if (tracer_record.domain != ACTIVITY_DOMAIN_ROCTX) {
|
||||
CHECK_ROCPROFILER(rocprofiler_query_tracer_operation_name(
|
||||
tracer_record.domain, tracer_record.operation_id, &operation_name_c));
|
||||
}
|
||||
output_file_t* output_file = get_output_file(output_type_t::TRACER, tracer_record.domain);
|
||||
*output_file << GetDomainName(tracer_record.domain);
|
||||
if (tracer_record.domain == ACTIVITY_DOMAIN_ROCTX && tracer_record.external_id.id >= 0)
|
||||
ss << "," << tracer_record.external_id.id;
|
||||
if (tracer_record.domain == ACTIVITY_DOMAIN_ROCTX) {
|
||||
if (roctx_message.size() > 1)
|
||||
ss << ",\"" << roctx_message << "\"";
|
||||
else
|
||||
ss << ",";
|
||||
}
|
||||
if (operation_name_c) ss << ",\"" << operation_name_c << "\"";
|
||||
if (tracer_record.name && tracer_record.domain != ACTIVITY_DOMAIN_ROCTX) {
|
||||
ss << ",\"" << rocprofiler::truncate_name(rocprofiler::cxx_demangle(tracer_record.name)) << "\"";
|
||||
} else if (tracer_record.domain == ACTIVITY_DOMAIN_HIP_OPS) {
|
||||
ss << ",";
|
||||
}
|
||||
if (tracer_record.domain != ACTIVITY_DOMAIN_ROCTX) {
|
||||
ss << "," << tracer_record.timestamps.begin.value << ","
|
||||
<< tracer_record.timestamps.end.value;
|
||||
ss << "," << tracer_record.correlation_id.value;
|
||||
} else {
|
||||
ss << "," << tracer_record.timestamps.begin.value;
|
||||
}
|
||||
ss << "\n";
|
||||
*output_file << ss.str();
|
||||
}
|
||||
|
||||
void FlushProfilerRecord(const rocprofiler_record_profiler_t* profiler_record,
|
||||
rocprofiler_session_id_t session_id, rocprofiler_buffer_id_t buffer_id) {
|
||||
|
||||
auto check_filter_cache = [&](const char * k_name) {
|
||||
auto str = std::string{k_name};
|
||||
auto f = kernel_filter_cache_.find(k_name);
|
||||
if (f == kernel_filter_cache_.end()) {
|
||||
bool found_match = false;
|
||||
for (const auto& filter : kernel_filters_) {
|
||||
if (str.find(filter) != std::string::npos) {
|
||||
found_match = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
f = kernel_filter_cache_.emplace(str, found_match).first;
|
||||
}
|
||||
return f->second;
|
||||
};
|
||||
|
||||
std::lock_guard<std::mutex> lock(writing_lock);
|
||||
std::stringstream ss;
|
||||
WriteHeader(output_type_t::COUNTER, ACTIVITY_DOMAIN_NUMBER);
|
||||
size_t name_length = 0;
|
||||
output_file_t* output_file{nullptr};
|
||||
output_file = get_output_file(output_type_t::COUNTER);
|
||||
CHECK_ROCPROFILER(rocprofiler_query_kernel_info_size(ROCPROFILER_KERNEL_NAME,
|
||||
profiler_record->kernel_id, &name_length));
|
||||
// Taken from rocprofiler: The size hasn't changed in recent past
|
||||
static const uint32_t lds_block_size = 128 * 4;
|
||||
const char* kernel_name_c = nullptr;
|
||||
if (name_length > 1) {
|
||||
CHECK_ROCPROFILER(rocprofiler_query_kernel_info(ROCPROFILER_KERNEL_NAME,
|
||||
profiler_record->kernel_id, &kernel_name_c));
|
||||
if (kernel_name_c != nullptr && !kernel_filters_.empty() && !check_filter_cache(kernel_name_c)) {
|
||||
free(const_cast<char*>(kernel_name_c));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!counter_header_written_) {
|
||||
if (profiler_record->counters) {
|
||||
if(is_individual_xcc_mode) ss << ',' << "XCC_Index";
|
||||
for (uint64_t i = 0; i < profiler_record->counters_count.value; i++) {
|
||||
auto counter_handler = profiler_record->counters[i].counter_handler;
|
||||
if (!counter_handler.handle) continue;
|
||||
|
||||
size_t counter_name_length = 0;
|
||||
const char* name_c = nullptr;
|
||||
|
||||
CHECK_ROCPROFILER(rocprofiler_query_counter_info_size(
|
||||
session_id, ROCPROFILER_COUNTER_NAME, counter_handler, &counter_name_length));
|
||||
|
||||
if (counter_name_length == 0) continue;
|
||||
|
||||
CHECK_ROCPROFILER(rocprofiler_query_counter_info(session_id, ROCPROFILER_COUNTER_NAME,
|
||||
counter_handler, &name_c));
|
||||
ss << ',' << name_c;
|
||||
}
|
||||
ss << '\n';
|
||||
}
|
||||
counter_header_written_ = true;
|
||||
ss << '\n';
|
||||
}
|
||||
|
||||
ss << std::to_string(profiler_record->header.id.handle) << ","
|
||||
<< std::to_string(profiler_record->gpu_id.handle) << ","
|
||||
<< std::to_string(profiler_record->queue_id.handle) << "," << std::to_string(GetPid()) << ","
|
||||
<< std::to_string(profiler_record->thread_id.value);
|
||||
ss << "," << std::to_string(profiler_record->kernel_properties.grid_size) << ","
|
||||
<< std::to_string(profiler_record->kernel_properties.workgroup_size) << ","
|
||||
<< std::to_string(((profiler_record->kernel_properties.lds_size + (lds_block_size - 1)) &
|
||||
~(lds_block_size - 1)))
|
||||
<< "," << std::to_string(profiler_record->kernel_properties.scratch_size) << ","
|
||||
<< std::to_string(profiler_record->kernel_properties.arch_vgpr_count) << ","
|
||||
<< std::to_string(profiler_record->kernel_properties.accum_vgpr_count) << ","
|
||||
<< std::to_string(profiler_record->kernel_properties.sgpr_count) << ","
|
||||
<< std::to_string(profiler_record->kernel_properties.wave_size);
|
||||
std::string kernel_name = "";
|
||||
if (name_length > 1) {
|
||||
kernel_name = rocprofiler::truncate_name(rocprofiler::cxx_demangle(kernel_name_c));
|
||||
std::string key = "\"";
|
||||
std::size_t found = kernel_name.rfind(key);
|
||||
while (found != std::string::npos) {
|
||||
kernel_name.replace(found, key.length(), "'");
|
||||
found = kernel_name.rfind(key, found - 1);
|
||||
}
|
||||
}
|
||||
ss << ",\"" << kernel_name << "\"," << std::to_string(profiler_record->timestamps.begin.value)
|
||||
<< "," << std::to_string(profiler_record->timestamps.end.value) << ","
|
||||
<< std::to_string(profiler_record->correlation_id.value);
|
||||
|
||||
// For Counters
|
||||
if (profiler_record->counters) {
|
||||
if(is_individual_xcc_mode) ss << "," << profiler_record->xcc_index;
|
||||
for (uint64_t i = 0; i < profiler_record->counters_count.value; i++) {
|
||||
if (profiler_record->counters[i].counter_handler.handle > 0) {
|
||||
ss << "," << std::to_string(profiler_record->counters[i].value.value);
|
||||
}
|
||||
}
|
||||
}
|
||||
ss << '\n';
|
||||
*output_file << ss.str();
|
||||
if (kernel_name_c) {
|
||||
free(const_cast<char*>(kernel_name_c));
|
||||
}
|
||||
}
|
||||
|
||||
void FlushPCSamplingRecord(const rocprofiler_record_pc_sample_t* pc_sampling_record) {
|
||||
WriteHeader(output_type_t::PC_SAMPLING, ACTIVITY_DOMAIN_NUMBER);
|
||||
std::stringstream ss;
|
||||
output_file_t* output_file{nullptr};
|
||||
output_file = get_output_file(output_type_t::PC_SAMPLING);
|
||||
const auto& sample = pc_sampling_record->pc_sample;
|
||||
ss << sample.dispatch_id.value << "," << sample.timestamp.value << "," << sample.gpu_id.handle
|
||||
<< "," << std::hex << std::showbase << sample.pc << "," << sample.se << "\n";
|
||||
*output_file << ss.str();
|
||||
}
|
||||
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: {
|
||||
const rocprofiler_record_profiler_t* profiler_record =
|
||||
reinterpret_cast<const rocprofiler_record_profiler_t*>(begin);
|
||||
FlushProfilerRecord(profiler_record, session_id, buffer_id);
|
||||
break;
|
||||
}
|
||||
case ROCPROFILER_TRACER_RECORD: {
|
||||
rocprofiler_record_tracer_t* tracer_record = const_cast<rocprofiler_record_tracer_t*>(
|
||||
reinterpret_cast<const rocprofiler_record_tracer_t*>(begin));
|
||||
FlushTracerRecord(*tracer_record, session_id, buffer_id);
|
||||
break;
|
||||
}
|
||||
case ROCPROFILER_ATT_TRACER_RECORD: {
|
||||
break;
|
||||
}
|
||||
case ROCPROFILER_PC_SAMPLING_RECORD: {
|
||||
[[deprecated("PC Sampling is deprecated")]]
|
||||
const rocprofiler_record_pc_sample_t* pc_sampling_record =
|
||||
reinterpret_cast<const rocprofiler_record_pc_sample_t*>(begin);
|
||||
FlushPCSamplingRecord(pc_sampling_record);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
rocprofiler_next_record(begin, &begin, session_id, buffer_id);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool is_valid() const { return valid_; }
|
||||
|
||||
private:
|
||||
bool valid_{false};
|
||||
bool counter_header_written_ = false;
|
||||
bool is_individual_xcc_mode=false;
|
||||
std::vector<std::string> counter_names_;
|
||||
std::set<std::string> kernel_filters_;
|
||||
std::map<std::string, bool> kernel_filter_cache_;
|
||||
|
||||
std::atomic<bool> roctx_header_written_{false}, hsa_api_header_written_{false},
|
||||
hip_api_header_written_{false}, hip_activity_header_written_{false},
|
||||
hsa_async_copy_header_written_{false}, pc_sample_header_written_{false},
|
||||
kernel_dispatches_header_written_{false};
|
||||
|
||||
output_file_t roctx_file_{"roctx_trace"}, hsa_api_file_{"hsa_api_trace"},
|
||||
hip_api_file_{"hip_api_trace"}, hip_activity_file_{"hcc_ops_trace"},
|
||||
hsa_async_copy_file_{"async_copy_trace"}, pc_sample_file_{"pcs_trace"},
|
||||
output_file_{"results"};
|
||||
};
|
||||
|
||||
file_plugin_t* file_plugin = nullptr;
|
||||
|
||||
} // namespace
|
||||
|
||||
ROCPROFILER_EXPORT int rocprofiler_plugin_initialize(uint32_t rocprofiler_major_version,
|
||||
uint32_t rocprofiler_minor_version,
|
||||
void* data) {
|
||||
if (rocprofiler_major_version != ROCPROFILER_VERSION_MAJOR ||
|
||||
rocprofiler_minor_version < ROCPROFILER_VERSION_MINOR)
|
||||
return -1;
|
||||
|
||||
if (file_plugin != nullptr) return -1;
|
||||
|
||||
file_plugin = new file_plugin_t(data);
|
||||
if (file_plugin->is_valid()) return 0;
|
||||
|
||||
// The plugin failed to initialized, destroy it and return an error.
|
||||
delete file_plugin;
|
||||
file_plugin = nullptr;
|
||||
return -1;
|
||||
}
|
||||
|
||||
ROCPROFILER_EXPORT void rocprofiler_plugin_finalize() {
|
||||
if (!file_plugin) return;
|
||||
delete file_plugin;
|
||||
file_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 (!file_plugin || !file_plugin->is_valid()) return -1;
|
||||
return file_plugin->WriteBufferRecords(begin, end, session_id, buffer_id);
|
||||
}
|
||||
|
||||
ROCPROFILER_EXPORT int rocprofiler_plugin_write_record(rocprofiler_record_tracer_t record) {
|
||||
if (!file_plugin || !file_plugin->is_valid()) return -1;
|
||||
if (record.header.id.handle == 0) return 0;
|
||||
file_plugin->FlushTracerRecord(record, rocprofiler_session_id_t{0}, rocprofiler_buffer_id_t{0});
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,516 @@
|
||||
/* 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 <atomic>
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <hsa/hsa.h>
|
||||
#include <mutex>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "rocprofiler.h"
|
||||
#include "rocprofiler_plugin.h"
|
||||
#include "../utils.h"
|
||||
|
||||
#include "src/utils/filesystem.hpp"
|
||||
|
||||
namespace fs = rocprofiler::common::filesystem;
|
||||
|
||||
namespace {
|
||||
|
||||
std::vector<std::string> GetCounterNames() {
|
||||
std::vector<std::string> counters;
|
||||
const char* line_c_str = getenv("ROCPROFILER_COUNTERS");
|
||||
if (line_c_str) {
|
||||
std::string line = line_c_str;
|
||||
// skip commented lines
|
||||
auto found = line.find_first_not_of(" \t");
|
||||
if (found != std::string::npos) {
|
||||
if (line[found] == '#') return {};
|
||||
}
|
||||
if (line.find("pmc") == std::string::npos) return counters;
|
||||
char seperator = ' ';
|
||||
std::string::size_type prev_pos = 0, pos = line.find(seperator, prev_pos);
|
||||
prev_pos = ++pos;
|
||||
if (pos != std::string::npos) {
|
||||
while ((pos = line.find(seperator, pos)) != std::string::npos) {
|
||||
std::string substring(line.substr(prev_pos, pos - prev_pos));
|
||||
if (substring.length() > 0 && substring != ":") {
|
||||
counters.push_back(substring);
|
||||
}
|
||||
prev_pos = ++pos;
|
||||
}
|
||||
if (!line.substr(prev_pos, pos - prev_pos).empty()) {
|
||||
counters.push_back(line.substr(prev_pos, pos - prev_pos));
|
||||
}
|
||||
}
|
||||
}
|
||||
return counters;
|
||||
}
|
||||
|
||||
class file_plugin_t {
|
||||
private:
|
||||
enum class output_type_t { COUNTER, TRACER, PC_SAMPLING };
|
||||
|
||||
class output_file_t {
|
||||
public:
|
||||
output_file_t(std::string name, bool bOpenOnInit = false) : name_(std::move(name)) {
|
||||
if (bOpenOnInit) open();
|
||||
}
|
||||
|
||||
std::string name() const { return name_; }
|
||||
|
||||
template <typename T> std::ostream& operator<<(T&& value) {
|
||||
if (!is_open()) open();
|
||||
return stream_ << std::forward<T>(value);
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& (*func)(std::ostream&)) {
|
||||
if (!is_open()) open();
|
||||
return stream_ << func;
|
||||
}
|
||||
|
||||
void open() {
|
||||
// If the stream is already in the failed state, there's no need to try
|
||||
// to open the file.
|
||||
if (fail()) return;
|
||||
|
||||
const char* output_dir = getenv("OUTPUT_PATH");
|
||||
std::string output_file_name = getenv("OUT_FILE_NAME") ? getenv("OUT_FILE_NAME") : "";
|
||||
|
||||
if (output_dir == nullptr && getenv("OUT_FILE_NAME") == nullptr) {
|
||||
stream_.copyfmt(std::cout);
|
||||
stream_.clear(std::cout.rdstate());
|
||||
stream_.basic_ios<char>::rdbuf(std::cout.rdbuf());
|
||||
bPrintToStdout = true;
|
||||
return;
|
||||
}
|
||||
if (output_dir == nullptr) output_dir = ".";
|
||||
|
||||
fs::path output_prefix(output_dir);
|
||||
if (!fs::is_directory(fs::status(output_prefix))) {
|
||||
if (!stream_.fail()) rocprofiler::warning("Cannot open output directory '%s'", output_dir);
|
||||
stream_.setstate(std::ios_base::failbit);
|
||||
return;
|
||||
}
|
||||
|
||||
output_file_name = replace_MPI_macros(output_file_name);
|
||||
|
||||
std::stringstream ss;
|
||||
ss << name_ << "_" << ((output_file_name.empty()) ? std::to_string(GetPid()) : "")
|
||||
<< output_file_name << ".csv";
|
||||
std::cout << "Results File: " << output_prefix / ss.str() << std::endl;
|
||||
stream_.open(output_prefix / ss.str());
|
||||
}
|
||||
|
||||
bool is_open() const { return stream_.is_open(); }
|
||||
bool fail() const { return stream_.fail(); }
|
||||
bool isStdOut() const { return bPrintToStdout; }
|
||||
|
||||
// Returns a string with the MPI %macro replaced with the corresponding envvar
|
||||
std::string replace_MPI_macros(std::string output_file_name) {
|
||||
std::unordered_map<const char*, const char*> MPI_BUILTINS = {
|
||||
{"MPI_RANK", "%rank"},
|
||||
{"OMPI_COMM_WORLD_RANK", "%rank"},
|
||||
{"MV2_COMM_WORLD_RANK", "%rank"}};
|
||||
|
||||
for (const auto& [envvar, key] : MPI_BUILTINS) {
|
||||
size_t key_find = output_file_name.rfind(key);
|
||||
if (key_find == std::string::npos) continue; // Does not contain a %?rank var
|
||||
|
||||
const char* env_var_set = getenv(envvar);
|
||||
if (env_var_set == nullptr) continue; // MPI_COMM_WORLD_x var is does not exist
|
||||
|
||||
int rank = atoi(env_var_set);
|
||||
output_file_name = output_file_name.substr(0, key_find) + std::to_string(rank) +
|
||||
output_file_name.substr(key_find + std::string(key).size());
|
||||
}
|
||||
|
||||
return output_file_name;
|
||||
}
|
||||
|
||||
private:
|
||||
const std::string name_;
|
||||
std::ofstream stream_;
|
||||
bool bPrintToStdout = false;
|
||||
};
|
||||
|
||||
output_file_t* get_output_file(output_type_t output_type, uint32_t domain = 0) {
|
||||
switch (output_type) {
|
||||
case output_type_t::COUNTER:
|
||||
return &output_file_;
|
||||
case output_type_t::TRACER:
|
||||
switch (domain) {
|
||||
case ACTIVITY_DOMAIN_ROCTX:
|
||||
return &roctx_file_;
|
||||
case ACTIVITY_DOMAIN_HSA_API:
|
||||
return &hsa_api_file_;
|
||||
case ACTIVITY_DOMAIN_HIP_API:
|
||||
return &hip_api_file_;
|
||||
case ACTIVITY_DOMAIN_HIP_OPS:
|
||||
return &hip_activity_file_;
|
||||
case ACTIVITY_DOMAIN_HSA_OPS:
|
||||
return &hsa_async_copy_file_;
|
||||
default:
|
||||
assert(!"domain/op not supported!");
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case output_type_t::PC_SAMPLING:
|
||||
return &pc_sample_file_;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
public:
|
||||
file_plugin_t(void* data) {
|
||||
if (data) counter_names_ = GetCounterNames();
|
||||
|
||||
valid_ = true;
|
||||
}
|
||||
|
||||
void WriteHeader(output_type_t type, rocprofiler_tracer_activity_domain_t domain) {
|
||||
output_file_t* output_file;
|
||||
switch (domain) {
|
||||
case ACTIVITY_DOMAIN_HSA_API: {
|
||||
if (hsa_api_header_written_.load(std::memory_order_relaxed)) return;
|
||||
output_file = get_output_file(output_type_t::TRACER, ACTIVITY_DOMAIN_HSA_API);
|
||||
*output_file << "Domain,Function,Start_Timestamp,End_Timestamp,Correlation_ID" << std::endl;
|
||||
*output_file << std::endl;
|
||||
hsa_api_header_written_.exchange(true, std::memory_order_release);
|
||||
return;
|
||||
}
|
||||
case ACTIVITY_DOMAIN_HIP_API: {
|
||||
if (hip_api_header_written_.load(std::memory_order_relaxed)) return;
|
||||
output_file = get_output_file(output_type_t::TRACER, ACTIVITY_DOMAIN_HIP_API);
|
||||
*output_file << "Domain,Function,Start_Timestamp,End_Timestamp,Correlation_ID" << std::endl;
|
||||
*output_file << std::endl;
|
||||
hip_api_header_written_.exchange(true, std::memory_order_release);
|
||||
return;
|
||||
}
|
||||
case ACTIVITY_DOMAIN_ROCTX: {
|
||||
if (roctx_header_written_.load(std::memory_order_relaxed)) return;
|
||||
output_file = get_output_file(output_type_t::TRACER, ACTIVITY_DOMAIN_ROCTX);
|
||||
*output_file << "Domain,ROCTX_ID,Message,Timestamp" << std::endl;
|
||||
*output_file << std::endl;
|
||||
roctx_header_written_.exchange(true, std::memory_order_release);
|
||||
return;
|
||||
}
|
||||
case ACTIVITY_DOMAIN_HSA_OPS: {
|
||||
if (hsa_async_copy_header_written_.load(std::memory_order_relaxed)) return;
|
||||
output_file = get_output_file(output_type_t::TRACER, ACTIVITY_DOMAIN_HSA_OPS);
|
||||
*output_file << "Domain,Operation,Start_Timestamp,Stop_Timestamp,Correlation_ID"
|
||||
<< std::endl;
|
||||
*output_file << std::endl;
|
||||
hsa_async_copy_header_written_.exchange(true, std::memory_order_release);
|
||||
return;
|
||||
}
|
||||
case ACTIVITY_DOMAIN_HIP_OPS: {
|
||||
if (hip_activity_header_written_.load(std::memory_order_relaxed)) return;
|
||||
output_file = get_output_file(output_type_t::TRACER, ACTIVITY_DOMAIN_HIP_OPS);
|
||||
*output_file << "Domain,Operation,Kernel_Name,Start_Timestamp,Stop_Timestamp,"
|
||||
"Correlation_ID"
|
||||
<< std::endl;
|
||||
*output_file << std::endl;
|
||||
hip_activity_header_written_.exchange(true, std::memory_order_release);
|
||||
return;
|
||||
}
|
||||
default: {
|
||||
if (type == output_type_t::COUNTER) {
|
||||
if (kernel_dispatches_header_written_.load(std::memory_order_relaxed)) return;
|
||||
output_file = get_output_file(output_type_t::COUNTER);
|
||||
|
||||
*output_file << "Index,KernelName,gpu-id,queue-id,queue-index,pid,tid,grd,wgr,lds,scr,"
|
||||
"arch_vgpr,accum_vgpr,sgpr,wave_size";
|
||||
if (counter_names_.size() > 0) {
|
||||
for (uint32_t i = 0; i < counter_names_.size(); i++)
|
||||
*output_file << "," << counter_names_[i];
|
||||
}
|
||||
*output_file << ",DispatchNs,BeginNs,EndNs,CompleteNs";
|
||||
*output_file << std::endl;
|
||||
*output_file << std::endl;
|
||||
kernel_dispatches_header_written_.exchange(true, std::memory_order_release);
|
||||
return;
|
||||
} else if (type == output_type_t::PC_SAMPLING) {
|
||||
if (pc_sample_header_written_.load(std::memory_order_relaxed)) return;
|
||||
output_file = get_output_file(output_type_t::PC_SAMPLING);
|
||||
*output_file << "Dispatch_ID,Timestamp,GPU_ID,PC_Sample,Shader_Engines" << std::endl;
|
||||
*output_file << std::endl;
|
||||
pc_sample_header_written_.exchange(true, std::memory_order_release);
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::mutex writing_lock;
|
||||
|
||||
const char* GetDomainName(rocprofiler_tracer_activity_domain_t domain) {
|
||||
switch (domain) {
|
||||
case ACTIVITY_DOMAIN_ROCTX:
|
||||
return "ROCTX_DOMAIN";
|
||||
break;
|
||||
case ACTIVITY_DOMAIN_HIP_API:
|
||||
return "HIP_API_DOMAIN";
|
||||
break;
|
||||
case ACTIVITY_DOMAIN_HIP_OPS:
|
||||
return "HIP_OPS_DOMAIN";
|
||||
break;
|
||||
case ACTIVITY_DOMAIN_HSA_API:
|
||||
return "HSA_API_DOMAIN";
|
||||
break;
|
||||
case ACTIVITY_DOMAIN_HSA_OPS:
|
||||
return "HSA_OPS_DOMAIN";
|
||||
break;
|
||||
case ACTIVITY_DOMAIN_HSA_EVT:
|
||||
return "HSA_EVT_DOMAIN";
|
||||
break;
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
void FlushTracerRecord(rocprofiler_record_tracer_t tracer_record,
|
||||
rocprofiler_session_id_t session_id,
|
||||
rocprofiler_buffer_id_t buffer_id = rocprofiler_buffer_id_t{0}) {
|
||||
std::lock_guard<std::mutex> lock(writing_lock);
|
||||
if (tracer_record.timestamps.end.value <= 0 && tracer_record.domain != ACTIVITY_DOMAIN_ROCTX)
|
||||
return;
|
||||
WriteHeader(output_type_t::TRACER, tracer_record.domain);
|
||||
std::string roctx_message;
|
||||
if (tracer_record.domain == ACTIVITY_DOMAIN_ROCTX && tracer_record.name) {
|
||||
roctx_message = tracer_record.name;
|
||||
}
|
||||
|
||||
const char* operation_name_c = nullptr;
|
||||
// ROCTX domain Operation ID doesn't have a name
|
||||
// It depends on the user input of the roctx functions.
|
||||
// ROCTX message is the tracer_record.name
|
||||
if (tracer_record.domain != ACTIVITY_DOMAIN_ROCTX) {
|
||||
CHECK_ROCPROFILER(rocprofiler_query_tracer_operation_name(
|
||||
tracer_record.domain, tracer_record.operation_id, &operation_name_c));
|
||||
}
|
||||
output_file_t* output_file = get_output_file(output_type_t::TRACER, tracer_record.domain);
|
||||
*output_file << GetDomainName(tracer_record.domain);
|
||||
if (tracer_record.domain == ACTIVITY_DOMAIN_ROCTX && tracer_record.external_id.id >= 0)
|
||||
*output_file << "," << tracer_record.external_id.id;
|
||||
if (tracer_record.domain == ACTIVITY_DOMAIN_ROCTX) {
|
||||
if (roctx_message.size() > 1)
|
||||
*output_file << ",\"" << roctx_message << "\"";
|
||||
else
|
||||
*output_file << ",";
|
||||
}
|
||||
if (operation_name_c) *output_file << ",\"" << operation_name_c << "\"";
|
||||
if (tracer_record.name && tracer_record.domain != ACTIVITY_DOMAIN_ROCTX) {
|
||||
*output_file << ",\"" << rocprofiler::truncate_name(rocprofiler::cxx_demangle(tracer_record.name)) << "\"";
|
||||
} else if (tracer_record.domain == ACTIVITY_DOMAIN_HIP_OPS) {
|
||||
*output_file << ",";
|
||||
}
|
||||
if (tracer_record.domain != ACTIVITY_DOMAIN_ROCTX) {
|
||||
*output_file << "," << tracer_record.timestamps.begin.value << ","
|
||||
<< tracer_record.timestamps.end.value;
|
||||
*output_file << "," << tracer_record.correlation_id.value;
|
||||
} else {
|
||||
*output_file << "," << tracer_record.timestamps.begin.value;
|
||||
}
|
||||
*output_file << std::endl;
|
||||
}
|
||||
|
||||
void FlushProfilerRecord(const rocprofiler_record_profiler_t* profiler_record,
|
||||
rocprofiler_session_id_t session_id, rocprofiler_buffer_id_t buffer_id) {
|
||||
std::lock_guard<std::mutex> lock(writing_lock);
|
||||
WriteHeader(output_type_t::COUNTER, ACTIVITY_DOMAIN_NUMBER);
|
||||
size_t name_length = 0;
|
||||
output_file_t* output_file{nullptr};
|
||||
output_file = get_output_file(output_type_t::COUNTER);
|
||||
CHECK_ROCPROFILER(rocprofiler_query_kernel_info_size(ROCPROFILER_KERNEL_NAME,
|
||||
profiler_record->kernel_id, &name_length));
|
||||
// Taken from rocprofiler: The size hasn't changed in recent past
|
||||
static const uint32_t lds_block_size = 128 * 4;
|
||||
const char* kernel_name_c = nullptr;
|
||||
if (name_length > 1) {
|
||||
CHECK_ROCPROFILER(rocprofiler_query_kernel_info(ROCPROFILER_KERNEL_NAME,
|
||||
profiler_record->kernel_id, &kernel_name_c));
|
||||
}
|
||||
*output_file << std::to_string(profiler_record->header.id.handle) << ",";
|
||||
std::string kernel_name = "";
|
||||
if (name_length > 1) {
|
||||
kernel_name = rocprofiler::truncate_name(rocprofiler::cxx_demangle(kernel_name_c));
|
||||
std::string key = "\"";
|
||||
std::size_t found = kernel_name.rfind(key);
|
||||
while (found != std::string::npos) {
|
||||
kernel_name.replace(found, key.length(), "'");
|
||||
found = kernel_name.rfind(key, found - 1);
|
||||
}
|
||||
}
|
||||
*output_file << "\"" << kernel_name << "\",";
|
||||
*output_file << std::to_string(profiler_record->gpu_id.handle) << ","
|
||||
<< std::to_string(profiler_record->queue_id.handle) << ","
|
||||
<< std::to_string(profiler_record->queue_idx.value) << ","
|
||||
<< std::to_string(GetPid()) << ","
|
||||
<< std::to_string(profiler_record->thread_id.value) << ","
|
||||
<< std::to_string(profiler_record->kernel_properties.grid_size) << ","
|
||||
<< std::to_string(profiler_record->kernel_properties.workgroup_size) << ","
|
||||
<< std::to_string(
|
||||
((profiler_record->kernel_properties.lds_size + (lds_block_size - 1)) &
|
||||
~(lds_block_size - 1)))
|
||||
<< "," << std::to_string(profiler_record->kernel_properties.scratch_size) << ","
|
||||
<< std::to_string(profiler_record->kernel_properties.arch_vgpr_count) << ","
|
||||
<< std::to_string(profiler_record->kernel_properties.accum_vgpr_count) << ","
|
||||
<< std::to_string(profiler_record->kernel_properties.sgpr_count) << ","
|
||||
<< std::to_string(profiler_record->kernel_properties.wave_size);
|
||||
|
||||
// For Counters
|
||||
if (profiler_record->counters) {
|
||||
for (uint64_t i = 0; i < profiler_record->counters_count.value; i++) {
|
||||
if (profiler_record->counters[i].counter_handler.handle > 0) {
|
||||
*output_file << "," << std::to_string(profiler_record->counters[i].value.value);
|
||||
}
|
||||
}
|
||||
}
|
||||
*output_file << ",0,"
|
||||
<< std::to_string(profiler_record->timestamps.begin.value) << ","
|
||||
<< std::to_string(profiler_record->timestamps.end.value) << ",0";
|
||||
*output_file << '\n';
|
||||
if (kernel_name_c) {
|
||||
free(const_cast<char*>(kernel_name_c));
|
||||
}
|
||||
}
|
||||
|
||||
void FlushPCSamplingRecord(const rocprofiler_record_pc_sample_t* pc_sampling_record) {
|
||||
WriteHeader(output_type_t::PC_SAMPLING, ACTIVITY_DOMAIN_NUMBER);
|
||||
output_file_t* output_file{nullptr};
|
||||
output_file = get_output_file(output_type_t::PC_SAMPLING);
|
||||
const auto& sample = pc_sampling_record->pc_sample;
|
||||
*output_file << sample.dispatch_id.value << "," << sample.timestamp.value << ","
|
||||
<< sample.gpu_id.handle << "," << std::hex << std::showbase << sample.pc << ","
|
||||
<< sample.se << std::endl;
|
||||
}
|
||||
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: {
|
||||
const rocprofiler_record_profiler_t* profiler_record =
|
||||
reinterpret_cast<const rocprofiler_record_profiler_t*>(begin);
|
||||
FlushProfilerRecord(profiler_record, session_id, buffer_id);
|
||||
break;
|
||||
}
|
||||
case ROCPROFILER_TRACER_RECORD: {
|
||||
rocprofiler_record_tracer_t* tracer_record = const_cast<rocprofiler_record_tracer_t*>(
|
||||
reinterpret_cast<const rocprofiler_record_tracer_t*>(begin));
|
||||
FlushTracerRecord(*tracer_record, session_id, buffer_id);
|
||||
break;
|
||||
}
|
||||
case ROCPROFILER_ATT_TRACER_RECORD: {
|
||||
break;
|
||||
}
|
||||
case ROCPROFILER_PC_SAMPLING_RECORD: {
|
||||
[[deprecated("PC Sampling is deprecated")]]
|
||||
const rocprofiler_record_pc_sample_t* pc_sampling_record =
|
||||
reinterpret_cast<const rocprofiler_record_pc_sample_t*>(begin);
|
||||
FlushPCSamplingRecord(pc_sampling_record);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
rocprofiler_next_record(begin, &begin, session_id, buffer_id);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool is_valid() const { return valid_; }
|
||||
|
||||
private:
|
||||
bool valid_{false};
|
||||
std::vector<std::string> counter_names_;
|
||||
|
||||
std::atomic<bool> roctx_header_written_{false}, hsa_api_header_written_{false},
|
||||
hip_api_header_written_{false}, hip_activity_header_written_{false},
|
||||
hsa_async_copy_header_written_{false}, pc_sample_header_written_{false},
|
||||
kernel_dispatches_header_written_{false};
|
||||
|
||||
output_file_t roctx_file_{"roctx_trace"}, hsa_api_file_{"hsa_api_trace"},
|
||||
hip_api_file_{"hip_api_trace"}, hip_activity_file_{"hcc_ops_trace"},
|
||||
hsa_async_copy_file_{"async_copy_trace"}, pc_sample_file_{"pcs_trace"},
|
||||
output_file_{"results"};
|
||||
};
|
||||
|
||||
file_plugin_t* file_plugin = nullptr;
|
||||
|
||||
} // namespace
|
||||
|
||||
ROCPROFILER_EXPORT int rocprofiler_plugin_initialize(uint32_t rocprofiler_major_version,
|
||||
uint32_t rocprofiler_minor_version,
|
||||
void* data) {
|
||||
if (rocprofiler_major_version != ROCPROFILER_VERSION_MAJOR ||
|
||||
rocprofiler_minor_version < ROCPROFILER_VERSION_MINOR)
|
||||
return -1;
|
||||
|
||||
if (file_plugin != nullptr) return -1;
|
||||
|
||||
file_plugin = new file_plugin_t(data);
|
||||
if (file_plugin->is_valid()) return 0;
|
||||
|
||||
// The plugin failed to initialized, destroy it and return an error.
|
||||
delete file_plugin;
|
||||
file_plugin = nullptr;
|
||||
return -1;
|
||||
}
|
||||
|
||||
ROCPROFILER_EXPORT void rocprofiler_plugin_finalize() {
|
||||
if (!file_plugin) return;
|
||||
delete file_plugin;
|
||||
file_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 (!file_plugin || !file_plugin->is_valid()) return -1;
|
||||
return file_plugin->WriteBufferRecords(begin, end, session_id, buffer_id);
|
||||
}
|
||||
|
||||
ROCPROFILER_EXPORT int rocprofiler_plugin_write_record(rocprofiler_record_tracer_t record) {
|
||||
if (!file_plugin || !file_plugin->is_valid()) return -1;
|
||||
if (record.header.id.handle == 0) return 0;
|
||||
file_plugin->FlushTracerRecord(record, rocprofiler_session_id_t{0}, rocprofiler_buffer_id_t{0});
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
include_directories(json/include)
|
||||
|
||||
file(GLOB ROCPROFILER_UTIL_SRC_FILES ${PROJECT_SOURCE_DIR}/src/utils/helper.cpp)
|
||||
file(GLOB SPEEDSCOPE_HEADER_FILES
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/json/include/nlohmann/json.hpp)
|
||||
|
||||
add_library(json_plugin ${LIBRARY_TYPE} ${ROCPROFILER_UTIL_SRC_FILES}
|
||||
${SPEEDSCOPE_HEADER_FILES} json.cpp)
|
||||
|
||||
set_target_properties(
|
||||
json_plugin
|
||||
PROPERTIES CXX_VISIBILITY_PRESET hidden
|
||||
LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/../exportmap
|
||||
LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib/rocprofiler
|
||||
INSTALL_RPATH "${ROCM_APPEND_PRIVLIB_RPATH}")
|
||||
|
||||
target_compile_definitions(json_plugin PRIVATE HIP_PROF_HIP_API_STRING=1
|
||||
__HIP_PLATFORM_AMD__=1)
|
||||
|
||||
target_include_directories(json_plugin PRIVATE ${PROJECT_SOURCE_DIR})
|
||||
|
||||
target_link_options(
|
||||
json_plugin PRIVATE -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/../exportmap
|
||||
-Wl,--no-undefined)
|
||||
|
||||
target_link_libraries(json_plugin PRIVATE rocprofiler-v2 Threads::Threads stdc++fs
|
||||
amd_comgr)
|
||||
|
||||
install(TARGETS json_plugin LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}
|
||||
COMPONENT plugins)
|
||||
@@ -0,0 +1,765 @@
|
||||
/* Copyright (c) 2022 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 "rocprofiler.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
#include <thread>
|
||||
#include <unordered_set>
|
||||
|
||||
#include <cxxabi.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "json/include/nlohmann/json.hpp"
|
||||
#include "rocprofiler_plugin.h"
|
||||
#include "../utils.h"
|
||||
|
||||
#include "src/utils/filesystem.hpp"
|
||||
|
||||
namespace fs = rocprofiler::common::filesystem;
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
struct TraceCategoryArgs {
|
||||
std::string name;
|
||||
};
|
||||
|
||||
struct TraceActivity {
|
||||
std::string phase;
|
||||
uint64_t category;
|
||||
std::string name;
|
||||
std::string category_str;
|
||||
std::string timestamp;
|
||||
std::string duration;
|
||||
uint64_t thread_id;
|
||||
uint64_t correlation_id;
|
||||
};
|
||||
|
||||
struct TraceCategory {
|
||||
TraceCategoryArgs args;
|
||||
std::string phase;
|
||||
uint64_t category;
|
||||
int sort_index;
|
||||
};
|
||||
|
||||
enum TraceFlowCategory {
|
||||
ROCPROFILER_DATA_FLOW_START = 0,
|
||||
ROCPROFILER_DATA_FLOW_END = 1,
|
||||
};
|
||||
|
||||
class TraceFlow {
|
||||
public:
|
||||
TraceFlow(uint64_t timestamp, std::string category_str, uint64_t category, uint64_t thread_id,
|
||||
uint64_t id);
|
||||
uint64_t getTimestamp();
|
||||
std::string getCategoryStr();
|
||||
uint64_t getID();
|
||||
uint64_t getCategory();
|
||||
uint64_t getThreadID();
|
||||
void setTimestamp(uint64_t timestamp);
|
||||
void setID(uint64_t id);
|
||||
|
||||
private:
|
||||
uint64_t timestamp_ = 0;
|
||||
std::string category_str_;
|
||||
uint64_t id_;
|
||||
uint64_t category_;
|
||||
uint64_t thread_id_;
|
||||
};
|
||||
|
||||
TraceFlow::TraceFlow(uint64_t timestamp, std::string category_str, uint64_t category,
|
||||
uint64_t thread_id, uint64_t id)
|
||||
: timestamp_(timestamp),
|
||||
category_str_(std::move(category_str)),
|
||||
category_(category),
|
||||
thread_id_(thread_id),
|
||||
id_(id) {}
|
||||
|
||||
uint64_t TraceFlow::getTimestamp() { return timestamp_; }
|
||||
std::string TraceFlow::getCategoryStr() { return category_str_; }
|
||||
uint64_t TraceFlow::getID() { return id_; }
|
||||
uint64_t TraceFlow::getCategory() { return category_; }
|
||||
uint64_t TraceFlow::getThreadID() { return thread_id_; }
|
||||
void TraceFlow::setTimestamp(uint64_t timestamp) { timestamp_ = timestamp; }
|
||||
void TraceFlow::setID(uint64_t id) { id_ = id; }
|
||||
|
||||
std::mutex writing_lock{};
|
||||
|
||||
std::string process_name;
|
||||
|
||||
std::string get_kernel_name(rocprofiler_record_profiler_t& profiler_record) {
|
||||
std::string kernel_name = "";
|
||||
size_t name_length = 1;
|
||||
CHECK_ROCPROFILER(rocprofiler_query_kernel_info_size(ROCPROFILER_KERNEL_NAME,
|
||||
profiler_record.kernel_id, &name_length));
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
|
||||
#pragma GCC diagnostic ignored "-Wstringop-overread"
|
||||
if (name_length > 1) {
|
||||
const char* kernel_name_c = nullptr;
|
||||
CHECK_ROCPROFILER(rocprofiler_query_kernel_info(ROCPROFILER_KERNEL_NAME,
|
||||
profiler_record.kernel_id, &kernel_name_c));
|
||||
if (kernel_name_c && strlen(kernel_name_c) > 1)
|
||||
kernel_name = rocprofiler::truncate_name(rocprofiler::cxx_demangle(kernel_name_c));
|
||||
}
|
||||
#pragma GCC diagnostic pop
|
||||
return kernel_name;
|
||||
}
|
||||
|
||||
class json_plugin_t {
|
||||
public:
|
||||
json_plugin_t() {
|
||||
is_valid_ = true;
|
||||
|
||||
const char* rocprofiler_trace_period = getenv("ROCPROFILER_TRACE_PERIOD");
|
||||
if (rocprofiler_trace_period) trace_period_enabled_ = true;
|
||||
|
||||
const char* enable_data_flow_str = getenv("ROCPROFILER_DISABLE_JSON_DATA_FLOWS");
|
||||
if (enable_data_flow_str) {
|
||||
if (std::string_view(enable_data_flow_str).find("ON") != std::string::npos) {
|
||||
enable_data_flow_ = false;
|
||||
}
|
||||
if (std::string_view(enable_data_flow_str).find("1") != std::string::npos) {
|
||||
enable_data_flow_ = false;
|
||||
}
|
||||
if (std::string_view(enable_data_flow_str).find("OFF") != std::string::npos) {
|
||||
enable_data_flow_ = true;
|
||||
}
|
||||
if (std::string_view(enable_data_flow_str).find("0") != std::string::npos) {
|
||||
enable_data_flow_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
const char* output_dir = getenv("OUTPUT_PATH");
|
||||
const char* temp_file_name = getenv("OUT_FILE_NAME");
|
||||
std::string output_file_name = temp_file_name ? std::string(temp_file_name) + "_" : "";
|
||||
|
||||
if (output_dir == nullptr) output_dir = "./";
|
||||
|
||||
output_prefix_ = output_dir;
|
||||
if (!fs::is_directory(fs::status(output_prefix_))) {
|
||||
if (!stream_.fail()) rocprofiler::warning("Cannot open output directory '%s'", output_dir);
|
||||
stream_.setstate(std::ios_base::failbit);
|
||||
return;
|
||||
}
|
||||
txt_output_prefix_ = output_prefix_;
|
||||
output_prefix_.append(output_file_name + std::to_string(GetPid()) + "_output.json");
|
||||
txt_output_prefix_.append(output_file_name + std::to_string(GetPid()) + "_output");
|
||||
// file.open(output_prefix_.string());
|
||||
|
||||
// This flush technique won't work we need to join JSON files
|
||||
// flush_thread_check_.exchange(true, std::memory_order_acquire);
|
||||
|
||||
// char* flush_interval = getenv("ROCPROFILER_FLUSH_INTERVAL");
|
||||
// uint64_t rocprofiler_flush_interval = 10;
|
||||
// char* end;
|
||||
// if (flush_interval) rocprofiler_flush_interval = std::stoi(flush_interval);
|
||||
|
||||
// flush_thread_ = std::thread(
|
||||
// [&](uint64_t rocprofiler_flush_interval) {
|
||||
// while (flush_thread_check_.load(std::memory_order_acquire)) {
|
||||
// writing_lock.lock();
|
||||
// ExportTraceEventsToJSON();
|
||||
// writing_lock.unlock();
|
||||
// usleep(rocprofiler_flush_interval);
|
||||
// }
|
||||
// },
|
||||
// rocprofiler_flush_interval);
|
||||
}
|
||||
|
||||
void delete_json_plugin() {
|
||||
if (is_valid_) {
|
||||
// flush_thread_check_.exchange(false, std::memory_order_acquire);
|
||||
// flush_thread_.join();
|
||||
ExportTraceEventsToJSON(output_prefix_.string());
|
||||
const char* flame_graph_env = getenv("ROCPROFILER_ENABLE_FLAME_GRAPH");
|
||||
if (flame_graph_env &&
|
||||
(std::string_view(flame_graph_env).find("1") != std::string::npos ||
|
||||
std::string_view(flame_graph_env).find("ON") != std::string::npos))
|
||||
ExportTraceEventsForFlameGraph(txt_output_prefix_.string());
|
||||
// if (file.is_open()) {
|
||||
// file.close();
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
void ExportTraceEventsToJSON(const std::string& path) {
|
||||
nlohmann::json j;
|
||||
j["traceEvents"] = nlohmann::json::array();
|
||||
|
||||
if (!trace_categories_check.load(std::memory_order_acquire)) {
|
||||
for (const auto& event : trace_categories) {
|
||||
nlohmann::json args;
|
||||
args["name"] = event.args.name;
|
||||
j["traceEvents"].push_back({{"args", args},
|
||||
{"ph", event.phase},
|
||||
{"pid", event.category},
|
||||
{"name", "process_name"},
|
||||
{"sort_index", event.sort_index}});
|
||||
}
|
||||
trace_categories_check.exchange(true, std::memory_order_acquire);
|
||||
}
|
||||
|
||||
std::sort(trace_copy_activities_.begin(), trace_copy_activities_.end(),
|
||||
[](auto& a, auto& b) { return (a.timestamp < b.timestamp); });
|
||||
|
||||
std::sort(trace_events_.begin(), trace_events_.end(),
|
||||
[](auto& a, auto& b) { return (a.timestamp < b.timestamp); });
|
||||
|
||||
std::sort(trace_gpu_activities_.begin(), trace_gpu_activities_.end(),
|
||||
[](auto& a, auto& b) { return (a.timestamp < b.timestamp); });
|
||||
|
||||
std::sort(trace_unknown_activities_.begin(), trace_unknown_activities_.end(),
|
||||
[](auto& a, auto& b) { return (a.timestamp < b.timestamp); });
|
||||
|
||||
for (const auto& event : trace_copy_activities_) {
|
||||
nlohmann::json args;
|
||||
args["cid"] = event.correlation_id;
|
||||
j["traceEvents"].push_back({{"name", event.name},
|
||||
{"ph", event.phase},
|
||||
{"ts", event.timestamp},
|
||||
{"tid", event.thread_id},
|
||||
{"pid", event.category},
|
||||
{"dur", event.duration},
|
||||
{"cat", event.category_str},
|
||||
{"args", args}});
|
||||
}
|
||||
|
||||
for (const auto& event : trace_unknown_activities_) {
|
||||
nlohmann::json args;
|
||||
args["cid"] = event.correlation_id;
|
||||
j["traceEvents"].push_back({{"name", event.name},
|
||||
{"ph", event.phase},
|
||||
{"ts", event.timestamp},
|
||||
{"tid", event.thread_id},
|
||||
{"pid", event.category},
|
||||
{"dur", event.duration},
|
||||
{"cat", event.category_str},
|
||||
{"args", args}});
|
||||
}
|
||||
|
||||
for (const auto& event : trace_events_) {
|
||||
nlohmann::json args;
|
||||
args["cid"] = event.correlation_id;
|
||||
j["traceEvents"].push_back({{"name", event.name},
|
||||
{"ph", event.phase},
|
||||
{"ts", event.timestamp},
|
||||
{"tid", event.thread_id},
|
||||
{"pid", event.category},
|
||||
{"dur", event.duration},
|
||||
{"cat", event.category_str},
|
||||
{"args", args}});
|
||||
}
|
||||
|
||||
for (const auto& event : trace_gpu_activities_) {
|
||||
nlohmann::json args;
|
||||
args["cid"] = event.correlation_id;
|
||||
j["traceEvents"].push_back({{"name", event.name},
|
||||
{"ph", event.phase},
|
||||
{"ts", event.timestamp},
|
||||
{"tid", event.thread_id},
|
||||
{"pid", event.category},
|
||||
{"dur", event.duration},
|
||||
{"cat", event.category_str},
|
||||
{"args", args}});
|
||||
}
|
||||
|
||||
if (enable_data_flow_) {
|
||||
for (auto data_flow : trace_data_flows_) {
|
||||
if (data_flow.second.size() > 1) {
|
||||
uint64_t id = trace_flow_counter.fetch_add(1, std::memory_order_acquire);
|
||||
// The following workaround to overcome the timestamp clock issues in Mi300X
|
||||
std::string df0 = "s";
|
||||
std::string df1 = "t";
|
||||
if (data_flow.second[0].getTimestamp() > data_flow.second[1].getTimestamp()) {
|
||||
df1 = "s";
|
||||
df0 = "t";
|
||||
}
|
||||
j["traceEvents"].push_back({{"id", id},
|
||||
{"ph", df0},
|
||||
{"ts", data_flow.second[0].getTimestamp()},
|
||||
{"cat", "DataFlow"},
|
||||
{"pid", data_flow.second[0].getCategory()},
|
||||
{"tid", data_flow.second[0].getThreadID()},
|
||||
{"name", "dep"}});
|
||||
j["traceEvents"].push_back({{"id", id},
|
||||
{"ph", df1},
|
||||
{"ts", data_flow.second[1].getTimestamp()},
|
||||
{"cat", "DataFlow"},
|
||||
{"pid", data_flow.second[1].getCategory()},
|
||||
{"tid", data_flow.second[1].getThreadID()},
|
||||
{"name", "dep"}});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::ofstream file(path);
|
||||
if (file.is_open()) {
|
||||
file << j.dump(2) << std::endl; // Pretty print with 4 spaces
|
||||
file.close();
|
||||
}
|
||||
// trace_gpu_activities_.clear();
|
||||
// trace_copy_activities_.clear();
|
||||
// trace_events_.clear();
|
||||
// trace_data_flows_.clear();
|
||||
}
|
||||
|
||||
void ExportTraceEventsForFlameGraph(const std::string& file_path) {
|
||||
uint64_t sample_rate = 10;
|
||||
const char* flame_graph_sample_rate_env = getenv("ROCPROFILER_FLAME_GRAPH_SAMPLE_RATE");
|
||||
if (flame_graph_sample_rate_env) sample_rate = std::stoull(flame_graph_sample_rate_env);
|
||||
std::thread kernels_graph = std::thread([&]() {
|
||||
const char* flame_graph_enable_kernels = getenv("ROCPROFILER_FLAME_GRAPH_ENABLE_KERNELS");
|
||||
if (flame_graph_enable_kernels &&
|
||||
(std::string_view(flame_graph_enable_kernels).find("0") != std::string::npos ||
|
||||
std::string_view(flame_graph_enable_kernels).find("OFF") != std::string::npos))
|
||||
return;
|
||||
uint64_t kernels_sample_rate = sample_rate;
|
||||
const char* flame_graph_sample_rate_kernels_env =
|
||||
getenv("ROCPROFILER_FLAME_GRAPH_KERNELS_SAMPLE_RATE");
|
||||
if (flame_graph_sample_rate_kernels_env)
|
||||
kernels_sample_rate = std::stoull(flame_graph_sample_rate_kernels_env);
|
||||
std::ofstream kernels_file(file_path + "_kernels.txt");
|
||||
if (!kernels_file.is_open()) {
|
||||
std::cerr << "Failed to open file for writing: " << file_path << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
for (const auto& event : trace_gpu_activities_) {
|
||||
// Convert duration to sample count (for simplicity, assume 1 sample per microsecond)
|
||||
uint64_t duration = std::stoul(event.duration);
|
||||
for (uint64_t i = 0; i < duration; i += kernels_sample_rate) {
|
||||
kernels_file << event.name << ";" << event.name << i << " " << kernels_sample_rate
|
||||
<< "\n"; // Simple example
|
||||
}
|
||||
}
|
||||
|
||||
kernels_file.close();
|
||||
});
|
||||
|
||||
std::thread copy_graph = std::thread([&]() {
|
||||
const char* flame_graph_enable_copy = getenv("ROCPROFILER_FLAME_GRAPH_ENABLE_MEM_COPY");
|
||||
if (flame_graph_enable_copy &&
|
||||
(std::string_view(flame_graph_enable_copy).find("0") != std::string::npos ||
|
||||
std::string_view(flame_graph_enable_copy).find("OFF") != std::string::npos))
|
||||
return;
|
||||
uint64_t copy_sample_rate = sample_rate;
|
||||
const char* flame_graph_sample_rate_copy_env =
|
||||
getenv("ROCPROFILER_FLAME_GRAPH_MEM_COPY_SAMPLE_RATE");
|
||||
if (flame_graph_sample_rate_copy_env)
|
||||
copy_sample_rate = std::stoull(flame_graph_sample_rate_copy_env);
|
||||
std::ofstream copy_file(file_path + "_mem_copies.txt");
|
||||
if (!copy_file.is_open()) {
|
||||
std::cerr << "Failed to open file for writing: " << file_path << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
for (const auto& event : trace_copy_activities_) {
|
||||
uint64_t duration = std::stoul(event.duration);
|
||||
for (uint64_t i = 0; i < duration; i += copy_sample_rate) {
|
||||
copy_file << event.name << ";" << event.name << i << " " << copy_sample_rate << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
copy_file.close();
|
||||
});
|
||||
|
||||
std::thread api_graph = std::thread([&]() {
|
||||
const char* flame_graph_enable_api = getenv("ROCPROFILER_FLAME_GRAPH_ENABLE_API");
|
||||
if (flame_graph_enable_api &&
|
||||
(std::string_view(flame_graph_enable_api).find("0") != std::string::npos ||
|
||||
std::string_view(flame_graph_enable_api).find("OFF") != std::string::npos))
|
||||
return;
|
||||
uint64_t api_sample_rate = sample_rate;
|
||||
const char* flame_graph_sample_rate_api_env =
|
||||
getenv("ROCPROFILER_FLAME_GRAPH_API_SAMPLE_RATE");
|
||||
if (flame_graph_sample_rate_api_env)
|
||||
api_sample_rate = std::stoull(flame_graph_sample_rate_api_env);
|
||||
std::ofstream api_file(file_path + "_api.txt");
|
||||
if (!api_file.is_open()) {
|
||||
std::cerr << "Failed to open file for writing: " << file_path << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
for (const auto& event : trace_events_) {
|
||||
// Convert duration to sample count (for simplicity, assume 1 sample per microsecond)
|
||||
uint64_t duration = std::stoul(event.duration);
|
||||
for (uint64_t i = 0; i < duration; i += api_sample_rate) {
|
||||
api_file << event.name << ";" << event.name << i << " " << api_sample_rate
|
||||
<< "\n"; // Simple example
|
||||
}
|
||||
}
|
||||
|
||||
api_file.close();
|
||||
});
|
||||
|
||||
kernels_graph.join();
|
||||
copy_graph.join();
|
||||
api_graph.join();
|
||||
}
|
||||
|
||||
void LogGpuActivityTrace(const std::string& name, const std::string& category_str,
|
||||
uint64_t timestamp, uint64_t thread_id, uint64_t category,
|
||||
uint64_t duration, uint64_t correlation_id) {
|
||||
if (duration == 0) duration = 1;
|
||||
trace_gpu_activities_.push_back({"X", category, name, category_str, std::to_string(timestamp),
|
||||
std::to_string(duration), thread_id, correlation_id});
|
||||
}
|
||||
|
||||
void LogCopyActivityTrace(const std::string& name, const std::string& category_str,
|
||||
uint64_t timestamp, uint64_t thread_id, uint64_t category,
|
||||
uint64_t duration, uint64_t correlation_id) {
|
||||
if (duration == 0) duration = 1;
|
||||
trace_copy_activities_.push_back({"X", category, name, category_str, std::to_string(timestamp),
|
||||
std::to_string(duration), thread_id, correlation_id});
|
||||
}
|
||||
|
||||
void LogCopyUnknownTrace(const std::string& name, const std::string& category_str,
|
||||
uint64_t timestamp, uint64_t thread_id, uint64_t category,
|
||||
uint64_t duration, uint64_t correlation_id) {
|
||||
if (duration == 0) duration = 1;
|
||||
trace_unknown_activities_.push_back({"X", category, name, category_str,
|
||||
std::to_string(timestamp), std::to_string(duration),
|
||||
thread_id, correlation_id});
|
||||
}
|
||||
|
||||
void LogAPITrace(const std::string& name, const std::string& category_str,
|
||||
uint64_t start_timestamp, uint64_t end_timestamp, uint64_t thread_id,
|
||||
uint64_t category, uint64_t correlation_id) {
|
||||
uint64_t duration = (end_timestamp - start_timestamp);
|
||||
if (duration == 0) duration = 1;
|
||||
trace_events_.push_back({"X", category, name, category_str, std::to_string(start_timestamp),
|
||||
std::to_string(duration), thread_id, correlation_id});
|
||||
}
|
||||
|
||||
void AddDataFlowEvent(uint64_t timestamp, TraceFlowCategory type, uint64_t category,
|
||||
uint64_t thread_id, uint64_t correlation_id) {
|
||||
if (enable_data_flow_) {
|
||||
if (type == ROCPROFILER_DATA_FLOW_START) {
|
||||
trace_data_flows_[correlation_id].emplace_back(
|
||||
TraceFlow(timestamp, "s", category, thread_id, correlation_id));
|
||||
} else {
|
||||
trace_data_flows_[correlation_id].emplace_back(
|
||||
TraceFlow(timestamp, "t", category, thread_id, correlation_id));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// We may need to use this in the Args of the trace
|
||||
const char* GetDomainName(rocprofiler_tracer_activity_domain_t domain) {
|
||||
switch (domain) {
|
||||
case ACTIVITY_DOMAIN_ROCTX:
|
||||
return "ROCTX_DOMAIN";
|
||||
break;
|
||||
case ACTIVITY_DOMAIN_HIP_API:
|
||||
return "HIP_API_DOMAIN";
|
||||
break;
|
||||
case ACTIVITY_DOMAIN_HIP_OPS:
|
||||
return "HIP_OPS_DOMAIN";
|
||||
break;
|
||||
case ACTIVITY_DOMAIN_HSA_API:
|
||||
return "HSA_API_DOMAIN";
|
||||
break;
|
||||
case ACTIVITY_DOMAIN_HSA_OPS:
|
||||
return "HSA_OPS_DOMAIN";
|
||||
break;
|
||||
case ACTIVITY_DOMAIN_HSA_EVT:
|
||||
return "HSA_EVT_DOMAIN";
|
||||
break;
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
int FlushProfilerRecord(rocprofiler_record_profiler_t profiler_record,
|
||||
rocprofiler_session_id_t session_id) {
|
||||
std::lock_guard<std::mutex> lock(writing_lock);
|
||||
|
||||
const uint64_t device_id = profiler_record.gpu_id.handle;
|
||||
const uint64_t queue_id = profiler_record.queue_id.handle;
|
||||
const uint64_t correlation_id = profiler_record.correlation_id.value;
|
||||
|
||||
// Taken from rocprofiler: The size hasn't changed in recent past
|
||||
static const uint32_t lds_block_size = 128 * 4;
|
||||
|
||||
std::string full_kernel_name = get_kernel_name(profiler_record);
|
||||
|
||||
uint64_t start_timestamp = profiler_record.timestamps.begin.value / 1000;
|
||||
uint64_t end_timestamp = profiler_record.timestamps.end.value / 1000;
|
||||
uint64_t duration = end_timestamp - start_timestamp;
|
||||
|
||||
LogGpuActivityTrace(full_kernel_name, "gpu", start_timestamp, device_id + 1, 2, duration,
|
||||
correlation_id);
|
||||
AddDataFlowEvent(start_timestamp, ROCPROFILER_DATA_FLOW_END, 2, device_id + 1, correlation_id);
|
||||
|
||||
// We need to add the information below in the Args of the trace
|
||||
// TRACE_EVENT_BEGIN("KERNELS", perfetto::DynamicString(full_kernel_name.c_str()), queue_track,
|
||||
// profiler_record.timestamps.begin.value, "Full Kernel Name",
|
||||
// full_kernel_name.c_str(), "Agent ID", device_id, "Queue ID",
|
||||
// profiler_record.queue_id.handle, "GRD",
|
||||
// profiler_record.kernel_properties.grid_size, "WGR",
|
||||
// profiler_record.kernel_properties.workgroup_size, "LDS",
|
||||
// (((profiler_record.kernel_properties.lds_size + (lds_block_size - 1)) &
|
||||
// ~(lds_block_size - 1))),
|
||||
// "SCR", profiler_record.kernel_properties.scratch_size, "Arch. VGPR",
|
||||
// profiler_record.kernel_properties.arch_vgpr_count, "Accumulation Vgpr",
|
||||
// profiler_record.kernel_properties.accum_vgpr_count, "SGPR",
|
||||
// profiler_record.kernel_properties.sgpr_count, "Wave Size",
|
||||
// profiler_record.kernel_properties.wave_size, "Signal",
|
||||
// profiler_record.kernel_properties.signal_handle,
|
||||
// perfetto::Flow::ProcessScoped(correlation_id));
|
||||
|
||||
// For Counters
|
||||
if (!profiler_record.counters) return 0;
|
||||
|
||||
for (uint64_t i = 0; i < profiler_record.counters_count.value; i++) {
|
||||
if (profiler_record.counters[i].counter_handler.handle == 0) continue;
|
||||
// We need to add the counter values below in the Args of the trace
|
||||
// TRACE_COUNTER("COUNTERS", counters_track, profiler_record.timestamps.begin.value,
|
||||
// profiler_record.counters[i].value.value);
|
||||
// // Added an extra zero event for maintaining start-end of the counter
|
||||
// TRACE_COUNTER("COUNTERS", counters_track, profiler_record.timestamps.end.value, 0);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int FlushTracerRecord(rocprofiler_record_tracer_t tracer_record,
|
||||
rocprofiler_session_id_t session_id) {
|
||||
std::lock_guard<std::mutex> lock(writing_lock);
|
||||
uint64_t device_id = tracer_record.agent_id.handle;
|
||||
const char* operation_name_c = nullptr;
|
||||
// ROCTX domain Operation ID doesn't have a name
|
||||
// It depends on the user input of the roctx functions.
|
||||
// ROCTX message is the tracer_record.name
|
||||
if (tracer_record.domain != ACTIVITY_DOMAIN_ROCTX) {
|
||||
CHECK_ROCPROFILER(rocprofiler_query_tracer_operation_name(
|
||||
tracer_record.domain, tracer_record.operation_id, &operation_name_c));
|
||||
if (!operation_name_c) operation_name_c = "Unknown Operation";
|
||||
}
|
||||
uint64_t start_timestamp = tracer_record.timestamps.begin.value / 1000;
|
||||
uint64_t end_timestamp = tracer_record.timestamps.end.value / 1000;
|
||||
uint64_t duration = end_timestamp - start_timestamp;
|
||||
uint64_t correlation_id = tracer_record.correlation_id.value;
|
||||
std::string roctx_message;
|
||||
uint64_t roctx_id = 0;
|
||||
uint64_t thread_id = tracer_record.thread_id.value;
|
||||
switch (tracer_record.domain) {
|
||||
case ACTIVITY_DOMAIN_ROCTX: {
|
||||
roctx_id = tracer_record.external_id.id;
|
||||
roctx_message = tracer_record.name ? tracer_record.name : "";
|
||||
LogAPITrace((!roctx_message.empty() ? roctx_message : ""), "CPU", start_timestamp,
|
||||
end_timestamp, thread_id, 1, correlation_id);
|
||||
break;
|
||||
}
|
||||
case ACTIVITY_DOMAIN_HSA_API: {
|
||||
LogAPITrace(operation_name_c, "CPU", start_timestamp, end_timestamp, thread_id, 1,
|
||||
correlation_id);
|
||||
AddDataFlowEvent(start_timestamp, ROCPROFILER_DATA_FLOW_START, 1, thread_id,
|
||||
correlation_id);
|
||||
break;
|
||||
}
|
||||
case ACTIVITY_DOMAIN_HIP_API: {
|
||||
LogAPITrace(operation_name_c, "CPU", start_timestamp, end_timestamp, thread_id, 1,
|
||||
correlation_id);
|
||||
AddDataFlowEvent(start_timestamp, ROCPROFILER_DATA_FLOW_START, 1, thread_id,
|
||||
correlation_id);
|
||||
if (trace_period_enabled_) found_correlation_ids_.insert(correlation_id);
|
||||
break;
|
||||
}
|
||||
case ACTIVITY_DOMAIN_EXT_API: {
|
||||
printf("Warning: External API is not supported!\n");
|
||||
break;
|
||||
}
|
||||
case ACTIVITY_DOMAIN_HIP_OPS: {
|
||||
if (trace_period_enabled_ &&
|
||||
found_correlation_ids_.find(correlation_id) == found_correlation_ids_.end())
|
||||
break;
|
||||
std::size_t pos = std::string::npos;
|
||||
if (tracer_record.name) {
|
||||
auto kernel_name_it = kernel_names_map.find(tracer_record.name);
|
||||
if (kernel_name_it == kernel_names_map.end()) {
|
||||
kernel_name_it =
|
||||
kernel_names_map
|
||||
.emplace(
|
||||
tracer_record.name,
|
||||
rocprofiler::truncate_name(rocprofiler::cxx_demangle(tracer_record.name)))
|
||||
.first;
|
||||
}
|
||||
LogGpuActivityTrace(kernel_name_it->second, "GPU", start_timestamp, device_id + 1, 2,
|
||||
duration, correlation_id);
|
||||
AddDataFlowEvent(start_timestamp, ROCPROFILER_DATA_FLOW_END, 2, device_id + 1,
|
||||
tracer_record.correlation_id.value);
|
||||
} else {
|
||||
// MEM Copies are not correlated to GPUs, so they need a special track
|
||||
pos = operation_name_c ? std::string_view(operation_name_c).find("Copy")
|
||||
: std::string::npos;
|
||||
std::string category_str = "COPY";
|
||||
uint64_t category = 3;
|
||||
uint64_t thread_id_json = 1;
|
||||
|
||||
if (std::string::npos == pos) {
|
||||
LogCopyUnknownTrace(operation_name_c, "HIPBLITKERNELS", start_timestamp, device_id, 4,
|
||||
duration, correlation_id);
|
||||
AddDataFlowEvent(start_timestamp, ROCPROFILER_DATA_FLOW_END, 4, device_id,
|
||||
correlation_id);
|
||||
} else {
|
||||
LogCopyActivityTrace(operation_name_c, category_str, start_timestamp, thread_id_json,
|
||||
category, duration, correlation_id);
|
||||
AddDataFlowEvent(start_timestamp, ROCPROFILER_DATA_FLOW_END, category, thread_id_json,
|
||||
correlation_id);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ACTIVITY_DOMAIN_HSA_OPS: {
|
||||
LogCopyActivityTrace(operation_name_c, "COPY", start_timestamp, 0, 3, duration,
|
||||
correlation_id);
|
||||
AddDataFlowEvent(start_timestamp, ROCPROFILER_DATA_FLOW_END, 3, 0, correlation_id);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
rocprofiler::warning("Ignored record for domain %d", tracer_record.domain);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
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: {
|
||||
rocprofiler_record_profiler_t* profiler_record =
|
||||
const_cast<rocprofiler_record_profiler_t*>(
|
||||
reinterpret_cast<const rocprofiler_record_profiler_t*>(begin));
|
||||
FlushProfilerRecord(*profiler_record, session_id);
|
||||
break;
|
||||
}
|
||||
case ROCPROFILER_TRACER_RECORD: {
|
||||
rocprofiler_record_tracer_t* tracer_record = const_cast<rocprofiler_record_tracer_t*>(
|
||||
reinterpret_cast<const rocprofiler_record_tracer_t*>(begin));
|
||||
FlushTracerRecord(*tracer_record, session_id);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
rocprofiler_next_record(begin, &begin, session_id, buffer_id);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool IsValid() const { return is_valid_; }
|
||||
|
||||
private:
|
||||
bool is_valid_{false};
|
||||
fs::path output_prefix_;
|
||||
fs::path txt_output_prefix_;
|
||||
|
||||
std::atomic<bool> flush_thread_check_{false};
|
||||
std::thread flush_thread_;
|
||||
|
||||
std::ofstream stream_;
|
||||
std::unordered_map<std::string, std::string> kernel_names_map;
|
||||
|
||||
std::atomic<uint64_t> trace_flow_counter{0};
|
||||
std::vector<TraceActivity> trace_events_;
|
||||
std::vector<TraceActivity> trace_gpu_activities_;
|
||||
std::vector<TraceActivity> trace_copy_activities_;
|
||||
std::vector<TraceActivity> trace_unknown_activities_;
|
||||
|
||||
std::map<uint64_t, std::vector<TraceFlow>> trace_data_flows_;
|
||||
|
||||
TraceCategory trace_categories[4] = {{{"CPU"}, "M", 1, 0},
|
||||
{{"GPU"}, "M", 2, 1},
|
||||
{{"COPY"}, "M", 3, 2},
|
||||
{{"HIPBLITKERNELS"}, "M", 4, 3}};
|
||||
std::atomic<bool> trace_categories_check{false};
|
||||
bool enable_data_flow_ = true;
|
||||
|
||||
std::unordered_set<uint64_t> found_correlation_ids_;
|
||||
bool trace_period_enabled_ = false;
|
||||
};
|
||||
|
||||
json_plugin_t* json_plugin = nullptr;
|
||||
|
||||
} // namespace
|
||||
|
||||
int rocprofiler_plugin_initialize(uint32_t rocprofiler_major_version,
|
||||
uint32_t rocprofiler_minor_version, void* data) {
|
||||
if (rocprofiler_major_version != ROCPROFILER_VERSION_MAJOR ||
|
||||
rocprofiler_minor_version > ROCPROFILER_VERSION_MINOR)
|
||||
return -1;
|
||||
|
||||
std::lock_guard<std::mutex> lock(writing_lock);
|
||||
if (json_plugin != nullptr) return -1;
|
||||
|
||||
json_plugin = new json_plugin_t();
|
||||
if (json_plugin->IsValid()) {
|
||||
writing_lock.unlock();
|
||||
return 0;
|
||||
}
|
||||
|
||||
delete json_plugin;
|
||||
json_plugin = nullptr;
|
||||
return -1;
|
||||
}
|
||||
|
||||
void rocprofiler_plugin_finalize() {
|
||||
std::lock_guard<std::mutex> lock(writing_lock);
|
||||
if (!json_plugin) return;
|
||||
json_plugin->delete_json_plugin();
|
||||
delete json_plugin;
|
||||
json_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 (!json_plugin || !json_plugin->IsValid()) return -1;
|
||||
return json_plugin->WriteBufferRecords(begin, end, session_id, buffer_id);
|
||||
}
|
||||
|
||||
ROCPROFILER_EXPORT int rocprofiler_plugin_write_record(rocprofiler_record_tracer_t record) {
|
||||
if (record.header.id.handle == 0) return 0;
|
||||
|
||||
if (!json_plugin || !json_plugin->IsValid()) return -1;
|
||||
return json_plugin->FlushTracerRecord(record, rocprofiler_session_id_t{0});
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2013-2022 Niels Lohmann
|
||||
|
||||
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.
|
||||
@@ -0,0 +1,73 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
@@ -0,0 +1,9 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) <year> <copyright holders>
|
||||
|
||||
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.
|
||||
Rozdílový obsah nebyl zobrazen, protože je příliš veliký
Načíst rozdílové porovnání
@@ -0,0 +1,176 @@
|
||||
// __ _____ _____ _____
|
||||
// __| | __| | | | JSON for Modern C++
|
||||
// | | |__ | | | | | | version 3.11.3
|
||||
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
|
||||
//
|
||||
// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#ifndef INCLUDE_NLOHMANN_JSON_FWD_HPP_
|
||||
#define INCLUDE_NLOHMANN_JSON_FWD_HPP_
|
||||
|
||||
#include <cstdint> // int64_t, uint64_t
|
||||
#include <map> // map
|
||||
#include <memory> // allocator
|
||||
#include <string> // string
|
||||
#include <vector> // vector
|
||||
|
||||
// #include <nlohmann/detail/abi_macros.hpp>
|
||||
// __ _____ _____ _____
|
||||
// __| | __| | | | JSON for Modern C++
|
||||
// | | |__ | | | | | | version 3.11.3
|
||||
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
|
||||
//
|
||||
// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
|
||||
|
||||
// This file contains all macro definitions affecting or depending on the ABI
|
||||
|
||||
#ifndef JSON_SKIP_LIBRARY_VERSION_CHECK
|
||||
#if defined(NLOHMANN_JSON_VERSION_MAJOR) && defined(NLOHMANN_JSON_VERSION_MINOR) && defined(NLOHMANN_JSON_VERSION_PATCH)
|
||||
#if NLOHMANN_JSON_VERSION_MAJOR != 3 || NLOHMANN_JSON_VERSION_MINOR != 11 || NLOHMANN_JSON_VERSION_PATCH != 3
|
||||
#warning "Already included a different version of the library!"
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define NLOHMANN_JSON_VERSION_MAJOR 3 // NOLINT(modernize-macro-to-enum)
|
||||
#define NLOHMANN_JSON_VERSION_MINOR 11 // NOLINT(modernize-macro-to-enum)
|
||||
#define NLOHMANN_JSON_VERSION_PATCH 3 // NOLINT(modernize-macro-to-enum)
|
||||
|
||||
#ifndef JSON_DIAGNOSTICS
|
||||
#define JSON_DIAGNOSTICS 0
|
||||
#endif
|
||||
|
||||
#ifndef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
|
||||
#define JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON 0
|
||||
#endif
|
||||
|
||||
#if JSON_DIAGNOSTICS
|
||||
#define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS _diag
|
||||
#else
|
||||
#define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS
|
||||
#endif
|
||||
|
||||
#if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
|
||||
#define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON _ldvcmp
|
||||
#else
|
||||
#define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON
|
||||
#endif
|
||||
|
||||
#ifndef NLOHMANN_JSON_NAMESPACE_NO_VERSION
|
||||
#define NLOHMANN_JSON_NAMESPACE_NO_VERSION 0
|
||||
#endif
|
||||
|
||||
// Construct the namespace ABI tags component
|
||||
#define NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b) json_abi ## a ## b
|
||||
#define NLOHMANN_JSON_ABI_TAGS_CONCAT(a, b) \
|
||||
NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b)
|
||||
|
||||
#define NLOHMANN_JSON_ABI_TAGS \
|
||||
NLOHMANN_JSON_ABI_TAGS_CONCAT( \
|
||||
NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS, \
|
||||
NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON)
|
||||
|
||||
// Construct the namespace version component
|
||||
#define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch) \
|
||||
_v ## major ## _ ## minor ## _ ## patch
|
||||
#define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(major, minor, patch) \
|
||||
NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch)
|
||||
|
||||
#if NLOHMANN_JSON_NAMESPACE_NO_VERSION
|
||||
#define NLOHMANN_JSON_NAMESPACE_VERSION
|
||||
#else
|
||||
#define NLOHMANN_JSON_NAMESPACE_VERSION \
|
||||
NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(NLOHMANN_JSON_VERSION_MAJOR, \
|
||||
NLOHMANN_JSON_VERSION_MINOR, \
|
||||
NLOHMANN_JSON_VERSION_PATCH)
|
||||
#endif
|
||||
|
||||
// Combine namespace components
|
||||
#define NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b) a ## b
|
||||
#define NLOHMANN_JSON_NAMESPACE_CONCAT(a, b) \
|
||||
NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b)
|
||||
|
||||
#ifndef NLOHMANN_JSON_NAMESPACE
|
||||
#define NLOHMANN_JSON_NAMESPACE \
|
||||
nlohmann::NLOHMANN_JSON_NAMESPACE_CONCAT( \
|
||||
NLOHMANN_JSON_ABI_TAGS, \
|
||||
NLOHMANN_JSON_NAMESPACE_VERSION)
|
||||
#endif
|
||||
|
||||
#ifndef NLOHMANN_JSON_NAMESPACE_BEGIN
|
||||
#define NLOHMANN_JSON_NAMESPACE_BEGIN \
|
||||
namespace nlohmann \
|
||||
{ \
|
||||
inline namespace NLOHMANN_JSON_NAMESPACE_CONCAT( \
|
||||
NLOHMANN_JSON_ABI_TAGS, \
|
||||
NLOHMANN_JSON_NAMESPACE_VERSION) \
|
||||
{
|
||||
#endif
|
||||
|
||||
#ifndef NLOHMANN_JSON_NAMESPACE_END
|
||||
#define NLOHMANN_JSON_NAMESPACE_END \
|
||||
} /* namespace (inline namespace) NOLINT(readability/namespace) */ \
|
||||
} // namespace nlohmann
|
||||
#endif
|
||||
|
||||
|
||||
/*!
|
||||
@brief namespace for Niels Lohmann
|
||||
@see https://github.com/nlohmann
|
||||
@since version 1.0.0
|
||||
*/
|
||||
NLOHMANN_JSON_NAMESPACE_BEGIN
|
||||
|
||||
/*!
|
||||
@brief default JSONSerializer template argument
|
||||
|
||||
This serializer ignores the template arguments and uses ADL
|
||||
([argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl))
|
||||
for serialization.
|
||||
*/
|
||||
template<typename T = void, typename SFINAE = void>
|
||||
struct adl_serializer;
|
||||
|
||||
/// a class to store JSON values
|
||||
/// @sa https://json.nlohmann.me/api/basic_json/
|
||||
template<template<typename U, typename V, typename... Args> class ObjectType =
|
||||
std::map,
|
||||
template<typename U, typename... Args> class ArrayType = std::vector,
|
||||
class StringType = std::string, class BooleanType = bool,
|
||||
class NumberIntegerType = std::int64_t,
|
||||
class NumberUnsignedType = std::uint64_t,
|
||||
class NumberFloatType = double,
|
||||
template<typename U> class AllocatorType = std::allocator,
|
||||
template<typename T, typename SFINAE = void> class JSONSerializer =
|
||||
adl_serializer,
|
||||
class BinaryType = std::vector<std::uint8_t>, // cppcheck-suppress syntaxError
|
||||
class CustomBaseClass = void>
|
||||
class basic_json;
|
||||
|
||||
/// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document
|
||||
/// @sa https://json.nlohmann.me/api/json_pointer/
|
||||
template<typename RefStringType>
|
||||
class json_pointer;
|
||||
|
||||
/*!
|
||||
@brief default specialization
|
||||
@sa https://json.nlohmann.me/api/json/
|
||||
*/
|
||||
using json = basic_json<>;
|
||||
|
||||
/// @brief a minimal map-like container that preserves insertion order
|
||||
/// @sa https://json.nlohmann.me/api/ordered_map/
|
||||
template<class Key, class T, class IgnoredLess, class Allocator>
|
||||
struct ordered_map;
|
||||
|
||||
/// @brief specialization that maintains the insertion order of object keys
|
||||
/// @sa https://json.nlohmann.me/api/ordered_json/
|
||||
using ordered_json = basic_json<nlohmann::ordered_map>;
|
||||
|
||||
NLOHMANN_JSON_NAMESPACE_END
|
||||
|
||||
#endif // INCLUDE_NLOHMANN_JSON_FWD_HPP_
|
||||
@@ -0,0 +1,35 @@
|
||||
rocprofiler_checkout_git_submodule(
|
||||
RECURSIVE
|
||||
TEST_FILE "sdk"
|
||||
RELATIVE_PATH perfetto
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
|
||||
REPO_URL https://github.com/google/perfetto.git
|
||||
REPO_BRANCH "releases/v44.x")
|
||||
|
||||
include_directories(perfetto/sdk)
|
||||
|
||||
file(GLOB ROCPROFILER_UTIL_SRC_FILES ${PROJECT_SOURCE_DIR}/src/utils/helper.cpp)
|
||||
|
||||
add_library(perfetto_plugin ${LIBRARY_TYPE} ${ROCPROFILER_UTIL_SRC_FILES} perfetto/sdk/perfetto.cc perfetto.cpp)
|
||||
|
||||
set_target_properties(
|
||||
perfetto_plugin
|
||||
PROPERTIES CXX_VISIBILITY_PRESET hidden
|
||||
LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/../exportmap
|
||||
LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib/rocprofiler
|
||||
INSTALL_RPATH "${ROCM_APPEND_PRIVLIB_RPATH}")
|
||||
|
||||
target_compile_definitions(perfetto_plugin PRIVATE HIP_PROF_HIP_API_STRING=1
|
||||
__HIP_PLATFORM_AMD__=1)
|
||||
|
||||
target_include_directories(perfetto_plugin PRIVATE ${PROJECT_SOURCE_DIR})
|
||||
|
||||
target_link_options(
|
||||
perfetto_plugin PRIVATE -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/../exportmap
|
||||
-Wl,--no-undefined)
|
||||
|
||||
target_link_libraries(perfetto_plugin PRIVATE rocprofiler-v2
|
||||
Threads::Threads stdc++fs amd_comgr)
|
||||
|
||||
install(TARGETS perfetto_plugin
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME} COMPONENT plugins)
|
||||
+1
Submodul projects/rocprofiler/plugin/perfetto/perfetto přidán v eb5ef24c58
@@ -0,0 +1,749 @@
|
||||
/* Copyright (c) 2022 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 "perfetto.h"
|
||||
#include "rocprofiler.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <condition_variable>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <string_view>
|
||||
|
||||
#include <cxxabi.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "perfetto/sdk/perfetto.h"
|
||||
#include "rocprofiler_plugin.h"
|
||||
#include "../utils.h"
|
||||
|
||||
#include "src/utils/filesystem.hpp"
|
||||
|
||||
#define STREAM_CONSTANT 98736677
|
||||
#define QUEUE_CONSTANT 18746479
|
||||
|
||||
namespace fs = rocprofiler::common::filesystem;
|
||||
|
||||
PERFETTO_DEFINE_CATEGORIES(
|
||||
perfetto::Category("GENERIC").SetDescription("GENERAL_CATEGORY"),
|
||||
perfetto::Category("ROCTX_MARK").SetDescription("ACTIVITY_DOMAIN_ROCTX_API"),
|
||||
perfetto::Category("ROCTX_RANGE_PUSH_POP").SetDescription("ACTIVITY_DOMAIN_ROCTX_API"),
|
||||
perfetto::Category("ROCTX_RANGE_START_STOP").SetDescription("ACTIVITY_DOMAIN_ROCTX_API"),
|
||||
perfetto::Category("HSA_API").SetDescription("ACTIVITY_DOMAIN_HSA_API"),
|
||||
perfetto::Category("HIP_API").SetDescription("ACTIVITY_DOMAIN_HIP_API"),
|
||||
perfetto::Category("External_API").SetDescription("ACTIVITY_DOMAIN_EXT_API"),
|
||||
perfetto::Category("HIP_OPS").SetDescription("ACTIVITY_DOMAIN_HIP_OPS"),
|
||||
perfetto::Category("HSA_OPS").SetDescription("ACTIVITY_DOMAIN_HSA_OPS"),
|
||||
perfetto::Category("MEM_COPIES").SetDescription("MEMORY_COPY_ASYNCHRONOUS_ACTIVITY"),
|
||||
perfetto::Category("KERNELS").SetDescription("KERNEL_DISPATCHES"),
|
||||
perfetto::Category("COUNTERS").SetDescription("PERFORMANCE_COUNTERS"));
|
||||
|
||||
PERFETTO_TRACK_EVENT_STATIC_STORAGE();
|
||||
|
||||
|
||||
enum class TrackType {
|
||||
DEVICE = 2,
|
||||
MCOPY,
|
||||
HIPAPI,
|
||||
HSAAPI,
|
||||
ROCTX_MARK,
|
||||
ROCTX_PUSH_RANGE,
|
||||
ROCTX_START_RANGE,
|
||||
TRACER_DEV_ID,
|
||||
PROFILER_DEV_ID,
|
||||
};
|
||||
|
||||
struct TrackID {
|
||||
TrackID(TrackType type, uint64_t machine, uint64_t device, uint64_t stream)
|
||||
: type(type), machine(machine), dev(device), stream(stream) {};
|
||||
TrackType type;
|
||||
uint64_t machine;
|
||||
uint64_t dev;
|
||||
uint64_t stream;
|
||||
bool operator==(const TrackID& other) const {
|
||||
return machine == other.machine && dev == other.dev && stream == other.stream &&
|
||||
type == other.type;
|
||||
}
|
||||
};
|
||||
|
||||
template <> struct std::hash<TrackID> {
|
||||
uint64_t operator()(const TrackID& s) const {
|
||||
return static_cast<uint64_t>(s.type) ^ (s.machine + 1) ^ (s.dev << 32) ^ (s.dev >> 32) ^
|
||||
(s.stream << 48) ^ (s.stream >> 16);
|
||||
}
|
||||
};
|
||||
|
||||
namespace {
|
||||
std::mutex writing_lock{};
|
||||
|
||||
std::string process_name;
|
||||
|
||||
std::string get_kernel_name(rocprofiler_record_profiler_t& profiler_record) {
|
||||
std::string kernel_name = "";
|
||||
size_t name_length = 1;
|
||||
CHECK_ROCPROFILER(rocprofiler_query_kernel_info_size(ROCPROFILER_KERNEL_NAME,
|
||||
profiler_record.kernel_id, &name_length));
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
|
||||
#pragma GCC diagnostic ignored "-Wstringop-overread"
|
||||
if (name_length > 1) {
|
||||
const char* kernel_name_c = nullptr;
|
||||
CHECK_ROCPROFILER(rocprofiler_query_kernel_info(ROCPROFILER_KERNEL_NAME,
|
||||
profiler_record.kernel_id, &kernel_name_c));
|
||||
if (kernel_name_c && strlen(kernel_name_c) > 1)
|
||||
kernel_name = rocprofiler::truncate_name(rocprofiler::cxx_demangle(kernel_name_c));
|
||||
}
|
||||
#pragma GCC diagnostic pop
|
||||
return kernel_name;
|
||||
}
|
||||
|
||||
class perfetto_plugin_t {
|
||||
public:
|
||||
perfetto_plugin_t(int filename_ext) {
|
||||
const char* output_dir = getenv("OUTPUT_PATH");
|
||||
const char* temp_file_name = getenv("OUT_FILE_NAME");
|
||||
std::string output_file_name = temp_file_name ? std::string(temp_file_name) + "_" : "";
|
||||
if (filename_ext > 0) output_file_name += "_run" + std::to_string(filename_ext) + "_";
|
||||
|
||||
if (output_dir == nullptr) output_dir = "./";
|
||||
|
||||
output_prefix_ = output_dir;
|
||||
if (!fs::is_directory(fs::status(output_prefix_))) {
|
||||
if (!stream_.fail()) rocprofiler::warning("Cannot open output directory '%s'", output_dir);
|
||||
stream_.setstate(std::ios_base::failbit);
|
||||
return;
|
||||
}
|
||||
|
||||
machine_id_ = gethostid();
|
||||
hostname_.resize(1024);
|
||||
gethostname(hostname_.data(), hostname_.size());
|
||||
|
||||
perfetto::TracingInitArgs args;
|
||||
args.backends |= perfetto::kInProcessBackend;
|
||||
perfetto::Tracing::Initialize(args);
|
||||
perfetto::TrackEvent::Register();
|
||||
|
||||
perfetto::protos::gen::TrackEventConfig track_event_cfg;
|
||||
track_event_cfg.add_enabled_categories("*");
|
||||
|
||||
perfetto::TraceConfig trace_cfg;
|
||||
|
||||
auto buffer_cfg = trace_cfg.add_buffers();
|
||||
uint32_t max_buffer_size = 1024 * 1024; // Default max buffer size is 1 GB
|
||||
buffer_cfg->set_fill_policy(
|
||||
perfetto::protos::gen::TraceConfig_BufferConfig_FillPolicy_RING_BUFFER);
|
||||
const char* max_buffer_size_str = getenv("rocprofiler_PERFETTO_MAX_BUFFER_SIZE_KIB");
|
||||
if (max_buffer_size_str && std::atol(max_buffer_size_str) > 0)
|
||||
max_buffer_size = std::atol(max_buffer_size_str);
|
||||
// Record up to max buffer size determined by user or the 10 GB (default value)
|
||||
buffer_cfg->set_size_kb(max_buffer_size);
|
||||
|
||||
auto* data_source_cfg = trace_cfg.add_data_sources()->mutable_config();
|
||||
data_source_cfg->set_name("track_event");
|
||||
|
||||
output_prefix_.append(output_file_name + std::to_string(GetPid()) + "_output.pftrace");
|
||||
file_descriptor_ = open(output_prefix_.string().c_str(), O_RDWR | O_CREAT | O_TRUNC, 0600);
|
||||
if (file_descriptor_ == -1) rocprofiler::warning("Can't open output file\n");
|
||||
|
||||
tracing_session_ = perfetto::Tracing::NewTrace();
|
||||
trace_cfg.set_unique_session_name(output_prefix_.string());
|
||||
tracing_session_->Setup(trace_cfg, file_descriptor_);
|
||||
tracing_session_->StartBlocking();
|
||||
|
||||
// Give a custom name for the traced process.
|
||||
perfetto::ProcessTrack process_track = perfetto::ProcessTrack::Current();
|
||||
perfetto::protos::gen::TrackDescriptor desc = process_track.Serialize();
|
||||
desc.mutable_process()->set_process_name("Node: " + hostname_);
|
||||
perfetto::TrackEvent::SetTrackDescriptor(process_track, desc);
|
||||
|
||||
is_valid_ = true;
|
||||
}
|
||||
|
||||
bool delete_perfetto_plugin() {
|
||||
if (is_valid_ && tracing_session_) {
|
||||
tracing_session_->StopBlocking();
|
||||
is_valid_ = false;
|
||||
close(file_descriptor_);
|
||||
tracing_session_.reset();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* GetDomainName(rocprofiler_tracer_activity_domain_t domain) {
|
||||
switch (domain) {
|
||||
case ACTIVITY_DOMAIN_ROCTX:
|
||||
return "ROCTX_DOMAIN";
|
||||
break;
|
||||
case ACTIVITY_DOMAIN_HIP_API:
|
||||
return "HIP_API_DOMAIN";
|
||||
break;
|
||||
case ACTIVITY_DOMAIN_HIP_OPS:
|
||||
return "HIP_OPS_DOMAIN";
|
||||
break;
|
||||
case ACTIVITY_DOMAIN_HSA_API:
|
||||
return "HSA_API_DOMAIN";
|
||||
break;
|
||||
case ACTIVITY_DOMAIN_HSA_OPS:
|
||||
return "HSA_OPS_DOMAIN";
|
||||
break;
|
||||
case ACTIVITY_DOMAIN_HSA_EVT:
|
||||
return "HSA_EVT_DOMAIN";
|
||||
break;
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
int FlushProfilerRecord(rocprofiler_record_profiler_t profiler_record,
|
||||
rocprofiler_session_id_t session_id) {
|
||||
// ToDO: rename this variable?
|
||||
if (!tracing_session_) rocprofiler::warning("Tracing session is deleted!\n");
|
||||
|
||||
const uint64_t device_id = profiler_record.gpu_id.handle;
|
||||
const uint64_t queue_id = profiler_record.queue_id.handle;
|
||||
const uint64_t correlation_id = profiler_record.correlation_id.value;
|
||||
|
||||
uint64_t queue_track_id =
|
||||
getTrackID(TrackType::PROFILER_DEV_ID, machine_id_, device_id, queue_id);
|
||||
|
||||
auto queue_track_it = queue_tracks_.find(queue_track_id);
|
||||
if (queue_track_it == queue_tracks_.end()) {
|
||||
/* Create a new perfetto::Track */
|
||||
queue_track_it = queue_tracks_.emplace(queue_track_id, perfetto::Track(queue_track_id)).first;
|
||||
|
||||
auto queue_desc = queue_track_it->second.Serialize();
|
||||
std::stringstream ss;
|
||||
ss << "Dev " << device_id << " Queue " << queue_id;
|
||||
queue_desc.set_name(ss.str());
|
||||
perfetto::TrackEvent::SetTrackDescriptor(queue_track_it->second, queue_desc);
|
||||
track_ids_used_.emplace_back(queue_track_id);
|
||||
}
|
||||
auto& queue_track = queue_track_it->second;
|
||||
|
||||
// Taken from rocprofiler: The size hasn't changed in recent past
|
||||
static const uint32_t lds_block_size = 128 * 4;
|
||||
|
||||
std::string full_kernel_name = get_kernel_name(profiler_record);
|
||||
|
||||
if (correlation_id) {
|
||||
TRACE_EVENT_BEGIN("KERNELS", perfetto::DynamicString(full_kernel_name.c_str()), queue_track,
|
||||
profiler_record.timestamps.begin.value, "Full Kernel Name",
|
||||
full_kernel_name.c_str(), "Agent ID", device_id, "Queue ID",
|
||||
profiler_record.queue_id.handle, "GRD",
|
||||
profiler_record.kernel_properties.grid_size, "WGR",
|
||||
profiler_record.kernel_properties.workgroup_size, "LDS",
|
||||
(((profiler_record.kernel_properties.lds_size + (lds_block_size - 1)) &
|
||||
~(lds_block_size - 1))),
|
||||
"SCR", profiler_record.kernel_properties.scratch_size, "Arch. VGPR",
|
||||
profiler_record.kernel_properties.arch_vgpr_count, "Accumulation Vgpr",
|
||||
profiler_record.kernel_properties.accum_vgpr_count, "SGPR",
|
||||
profiler_record.kernel_properties.sgpr_count, "Wave Size",
|
||||
profiler_record.kernel_properties.wave_size, "Signal",
|
||||
profiler_record.kernel_properties.signal_handle,
|
||||
perfetto::Flow::ProcessScoped(correlation_id));
|
||||
} else {
|
||||
TRACE_EVENT_BEGIN("KERNELS", perfetto::DynamicString(full_kernel_name.c_str()), queue_track,
|
||||
profiler_record.timestamps.begin.value, "Full Kernel Name",
|
||||
full_kernel_name.c_str(), "Agent ID", device_id, "Queue ID",
|
||||
profiler_record.queue_id.handle, "GRD",
|
||||
profiler_record.kernel_properties.grid_size, "WGR",
|
||||
profiler_record.kernel_properties.workgroup_size, "LDS",
|
||||
(((profiler_record.kernel_properties.lds_size + (lds_block_size - 1)) &
|
||||
~(lds_block_size - 1))),
|
||||
"SCR", profiler_record.kernel_properties.scratch_size, "Arch. VGPR",
|
||||
profiler_record.kernel_properties.arch_vgpr_count, "Accumulation Vgpr",
|
||||
profiler_record.kernel_properties.accum_vgpr_count, "SGPR",
|
||||
profiler_record.kernel_properties.sgpr_count, "Wave Size",
|
||||
profiler_record.kernel_properties.wave_size, "Signal",
|
||||
profiler_record.kernel_properties.signal_handle);
|
||||
}
|
||||
|
||||
TRACE_EVENT_END("KERNELS", queue_track, profiler_record.timestamps.end.value);
|
||||
|
||||
auto get_counter_track_fn = [&](size_t i, rocprofiler_counter_id_t counter_handler) {
|
||||
auto& ctrack = counter_tracks_[device_id];
|
||||
|
||||
if (i < ctrack.size()) return;
|
||||
|
||||
const char* name_c = nullptr;
|
||||
CHECK_ROCPROFILER(rocprofiler_query_counter_info(session_id, ROCPROFILER_COUNTER_NAME,
|
||||
counter_handler, &name_c));
|
||||
|
||||
std::stringstream ss;
|
||||
ss << "Dev " << device_id << " Counter " << name_c;
|
||||
ctrack.push_back(perfetto::CounterTrack(ss.str().c_str()));
|
||||
auto counter_track_desc = ctrack.back().Serialize();
|
||||
counter_track_desc.set_name(ss.str());
|
||||
perfetto::TrackEvent::SetTrackDescriptor(ctrack.back(), counter_track_desc);
|
||||
};
|
||||
|
||||
// For Counters
|
||||
if (!profiler_record.counters) return 0;
|
||||
|
||||
for (uint64_t i = 0; i < profiler_record.counters_count.value; i++) {
|
||||
if (profiler_record.counters[i].counter_handler.handle == 0) continue;
|
||||
|
||||
get_counter_track_fn(i, profiler_record.counters[i].counter_handler);
|
||||
auto& counters_track = counter_tracks_.at(device_id).at(i);
|
||||
TRACE_COUNTER("COUNTERS", counters_track, profiler_record.timestamps.begin.value,
|
||||
profiler_record.counters[i].value.value);
|
||||
// Added an extra zero event for maintaining start-end of the counter
|
||||
TRACE_COUNTER("COUNTERS", counters_track, profiler_record.timestamps.end.value, 0);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int FlushTracerRecord(rocprofiler_record_tracer_t tracer_record,
|
||||
rocprofiler_session_id_t session_id) {
|
||||
if (!tracing_session_) rocprofiler::warning("Tracing session is deleted!\n");
|
||||
uint64_t device_id = tracer_record.agent_id.handle;
|
||||
const char* operation_name_c = nullptr;
|
||||
// ROCTX domain Operation ID doesn't have a name
|
||||
// It depends on the user input of the roctx functions.
|
||||
// ROCTX message is the tracer_record.name
|
||||
if (tracer_record.domain != ACTIVITY_DOMAIN_ROCTX) {
|
||||
CHECK_ROCPROFILER(rocprofiler_query_tracer_operation_name(
|
||||
tracer_record.domain, tracer_record.operation_id, &operation_name_c));
|
||||
if (!operation_name_c) operation_name_c = "Unknown Operation";
|
||||
}
|
||||
std::string roctx_message;
|
||||
uint64_t roctx_id = 0;
|
||||
uint64_t thread_id = tracer_record.thread_id.value;
|
||||
std::unordered_map<uint64_t, perfetto::Track>::iterator hip_stream_tracks_it;
|
||||
|
||||
std::unordered_map<uint64_t, perfetto::Track>::iterator mem_copies_track_it;
|
||||
if (tracer_record.domain == ACTIVITY_DOMAIN_HIP_OPS ||
|
||||
tracer_record.domain == ACTIVITY_DOMAIN_HSA_OPS) {
|
||||
bool bIsHSAQueue = tracer_record.domain == ACTIVITY_DOMAIN_HSA_OPS;
|
||||
uint64_t qID = tracer_record.queue_id.handle;
|
||||
|
||||
uint64_t hip_track_id = getTrackID(TrackType::TRACER_DEV_ID, machine_id_, device_id, qID);
|
||||
hip_stream_tracks_it = hip_stream_tracks.find(hip_track_id);
|
||||
if (hip_stream_tracks_it == hip_stream_tracks.end()) {
|
||||
/* Create a new perfetto::Track (Sub-Track) */
|
||||
hip_stream_tracks_it =
|
||||
hip_stream_tracks.emplace(hip_track_id, perfetto::Track(hip_track_id)).first;
|
||||
auto gpu_desc = hip_stream_tracks_it->second.Serialize();
|
||||
std::string queue_str = (bIsHSAQueue ? "Stream " : "HipStream ") + std::to_string(qID);
|
||||
gpu_desc.set_name(queue_str);
|
||||
perfetto::TrackEvent::SetTrackDescriptor(hip_stream_tracks_it->second, gpu_desc);
|
||||
track_ids_used_.emplace_back(hip_track_id);
|
||||
}
|
||||
|
||||
{
|
||||
uint64_t mcpy_track_id = getTrackID(TrackType::MCOPY, machine_id_, 0, thread_id);
|
||||
mem_copies_track_it = mem_copies_tracks_.find(mcpy_track_id);
|
||||
if (mem_copies_track_it == mem_copies_tracks_.end()) {
|
||||
mem_copies_track_it =
|
||||
mem_copies_tracks_.emplace(mcpy_track_id, perfetto::Track(mcpy_track_id)).first;
|
||||
|
||||
auto mem_copies_track_desc = mem_copies_track_it->second.Serialize();
|
||||
std::string mem_copies_track_str =
|
||||
rocprofiler::string_printf("MEM COPIES(%lu): ", thread_id);
|
||||
mem_copies_track_desc.set_name(mem_copies_track_str);
|
||||
perfetto::TrackEvent::SetTrackDescriptor(mem_copies_track_it->second,
|
||||
mem_copies_track_desc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto& gpu_track = hip_stream_tracks_it->second;
|
||||
auto& mem_copies_track = mem_copies_track_it->second;
|
||||
switch (tracer_record.domain) {
|
||||
case ACTIVITY_DOMAIN_ROCTX: {
|
||||
roctx_id = tracer_record.external_id.id;
|
||||
roctx_message = tracer_record.name ? tracer_record.name : "";
|
||||
switch (tracer_record.operation_id.id) {
|
||||
case 0: {
|
||||
std::unordered_map<uint64_t, perfetto::Track>::iterator roctx_track_it;
|
||||
{
|
||||
uint64_t rtx_track_id = getTrackID(TrackType::ROCTX_MARK, machine_id_, 0, thread_id);
|
||||
roctx_track_it = roctx_mark_tracks_.find(rtx_track_id);
|
||||
if (roctx_track_it == roctx_mark_tracks_.end()) {
|
||||
roctx_track_it =
|
||||
roctx_mark_tracks_.emplace(rtx_track_id, perfetto::Track(rtx_track_id)).first;
|
||||
|
||||
auto roctx_track_desc = roctx_track_it->second.Serialize();
|
||||
std::string roctx_track_str = rocprofiler::string_printf("ROCTX Markers");
|
||||
roctx_track_desc.set_name(roctx_track_str);
|
||||
perfetto::TrackEvent::SetTrackDescriptor(roctx_track_it->second, roctx_track_desc);
|
||||
}
|
||||
}
|
||||
auto& roctx_track = roctx_track_it->second;
|
||||
perfetto::DynamicString roctx_message_pft(
|
||||
(!roctx_message.empty() ? roctx_message.c_str() : ""));
|
||||
TRACE_EVENT_INSTANT("ROCTX_MARK", roctx_message_pft, roctx_track,
|
||||
tracer_record.timestamps.begin.value, "Timestamp(ns)",
|
||||
tracer_record.timestamps.begin.value, "RocTx ID", roctx_id);
|
||||
break;
|
||||
}
|
||||
case 1: {
|
||||
std::unordered_map<uint64_t, perfetto::Track>::iterator roctx_track_it;
|
||||
{
|
||||
uint64_t rtx_track_id = getTrackID(TrackType::ROCTX_PUSH_RANGE, machine_id_, 0, thread_id);
|
||||
roctx_track_it = roctx_push_tracks_.find(rtx_track_id);
|
||||
if (roctx_track_it == roctx_push_tracks_.end()) {
|
||||
roctx_track_it =
|
||||
roctx_push_tracks_.emplace(rtx_track_id, perfetto::Track(rtx_track_id)).first;
|
||||
|
||||
auto roctx_track_desc = roctx_track_it->second.Serialize();
|
||||
std::string roctx_track_str = rocprofiler::string_printf("ROCTX Push/Pop Ranges");
|
||||
roctx_track_desc.set_name(roctx_track_str);
|
||||
perfetto::TrackEvent::SetTrackDescriptor(roctx_track_it->second, roctx_track_desc);
|
||||
}
|
||||
}
|
||||
auto& roctx_track = roctx_track_it->second;
|
||||
perfetto::DynamicString roctx_message_pft(
|
||||
(!roctx_message.empty() ? roctx_message.c_str() : ""));
|
||||
TRACE_EVENT_BEGIN("ROCTX_RANGE_PUSH_POP", roctx_message_pft, roctx_track,
|
||||
tracer_record.timestamps.begin.value, "Timestamp(ns)",
|
||||
tracer_record.timestamps.begin.value, "RocTx ID", roctx_id);
|
||||
roctx_push_track_entries_++;
|
||||
break;
|
||||
}
|
||||
case 3: {
|
||||
std::unordered_map<uint64_t, perfetto::Track>::iterator roctx_track_it;
|
||||
{
|
||||
uint64_t rtx_track_id = getTrackID(TrackType::ROCTX_START_RANGE, machine_id_, 0, thread_id);
|
||||
roctx_track_it = roctx_start_tracks_.find(rtx_track_id);
|
||||
if (roctx_track_it == roctx_start_tracks_.end()) {
|
||||
roctx_track_it =
|
||||
roctx_start_tracks_.emplace(rtx_track_id, perfetto::Track(rtx_track_id)).first;
|
||||
|
||||
auto roctx_track_desc = roctx_track_it->second.Serialize();
|
||||
std::string roctx_track_str = rocprofiler::string_printf("ROCTX Start/Stop Ranges");
|
||||
roctx_track_desc.set_name(roctx_track_str);
|
||||
perfetto::TrackEvent::SetTrackDescriptor(roctx_track_it->second, roctx_track_desc);
|
||||
}
|
||||
}
|
||||
auto& roctx_track = roctx_track_it->second;
|
||||
perfetto::DynamicString roctx_message_pft(
|
||||
(!roctx_message.empty() ? roctx_message.c_str() : ""));
|
||||
TRACE_EVENT_BEGIN("ROCTX_RANGE_START_STOP", roctx_message_pft, roctx_track,
|
||||
tracer_record.timestamps.begin.value, "Timestamp(ns)",
|
||||
tracer_record.timestamps.begin.value, "RocTx ID", roctx_id);
|
||||
roctx_start_track_entries_++;
|
||||
break;
|
||||
}
|
||||
case 2: {
|
||||
std::unordered_map<uint64_t, perfetto::Track>::iterator roctx_track_it;
|
||||
{
|
||||
uint64_t rtx_track_id = getTrackID(TrackType::ROCTX_PUSH_RANGE, machine_id_, 0, thread_id);
|
||||
roctx_track_it = roctx_push_tracks_.find(rtx_track_id);
|
||||
if (roctx_track_it == roctx_push_tracks_.end()) {
|
||||
roctx_track_it =
|
||||
roctx_push_tracks_.emplace(rtx_track_id, perfetto::Track(rtx_track_id)).first;
|
||||
|
||||
auto roctx_track_desc = roctx_track_it->second.Serialize();
|
||||
std::string roctx_track_str = rocprofiler::string_printf("ROCTX Push/Pop Ranges");
|
||||
roctx_track_desc.set_name(roctx_track_str);
|
||||
perfetto::TrackEvent::SetTrackDescriptor(roctx_track_it->second, roctx_track_desc);
|
||||
}
|
||||
}
|
||||
auto& roctx_track = roctx_track_it->second;
|
||||
TRACE_EVENT_END("ROCTX_RANGE_PUSH_POP", roctx_track,
|
||||
tracer_record.timestamps.begin.value);
|
||||
roctx_push_track_entries_--;
|
||||
break;
|
||||
}
|
||||
case 4: {
|
||||
std::unordered_map<uint64_t, perfetto::Track>::iterator roctx_track_it;
|
||||
{
|
||||
uint64_t rtx_track_id = getTrackID(TrackType::ROCTX_START_RANGE, machine_id_, 0, thread_id);
|
||||
roctx_track_it = roctx_start_tracks_.find(rtx_track_id);
|
||||
if (roctx_track_it == roctx_start_tracks_.end()) {
|
||||
roctx_track_it =
|
||||
roctx_start_tracks_.emplace(rtx_track_id, perfetto::Track(rtx_track_id)).first;
|
||||
|
||||
auto roctx_track_desc = roctx_track_it->second.Serialize();
|
||||
std::string roctx_track_str = rocprofiler::string_printf("ROCTX Start/Stop Ranges");
|
||||
roctx_track_desc.set_name(roctx_track_str);
|
||||
perfetto::TrackEvent::SetTrackDescriptor(roctx_track_it->second, roctx_track_desc);
|
||||
}
|
||||
}
|
||||
auto& roctx_track = roctx_track_it->second;
|
||||
TRACE_EVENT_END("ROCTX_RANGE_START_STOP", roctx_track,
|
||||
tracer_record.timestamps.begin.value);
|
||||
roctx_start_track_entries_--;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
rocprofiler::warning("ROCPROFILER_TOOL: Wrong ROCTx Operation ID!");
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ACTIVITY_DOMAIN_HSA_API: {
|
||||
std::unordered_map<uint64_t, perfetto::Track>::iterator hsa_track_it;
|
||||
{
|
||||
uint64_t hsa_track_id = getTrackID(TrackType::HSAAPI, machine_id_, 0, thread_id);
|
||||
hsa_track_it = hsa_tracks_.find(hsa_track_id);
|
||||
if (hsa_track_it == hsa_tracks_.end()) {
|
||||
hsa_track_it = hsa_tracks_.emplace(hsa_track_id, perfetto::Track(hsa_track_id)).first;
|
||||
auto hsa_track_desc = hsa_track_it->second.Serialize();
|
||||
std::stringstream hsa_track_str;
|
||||
hsa_track_str << "HSA API " << thread_id;
|
||||
hsa_track_desc.set_name(hsa_track_str.str());
|
||||
perfetto::TrackEvent::SetTrackDescriptor(hsa_track_it->second, hsa_track_desc);
|
||||
}
|
||||
}
|
||||
auto& hsa_track = hsa_track_it->second;
|
||||
if (tracer_record.phase == ROCPROFILER_PHASE_ENTER)
|
||||
TRACE_EVENT_BEGIN("HSA_API", perfetto::DynamicString(operation_name_c), hsa_track,
|
||||
tracer_record.timestamps.begin.value,
|
||||
perfetto::Flow::ProcessScoped(tracer_record.correlation_id.value));
|
||||
if (tracer_record.phase == ROCPROFILER_PHASE_EXIT)
|
||||
TRACE_EVENT_END("HSA_API", hsa_track, tracer_record.timestamps.end.value);
|
||||
if (tracer_record.phase == ROCPROFILER_PHASE_NONE) {
|
||||
TRACE_EVENT_BEGIN("HSA_API", perfetto::DynamicString(operation_name_c), hsa_track,
|
||||
tracer_record.timestamps.begin.value,
|
||||
perfetto::Flow::ProcessScoped(tracer_record.correlation_id.value));
|
||||
TRACE_EVENT_END("HSA_API", hsa_track, tracer_record.timestamps.end.value);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ACTIVITY_DOMAIN_HIP_API: {
|
||||
std::unordered_map<uint64_t, perfetto::Track>::iterator hip_track_it;
|
||||
{
|
||||
uint64_t hipapi_track_id = getTrackID(TrackType::HIPAPI, machine_id_, 0, thread_id);
|
||||
hip_track_it = hip_tracks_.find(hipapi_track_id);
|
||||
if (hip_track_it == hip_tracks_.end()) {
|
||||
hip_track_it =
|
||||
hip_tracks_.emplace(hipapi_track_id, perfetto::Track(hipapi_track_id)).first;
|
||||
|
||||
auto hip_track_desc = hip_track_it->second.Serialize();
|
||||
std::stringstream hip_track_str;
|
||||
hip_track_str << "HIP API " << thread_id;
|
||||
hip_track_desc.set_name(hip_track_str.str());
|
||||
perfetto::TrackEvent::SetTrackDescriptor(hip_track_it->second, hip_track_desc);
|
||||
}
|
||||
}
|
||||
auto& hip_track = hip_track_it->second;
|
||||
if (tracer_record.phase == ROCPROFILER_PHASE_ENTER)
|
||||
TRACE_EVENT_BEGIN("HIP_API", perfetto::DynamicString(operation_name_c), hip_track,
|
||||
tracer_record.timestamps.begin.value,
|
||||
perfetto::Flow::ProcessScoped(tracer_record.correlation_id.value));
|
||||
if (tracer_record.phase == ROCPROFILER_PHASE_EXIT)
|
||||
TRACE_EVENT_END("HIP_API", hip_track, tracer_record.timestamps.end.value);
|
||||
if (tracer_record.phase == ROCPROFILER_PHASE_NONE) {
|
||||
TRACE_EVENT_BEGIN("HIP_API", perfetto::DynamicString(operation_name_c), hip_track,
|
||||
tracer_record.timestamps.begin.value,
|
||||
perfetto::Flow::ProcessScoped(tracer_record.correlation_id.value));
|
||||
TRACE_EVENT_END("HIP_API", hip_track, tracer_record.timestamps.end.value);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ACTIVITY_DOMAIN_EXT_API: {
|
||||
printf("Warning: External API is not supported!\n");
|
||||
break;
|
||||
}
|
||||
case ACTIVITY_DOMAIN_HIP_OPS: {
|
||||
std::size_t pos = std::string::npos;
|
||||
if (tracer_record.name) {
|
||||
auto kernel_name_it = kernel_names_map.find(tracer_record.name);
|
||||
if (kernel_name_it == kernel_names_map.end()) {
|
||||
kernel_name_it =
|
||||
kernel_names_map
|
||||
.emplace(
|
||||
tracer_record.name,
|
||||
rocprofiler::truncate_name(rocprofiler::cxx_demangle(tracer_record.name)))
|
||||
.first;
|
||||
}
|
||||
TRACE_EVENT_BEGIN("HIP_OPS", perfetto::DynamicString(kernel_name_it->second.c_str()),
|
||||
gpu_track, tracer_record.timestamps.begin.value, "Agent ID",
|
||||
tracer_record.agent_id.handle, "Process ID", GetPid(),
|
||||
perfetto::Flow::ProcessScoped(tracer_record.correlation_id.value));
|
||||
TRACE_EVENT_END("HIP_OPS", gpu_track, tracer_record.timestamps.end.value);
|
||||
} else {
|
||||
// MEM Copies are not correlated to GPUs, so they need a special track
|
||||
pos = operation_name_c ? std::string_view(operation_name_c).find("Copy")
|
||||
: std::string::npos;
|
||||
|
||||
if (std::string::npos == pos) {
|
||||
TRACE_EVENT_BEGIN("HIP_OPS", perfetto::DynamicString(operation_name_c), gpu_track,
|
||||
tracer_record.timestamps.begin.value, "Process ID", GetPid(),
|
||||
perfetto::Flow::ProcessScoped(tracer_record.correlation_id.value));
|
||||
TRACE_EVENT_END("HIP_OPS", gpu_track, tracer_record.timestamps.end.value);
|
||||
} else {
|
||||
TRACE_EVENT_BEGIN("MEM_COPIES", perfetto::DynamicString(operation_name_c),
|
||||
mem_copies_track, tracer_record.timestamps.begin.value, "Process ID",
|
||||
GetPid(),
|
||||
perfetto::Flow::ProcessScoped(tracer_record.correlation_id.value));
|
||||
TRACE_EVENT_END("MEM_COPIES", mem_copies_track, tracer_record.timestamps.end.value);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ACTIVITY_DOMAIN_HSA_OPS: {
|
||||
TRACE_EVENT_BEGIN("MEM_COPIES", perfetto::DynamicString(operation_name_c), mem_copies_track,
|
||||
tracer_record.timestamps.begin.value, "Process ID", GetPid(),
|
||||
perfetto::Flow::ProcessScoped(tracer_record.correlation_id.value));
|
||||
TRACE_EVENT_END("MEM_COPIES", mem_copies_track, tracer_record.timestamps.end.value);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
rocprofiler::warning("Ignored record for domain %d", tracer_record.domain);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
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) {
|
||||
if (!tracing_session_) rocprofiler::warning("Tracing session is deleted!\n");
|
||||
while (begin < end) {
|
||||
if (!begin) return 0;
|
||||
switch (begin->kind) {
|
||||
case ROCPROFILER_PROFILER_RECORD: {
|
||||
rocprofiler_record_profiler_t* profiler_record =
|
||||
const_cast<rocprofiler_record_profiler_t*>(
|
||||
reinterpret_cast<const rocprofiler_record_profiler_t*>(begin));
|
||||
FlushProfilerRecord(*profiler_record, session_id);
|
||||
break;
|
||||
}
|
||||
case ROCPROFILER_TRACER_RECORD: {
|
||||
rocprofiler_record_tracer_t* tracer_record = const_cast<rocprofiler_record_tracer_t*>(
|
||||
reinterpret_cast<const rocprofiler_record_tracer_t*>(begin));
|
||||
FlushTracerRecord(*tracer_record, session_id);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
rocprofiler_next_record(begin, &begin, session_id, buffer_id);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool IsValid() const { return is_valid_; }
|
||||
|
||||
private:
|
||||
std::unique_ptr<perfetto::TracingSession> tracing_session_;
|
||||
fs::path output_prefix_;
|
||||
int file_descriptor_;
|
||||
bool is_valid_{false};
|
||||
size_t roctx_start_track_entries_{0};
|
||||
size_t roctx_push_track_entries_{0};
|
||||
|
||||
// Correlate stream id(s) with correlation id(s) to identify the stream id of every HIP activity
|
||||
std::unordered_map<uint64_t, uint64_t> stream_ids_;
|
||||
|
||||
// Callback Tracks
|
||||
std::unordered_map<uint64_t, perfetto::Track> roctx_mark_tracks_, roctx_push_tracks_,
|
||||
roctx_start_tracks_, hsa_tracks_, hip_tracks_, hip_ext_tracks_, mem_copies_tracks_;
|
||||
|
||||
// Activity Tracks
|
||||
std::unordered_map<uint64_t, perfetto::Track> queue_tracks_;
|
||||
|
||||
std::unordered_map<uint64_t, std::vector<perfetto::CounterTrack>> counter_tracks_;
|
||||
|
||||
std::atomic<uint64_t> track_counter_{GetPid()};
|
||||
std::vector<uint64_t> track_ids_used_;
|
||||
|
||||
std::string hostname_;
|
||||
uint64_t machine_id_;
|
||||
|
||||
std::ofstream stream_;
|
||||
|
||||
std::unordered_map<TrackID, uint64_t> track_ids;
|
||||
std::unordered_map<uint64_t, perfetto::Track> device_tracks;
|
||||
std::unordered_map<uint64_t, perfetto::Track> hip_stream_tracks;
|
||||
std::unordered_map<std::string, std::string> kernel_names_map;
|
||||
|
||||
uint64_t getTrackID(TrackType type, uint64_t machine, uint64_t device, uint64_t queue) {
|
||||
TrackID id(type, machine, device, queue);
|
||||
|
||||
auto it = track_ids.find(id);
|
||||
if (it == track_ids.end()) it = track_ids.emplace(id, getUniqueID()).first;
|
||||
|
||||
return it->second;
|
||||
}
|
||||
|
||||
uint64_t getUniqueID() { return cur_unique_id.fetch_add(1); };
|
||||
std::atomic<uint64_t> cur_unique_id{uint64_t(GetPid()) << 30};
|
||||
};
|
||||
|
||||
perfetto_plugin_t* perfetto_plugin = nullptr;
|
||||
|
||||
} // namespace
|
||||
|
||||
int rocprofiler_plugin_initialize(uint32_t rocprofiler_major_version,
|
||||
uint32_t rocprofiler_minor_version, void* data) {
|
||||
if (rocprofiler_major_version != ROCPROFILER_VERSION_MAJOR ||
|
||||
rocprofiler_minor_version > ROCPROFILER_VERSION_MINOR)
|
||||
return -1;
|
||||
|
||||
// if (perfetto_plugin != nullptr && perfetto_plugin->IsValid()) return -1;
|
||||
|
||||
std::lock_guard<std::mutex> lock(writing_lock);
|
||||
if (perfetto_plugin != nullptr) perfetto_plugin->delete_perfetto_plugin();
|
||||
|
||||
static int perfetto_init_count = 0;
|
||||
perfetto_plugin = new perfetto_plugin_t(perfetto_init_count++);
|
||||
|
||||
if (perfetto_plugin->IsValid()) return 0;
|
||||
|
||||
// delete perfetto_plugin;
|
||||
// perfetto_plugin = nullptr;
|
||||
return -1;
|
||||
}
|
||||
|
||||
void rocprofiler_plugin_finalize() {
|
||||
std::lock_guard<std::mutex> lock(writing_lock);
|
||||
if (!perfetto_plugin) return;
|
||||
if (perfetto_plugin->delete_perfetto_plugin()) {
|
||||
delete perfetto_plugin;
|
||||
perfetto_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) {
|
||||
std::lock_guard<std::mutex> lock(writing_lock);
|
||||
if (!perfetto_plugin || !perfetto_plugin->IsValid()) return -1;
|
||||
return perfetto_plugin->WriteBufferRecords(begin, end, session_id, buffer_id);
|
||||
}
|
||||
|
||||
ROCPROFILER_EXPORT int rocprofiler_plugin_write_record(rocprofiler_record_tracer_t record) {
|
||||
if (record.header.id.handle == 0) return 0;
|
||||
|
||||
std::lock_guard<std::mutex> lock(writing_lock);
|
||||
if (!perfetto_plugin || !perfetto_plugin->IsValid()) return -1;
|
||||
return perfetto_plugin->FlushTracerRecord(record, rocprofiler_session_id_t{0});
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/* Copyright (c) 2018-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. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cxxabi.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
|
||||
#include "src/utils/helper.h"
|
||||
|
||||
// Macro to check ROCProfiler calls status
|
||||
#define CHECK_ROCPROFILER(call) \
|
||||
do { \
|
||||
if ((call) != ROCPROFILER_STATUS_SUCCESS) \
|
||||
rocprofiler::fatal("Error: ROCProfiler API Call Error!"); \
|
||||
} while (false)
|
||||
|
||||
namespace {
|
||||
|
||||
[[maybe_unused]] uint32_t GetPid() {
|
||||
static uint32_t pid = syscall(__NR_getpid);
|
||||
return pid;
|
||||
}
|
||||
|
||||
[[maybe_unused]] uint64_t GetMachineID() { return gethostid(); }
|
||||
|
||||
[[maybe_unused]] std::set<std::string> GetKernelFilters() {
|
||||
std::set<std::string> ret;
|
||||
if (const char* line_c_str = getenv("ROCPROFILER_KERNEL_FILTER")) {
|
||||
std::stringstream ss(std::string{line_c_str});
|
||||
std::string filter_name;
|
||||
while(std::getline(ss, filter_name, ' '))
|
||||
{
|
||||
if (filter_name.find("kernel:") != std::string::npos) {
|
||||
continue;
|
||||
}
|
||||
ret.insert(filter_name);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
Odkázat v novém úkolu
Zablokovat Uživatele