Files
rocm-systems/projects/rocprofiler-systems/source/lib/rocprof-sys/library/coverage.cpp
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

342 строки
11 KiB
C++
Исходник Обычный вид История

2022-04-25 17:00:52 -05:00
// MIT License
//
2025-01-15 13:06:12 -05:00
// Copyright (c) 2022-2025 Advanced Micro Devices, Inc. All Rights Reserved.
2022-04-25 17:00:52 -05:00
//
// 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 "library/coverage.hpp"
2022-08-31 01:24:31 -05:00
#include "api.hpp"
#include "core/config.hpp"
#include "library/coverage/impl.hpp"
2022-04-25 17:00:52 -05:00
#include "library/thread_data.hpp"
#include <timemory/backends/threading.hpp>
#include <timemory/tpls/cereal/cereal.hpp>
#include <timemory/utility/popen.hpp>
#include "logger/debug.hpp"
2022-04-25 17:00:52 -05:00
#include <algorithm>
#include <map>
#include <mutex>
#include <string>
#include <string_view>
#include <type_traits>
#include <unordered_map>
#define ROCPROFSYS_SERIALIZE(MEMBER_VARIABLE) \
2022-04-25 17:00:52 -05:00
ar(::tim::cereal::make_nvp(#MEMBER_VARIABLE, MEMBER_VARIABLE))
namespace rocprofsys
2022-04-25 17:00:52 -05:00
{
namespace coverage
{
namespace
{
template <typename... Tp>
using uomap_t = std::unordered_map<Tp...>;
//
using coverage_thread_data_type =
uomap_t<std::string_view, uomap_t<std::string_view, std::map<size_t, size_t>>>;
//
2022-05-08 01:40:56 -05:00
using coverage_data_vector = std::vector<coverage_data>;
2022-04-25 17:00:52 -05:00
//
2022-05-08 01:40:56 -05:00
using coverage_data_map =
uomap_t<std::string_view,
uomap_t<std::string_view, std::map<size_t, coverage_data_vector::iterator>>>;
2022-04-25 17:00:52 -05:00
//
using coverage_thread_data =
rocprofsys::thread_data<coverage_thread_data_type, code_coverage>;
2022-04-25 17:00:52 -05:00
//
auto&
get_code_coverage()
{
static auto _v = code_coverage{};
return _v;
}
//
auto&
2022-05-08 01:40:56 -05:00
get_post_processed()
{
static auto* _v = new bool{ false };
return *_v;
}
//
auto&
2022-04-25 17:00:52 -05:00
get_coverage_data()
{
2022-05-08 01:40:56 -05:00
static auto _v = coverage_data_vector{};
2022-04-25 17:00:52 -05:00
return _v;
}
//
auto&
get_coverage_count(int64_t _tid = tim::threading::get_id())
{
2023-10-16 18:04:47 -05:00
return coverage_thread_data::instance(construct_on_thread{ _tid });
2022-04-25 17:00:52 -05:00
}
} // namespace
//--------------------------------------------------------------------------------------//
void
post_process()
{
2022-05-08 01:40:56 -05:00
using data_tuple_t = coverage_data::data_tuple_t;
if(get_post_processed()) return;
get_post_processed() = true;
2022-04-25 17:00:52 -05:00
if(!config::get_use_code_coverage()) return;
2022-05-08 01:40:56 -05:00
auto& _coverage = get_code_coverage();
auto& _coverage_data = get_coverage_data();
2022-04-25 17:00:52 -05:00
if(_coverage.size == 0)
{
LOG_WARNING("Code coverage enabled but no code coverage data is available!");
2022-04-25 17:00:52 -05:00
return;
}
auto _data = coverage_thread_data_type{};
{
2022-05-08 01:40:56 -05:00
auto _coverage_map = coverage_data_map{};
auto _find = [&_coverage_data, &_coverage_map](data_tuple_t&& _v) {
auto& _cache = _coverage_map[std::get<0>(_v)][std::get<1>(_v)];
auto mitr = _cache.find(std::get<2>(_v));
if(mitr != _cache.end()) return std::make_pair(mitr->second, true);
for(auto itr = _coverage_data.begin(); itr != _coverage_data.end(); ++itr)
{
if(*itr == _v)
{
_cache[std::get<2>(_v)] = itr;
return std::make_pair(itr, true);
}
}
return std::make_pair(_coverage_data.end(), false);
};
for(size_t i = 0; i < coverage_thread_data::size(); ++i)
2022-04-25 17:00:52 -05:00
{
2022-05-08 01:40:56 -05:00
const auto& _thr_data = *get_coverage_count(i);
for(const auto& file : _thr_data)
2022-04-25 17:00:52 -05:00
{
2022-05-08 01:40:56 -05:00
for(const auto& func : file.second)
2022-04-25 17:00:52 -05:00
{
2022-05-08 01:40:56 -05:00
for(const auto& addr : func.second)
2022-04-25 17:00:52 -05:00
{
2022-05-08 01:40:56 -05:00
_data[file.first][func.first][addr.first] += addr.second;
auto&& _v = _find({ file.first, func.first, addr.first });
if(_v.second)
{
_v.first->count += addr.second;
}
else
{
LOG_WARNING("No matching coverage data for {} :: {} (0x{:X})",
func.first, file.first,
(unsigned int) addr.first);
2022-05-08 01:40:56 -05:00
}
2022-04-25 17:00:52 -05:00
}
}
}
}
}
for(const auto& file : _data)
{
for(const auto& func : file.second)
{
for(const auto& addr : func.second)
{
if(addr.second > 0)
{
_coverage.count += 1;
_coverage.covered.modules.emplace(file.first);
_coverage.covered.functions.emplace(func.first);
_coverage.covered.addresses.emplace(addr.first);
}
}
}
}
std::sort(_coverage_data.begin(), _coverage_data.end(),
std::greater<coverage_data>{});
{
2022-05-08 01:40:56 -05:00
auto _tmp_map = coverage_data_map{};
2022-04-25 17:00:52 -05:00
auto _tmp = std::decay_t<decltype(_coverage_data)>{};
2022-05-08 01:40:56 -05:00
auto _find_in_tmp = [&_tmp, &_tmp_map](const auto& _v) {
auto& _cache = _tmp_map[_v.module][_v.function];
auto mitr = _cache.find(_v.address);
if(mitr != _cache.end()) return std::make_pair(mitr->second, true);
for(auto titr = _tmp.begin(); titr != _tmp.end(); ++titr)
2022-04-25 17:00:52 -05:00
{
if(titr->source == _v.source && titr->address != _v.address &&
titr->count == _v.count)
2022-05-08 01:40:56 -05:00
{
_cache[_v.address] = titr;
return std::make_pair(titr, true);
2022-05-08 01:40:56 -05:00
}
2022-04-25 17:00:52 -05:00
}
return std::make_pair(_tmp.end(), false);
};
for(auto&& itr : _coverage_data)
{
if(!_find_in_tmp(itr).second) _tmp.emplace_back(itr);
}
std::swap(_coverage_data, _tmp);
}
LOG_INFO("code coverage :: {:.2f}%", _coverage() * 100.0);
LOG_INFO("module coverage :: {:.2f}%", _coverage(code_coverage::MODULE) * 100.0);
LOG_INFO("function coverage :: {:.2f}%", _coverage(code_coverage::FUNCTION) * 100.0);
2022-04-25 17:00:52 -05:00
std::sort(_coverage_data.begin(), _coverage_data.end(),
std::greater<coverage_data>{});
auto _get_setting = [](const std::string& _v) {
auto&& _b = config::get_setting_value<bool>(_v);
if(!_b && get_is_continuous_integration())
{
throw std::runtime_error(
fmt::format("Error! No configuration setting named '{}'", _v));
}
2023-03-23 01:13:12 -05:00
return _b.value_or(true);
2022-04-25 17:00:52 -05:00
};
auto _text_output = _get_setting("ROCPROFSYS_TEXT_OUTPUT");
auto _json_output = _get_setting("ROCPROFSYS_JSON_OUTPUT");
2022-04-25 17:00:52 -05:00
if(_text_output)
{
auto _fname = tim::settings::compose_output_filename("coverage", ".txt");
std::ofstream ofs{};
if(tim::filepath::open(ofs, _fname))
{
if(get_verbose() >= 0)
2022-08-31 01:24:31 -05:00
operation::file_output_message<code_coverage>{}(
_fname, std::string{ "coverage" });
2022-04-25 17:00:52 -05:00
for(auto& itr : _coverage_data)
{
// if(get_debug() && get_verbose() >= 2)
if(true)
{
auto _addr = TIMEMORY_JOIN("", "0x", std::hex, itr.address);
ofs << std::setw(8) << itr.count << " " << std::setw(8) << _addr
<< " " << itr.source << "\n";
}
else
{
ofs << std::setw(8) << itr.count << " " << itr.source << "\n";
}
}
}
else
{
throw std::runtime_error(
fmt::format("Error opening coverage output file: {}", _fname));
2022-04-25 17:00:52 -05:00
}
}
if(_json_output)
{
2022-05-08 01:40:56 -05:00
std::stringstream oss{};
{
namespace cereal = tim::cereal;
auto ar =
tim::policy::output_archive<cereal::PrettyJSONOutputArchive>::get(oss);
ar->setNextName("rocprofsys");
2022-05-08 01:40:56 -05:00
ar->startNode();
ar->setNextName("coverage");
ar->startNode();
(*ar)(cereal::make_nvp("summary", _coverage));
(*ar)(cereal::make_nvp("details", _coverage_data));
ar->finishNode();
ar->finishNode();
}
2022-04-25 17:00:52 -05:00
auto _fname = tim::settings::compose_output_filename("coverage", ".json");
std::ofstream ofs{};
if(tim::filepath::open(ofs, _fname))
{
if(get_verbose() >= 0)
2022-08-31 01:24:31 -05:00
operation::file_output_message<code_coverage>{}(
_fname, std::string{ "coverage" });
2022-05-08 01:40:56 -05:00
ofs << oss.str() << "\n";
2022-04-25 17:00:52 -05:00
}
else
{
throw std::runtime_error(
fmt::format("Error opening coverage output file: {}", _fname));
2022-04-25 17:00:52 -05:00
}
}
}
} // namespace coverage
} // namespace rocprofsys
2022-04-25 17:00:52 -05:00
//--------------------------------------------------------------------------------------//
namespace coverage = rocprofsys::coverage;
2022-04-25 17:00:52 -05:00
extern "C" void
rocprofsys_register_source_hidden(const char* file, const char* func, size_t line,
size_t address, const char* source)
2022-04-25 17:00:52 -05:00
{
2022-05-08 01:40:56 -05:00
if(coverage::get_post_processed()) return;
2022-04-25 17:00:52 -05:00
using coverage_data = coverage::coverage_data;
LOG_DEBUG("[0x{:X}] :: {:20s} :: {:20s}:{} :: {}", (unsigned int) address, func, file,
line, source);
2022-04-25 17:00:52 -05:00
coverage::get_coverage_data().emplace_back(
coverage_data{ size_t{ 0 }, address, line, file, func,
(source && strlen(source) > 0) ? source : func });
coverage::get_code_coverage().size += 1;
coverage::get_code_coverage().possible.modules.emplace(file);
coverage::get_code_coverage().possible.functions.emplace(func);
coverage::get_code_coverage().possible.addresses.emplace(address);
// initialize
for(size_t i = 0; i < coverage::coverage_thread_data::size(); ++i)
2022-05-08 01:40:56 -05:00
{
(*coverage::get_coverage_count(i))[file][func].emplace(address, 0);
}
2022-04-25 17:00:52 -05:00
}
//--------------------------------------------------------------------------------------//
extern "C" void
rocprofsys_register_coverage_hidden(const char* file, const char* func, size_t address)
2022-04-25 17:00:52 -05:00
{
2022-05-08 01:40:56 -05:00
if(coverage::get_post_processed()) return;
if(rocprofsys::get_state() < rocprofsys::State::Active &&
!rocprofsys_init_tooling_hidden())
2022-05-08 01:40:56 -05:00
return;
else if(rocprofsys::get_state() >= rocprofsys::State::Finalized)
2022-05-08 01:40:56 -05:00
return;
(*coverage::get_coverage_count())[file][func][address] += 1;
2022-04-25 17:00:52 -05:00
}
//--------------------------------------------------------------------------------------//