[llvm] [memprof] Use CSId to construct MemProfRecord (PR #88362)

Snehasish Kumar via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 16 09:27:13 PDT 2024


================
@@ -432,4 +434,86 @@ TEST(MemProf, BaseMemProfReader) {
   EXPECT_THAT(Records[0].AllocSites[0].CallStack[1],
               FrameContains("bar", 10U, 2U, false));
 }
+
+TEST(MemProf, IndexedMemProfRecordToMemProfRecord) {
+  // Verify that MemProfRecord can be constructed from IndexedMemProfRecord with
+  // CallStackIds only.
+
+  llvm::DenseMap<FrameId, Frame> FrameIdMap;
+  Frame F1(1, 0, 0, false);
+  Frame F2(2, 0, 0, false);
+  Frame F3(3, 0, 0, false);
+  Frame F4(4, 0, 0, false);
+  FrameIdMap.insert({F1.hash(), F1});
+  FrameIdMap.insert({F2.hash(), F2});
+  FrameIdMap.insert({F3.hash(), F3});
+  FrameIdMap.insert({F4.hash(), F4});
+
+  llvm::DenseMap<CallStackId, llvm::SmallVector<FrameId>> CallStackIdMap;
+  llvm::SmallVector<FrameId> CS1 = {F1.hash(), F2.hash()};
+  llvm::SmallVector<FrameId> CS2 = {F1.hash(), F3.hash()};
+  llvm::SmallVector<FrameId> CS3 = {F2.hash(), F3.hash()};
+  llvm::SmallVector<FrameId> CS4 = {F2.hash(), F4.hash()};
+  CallStackIdMap.insert({llvm::memprof::hashCallStack(CS1), CS1});
+  CallStackIdMap.insert({llvm::memprof::hashCallStack(CS2), CS2});
+  CallStackIdMap.insert({llvm::memprof::hashCallStack(CS3), CS3});
+  CallStackIdMap.insert({llvm::memprof::hashCallStack(CS4), CS4});
+
+  IndexedMemProfRecord IndexedRecord;
+  IndexedAllocationInfo AI;
+  AI.CSId = llvm::memprof::hashCallStack(CS1);
+  IndexedRecord.AllocSites.push_back(AI);
+  AI.CSId = llvm::memprof::hashCallStack(CS2);
+  IndexedRecord.AllocSites.push_back(AI);
+  IndexedRecord.CallSiteIds.push_back(llvm::memprof::hashCallStack(CS3));
+  IndexedRecord.CallSiteIds.push_back(llvm::memprof::hashCallStack(CS4));
+
+  bool CSIdMissing = false;
+  bool FrameIdMissing = false;
+
+  auto Callback = [&](CallStackId CSId) -> llvm::SmallVector<Frame> {
+    llvm::SmallVector<Frame> CallStack;
+    llvm::SmallVector<FrameId> FrameIds;
+
+    auto Iter = CallStackIdMap.find(CSId);
+    if (Iter == CallStackIdMap.end())
+      CSIdMissing = true;
+    else
+      FrameIds = Iter->second;
+
+    for (FrameId Id : FrameIds) {
+      Frame F(0, 0, 0, false);
+      auto Iter = FrameIdMap.find(Id);
+      if (Iter == FrameIdMap.end())
+        FrameIdMissing = true;
+      else
+        F = Iter->second;
+      CallStack.push_back(F);
+    }
+
+    return CallStack;
+  };
+
+  MemProfRecord Record = IndexedRecord.toMemProfRecord(Callback);
+
+  // Make sure that all lookups are successful.
+  ASSERT_EQ(CSIdMissing, false);
+  ASSERT_EQ(FrameIdMissing, false);
+
+  // Verify the contents of Record.
+  ASSERT_EQ(Record.AllocSites.size(), 2U);
+  ASSERT_EQ(Record.AllocSites[0].CallStack.size(), 2U);
+  ASSERT_EQ(Record.AllocSites[0].CallStack[0].hash(), F1.hash());
+  ASSERT_EQ(Record.AllocSites[0].CallStack[1].hash(), F2.hash());
+  ASSERT_EQ(Record.AllocSites[1].CallStack.size(), 2U);
+  ASSERT_EQ(Record.AllocSites[1].CallStack[0].hash(), F1.hash());
+  ASSERT_EQ(Record.AllocSites[1].CallStack[1].hash(), F3.hash());
+  ASSERT_EQ(Record.CallSites.size(), 2U);
+  ASSERT_EQ(Record.CallSites[0].size(), 2U);
+  ASSERT_EQ(Record.CallSites[0][0].hash(), F2.hash());
+  ASSERT_EQ(Record.CallSites[0][1].hash(), F3.hash());
+  ASSERT_EQ(Record.CallSites[1].size(), 2U);
+  ASSERT_EQ(Record.CallSites[1][0].hash(), F2.hash());
+  ASSERT_EQ(Record.CallSites[1][1].hash(), F4.hash());
----------------
snehasish wrote:

A couple of nits:

1. Assertions will fail the test immediately so they are good for the size checks to avoid invalid accesses when we check the contents later but the others should be expectations to allow multiple checks to be reported. See https://google.github.io/googletest/primer.html#assertions
2. The `SizeIs` matcher can make the tests a little more readable and yields a better error message. 
 See https://github.com/google/googletest/blob/main/docs/reference/matchers.md

https://github.com/llvm/llvm-project/pull/88362


More information about the llvm-commits mailing list