================
@@ -193,103 +293,45 @@ size_t MemoryCache::Read(addr_t addr, void *dst, size_t 
dst_len,
     return 0;
 
   std::lock_guard<std::recursive_mutex> guard(m_mutex);
-
+  addr_t invalid_addr = LLDB_INVALID_ADDRESS;
   if (const InvalidRanges::Entry *invalid =
           m_invalid_ranges.FindEntryThatIntersects(
               InvalidRanges::Entry(addr, dst_len))) {
-    const addr_t invalid_addr = invalid->GetRangeBase();
+    invalid_addr = invalid->GetRangeBase();
     error = Status::FromErrorStringWithFormat(
         "memory read failed for 0x%" PRIx64, invalid_addr);
     if (invalid_addr <= addr)
       return 0;
     dst_len = invalid_addr - addr;
   }
 
-  // Check the L1 cache for a range that contains the entire memory read.
-  // L1 cache contains chunks of memory that are not required to be the size of
-  // an L2 cache line. We avoid trying to do partial reads from the L1 cache to
-  // simplify the implementation.
-  if (const uint8_t *l1_data = FindL1CacheEntry(addr, dst_len)) {
-    memcpy(dst, l1_data, dst_len);
+  size_t bytes_from_cache = ReadFromCaches(addr, dst, dst_len);
+  if (bytes_from_cache == dst_len)
     return dst_len;
-  }
 
-  // If the size of the read is greater than the size of an L2 cache line, 
we'll
-  // just read from the inferior. If that read is successful, we'll cache what
-  // we read in the L1 cache for future use.
-  if (dst_len > m_L2_cache_line_byte_size) {
-    size_t bytes_read =
-        m_process.ReadMemoryFromInferior(addr, dst, dst_len, error);
-    if (bytes_read > 0)
-      AddCacheData(addr, dst, bytes_read);
-    return bytes_read;
+  addr_t read_addr = addr + bytes_from_cache;
+  addr_t read_end = addr + dst_len;
+  // A request hits the invalid range above, don't grow.
+  if (invalid_addr == LLDB_INVALID_ADDRESS) {
+    const AddrRange grown =
+        GrowReadRange(read_addr, read_end, bytes_from_cache);
+    read_addr = grown.GetRangeBase();
+    read_end = grown.GetRangeEnd();
   }
 
-  // If the size of the read fits inside one L2 cache line, we'll try reading
-  // from the L2 cache. Note that if the range of memory we're reading sits
-  // between two contiguous cache lines, we'll touch two cache lines instead of
-  // just one.
-
-  // We're going to have all of our loads and reads be cache line aligned.
-  addr_t cache_line_offset = addr % m_L2_cache_line_byte_size;
-  addr_t cache_line_base_addr = addr - cache_line_offset;
-  DataBufferSP first_cache_line = GetL2CacheLine(cache_line_base_addr, error);
-  // If we get nothing, then the read to the inferior likely failed. Nothing to
-  // do here.
-  if (!first_cache_line)
-    return 0;
+  std::vector<uint8_t> read_buf(read_end - read_addr);
+  const size_t bytes_from_inferior = m_process.ReadMemoryFromInferior(
+      read_addr, read_buf.data(), read_buf.size(), error);
+  if (bytes_from_inferior == 0)
+    return bytes_from_cache;
 
-  // If the cache line was not filled out completely and the offset is greater
-  // than what we have available, we can't do anything further here.
-  if (cache_line_offset >= first_cache_line->GetByteSize())
-    return 0;
-
-  uint8_t *dst_buf = (uint8_t *)dst;
-  size_t bytes_left = dst_len;
-  size_t read_size = first_cache_line->GetByteSize() - cache_line_offset;
-  if (read_size > bytes_left)
-    read_size = bytes_left;
-
-  memcpy(dst_buf + dst_len - bytes_left,
-         first_cache_line->GetBytes() + cache_line_offset, read_size);
-  bytes_left -= read_size;
-
-  // If the cache line was not filled out completely and we still have data to
-  // read, we can't do anything further.
-  if (first_cache_line->GetByteSize() < m_L2_cache_line_byte_size &&
-      bytes_left > 0)
-    return dst_len - bytes_left;
-
-  // We'll hit this scenario if our read straddles two cache lines.
-  if (bytes_left > 0) {
-    cache_line_base_addr += m_L2_cache_line_byte_size;
-
-    // FIXME: Until we are able to more thoroughly check for invalid ranges, we
-    // will have to check the second line to see if it is in an invalid range 
as
-    // well. See the check near the beginning of the function for more details.
-    if (m_invalid_ranges.FindEntryThatContains(cache_line_base_addr)) {
-      error = Status::FromErrorStringWithFormat(
-          "memory read failed for 0x%" PRIx64, cache_line_base_addr);
-      return dst_len - bytes_left;
-    }
-
-    DataBufferSP second_cache_line =
-        GetL2CacheLine(cache_line_base_addr, error);
-    if (!second_cache_line)
-      return dst_len - bytes_left;
-
-    read_size = bytes_left;
-    if (read_size > second_cache_line->GetByteSize())
-      read_size = second_cache_line->GetByteSize();
-
-    memcpy(dst_buf + dst_len - bytes_left, second_cache_line->GetBytes(),
-           read_size);
-    bytes_left -= read_size;
-
-    return dst_len - bytes_left;
-  }
+  AddCacheData(read_addr, read_buf.data(), bytes_from_inferior);
 
-  return dst_len;
+  // The grown or clipped fetch may not align with what the caller asked for,
+  // so pull back only the portion contiguous with what dst already holds.
+  uint8_t *dst_tail = static_cast<uint8_t *>(dst) + bytes_from_cache;
+  return bytes_from_cache + ReadFromCaches(addr + bytes_from_cache, dst_tail,
+                                           dst_len - bytes_from_cache);
----------------
felipepiovezan wrote:

are we re-reading from the cache what we just read from the process? Why not 
just pass the `dst` in the call to `m_process.ReadMemoryFromInferior`?

https://github.com/llvm/llvm-project/pull/222688
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to