Skip to content

Commit

Permalink
Make copy of exported data so it isn't corrupted (#488)
Browse files Browse the repository at this point in the history
  • Loading branch information
tgerdesnv authored Mar 6, 2024
1 parent fd6f2af commit 906f54f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/c++/perf_analyzer/infer_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ InferContext::GetOutput(const cb::InferResult& infer_result)
const uint8_t* buf{nullptr};
size_t byte_size{0};
infer_result.RawData(requested_output->Name(), &buf, &byte_size);
output[requested_output->Name()] = {buf, byte_size};
output.emplace(requested_output->Name(), ResponseData(buf, byte_size));
}
return output;
}
Expand Down
4 changes: 2 additions & 2 deletions src/c++/perf_analyzer/profile_data_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ ProfileDataExporter::AddResponseOutputs(
rapidjson::Value response_output_json(rapidjson::kObjectType);
for (const auto& output : response_output) {
const auto& name{output.first};
const auto& buf{output.second.first};
const auto& byte_size{output.second.second};
const auto& buf{output.second.data_.get()};
const auto& byte_size{output.second.size_};
rapidjson::Value name_json(name.c_str(), document_.GetAllocator());
rapidjson::Value output_json{};
if (buf != nullptr) {
Expand Down
30 changes: 25 additions & 5 deletions src/c++/perf_analyzer/request_record.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,40 @@

namespace triton { namespace perfanalyzer {

/// A record containing the data of a single response
struct ResponseData {
ResponseData(const uint8_t* buf, size_t size)
{
uint8_t* array = new uint8_t[size];
std::memcpy(array, buf, size);
data_ = std::shared_ptr<uint8_t>(array, [](uint8_t* p) { delete[] p; });
size_ = size;
}

// Define equality comparison operator so it can be inserted into maps
bool operator==(const ResponseData& other) const
{
if (size_ != other.size_)
return false;
// Compare the contents of the arrays
return std::memcmp(data_.get(), other.data_.get(), size_) == 0;
}

std::shared_ptr<uint8_t> data_;
size_t size_;
};


/// A record of an individual request
struct RequestRecord {
using ResponseOutput =
std::unordered_map<std::string, std::pair<const uint8_t*, size_t>>;
using ResponseOutput = std::unordered_map<std::string, ResponseData>;

RequestRecord(
std::chrono::time_point<std::chrono::system_clock> start_time =
std::chrono::time_point<std::chrono::system_clock>(),
std::vector<std::chrono::time_point<std::chrono::system_clock>>
response_timestamps = {},
std::vector<
std::unordered_map<std::string, std::pair<const uint8_t*, size_t>>>
response_outputs = {},
std::vector<ResponseOutput> response_outputs = {},
bool sequence_end = true, bool delayed = false, uint64_t sequence_id = 0,
bool has_null_last_response = false)
: start_time_(start_time), response_timestamps_(response_timestamps),
Expand Down
13 changes: 9 additions & 4 deletions src/c++/perf_analyzer/test_profile_data_collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,13 @@ TEST_CASE("profile_data_collector: AddData")
auto request1_timestamp{clock_epoch + std::chrono::nanoseconds(1)};
auto request1_response1_timestamp{clock_epoch + std::chrono::nanoseconds(2)};
auto request1_response2_timestamp{clock_epoch + std::chrono::nanoseconds(3)};
uint8_t fake_data[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
RequestRecord::ResponseOutput request1_response1_output{
{"key1", {nullptr, 1}}, {"key2", {nullptr, 2}}};
{"key1", ResponseData(fake_data, 1)},
{"key2", ResponseData(fake_data, 2)}};
RequestRecord::ResponseOutput request1_response2_output{
{"key3", {nullptr, 3}}, {"key4", {nullptr, 4}}};
{"key3", ResponseData(fake_data, 3)},
{"key4", ResponseData(fake_data, 4)}};

RequestRecord request_record1{
request1_timestamp,
Expand All @@ -83,9 +86,11 @@ TEST_CASE("profile_data_collector: AddData")
auto request2_response1_timestamp{clock_epoch + std::chrono::nanoseconds(5)};
auto request2_response2_timestamp{clock_epoch + std::chrono::nanoseconds(6)};
RequestRecord::ResponseOutput request2_response1_output{
{"key5", {nullptr, 5}}, {"key6", {nullptr, 6}}};
{"key5", ResponseData(fake_data, 5)},
{"key6", ResponseData(fake_data, 6)}};
RequestRecord::ResponseOutput request2_response2_output{
{"key7", {nullptr, 7}}, {"key8", {nullptr, 8}}};
{"key7", ResponseData(fake_data, 7)},
{"key8", ResponseData(fake_data, 8)}};

RequestRecord request_record2{
request2_timestamp,
Expand Down

0 comments on commit 906f54f

Please sign in to comment.