Skip to content

Commit

Permalink
Removing cache of decoded lines and returning shared_ptr (#953)
Browse files Browse the repository at this point in the history
  • Loading branch information
ApoKalipse-V authored Jun 25, 2024
1 parent 8da0c35 commit a045947
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 42 deletions.
4 changes: 2 additions & 2 deletions samples/advanced_thread_trace/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ struct isa_map_elem_t
{
std::atomic<size_t> hitcount{0};
std::atomic<size_t> latency{0};
std::shared_ptr<Instruction> code_line{nullptr};
std::unique_ptr<Instruction> code_line{nullptr};
};

struct ToolData
Expand Down Expand Up @@ -332,7 +332,7 @@ isa_callback(char* isa_instruction,
C_API_BEGIN
assert(userdata && "ISA callback passed null!");

std::shared_ptr<Instruction> instruction;
std::unique_ptr<Instruction> instruction;

{
std::unique_lock<std::shared_mutex> unique_lock(tool->isa_map_mut);
Expand Down
9 changes: 5 additions & 4 deletions samples/code_object_isa_decode/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,11 @@ tool_codeobj_tracing_callback(rocprofiler_callback_tracing_record_t record,
<< std::dec << ". Printing first 64 bytes:" << std::endl;

std::unordered_set<std::string> references{};
int num_waitcnts = 0;
int num_scalar = 0;
int num_vector = 0;
int num_other = 0;

int num_waitcnts = 0;
int num_scalar = 0;
int num_vector = 0;
int num_other = 0;

size_t vaddr = begin_end.first;
while(vaddr < begin_end.second)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class CodeobjDecoderComponent
return {};
};

std::shared_ptr<Instruction> disassemble_instruction(uint64_t faddr, uint64_t vaddr)
std::unique_ptr<Instruction> disassemble_instruction(uint64_t faddr, uint64_t vaddr)
{
if(!disassembly) throw std::exception();

Expand All @@ -191,7 +191,7 @@ class CodeobjDecoderComponent
{}

auto pair = disassembly->ReadInstruction(faddr);
auto inst = std::make_shared<Instruction>(std::move(pair.first), pair.second);
auto inst = std::make_unique<Instruction>(std::move(pair.first), pair.second);
inst->faddr = faddr;
inst->vaddr = vaddr;

Expand All @@ -215,15 +215,15 @@ class LoadedCodeobjDecoder
: load_addr(_load_addr)
, load_end(_load_addr + _memsize)
{
if(!filepath) throw "Empty filepath.";
if(!filepath) throw std::runtime_error("Empty filepath.");

std::string_view fpath(filepath);

if(fpath.rfind(".out") + 4 == fpath.size())
{
std::ifstream file(filepath, std::ios::in | std::ios::binary);

if(!file.is_open()) throw "Invalid filename " + std::string(filepath);
if(!file.is_open()) throw std::runtime_error("Invalid file " + std::string(filepath));

std::vector<char> buffer;
file.seekg(0, file.end);
Expand All @@ -247,33 +247,19 @@ class LoadedCodeobjDecoder
decoder =
std::make_unique<CodeobjDecoderComponent>(reinterpret_cast<const char*>(data), size);
}
std::shared_ptr<Instruction> add_to_map(uint64_t ld_addr)
std::unique_ptr<Instruction> get(uint64_t ld_addr)
{
if(!decoder || ld_addr < load_addr) throw std::out_of_range("Addr not in decoder");
if(!decoder || ld_addr < load_addr) return nullptr;

uint64_t voffset = ld_addr - load_addr;
auto faddr = decoder->va2fo(voffset);
if(!faddr) throw std::out_of_range("Could not find file offset");
if(!faddr) return nullptr;

auto shared = decoder->disassemble_instruction(*faddr, voffset);
shared->ld_addr = ld_addr;
decoded_map[ld_addr] = shared;
return shared;
auto unique = decoder->disassemble_instruction(*faddr, voffset);
unique->ld_addr = ld_addr;
return unique;
}

std::shared_ptr<Instruction> get(uint64_t addr)
{
if(decoded_map.find(addr) != decoded_map.end()) return decoded_map[addr];
try
{
return add_to_map(addr);
} catch(std::exception& e)
{
std::cerr << e.what() << " at addr " << std::hex << addr << std::dec << std::endl;
}
throw std::out_of_range("Invalid address");
return nullptr;
}
uint64_t begin() const { return load_addr; };
uint64_t end() const { return load_end; }
uint64_t size() const { return load_end - load_addr; }
Expand All @@ -299,8 +285,7 @@ class LoadedCodeobjDecoder
private:
uint64_t load_end = 0;

std::unordered_map<uint64_t, std::shared_ptr<Instruction>> decoded_map;
std::unique_ptr<CodeobjDecoderComponent> decoder{nullptr};
std::unique_ptr<CodeobjDecoderComponent> decoder{nullptr};
};

/**
Expand Down Expand Up @@ -332,7 +317,7 @@ class CodeobjMap

virtual bool removeDecoderbyId(codeobj_marker_id_t id) { return decoders.erase(id) != 0; }

std::shared_ptr<Instruction> get(codeobj_marker_id_t id, uint64_t offset)
std::unique_ptr<Instruction> get(codeobj_marker_id_t id, uint64_t offset)
{
auto& decoder = decoders.at(id);
return decoder->get(decoder->begin() + offset);
Expand Down Expand Up @@ -387,13 +372,13 @@ class CodeobjAddressTranslate : public CodeobjMap
return table.remove(load_addr) && this->Super::removeDecoderbyId(id);
}

std::shared_ptr<Instruction> get(uint64_t vaddr)
std::unique_ptr<Instruction> get(uint64_t vaddr)
{
auto& addr_range = table.find_codeobj_in_range(vaddr);
return this->Super::get(addr_range.id, vaddr - addr_range.vbegin);
}

std::shared_ptr<Instruction> get(codeobj_marker_id_t id, uint64_t offset)
std::unique_ptr<Instruction> get(codeobj_marker_id_t id, uint64_t offset)
{
if(id == 0)
return get(offset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,18 @@ class CodeObjectBinary
if(!(size = std::stoul(size_it->second, nullptr, 0))) return;
}

if(protocol == "memory") throw protocol + " protocol not supported!";
if(protocol == "memory") throw std::runtime_error(protocol + " protocol not supported!");

std::ifstream file(decoded_path, std::ios::in | std::ios::binary);
if(!file || !file.is_open()) throw "could not open " + decoded_path;
if(!file || !file.is_open()) throw std::runtime_error("could not open " + decoded_path);

if(!size)
{
file.ignore(std::numeric_limits<std::streamsize>::max());
size_t bytes = file.gcount();
file.clear();

if(bytes < offset) throw "invalid uri " + decoded_path + " (file size < offset)";
if(bytes < offset) throw std::runtime_error("invalid uri " + decoded_path);

size = bytes - offset;
}
Expand Down
8 changes: 4 additions & 4 deletions tests/thread-trace/trace_callbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ get_trace_data(rocprofiler_att_parser_data_type_t type, void* att_data, void* us
auto ptr = std::make_unique<TrackedIsa>();
try
{
auto shared_inst = codeobjTranslate->get(pc.marker_id, pc.addr);
if(shared_inst == nullptr) return;
ptr->inst = shared_inst->inst;
auto unique_inst = codeobjTranslate->get(pc.marker_id, pc.addr);
if(unique_inst == nullptr) return;
ptr->inst = unique_inst->inst;
} catch(...)
{
return;
Expand Down Expand Up @@ -178,7 +178,7 @@ isa_callback(char* isa_instruction,
assert(trace_data.tool && "ISA callback passed null!");
ToolData& tool = *reinterpret_cast<ToolData*>(trace_data.tool);

std::shared_ptr<Instruction> instruction;
std::unique_ptr<Instruction> instruction;

try
{
Expand Down

0 comments on commit a045947

Please sign in to comment.