[Lldb-commits] [lldb] 37a466d - [trace][intelpt] Added total memory usage by decoded trace

Alisamar Husain via lldb-commits lldb-commits at lists.llvm.org
Mon Mar 21 00:06:21 PDT 2022


Author: Alisamar Husain
Date: 2022-03-21T12:36:08+05:30
New Revision: 37a466dd72b0c46090198c9f1779c384809c0662

URL: https://github.com/llvm/llvm-project/commit/37a466dd72b0c46090198c9f1779c384809c0662
DIFF: https://github.com/llvm/llvm-project/commit/37a466dd72b0c46090198c9f1779c384809c0662.diff

LOG: [trace][intelpt] Added total memory usage by decoded trace

This fails currently but the basics are there

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

Added: 
    

Modified: 
    lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
    lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
    lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
    lldb/test/API/commands/trace/TestTraceDumpInfo.py

Removed: 
    


################################################################################
diff  --git a/lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp b/lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
index 4822a786c68c1..a81a779302605 100644
--- a/lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
+++ b/lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
@@ -48,6 +48,8 @@ bool IntelPTInstruction::IsError() const { return (bool)m_error; }
 
 lldb::addr_t IntelPTInstruction::GetLoadAddress() const { return m_pt_insn.ip; }
 
+size_t IntelPTInstruction::GetNonErrorMemoryUsage() { return sizeof(IntelPTInstruction); }
+
 Optional<uint64_t> IntelPTInstruction::GetTimestampCounter() const {
   return m_timestamp;
 }
@@ -116,3 +118,9 @@ DecodedThread::DecodedThread(ThreadSP thread_sp,
 lldb::TraceCursorUP DecodedThread::GetCursor() {
   return std::make_unique<TraceCursorIntelPT>(m_thread_sp, shared_from_this());
 }
+
+size_t DecodedThread::CalculateApproximateMemoryUsage() const {
+  return m_raw_trace_size
+    + IntelPTInstruction::GetNonErrorMemoryUsage() * m_instructions.size()
+    + sizeof(DecodedThread);
+}

diff  --git a/lldb/source/Plugins/Trace/intel-pt/DecodedThread.h b/lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
index 592c402cd0e50..4063012997045 100644
--- a/lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
+++ b/lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
@@ -81,6 +81,9 @@ class IntelPTInstruction {
   ///     The instruction pointer address, or \a LLDB_INVALID_ADDRESS if it is
   ///     an error.
   lldb::addr_t GetLoadAddress() const;
+  
+  /// Get the size in bytes of a non-error instance of this class
+  static size_t GetNonErrorMemoryUsage();
 
   /// \return
   ///     An \a llvm::Error object if this class corresponds to an Error, or an
@@ -112,6 +115,8 @@ class IntelPTInstruction {
   IntelPTInstruction(const IntelPTInstruction &other) = delete;
   const IntelPTInstruction &operator=(const IntelPTInstruction &other) = delete;
 
+  // When adding new members to this class, make sure to update 
+  // IntelPTInstruction::GetNonErrorMemoryUsage() if needed.
   pt_insn m_pt_insn;
   llvm::Optional<uint64_t> m_timestamp;
   std::unique_ptr<llvm::ErrorInfoBase> m_error;
@@ -150,7 +155,13 @@ class DecodedThread : public std::enable_shared_from_this<DecodedThread> {
   ///   The size of the trace.
   size_t GetRawTraceSize() const;
 
+  /// The approximate size in bytes used by this instance, 
+  /// including all the already decoded instructions.
+  size_t CalculateApproximateMemoryUsage() const;
+
 private:
+  /// When adding new members to this class, make sure 
+  /// to update \a CalculateApproximateMemoryUsage() accordingly.
   lldb::ThreadSP m_thread_sp;
   std::vector<IntelPTInstruction> m_instructions;
   size_t m_raw_trace_size;

diff  --git a/lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp b/lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
index 831cd3764672c..63318031f8893 100644
--- a/lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ b/lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -111,9 +111,13 @@ void TraceIntelPT::DumpTraceInfo(Thread &thread, Stream &s, bool verbose) {
     return;
   }
   s.Printf("\n");
-  s.Printf("  Raw trace size: %zu bytes\n", *raw_size);
-  s.Printf("  Total number of instructions: %zu\n", 
-    Decode(thread)->GetInstructions().size());
+
+  size_t mem_used = Decode(thread)->CalculateApproximateMemoryUsage();
+  s.Printf("  Raw trace size: %zu KiB\n", *raw_size / 1024);
+  s.Printf("  Total number of instructions: %zu\n",
+           Decode(thread)->GetInstructions().size());
+  s.Printf("  Total approximate memory usage: %0.2lf KiB\n",
+           (double)mem_used / 1024);
   return;
 }
 

diff  --git a/lldb/test/API/commands/trace/TestTraceDumpInfo.py b/lldb/test/API/commands/trace/TestTraceDumpInfo.py
index 18088e07b8df3..bc50b5274195a 100644
--- a/lldb/test/API/commands/trace/TestTraceDumpInfo.py
+++ b/lldb/test/API/commands/trace/TestTraceDumpInfo.py
@@ -38,5 +38,6 @@ def testDumpRawTraceSize(self):
             substrs=['''Trace technology: intel-pt
 
 thread #1: tid = 3842849
-  Raw trace size: 4096 bytes
-  Total number of instructions: 21'''])
+  Raw trace size: 4 KiB
+  Total number of instructions: 21
+  Total approximate memory usage: 5.38 KiB'''])


        


More information about the lldb-commits mailing list