Skip to content

Commit

Permalink
improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Applencourt committed Jun 18, 2024
1 parent d0ecf9c commit 2959fbc
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 91 deletions.
57 changes: 32 additions & 25 deletions utils/xprof_utils.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#pragma once

#include "babeltrace2/babeltrace.h"
#include "magic_enum.hpp"
#include <map>
#include <optional>
#include <stdexcept>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <optional>
#include <type_traits>
#include <unordered_map>
#include <vector>
#include <stdexcept>
#include <string.h>

enum backend_e {
BACKEND_UNKNOWN = 0,
Expand All @@ -21,14 +22,25 @@ enum backend_e {
BACKEND_HIP = 6,
};

constexpr const char *pretty_backend_name[] = {
"unknown",
"ze",
"cl",
"cuda",
"omp_target",
"omp",
"hip",
// Should be ordered by the value of the backend_e
constexpr const char *pretty_backend_name_g[magic_enum::enum_count<backend_e>()] = {
"unknown", // BACKEND_UNKNOWN
"ze", // BACKEND_ZE
"cl", // BACKEND_OPENCL
"cuda", // BACKEND_CUDA
"omp_target", // BACKEND_OMP_TARGET_OPERATIONS
"omp", // BACKEND_OMP
"hip", // BACKEND_HIP
};

constexpr const int backend_levels_g[magic_enum::enum_count<backend_e>()] = {
0, // BACKEND_UNKNOWN
1, // BACKEND_ZE
1, // BACKEND_OPENCL
1, // BACKEND_CUDA
2, // BACKEND_OMP_TARGET_OPERATIONS
2, // BACKEND_OMP
1, // BACKEND_HIP
};

typedef enum backend_e backend_t;
Expand Down Expand Up @@ -64,8 +76,7 @@ typedef std::tuple<thapi_function_name, long> fn_ts_t;
// Most efficient possible access when NDEBUG is set; if not
// set, use .at which can be debugged with stack trace in
// valgrind's memcheck.
template <class M, class K>
inline auto& thapi_at(M& map, K key) {
template <class M, class K> inline auto &thapi_at(M &map, K key) {
#ifdef THAPI_DEBUG
return map.at(key);
#else
Expand Down Expand Up @@ -117,20 +128,17 @@ template <typename... TT> struct hash<std::pair<TT...>> {
};
} // namespace std


// Save Entry State so can be accessed in Exit
class EntryState {
public:
template <class K,
typename = std::enable_if_t<std::is_trivially_copyable_v<K>>>
template <class K, typename = std::enable_if_t<std::is_trivially_copyable_v<K>>>
void set_data(hpt_t hpt, K v) {
const auto *b = (std::byte *)&v;
std::vector<std::byte> res{b, b + sizeof(K)};
set_data_impl(hpt, res);
}

template <class K,
typename = std::enable_if_t<std::is_trivially_copyable_v<K>>>
template <class K, typename = std::enable_if_t<std::is_trivially_copyable_v<K>>>
K get_data(hpt_t hpt) {
auto &v = set_data_impl(hpt);
const K res{*(K *)(v.value().data())};
Expand All @@ -145,8 +153,7 @@ class EntryState {
set_data_impl(hpt, res);
}

template <class K,
typename = std::enable_if_t<std::is_same_v<K, std::string>>>
template <class K, typename = std::enable_if_t<std::is_same_v<K, std::string>>>
std::enable_if_t<std::is_same_v<K, std::string>, K> get_data(hpt_t hpt) {
auto &v = set_data_impl(hpt);
const std::string res{(char *)v.value().data()};
Expand Down Expand Up @@ -174,13 +181,13 @@ class EntryState {
}
}

std::optional<std::vector<std::byte>> &set_data_impl(hpt_t hpt) { return thapi_at(entry_data, hpt); }
std::optional<std::vector<std::byte>> &set_data_impl(hpt_t hpt) {
return thapi_at(entry_data, hpt);
}
};


// ClassName Striping
template <size_t SuffixLen>
static inline std::string _strip_event_class_name(const char *str) {
template <size_t SuffixLen> static inline std::string _strip_event_class_name(const char *str) {
const char *p = strchr(str + strlen("lttng_"), ':') + 1;
return std::string{p, strlen(p) - SuffixLen};
}
Expand Down
133 changes: 68 additions & 65 deletions xprof/btx_tally.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <metababel/metababel.h>
#include "magic_enum.hpp"
#include "btx_tally.hpp"
#include "magic_enum.hpp"
#include "my_demangle.h"
#include "xprof_utils.hpp"
#include <array>
#include <metababel/metababel.h>
#include <sstream> // std::stringstream, std::stringbuf
#include <string>
#include <tuple>
Expand All @@ -12,17 +12,17 @@

class TallyCoreTime : public TallyCoreString {
using TallyCoreString::TallyCoreString;

public:
static constexpr std::array headers{"Time", "Time(%)", "Calls", "Average", "Min", "Max", "Error"};
virtual const std::array<std::string, nfields> to_string() override {
return std::array{
(count == error) ? "" : this->format_time(duration),
(count == error) ? "" : this->to_pretty_string(100. * duration_ratio, "%"),
this->to_pretty_string(count, "", 0),
(count == error) ? "" : this->format_time(average()),
(count == error) ? "" : this->format_time(min),
(count == error) ? "" : this->format_time(max),
this->to_pretty_string(error, "", 0)};
return std::array{(count == error) ? "" : this->format_time(duration),
(count == error) ? "" : this->to_pretty_string(100. * duration_ratio, "%"),
this->to_pretty_string(count, "", 0),
(count == error) ? "" : this->format_time(average()),
(count == error) ? "" : this->format_time(min),
(count == error) ? "" : this->format_time(max),
this->to_pretty_string(error, "", 0)};
}

private:
Expand Down Expand Up @@ -54,16 +54,17 @@ class TallyCoreTime : public TallyCoreString {

class TallyCoreByte : public TallyCoreString {
using TallyCoreString::TallyCoreString;

public:
static constexpr std::array headers{"Byte", "Byte(%)", "Calls", "Average", "Min", "Max", "Error"};
virtual const std::array<std::string, nfields> to_string() override {
return std::array {format_byte(duration),
this->to_pretty_string(100. * duration_ratio, "%"),
this->to_pretty_string(count, "", 0),
this->format_byte(average()),
this->format_byte(min),
this->format_byte(max),
this->to_pretty_string(error, "", 0)};
return std::array{format_byte(duration),
this->to_pretty_string(100. * duration_ratio, "%"),
this->to_pretty_string(count, "", 0),
this->format_byte(average()),
this->format_byte(min),
this->format_byte(max),
this->to_pretty_string(error, "", 0)};
}

private:
Expand Down Expand Up @@ -93,15 +94,14 @@ class TallyCoreByte : public TallyCoreString {
}
};


//! User data collection structure.
//! It is used to collect interval messages data, once data is collected,
//! it is aggregated and tabulated for printing.
struct tally_dispatch_s {
//! User params provided to the user component.
btx_params_t *params;

std::array<int, magic_enum::enum_count<backend_t>()> backend_levels;
std::unordered_map<int, backend_level_t> backend_traced_levels;

std::map<backend_level_t, std::set<std::string>> host_backend_name;
std::map<backend_level_t, std::set<std::string>> traffic_backend_name;
Expand All @@ -123,14 +123,6 @@ static std::string join_iterator(const std::set<std::string> &x, std::string del
});
}

static int get_backend_id(std::string name) {
for (size_t i = 0; i < magic_enum::enum_count<backend_t>(); ++i)
// backend_name is located in xprof_utils.hpp
if (std::string{pretty_backend_name[i]} == name)
return i;
return -1;
}

static thapi_function_name f_demangle_name(thapi_function_name mangle_name) {
std::string result = mangle_name;
std::string line_num;
Expand Down Expand Up @@ -177,35 +169,37 @@ static void initialize_component_callback(void **usr_data) {
/* User allocates its own data structure */
auto *data = new tally_dispatch_t;
*usr_data = data;

/* Backend information must match enum backend_e in xprof_utils.hpp */
data->backend_levels = {
2, // BACKEND_UNKNOWN
2, // BACKEND_ZE
2, // BACKEND_OPENCL
2, // BACKEND_CUDA
1, // BACKEND_OMP_TARGET_OPERATIONS
0, // BACKEND_OMP
2, // BACKEND_HIP
};
}

static void read_params_callback(void *usr_data, btx_params_t *usr_params) {

auto *data = static_cast<tally_dispatch_t *>(usr_data);
data->params = usr_params;

// Consumes key:value pairs in the stringstream k1:v1,..,kn:vn
// pretty_backend_name and backend_levels store in xprof_utils.hpp
auto get_backend_id = [](std::string name) {
for (size_t i = 0; i < magic_enum::enum_count<backend_t>(); ++i)
if (std::string{pretty_backend_name_g[i]} == name)
return int(i);
return -1;
};

// Consumes key:value pairs in the stringstream b1:l1,...
std::stringstream tokens{data->params->backend_levels};
std::string tmp;
while (std::getline(tokens, tmp, ',')) {
std::stringstream tmp_string{tmp};
std::string k, v;
std::getline(tmp_string, k, ':');
int id = get_backend_id(k);
assert((id > 0) && "Backend not found. Please check --backend-level format.");
std::getline(tmp_string, v);
data->backend_levels[id] = std::stoi(v);
for (std::string bl; std::getline(tokens, bl, ',');) {
auto pos = bl.find(':');
int backend_id = get_backend_id(bl.substr(0, pos));
if (backend_id == -1)
continue;
if (pos != std::string::npos) {
int level = std::stoi(bl.substr(pos + 1, bl.length()));
data->backend_traced_levels[backend_id] = level;
} else
data->backend_traced_levels[backend_id] = backend_levels_g[backend_id];
}
if (data->backend_traced_levels.empty()) {
for (size_t backend_id = 0; backend_id < magic_enum::enum_count<backend_e>(); backend_id++)
data->backend_traced_levels[backend_id] = backend_levels_g[backend_id];
}
}

Expand All @@ -221,21 +215,21 @@ static void finalize_component_callback(void *usr_data) {

if (strcmp(data->params->display, "compact") == 0) {

for (const auto &[level, host] : data->host) {
for (const auto &[level, host] : reverse(data->host)) {
std::string s = join_iterator(data->host_backend_name[level]);
print_compact(s, host, std::make_tuple("Hostnames", "Processes", "Threads"), max_name_size);
}
print_compact("Device profiling", data->device,
std::make_tuple("Hostnames", "Processes", "Threads", "Devices", "Subdevices"),
max_name_size);

for (const auto &[level, traffic] : data->traffic) {
for (const auto &[level, traffic] : reverse(data->traffic)) {
std::string s = join_iterator(data->traffic_backend_name[level]);
print_compact("Explicit memory traffic (" + s + ")", traffic,
std::make_tuple("Hostnames", "Processes", "Threads"), max_name_size);
}
} else {
for (const auto &[level, host] : data->host) {
for (const auto &[level, host] : reverse(data->host)) {
std::string s = join_iterator(data->host_backend_name[level]);
print_extended(s, host, std::make_tuple("Hostname", "Process", "Thread"), max_name_size);
}
Expand All @@ -244,41 +238,43 @@ static void finalize_component_callback(void *usr_data) {
std::make_tuple("Hostname", "Process", "Thread", "Device pointer", "Subdevice pointer"),
max_name_size);

for (const auto &[level, traffic] : data->traffic) {
for (const auto &[level, traffic] : reverse(data->traffic)) {
std::string s = join_iterator(data->traffic_backend_name[level]);
print_extended("Explicit memory traffic (" + s + ")", traffic,
std::make_tuple("Hostname", "Process", "Thread"), max_name_size);
}
}
} else {

nlohmann::json j;
j["units"] = {{"time", "ns"}, {"size", "bytes"}};

if (data->params->display_metadata)
j["metadata"] = data->metadata;

if (strcmp(data->params->display, "compact") == 0) {
for (auto &[level, host] : data->host)
j["host"][level] = json_compact(host);
for (auto &[level, host] : reverse(data->host)) {
j["host"][std::to_string(level)] = json_compact(host);
}

if (!data->device.empty())
j["device"] = json_compact(data->device);

for (auto &[level, traffic] : data->traffic)
j["traffic"][level] = json_compact(traffic);
for (auto &[level, traffic] : reverse(data->traffic))
j["traffic"][std::to_string(level)] = json_compact(traffic);

} else {
for (auto &[level, host] : data->host)
j["host"][level] = json_extented(host, std::make_tuple("Hostname", "Process", "Thread"));
for (auto &[level, host] : reverse(data->host)) {
j["host"][std::to_string(level)] =
json_extented(host, std::make_tuple("Hostname", "Process", "Thread"));
}

if (!data->device.empty())
j["device"] =
json_extented(data->device, std::make_tuple("Hostname", "Process", "Thread",
"Device pointer", "Subdevice pointer"));

for (auto &[level, traffic] : data->traffic)
j["traffic"][level] =
for (auto &[level, traffic] : reverse(data->traffic))
j["traffic"][std::to_string(level)] =
json_extented(traffic, std::make_tuple("Hostname", "Process", "Thread"));
}
std::cout << j << std::endl;
Expand All @@ -294,7 +290,10 @@ static void aggreg_host_callback(void *btx_handle, void *usr_data, const char *h
uint64_t err) {

auto *data = static_cast<tally_dispatch_t *>(usr_data);
const int level = data->backend_levels[backend];
const auto key = data->backend_traced_levels.find(backend);
if (key == data->backend_traced_levels.end())
return;
const int level = key->second;
std::string backend_name(magic_enum::enum_names<backend_t>()[backend]);
data->host_backend_name[level].insert(backend_name);
data->host[level][{hostname, vpid, vtid, name}] += {total, err, count, min, max};
Expand All @@ -317,11 +316,15 @@ static void aggreg_device_callback(void *btx_handle, void *usr_data, const char

static void aggreg_traffic_callback(void *btx_handle, void *usr_data, const char *hostname,
int64_t vpid, uint64_t vtid, const char *name, uint64_t min,
uint64_t max, uint64_t total, uint64_t count,
uint64_t backend, const char *metadata) {
uint64_t max, uint64_t total, uint64_t count, uint64_t backend,
const char *metadata) {

auto *data = static_cast<tally_dispatch_t *>(usr_data);
const int level = data->backend_levels[backend];

const auto key = data->backend_traced_levels.find(backend);
if (key == data->backend_traced_levels.end())
return;
const int level = key->second;
std::string backend_name(magic_enum::enum_names<backend_t>()[backend]);
data->traffic_backend_name[level].insert(backend_name);

Expand Down
Loading

0 comments on commit 2959fbc

Please sign in to comment.