TEST_F(SampleProfTest, sample_overflow_saturation) { const uint64_t Max = std::numeric_limits<uint64_t>::max(); sampleprof_error Result; StringRef FooName("_Z3fooi"); FunctionSamples FooSamples; Result = FooSamples.addTotalSamples(1); ASSERT_EQ(Result, sampleprof_error::success); Result = FooSamples.addHeadSamples(1); ASSERT_EQ(Result, sampleprof_error::success); Result = FooSamples.addBodySamples(10, 0, 1); ASSERT_EQ(Result, sampleprof_error::success); Result = FooSamples.addTotalSamples(Max); ASSERT_EQ(Result, sampleprof_error::counter_overflow); ASSERT_EQ(FooSamples.getTotalSamples(), Max); Result = FooSamples.addHeadSamples(Max); ASSERT_EQ(Result, sampleprof_error::counter_overflow); ASSERT_EQ(FooSamples.getHeadSamples(), Max); Result = FooSamples.addBodySamples(10, 0, Max); ASSERT_EQ(Result, sampleprof_error::counter_overflow); ErrorOr<uint64_t> BodySamples = FooSamples.findSamplesAt(10, 0); ASSERT_FALSE(BodySamples.getError()); ASSERT_EQ(BodySamples.get(), Max); }
/// Write samples to a text file. /// /// Note: it may be tempting to implement this in terms of /// FunctionSamples::print(). Please don't. The dump functionality is intended /// for debugging and has no specified form. /// /// The format used here is more structured and deliberate because /// it needs to be parsed by the SampleProfileReaderText class. std::error_code SampleProfileWriterText::write(const FunctionSamples &S) { auto &OS = *OutputStream; OS << S.getName() << ":" << S.getTotalSamples(); if (Indent == 0) OS << ":" << S.getHeadSamples(); OS << "\n"; SampleSorter<LineLocation, SampleRecord> SortedSamples(S.getBodySamples()); for (const auto &I : SortedSamples.get()) { LineLocation Loc = I->first; const SampleRecord &Sample = I->second; OS.indent(Indent + 1); if (Loc.Discriminator == 0) OS << Loc.LineOffset << ": "; else OS << Loc.LineOffset << "." << Loc.Discriminator << ": "; OS << Sample.getSamples(); for (const auto &J : Sample.getCallTargets()) OS << " " << J.first() << ":" << J.second; OS << "\n"; } SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples( S.getCallsiteSamples()); Indent += 1; for (const auto &I : SortedCallsiteSamples.get()) for (const auto &FS : I->second) { LineLocation Loc = I->first; const FunctionSamples &CalleeSamples = FS.second; OS.indent(Indent); if (Loc.Discriminator == 0) OS << Loc.LineOffset << ": "; else OS << Loc.LineOffset << "." << Loc.Discriminator << ": "; if (std::error_code EC = write(CalleeSamples)) return EC; } Indent -= 1; return sampleprof_error::success; }
std::error_code SampleProfileWriterBinary::writeBody(const FunctionSamples &S) { auto &OS = *OutputStream; if (std::error_code EC = writeNameIdx(S.getName())) return EC; encodeULEB128(S.getTotalSamples(), OS); // Emit all the body samples. encodeULEB128(S.getBodySamples().size(), OS); for (const auto &I : S.getBodySamples()) { LineLocation Loc = I.first; const SampleRecord &Sample = I.second; encodeULEB128(Loc.LineOffset, OS); encodeULEB128(Loc.Discriminator, OS); encodeULEB128(Sample.getSamples(), OS); encodeULEB128(Sample.getCallTargets().size(), OS); for (const auto &J : Sample.getCallTargets()) { StringRef Callee = J.first(); uint64_t CalleeSamples = J.second; if (std::error_code EC = writeNameIdx(Callee)) return EC; encodeULEB128(CalleeSamples, OS); } } // Recursively emit all the callsite samples. uint64_t NumCallsites = 0; for (const auto &J : S.getCallsiteSamples()) NumCallsites += J.second.size(); encodeULEB128(NumCallsites, OS); for (const auto &J : S.getCallsiteSamples()) for (const auto &FS : J.second) { LineLocation Loc = J.first; const FunctionSamples &CalleeSamples = FS.second; encodeULEB128(Loc.LineOffset, OS); encodeULEB128(Loc.Discriminator, OS); if (std::error_code EC = writeBody(CalleeSamples)) return EC; } return sampleprof_error::success; }
/// \brief Write samples to a binary file. /// /// \returns true if the samples were written successfully, false otherwise. bool SampleProfileWriterBinary::write(StringRef FName, const FunctionSamples &S) { OS << FName; encodeULEB128(0, OS); encodeULEB128(S.getTotalSamples(), OS); encodeULEB128(S.getHeadSamples(), OS); encodeULEB128(S.getBodySamples().size(), OS); // Emit all the body samples. for (const auto &I : S.getBodySamples()) { LineLocation Loc = I.first; const SampleRecord &Sample = I.second; encodeULEB128(Loc.LineOffset, OS); encodeULEB128(Loc.Discriminator, OS); encodeULEB128(Sample.getSamples(), OS); encodeULEB128(Sample.getCallTargets().size(), OS); for (const auto &J : Sample.getCallTargets()) { std::string Callee = J.first(); unsigned CalleeSamples = J.second; OS << Callee; encodeULEB128(0, OS); encodeULEB128(CalleeSamples, OS); } } // Recursively emit all the callsite samples. encodeULEB128(S.getCallsiteSamples().size(), OS); for (const auto &J : S.getCallsiteSamples()) { CallsiteLocation Loc = J.first; const FunctionSamples &CalleeSamples = J.second; encodeULEB128(Loc.LineOffset, OS); encodeULEB128(Loc.Discriminator, OS); write(Loc.CalleeName, CalleeSamples); } return true; }
/// \brief Write samples to a text file. bool SampleProfileWriterText::write(StringRef FName, const FunctionSamples &S) { OS << FName << ":" << S.getTotalSamples(); if (Indent == 0) OS << ":" << S.getHeadSamples(); OS << "\n"; for (const auto &I : S.getBodySamples()) { LineLocation Loc = I.first; const SampleRecord &Sample = I.second; OS.indent(Indent + 1); if (Loc.Discriminator == 0) OS << Loc.LineOffset << ": "; else OS << Loc.LineOffset << "." << Loc.Discriminator << ": "; OS << Sample.getSamples(); for (const auto &J : Sample.getCallTargets()) OS << " " << J.first() << ":" << J.second; OS << "\n"; } Indent += 1; for (const auto &I : S.getCallsiteSamples()) { CallsiteLocation Loc = I.first; const FunctionSamples &CalleeSamples = I.second; OS.indent(Indent); if (Loc.Discriminator == 0) OS << Loc.LineOffset << ": "; else OS << Loc.LineOffset << "." << Loc.Discriminator << ": "; write(Loc.CalleeName, CalleeSamples); } Indent -= 1; return true; }
std::error_code SampleProfileReaderGCC::readOneFunctionProfile( const InlineCallStack &InlineStack, bool Update, uint32_t Offset) { uint64_t HeadCount = 0; if (InlineStack.size() == 0) if (!GcovBuffer.readInt64(HeadCount)) return sampleprof_error::truncated; uint32_t NameIdx; if (!GcovBuffer.readInt(NameIdx)) return sampleprof_error::truncated; StringRef Name(Names[NameIdx]); uint32_t NumPosCounts; if (!GcovBuffer.readInt(NumPosCounts)) return sampleprof_error::truncated; uint32_t NumCallsites; if (!GcovBuffer.readInt(NumCallsites)) return sampleprof_error::truncated; FunctionSamples *FProfile = nullptr; if (InlineStack.size() == 0) { // If this is a top function that we have already processed, do not // update its profile again. This happens in the presence of // function aliases. Since these aliases share the same function // body, there will be identical replicated profiles for the // original function. In this case, we simply not bother updating // the profile of the original function. FProfile = &Profiles[Name]; FProfile->addHeadSamples(HeadCount); if (FProfile->getTotalSamples() > 0) Update = false; } else { // Otherwise, we are reading an inlined instance. The top of the // inline stack contains the profile of the caller. Insert this // callee in the caller's CallsiteMap. FunctionSamples *CallerProfile = InlineStack.front(); uint32_t LineOffset = Offset >> 16; uint32_t Discriminator = Offset & 0xffff; FProfile = &CallerProfile->functionSamplesAt( LineLocation(LineOffset, Discriminator))[Name]; } FProfile->setName(Name); for (uint32_t I = 0; I < NumPosCounts; ++I) { uint32_t Offset; if (!GcovBuffer.readInt(Offset)) return sampleprof_error::truncated; uint32_t NumTargets; if (!GcovBuffer.readInt(NumTargets)) return sampleprof_error::truncated; uint64_t Count; if (!GcovBuffer.readInt64(Count)) return sampleprof_error::truncated; // The line location is encoded in the offset as: // high 16 bits: line offset to the start of the function. // low 16 bits: discriminator. uint32_t LineOffset = Offset >> 16; uint32_t Discriminator = Offset & 0xffff; InlineCallStack NewStack; NewStack.push_back(FProfile); NewStack.insert(NewStack.end(), InlineStack.begin(), InlineStack.end()); if (Update) { // Walk up the inline stack, adding the samples on this line to // the total sample count of the callers in the chain. for (auto CallerProfile : NewStack) CallerProfile->addTotalSamples(Count); // Update the body samples for the current profile. FProfile->addBodySamples(LineOffset, Discriminator, Count); } // Process the list of functions called at an indirect call site. // These are all the targets that a function pointer (or virtual // function) resolved at runtime. for (uint32_t J = 0; J < NumTargets; J++) { uint32_t HistVal; if (!GcovBuffer.readInt(HistVal)) return sampleprof_error::truncated; if (HistVal != HIST_TYPE_INDIR_CALL_TOPN) return sampleprof_error::malformed; uint64_t TargetIdx; if (!GcovBuffer.readInt64(TargetIdx)) return sampleprof_error::truncated; StringRef TargetName(Names[TargetIdx]); uint64_t TargetCount; if (!GcovBuffer.readInt64(TargetCount)) return sampleprof_error::truncated; if (Update) FProfile->addCalledTargetSamples(LineOffset, Discriminator, TargetName, TargetCount); } } // Process all the inlined callers into the current function. These // are all the callsites that were inlined into this function. for (uint32_t I = 0; I < NumCallsites; I++) { // The offset is encoded as: // high 16 bits: line offset to the start of the function. // low 16 bits: discriminator. uint32_t Offset; if (!GcovBuffer.readInt(Offset)) return sampleprof_error::truncated; InlineCallStack NewStack; NewStack.push_back(FProfile); NewStack.insert(NewStack.end(), InlineStack.begin(), InlineStack.end()); if (std::error_code EC = readOneFunctionProfile(NewStack, Update, Offset)) return EC; } return sampleprof_error::success; }