Skip to content

Avoid ats_malloc in unmarshal - #4874

Merged
zwoop merged 1 commit into
apache:masterfrom
scw00:refactor_marshal_and_unmarshal
Feb 13, 2019
Merged

zwoop merged 1 commit into
apache:masterfrom
scw00:refactor_marshal_and_unmarshal

Conversation

@scw00

@scw00 scw00 commented Jan 28, 2019

Copy link
Copy Markdown
Member

After #84fa7a4d7123c86e6debc42bac2684945769bb3c. We allocate memory from system to hold the fragment table which exceed HTTPCacheAlt::N_INTEGRAL_FRAG_OFFSETS.

When cache is writing, The HTTPCacheAlt is writable (m_writable == true). So we can deallocate the memory when writer closed .

But when we are on read side. The memory is never deallocate. Because m_writable is never be true. Also this un-marshalled data can be cached in RamCache and be knocked out without any deallocate operations.

So there is no reason to keep a separate memory , we can just used the data we read from disk or memory.

#2436 #4872

@scw00 scw00 added this to the 9.0.0 milestone Jan 28, 2019
@scw00 scw00 self-assigned this Jan 28, 2019
@scw00
scw00 requested a review from SolidWallOfCode January 28, 2019 07:56

@zwoop zwoop left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@SolidWallOfCode

Copy link
Copy Markdown
Member

What happens in HTTPInfo::destroy() when it frees the fragment table?

@zwoop

zwoop commented Jan 28, 2019

Copy link
Copy Markdown
Contributor

This means we can close #4872 ?

@scw00

scw00 commented Jan 29, 2019

Copy link
Copy Markdown
Member Author

@SolidWallOfCode The key is HTTPInfo::m_writeable. The alt is only destroy when m_writeable is true.

inline void
HTTPInfo::destroy()
{
if (m_alt) {
if (m_alt->m_writeable) {
m_alt->destroy();
} else if (m_alt->m_ext_buffer) {
if (m_alt->m_ext_buffer->refcount_dec() == 0) {
m_alt->m_ext_buffer->free();
}
}
}
clear();
}

But on read side, The HTTPInfo::m_writeable is always false. So the memory can not be freed. And we can not simply set this flag, because the un-marshaled data could be push to RamCache. When another request is coming, The CacheVC will get this data from RamCache without un-marshaling.

ink_assert(vol->mutex->thread_holding == this_ethread());
int64_t o = dir_offset(&dir);
int ram_hit_state = vol->ram_cache->get(read_key, &buf, (uint32_t)(o >> 32), (uint32_t)o);
f.compressed_in_ram = (ram_hit_state > RAM_HIT_COMPRESS_NONE) ? 1 : 0;
if (ram_hit_state >= RAM_HIT_COMPRESS_NONE) {
goto LramHit;
}
// check if it was read in the last open_read call
if (*read_key == vol->first_fragment_key && dir_offset(&dir) == vol->first_fragment_offset) {
buf = vol->first_fragment_data;
goto LmemHit;
}

@scw00

scw00 commented Jan 29, 2019

Copy link
Copy Markdown
Member Author

@zwoop , Yes we can close that pr if this pr accepted.

@zwoop zwoop added the OnDocs This is for PR currently running, or will run, on the Docs ATS server label Jan 29, 2019
@scw00

scw00 commented Jan 29, 2019 •

Copy link
Copy Markdown
Member Author
diff --git a/proxy/hdrs/HTTP.cc b/proxy/hdrs/HTTP.cc
index 3858bd0..b00c1cf 100644
--- a/proxy/hdrs/HTTP.cc
+++ b/proxy/hdrs/HTTP.cc
@@ -30,6 +30,7 @@
 #include "HTTP.h"
 #include "HdrToken.h"
 #include "tscore/Diags.h"
+#include <atomic>

 /***********************************************************************
  *                                                                     *
@@ -151,6 +152,21 @@ Arena *const HTTPHdr::USE_HDR_HEAP_MAGIC = reinterpret_cast<Arena *>(1);
  *                                                                     *
  ***********************************************************************/

+#define PRINT_COUNT(incr) print_count(__FUNCTION__, incr)
+
+void
+print_count(const char *func, bool incr)
+{
+  static std::atomic<int> count;
+  if (incr) {
+    count++;
+  } else {
+    count--;
+  }
+
+  printf("count %d\n", count.load());
+}
+
 inline static int
 is_digit(char c)
 {
@@ -1907,6 +1923,7 @@ HTTPCacheAlt::destroy()
   m_response_hdr.destroy();
   m_frag_offset_count = 0;
   if (m_frag_offsets && m_frag_offsets != m_integral_frag_offsets) {
+    PRINT_COUNT(false);
     ats_free(m_frag_offsets);
     m_frag_offsets = nullptr;
   }
@@ -1953,6 +1970,7 @@ HTTPCacheAlt::copy_frag_offsets_from(HTTPCacheAlt *src)
       while (bcount < m_frag_offset_count) {
         bcount *= 2;
       }
+      PRINT_COUNT(true);
       m_frag_offsets = static_cast<FragOffset *>(ats_malloc(sizeof(FragOffset) * bcount));
     } else {
       m_frag_offsets = m_integral_frag_offsets;
@@ -2121,6 +2139,7 @@ HTTPInfo::unmarshal(char *buf, int len, RefCountObj *block_ref)
     while (bcount < alt->m_frag_offset_count) {
       bcount *= 2;
     }
+    PRINT_COUNT(true);
     alt->m_frag_offsets =
       static_cast<FragOffset *>(ats_malloc(bcount * sizeof(FragOffset))); // WRONG - must round up to next power of 2.
     memcpy(alt->m_frag_offsets, alt->m_integral_frag_offsets, sizeof(alt->m_integral_frag_offsets));
@@ -2278,9 +2297,11 @@ HTTPInfo::push_frag_offset(FragOffset offset)
              0 == (m_alt->m_frag_offset_count & (m_alt->m_frag_offset_count - 1))) {
     // need more space than in integral storage and we're at an upgrade
     // size (power of 2).
+    PRINT_COUNT(true);
     FragOffset *nf = static_cast<FragOffset *>(ats_malloc(sizeof(FragOffset) * (m_alt->m_frag_offset_count * 2)));
     memcpy(nf, m_alt->m_frag_offsets, sizeof(FragOffset) * m_alt->m_frag_offset_count);
     if (m_alt->m_frag_offsets != m_alt->m_integral_frag_offsets) {
+      PRINT_COUNT(false);
       ats_free(m_alt->m_frag_offsets);
     }
     m_alt->m_frag_offsets = nf;

I use this hack to detect this leaking. It is easily to see the monotonic increasing of count even if I disable RamCache.

@zwoop

zwoop commented Jan 29, 2019

Copy link
Copy Markdown
Contributor

I'm testing this on Docs. Would this be a candidate for 7.x and 8.x as well ?

@scw00

scw00 commented Jan 29, 2019

Copy link
Copy Markdown
Member Author

Yes, It is a candidate for 7.x and 8.x. Also 6.x need this

@zwoop

zwoop commented Feb 12, 2019

Copy link
Copy Markdown
Contributor

Should we land this? And close #4872 ?

@scw00

scw00 commented Feb 13, 2019

Copy link
Copy Markdown
Member Author

Yes , if @SolidWallOfCode approve this .

@zwoop
zwoop merged commit 42ca943 into apache:master Feb 13, 2019
@scw00

scw00 commented Feb 13, 2019

Copy link
Copy Markdown
Member Author

This case is discovered by by @WolfgangBai . Thanks a lot !

@scw00
scw00 deleted the refactor_marshal_and_unmarshal branch February 13, 2019 08:12
@zwoop zwoop removed OnDocs This is for PR currently running, or will run, on the Docs ATS server labels Feb 13, 2019
@scw00 scw00 mentioned this pull request Feb 24, 2019
@bryancall bryancall modified the milestones: 9.0.0, 8.1.0 Mar 19, 2020
@bryancall

Copy link
Copy Markdown
Contributor

Cherry picked into 8.1.x for the 8.1.0 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants