Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions profile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ void Profile::AggregatePerFunctionProfile(bool check_lbr_entry) {
}
}

const AddressTimestampMap *address_timestamp_map = &sample_reader_->address_timestamp_map ();
for (const auto &[addr, timestamp] : *address_timestamp_map) {
uint64_t vaddr = symbol_map_->get_static_vaddr(addr);
ProfileMaps *maps = GetProfileMaps(vaddr);
if (maps != nullptr && maps->timestamp == 0) {
maps->timestamp = timestamp;
}
}

#if defined(HAVE_LLVM)
std::unique_ptr<MiniDisassembler> Disassembler;
if (check_lbr_entry) {
Expand Down Expand Up @@ -149,6 +158,8 @@ void Profile::ProcessPerFunctionProfile(absl::string_view func_name,
maps.end_addr);
// LOG(INFO) << "Built instruction map for func: " << func_name;

symbol_map_->AddSymbolTimestamp (func_name, maps.timestamp);

AddressCountMap map;
const AddressCountMap *map_ptr;
if (absl::GetFlag(FLAGS_use_lbr)) {
Expand Down
3 changes: 2 additions & 1 deletion profile.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ class Profile {
// Internal data structure that aggregates profile for each symbol.
struct ProfileMaps {
ProfileMaps(uint64_t start, uint64_t end)
: start_addr(start), end_addr(end) {}
: start_addr(start), end_addr(end), timestamp (0) {}
uint64_t GetAggregatedCount() const;
uint64_t start_addr;
uint64_t end_addr;
uint64_t timestamp;
AddressCountMap address_count_map;
RangeCountMap range_count_map;
BranchCountMap branch_count_map;
Expand Down
6 changes: 6 additions & 0 deletions profile_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ void AutoFDOProfileReader::ReadFunctionProfile() {
void AutoFDOProfileReader::ReadSymbolProfile(const SourceStack &stack,
bool update) {
uint64_t head_count;
uint64_t timestamp = 0;

if (stack.size() == 0) {
head_count = gcov_read_counter();
if (absl::GetFlag(FLAGS_gcov_version) >= 3) {
timestamp = gcov_read_counter();
}
} else {
head_count = 0;
}
Expand All @@ -43,6 +48,7 @@ void AutoFDOProfileReader::ReadSymbolProfile(const SourceStack &stack,
uint32_t num_callsites = gcov_read_unsigned();
if (stack.size() == 0) {
symbol_map_->AddSymbol(name);
symbol_map_->AddSymbolTimestamp(name, timestamp);
if (!force_update_ && symbol_map_->GetSymbolByName(name)->total_count > 0) {
update = false;
}
Expand Down
4 changes: 4 additions & 0 deletions profile_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ class SourceProfileWriter: public SymbolTraverser {

virtual void VisitTopSymbol(const std::string &name, const Symbol *node) {
gcov_write_counter(node->head_count);
if (absl::GetFlag(FLAGS_gcov_version) >= 3) {
gcov_write_counter(node->timestamp);
}
gcov_write_unsigned(GetStringIndex(Symbol::Name(name.c_str())));
}

Expand Down Expand Up @@ -326,6 +329,7 @@ class ProfileDumper : public SymbolTraverser {
node->Dump(0);
absl::PrintF("node->head_count: %u\n", node->head_count);
printf("GetStringIndex(%s): %u\n", name.c_str(), GetStringIndex(name));
absl::PrintF("node->timestamp: %lu\n", (unsigned long) node->timestamp);
printf("\n");
}

Expand Down
3 changes: 3 additions & 0 deletions sample_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ bool PerfDataSampleReader::Append(const std::string &profile_file) {
}
if (MatchBinary(event.dso_and_offset)) {
address_count_map_[event.dso_and_offset.offset()]++;
uint64_t address = event.dso_and_offset.offset();
uint64_t timestamp = event.event_ptr->timestamp();
address_timestamp_map_.insert({address, timestamp});
}
int start_index = 0;
while (start_index < event.branch_stack.size() &&
Expand Down
6 changes: 6 additions & 0 deletions sample_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ typedef std::pair<uint64_t, uint64_t> Range;
typedef std::map<Range, uint64_t> RangeCountMap;
typedef std::pair<uint64_t, uint64_t> Branch;
typedef std::map<Branch, uint64_t> BranchCountMap;
typedef std::map<uint64_t, uint64_t> AddressTimestampMap;

// Reads in the profile data, and represent it in address_count_map_.
class SampleReader {
Expand All @@ -52,6 +53,10 @@ class SampleReader {
return branch_count_map_;
}

const AddressTimestampMap &address_timestamp_map() const {
return address_timestamp_map_;
}

std::set<uint64_t> GetSampledAddresses() const;

// Returns the sample count for a given instruction.
Expand All @@ -77,6 +82,7 @@ class SampleReader {
AddressCountMap address_count_map_;
RangeCountMap range_count_map_;
BranchCountMap branch_count_map_;
AddressTimestampMap address_timestamp_map_;

bool is_kernel_ = false;
};
Expand Down
9 changes: 7 additions & 2 deletions symbol_map.cc
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,11 @@ void SymbolMap::AddSymbolEntryCount(absl::string_view symbol_name,
symbol->total_count += total_count;
}

void SymbolMap::AddSymbolTimestamp(absl::string_view symbol_name, uint64_t timestamp) {
Symbol *symbol = map_.find(symbol_name)->second;
symbol->timestamp = timestamp;
}

Symbol *SymbolMap::TraverseInlineStack(absl::string_view symbol_name,
const SourceStack &src, uint64_t count,
DataSource data_source) {
Expand Down Expand Up @@ -655,8 +660,8 @@ void Symbol::DumpBody(int indent, bool for_analysis) const {
void Symbol::Dump(int indent) const {
const std::string printed_name = getPrintName(info.func_name);
if (indent == 0) {
absl::PrintF("%s total:%u head:%u\n", printed_name, total_count,
head_count);
absl::PrintF("%s total:%u head:%u timestamp: %lu\n", printed_name, total_count,
head_count, (unsigned long) timestamp);
} else {
absl::PrintF("%s total:%u\n", printed_name, total_count);
}
Expand Down
7 changes: 6 additions & 1 deletion symbol_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ class Symbol {
total_count_incl(0),
head_count(0),
callsites(0),
pos_counts() {
pos_counts(),
timestamp(0) {
}

// This constructor is used to create aliased symbol.
Expand Down Expand Up @@ -242,6 +243,8 @@ class Symbol {
CallsiteMap callsites;
// Map from source location to count and instruction number.
PositionCountMap pos_counts;
// timestamp of first executed instruction
uint64_t timestamp;
};

// Vector of unique pointers to symbols.
Expand Down Expand Up @@ -420,6 +423,8 @@ class SymbolMap {
void AddSymbolEntryCount(absl::string_view symbol, uint64_t head_count,
uint64_t total_count = 0);

void AddSymbolTimestamp(absl::string_view symbol, uint64_t timestamp);

// DataSource represents what kind of data is used to generate afdo profile.
// PERFDATA: convert perf.data to afdo profile.
// AFDOPROTO: convert afdo proto generated by GWP autofdo pipeline to
Expand Down