tool xml input
Этот коммит содержится в:
@@ -29,6 +29,7 @@ THE SOFTWARE.
|
||||
#include <inc/roctracer_hip.h>
|
||||
#include <inc/roctracer_hcc.h>
|
||||
#include <inc/ext/hsa_rt_utils.hpp>
|
||||
#include <util/xml.h>
|
||||
|
||||
#define PUBLIC_API __attribute__((visibility("default")))
|
||||
#define CONSTRUCTOR_API __attribute__((constructor))
|
||||
@@ -51,6 +52,14 @@ thread_local timestamp_t hip_begin_timestamp = 0;
|
||||
bool trace_hsa = false;
|
||||
bool trace_hip = false;
|
||||
|
||||
// Error handler
|
||||
void fatal(const std::string msg) {
|
||||
fflush(stdout);
|
||||
fprintf(stderr, "%s\n\n", msg.c_str());
|
||||
fflush(stderr);
|
||||
abort();
|
||||
}
|
||||
|
||||
// HSA API callback function
|
||||
void hsa_api_callback(
|
||||
uint32_t domain,
|
||||
@@ -150,22 +159,119 @@ void activity_callback(const char* begin, const char* end, void* arg) {
|
||||
}
|
||||
}
|
||||
|
||||
// Input parser
|
||||
std::string normalize_token(const std::string& token, bool not_empty, const std::string& label) {
|
||||
const std::string space_chars_set = " \t";
|
||||
const size_t first_pos = token.find_first_not_of(space_chars_set);
|
||||
size_t norm_len = 0;
|
||||
std::string error_str = "none";
|
||||
if (first_pos != std::string::npos) {
|
||||
const size_t last_pos = token.find_last_not_of(space_chars_set);
|
||||
if (last_pos == std::string::npos) error_str = "token string error: \"" + token + "\"";
|
||||
else {
|
||||
const size_t end_pos = last_pos + 1;
|
||||
if (end_pos <= first_pos) error_str = "token string error: \"" + token + "\"";
|
||||
else norm_len = end_pos - first_pos;
|
||||
}
|
||||
}
|
||||
if (((first_pos != std::string::npos) && (norm_len == 0)) ||
|
||||
((first_pos == std::string::npos) && not_empty)) {
|
||||
fatal("normalize_token error, " + label + ": '" + token + "'," + error_str);
|
||||
}
|
||||
return (norm_len != 0) ? token.substr(first_pos, norm_len) : std::string("");
|
||||
}
|
||||
|
||||
int get_xml_array(const xml::Xml::level_t* node, const std::string& field, const std::string& delim, std::vector<std::string>* vec, const char* label = NULL) {
|
||||
int parse_iter = 0;
|
||||
const auto& opts = node->opts;
|
||||
auto it = opts.find(field);
|
||||
if (it != opts.end()) {
|
||||
const std::string array_string = it->second;
|
||||
if (label != NULL) printf("%s%s = %s\n", label, field.c_str(), array_string.c_str());
|
||||
size_t pos1 = 0;
|
||||
const size_t string_len = array_string.length();
|
||||
while (pos1 < string_len) {
|
||||
const size_t pos2 = array_string.find(delim, pos1);
|
||||
const bool found = (pos2 != std::string::npos);
|
||||
const size_t token_len = (pos2 != std::string::npos) ? pos2 - pos1 : string_len - pos1;
|
||||
const std::string token = array_string.substr(pos1, token_len);
|
||||
const std::string norm_str = normalize_token(token, found, "get_xml_array");
|
||||
if (norm_str.length() != 0) vec->push_back(norm_str);
|
||||
if (!found) break;
|
||||
pos1 = pos2 + 1;
|
||||
++parse_iter;
|
||||
}
|
||||
}
|
||||
return parse_iter;
|
||||
}
|
||||
|
||||
// HSA-runtime tool on-load method
|
||||
extern "C" PUBLIC_API bool OnLoad(HsaApiTable* table, uint64_t runtime_version, uint64_t failed_tool_count,
|
||||
const char* const* failed_tool_names) {
|
||||
timer = new hsa_rt_utils::Timer(table->core_->hsa_system_get_info_fn);
|
||||
|
||||
// API traces switches
|
||||
const char* trace_domain = getenv("ROCTRACER_DOMAIN");
|
||||
trace_hsa = (trace_domain == NULL) || (strncmp(trace_domain, "hsa", 3) == 0);
|
||||
trace_hip = (trace_domain == NULL) || (strncmp(trace_domain, "hip", 3) == 0);
|
||||
|
||||
// API trace vector
|
||||
std::vector<std::string> hsa_api_vec;
|
||||
|
||||
// XML input
|
||||
const char* xml_name = getenv("ROCP_INPUT");
|
||||
if (xml_name != NULL) {
|
||||
printf("ROCTracer: input from \"%s\"\n", xml_name);
|
||||
xml::Xml* xml = xml::Xml::Create(xml_name);
|
||||
if (xml == NULL) {
|
||||
fprintf(stderr, "ROCTracer: Input file not found '%s'\n", xml_name);
|
||||
abort();
|
||||
}
|
||||
|
||||
for (const auto* entry : xml->GetNodes("top.trace")) {
|
||||
auto it = entry->opts.find("name");
|
||||
if (it == entry->opts.end()) fatal("ROCTracer: trace name is missing");
|
||||
const std::string& name = it->second;
|
||||
|
||||
std::vector<std::string> api_vec;
|
||||
for (const auto* node : entry->nodes) {
|
||||
if (node->tag != "parameters") fatal("ROCProfiler: trace node is not supported '" + name + ":" + node->tag + "'");
|
||||
get_xml_array(node, "api", ",", &api_vec);
|
||||
break;
|
||||
}
|
||||
|
||||
if (name == "HSA") {
|
||||
trace_hsa |= true;
|
||||
hsa_api_vec = api_vec;
|
||||
}
|
||||
if (name == "HIP") {
|
||||
trace_hip |= true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Enable HSA API callbacks
|
||||
if (trace_hsa) {
|
||||
ROCTRACER_CALL(roctracer_enable_domain_callback(ACTIVITY_DOMAIN_HSA_API, hsa_api_callback, NULL));
|
||||
printf(" HSA-trace");
|
||||
if (hsa_api_vec.size() != 0) {
|
||||
printf("(");
|
||||
for (unsigned i = 0; i < hsa_api_vec.size(); ++i) {
|
||||
uint32_t cid = HSA_API_ID_NUMBER;
|
||||
const char* api = hsa_api_vec[i].c_str();
|
||||
ROCTRACER_CALL(roctracer_op_code(ACTIVITY_DOMAIN_HSA_API, api, &cid));
|
||||
ROCTRACER_CALL(roctracer_enable_op_callback(ACTIVITY_DOMAIN_HSA_API, cid, hsa_api_callback, NULL));
|
||||
printf(" %s", api);
|
||||
}
|
||||
printf(" )");
|
||||
} else {
|
||||
ROCTRACER_CALL(roctracer_enable_domain_callback(ACTIVITY_DOMAIN_HSA_API, hsa_api_callback, NULL));
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// Enable HIP API callbacks/activity
|
||||
if (trace_hip) {
|
||||
printf(" HIP-trace\n");
|
||||
// Allocating tracing pool
|
||||
roctracer_properties_t properties{};
|
||||
properties.buffer_size = 12;
|
||||
@@ -181,8 +287,12 @@ extern "C" PUBLIC_API bool OnLoad(HsaApiTable* table, uint64_t runtime_version,
|
||||
|
||||
// HSA-runtime tool on-unload method
|
||||
extern "C" PUBLIC_API void OnUnload() {
|
||||
if (trace_hsa) ROCTRACER_CALL(roctracer_disable_domain_callback(ACTIVITY_DOMAIN_HSA_API));
|
||||
if (trace_hip) ROCTRACER_CALL(roctracer_disable_domain_callback(ACTIVITY_DOMAIN_HIP_API));
|
||||
if (trace_hip) ROCTRACER_CALL(roctracer_disable_domain_callback(ACTIVITY_DOMAIN_HCC_OPS));
|
||||
ROCTRACER_CALL(roctracer_close_pool());
|
||||
if (trace_hsa) {
|
||||
ROCTRACER_CALL(roctracer_disable_domain_callback(ACTIVITY_DOMAIN_HSA_API));
|
||||
}
|
||||
if (trace_hip) {
|
||||
ROCTRACER_CALL(roctracer_disable_domain_callback(ACTIVITY_DOMAIN_HIP_API));
|
||||
ROCTRACER_CALL(roctracer_disable_domain_callback(ACTIVITY_DOMAIN_HCC_OPS));
|
||||
ROCTRACER_CALL(roctracer_close_pool());
|
||||
}
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user