Adding rocprofilerv2

Change-Id: Ic0cc280ba207d2b8f6ccae1cd4ac3184152fc1ad


[ROCm/rocprofiler commit: 8032adb64f]
This commit is contained in:
Ammar ELWazir
2023-02-03 12:31:39 -06:00
parent b23a2f3029
commit de4abd0d0f
263 changed files with 607729 additions and 307 deletions
@@ -0,0 +1,52 @@
# ############################################################################################################################################
# ROCMTOOL Tool
# ############################################################################################################################################
# Setting Default Binary output directory
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
# Getting Source files for ROCProfiler Tool
file(GLOB ROCPROFILER_TOOL_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/tool.cpp)
file(GLOB ROCPROFILER_UTIL_SRC_FILES ${PROJECT_SOURCE_DIR}/src/utils/helper.cpp)
# Compiling/Installing ROCProfiler API
add_library(rocprofiler_tool SHARED ${ROCPROFILER_TOOL_SRC_FILES} ${ROCPROFILER_UTIL_SRC_FILES})
set_target_properties(rocprofiler_tool PROPERTIES
CXX_VISIBILITY_PRESET hidden
LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/exportmap)
target_include_directories(rocprofiler_tool
PRIVATE
${PROJECT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/inc
${PROJECT_SOURCE_DIR}/src)
target_compile_definitions(rocprofiler_tool
PRIVATE HIP_PROF_HIP_API_STRING=1 __HIP_PLATFORM_HCC__=1)
if(ASAN)
target_compile_options(rocprofiler_tool PRIVATE -fsanitize=address)
target_link_libraries(rocprofiler_tool ${ROCPROFILER_TARGET} hsa-runtime64::hsa-runtime64 Threads::Threads atomic asan dl rt stdc++fs amd_comgr)
target_link_options(rocprofiler_tool PRIVATE -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/exportmap -Wl,--no-undefined,-fsanitize=address)
else()
target_link_libraries(rocprofiler_tool ${ROCPROFILER_TARGET} hsa-runtime64::hsa-runtime64 Threads::Threads atomic dl rt stdc++fs amd_comgr)
target_link_options(rocprofiler_tool PRIVATE -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/exportmap -Wl,--no-undefined)
endif()
install(TARGETS rocprofiler_tool LIBRARY
DESTINATION ${CMAKE_INSTALL_LIBDIR}
COMPONENT runtime)
add_subdirectory(amdsys)
add_subdirectory(rocprofv2)
add_executable(ctrl ctrl.cpp)
target_include_directories(ctrl PRIVATE ${PROJECT_SOURCE_DIR}/inc)
target_link_options(rocprofiler_tool PRIVATE -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/exportmap -Wl,--no-undefined)
target_link_libraries(ctrl PRIVATE ${ROCPROFILER_TARGET} hsa-runtime64::hsa-runtime64)
install(TARGETS ctrl RUNTIME
DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/rocprofiler
COMPONENT runtime)
# ############################################################################################################################################
@@ -0,0 +1,27 @@
# ############################################################################################################################################
# ROCMTOOL AMDSYS Frontend
# ############################################################################################################################################
set(CMAKE_BINARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
# Getting Source files for RDC, Utils, Counters
file(GLOB ROCPROFILER_AMDSYS_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
# Compiling/Installing ROCProfiler API
add_executable(rocprofiler_amdsys_fe ${ROCPROFILER_AMDSYS_SRC_FILES})
set_target_properties(rocprofiler_amdsys_fe PROPERTIES
OUTPUT_NAME "amdsys")
target_include_directories(rocprofiler_amdsys_fe
PRIVATE
${PROJECT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/inc)
target_link_libraries(rocprofiler_amdsys_fe dl rt stdc++fs)
install(TARGETS rocprofiler_amdsys_fe RUNTIME
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
DESTINATION ${CMAKE_INSTALL_BINDIR}
COMPONENT runtime)
# ############################################################################################################################################
@@ -0,0 +1,238 @@
/* 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 <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <iostream>
#include <algorithm>
#include <experimental/filesystem>
/*
mpiexec -n 16 amdsys --session-new test launch python app.py
amdsys
*/
namespace {
struct shmd_t {
int command;
};
struct shmd_t* shmd;
} // namespace
namespace fs = std::experimental::filesystem;
void report(const char* msg, int terminate) {
std::cerr << msg << ": " << errno << std::endl;
if (terminate) exit(-1); /* failure */
}
pid_t getpid(void);
void exit(int);
int toint(const char* chararr) {
int num = 0;
std::string s;
if (chararr != nullptr) {
s = chararr;
}
if (s.empty()) {
return 0;
}
transform(s.begin(), s.end(), s.begin(), ::tolower);
for (auto itr : s) {
if (itr >= 'a' && itr <= 'z') {
num += (itr - 'a' + 1) * 1 + (itr - 'a' + 1) * 10 + (itr - 'a' + 1) * 100;
}
}
if (std::to_string(num).length() > 16) {
perror("generated number is more than permissable limit");
exit(1);
}
return num;
}
int main(int argc, char* argv[]) {
fs::path bin_path;
if (argc < 4) {
perror(
"amdsys: start must be preceeded by --session <name>\n\t"
"amdsys --session <name> start \n"
"amdsys: stop must be preceeded by --session <name>\n \t"
"amdsys --session <name> stop \n"
"amdsys: launch must be preceeded by --session-new <name>\n \t"
"e.g. mpiexec -np 16 amdsys --session-new <name> launch py app.py\n"
"where all mpiexec options must come before amdsys");
exit(1);
}
if (Dl_info dl_info; dladdr(reinterpret_cast<void*>(main), &dl_info) != 0) {
bin_path = fs::path(dl_info.dli_fname).remove_filename();
} else {
bin_path = "/opt/rocm";
}
std::string session_name = "default_session_roctracer";
bool session_name_found{false};
std::string message;
bool message_flag{true};
bool args_flag{true};
int i = 0;
int session_id = 0;
int* session_id_shm;
int sys_type = 0;
while (std::string(argv[i]).find("amdsys") != std::string::npos) {
i++;
break;
}
const char* shared_memory_key = "AMD_SYS_KEY";
const int SIZE = sizeof(int);
for (; i < argc; i++) {
if (args_flag && strcmp(argv[i], "--session") == 0) {
i++;
if (argv[i])
session_name = argv[i];
else
std::cerr << "Error: missing session name" << std::endl;
session_name_found = true;
session_id = toint(argv[i]);
} else if (args_flag && message_flag && (strcmp(argv[i], "launch") == 0)) {
sys_type = 3;
message_flag = false;
args_flag = false;
} else if (args_flag && message_flag && (strcmp(argv[i], "start") == 0)) {
sys_type = 4;
message_flag = false;
args_flag = false;
break;
} else if (args_flag && message_flag && (strcmp(argv[i], "stop") == 0)) {
sys_type = 5;
message_flag = false;
args_flag = false;
break;
} else if (args_flag && message_flag && (strcmp(argv[i], "exit") == 0)) {
sys_type = 6;
message_flag = false;
args_flag = false;
} else if (!args_flag) {
break;
} else {
report(
"amdsys: start must be preceeded by --session <name>\n\t"
"amdsys --session <name> start \n"
"amdsys: stop must be preceeded by --session <name>\n \t"
"amdsys --session <name> stop \n"
"amdsys: launch must be preceeded by --session <name>\n \t"
"e.g. amdsys --session <SESSION_NAME> launch <MPI_COMMAND> <MPI_ARGUMENTS> rocprofv2 "
"<ROCPROFV2_OPTIONS> <APP_EXEC>\n"
"where all mpiexec options must come before amdsys\n",
1);
}
}
if (!session_name_found) session_id = toint(session_name.c_str());
if (sys_type != 3) {
static int shm_fd_sn = shm_open(std::to_string(session_id).c_str(), O_CREAT | O_RDWR, 0666);
int status = ftruncate(shm_fd_sn, 1024);
if (status < 0) std::cerr << "Invalid Command!" << std::endl;
shmd = reinterpret_cast<struct shmd_t*>(mmap(0, 1024, PROT_WRITE, MAP_SHARED, shm_fd_sn, 0));
}
switch (sys_type) {
case 3: {
static int shm_fd = shm_open(shared_memory_key, O_CREAT | O_RDWR, 0666);
int status = ftruncate(shm_fd, SIZE);
if (status < 0) std::cerr << "Invalid Command!" << std::endl;
session_id_shm = reinterpret_cast<int*>(mmap(0, SIZE, PROT_WRITE, MAP_SHARED, shm_fd, 0));
*session_id_shm = session_id;
printf("AMDSYS:: Session ID: %d\n", session_id);
int argindex = i;
std::vector<std::string> env_vars;
env_vars.emplace_back("");
if (strncmp(argv[i], "rocprofv2", 8) == 0) {
env_vars.emplace_back("--amd-sys");
env_vars.emplace_back(std::to_string(session_id));
}
for (argindex++; argindex < argc; argindex++) {
if (strncmp(argv[argindex], "rocprofv2", 8) == 0) {
fs::path command = bin_path;
command.append("rocprofv2");
env_vars.emplace_back(command.c_str());
env_vars.emplace_back("--amd-sys");
env_vars.emplace_back(std::to_string(session_id));
} else {
env_vars.emplace_back(argv[argindex]);
}
}
char* exec_args[env_vars.size() + 1];
for (uint32_t i = 0; i < env_vars.size(); i++) {
exec_args[i] = const_cast<char*>(env_vars.at(i).c_str());
std::cout << exec_args[i] << " ";
}
exec_args[env_vars.size()] = NULL;
std::cout << std::endl;
if (strncmp(argv[i], "rocprofv2", 8) != 0) {
execvp(argv[i], exec_args);
} else {
fs::path command = bin_path;
command.append("rocprofv2");
execvp(command.c_str(), exec_args);
}
std::cout << "Error: can't launch server (" << errno << "):" << std::endl;
exit(EXIT_FAILURE);
}
case 4: {
shmd->command = 4;
break;
}
case 5: {
shmd->command = 5;
break;
}
case 6: {
shmd->command = 6;
break;
}
default: {
report("AMDSYS:: Error: Not possible to reach here, please report(invalid sys_type)!\n", 1);
}
}
return 1;
}
+25
View File
@@ -0,0 +1,25 @@
#include <hsa/hsa.h>
#include <rocprofiler.h>
#include <cstdio>
static int info_callback(const rocprofiler_counter_info_t info, const char* gpu_name,
uint32_t gpu_index) {
fprintf(stdout, "\n %s:%u : %s : %s\n", gpu_name, gpu_index, info.name, info.description);
if (info.expression != nullptr) {
fprintf(stdout, " %s = %s\n", info.name, info.expression);
} else {
if (info.instances_count > 1) fprintf(stdout, "[0-%u]", info.instances_count - 1);
fprintf(stdout, " : %s\n", info.description);
fprintf(stdout, " block %s can only handle %u counters at a time\n", info.block_name,
info.block_counters);
}
fflush(stdout);
return 1;
}
int main(int argc, char** argv) {
hsa_init();
rocprofiler_initialize();
rocprofiler_iterate_counters(info_callback);
}
+1
View File
@@ -0,0 +1 @@
{ global: HSA_AMD_TOOL_PRIORITY; OnLoad; OnUnload; local: *; };
@@ -0,0 +1,29 @@
# Setting Default Binary output directory
# set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
# Getting HSA Include Directory
get_property(HSA_RUNTIME_INCLUDE_DIRECTORIES TARGET hsa-runtime64::hsa-runtime64 PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
find_library(AQLPROFILE_LIB "libhsa-amd-aqlprofile64.so" HINTS ${CMAKE_PREFIX_PATH} PATHS ${ROCM_PATH} PATH_SUFFIXES lib)
if(NOT AQLPROFILE_LIB)
message(FATAL_ERROR "AQL_PROFILE not installed. Please install hsa-amd-aqlprofile!")
endif()
file(GLOB ROCPROFV2_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
add_executable(rocprofv2 ${ROCPROFV2_SRC_FILES})
target_compile_definitions(rocprofv2
PUBLIC AMD_INTERNAL_BUILD
PRIVATE PROF_API_IMPL)
target_include_directories(rocprofv2
PUBLIC
${HSA_RUNTIME_INCLUDE_DIRECTORIES}
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/inc>
PRIVATE
${PROJECT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/inc)
target_link_libraries(rocprofv2 PRIVATE ${AQLPROFILE_LIB} hsa-runtime64::hsa-runtime64 stdc++fs Threads::Threads atomic -ldl)
# install(TARGETS rocprofv2 RUNTIME
# DESTINATION ${CMAKE_INSTALL_BINDIR}
# COMPONENT runtime)
@@ -0,0 +1,256 @@
/* 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 <link.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <atomic>
#include <experimental/filesystem>
#include <iostream>
#include <vector>
namespace fs = std::experimental::filesystem;
// Usage message
void printUsage() {
std::cout << "ROCMTools Run Binary Usage:" << std::endl;
std::cout << "\nTo run ./run.sh PARAMs, PARAMs can be the following:" << std::endl;
std::cout << "-h | --help For showing this message" << std::endl;
std::cout << "-t | --test For Running the tests" << std::endl;
std::cout << "-mt | --mem-test For Running the Memory Leak tests" << std::endl;
std::cout << "\nTo run ./run.sh PARAMs APP_EXEC, PARAMs can be the following:" << std::endl;
std::cout << "-i | --input For adding counters file path "
"(every line in the text file represents a counter)"
<< std::endl;
std::cout << "-d | --output-directory For adding output path where the "
"output files will be saved"
<< std::endl;
std::cout << "-fi | --flush-interval For adding a flush interval in "
"milliseconds, every \"flush interval\" the buffers will be flushed"
<< std::endl;
std::cout << "-a | --asan For adding libasan.so.6 for memory leak "
"check run requires building using -act | --asan-clean-build option"
<< std::endl;
}
void runMemCheck(fs::path bin_path) {
fs::path counter_path = bin_path;
fs::path app_path = bin_path;
fs::path log_path = bin_path;
fs::path suppr_path = bin_path;
fs::path pyscript_path = bin_path;
std::string pathenv = bin_path.replace_filename("librocprofiler_tool.so");
pyscript_path = pyscript_path.replace_filename("tests/memorytests/test_mem.py");
counter_path = counter_path.replace_filename("tests/memorytests/input.txt");
suppr_path = suppr_path.replace_filename("tests/memorytests/suppr.txt");
app_path = app_path.replace_filename(
"tests/featuretests/profiler/gtests/apps/"
"hip_vectoradd");
log_path = log_path.replace_filename("memleaks.log");
std::cout << "Running Memory Leaks Check...." << std::endl;
std::string run_command = "LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libasan.so.6:" + pathenv +
" ASAN_OPTIONS=detect_leaks=1 LSAN_OPTIONS=suppressions=" + suppr_path.string() +
" COUNTERS_PATH=" + counter_path.string() + " " + app_path.string() +
" > /tmp/rocprofv2-temp 2> " + log_path.string();
int status = system(run_command.c_str());
if (status < 0) std::cerr << "Invalid Command!" << std::endl;
std::cout << "Log with all detected leaks is available at " << log_path.string() << std::endl;
std::string python_command = "python3 " + pyscript_path.string() + " " + log_path.string();
status = system(python_command.c_str());
if (status < 0) std::cerr << "Invalid Command!" << std::endl;
}
// Passes Counter
static std::atomic<uint32_t> pass_num{1};
// Getting the number of passes needed using the counters file path
uint32_t getNumberOfPasses(std::string counter_path) {
// TODO(aelwazir): Adding a way to get the number of passes;
return 1;
}
// Function to run the app for the amount of passes needed
void runApp(const char* app_path, char* const envp[], char* const args[],
uint32_t number_of_passes = 1) {
while (pass_num.load(std::memory_order_relaxed) <= number_of_passes) {
// Forking to keep the original
int status = 0;
if (fork() > 0) {
status = execve(app_path, args, envp);
std::cout << "Error: can't launch(" << app_path << "):" << std::endl;
perror("errno");
exit(EXIT_FAILURE);
}
if (status == 0)
std::cout << "Pass(" << pass_num.fetch_add(1, std::memory_order_release)
<< ") of the application: " << fs::path(app_path).filename()
<< " is executed successfully!" << std::endl;
}
}
uint32_t number_of_passes = 1;
int main(int argc, char** argv) {
// Getting Current Path
char current_path[FILENAME_MAX];
if (!getcwd(current_path, sizeof(current_path))) {
throw errno;
}
current_path[sizeof(current_path) - 1] = '\0';
// Getting the rocprofv2 binary path to locate rocmtools library path
fs::path bin_path;
if (Dl_info dl_info; dladdr((void*)runApp, &dl_info) != 0) {
bin_path = fs::path(dl_info.dli_fname);
}
// Environment cariables vector
std::vector<std::string> pathenv;
// Reading Arguments
// One ENV Var for the whole options!!
int app_argc_start = 0;
char* app_path = nullptr;
for (int i = 1; i < argc; i++) {
// Help Message
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-h") == 0) {
printUsage();
return 1;
// Normal ROCMTools Tests
} else if (strcmp(argv[i], "-t") == 0 || strcmp(argv[i], "--test") == 0) {
fs::path test_path = bin_path;
test_path = test_path.replace_filename("run_tests.sh");
int status = system(test_path.string().c_str());
if (status < 0) std::cerr << "Invalid Command!" << std::endl;
return 1;
// Memory Check Test
} else if (strcmp(argv[i], "-mt") == 0 || strcmp(argv[i], "--mem-test") == 0) {
runMemCheck(bin_path);
return 1;
// Counters File
} else if (strcmp(argv[i], "-i") == 0 || strcmp(argv[i], "--input") == 0) {
i++;
if (argv[i]) {
std::string counters_path = std::string(current_path) + "/" + argv[2];
std::cout << "Reading counters from: " << counters_path << std::endl;
number_of_passes = getNumberOfPasses(counters_path);
pathenv.emplace_back("COUNTERS_PATH=" + counters_path);
} else {
std::cerr << "Error: Missing Counters File path!" << std::endl;
printUsage();
exit(EXIT_FAILURE);
}
// Output Directory
} else if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--output-directory") == 0) {
i++;
if (argv[i]) {
std::string output_path = std::string(current_path) + "/" + argv[2];
fs::create_directory(output_path);
std::cout << "Output directory path: " << output_path << std::endl;
pathenv.emplace_back("OUTPUT_PATH=" + output_path);
} else {
std::cerr << "Error: Missing output directory path!" << std::endl;
printUsage();
exit(EXIT_FAILURE);
}
// ASAN run
} else if (strcmp(argv[i], "-a") == 0 || strcmp(argv[i], "--asan") == 0) {
std::string current_ld_preload{getenv("LD_PRELOAD")};
std::string ld_preload = "/usr/lib/x86_64-linux-gnu/libasan.so.6";
if (!current_ld_preload.empty()) ld_preload = current_ld_preload + ":" + ld_preload;
std::cout << ld_preload << std::endl;
pathenv.emplace_back("LD_PRELOAD=" + ld_preload);
pathenv.emplace_back("ASAN_OPTIONS=detect_leaks=1");
fs::path suppr_path = bin_path;
std::string suppr =
"suppressions=" + suppr_path.replace_filename("tests/memorytests/suppr.txt").string();
std::cout << suppr << std::endl;
pathenv.emplace_back("LSAN_OPTIONS=" + suppr);
// Flush Interval
} else if (strcmp(argv[i], "-fi") == 0 || strcmp(argv[i], "--flush-interval") == 0) {
i++;
if (argv[i]) {
pathenv.emplace_back("ROCPROFILER_FLUSH_INTERVAL=" + std::string(argv[i]));
} else {
std::cerr << "Error: Missing flush interval value!" << std::endl;
printUsage();
exit(EXIT_FAILURE);
}
// Wrong argument given
} else if (argv[i][0] == '-') {
std::cerr << "Wrong option (" << argv[i] << "), Please use the following options:\n"
<< std::endl;
printUsage();
exit(EXIT_FAILURE);
// Taking up the Application path with its arguments
} else {
app_path = argv[i];
app_argc_start = i;
break;
}
}
// Getting Original Application Arguments
char* app_args[argc - app_argc_start + 1];
int j = 0;
for (int i = app_argc_start; i < argc; i++) {
app_args[j] = argv[i];
j++;
}
app_args[j] = NULL;
// Providing LD_PRELOAD of ROCMTools Library to runApp function
std::string pathenv_str = "LD_PRELOAD=librocprofiler_tool.so";
std::string current_ld_preload;
if (getenv("LD_PRELOAD")) current_ld_preload = getenv("LD_PRELOAD");
if (strstr(bin_path.c_str(), "build") != nullptr)
pathenv_str = bin_path.replace_filename("librocprofiler_tool.so");
else
pathenv_str = bin_path.remove_filename().replace_filename("lib/librocprofiler_tool.so");
if (!current_ld_preload.empty())
pathenv_str = "LD_PRELOAD=" + current_ld_preload + ":" + pathenv_str;
else
pathenv_str = "LD_PRELOAD=" + current_ld_preload + pathenv_str;
pathenv.emplace_back(pathenv_str);
// Providing all Environment variables needed by the arguments
char* envp_run[pathenv.size() + 1];
for (uint32_t i = 0; i < pathenv.size(); i++)
envp_run[i] = const_cast<char*>(pathenv.at(i).c_str());
envp_run[pathenv.size()] = NULL;
// Getting Application Executable path to be provided to runApp function
std::string app_path_str;
if (app_path) app_path_str = std::string(current_path) + "/" + std::string(app_path);
// Calling runApp function to execute the application with Environment
// variables and original arguments
runApp(app_path_str.c_str(), envp_run, app_args, number_of_passes);
return 1;
}
+443
View File
@@ -0,0 +1,443 @@
/* 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. */
#define ROCPROFILER_V2
#include <dirent.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <hsa/hsa.h>
#include <hsa/hsa_api_trace.h>
#include <rocprofiler.h>
#include <rocprofiler_plugin.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <bits/stdc++.h>
#include <atomic>
#include <cassert>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <csetjmp>
#include <exception>
#include <experimental/filesystem>
#include <fstream>
#include <iostream>
#include <map>
#include <optional>
#include <string>
#include <vector>
#include <mutex>
#include <thread>
#include <chrono>
#include "rocprofiler.h"
#include "utils/helper.h"
namespace fs = std::experimental::filesystem;
// Macro to check ROCMTools calls status
#define CHECK_ROCMTOOLS(call) \
do { \
if ((call) != ROCPROFILER_STATUS_SUCCESS) rocmtools::fatal("Error: ROCMTools API Call Error!"); \
} while (false)
namespace {
struct shmd_t {
int command;
};
static const char* amd_sys_session_id;
static int shm_fd_sn = -1;
struct shmd_t* shmd;
std::thread wait_for_start_shm;
std::atomic<bool> amd_sys_handler{false};
std::atomic<bool> session_created{false};
[[maybe_unused]] static rocprofiler_session_id_t session_id;
static std::vector<rocprofiler_filter_id_t> filter_ids;
static std::vector<rocprofiler_buffer_id_t> buffer_ids;
void warning(const std::string& msg) { std::cerr << msg << std::endl; }
class rocprofiler_plugin_t {
public:
rocprofiler_plugin_t(const std::string& plugin_path) {
plugin_handle_ = dlopen(plugin_path.c_str(), RTLD_LAZY);
if (plugin_handle_ == nullptr) {
warning(std::string("Warning: dlopen for ") + plugin_path + " failed: " + dlerror());
return;
}
rocprofiler_plugin_write_buffer_records_ =
reinterpret_cast<decltype(rocprofiler_plugin_write_buffer_records)*>(
dlsym(plugin_handle_, "rocprofiler_plugin_write_buffer_records"));
if (!rocprofiler_plugin_write_buffer_records_) return;
rocprofiler_plugin_write_record_ = reinterpret_cast<decltype(rocprofiler_plugin_write_record)*>(
dlsym(plugin_handle_, "rocprofiler_plugin_write_record"));
if (!rocprofiler_plugin_write_record_) return;
rocprofiler_plugin_finalize_ = reinterpret_cast<decltype(rocprofiler_plugin_finalize)*>(
dlsym(plugin_handle_, "rocprofiler_plugin_finalize"));
if (!rocprofiler_plugin_finalize_) return;
if (auto* initialize = reinterpret_cast<decltype(rocprofiler_plugin_initialize)*>(
dlsym(plugin_handle_, "rocprofiler_plugin_initialize"));
initialize != nullptr)
valid_ = initialize(ROCPROFILER_VERSION_MAJOR, ROCPROFILER_VERSION_MINOR) == 0;
}
~rocprofiler_plugin_t() {
if (is_valid()) rocprofiler_plugin_finalize_();
if (plugin_handle_ != nullptr) dlclose(plugin_handle_);
}
bool is_valid() const { return valid_; }
template <typename... Args> auto write_callback_record(Args... args) const {
assert(is_valid());
return rocprofiler_plugin_write_record_(std::forward<Args>(args)...);
}
template <typename... Args> auto write_buffer_records(Args... args) const {
assert(is_valid());
return rocprofiler_plugin_write_buffer_records_(std::forward<Args>(args)...);
}
private:
bool valid_{false};
void* plugin_handle_;
decltype(rocprofiler_plugin_finalize)* rocprofiler_plugin_finalize_;
decltype(rocprofiler_plugin_write_buffer_records)* rocprofiler_plugin_write_buffer_records_;
decltype(rocprofiler_plugin_write_record)* rocprofiler_plugin_write_record_;
};
std::optional<rocprofiler_plugin_t> plugin;
} // namespace
uint64_t getFlushIntervalFromEnv() {
const char* path = getenv("ROCPROFILER_FLUSH_INTERVAL");
if (path) return std::stoll(std::string(path), nullptr, 0);
return 0;
}
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;
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;
}
void finish() {
if (amd_sys_handler.load(std::memory_order_release)) {
amd_sys_handler.exchange(false, std::memory_order_release);
wait_for_start_shm.join();
shm_unlink(std::to_string(*amd_sys_session_id).c_str());
}
if (session_created.load(std::memory_order_relaxed)) {
session_created.exchange(false, std::memory_order_release);
CHECK_ROCMTOOLS(rocprofiler_terminate_session(session_id));
for ([[maybe_unused]] rocprofiler_buffer_id_t buffer_id : buffer_ids) {
CHECK_ROCMTOOLS(rocprofiler_flush_data(session_id, buffer_id));
}
}
// CHECK_ROCMTOOLS(rocprofiler_destroy_session(session_id));
// CHECK_ROCMTOOLS(rocprofiler_finalize());
}
// load plugins
void plugins_load() {
// Load output plugin
if (Dl_info dl_info; dladdr((void*)plugins_load, &dl_info) != 0) {
const char* plugin_name = getenv("ROCPROFILER_PLUGIN_LIB");
if (plugin_name == nullptr) {
if (fs::path(dl_info.dli_fname).string().find("build") != std::string::npos) {
plugin_name = "libfile_plugin.so";
} else {
plugin_name = "rocmtools/libfile_plugin.so";
}
}
if (!plugin.emplace(fs::path(dl_info.dli_fname).replace_filename(plugin_name)).is_valid()) {
plugin.reset();
}
}
}
void plugin_write_record(rocprofiler_record_tracer_t record, rocprofiler_session_id_t session_id) {
if (plugin) plugin->write_callback_record(record, session_id);
}
void wait_for_amdsys() {
while (amd_sys_handler.load(std::memory_order_relaxed)) {
shm_fd_sn = shm_open(amd_sys_session_id, O_RDONLY, 0666);
if (shm_fd_sn < 0) {
continue;
}
shmd = reinterpret_cast<struct shmd_t*>(mmap(0, 1024, PROT_READ, MAP_SHARED, shm_fd_sn, 0));
bool flag{false};
if (shmd && (sizeof(shmd->command) == sizeof(int))) {
switch (shmd->command) {
// Start
case 4: {
printf("AMDSYS:: Starting Tools Session...\n");
CHECK_ROCMTOOLS(rocprofiler_start_session(session_id));
session_created.exchange(true, std::memory_order_release);
break;
}
// Stop
case 5: {
if (session_created.load(std::memory_order_relaxed)) {
printf("AMDSYS:: Stopping Tools Session...\n");
session_created.exchange(false, std::memory_order_release);
CHECK_ROCMTOOLS(rocprofiler_terminate_session(session_id));
for ([[maybe_unused]] rocprofiler_buffer_id_t buffer_id : buffer_ids) {
CHECK_ROCMTOOLS(rocprofiler_flush_data(session_id, buffer_id));
}
}
break;
}
// Exit
case 6: {
printf("AMDSYS:: Exiting the Application..\n");
if (session_created.load(std::memory_order_relaxed)) {
printf("AMDSYS:: Stopping Tools Session...\n");
session_created.exchange(false, std::memory_order_release);
CHECK_ROCMTOOLS(rocprofiler_terminate_session(session_id));
for ([[maybe_unused]] rocprofiler_buffer_id_t buffer_id : buffer_ids) {
CHECK_ROCMTOOLS(rocprofiler_flush_data(session_id, buffer_id));
}
}
amd_sys_handler.exchange(false, std::memory_order_release);
flag = true;
}
}
}
shm_unlink(amd_sys_session_id);
if (flag) break;
}
}
static int info_callback(const rocprofiler_counter_info_t info, const char* gpu_name,
uint32_t gpu_index) {
fprintf(stdout, "\n %s:%u : %s : %s\n", gpu_name, gpu_index, info.name, info.description);
if (info.expression != nullptr) {
fprintf(stdout, " %s = %s\n", info.name, info.expression);
} else {
if (info.instances_count > 1) fprintf(stdout, "[0-%u]", info.instances_count - 1);
fprintf(stdout, " : %s\n", info.description);
fprintf(stdout, " block %s can only handle %u counters at a time\n", info.block_name,
info.block_counters);
}
fflush(stdout);
return 1;
}
extern "C" {
// The HSA_AMD_TOOL_PRIORITY variable must be a constant value type
// initialized by the loader itself, not by code during _init. 'extern const'
// seems do that although that is not a guarantee.
ROCPROFILER_EXPORT extern const uint32_t HSA_AMD_TOOL_PRIORITY = 1025;
/**
@brief Callback function called upon loading the HSA.
The function updates the core api table function pointers to point to the
interceptor functions in this file.
*/
ROCPROFILER_EXPORT bool OnLoad(HsaApiTable* table, uint64_t runtime_version,
uint64_t failed_tool_count, const char* const* failed_tool_names) {
if (rocprofiler_version_major() != ROCPROFILER_VERSION_MAJOR ||
rocprofiler_version_minor() < ROCPROFILER_VERSION_MINOR) {
warning("the ROCMTools API version is not compatible with this tool");
return true;
}
std::atexit(finish);
amd_sys_session_id = getenv("ROCPROFILER_ENABLE_AMDSYS");
if (amd_sys_session_id != nullptr) {
printf("AMDSYS Session Started!\n");
wait_for_start_shm = std::thread{wait_for_amdsys};
amd_sys_handler.exchange(true, std::memory_order_release);
}
CHECK_ROCMTOOLS(rocprofiler_initialize());
// Printing out info
char* info_symb = getenv("ROCPROFILER_COUNTER_LIST");
if (info_symb != nullptr) {
if (*info_symb == 'b')
printf("Basic HW counters:\n");
else
printf("Derived metrics:\n");
CHECK_ROCMTOOLS(rocprofiler_iterate_counters(info_callback));
exit(1);
}
// load the plugins
plugins_load();
std::vector<rocprofiler_tracer_activity_domain_t> apis_requested;
if (getenv("ROCPROFILER_HIP_API_TRACE")) apis_requested.emplace_back(ACTIVITY_DOMAIN_HIP_API);
if (getenv("ROCPROFILER_HIP_ACTIVITY_TRACE")) apis_requested.emplace_back(ACTIVITY_DOMAIN_HIP_OPS);
if (getenv("ROCPROFILER_HSA_API_TRACE")) apis_requested.emplace_back(ACTIVITY_DOMAIN_HSA_API);
if (getenv("ROCPROFILER_HSA_ACTIVITY_TRACE")) apis_requested.emplace_back(ACTIVITY_DOMAIN_HSA_OPS);
if (getenv("ROCPROFILER_ROCTX_TRACE")) apis_requested.emplace_back(ACTIVITY_DOMAIN_ROCTX);
std::vector<std::string> counters = GetCounterNames();
std::vector<const char*> counters_;
if (counters.size() > 0) {
printf("ROCMTools: Collecting the following counters:\n");
for (size_t i = 0; i < counters.size(); i++) {
counters_.emplace_back(counters.at(i).c_str());
printf("- %s\n", counters_.back());
}
}
CHECK_ROCMTOOLS(rocprofiler_create_session(ROCPROFILER_KERNEL_REPLAY_MODE, &session_id));
bool want_pc_sampling = getenv("ROCPROFILER_PC_SAMPLING");
std::vector<rocprofiler_filter_kind_t> filters_requested;
if ((counters.size() == 0 && (apis_requested.size() == 0 || getenv("ROCPROFILER_KERNEL_TRACE")))
|| want_pc_sampling /* PC sampling needs a profiler, even it's doing
nothing */)
filters_requested.emplace_back(ROCPROFILER_DISPATCH_TIMESTAMPS_COLLECTION);
if (want_pc_sampling) {
filters_requested.emplace_back(ROCPROFILER_PC_SAMPLING_COLLECTION);
}
if (counters.size() > 0) filters_requested.emplace_back(ROCPROFILER_COUNTERS_COLLECTION);
if (apis_requested.size() > 0) filters_requested.emplace_back(ROCPROFILER_API_TRACE);
rocprofiler_buffer_id_t buffer_id;
CHECK_ROCMTOOLS(rocprofiler_create_buffer(
session_id,
[](const rocprofiler_record_header_t* record, const rocprofiler_record_header_t* end_record,
rocprofiler_session_id_t session_id, rocprofiler_buffer_id_t buffer_id) {
if (plugin) plugin->write_buffer_records(record, end_record, session_id, buffer_id);
},
0x9999, &buffer_id));
buffer_ids.emplace_back(buffer_id);
rocprofiler_buffer_id_t buffer_id_1;
CHECK_ROCMTOOLS(rocprofiler_create_buffer(
session_id,
[](const rocprofiler_record_header_t* record, const rocprofiler_record_header_t* end_record,
rocprofiler_session_id_t session_id, rocprofiler_buffer_id_t buffer_id_1) {
if (plugin) plugin->write_buffer_records(record, end_record, session_id, buffer_id_1);
},
0x9999, &buffer_id_1));
buffer_ids.emplace_back(buffer_id_1);
for (rocprofiler_filter_kind_t filter_kind : filters_requested) {
switch (filter_kind) {
case ROCPROFILER_COUNTERS_COLLECTION: {
printf("Enabling Counter Collection\n");
rocprofiler_filter_id_t filter_id;
[[maybe_unused]] rocprofiler_filter_property_t property = {};
CHECK_ROCMTOOLS(rocprofiler_create_filter(
session_id, filter_kind, rocprofiler_filter_data_t{.counters_names = &counters_[0]},
counters_.size(), &filter_id, property));
CHECK_ROCMTOOLS(rocprofiler_set_filter_buffer(session_id, filter_id, buffer_id_1));
filter_ids.emplace_back(filter_id);
break;
}
case ROCPROFILER_DISPATCH_TIMESTAMPS_COLLECTION: {
rocprofiler_filter_id_t filter_id;
[[maybe_unused]] rocprofiler_filter_property_t property = {};
CHECK_ROCMTOOLS(rocprofiler_create_filter(session_id, filter_kind, rocprofiler_filter_data_t{},
0, &filter_id, property));
CHECK_ROCMTOOLS(rocprofiler_set_filter_buffer(session_id, filter_id, buffer_id_1));
filter_ids.emplace_back(filter_id);
break;
}
case ROCPROFILER_API_TRACE: {
printf("Enabling API Tracing\n");
rocprofiler_filter_id_t filter_id;
[[maybe_unused]] rocprofiler_filter_property_t property = {};
CHECK_ROCMTOOLS(rocprofiler_create_filter(session_id, filter_kind,
rocprofiler_filter_data_t{&apis_requested[0]},
apis_requested.size(), &filter_id, property));
CHECK_ROCMTOOLS(rocprofiler_set_filter_buffer(session_id, filter_id, buffer_id));
CHECK_ROCMTOOLS(
rocprofiler_set_api_trace_sync_callback(session_id, filter_id, plugin_write_record));
filter_ids.emplace_back(filter_id);
break;
}
case ROCPROFILER_PC_SAMPLING_COLLECTION: {
puts("Enabling PC sampling");
rocprofiler_filter_id_t filter_id;
[[maybe_unused]] rocprofiler_filter_property_t property = {};
CHECK_ROCMTOOLS(rocprofiler_create_filter(session_id, filter_kind,
rocprofiler_filter_data_t{},
0, &filter_id, property));
CHECK_ROCMTOOLS(rocprofiler_set_filter_buffer(session_id, filter_id, buffer_id));
filter_ids.emplace_back(filter_id);
break;
}
default:
warning("Not available for profiling or tracing");
}
}
if (getenv("ROCPROFILER_ENABLE_AMDSYS") == nullptr) {
CHECK_ROCMTOOLS(rocprofiler_start_session(session_id));
session_created.exchange(true, std::memory_order_release);
}
return true;
}
/**
@brief Callback function upon unloading the HSA.
*/
ROCPROFILER_EXPORT void OnUnload() { printf("\n\nTool is getting unloaded\n\n"); }
} // extern "C"