From d495c5340d57a30a83496e6e9794b8bc746f99ad Mon Sep 17 00:00:00 2001 From: Greg Clayton Date: Tue, 17 May 2011 03:37:42 +0000 Subject: [PATCH] Added an allocated memory cache to avoid having to allocate memory over and over when running JITed expressions. The allocated memory cache will cache allocate memory a page at a time for each permission combination and divvy up the memory and hand it out in 16 byte increments. llvm-svn: 131453 --- lldb/include/lldb/Core/State.h | 3 + lldb/include/lldb/Target/Memory.h | 189 ++++++++ lldb/include/lldb/Target/Process.h | 44 +- lldb/lldb.xcodeproj/project.pbxproj | 6 + lldb/source/Core/State.cpp | 25 ++ .../Process/gdb-remote/ProcessGDBRemote.cpp | 2 +- lldb/source/Target/Memory.cpp | 413 ++++++++++++++++++ lldb/source/Target/Process.cpp | 167 +------ 8 files changed, 658 insertions(+), 191 deletions(-) create mode 100644 lldb/include/lldb/Target/Memory.h create mode 100644 lldb/source/Target/Memory.cpp diff --git a/lldb/include/lldb/Core/State.h b/lldb/include/lldb/Core/State.h index 8bb42a8be497..6b98f3b2b0ac 100644 --- a/lldb/include/lldb/Core/State.h +++ b/lldb/include/lldb/Core/State.h @@ -41,6 +41,9 @@ StateIsStoppedState (lldb::StateType state); const char * GetFormatAsCString (lldb::Format format); +const char * +GetPermissionsAsCString (uint32_t permissions); + } // namespace lldb_private #endif // liblldb_State_h_ diff --git a/lldb/include/lldb/Target/Memory.h b/lldb/include/lldb/Target/Memory.h new file mode 100644 index 000000000000..941e4c0258bf --- /dev/null +++ b/lldb/include/lldb/Target/Memory.h @@ -0,0 +1,189 @@ +//===-- Memory.h ------------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_Memory_h_ +#define liblldb_Memory_h_ + +// C Includes +// C++ Includes +#include +#include + +// Other libraries and framework includes +//#include "llvm/ADT/BitVector.h" + +// Project includes +#include "lldb/lldb-private.h" +#include "lldb/Host/Mutex.h" + +namespace lldb_private { + //---------------------------------------------------------------------- + // A class to track memory that was read from a live process between + // runs. + //---------------------------------------------------------------------- + class MemoryCache + { + public: + //------------------------------------------------------------------ + // Constructors and Destructors + //------------------------------------------------------------------ + MemoryCache (Process &process); + + ~MemoryCache (); + + void + Clear(); + + void + Flush (lldb::addr_t addr, size_t size); + + size_t + Read (lldb::addr_t addr, + void *dst, + size_t dst_len, + Error &error); + + uint32_t + GetMemoryCacheLineSize() const + { + return m_cache_line_byte_size ; + } + protected: + typedef std::map collection; + //------------------------------------------------------------------ + // Classes that inherit from MemoryCache can see and modify these + //------------------------------------------------------------------ + Process &m_process; + uint32_t m_cache_line_byte_size; + Mutex m_cache_mutex; + collection m_cache; + + private: + DISALLOW_COPY_AND_ASSIGN (MemoryCache); + }; + + + class AllocatedBlock + { + public: + AllocatedBlock (lldb::addr_t addr, + uint32_t byte_size, + uint32_t permissions, + uint32_t chunk_size); + + ~AllocatedBlock (); + + lldb::addr_t + ReserveBlock (uint32_t size); + + bool + FreeBlock (lldb::addr_t addr); + + lldb::addr_t + GetBaseAddress () const + { + return m_addr; + } + + uint32_t + GetByteSize () const + { + return m_byte_size; + } + + uint32_t + GetPermissions () const + { + return m_permissions; + } + + uint32_t + GetChunkSize () const + { + return m_chunk_size; + } + + bool + Contains (lldb::addr_t addr) const + { + return ((addr >= m_addr) && addr < (m_addr + m_byte_size)); + } + protected: + uint32_t + TotalChunks () const + { + return m_byte_size / m_chunk_size; + } + + uint32_t + CalculateChunksNeededForSize (uint32_t size) const + { + return (size + m_chunk_size - 1) / m_chunk_size; + } + const lldb::addr_t m_addr; // Base address of this block of memory + const uint32_t m_byte_size; // 4GB of chunk should be enough... + const uint32_t m_permissions; // Permissions for this memory (logical OR of lldb::Permissions bits) + const uint32_t m_chunk_size; // The size of chunks that the memory at m_addr is divied up into + typedef std::map OffsetToChunkSize; + OffsetToChunkSize m_offset_to_chunk_size; + //llvm::BitVector m_allocated; + }; + + + //---------------------------------------------------------------------- + // A class that can track allocated memory and give out allocated memory + // without us having to make an allocate/deallocate call every time we + // need some memory in a process that is being debugged. + //---------------------------------------------------------------------- + class AllocatedMemoryCache + { + public: + //------------------------------------------------------------------ + // Constructors and Destructors + //------------------------------------------------------------------ + AllocatedMemoryCache (Process &process); + + ~AllocatedMemoryCache (); + + void + Clear(); + + lldb::addr_t + AllocateMemory (size_t byte_size, + uint32_t permissions, + Error &error); + + bool + DeallocateMemory (lldb::addr_t ptr); + + protected: + typedef lldb::SharedPtr::Type AllocatedBlockSP; + + AllocatedBlockSP + AllocatePage (uint32_t byte_size, + uint32_t permissions, + uint32_t chunk_size, + Error &error); + + + //------------------------------------------------------------------ + // Classes that inherit from MemoryCache can see and modify these + //------------------------------------------------------------------ + Process &m_process; + Mutex m_mutex; + typedef std::multimap PermissionsToBlockMap; + PermissionsToBlockMap m_memory_map; + + private: + DISALLOW_COPY_AND_ASSIGN (AllocatedMemoryCache); + }; + +} // namespace lldb_private + +#endif // liblldb_Memory_h_ diff --git a/lldb/include/lldb/Target/Process.h b/lldb/include/lldb/Target/Process.h index 93819bb04907..dddf7c403e09 100644 --- a/lldb/include/lldb/Target/Process.h +++ b/lldb/include/lldb/Target/Process.h @@ -37,6 +37,7 @@ #include "lldb/Interpreter/Args.h" #include "lldb/Interpreter/Options.h" #include "lldb/Target/ExecutionContextScope.h" +#include "lldb/Target/Memory.h" #include "lldb/Target/ThreadList.h" #include "lldb/Target/UnixSignals.h" @@ -2602,48 +2603,6 @@ protected: std::string m_exit_string; }; - - class MemoryCache - { - public: - //------------------------------------------------------------------ - // Constructors and Destructors - //------------------------------------------------------------------ - MemoryCache (); - - ~MemoryCache (); - - void - Clear(); - - void - Flush (lldb::addr_t addr, size_t size); - - size_t - Read (Process *process, - lldb::addr_t addr, - void *dst, - size_t dst_len, - Error &error); - - uint32_t - GetMemoryCacheLineSize() const - { - return m_cache_line_byte_size ; - } - protected: - typedef std::map collection; - //------------------------------------------------------------------ - // Classes that inherit from MemoryCache can see and modify these - //------------------------------------------------------------------ - uint32_t m_cache_line_byte_size; - Mutex m_cache_mutex; - collection m_cache; - - private: - DISALLOW_COPY_AND_ASSIGN (MemoryCache); - }; - bool HijackPrivateProcessEvents (Listener *listener); @@ -2686,6 +2645,7 @@ protected: lldb_private::Mutex m_stdio_communication_mutex; std::string m_stdout_data; MemoryCache m_memory_cache; + AllocatedMemoryCache m_allocated_memory_cache; typedef std::map LanguageRuntimeCollection; LanguageRuntimeCollection m_language_runtimes; diff --git a/lldb/lldb.xcodeproj/project.pbxproj b/lldb/lldb.xcodeproj/project.pbxproj index 79b8c2f19276..d7d98819df1a 100644 --- a/lldb/lldb.xcodeproj/project.pbxproj +++ b/lldb/lldb.xcodeproj/project.pbxproj @@ -340,6 +340,7 @@ 2689FFFF13353DB600698AC0 /* BreakpointResolver.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 26BC7E1210F1B83100F91463 /* BreakpointResolver.cpp */; }; 268F9D53123AA15200B91E9B /* SBSymbolContextList.h in Headers */ = {isa = PBXBuildFile; fileRef = 268F9D52123AA15200B91E9B /* SBSymbolContextList.h */; settings = {ATTRIBUTES = (Public, ); }; }; 268F9D55123AA16600B91E9B /* SBSymbolContextList.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 268F9D54123AA16600B91E9B /* SBSymbolContextList.cpp */; }; + 2690B3711381D5C300ECFBAE /* Memory.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2690B3701381D5C300ECFBAE /* Memory.cpp */; }; 2692BA15136610C100F9E14D /* UnwindAssemblyInstEmulation.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2692BA13136610C100F9E14D /* UnwindAssemblyInstEmulation.cpp */; }; 2697A54D133A6305004E4240 /* PlatformDarwin.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2697A54B133A6305004E4240 /* PlatformDarwin.cpp */; }; 26A69C5F137A17A500262477 /* RegisterValue.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 26C6886E137880C400407EDF /* RegisterValue.cpp */; }; @@ -695,6 +696,8 @@ 268DA873130095ED00C9483A /* Terminal.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Terminal.cpp; sourceTree = ""; }; 268F9D52123AA15200B91E9B /* SBSymbolContextList.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SBSymbolContextList.h; path = include/lldb/API/SBSymbolContextList.h; sourceTree = ""; }; 268F9D54123AA16600B91E9B /* SBSymbolContextList.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SBSymbolContextList.cpp; path = source/API/SBSymbolContextList.cpp; sourceTree = ""; }; + 2690B36F1381D5B600ECFBAE /* Memory.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = Memory.h; path = include/lldb/Target/Memory.h; sourceTree = ""; }; + 2690B3701381D5C300ECFBAE /* Memory.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = Memory.cpp; path = source/Target/Memory.cpp; sourceTree = ""; }; 2692BA13136610C100F9E14D /* UnwindAssemblyInstEmulation.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = UnwindAssemblyInstEmulation.cpp; sourceTree = ""; }; 2692BA14136610C100F9E14D /* UnwindAssemblyInstEmulation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UnwindAssemblyInstEmulation.h; sourceTree = ""; }; 269416AD119A024800FF2715 /* CommandObjectTarget.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CommandObjectTarget.cpp; path = source/Commands/CommandObjectTarget.cpp; sourceTree = ""; }; @@ -2242,6 +2245,8 @@ 26DAFD9711529BC7005A394E /* ExecutionContextScope.h */, 4CB4430912491DDA00C13DC2 /* LanguageRuntime.h */, 4CB4430A12491DDA00C13DC2 /* LanguageRuntime.cpp */, + 2690B36F1381D5B600ECFBAE /* Memory.h */, + 2690B3701381D5C300ECFBAE /* Memory.cpp */, 4CB443F612499B6E00C13DC2 /* ObjCLanguageRuntime.h */, 4CB443F212499B5000C13DC2 /* ObjCLanguageRuntime.cpp */, 495BBACF119A0DE700418BEA /* PathMappingList.h */, @@ -3187,6 +3192,7 @@ 26DB3E1C1379E7AD0080DC73 /* ABIMacOSX_i386.cpp in Sources */, 26DB3E1F1379E7AD0080DC73 /* ABISysV_x86_64.cpp in Sources */, 26A69C5F137A17A500262477 /* RegisterValue.cpp in Sources */, + 2690B3711381D5C300ECFBAE /* Memory.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/lldb/source/Core/State.cpp b/lldb/source/Core/State.cpp index 92687045fb92..ad73ba8e3d50 100644 --- a/lldb/source/Core/State.cpp +++ b/lldb/source/Core/State.cpp @@ -84,6 +84,31 @@ lldb_private::GetFormatAsCString (lldb::Format format) return unknown_format_string; } + +const char * +lldb_private::GetPermissionsAsCString (uint32_t permissions) +{ + switch (permissions) + { + case 0: return "---"; + case ePermissionsWritable: return "-w-"; + case ePermissionsReadable: return "r--"; + case ePermissionsExecutable: return "--x"; + case ePermissionsReadable | + ePermissionsWritable: return "rw-"; + case ePermissionsReadable | + ePermissionsExecutable: return "r-x"; + case ePermissionsWritable | + ePermissionsExecutable: return "-wx"; + case ePermissionsReadable | + ePermissionsWritable | + ePermissionsExecutable: return "rwx"; + default: + break; + } + return "???"; +} + bool lldb_private::StateIsRunningState (StateType state) { diff --git a/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp b/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp index 0a65b987a40b..0008bdbc4250 100644 --- a/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp +++ b/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp @@ -1663,7 +1663,7 @@ ProcessGDBRemote::DoAllocateMemory (size_t size, uint32_t permissions, Error &er } if (allocated_addr == LLDB_INVALID_ADDRESS) - error.SetErrorStringWithFormat("unable to allocate %zu bytes of memory with permissions %u", size, permissions); + error.SetErrorStringWithFormat("unable to allocate %zu bytes of memory with permissions %s", size, GetPermissionsAsCString (permissions)); else error.Clear(); return allocated_addr; diff --git a/lldb/source/Target/Memory.cpp b/lldb/source/Target/Memory.cpp new file mode 100644 index 000000000000..3f5567b7c8fa --- /dev/null +++ b/lldb/source/Target/Memory.cpp @@ -0,0 +1,413 @@ +//===-- Memory.cpp ----------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/Target/Memory.h" +// C Includes +// C++ Includes +// Other libraries and framework includes +// Project includes +#include "lldb/Core/DataBufferHeap.h" +#include "lldb/Core/State.h" +#include "lldb/Core/Log.h" +#include "lldb/Target/Process.h" + +using namespace lldb; +using namespace lldb_private; + +//---------------------------------------------------------------------- +// MemoryCache constructor +//---------------------------------------------------------------------- +MemoryCache::MemoryCache(Process &process) : + m_process (process), + m_cache_line_byte_size (512), + m_cache_mutex (Mutex::eMutexTypeRecursive), + m_cache () +{ +} + +//---------------------------------------------------------------------- +// Destructor +//---------------------------------------------------------------------- +MemoryCache::~MemoryCache() +{ +} + +void +MemoryCache::Clear() +{ + Mutex::Locker locker (m_cache_mutex); + m_cache.clear(); +} + +void +MemoryCache::Flush (addr_t addr, size_t size) +{ + if (size == 0) + return; + + const uint32_t cache_line_byte_size = m_cache_line_byte_size; + const addr_t end_addr = (addr + size - 1); + const addr_t flush_start_addr = addr - (addr % cache_line_byte_size); + const addr_t flush_end_addr = end_addr - (end_addr % cache_line_byte_size); + + Mutex::Locker locker (m_cache_mutex); + if (m_cache.empty()) + return; + + assert ((flush_start_addr % cache_line_byte_size) == 0); + + for (addr_t curr_addr = flush_start_addr; curr_addr <= flush_end_addr; curr_addr += cache_line_byte_size) + { + collection::iterator pos = m_cache.find (curr_addr); + if (pos != m_cache.end()) + m_cache.erase(pos); + } +} + +size_t +MemoryCache::Read (addr_t addr, + void *dst, + size_t dst_len, + Error &error) +{ + size_t bytes_left = dst_len; + if (dst && bytes_left > 0) + { + const uint32_t cache_line_byte_size = m_cache_line_byte_size; + uint8_t *dst_buf = (uint8_t *)dst; + addr_t curr_addr = addr - (addr % cache_line_byte_size); + addr_t cache_offset = addr - curr_addr; + Mutex::Locker locker (m_cache_mutex); + + while (bytes_left > 0) + { + collection::const_iterator pos = m_cache.find (curr_addr); + collection::const_iterator end = m_cache.end (); + + if (pos != end) + { + size_t curr_read_size = cache_line_byte_size - cache_offset; + if (curr_read_size > bytes_left) + curr_read_size = bytes_left; + + memcpy (dst_buf + dst_len - bytes_left, pos->second->GetBytes() + cache_offset, curr_read_size); + + bytes_left -= curr_read_size; + curr_addr += curr_read_size + cache_offset; + cache_offset = 0; + + if (bytes_left > 0) + { + // Get sequential cache page hits + for (++pos; (pos != end) && (bytes_left > 0); ++pos) + { + assert ((curr_addr % cache_line_byte_size) == 0); + + if (pos->first != curr_addr) + break; + + curr_read_size = pos->second->GetByteSize(); + if (curr_read_size > bytes_left) + curr_read_size = bytes_left; + + memcpy (dst_buf + dst_len - bytes_left, pos->second->GetBytes(), curr_read_size); + + bytes_left -= curr_read_size; + curr_addr += curr_read_size; + + // We have a cache page that succeeded to read some bytes + // but not an entire page. If this happens, we must cap + // off how much data we are able to read... + if (pos->second->GetByteSize() != cache_line_byte_size) + return dst_len - bytes_left; + } + } + } + + // We need to read from the process + + if (bytes_left > 0) + { + assert ((curr_addr % cache_line_byte_size) == 0); + std::auto_ptr data_buffer_heap_ap(new DataBufferHeap (cache_line_byte_size, 0)); + size_t process_bytes_read = m_process.ReadMemoryFromInferior (curr_addr, + data_buffer_heap_ap->GetBytes(), + data_buffer_heap_ap->GetByteSize(), + error); + if (process_bytes_read == 0) + return dst_len - bytes_left; + + if (process_bytes_read != cache_line_byte_size) + data_buffer_heap_ap->SetByteSize (process_bytes_read); + m_cache[curr_addr] = DataBufferSP (data_buffer_heap_ap.release()); + // We have read data and put it into the cache, continue through the + // loop again to get the data out of the cache... + } + } + } + + return dst_len - bytes_left; +} + + + +AllocatedBlock::AllocatedBlock (lldb::addr_t addr, + uint32_t byte_size, + uint32_t permissions, + uint32_t chunk_size) : + m_addr (addr), + m_byte_size (byte_size), + m_permissions (permissions), + m_chunk_size (chunk_size), + m_offset_to_chunk_size () +// m_allocated (byte_size / chunk_size) +{ + assert (byte_size > chunk_size); +} + +AllocatedBlock::~AllocatedBlock () +{ +} + +lldb::addr_t +AllocatedBlock::ReserveBlock (uint32_t size) +{ + addr_t addr = LLDB_INVALID_ADDRESS; + if (size <= m_byte_size) + { + const uint32_t needed_chunks = CalculateChunksNeededForSize (size); + LogSP log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_VERBOSE)); + + if (m_offset_to_chunk_size.empty()) + { + m_offset_to_chunk_size[0] = needed_chunks; + if (log) + log->Printf ("[1] AllocatedBlock::ReserveBlock (size = %u (0x%x)) => offset = 0x%x, %u %u bit chunks", size, size, 0, needed_chunks, m_chunk_size); + addr = m_addr; + } + else + { + uint32_t last_offset = 0; + OffsetToChunkSize::const_iterator pos = m_offset_to_chunk_size.begin(); + OffsetToChunkSize::const_iterator end = m_offset_to_chunk_size.end(); + while (pos != end) + { + if (pos->first > last_offset) + { + const uint32_t bytes_available = pos->first - last_offset; + const uint32_t num_chunks = CalculateChunksNeededForSize (bytes_available); + if (num_chunks >= needed_chunks) + { + m_offset_to_chunk_size[last_offset] = needed_chunks; + if (log) + log->Printf ("[2] AllocatedBlock::ReserveBlock (size = %u (0x%x)) => offset = 0x%x, %u %u bit chunks", size, size, last_offset, needed_chunks, m_chunk_size); + addr = m_addr + last_offset; + break; + } + } + + last_offset = pos->first + pos->second * m_chunk_size; + + if (++pos == end) + { + // Last entry... + const uint32_t chunks_left = CalculateChunksNeededForSize (m_byte_size - last_offset); + if (chunks_left >= needed_chunks) + { + m_offset_to_chunk_size[last_offset] = needed_chunks; + if (log) + log->Printf ("[3] AllocatedBlock::ReserveBlock (size = %u (0x%x)) => offset = 0x%x, %u %u bit chunks", size, size, last_offset, needed_chunks, m_chunk_size); + addr = m_addr + last_offset; + break; + } + } + } + } +// const uint32_t total_chunks = m_allocated.size (); +// uint32_t unallocated_idx = 0; +// uint32_t allocated_idx = m_allocated.find_first(); +// uint32_t first_chunk_idx = UINT32_MAX; +// uint32_t num_chunks; +// while (1) +// { +// if (allocated_idx == UINT32_MAX) +// { +// // No more bits are set starting from unallocated_idx, so we +// // either have enough chunks for the request, or we don't. +// // Eiter way we break out of the while loop... +// num_chunks = total_chunks - unallocated_idx; +// if (needed_chunks <= num_chunks) +// first_chunk_idx = unallocated_idx; +// break; +// } +// else if (allocated_idx > unallocated_idx) +// { +// // We have some allocated chunks, check if there are enough +// // free chunks to satisfy the request? +// num_chunks = allocated_idx - unallocated_idx; +// if (needed_chunks <= num_chunks) +// { +// // Yep, we have enough! +// first_chunk_idx = unallocated_idx; +// break; +// } +// } +// +// while (unallocated_idx < total_chunks) +// { +// if (m_allocated[unallocated_idx]) +// ++unallocated_idx; +// else +// break; +// } +// +// if (unallocated_idx >= total_chunks) +// break; +// +// allocated_idx = m_allocated.find_next(unallocated_idx); +// } +// +// if (first_chunk_idx != UINT32_MAX) +// { +// const uint32_t end_bit_idx = unallocated_idx + needed_chunks; +// for (uint32_t idx = first_chunk_idx; idx < end_bit_idx; ++idx) +// m_allocated.set(idx); +// return m_addr + m_chunk_size * first_chunk_idx; +// } + } + LogSP log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_VERBOSE)); + if (log) + log->Printf ("AllocatedBlock::ReserveBlock (size = %u (0x%x)) => 0x%16.16llx", size, size, (uint64_t)addr); + return addr; +} + +bool +AllocatedBlock::FreeBlock (addr_t addr) +{ + uint32_t offset = addr - m_addr; + OffsetToChunkSize::iterator pos = m_offset_to_chunk_size.find (offset); + bool success = false; + if (pos != m_offset_to_chunk_size.end()) + { + m_offset_to_chunk_size.erase (pos); + success = true; + } + LogSP log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_VERBOSE)); + if (log) + log->Printf ("AllocatedBlock::FreeBlock (addr = 0x%16.16llx) => %i", (uint64_t)addr, success); + return success; +} + + +AllocatedMemoryCache::AllocatedMemoryCache (Process &process) : + m_process (process), + m_mutex (Mutex::eMutexTypeRecursive), + m_memory_map() +{ +} + +AllocatedMemoryCache::~AllocatedMemoryCache () +{ +} + + +void +AllocatedMemoryCache::Clear() +{ + Mutex::Locker locker (m_mutex); + if (m_process.IsAlive()) + { + PermissionsToBlockMap::iterator pos, end = m_memory_map.end(); + for (pos = m_memory_map.begin(); pos != end; ++pos) + m_process.DoDeallocateMemory(pos->second->GetBaseAddress()); + } + m_memory_map.clear(); +} + + +AllocatedMemoryCache::AllocatedBlockSP +AllocatedMemoryCache::AllocatePage (uint32_t byte_size, + uint32_t permissions, + uint32_t chunk_size, + Error &error) +{ + AllocatedBlockSP block_sp; + const size_t page_size = 4096; + const size_t num_pages = (byte_size + page_size - 1) / page_size; + const size_t page_byte_size = num_pages * page_size; + + addr_t addr = m_process.DoAllocateMemory(page_byte_size, permissions, error); + + LogSP log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); + if (log) + { + log->Printf ("Process::DoAllocateMemory (byte_size = 0x%8.8zx, permissions = %s) => 0x%16.16llx", + page_byte_size, + GetPermissionsAsCString(permissions), + (uint64_t)addr); + } + + if (addr != LLDB_INVALID_ADDRESS) + { + block_sp.reset (new AllocatedBlock (addr, page_byte_size, permissions, chunk_size)); + m_memory_map.insert (std::make_pair (permissions, block_sp)); + } + return block_sp; +} + +lldb::addr_t +AllocatedMemoryCache::AllocateMemory (size_t byte_size, + uint32_t permissions, + Error &error) +{ + Mutex::Locker locker (m_mutex); + + addr_t addr = LLDB_INVALID_ADDRESS; + std::pair range = m_memory_map.equal_range (permissions); + + for (PermissionsToBlockMap::iterator pos = range.first; pos != range.second; ++pos) + { + addr = (*pos).second->ReserveBlock (byte_size); + } + + if (addr == LLDB_INVALID_ADDRESS) + { + AllocatedBlockSP block_sp (AllocatePage (byte_size, permissions, 16, error)); + + if (block_sp) + addr = block_sp->ReserveBlock (byte_size); + } + LogSP log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); + if (log) + log->Printf ("AllocatedMemoryCache::AllocateMemory (byte_size = 0x%8.8zx, permissions = %s) => 0x%16.16llx", byte_size, GetPermissionsAsCString(permissions), (uint64_t)addr); + return addr; +} + +bool +AllocatedMemoryCache::DeallocateMemory (lldb::addr_t addr) +{ + Mutex::Locker locker (m_mutex); + + PermissionsToBlockMap::iterator pos, end = m_memory_map.end(); + bool success = false; + for (pos = m_memory_map.begin(); pos != end; ++pos) + { + if (pos->second->Contains (addr)) + { + success = pos->second->FreeBlock (addr); + break; + } + } + LogSP log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); + if (log) + log->Printf("AllocatedMemoryCache::DeallocateMemory (addr = 0x%16.16llx) => %i", (uint64_t)addr, success); + return success; +} + + diff --git a/lldb/source/Target/Process.cpp b/lldb/source/Target/Process.cpp index 88b813c640c8..70ec107a7e35 100644 --- a/lldb/source/Target/Process.cpp +++ b/lldb/source/Target/Process.cpp @@ -539,144 +539,6 @@ ProcessInstanceInfoMatch::Clear() m_match_all_users = false; } -//---------------------------------------------------------------------- -// MemoryCache constructor -//---------------------------------------------------------------------- -Process::MemoryCache::MemoryCache() : - m_cache_line_byte_size (512), - m_cache_mutex (Mutex::eMutexTypeRecursive), - m_cache () -{ -} - -//---------------------------------------------------------------------- -// Destructor -//---------------------------------------------------------------------- -Process::MemoryCache::~MemoryCache() -{ -} - -void -Process::MemoryCache::Clear() -{ - Mutex::Locker locker (m_cache_mutex); - m_cache.clear(); -} - -void -Process::MemoryCache::Flush (addr_t addr, size_t size) -{ - if (size == 0) - return; - - const uint32_t cache_line_byte_size = m_cache_line_byte_size; - const addr_t end_addr = (addr + size - 1); - const addr_t flush_start_addr = addr - (addr % cache_line_byte_size); - const addr_t flush_end_addr = end_addr - (end_addr % cache_line_byte_size); - - Mutex::Locker locker (m_cache_mutex); - if (m_cache.empty()) - return; - - assert ((flush_start_addr % cache_line_byte_size) == 0); - - for (addr_t curr_addr = flush_start_addr; curr_addr <= flush_end_addr; curr_addr += cache_line_byte_size) - { - collection::iterator pos = m_cache.find (curr_addr); - if (pos != m_cache.end()) - m_cache.erase(pos); - } -} - -size_t -Process::MemoryCache::Read -( - Process *process, - addr_t addr, - void *dst, - size_t dst_len, - Error &error -) -{ - size_t bytes_left = dst_len; - if (dst && bytes_left > 0) - { - const uint32_t cache_line_byte_size = m_cache_line_byte_size; - uint8_t *dst_buf = (uint8_t *)dst; - addr_t curr_addr = addr - (addr % cache_line_byte_size); - addr_t cache_offset = addr - curr_addr; - Mutex::Locker locker (m_cache_mutex); - - while (bytes_left > 0) - { - collection::const_iterator pos = m_cache.find (curr_addr); - collection::const_iterator end = m_cache.end (); - - if (pos != end) - { - size_t curr_read_size = cache_line_byte_size - cache_offset; - if (curr_read_size > bytes_left) - curr_read_size = bytes_left; - - memcpy (dst_buf + dst_len - bytes_left, pos->second->GetBytes() + cache_offset, curr_read_size); - - bytes_left -= curr_read_size; - curr_addr += curr_read_size + cache_offset; - cache_offset = 0; - - if (bytes_left > 0) - { - // Get sequential cache page hits - for (++pos; (pos != end) && (bytes_left > 0); ++pos) - { - assert ((curr_addr % cache_line_byte_size) == 0); - - if (pos->first != curr_addr) - break; - - curr_read_size = pos->second->GetByteSize(); - if (curr_read_size > bytes_left) - curr_read_size = bytes_left; - - memcpy (dst_buf + dst_len - bytes_left, pos->second->GetBytes(), curr_read_size); - - bytes_left -= curr_read_size; - curr_addr += curr_read_size; - - // We have a cache page that succeeded to read some bytes - // but not an entire page. If this happens, we must cap - // off how much data we are able to read... - if (pos->second->GetByteSize() != cache_line_byte_size) - return dst_len - bytes_left; - } - } - } - - // We need to read from the process - - if (bytes_left > 0) - { - assert ((curr_addr % cache_line_byte_size) == 0); - std::auto_ptr data_buffer_heap_ap(new DataBufferHeap (cache_line_byte_size, 0)); - size_t process_bytes_read = process->ReadMemoryFromInferior (curr_addr, - data_buffer_heap_ap->GetBytes(), - data_buffer_heap_ap->GetByteSize(), - error); - if (process_bytes_read == 0) - return dst_len - bytes_left; - - if (process_bytes_read != cache_line_byte_size) - data_buffer_heap_ap->SetByteSize (process_bytes_read); - m_cache[curr_addr] = DataBufferSP (data_buffer_heap_ap.release()); - // We have read data and put it into the cache, continue through the - // loop again to get the data out of the cache... - } - } - } - - return dst_len - bytes_left; -} - Process* Process::FindPlugin (Target &target, const char *plugin_name, Listener &listener) { @@ -735,7 +597,8 @@ Process::Process(Target &target, Listener &listener) : m_stdio_communication ("process.stdio"), m_stdio_communication_mutex (Mutex::eMutexTypeRecursive), m_stdout_data (), - m_memory_cache (), + m_memory_cache (*this), + m_allocated_memory_cache (*this), m_next_event_action_ap() { UpdateInstanceName(); @@ -1759,7 +1622,7 @@ size_t Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error) { // Memory caching enabled, no verification - return m_memory_cache.Read (this, addr, buf, size, error); + return m_memory_cache.Read (addr, buf, size, error); } #endif // #else for #if defined (VERIFY_MEMORY_READS) @@ -1968,29 +1831,36 @@ Process::WriteMemory (addr_t addr, const void *buf, size_t size, Error &error) return bytes_written; } - +#define USE_ALLOCATE_MEMORY_CACHE 1 addr_t Process::AllocateMemory(size_t size, uint32_t permissions, Error &error) { - // Fixme: we should track the blocks we've allocated, and clean them up... - // We could even do our own allocator here if that ends up being more efficient. +#if defined (USE_ALLOCATE_MEMORY_CACHE) + return m_allocated_memory_cache.AllocateMemory(size, permissions, error); +#else addr_t allocated_addr = DoAllocateMemory (size, permissions, error); LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); if (log) - log->Printf("Process::AllocateMemory(size=%4zu, permissions=%c%c%c) => 0x%16.16llx (m_stop_id = %u)", + log->Printf("Process::AllocateMemory(size=%4zu, permissions=%s) => 0x%16.16llx (m_stop_id = %u)", size, - permissions & ePermissionsReadable ? 'r' : '-', - permissions & ePermissionsWritable ? 'w' : '-', - permissions & ePermissionsExecutable ? 'x' : '-', + GetPermissionsAsCString (permissions), (uint64_t)allocated_addr, m_stop_id); return allocated_addr; +#endif } Error Process::DeallocateMemory (addr_t ptr) { - Error error(DoDeallocateMemory (ptr)); + Error error; +#if defined (USE_ALLOCATE_MEMORY_CACHE) + if (!m_allocated_memory_cache.DeallocateMemory(ptr)) + { + error.SetErrorStringWithFormat ("deallocation of memory at 0x%llx failed.", (uint64_t)ptr); + } +#else + error = DoDeallocateMemory (ptr); LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); if (log) @@ -1998,6 +1868,7 @@ Process::DeallocateMemory (addr_t ptr) ptr, error.AsCString("SUCCESS"), m_stop_id); +#endif return error; }