google / autofdo

AutoFDO
https://groups.google.com/forum/#!forum/autofdo
Apache License 2.0
523 stars 109 forks source link

create_llvm_prof can not found build id in propeller format #190

Closed zcfh closed 3 months ago

zcfh commented 6 months ago

create_llvm_prof can not found build id, but use format=text can work well

error log

I0000 00:00:1712458744.551049   26548 perfdata_reader.cc:276] Build Id found in 'test_base/test_clang11': 0400000008000000
I0000 00:00:1712458744.551070   26548 perfdata_reader.cc:496] No file with matching buildId in perf data, which contains the following <file, buildid>:
    test_base/test: e92f46d2108b8df2
    /usr/lib64/ld-2.17.so: 27ffd1fbc69569c776e666474eed723395e6d727
    [vdso]: 81f468f7acc6d1b49d312cc97b13e258d4576eab
    /usr/lib64/libstdc++.so.6.0.19: e2fb6e9c483d89e8e96d73c7ccf3e3a91e91bb81

I use readelf -n , will report same build id as perf.data.

$ readelf -n test_base/test

Displaying notes found at file offset 0x0000028c with length 0x00000020:
  Owner                 Data size   Description
  GNU                  0x00000010   NT_GNU_ABI_TAG (ABI version tag)
    OS: Linux, ABI: 2.6.32

Displaying notes found at file offset 0x000002ac with length 0x00000018:
  Owner                 Data size   Description
  GNU                  0x00000008   NT_GNU_BUILD_ID (unique build ID bitstring)
    Build ID: e92f46d2108b8df2

I try --profiled_binary_name will appear Segmentation fault.

WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1712458838.296938   30806 perfdata_reader.cc:225] 'test_base/test' is PIE: true
I0000 00:00:1712458838.297059   30806 perfdata_reader.cc:233] Build Id found in 'test_base/test': 0400000008000000
[INFO:/test_base/testautofdo/llvm_propeller_whole_program_info.cc:240] Parsing [1/1] test_base/test_perf.data ...[INFO:/test_base/testautofdo/llvm_propeller_whole_program_info.cc:708] Started reading the binary info from: test_base/test

[INFO:/test_base/testautofdo/llvm_propeller_whole_program_info.cc:716] Finished reading the binary info from: test_base/test
[WARNING:/test_base/testautofdo/third_party/perf_data_converter/src/quipper/perf_reader.cc:1067] Skipping unsupported event PERF_RECORD_CPU_MAP
[INFO:/test_base/testautofdo/third_party/perf_data_converter/src/quipper/perf_reader.cc:1058] Number of events stored: 163
[INFO:/test_base/testautofdo/third_party/perf_data_converter/src/quipper/perf_parser.cc:274] Parser processed: 7 MMAP/MMAP2 events, 2 COMM events, 0 FORK events, 1 EXIT events, 0 SAMPLE events, 0 of these were mapped, 0 SAMPLE events with a data address, 0 of these were mapped
[INFO:/test_base/testautofdo/third_party/perf_data_converter/src/quipper/perf_parser.cc:290] Input perf.data has no sample events due to PERF_RECORD_SAMPLE being skipped.
I0000 00:00:1712458838.317060   30806 perfdata_reader.cc:386] Found mmap: pid=12274
    [0x558785729000, 0x55878572e000] (pgoff=0, size=0x5000, fn='/test_base/testsvn/samples/pgo/pgo_bolt/test_base/test')

[WARNING:/test_base/testautofdo/third_party/perf_data_converter/src/quipper/perf_reader.cc:1067] Skipping unsupported event PERF_RECORD_CPU_MAP
[INFO:/test_base/testautofdo/third_party/perf_data_converter/src/quipper/perf_reader.cc:1058] Number of events stored: 161
Segmentation fault (core dumped)

#0  0x00000000004709ed in devtools_crosstool_autofdo::PropellerWholeProgramInfo::DropNonSelectedFunctions(absl::btree_set<int, std::less<int>, std::allocator<int> > const&) ()
(gdb) bt
#0  0x00000000004709ed in devtools_crosstool_autofdo::PropellerWholeProgramInfo::DropNonSelectedFunctions(absl::btree_set<int, std::less<int>, std::allocator<int> > const&) ()
#1  0x0000000000476c47 in devtools_crosstool_autofdo::PropellerWholeProgramInfo::SelectFunctions(devtools_crosstool_autofdo::CfgCreationMode, devtools_crosstool_autofdo::LBRAggregation const*) ()
#2  0x0000000000479953 in devtools_crosstool_autofdo::PropellerWholeProgramInfo::CreateCfgs(devtools_crosstool_autofdo::CfgCreationMode) ()
#3  0x000000000046a793 in devtools_crosstool_autofdo::PropellerProfWriter::Create(devtools_crosstool_autofdo::PropellerOptions const&, std::unique_ptr<devtools_crosstool_autofdo::PerfDataProvider, std::default_delete<devtools_crosstool_autofdo::PerfDataProvider> >, devtools_crosstool_autofdo::MultiStatusProvider*) ()
#4  0x000000000046ae21 in devtools_crosstool_autofdo::GeneratePropellerProfiles(devtools_crosstool_autofdo::PropellerOptions const&, std::unique_ptr<devtools_crosstool_autofdo::PerfDataProvider, std::default_delete<devtools_crosstool_autofdo::PerfDataProvider> >) ()
#5  0x000000000046b63f in devtools_crosstool_autofdo::GeneratePropellerProfiles(devtools_crosstool_autofdo::PropellerOptions const&) ()
#6  0x0000000000425bae in main ()

and I try debug mode create_llvm_prof, it core in assert

#0  0x00007fcfdb6b5387 in raise () from /lib64/libc.so.6
#1  0x00007fcfdb6b6a78 in abort () from /lib64/libc.so.6
#2  0x00007fcfdb6ae1a6 in __assert_fail_base () from /lib64/libc.so.6
#3  0x00007fcfdb6ae252 in __assert_fail () from /lib64/libc.so.6
#4  0x0000000000524cb6 in llvm::alignToPowerOf2 (Value=16, Align=0)
    at /autofdo/third_party/llvm-project/llvm/include/llvm/Support/MathExtras.h:383
#5  0x0000000000537459 in llvm::object::Elf_Note_Impl<llvm::object::ELFType<(llvm::endianness)1, true> >::getDesc (this=0x7ffc10673740, Align=0)
    at /autofdo/third_party/llvm-project/llvm/include/llvm/Object/ELFTypes.h:641
#6  0x000000000053331b in devtools_crosstool_autofdo::ELFFileUtil<llvm::object::ELFType<(llvm::endianness)1, true> >::GetBuildId[abi:cxx11]() (
    this=0xea4070) at /autofdo/perfdata_reader.cc:65
#7  0x0000000000521ad9 in devtools_crosstool_autofdo::PerfDataReader::SelectBinaryInfo (this=0x7ffc106743a0, binary_file_name=...,
    binary_info=0x7ffc106742c0) at /autofdo/perfdata_reader.cc:231
#8  0x00000000004edac5 in devtools_crosstool_autofdo::PropellerWholeProgramInfo::Create (options=..., perf_data_provider=...,
    frontend_status=0xea4120) at /autofdo/llvm_propeller_whole_program_info.cc:206
#9  0x00000000004dadb8 in devtools_crosstool_autofdo::PropellerProfWriter::Create (options=..., perf_data_provider=..., frontend_status=0xea4120)
    at /autofdo/llvm_propeller_profile_writer.cc:151
#10 0x00000000004da8f4 in devtools_crosstool_autofdo::GeneratePropellerProfiles (opts=..., perf_data_provider=...)
    at /autofdo/llvm_propeller_profile_writer.cc:121
#11 0x00000000004da56b in devtools_crosstool_autofdo::GeneratePropellerProfiles (opts=...)
    at /autofdo/llvm_propeller_profile_writer.cc:92
#12 0x0000000000440b09 in main (argc=10, argv=0x7ffc10675e28) at /autofdo/create_llvm_prof.cc:197
(gdb) f 4
#4  0x0000000000524cb6 in llvm::alignToPowerOf2 (Value=16, Align=0)
    at /autofdo/third_party/llvm-project/llvm/include/llvm/Support/MathExtras.h:383
383   assert(Align != 0 && (Align & (Align - 1)) == 0 &&
zcfh commented 6 months ago

I also got the same error when I ran LlvmPropellerProfileWriterTest. Is it a problem with my build environment or is it due to other reasons?

zcfh commented 6 months ago

According to the comment information, I adjusted

       if (r == kBuildIdNoteName) {
         // Or use shdr.sh_addralign instead of 0?
-        llvm::ArrayRef<uint8_t> build_id = note.getDesc(/*Align=*/0);
+        llvm::ArrayRef<uint8_t> build_id = note.getDesc(/*Align=*/shdr.sh_addralign);
         std::string build_id_str(build_id.size() * 2, '0');
         int k = 0;
template <class ELFT>
struct Elf_Nhdr_Impl {
  LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
  Elf_Word n_namesz;
  Elf_Word n_descsz;
...

If Align = 0, getDesc will use the offset of n_namesz, this seems to be a bug?

But a new error occurred.

#0 0x00007f9031e48387 in raise () from /lib64/libc.so.6
(gdb) bt
#0 0x00007f9031e48387 in raise () from /lib64/libc.so.6
#1 0x00007f9031e49a78 in abort () from /lib64/libc.so.6
#2 0x00007f9031e411a6 in __assert_fail_base () from /lib64/libc.so.6
#3 0x00007f9031e41252 in __assert_fail () from /lib64/libc.so.6
#4 0x000000000046db9c in llvm::object::BBAddrMap::getFunctionAddress (this=0x7f9028002a10)
     at autofdo/third_party/llvm-project/llvm/include/llvm/Object/ELFTypes.h:924
#5 0x00000000004f052a in devtools_crosstool_autofdo::PropellerWholeProgramInfo::DropNonSelectedFunctions (this=0x29f9270, selected_functions=...)
     at autofdo/llvm_propeller_whole_program_info.cc:584
#6 0x00000000004f1349 in devtools_crosstool_autofdo::PropellerWholeProgramInfo::SelectFunctions (this=0x29f9270,
     cfg_creation_mode=devtools_crosstool_autofdo::CfgCreationMode::kOnlyHotFunctions, lbr_aggregation=0x7fff41e6a808)
     at autofdo/llvm_propeller_whole_program_info.cc:689
#7 0x00000000004f1a57 in devtools_crosstool_autofdo::PropellerWholeProgramInfo::CreateCfgs (this=0x29f9270,
     cfg_creation_mode=devtools_crosstool_autofdo::CfgCreationMode::kOnlyHotFunctions)
     at autofdo/llvm_propeller_whole_program_info.cc:748
#8 0x00000000004dae4c in devtools_crosstool_autofdo::PropellerProfWriter::Create (options=..., perf_data_provider=..., frontend_status=0x29f1440)
     at autofdo/llvm_propeller_profile_writer.cc:158
#9 0x00000000004da8f4 in devtools_crosstool_autofdo::GeneratePropellerProfiles (opts=..., perf_data_provider=...)
     at autofdo/llvm_propeller_profile_writer.cc:121
#10 0x00000000004da56b in devtools_crosstool_autofdo::GeneratePropellerProfiles (opts=...)
     at autofdo/llvm_propeller_profile_writer.cc:92
#11 0x0000000000440b09 in main (argc=8, argv=0x7fff41e6c2f8) at autofdo/create_llvm_prof.cc:197
zcfh commented 6 months ago

llvm_propeller_whole_program_info.cc

void PropellerWholeProgramInfo::DropNonSelectedFunctions(
    const absl::btree_set<int> &selected_functions) {
  for (int i = 0; i != bb_addr_map_.size(); ++i) {
    if (selected_functions.contains(i))
      continue;
    bb_addr_map_[i].BBRanges.clear();
    bb_addr_map_[i].BBRanges.shrink_to_fit();
    if (!options_.keep_frontend_intermediate_data())
      symtab_.erase(bb_addr_map_[i].getFunctionAddress());
  }
}

In the new llvm api, getFunctionAddress core dumps due to bb_addr_map_[i].BBRanges.clear();. The address should be saved before call BBRanges.clear();

    uint64_t getFunctionAddress() const {
      assert(!BBRanges.empty());
      Return BBRanges.front().BaseAddress;
    }
shenhanc78 commented 5 months ago

Fixed buildid and getFunctionAddress problems (so we can drop the --profiled_binary_name flag) with https://github.com/google/autofdo/pull/192 . And as to the performance, we are running experiments to see whether this regress the origin results. Will report back shortly.