Files
rocm-systems/projects/rocprofiler-systems/source/lib/core/cpu.cpp
T
systems-assistant[bot] 1f86010ca2 ROCpd support [Part 2] (#109)
* Rocpd part 2, caching

* Fix shadowed variables

* backward compatibility

* Fixed designated initializers

* Fix timemory include

* Remove benchmark & Fix build issues for rhel

* Add missing bracket

* Fix shadowing and pedantic

* Fix pedantic pt2

* Fix duplicated SDK calls

* Add decay in get_size_impl

* Rename sample cache to trace cache

* Add cache storage supported types

* Resolving track naming in sampling module

* fix sampling of flushing thread

* fix sampling of flushing thread 2

* throw exception upon store while buffer storage is not running

* Prevent fork crashing

* Fix rebase issue

* Applied suggestions from code review

* Change flushing thread to use PTL

* Fix agent creation order

* Fix stream id ci throw

* Remove force setup of rocprofiler-sdk

* Code cleanup

* Change initialization for agent

* Add missing namespace

* Fix the mismatch within the tool_agent->device_id

* Switch from using handle to use agent type index

* Fix pmc info comparator in metadata registry

---------

Co-authored-by: Aleksandar <aleksandar.djordjevic@amd.com>
Co-authored-by: Milan Radosavljevic <milan.radosavljevic@amd.com>
Co-authored-by: Marjan Antic <marantic@amd.com>
2025-08-19 22:01:04 -04:00

192 rindas
5.6 KiB
C++

// MIT License
//
// Copyright (c) 2022-2025 Advanced Micro Devices, Inc. All Rights Reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "cpu.hpp"
#include "agent_manager.hpp"
#include <algorithm>
#include <cstdint>
#include <fstream>
#include <functional>
#include <unordered_map>
namespace rocprofsys
{
namespace cpu
{
std::vector<cpu_info>
process_cpu_info_data()
{
std::vector<cpu_info> cpu_data;
std::ifstream cpuinfo_file("/proc/cpuinfo");
if(!cpuinfo_file.is_open())
{
return cpu_data;
}
std::string line;
cpu_info current_cpu;
bool has_processor_entry = false;
auto parse_long = [](const std::string& value) -> long {
try
{
return std::stol(value);
} catch(const std::exception&)
{
return -1;
}
};
auto trim_whitespace = [](const std::string& str) -> std::string {
size_t start = str.find_first_not_of(" \t");
if(start == std::string::npos) return "";
size_t end = str.find_last_not_of(" \t");
return str.substr(start, end - start + 1);
};
static const std::unordered_map<std::string,
std::function<void(cpu_info&, const std::string&)>>
field_parsers = {
{ "processor",
[&parse_long](cpu_info& cpu, const std::string& val) {
cpu.processor = parse_long(val);
} },
{ "cpu family",
[&parse_long](cpu_info& cpu, const std::string& val) {
cpu.family = parse_long(val);
} },
{ "model",
[&parse_long](cpu_info& cpu, const std::string& val) {
cpu.model = parse_long(val);
} },
{ "physical id",
[&parse_long](cpu_info& cpu, const std::string& val) {
cpu.physical_id = parse_long(val);
} },
{ "core id",
[&parse_long](cpu_info& cpu, const std::string& val) {
cpu.core_id = parse_long(val);
} },
{ "apicid",
[&parse_long](cpu_info& cpu, const std::string& val) {
cpu.apicid = parse_long(val);
} },
{ "vendor_id",
[](cpu_info& cpu, const std::string& val) { cpu.vendor_id = val; } },
{ "model name",
[](cpu_info& cpu, const std::string& val) { cpu.model_name = val; } }
};
while(std::getline(cpuinfo_file, line))
{
if(line.empty())
{
if(has_processor_entry)
{
cpu_data.push_back(current_cpu);
return cpu_data; // Return immediately after first core
}
continue;
}
size_t colon_pos = line.find(':');
if(colon_pos == std::string::npos)
{
continue;
}
std::string key = trim_whitespace(line.substr(0, colon_pos));
std::string value = trim_whitespace(line.substr(colon_pos + 1));
std::transform(key.begin(), key.end(), key.begin(), ::tolower);
auto it = field_parsers.find(key);
if(it != field_parsers.end())
{
it->second(current_cpu, value);
if(key == "processor")
{
has_processor_entry = true;
}
}
}
if(has_processor_entry)
{
cpu_data.push_back(current_cpu);
}
return cpu_data;
}
std::vector<cpu_info>
get_cpu_info()
{
static auto _v = process_cpu_info_data();
return _v;
}
size_t
device_count()
{
auto cpu_data = get_cpu_info();
return cpu_data.size();
}
void
query_cpu_agents()
{
int32_t id_count = 0;
uint32_t node_count = 0;
uint32_t cpu_count = 0;
if(device_count() == 0)
{
return;
}
auto& _agent_manager = agent_manager::get_instance();
auto cpu_data = get_cpu_info();
for(auto& cpu : cpu_data)
{
auto node_id = node_count++;
auto logical_id = id_count++;
auto id = cpu_count++;
auto cur_agent = agent{ agent_type::CPU,
0,
id,
node_id,
logical_id,
static_cast<int32_t>(id),
cpu.model_name,
cpu.model_name,
cpu.vendor_id,
"" };
_agent_manager.insert_agent(cur_agent);
}
}
} // namespace cpu
} // namespace rocprofsys