rocprofiler-sdk/cxx/codeobj/segment.hpp Source File

rocprofiler-sdk/cxx/codeobj/segment.hpp Source File#

ROCprofiler-SDK developer API: rocprofiler-sdk/cxx/codeobj/segment.hpp Source File
ROCprofiler-SDK developer API 1.1.0
ROCm Profiling API and tools
segment.hpp
1// MIT License
2//
3// Copyright (c) 2024-2025 Advanced Micro Devices, Inc. All rights reserved.
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22
23#pragma once
24
25#include <algorithm>
26#include <iostream>
27#include <random>
28#include <set>
29#include <string>
30#include <unordered_set>
31#include <vector>
32
33namespace rocprofiler
34{
35namespace sdk
36{
37namespace codeobj
38{
39namespace segment
40{
41using marker_id_t = size_t;
42
43struct address_range_t
44{
45 uint64_t addr{0};
46 uint64_t size{0};
47 marker_id_t id{0};
48
49 bool operator==(const address_range_t& other) const
50 {
51 return (addr >= other.addr && addr < other.addr + other.size) ||
52 (other.addr >= addr && other.addr < addr + size);
53 }
54 bool operator<(const address_range_t& other) const
55 {
56 if(*this == other) return false;
57 return addr < other.addr;
58 }
59 bool inrange(uint64_t _addr) const { return addr <= _addr && addr + size > _addr; };
60};
61
62/**
63 * @brief Finds a candidate codeobj for the given vaddr
64 */
65class CodeobjTableTranslator : public std::set<address_range_t>
66{
67 using Super = std::set<address_range_t>;
68
69public:
70 address_range_t find_codeobj_in_range(uint64_t addr)
71 {
72 if(!cached_segment.inrange(addr))
73 {
74 auto it = this->find(address_range_t{addr, 0, 0});
75 if(it == this->end()) throw std::exception();
76 cached_segment = *it;
77 }
78 return cached_segment;
79 }
80
81 void clear_cache() { cached_segment = {}; }
82 bool remove(const address_range_t& range)
83 {
84 clear_cache();
85 return this->erase(range) != 0;
86 }
87 bool remove(uint64_t addr) { return remove(address_range_t{addr, 0, 0}); }
88
89private:
90 address_range_t cached_segment{};
91};
92
93} // namespace segment
94} // namespace codeobj
95} // namespace sdk
96} // namespace rocprofiler
Finds a candidate codeobj for the given vaddr.
Definition segment.hpp:66