1 Star 1 Fork 0

Rong Tao/autofdo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
profile_reader.cc 4.03 KB
一键复制 编辑 原始数据 按行查看 历史
Eugene Rozenfeld 提交于 2021-09-03 16:45 . Fix dump_gcov
#include "profile_reader.h"
#include <cstdint>
#include "base/commandlineflags.h"
#include "base/logging.h"
#include "addr2line.h"
#include "gcov.h"
#include "symbol_map.h"
#include "third_party/abseil/absl/flags/flag.h"
namespace devtools_crosstool_autofdo {
void AutoFDOProfileReader::ReadModuleGroup() {
CHECK_EQ(gcov_read_unsigned(), GCOV_TAG_MODULE_GROUPING);
// Length of the section. Always 0.
gcov_read_unsigned();
// Number of modules. Always 0.
gcov_read_unsigned();
}
void AutoFDOProfileReader::ReadFunctionProfile() {
CHECK_EQ(gcov_read_unsigned(), GCOV_TAG_AFDO_FUNCTION);
gcov_read_unsigned();
uint32_t num_functions = gcov_read_unsigned();
SourceStack stack;
for (uint32_t i = 0; i < num_functions; i++) {
ReadSymbolProfile(stack, true);
}
}
void AutoFDOProfileReader::ReadSymbolProfile(const SourceStack &stack,
bool update) {
uint64_t head_count;
if (stack.size() == 0) {
head_count = gcov_read_counter();
} else {
head_count = 0;
}
const char *name = names_.at(gcov_read_unsigned()).c_str();
uint32_t num_pos_counts = gcov_read_unsigned();
uint32_t num_callsites = gcov_read_unsigned();
if (stack.size() == 0) {
symbol_map_->AddSymbol(name);
if (!force_update_ && symbol_map_->GetSymbolByName(name)->total_count > 0) {
update = false;
}
if (force_update_ || update) {
symbol_map_->AddSymbolEntryCount(name, head_count);
}
}
for (int i = 0; i < num_pos_counts; i++) {
uint32_t offset = gcov_read_unsigned();
uint32_t num_targets = gcov_read_unsigned();
uint64_t count = gcov_read_counter();
SourceInfo info(name, "", "", 0, offset >> 16, offset & 0xffff);
SourceStack new_stack;
new_stack.push_back(info);
new_stack.insert(new_stack.end(), stack.begin(), stack.end());
if (force_update_ || update) {
symbol_map_->AddSourceCount(new_stack[new_stack.size() - 1].func_name,
new_stack, count, 1);
}
for (int j = 0; j < num_targets; j++) {
// Only indirect call target histogram is supported now.
CHECK_EQ(gcov_read_unsigned(), HIST_TYPE_INDIR_CALL_TOPN);
const std::string &target_name = names_.at(gcov_read_counter());
uint64_t target_count = gcov_read_counter();
if (force_update_ || update) {
symbol_map_->AddIndirectCallTarget(
new_stack[new_stack.size() - 1].func_name,
new_stack, target_name, target_count);
}
}
}
for (int i = 0; i < num_callsites; i++) {
// offset is encoded as:
// higher 16 bits: line offset to the start of the function.
// lower 16 bits: discriminator.
uint32_t offset = gcov_read_unsigned();
SourceInfo info(name, "", "", 0, offset >> 16, offset & 0xffff);
SourceStack new_stack;
new_stack.push_back(info);
new_stack.insert(new_stack.end(), stack.begin(), stack.end());
ReadSymbolProfile(new_stack, update);
}
}
void AutoFDOProfileReader::ReadNameTable() {
CHECK_EQ(gcov_read_unsigned(), GCOV_TAG_AFDO_FILE_NAMES);
gcov_read_unsigned();
uint32_t name_vector_size = gcov_read_unsigned();
for (uint32_t i = 0; i < name_vector_size; i++) {
names_.push_back(gcov_read_string());
}
}
void AutoFDOProfileReader::ReadWorkingSet() {
CHECK_EQ(gcov_read_unsigned(), GCOV_TAG_AFDO_WORKING_SET);
gcov_read_unsigned();
for (uint32_t i = 0; i < NUM_GCOV_WORKING_SETS; i++) {
uint32_t num_counters = gcov_read_unsigned();
uint64_t min_counter = gcov_read_counter();
symbol_map_->UpdateWorkingSet(
i, num_counters * WORKING_SET_INSN_PER_BB, min_counter);
}
}
bool AutoFDOProfileReader::ReadFromFile(const std::string &output_file) {
CHECK_NE(gcov_open(output_file.c_str(), 1), -1);
// Read tags
CHECK_EQ(gcov_read_unsigned(), GCOV_DATA_MAGIC) << output_file;
absl::SetFlag(&FLAGS_gcov_version, gcov_read_unsigned());
gcov_read_unsigned();
ReadNameTable();
ReadFunctionProfile();
ReadModuleGroup();
ReadWorkingSet();
CHECK(!gcov_close());
return true;
}
} // namespace devtools_crosstool_autofdo
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/rtoax/autofdo.git
git@gitee.com:rtoax/autofdo.git
rtoax
autofdo
autofdo
master

搜索帮助