1 #include "llvm/ProfileData/MemProf.h" 2 #include "llvm/ADT/SmallVector.h" 3 #include "llvm/IR/Function.h" 4 #include "llvm/ProfileData/InstrProf.h" 5 #include "llvm/Support/Endian.h" 6 #include "llvm/Support/EndianStream.h" 7 8 namespace llvm { 9 namespace memprof { 10 11 void MemProfRecord::serialize(const MemProfSchema &Schema, raw_ostream &OS) { 12 using namespace support; 13 14 endian::Writer LE(OS, little); 15 16 LE.write<uint64_t>(AllocSites.size()); 17 for (const AllocationInfo &N : AllocSites) { 18 LE.write<uint64_t>(N.CallStack.size()); 19 for (const Frame &F : N.CallStack) 20 F.serialize(OS); 21 N.Info.serialize(Schema, OS); 22 } 23 24 // Related contexts. 25 LE.write<uint64_t>(CallSites.size()); 26 for (const auto &Frames : CallSites) { 27 LE.write<uint64_t>(Frames.size()); 28 for (const Frame &F : Frames) 29 F.serialize(OS); 30 } 31 } 32 33 MemProfRecord MemProfRecord::deserialize(const MemProfSchema &Schema, 34 const unsigned char *Ptr) { 35 using namespace support; 36 37 MemProfRecord Record; 38 39 // Read the meminfo nodes. 40 const uint64_t NumNodes = endian::readNext<uint64_t, little, unaligned>(Ptr); 41 for (uint64_t I = 0; I < NumNodes; I++) { 42 MemProfRecord::AllocationInfo Node; 43 const uint64_t NumFrames = 44 endian::readNext<uint64_t, little, unaligned>(Ptr); 45 for (uint64_t J = 0; J < NumFrames; J++) { 46 const auto F = MemProfRecord::Frame::deserialize(Ptr); 47 Ptr += MemProfRecord::Frame::serializedSize(); 48 Node.CallStack.push_back(F); 49 } 50 Node.Info.deserialize(Schema, Ptr); 51 Ptr += PortableMemInfoBlock::serializedSize(); 52 Record.AllocSites.push_back(Node); 53 } 54 55 // Read the callsite information. 56 const uint64_t NumCtxs = endian::readNext<uint64_t, little, unaligned>(Ptr); 57 for (uint64_t J = 0; J < NumCtxs; J++) { 58 const uint64_t NumFrames = 59 endian::readNext<uint64_t, little, unaligned>(Ptr); 60 llvm::SmallVector<Frame> Frames; 61 for (uint64_t K = 0; K < NumFrames; K++) { 62 const auto F = MemProfRecord::Frame::deserialize(Ptr); 63 Ptr += MemProfRecord::Frame::serializedSize(); 64 Frames.push_back(F); 65 } 66 Record.CallSites.push_back(Frames); 67 } 68 69 return Record; 70 } 71 72 GlobalValue::GUID MemProfRecord::getGUID(const StringRef FunctionName) { 73 const auto Pos = FunctionName.find(".llvm."); 74 75 // We use the function guid which we expect to be a uint64_t. At 76 // this time, it is the lower 64 bits of the md5 of the function 77 // name. Any suffix with .llvm. is trimmed since these are added by 78 // thinLTO global promotion. At the time the profile is consumed, 79 // these suffixes will not be present. 80 return Function::getGUID(FunctionName.take_front(Pos)); 81 } 82 83 Expected<MemProfSchema> readMemProfSchema(const unsigned char *&Buffer) { 84 using namespace support; 85 86 const unsigned char *Ptr = Buffer; 87 const uint64_t NumSchemaIds = 88 endian::readNext<uint64_t, little, unaligned>(Ptr); 89 if (NumSchemaIds > static_cast<uint64_t>(Meta::Size)) { 90 return make_error<InstrProfError>(instrprof_error::malformed, 91 "memprof schema invalid"); 92 } 93 94 MemProfSchema Result; 95 for (size_t I = 0; I < NumSchemaIds; I++) { 96 const uint64_t Tag = endian::readNext<uint64_t, little, unaligned>(Ptr); 97 if (Tag >= static_cast<uint64_t>(Meta::Size)) { 98 return make_error<InstrProfError>(instrprof_error::malformed, 99 "memprof schema invalid"); 100 } 101 Result.push_back(static_cast<Meta>(Tag)); 102 } 103 // Advace the buffer to one past the schema if we succeeded. 104 Buffer = Ptr; 105 return Result; 106 } 107 108 } // namespace memprof 109 } // namespace llvm 110