[memprof] Print out the summary in YAML format.
authorSnehasish Kumar <snehasishk@google.com>
Fri, 7 Jan 2022 00:14:41 +0000 (16:14 -0800)
committerSnehasish Kumar <snehasishk@google.com>
Thu, 3 Feb 2022 22:33:50 +0000 (14:33 -0800)
Print out the profile summary in YAML format to make it easier to for
tools and tests to read in the contents of the raw profile.

Differential Revision: https://reviews.llvm.org/D116783

llvm/include/llvm/ProfileData/RawMemProfReader.h
llvm/lib/ProfileData/RawMemProfReader.cpp
llvm/test/tools/llvm-profdata/memprof-basic.test
llvm/test/tools/llvm-profdata/memprof-multi.test
llvm/tools/llvm-profdata/llvm-profdata.cpp

index 4554492..5041f54 100644 (file)
@@ -22,8 +22,8 @@ class RawMemProfReader {
 public:
   RawMemProfReader(std::unique_ptr<MemoryBuffer> DataBuffer)
       : DataBuffer(std::move(DataBuffer)) {}
-  // Prints aggregate counts for each raw profile parsed from the DataBuffer.
-  void printSummaries(raw_ostream &OS) const;
+  // Prints the contents of the profile in YAML format.
+  void printYAML(raw_ostream &OS);
 
   // Return true if the \p DataBuffer starts with magic bytes indicating it is
   // a raw binary memprof profile.
@@ -34,6 +34,10 @@ public:
   static Expected<std::unique_ptr<RawMemProfReader>> create(const Twine &Path);
 
 private:
+  // Prints aggregate counts for each raw profile parsed from the DataBuffer in
+  // YAML format.
+  void printSummaries(raw_ostream &OS) const;
+
   std::unique_ptr<MemoryBuffer> DataBuffer;
 };
 
index f8d13c7..f6c59cd 100644 (file)
@@ -98,17 +98,22 @@ bool RawMemProfReader::hasFormat(const MemoryBuffer &Buffer) {
   return Magic == MEMPROF_RAW_MAGIC_64;
 }
 
+void RawMemProfReader::printYAML(raw_ostream &OS) {
+  OS << "MemprofProfile:\n";
+  printSummaries(OS);
+}
+
 void RawMemProfReader::printSummaries(raw_ostream &OS) const {
-  int Count = 0;
   const char *Next = DataBuffer->getBufferStart();
   while (Next < DataBuffer->getBufferEnd()) {
     auto Summary = computeSummary(Next);
-    OS << "MemProf Profile " << ++Count << "\n";
-    OS << "  Version: " << Summary.Version << "\n";
-    OS << "  TotalSizeBytes: " << Summary.TotalSizeBytes << "\n";
-    OS << "  NumSegments: " << Summary.NumSegments << "\n";
-    OS << "  NumMIBInfo: " << Summary.NumMIBInfo << "\n";
-    OS << "  NumStackOffsets: " << Summary.NumStackOffsets << "\n";
+    OS << "  -\n";
+    OS << "  Header:\n";
+    OS << "    Version: " << Summary.Version << "\n";
+    OS << "    TotalSizeBytes: " << Summary.TotalSizeBytes << "\n";
+    OS << "    NumSegments: " << Summary.NumSegments << "\n";
+    OS << "    NumMibInfo: " << Summary.NumMIBInfo << "\n";
+    OS << "    NumStackOffsets: " << Summary.NumStackOffsets << "\n";
     // TODO: Print the build ids once we can record them using the
     // sanitizer_procmaps library for linux.
 
index 321119b..8e4adaa 100644 (file)
@@ -34,9 +34,11 @@ RUN: llvm-profdata show --memory %p/Inputs/basic.memprofraw -o - | FileCheck %s
 We expect 3 MIB entries, 1 each for the malloc calls in the program and one
 additional entry from a realloc in glibc/libio/vasprintf.c.
 
-CHECK: MemProf Profile 1
-CHECK:   Version: 1
-CHECK:   TotalSizeBytes: 1016
-CHECK:   NumSegments: 9
-CHECK:   NumMIBInfo: 3
-CHECK:   NumStackOffsets: 3
+CHECK: MemprofProfile:
+CHECK-NEXT:   -
+CHECK-NEXT:   Header:
+CHECK-NEXT:     Version: 1
+CHECK-NEXT:     TotalSizeBytes: 1016
+CHECK-NEXT:     NumSegments: 9
+CHECK-NEXT:     NumMibInfo: 3
+CHECK-NEXT:     NumStackOffsets: 3
index f143955..99c32a9 100644 (file)
@@ -36,15 +36,18 @@ RUN: llvm-profdata show --memory %p/Inputs/multi.memprofraw -o - | FileCheck %s
 We expect 2 MIB entries, 1 each for the malloc calls in the program. Unlike the
 memprof-basic.test we do not see any allocation from glibc.
 
-CHECK: MemProf Profile 1
-CHECK:   Version: 1
-CHECK:   TotalSizeBytes: 864
-CHECK:   NumSegments: 9
-CHECK:   NumMIBInfo: 2
-CHECK:   NumStackOffsets: 2
-CHECK: MemProf Profile 2
-CHECK:   Version: 1
-CHECK:   TotalSizeBytes: 864
-CHECK:   NumSegments: 9
-CHECK:   NumMIBInfo: 2
-CHECK:   NumStackOffsets: 2
+CHECK: MemprofProfile:
+CHECK-NEXT:   -
+CHECK-NEXT:   Header:
+CHECK-NEXT:     Version: 1
+CHECK-NEXT:     TotalSizeBytes: 864
+CHECK-NEXT:     NumSegments: 9
+CHECK-NEXT:     NumMibInfo: 2
+CHECK-NEXT:     NumStackOffsets: 2
+CHECK-NEXT:   -
+CHECK-NEXT:   Header:
+CHECK-NEXT:     Version: 1
+CHECK-NEXT:     TotalSizeBytes: 864
+CHECK-NEXT:     NumSegments: 9
+CHECK-NEXT:     NumMibInfo: 2
+CHECK-NEXT:     NumStackOffsets: 2
index 6000460..9a345b4 100644 (file)
@@ -2487,7 +2487,8 @@ static int showMemProfProfile(const std::string &Filename, raw_fd_ostream &OS) {
 
   std::unique_ptr<llvm::memprof::RawMemProfReader> Reader(
       ReaderOr.get().release());
-  Reader->printSummaries(OS);
+
+  Reader->printYAML(OS);
   return 0;
 }