Skip to content

Commit

Permalink
timeline cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
solo2abera committed Jun 13, 2024
1 parent aace2fd commit 7935e50
Showing 1 changed file with 80 additions and 76 deletions.
156 changes: 80 additions & 76 deletions xprof/btx_timeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@
#include <iomanip> // set precision
#include <iostream> // stdcout
#include <map>
//#include <optional>
#include <string>
#include <stack>
#include <unordered_map>
#include <utility> // pair

#include <sstream>
#include <tuple>


#include "perfetto_prunned.pb.h"

#define TRUSTED_PACKED_SEQUENCE_ID 10
Expand Down Expand Up @@ -43,8 +38,16 @@ struct timeline_dispatch_s {
std::unordered_map<hp_dfsdev_t, perfetto_uuid_t> hp_dfsdev2fptracks;
perfetto_pruned::Trace trace;
};

struct FabricDetails {
uintptr_t hFabricPort;
bool RxTx;
uint32_t fabricId;
uint32_t remotePortId;
};

using timeline_dispatch_t = struct timeline_dispatch_s;
using uuid_getter_t = perfetto_uuid_t (*)(timeline_dispatch_t *, const std::string &, uint64_t, uintptr_t, std::optional<uintptr_t>, uint32_t, std::optional<bool>);
using uuid_getter_t = perfetto_uuid_t (*)(timeline_dispatch_t *, const std::string &, uint64_t, uintptr_t, uint32_t, std::optional<FabricDetails>);

static perfetto_uuid_t gen_perfetto_uuid() {
// Start at one, Look like UUID 0 is special
Expand Down Expand Up @@ -83,34 +86,35 @@ static perfetto_uuid_t get_parent_counter_track_uuid(timeline_dispatch_t *dispat
return hp_uuid;
}

static perfetto_uuid_t
get_counter_track_uuuid(timeline_dispatch_t *dispatch,
std::unordered_map<hp_ddomain_t, perfetto_uuid_t> &counter_tracks,
const std::string &track_name, const std::string &hostname, uint64_t process_id,
thapi_device_id did, thapi_domain_idx domain, int64_t unit_multiplier = 1,
std::optional<uintptr_t> hFabricPort = std::nullopt,
std::unordered_map<hp_dfsdev_t, perfetto_uuid_t> *counter_tracks_fp = nullptr, std::optional<bool> RxTx = std::nullopt) {
static perfetto_uuid_t get_counter_track_uuuid(timeline_dispatch_t *dispatch,
std::unordered_map<hp_ddomain_t, perfetto_uuid_t> &counter_tracks,
const std::string &track_name, const std::string &hostname, uint64_t process_id,
thapi_device_id did, thapi_domain_idx domain, int64_t unit_multiplier = 1,
std::optional<FabricDetails> details = std::nullopt,
std::unordered_map<hp_dfsdev_t, perfetto_uuid_t> *counter_tracks_fp = nullptr) {
perfetto_uuid_t hp_dev_uuid = 0;
perfetto_uuid_t hp_uuid;
if (hFabricPort.has_value() && counter_tracks_fp != nullptr && RxTx.has_value()) {
auto [it, inserted] = counter_tracks_fp->insert({{hostname, process_id, did, hFabricPort.value(), domain, RxTx.value()}, hp_dev_uuid});

if (details && counter_tracks_fp != nullptr) { // Corrected variable name 'options' to 'option'
auto [it, inserted] = counter_tracks_fp->insert({{hostname, process_id, did, details->hFabricPort, domain, details->RxTx},
hp_dev_uuid});
auto &potential_uuid = it->second;
if (!inserted)
if (!inserted)
return potential_uuid;

hp_uuid = get_parent_counter_track_uuid(dispatch, hostname, process_id, did);
hp_dev_uuid = gen_perfetto_uuid();
potential_uuid = hp_dev_uuid;
} else {
auto [it, inserted] = counter_tracks.insert({{hostname, process_id, did, domain}, hp_dev_uuid});
auto &potential_uuid = it->second;
if (!inserted)
return potential_uuid;
hp_uuid = get_parent_counter_track_uuid(dispatch, hostname, process_id, did);
hp_dev_uuid = gen_perfetto_uuid();
potential_uuid = hp_dev_uuid;
}
} else {
auto [it, inserted] = counter_tracks.insert({{hostname, process_id, did, domain}, hp_dev_uuid});
auto &potential_uuid = it->second;
if (!inserted)
return potential_uuid;

hp_uuid = get_parent_counter_track_uuid(dispatch, hostname, process_id, did);
hp_dev_uuid = gen_perfetto_uuid();
potential_uuid = hp_dev_uuid;
}

auto *packet = dispatch->trace.add_packet();
packet->set_timestamp(0);
Expand All @@ -119,9 +123,11 @@ get_counter_track_uuuid(timeline_dispatch_t *dispatch,
track_descriptor->set_uuid(hp_dev_uuid);
track_descriptor->set_parent_uuid(hp_uuid);
std::ostringstream oss;
oss << track_name << " | Domain " << domain;
if (RxTx.has_value()) {
oss << (RxTx.value() ? " TX" : " RX");
if (details) {
oss << track_name << " | SD " << domain;
oss << " | " << details->fabricId << "<->"<< details->remotePortId << " | " <<(details->RxTx ? " TX" : " RX");
} else {
oss << track_name << " | Domain " << domain;
}
track_descriptor->set_name(oss.str());
auto *counter_descriptor = track_descriptor->mutable_counter();
Expand All @@ -130,92 +136,90 @@ get_counter_track_uuuid(timeline_dispatch_t *dispatch,
}

static perfetto_uuid_t get_copyEU_track_uuuid(timeline_dispatch_t *dispatch, const std::string &hostname, uint64_t process_id,
uintptr_t did, std::optional<uintptr_t> hFabricPort, uint32_t subDevice,
std::optional<bool> RxTx = std::nullopt) {
return get_counter_track_uuuid(dispatch, dispatch->hp_dsdev2cpytracks, "CopyEngine (%)", hostname,
process_id, did, subDevice, 100, std::nullopt, nullptr, RxTx);
uintptr_t did, uint32_t subDevice, std::optional<FabricDetails> options) {
return get_counter_track_uuuid(dispatch, dispatch->hp_dsdev2cpytracks, "CopyEngine (%)", hostname, process_id, did, subDevice, 100);
}

static perfetto_uuid_t get_fpThroughput_track_uuuid(timeline_dispatch_t *dispatch, const std::string &hostname, uint64_t process_id,
uintptr_t did, std::optional<uintptr_t> hFabricPort, uint32_t subDevice, std::optional<bool> RxTx) {
return get_counter_track_uuuid(dispatch, dispatch->hp_ddomain2pwrtracks, "Fabric | Throughput", hostname,
process_id, did, subDevice, 100, hFabricPort, &dispatch->hp_dfsdev2fptracks, RxTx);
static perfetto_uuid_t get_fpThroughput_track_uuuid(timeline_dispatch_t *dispatch, const std::string &hostname, uint64_t process_id,
uintptr_t did, uint32_t subDevice, std::optional<FabricDetails> options) {
return get_counter_track_uuuid(dispatch, dispatch->hp_ddomain2pwrtracks, "FabricT", hostname, process_id, did, subDevice, 100, options, &dispatch->hp_dfsdev2fptracks);
}

static perfetto_uuid_t get_power_track_uuuid(timeline_dispatch_t *dispatch, const std::string &hostname, uint64_t process_id,
uintptr_t did, std::optional<uintptr_t> hFabricPort, uint32_t subDevice, std::optional<bool> RxTx) {
return get_counter_track_uuuid(dispatch, dispatch->hp_ddomain2pwrtracks, " Power", hostname,
process_id, did, subDevice, 100, std::nullopt, nullptr, RxTx);
static perfetto_uuid_t get_power_track_uuuid(timeline_dispatch_t *dispatch, const std::string &hostname, uint64_t process_id,
uintptr_t did, uint32_t subDevice, std::optional<FabricDetails> options) {
// Extra space to maintain track sequence in the timeline
return get_counter_track_uuuid(dispatch, dispatch->hp_ddomain2pwrtracks, " Power", hostname, process_id, did, subDevice, 100);
}

static perfetto_uuid_t get_frequency_track_uuuid(timeline_dispatch_t *dispatch, const std::string &hostname, uint64_t process_id,
uintptr_t did, std::optional<uintptr_t> hFabricPort, uint32_t subDevice, std::optional<bool> RxTx) {
return get_counter_track_uuuid(dispatch, dispatch->hp_ddomain2frqtracks, " Ferquency", hostname,
process_id, did, subDevice, 100, std::nullopt, nullptr, RxTx);
uintptr_t did, uint32_t subDevice, std::optional<FabricDetails> options) {
return get_counter_track_uuuid(dispatch, dispatch->hp_ddomain2frqtracks, " Ferquency", hostname, process_id, did, subDevice, 100);
}

static perfetto_uuid_t get_computeEU_track_uuuid(timeline_dispatch_t *dispatch, const std::string &hostname, uint64_t process_id,
uintptr_t did, std::optional<uintptr_t> hFabricPort, uint32_t subDevice, std::optional<bool> RxTx) {
return get_counter_track_uuuid(dispatch, dispatch->hp_dsdev2cpetracks, "ComputeEngine (%)", hostname,
process_id, did, subDevice, 100, std::nullopt, nullptr, RxTx);
uintptr_t did, uint32_t subDevice, std::optional<FabricDetails> options ) {
return get_counter_track_uuuid(dispatch, dispatch->hp_dsdev2cpetracks, "ComputeEngine (%)", hostname, process_id, did, subDevice, 100);
}

static void add_event_DTelemetry(timeline_dispatch_t *dispatch, const std::string &hostname, uint64_t process_id,
uint64_t thread_id, uintptr_t did, std::optional<uintptr_t> hFabricPort, uint32_t subDevice,
uint64_t timestamp, float value, uuid_getter_t uuid_getter, const std::string &eventName,
std::optional<bool> RxTx = std::nullopt) {
uint64_t thread_id, uintptr_t did, uint32_t subDevice,
uint64_t timestamp, float value, uuid_getter_t uuid_getter, const std::string &eventName,
std::optional<FabricDetails> options = std::nullopt) {
perfetto_uuid_t track_uuid;
if (hFabricPort.has_value()) {
track_uuid = uuid_getter(dispatch, hostname, process_id, did, hFabricPort, subDevice, RxTx);
} else {
track_uuid = uuid_getter(dispatch, hostname, process_id, did, std::nullopt, subDevice, RxTx);
}
if (options.has_value()) {
track_uuid = uuid_getter(dispatch, hostname, process_id, did, subDevice, options);
} else {
track_uuid = uuid_getter(dispatch, hostname, process_id, did, subDevice, std::nullopt);
}

auto *packet = dispatch->trace.add_packet();
packet->set_trusted_packet_sequence_id(TRUSTED_PACKED_SEQUENCE_ID);
packet->set_timestamp(timestamp);
auto *track_event = packet->mutable_track_event();
track_event->set_type(perfetto_pruned::TrackEvent::TYPE_COUNTER);
track_event->set_track_uuid(track_uuid);
track_event->set_double_counter_value(value);
auto *packet = dispatch->trace.add_packet();
packet->set_trusted_packet_sequence_id(TRUSTED_PACKED_SEQUENCE_ID);
packet->set_timestamp(timestamp);
auto *track_event = packet->mutable_track_event();
track_event->set_type(perfetto_pruned::TrackEvent::TYPE_COUNTER);
track_event->set_track_uuid(track_uuid);
track_event->set_double_counter_value(value);
}

static void add_event_frequency(timeline_dispatch_t *dispatch, std::string hostname,
uint64_t process_id, uint64_t thread_id, uintptr_t did,
uint32_t subDevice, uint64_t timestamp, float frequency) {
add_event_DTelemetry(dispatch, hostname, process_id, thread_id, did, std::nullopt, subDevice, timestamp,
add_event_DTelemetry(dispatch, hostname, process_id, thread_id, did, subDevice, timestamp,
frequency, get_frequency_track_uuuid, "Frequency");
}

static void add_event_fabricPort(timeline_dispatch_t *dispatch, std::string hostname,
static void add_event_fabricPort( timeline_dispatch_t *dispatch, std::string hostname,
uint64_t process_id, uint64_t thread_id, uintptr_t did, uintptr_t hFabricPort,
uint32_t subDevice, uint64_t timestamp, uint32_t fabricId, uint32_t remotePortId,
float rxThroughput, float txThroughput, float rxSpeed, float txSpeed) {

add_event_DTelemetry(dispatch, hostname, process_id, thread_id, did, hFabricPort, subDevice, timestamp,
rxThroughput, get_fpThroughput_track_uuuid, "Fabric ThroughputRX", std::make_optional(false));
add_event_DTelemetry(dispatch, hostname, process_id, thread_id, did, hFabricPort, subDevice, timestamp,
txThroughput, get_fpThroughput_track_uuuid, "Fabric ThroughputTX", std::make_optional(true));
// Define details for RX throughput.
FabricDetails details = {hFabricPort, false, fabricId, remotePortId};
add_event_DTelemetry(dispatch, hostname, process_id, thread_id, did, subDevice, timestamp,
rxThroughput, get_fpThroughput_track_uuuid, "Fabric ThroughputRX", details);

details.RxTx = true;
add_event_DTelemetry(dispatch, hostname, process_id, thread_id, did, subDevice, timestamp,
txThroughput, get_fpThroughput_track_uuuid, "Fabric ThroughputTX", details);
}

static void add_event_power(timeline_dispatch_t *dispatch, std::string hostname,
uint64_t process_id, uint64_t thread_id, uintptr_t did,
uint32_t subDevice, uint64_t timestamp, float power) {
add_event_DTelemetry(dispatch, hostname, process_id, thread_id, did, std::nullopt, subDevice, timestamp,
add_event_DTelemetry(dispatch, hostname, process_id, thread_id, did, subDevice, timestamp,
power, get_power_track_uuuid, "Power");
}

static void add_event_computeEU(timeline_dispatch_t *dispatch, std::string hostname,
uint64_t process_id, uint64_t thread_id, uintptr_t did,
uint32_t subDevice, uint64_t timestamp, float activeTime) {
add_event_DTelemetry(dispatch, hostname, process_id, thread_id, did, std::nullopt, subDevice, timestamp,
add_event_DTelemetry(dispatch, hostname, process_id, thread_id, did, subDevice, timestamp,
activeTime, get_computeEU_track_uuuid, "ComputeEngine");
}

static void add_event_copyEU(timeline_dispatch_t *dispatch, std::string hostname,
uint64_t process_id, uint64_t thread_id, uintptr_t did,
uint32_t subDevice, uint64_t timestamp, float activeTime) {
add_event_DTelemetry(dispatch, hostname, process_id, thread_id, did, std::nullopt, subDevice, timestamp,
add_event_DTelemetry(dispatch, hostname, process_id, thread_id, did, subDevice, timestamp,
activeTime, get_copyEU_track_uuuid, "CopyEngine");
}

Expand Down Expand Up @@ -457,10 +461,10 @@ static void copyEU_usr_callback(void *btx_handle, void *usr_data, const char *ho
}

static void fabricPort_usr_callback(void *btx_handle, void *usr_data, const char *hostname,
int64_t vpid, uint64_t vtid, int64_t ts, int64_t backend,
uint64_t did, uint64_t hFabricPort, uint32_t subDevice,
uint32_t fabricId, uint32_t remotePortId, float rxThroughput,
float txThroughput, float rxSpeed, float txSpeed) {
int64_t vpid, uint64_t vtid, int64_t ts, int64_t backend,
uint64_t did, uint64_t hFabricPort, uint32_t subDevice,
uint32_t fabricId, uint32_t remotePortId, float rxThroughput,
float txThroughput, float rxSpeed, float txSpeed) {
auto *dispatch = static_cast<timeline_dispatch_t *>(usr_data);
add_event_fabricPort(dispatch, hostname, vpid, vtid, did, hFabricPort, subDevice, ts, fabricId,
remotePortId, rxThroughput, txThroughput, rxSpeed, txSpeed);
Expand Down

0 comments on commit 7935e50

Please sign in to comment.