Skip to content

Implement retain on Windows. - #1545

Merged
interwq merged 3 commits into
jemalloc:devfrom
interwq:retain
Jul 24, 2019
Merged

interwq merged 3 commits into
jemalloc:devfrom
interwq:retain

Conversation

@interwq

@interwq interwq commented Jul 12, 2019 •

Copy link
Copy Markdown
Contributor

The VirtualAlloc and VirtualFree APIs are different because MEM_DECOMMIT cannot
be used across multiple VirtualAlloc regions. To properly support decommit, only
allow merge / split within the same region -- this is done by not merging extents
with different serial numbers (w/ opt.retain).

@interwq
interwq force-pushed the retain branch 8 times, most recently from 87e6663 to a145d2b Compare July 15, 2019 19:22
@interwq interwq changed the title [testing] WIP -- test retain on Windows. Implement retain on Windows. Jul 15, 2019
@interwq interwq mentioned this pull request Jul 15, 2019
@interwq

interwq commented Jul 18, 2019

Copy link
Copy Markdown
Contributor Author

@Ignition, @frederik-h, @santagada, @cytoo, @j0t -- FYI that we plan to do a 5.2.1 release in a couple of weeks, for the Windows VM leak fix in this patch. The issues you guys experienced before on Windows are likely all related to the issue here; any chance you could help test / benchmark the changes here?

@tmedioni

Copy link
Copy Markdown

Will do, but I can't guarantee that I'll be able to do proper testing in this timeframe.

@Ignition

Ignition commented Jul 19, 2019 •

Copy link
Copy Markdown
Contributor

The way I built for windows retain was not default true. Even with --with-malloc-conf=retain:true, malloc_stats_print(NULL, NULL, NULL); still reports opt.retain: false. You can have a copy of the conan recipe I use to double check nothing odd is going on.

@Ignition

Copy link
Copy Markdown
Contributor

@Ignition

Copy link
Copy Markdown
Contributor

Ok, never mind. It was due to my conan build cache having an old build. I now get opt.retain: true, initial testing perfectly fine.

Comment thread src/extent.c Outdated
return true;
/*
* When coalesce is not always allowed (Windows), only merge
* extents with identical sn (i.e. from the same VirtualAlloc

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.

Can you include some back-of-the envelope math to convince me / the reader that "same sn == same VirtualAlloc call"? This is the first time that we're making that fact a correctness issue instead of just a QoI one, and that makes me somewhat nervous.

@davidtgoldblatt

Copy link
Copy Markdown
Contributor

You know, it occurs to me that we can use two rtree bits to indicate "same VirtualAlloc" instead of using the extent sn. Have the bits be 0 if the extent is in the interior of a mapping, 1 if it's at the beginning of a mapping, and 2 if it's at the end of a mapping. That would let us avoid trying to play games with serial numbers entirely.

@davidtgoldblatt

Copy link
Copy Markdown
Contributor

Actually, we don't even need to take it from the rtree; it can just live in the extent state itself. We can take two bits from the serial number (or squirrel them away into the low bits of a pointer, etc.), and then we get a firm correctness guarantee regardless of serial number overflow.

@interwq

interwq commented Jul 22, 2019

Copy link
Copy Markdown
Contributor Author

Actually, we don't even need to take it from the rtree; it can just live in the extent state itself. We can take two bits from the serial number (or squirrel them away into the low bits of a pointer, etc.), and then we get a firm correctness guarantee regardless of serial number overflow.

Yeah realized we can further simplify by only tracking the is_head state, and disallow merging if the second extent (higher addr) is a head extent. Will update soon.

@j0t

j0t commented Jul 23, 2019

Copy link
Copy Markdown

Just have tested my app with a library built from latest interwq:retain in CYGWIN. At first sight, all works fine with default JE_MALLOC_CONF settings. The statistics shows a great amount of retained bytes and mapped quantity is not growing anymore.

Allocated: 1321568, active: 8912896, metadata: 4691552 (n_thp 0), resident: 48103424, mapped: 58261504, retained: 238485504

However, some jemalloc tests didn't pass. Don't know yet is it a specific CYGWIN issue or something else. Will try to found out more later.

@davidtgoldblatt davidtgoldblatt 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.

Can you squash down? It's a little harder to review this way.

Comment thread src/extent.c Outdated
}

/* See comments in extent_merge_default_impl(). */
return opt_retain ? false : true;

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.

Why does extent splitting return an error only if retain is off? I see why we might want to treat this differently, but erroring out on splitting seems like a weird way to do so.

Also, I think this whole thing is most clearly written as: return !(maps_coalesce || opt_retain).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Will refactor this. I wanted to separate the Windows logic and add some comments. Let me make that happen.

I think we want to consider the returning true as "not allowed to split" instead of a real error; without retain, purging does MEM_RELEASE which only accepts the whole region. In that case we don't allow split / merge. The user custom hooks might return true as a way to say not-allowed as well, e.g. in hhvm for arenas owning reserved huge pages (but with regular pages as back up), they may check if an extent is part of the huge pages and disallow purging.

@interwq
interwq force-pushed the retain branch 2 times, most recently from bfac669 to eb9460a Compare July 23, 2019 18:13
@interwq

interwq commented Jul 23, 2019

Copy link
Copy Markdown
Contributor Author

@Ignition, @j0t : many thanks for help testing!

The high retained bytes might be expected, normally it should be a couple times of the peak RSS. Also the total VM size won't decrease because only decommit will happen instead of unmap. It would be helpful if you can share the full stats too.

@interwq

interwq commented Jul 23, 2019

Copy link
Copy Markdown
Contributor Author

@j0t : which tests are failing for you? You can test against the dev branch to verify if it's related to retain here.

@j0t

j0t commented Jul 23, 2019

Copy link
Copy Markdown

@Ignition, @j0t : many thanks for help testing!

You are welcome! It's great that this issue has been resolved finally.

It would be helpful if you can share the full stats too.

Here are two stats, dev as baseline and retain:
dev
dev.log
retain
retain.log

@j0t

j0t commented Jul 23, 2019

Copy link
Copy Markdown

@j0t : which tests are failing for you? You can test against the dev branch to verify if it's related to retain here.

No, the same failures are originated from dev. They are specific to CYGWIN and related to prof_backtrace() in prof.c and thread cleanup machinery in tsd_win.h. BTW, I did managed to fix them, but it could require a separate PR, I believe...

Comment thread src/extent.c Resolved
Comment thread src/extent.c Resolved
Comment thread src/extent.c Resolved
interwq added 3 commits July 23, 2019 16:25
The VirtualAlloc and VirtualFree APIs are different because MEM_DECOMMIT cannot
be used across multiple VirtualAlloc regions.  To properly support decommit,
only allow merge / split within the same region -- this is done by tracking the
"is_head" state of extents and not merging cross-region.

Add a new state is_head (only relevant for retain && !maps_coalesce), which is
true for the first extent in each VirtualAlloc region.  Determine if two extents
can be merged based on the head state, and use serial numbers for sanity checks.
This can only happen on Windows and with opt.retain disabled (which isn't the
default).  The solution is suboptimal, however not a common case as retain is
the long term plan for all platforms anyway.
@interwq
interwq merged commit 57dbab5 into jemalloc:dev Jul 24, 2019
@interwq
interwq deleted the retain branch July 24, 2019 05:19
@interwq interwq mentioned this pull request Jul 24, 2019
@interwq

interwq commented Jul 24, 2019 •

Copy link
Copy Markdown
Contributor Author

@j0t : thanks again for sharing the details! I checked the stats and they looked good; also added a new counter in #1553 to detect the problematic behavior just in case.

For the profiling related fix, please feel free to submit when ready; we have a 5.2.1 release coming in a couple of weeks, if it's a small-ish change we can still include it.

@j0t

j0t commented Jul 25, 2019

Copy link
Copy Markdown

@j0t : thanks again for sharing the details! I checked the stats and they looked good; also added a new counter in #1553 to detect the problematic behavior just in case.

@interwq, oh, thanks, that's nice! Will check this counter later.

For the profiling related fix, please feel free to submit when ready; we have a 5.2.1 release coming in a couple of weeks, if it's a small-ish change we can still include it.

Currently I'm struggling with some build/makefile issues related to CYGWIN specific, carefully trying
to keep everything in buildable state. (In particular CYGWIN has different dll naming conventions and requires import libraries to be present for proper linking).

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.

6 participants